% BEGIN LICENSE BLOCK
% Version: CMPL 1.1
%
% The contents of this file are subject to the Cisco-style Mozilla Public
% License Version 1.1 (the "License"); you may not use this file except
% in compliance with the License.  You may obtain a copy of the License
% at www.eclipse-clp.org/license.
%
% Software distributed under the License is distributed on an "AS IS"
% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
% the License for the specific language governing rights and limitations
% under the License.
%
% The Original Code is  The ECLiPSe Constraint Logic Programming System.
% The Initial Developer of the Original Code is  Cisco Systems, Inc.
% Portions created by the Initial Developer are
% Copyright (C) 2006 Cisco Systems, Inc.  All Rights Reserved.
%
% Contributor(s):
%
% END LICENSE BLOCK

%----------------------------------------------------------------------
\chapter{The Integer Sets Library}
\label{icsets}
\index{library!ic_sets|(}
%HEVEA\cutdef[1]{section}
%----------------------------------------------------------------------

%----------------------------------------------------------------------
\section{Why Sets}
%----------------------------------------------------------------------

\index{finite sets}
The {\em ic_sets} library is a solver for constraints over the domain
of finite sets of integers.
Modelling with sets is useful for problems where one is not
interested in each item as a specific individual, but in a
collection of item where no specific distinction is made and thus
where symmetries among the element values need to be avoided.

%----------------------------------------------------------------------
\section{Finite Sets of Integers}
%----------------------------------------------------------------------

In the context of the {\em ic_sets} library, (ground) integer sets are
simply sorted, duplicate-free lists of integers e.g.
\begin{quote}\begin{verbatim}
SetOfThree = [1,3,7]
EmptySet = []
\end{verbatim}\end{quote}
Lists which contain non-integers, are unsorted or contain duplicates,
are not sets in the sense of this library.

%----------------------------------------------------------------------
\section{Set Variables}
\index{set variable}
%----------------------------------------------------------------------

%\subsection{Declaring}
Set variables are variables which can eventually take a ground integer
set as their value.  They are characterized by a lower bound (the set
of elements that are definitely in the set) and an upper bound (the
set of elements that may be in the set).  A set variable can be
declared as follows:
\begin{quote}\begin{verbatim}
SetVar :: []..[1,2,3,4,5,6,7]
\end{verbatim}\end{quote}
%If the lower bound is the empty set (like in this case) this can be written as
%\begin{verbatim}
%        SetVar subset [1,2,3,4,5,6,7]
%\end{verbatim}
If the lower bound is the empty set and the upper bound is a set of
consecutive integers, one can also declare it like
\begin{quote}\begin{verbatim}
intset(SetVar, 1, 7)
\end{verbatim}\end{quote}
which is equivalent to the above.
\quickref{Declaring Set Variables}{
\begin{description}
\item[\biptxtrefni{?Set :: ++Lwb..++Upb}{::/2!ic_sets}{../bips/lib/ic_sets/NN-2.html}]
\index{::/2@\texttt{::/2}!ic_sets} Set is an integer set within the given bounds
\item[\biptxtref{intset(?Set, +Min, +Max)}{intset/3}{../bips/lib/ic_sets/intset-3.html}]
         Set is a set containing numbers between Min and Max
\item[\biptxtref{intsets(?Sets, ?N, +Min, +Max)}{intsets/4}{../bips/lib/ic_sets/intsets-4.html}]
         Sets is a list of N sets containing numbers between Min and Max
\end{description}
}

The system prints set variables in a particular way, for instance:
\begin{quote}\begin{verbatim}
?- lib(ic_sets).
?- X :: [2,3]..[1,2,3,4].
X = X{[2, 3] \/ ([] .. [1, 4]) : _308{[2 .. 4]}}
\end{verbatim}\end{quote}
The curly brackets contain the description of the current domain
of the set variable in the form of
\begin{enumerate}
\item the lower bound of the set (values which definitely are in the set)
\item the union symbol \verb.\/.
\item the set of optional values (which may or may not be in the set)
\item a colon
\item a finite domain variable indicating the admissible cardinality for the set
\end{enumerate}

%----------------------------------------------------------------------
\section{Constraints}
%----------------------------------------------------------------------

\index{membership constraint}
\index{cardinality constraint}
The constraints that {\em ic_sets} implements are the usual relations
over sets.
The membership (in/2, notin/2) and cardinality constraints
(\#/2) establish
relationships between set variables and integer variables:
\quickref{Membership and Cardinality Constraints}{
\begin{description}
\item[\biptxtrefni{?X in ?Set}{in/2!ic_sets}{../bips/lib/ic_sets/in-2.html}]
         \index{in/2@\texttt{in/2}!ic_sets} The integer X is member of the integer set Set
\item[\biptxtrefni{?X notin ?Set}{notin/2!ic_sets}{../bips/lib/ic_sets/notin-2.html}]
         \index{notin/2@\texttt{notin/2}!ic_sets} The integer X is not a member of the integer set Set
%\item[\biptxtref{membership_booleans(?Set, ?BoolArr)}{membership_booleans/2!ic_sets}{../bips/lib/ic_sets/membership_booleans-2.html}]
%         BoolArr is an array of booleans describing Set
\item[\biptxtrefni{\#(?Set, ?Card)}{\#/2!ic_sets}{../bips/lib/ic_sets/H-2.html}]
         \index{\#/2@\texttt{\#/2}!ic_sets} Card is the cardinality of the integer set Set
\end{description}
}
\begin{quote}\begin{verbatim}
?- X ::[]..[1, 2, 3], 2 in X, 3 in X, #(X, 2).
X = [2, 3]
Yes (0.01s cpu)

?- X :: []..[1, 2, 3, 4], 3 in X, 4 notin X.
X = X{[3] \/ ([] .. [1, 2]) : _2161{1 .. 3}}
Yes (0.00s cpu)
\end{verbatim}\end{quote}
Possible constraints between two sets are equality, inclusion/subset
and disjointness:
\index{inclusion constraint}
\index{disjointness constraint}
\index{subset constraint}
\begin{quote}\begin{verbatim}
?- X subset [1, 2, 3, 4].
X = X{([] .. [1, 2, 3, 4]) : _2139{0 .. 4}}
Yes (0.00s cpu)

?- X :: []..[1, 2, 3, 4], Y :: []..[3, 4, 5, 6], X subset Y.
X = X{([] .. [3, 4]) : _2176{0 .. 2}}
Y = Y{([] .. [3, 4, 5, 6]) : _2367{0 .. 4}}
There are 4 delayed goals.
Yes (0.00s cpu)

?- X :: [2] .. [1, 2, 3, 4], Y :: [3] .. [1, 2, 3, 4], X disjoint Y.
X = X{[2] \/ ([] .. [1, 4]) : _2118{1 .. 3}}
Y = Y{[3] \/ ([] .. [1, 4]) : _2213{1 .. 3}}
There are 2 delayed goals.
Yes (0.00s cpu)
\end{verbatim}\end{quote}
\quickref{Basic Set Relations}{
\begin{description}
\item[\biptxtrefni{?Set1 sameset ?Set2}{sameset/2!ic_sets}{../bips/lib/ic_sets/sameset-2.html}]
         \index{sameset/2@\texttt{sameset/2}!ic_sets} The sets Set1 and Set2 are equal
\item[\biptxtrefni{?Set1 disjoint ?Set2}{disjoint/2!ic_sets}{../bips/lib/ic_sets/disjoint-2.html}]
         \index{disjoint/2@\texttt{disjoint/2}!ic_sets} The integer sets Set1 and Set2 are disjoint
\item[\biptxtrefni{?Set1 includes ?Set2}{includes/2!ic_sets}{../bips/lib/ic_sets/includes-2.html}]
         \index{includes/2@\texttt{includes/2}!ic_sets} Set1 includes (is a superset) of the integer set Set2
\item[\biptxtrefni{?Set1 subset ?Set2}{subset/2!ic_sets}{../bips/lib/ic_sets/subset-2.html}]
         \index{subset/2@\texttt{subset/2}!ic_sets} Set1 is a (non-strict) subset of the integer set Set2
\item[\biptxtrefni{intersection(?Set1, ?Set2, ?Set3)}{intersection/3!ic_sets}{../bips/lib/ic_sets/intersection-3.html}]
         \index{intersection/3@\texttt{intersection/3}!ic_sets} Set3 is the intersection of the integer sets Set1 and Set2
\item[\biptxtrefni{union(?Set1, ?Set2, ?Set3)}{union/3!ic_sets}{../bips/lib/ic_sets/union-3.html}]
         \index{union/3@\texttt{union/3}!ic_sets} Set3 is the union of the integer sets Set1 and Set2
\item[\biptxtrefni{difference(?Set1, ?Set2, ?Set3)}{difference/3!ic_sets}{../bips/lib/ic_sets/difference-3.html}]
         Set3 is the difference of the integer sets Set1 and Set2
\item[\biptxtrefni{symdiff(?Set1, ?Set2, ?Set3)}{symdiff/3!ic_sets}{../bips/lib/ic_sets/symdiff-3.html}]
         \index{symdiff/3@\texttt{symdiff/3}!ic_sets} Set3 is the symmetric difference of the integer sets Set1 and Set2
\end{description}
}
Possible constraints between three sets are for example
intersection, union, difference and symmetric difference.
For example:
\begin{quote}\begin{verbatim}
?- X :: [2, 3] .. [1, 2, 3, 4],
   Y :: [3, 4] .. [3, 4, 5, 6],
   ic_sets : intersection(X, Y, Z).
X = X{[2, 3] \/ ([] .. [1, 4]) : _2127{2 .. 4}}
Y = Y{[3, 4] \/ ([] .. [5, 6]) : _2222{2 .. 4}}
Z = Z{[3] \/ ([] .. [4]) : _2302{[1, 2]}}
There are 6 delayed goals.
Yes (0.00s cpu)
\end{verbatim}\end{quote}
\Note{Note that we needed to qualify the intersection/3 constraint with
the {\em ic_sets} module prefix because of a name conflict with a predicate from
the {\em lists} library of the same name.}
\Note{Note the lack of a complement constraint: this is because the complement
of a finite set is infinite and cannot be represented. Complements can be
modelled using an explicit universal set and a difference constraint.}

\ignore{
\subsection{Domain Access}

\begin{description}
\item[\biptxtref{potential_members(?Set, -List)}{potential_members/2}{../bips/lib/ic_sets/potential_members-2.html}]
         List is the list of elements of whose membership in Set is currently uncertain
\item[\biptxtref{set_range(?Set, -Lwb, -Upb)}{set_range/3}{../bips/lib/ic_sets/set_range-3.html}]
         Lwb and Upb are the current lower and upper bounds on Set
\end{description}
}

Finally, there are a number of n-ary constraints that apply to lists of sets:
disjointness, union and intersection. For example:
\quickref{N-ary Set Relations}{
\begin{description}
\item[\biptxtref{all_disjoint(+Sets)}{all_disjoint/1}{../bips/lib/ic_sets/all_disjoint-1.html}]
         Sets is a list of integers sets which are all disjoint
\item[\biptxtref{all_union(+Sets, ?SetUnion)}{all_union/2}{../bips/lib/ic_sets/all_union-2.html}]
         SetUnion is the union of all the sets in the list Sets
\item[\biptxtref{all_intersection(+Sets, ?SetIntersection)}{all_intersection/2}{../bips/lib/ic_sets/all_intersection-2.html}]
         SetIntersection is the intersection of all the sets in the list Sets
\end{description}
}
\begin{quote}\begin{verbatim}
?- intsets(Sets, 5, 1, 5), all_intersection(Sets, Common).
Sets = [_2079{([] .. [1, 2, 3, 4, 5]) : _2055{0 .. 5}}, ... ]
Common = Common{([] .. [1, 2, 3, 4, 5]) : _3083{0 .. 5}}
There are 24 delayed goals.
Yes (0.00s cpu)
\end{verbatim}\end{quote}

%----------------------------------------------------------------------
%\section{Set Expressions}
%----------------------------------------------------------------------

In most positions where a set or set variable is expected one can also
use a set expression. A set expression is composed from ground sets
(integer lists), set variables, and the following set operators:
\begin{quote}\begin{verbatim}
Set1 /\ Set2       % intersection
Set1 \/ Set2       % union
Set1 \ Set2        % difference
\end{verbatim}\end{quote}
When such set expressions occur, they are translated into auxiliary
\bipref{intersection/3}{../bips/lib/ic_sets/intersection-3.html},
\bipref{union/3}{../bips/lib/ic_sets/union-3.html} and
\bipref{difference/3}{../bips/lib/ic_sets/difference-3.html}
constraints, respectively.

%----------------------------------------------------------------------
\section{Search Support}
%----------------------------------------------------------------------

The
\bipref{insetdomain/4}{../bips/lib/ic_sets/insetdomain-4.html}
predicate can be used to enumerate all ground instantiations of a set
variable, much like
\bipref{indomain/1}{../bips/lib/ic/indomain-1.html}
in the finite domain case.
Here is an example of the default enumeration strategy:
\begin{quote}\begin{verbatim}
?-  X::[]..[1,2,3], insetdomain(X,_,_,_), writeln(X), fail.
[1, 2, 3]
[1, 2]
[1, 3]
[1]
[2, 3]
[2]
[3]
[]
\end{verbatim}\end{quote}
Other enumeration strategies can be selected (see the Reference Manual
on insetdomain/4).

%\begin{description}
%\item[\biptxtref{insetdomain(?Set, ?CardSel, ?ElemSel, ?Order)}{insetdomain/4}{../bips/lib/ic_sets/insetdomain-4.html}]
%         Instantiate Set to a possible value
%\end{description}

%----------------------------------------------------------------------
\section{Example}
%----------------------------------------------------------------------

\index{Steiner problem}
The following program computes so-called Steiner triplets.
The problem is to compute triplets of numbers between 1 and N,
such that any two triplets have at most one element in common.
\begin{code}
:- lib(ic_sets).
:- lib(ic).

steiner(N, Sets) :-
        NB is N * (N-1) // 6,           % compute number of triplets
        intsets(Sets, NB, 1, N),        % initialise the set variables
        ( foreach(S,Sets) do
            #(S,3)                      % constrain their cardinality
        ),
        ( fromto(Sets,[S1|Ss],Ss,[]) do
            ( foreach(S2,Ss), param(S1) do
                #(S1 /\verb.\. S2, C),         % constrain the cardinality
                C #=< 1                 % of pairwise intersections
            )
        ),
        label_sets(Sets).               % search

label_sets([]).
label_sets([S|Ss]) :-
        insetdomain(S,_,_,_),
        label_sets(Ss).
\end{code}
Running this program yields the following first solution:
\begin{quote}\begin{verbatim}
?- steiner(9,X).

X = [[1, 2, 3], [1, 4, 5], [1, 6, 7], [1, 8, 9],
     [2, 4, 6], [2, 5, 8], [2, 7, 9], [3, 4, 9],
     [3, 5, 7], [3, 6, 8], [4, 7, 8], [5, 6, 9]] More? (;)
\end{verbatim}\end{quote}

%----------------------------------------------------------------------
\section{Weight Constraints}
\label{weight-constraint}
\index{weight constraint}
%----------------------------------------------------------------------

\index{knapsack}
\index{bin packing}
Another constraint between sets and integers is the weight/3 constraint.
It allows the association of weights to set elements, and can help when
solving problems of the knapsack or bin packing type.
The constraint takes a set and an array of element weights and
constrains the weight of the whole set:
\begin{quote}\begin{verbatim}
?- ic_sets:(Container :: [] .. [1, 2, 3, 4, 5]),
   Weights = [](20, 34, 9, 12, 19),
   weight(Container, Weights, W).
Container = Container{([] .. [1, 2, 3, 4, 5]) : _2127{0 .. 5}}
Weights = [](20, 34, 9, 12, 19)
W = W{0 .. 94}
There is 1 delayed goal.
Yes (0.01s cpu)
\end{verbatim}\end{quote}
By adding a capacity limit and a search primitive, we can solve a
knapsack problem:
\begin{quote}\begin{verbatim}
?- ic_sets:(Container :: [] .. [1, 2, 3, 4, 5]),
   Weights = [](20, 34, 9, 12, 19),
   weight(Container, Weights, W),
   W #=< 50,
   insetdomain(Container,_,_,_).
Weights = [](20, 34, 9, 12, 19)
W = 41
Container = [1, 3, 4]
More (0.00s cpu)
\end{verbatim}\end{quote}

By using the heuristic options provided by insetdomain, we can
implement a greedy heuristic, which finds the optimal solution
(in terms of greatest weight) straight away:
\index{greedy heuristic}
\begin{quote}\begin{verbatim}
?- ic_sets:(Container :: [] .. [1, 2, 3, 4, 5]),
   Weights = [](20, 34, 9, 12, 19),
   weight(Container, Weights, W),
   W #=< 50,
   insetdomain(Container,decreasing,heavy_first(Weights),_).
W = 48
Container = [1, 3, 5]
Weights = [](20, 34, 9, 12, 19)
More (0.00s cpu)
\end{verbatim}\end{quote}
\quickref{Set Weight Constraint}{
\begin{description}
\item[\biptxtref{weight(?Set, ++ElementWeights, ?Weight)}{weight/3}{../bips/lib/ic_sets/weight-3.html}]
         According to the array of element weights, the weight of set Set1 is Weight
\end{description}
}

\section{Exercises}

\begin{enumerate}

\item

Consider the knapsack problem in section~\ref{weight-constraint}.
Suppose that the items each have an associated profit, namely 17, 38, 18, 10
and 5, respectively.  Which items should be included to maximise profit?

\item

Write a predicate which, given a list of sizes of items and a list of
capacities of buckets, returns a list of (ground) sets indicating which
items should go into each bucket.  Obviously each item should go into
exactly one bucket.

Try it out with 5 items of sizes 20, 34, 9, 12 and 19, into 3 buckets of
sizes 60, 20 and 20.

\end{enumerate}

\index{library!ic_sets|)}

%HEVEA\cutend


\section{Theoretical Foundations}
\label{sec:Theoretical_Foundations}
In this experiment the speed of sound in various solids and liquids was determined. This section summarizes the theory and formulas necessary to understand the following experiments on ultrasound in section \ref{sec:Evaluation}.

%-------------------------------------------------------------------------------------------
\subsection{Wave Propagation}
\label{subsec:Wave_Propagation}
Wave propagation is the way in which waves travel. It can be distinguished between transverse waves and longitudinal waves. These waveforms are defined as follows:

\textbf{Transverse wave:} The displacement of the medium the wave travels through is perpendicular to the direction in which the wave propagates. Figure \ref{fig:transverse} shows a graphical representation of this sinusoidal waveform in a rope \cite{waves}.

\begin{figure}[H]
	\centering
	\includegraphics[scale=0.4]{transverse}
	\caption{Representation of a periodic transverse sinusoidal wave in a rope. The up and down motion of the rope is perpendicular to the direction in which the wave propagates. This leads to each particle of the rope moving up and down \cite{waves}. - partially modified}
	\label{fig:transverse}
\end{figure}

\textbf{Longitudinal wave:} The displacement of the medium the wave travels through is parallel to the direction in which the wave propagates. Sound waves in air behave like this. The air molecules oscillate back and forth and their energy is propagated in the same direction as their motion. Figure \ref{fig:longitudinal} shows a graphical representation of this waveform in a mechanical spring \cite{waves}.

\begin{figure}[H]
	\centering
	\includegraphics[scale=0.45]{longitudinal}
	\caption{Representation of a longitudinal wave in a mechanical spring. The elastic deflection (compression and extension) of the spring is parallel to the direction in which the wave propagates. This produces areas of increased and decreased pressure \cite{waves}.}
	\label{fig:longitudinal}
\end{figure}

\newpage
%-------------------------------------------------------------------------------------------
\subsection{Reflection and Refraction}
\label{subsec:Reflection_and_Refraction}
Similar to optics, reflection and refraction also occur when sound waves hit the boundary between two different media. Contrary to optics, the refraction index is not the important quantity. The phase velocity leads to refraction and the impedance leads to reflection. It can be distinguished between a vertical and an oblique angle of incidence. The following two sections explain this in more detail \cite{ultrasound}.

%-------------------------------------------------------------------------------------------
\subsubsection{Vertical Angle of Incidence}
\label{subsubsec:Vertical_Angle_of_Incidence}
A sound wave with the pressure $\hat{p}_e$ and velocity $v_e$ that travels trough a medium with the impedance $Z_1$ hits a boundary between two media with a vertical angle of incidence. This results in a different ratio between the pressure $\hat{p}_t$ and velocity $v_t$ in the medium with the impedance $Z_2$. At the same time the pressure and the velocity have to be equal at the boundary between the two media. This leads to a third reflected partial wave. Figure \ref{fig:vertical_angle_of_incidence} shows the just described phenomenon \cite{ultrasound}.

\begin{figure}[H]
	\centering
	\includegraphics[scale=1.5]{vertical_angle_of_incidence}
	\caption{Representation of a sound wave $e$ in blue that hits a boundary between the two media with the impedances $Z_1$ and $Z_2$. The transmitted wave $t$ is shown in green and the reflected partial wave $r$ is shown in red \cite{ultrasound}. - partially modified}
	\label{fig:vertical_angle_of_incidence}
\end{figure}

The calculation of the amplitude reflection coefficient $r$ is shown in equation \ref{eq:vertical_reflection} and the amplitude transmission coefficient $t$ is shown in equation \ref{eq:vertical_transmission}. They can be derived by utilizing the continuity conditions that are valid at the interface between the two media \cite{ultrasound}.

\begin{equation}
r = \dfrac{\hat{p}_r}{\hat{p}_e} = \dfrac{Z_2-Z_1}{Z_2+Z_1}
\label{eq:vertical_reflection}
\end{equation}
\begin{equation}
t = \dfrac{\hat{p}_t}{\hat{p}_e} = \dfrac{2 Z_2}{Z_2+Z_1}
\label{eq:vertical_transmission}
\end{equation}
where:
\begin{multicols}{2}
\begin{conditions}
	r & reflection coefficient \\
	t & transmission coefficient \\
	Z_1 \text{, } Z_2 & medium impedance
\end{conditions}
\begin{conditions}
	\hat{p}_r & reflected pressure \\
	\hat{p}_t & transmitted pressure \\
	\hat{p}_e & pressure amplitude
\end{conditions}
\end{multicols}

If $Z_2$ is smaller than $Z_1$ the reflection coefficient turns out negative which means the reflected wave is phase-shifted by 180\textdegree\ \cite{ultrasound}.
\newpage
%-------------------------------------------------------------------------------------------
\subsubsection{Oblique Angle of Incidence}
\label{subsubsec:Oblique_Angle_of_Incidence}
The reflection and transmission coefficients of a sound wave $e$ that hits an interface between two media with an oblique angle $\alpha$ depend on this angle of incidence. Furthermore, the transmitted wave will be refracted. If the medium $Z_2$ happens to be a solid, a transversal and a longitudinal wave will form. Figure \ref{fig:oblique_angle_of_incidence} shows this phenomenon which is called \flqq mode conversion\frqq\ \cite{ultrasound}.

\begin{figure}[H]
	\centering
	\includegraphics[scale=1.5]{oblique_angle_of_incidence}
	\caption{Representation of a sound wave $e$ shown in blue that hits an interface between the two media $Z_1$ and $Z_2$ with an oblique angle of incidence. The reflected wave $r$ is shown in red and the refracted transverse and longitudinal waves are shown in green \cite{ultrasound}.}
	\label{fig:oblique_angle_of_incidence}
\end{figure}

To calculate the angle of reflection shown in equation \ref{eq:oblique_angle_of_incidence} and the refraction shown in equation \ref{eq:oblique_refraction}, the same known laws from optics apply. Equation \ref{eq:oblique_refraction} allows the use of the transversal or longitudinal sound velocity for the variable $c_2$ depending on the desired result \cite{ultrasound}.

\begin{equation}
\alpha = \alpha^\prime
\label{eq:oblique_angle_of_incidence}
\end{equation}
\begin{equation}
\dfrac{\sin{(\alpha)}}{\sin{(\beta)}} = \dfrac{c_1}{c_2}
\label{eq:oblique_refraction}
\end{equation}
where:
\begin{multicols}{2}
\begin{conditions}
	\alpha & angle of incidence \\
	\beta & angle of refraction
\end{conditions}
\begin{conditions}
	\alpha^\prime & angle of reflection \\
	c_1 \text{, } c_2 & sound velocity in the medium
\end{conditions}
\end{multicols}

%-------------------------------------------------------------------------------------------
\subsection{Total Internal Reflection}
\label{subsec:Total_Internal_Reflection}
Total internal reflection can take place at the interface between two media. This happens when the following equation is valid:

\[
\sin{(\alpha_\text{crit})} = \dfrac{c_1}{c_2}
\]

This is a variation of the above shown equation \ref{eq:oblique_refraction}. Thus, the angle of refraction $\beta$ has to be 90\textdegree\ for the equation to be valid. For $\alpha \geq \alpha_\text{crit}$ the wave is totally reflected \cite{ultrasound}.

%-------------------------------------------------------------------------------------------
\subsection{Absorption}
\label{subsec:Absorption}
Sound waves are attenuated when they propagate in a medium due to absorption and scattering. Absorption usually converts a part of the ultrasound wave into heat. Scattering leads a particle that has been excited by the incoming wave to emit a wave in all direction itself. Equation \ref{eq:absorption} shows the exponential attenuation for monochromatic radiation. The \textbf{higher the frequency}, the \textbf{stronger the absorption} and scattering \cite{ultrasound}.

\begin{equation}
\hat{p}(x) = \hat{p}_0 \cdot e^{-\mu x}
\label{eq:absorption}
\end{equation}
where:
\begin{multicols}{2}
\begin{conditions}
	\hat{p}(x) & attenuated amplitude \\
	\mu & attenuation coefficient in m$^{-1}$
\end{conditions}
\begin{conditions}
	\hat{p}_0 & initial amplitude \\
	x & distance in m
\end{conditions}
\end{multicols}

%-------------------------------------------------------------------------------------------
\subsection{Ultrasound Generation}
\label{subsec:Ultrasound_Generation}
Ultrasound can be generated and received by using a ultrasonic transducer. In this experiment the piezoelectric effect is used in the ultrasonic transducer. Certain crystals expand proportional to the applied voltage. Contrary to this, the piezoelectric transducer induces a voltage when deformed. Thus, it can be used as a transmitter or receiver \cite{ultrasound}.

\begin{figure}[H]
	\centering
	\includegraphics[scale=1.5]{ultrasound_generation}
	\caption{Graphical representation of a piezoelectric transducer. The active part is the thin piezo disk at the bottom of the transducer. The mechanical dimensions determine the resonance frequency of the oscillation. The transducer is impedance matched to the amplifier \cite{ultrasound}.}
	\label{fig:ultrasound_generation}
\end{figure}

\newpage
%-------------------------------------------------------------------------------------------
\subsection{Ultrasound Measurement}
\label{subsec:Ultrasound_Measurement}
There are two common ways to measure ultrasound, the A-Scan and the B-Scan. This experiment uses the A-Scan as measurement method and therefore only this one will be explained.

The A-Scan is a one-dimensional procedure where short ultrasound impulses ($f > 20 \text{kHz}$) are transmitted. These are partially reflected on the interfaces between media and are received again by the transmitter. From the time difference between the sent impulse and the received echo and a known distance, the sound velocity of a medium can be determined. Equation \ref{eq:sound_velocity} shows this relationship \cite{ultrasound}.

\begin{equation}
c_m = \dfrac{2x}{\Delta t}
\label{eq:sound_velocity}
\end{equation}
where:
\begin{multicols}{2}
	\begin{conditions}
		c_m & sound velocity of a medium in $\,^{\text{m}}\!/_{\text{s}}$ \\
		\Delta t & time difference in s
	\end{conditions}
	\begin{conditions}
		x & distance to interface in m
	\end{conditions}
\end{multicols}

This technique is often used to check the integrity of materials. If the part has defects such as cracks or inclusions of different substances, there are detectable echoes. Furthermore, the wall thickness of work pieces can be determined by this method \cite{ultrasound}.


\documentclass[]{article}

%opening
\title{}
\author{}

\begin{document}

\maketitle

\begin{abstract}

\end{abstract}

\section{}

% latex table generated in R 4.2.0 by xtable 1.8-4 package
% Fri May 13 15:13:49 2022
\begin{table}[ht]
	\centering
	\begin{tabular}{rrrrr}
		\hline
		& 2 & 1 & 3 & All \\
		\hline
		Less than \$10,000 & 11.45 & 8.74 & 11.48 & 9.67 \\
		\$10,000 to \$30,000 & 33.73 & 20.69 & 26.23 & 24.47 \\
		\$30,000 to \$50,000 & 15.66 & 19.31 & 18.03 & 18.28 \\
		\$50,000 to \$70,000 & 13.25 & 16.09 & 16.39 & 15.41 \\
		\$70,000 to \$100,000 & 10.84 & 15.86 & 11.48 & 14.20 \\
		\$100,000 to \$200,000 & 10.84 & 13.79 & 11.48 & 12.84 \\
		\$200,000 to \$500,000 & 2.41 & 5.06 & 4.92 & 4.38 \\
		\$500,000 or more & 1.81 & 0.46 & 0.00 & 0.76 \\
		Total & 100.00 & 100.00 & 100.00 & 100.00 \\
		\hline
	\end{tabular}
\end{table}

\end{document}


\subsubsection{\stid {3.01} xSDK Sub-project: multiprecision}
\paragraph{Overview}
Within the past years, hardware vendors have started designing low precision special function units
in response to the demand of the Machine Learning community for high compute power
in low precision formats. At the same time, the gap between compute power
on the one hand and memory bandwidth on the other hand keeps increasing,
making data access and communication prohibitively expensive compared to arithmetic operations.
Having the choice between ignoring the hardware trends and continuing the traditional path
or adjusting the software stack to the changing hardware designs, the US Exascale Computing Project
decided to build a multiprecision focus effort to take on the challenge of designing and engineering novel algorithms capable to exploit the compute power available in low precision and to adjust the communication format to application specific needs.
As part of the xSDK project, the multiprecision focus effort is a coordinated effort creating synergies via cross-institutional collaboration.

\paragraph{Key Challenges}
Generally, there exists a strong relationship between the precision used in arithmetic operations and the accuracy of the computed result. Since scientific applications need to provide high quality output, replacing high precision formats with low precision formats throughout a complete application code is generally not feasible. Instead, to utilize lower precision formats, the underlying numerical algorithms have to be redesigned to employ low precision formats for the most time-consuming parts while preserving  high accuracy in the solution. In this context, the arithmetic operations are only one aspect. As the execution time of many scientific applications is dominated by communication and memory access, the algorithm redesign also has to include strategies for compressing data to reduce the pressure on the memory bandwidth. This aspect becomes even more relevant as the arithmetic power continues to grow faster than the memory bandwidth, therewith widening the gap between arithmetic performance and memory performance, see Figure~\ref{fig:xsdk-machinebalance}.

\begin{figure}[htb]
	\centering
	\includegraphics[width=.8\columnwidth]{projects/2.3.3-MathLibs/2.3.3.01-xSDK/xSDK-machinebalance.pdf}
	%\includegraphics[width=.8\columnwidth]{xSDK-machinebalance.pdf}
	\caption{\label{fig:xsdk-machinebalance} Evolution of the machine balance of processors over different hardware generations.}
\end{figure}

\paragraph{Solution Strategy}
In the multiprecision effort, the team assesses current status and functionalities, advances the theoretical knowledge on multiprecision algorithms, designs prototype implementations and multiprecision interoperability layers, deploys production-ready multiprecision algorithms in the xSDK math libraries, ensures multiprecision cross-library interoperability and integrates multiprecision algorithms into ECP application projects. The long list of project activities is organized in a multi-phase approach:
The first stage is dedicated to the exploration of the design space and existing research efforts focusing on low precision, mixed precision, and extended precision. To this end, the effort  includes internationally-renown external experts for mixed precision algorithms. In the second stage, the multiprecision effort increasingly focuses on developing production code ready to be used in the ECP application projects. Relevant algorithmic functionality includes sparse linear algebra, multigrid methods, preconditioners, iterative solvers, low-rank approximations, and mixed precision machine learning.
On the hardware side, special focus is put on low-precision special function units like NVIDIA’s tensor cores that -- originally designed for machine learning algorithms -- offer about an order of magnitude higher arithmetic performance than conventional fp64 units.
In the third phase of the multiprecision effort, the project team will aid the ECP application projects with the adoption of multiprecision functionality and continue to adapt to new hardware technologies.

\paragraph{Recent Progress}
As a first step, the multiprecision team surveyed the state of the art in terms of mixed precision algorithms, low precision and extended precision algorithms, and the algorithmic needs of the ECP application projects. The results of this landscape assessment are made publicly available as ``A Survey of Numerical Methods Utilizing Mixed Precision Arithmetic''~\cite{Anztetal2020}.
A more compact version of this survey -- exclusively focusing on numerical linear algebra -- has been submitted as a journal article.

Based on the findings of the multiprecision landscape assessment, we created a set of cross-institutional focus groups that address different algorithms and aspects of the ECP software ecosystem including dense and sparse direct solvers, eigensolvers, Krylov-based iterative solvers, multigrid methods, preconditioners, Fast Fourier Transform, and machine learning technology. Orthogonal to those, we created focus groups on the design on a memory accessor that separates the arithmetic precision from the communication precision, and the efficient realization of multiprecision basic building blocks like sparse matrix vector multiply.
In bi-weekly virtual meetings, the progress on the different efforts is presented and discussed as many of these efforts are closely related. As another integral part of the bi-weekly phone calls we established a series of short talks where each meeting is commenced by an invited talk presenting an idea, success story, or progress update on mixed precision functionality to the audience.

Following the idea of decoupling the memory precision from the arithmetic precision to reduce the pressure on the memory bandwidth, the team released an accessor design document detailing the implementation and usage of a memory accessor that compresses data, e.g. by converting to a lower precision format, before invoking memory operations.
The document has been made available to the ECP community.

\paragraph{Next Steps}

Our next efforts include
\begin{itemize}
    \item the publication of a compacted version of the multiprecision landscape assessement as a scientific journal paper,
    \item the deployment of the accessor separating memory precision and arithmetic precision in the Ginkgo library with support for AMD GPUs, NVIDIA GPUs, and multicore CPUs,
    \item the implementation of compressed basis Krylov solvers that utilize the memory accessor to compact the Krylov search directions,
    \item the advancement of multiprecision capabilities for solvers, preconditioners, and other ECP-relevant kernels in xSDK libraries, including
    \subitem - the design and implementation of mixed precision eigensolvers,
    \subitem - the research and realization of mixed precision multigrid solvers,
    \subitem - the design and implementation of mixed precision sparse factorizations.
\end{itemize}


\paragraph{Influence of Blur Factor} \label{influence_of_blur_factor}
The blur factor for a sliding window filter is expanding the interval that is accepting time series windows. A too small
interval can lead to many false positive classifications and a too large interval may lead to too much 1NN-DTW calls.
The underlying time series measure for a sliding window filter is affecting the best performing blur factor for the
filter. Eleven different blur factors were tested for the two different underlying time series measures. The influence of
the blur factor for the filter is tested for the two underlying time series measures and compared to the
dominating configuration without filter. The right plot of figure \ref{fig:blur_factor_result} illustrates the influence
of the blur factor on the $F_{1}score_{\mu}$. Only the LNCE estimate is reaching with the given blur factors the same
$F_{1}score_{\mu}$ as the dominating configuration without filter. However, the two filter types come also with small
blur factors very close to the top $F_{1}score_{\mu}$ value.

\begin{figure}[H]
    \begin{center}
        \resizebox {\textwidth} {!} {
            \begin{tabular}{cc}
                \resizebox {!} {\height} {
                    \begin{tikzpicture}
                        \begin{axis}[
                            legend pos=south east,
                            xmin=100,
                            xmax=300,
                            ymin=0.6,
                            ymax=0.75,
                            xlabel=filter interval size in \%,
                            ylabel=$F_{1}score_{\mu}$,
                            width=\axisdefaultwidth,
                            height=0.7*\axisdefaultheight]
                            \addplot[blue, ultra thick] table {../data/fig/blur_factor_result/lnce.dat};
                            \addlegendentry{LNCE}
                            \addplot[red, ultra thick] table {../data/fig/blur_factor_result/var.dat};
                            \addlegendentry{VAR}
                            \addplot[dotted, black, domain=100:300] {0.738393631276109};
                            \addlegendentry{No Filter}
                        \end{axis}
                    \end{tikzpicture}
                } &
                \resizebox {!} {\height} {
                    \begin{tikzpicture}
                        \begin{axis}[
                            legend pos=south east,
                            xmin=100,
                            xmax=300,
                            ymin=0,
                            ymax=5500,
                            xlabel=filter interval size in \%,
                            ylabel=\# 1NN-DTW calls,
                            width=\axisdefaultwidth,
                            height=0.7*\axisdefaultheight]
                            \addplot[blue, ultra thick] table {../data/fig/nnc_calls_result/lnce.dat};
                            \addlegendentry{LNCE}
                            \addplot[red, ultra thick] table {../data/fig/nnc_calls_result/var.dat};
                            \addlegendentry{VAR}
                            \addplot[dotted, black, domain=100:300] {4893};
                            \addlegendentry{No Filter}
                        \end{axis}
                    \end{tikzpicture}
                }
            \end{tabular}
        }
    \end{center}
    \caption{The left plot shows the $F_{1}score_{\mu}$ depending on the blur factor that expands the size of the filter
    interval in \%. The right plot shows the amount of 1NN-DTW calls depending on the blur factor that expands the size
    of the filter interval in \%.}
    \label{fig:blur_factor_result}
\end{figure}

The left plot of figure \ref{fig:blur_factor_result} and also table \ref{tab:result} shows that the presented
filters can reach the same $F_{1}score_{\mu}$ value as the dominating simulation without filter. The question remains on
how often are the presented filters block the 1NN-DTW calls. The right plot of figure \ref{fig:blur_factor_result}
illustrates the influence of the blur factor on the amount of 1NN-DTW calls. All two filter types remain clearly below
the amount of 1NN-DTW calls of the dominating configuration without filter. Figure \ref{fig:blur_factor_tradeoff}
combines both plots of figure \ref{fig:blur_factor_result} to trade-off curves for both filters.

\begin{figure}[H]
    \begin{center}
        \resizebox {\textwidth} {!} {
            \begin{tabular}{cc}
                \resizebox {!} {\height} {
                    \begin{tikzpicture}
                        \begin{axis}[
                            legend pos=south west,
                            xlabel=$F_{1}score_{\mu}$,
                            ylabel=\# blocked 1NN-DTW calls,
                            width=\axisdefaultwidth,
                            height=\axisdefaultwidth]
                            \addplot[blue, only marks, mark=*] table[y=Y] {../data/fig/nnc_calls_result/tradeoff-lnce.dat};
                            \addplot[gray, domain=0.7:0.74] {-83543.0872*x^2+55746.6816*x+4892.4835};
                            \addlegendentry{LNCE}
                        \end{axis}
                    \end{tikzpicture}
                } &
                \resizebox {!} {\height} {
                    \begin{tikzpicture}
                        \begin{axis}[
                            legend pos=south west,
                            xlabel=$F_{1}score_{\mu}$,
                            ylabel=\# blocked 1NN-DTW calls,
                            width=\axisdefaultwidth,
                            height=\axisdefaultwidth]
                            \addplot[red, only marks, mark=*] table[y=Y] {../data/fig/nnc_calls_result/tradeoff-var.dat};
                            \addplot[gray, domain=0.63:0.74] {-34477.4805*x^2+20729.4890*x+4884.5834};
                            \addlegendentry{VAR}
                        \end{axis}
                    \end{tikzpicture}
                }
            \end{tabular}
        }
    \end{center}
    \caption{Trade-off between the $F_{1}score_{\mu}$ and the amount of blocked 1NN-DTW calls for both filter
    types. The gray graphs are quadratic regressions to illustrate the trade-off as curve on the observed intervals.}
    \label{fig:blur_factor_tradeoff}
\end{figure}


\chapter{How to model a system}\label{How to model a system}
\begin{aquote}{The Chemical Basis of Morphogenesis. A. Turing 1952.}
\textit{This model will be a simplification and an idealization, and consequently a falsification. It is to be hoped that the features retained for discussion are those of greatest importance in the present state of knowledge.}
\end{aquote}
\begin{aquote}{Empirical Model-Building and Response Surfaces. G. Box \& N. Draper 1987.}
\textit{All models are wrong, but some are useful.}
\end{aquote}
Modelling a system, whether it be physical, chemical, or biological, is, in some ways, more of art than a science. You try and strip away all extraneous information and mathematically describe that which is left. Sometimes there are physical laws to help you, \eg gravity, conservation of energy and mass. Other times we only have experimental intuition, \eg predator-prey interactions from population data. In either case, the central idea of modelling is that it should always form part of a cyclical process \see{Modelling_loop}.

You try to start with physical intuition (experiment), represent the important parts mathematically (model), hopefully reproduce reality (test) and, finally, use your mathematical model to predict unknown outcomes (predict). These prediction can then feed back into experiment and the process begins anew.
\begin{figure}[!!!h!!!tb]
\centering
\includegraphics[width=\tp]{../Pictures/Modelling_loop.png}
\caption{\label{Modelling_loop}Diagram of the modelling cycle.}
\end{figure}

In this chapter we are going to review some of the methods that can be used to produce a mathematical interpretation of reality.

\section{Physical laws}
\begin{defin}
A \textbf{constitutive relation} (or `physical law') is a rule that the modeller adds to the system based on their experimental experience, which relates interacting components.
\end{defin}
Physics has many laws such as: conservation of energy, general relativity and the laws of thermodynamics. There is (as yet) no fundamental reason for these laws to hold. We just take them as laws because they fit the data that we observe.

Here are just a few examples of the laws that you might come across.
\begin{itemize}

\item \textbf{Newton's Law of Cooling.}

\textit{The rate of cooling of a body is proportional to the difference between the bodies temperature and the temperature of its environment.}

\COL{Let $T$ and $T_e$ be the temperatures of the object and the environment, respectively, then
\bb
\dot{T}=k(T_e-T),\label{Law_of_cooling}
\ee
where $k$ is defined to be the heat transfer coefficient.}
\begin{example}[frametitle=Cooling tea]\label{Cooling_tea}
Suppose I prepare two cups of tea at exactly the same time, so initially they both start at $100^o$C. I quickly add enough milk to cup 1 to cool it to $90^o$C. Ten minutes later I add milk to cup 2, which cools cup 2 by $10^o$C. Which cup is hotter at that point?
\COL{
The general solution to \eqn{Law_of_cooling} is
\bb
T(t)=T_e+\l T(0)-T_e\r e^{-kt},
\ee
where $T(0)$ is the initial temperature. For cup 1 we have
\bb
T_1(10)=T_{e}+\l 90-T_{e}\r e^{-k10}.
\ee
For cup 2 we have
\bb
T_2(10)=T_{e}+\l 100-T_{e}\r e^{-k10}-10.
\ee
Subtracting $T_1$ from $T_2$ gives
\bb
T_1(10)-T_2(10)=10\l 1- e^{-k10}\r>0.
\ee
Since $\exp(0)=1$ and $\exp(-kt)$ is a strictly monotonically decreasing function of time. Hence, cup 1 is hotter than cup 2 at time $t=10$ minutes. This means that the earlier you put your milk in the hotter your tea will stay!

Note that neither the ambient temperature, nor the heat transfer coefficient were needed.}
\end{example}

\item \textbf{Newton's Second Law of Motion}

\textit{The rate of change of momentum of a body is directly proportional to the force applied to the body.}

\COL{This is the standard $F=ma$ that everyone knows and loves (assuming that the mass remains that same throughout the interaction), where the acceleration, $a$, is the second derivative of the location with respect to time, $a=\ddot{x}$.}

\item \textbf{Newton's Law of Gravitation}\footnote{Newton devised the laws of optics, the laws of motion and invented calculus practically on a dare... then he turned 26. What have you done today? }

\textit{A particle attracts every other particle in the universe using a force that is directly proportional to the product of their masses and inversely proportional to the square of the distance between their centres.}

\COL{Suppose body $i$ has mass $m_i$ and is at position $\bm{r}_i=(x_i,y_i)$. Body $i$ is then separated from body $j$ by a distance $r_{ij}=\sqrt{\l x_i-x_j\r^2+\l y_i-y_j \r^2}=|\bm{r}_i-\bm{r}_j|$. Let $G$ be the universal gravitational constant then the force, $\bm{F}_{ij}$, acting on body $i$ from body $j$ is
\bb
\bm{F}_{ij}=-G\frac{m_im_j}{r^2_{ij}}\bm{\hat{r}}_{ji},
\ee
where
\bb
\bm{\hat{r}}_{ji}=\frac{r_i-r_j}{|r_i-r_j|}
\ee
is the unit vector from body $j$ to body $i$.

Note that the force is vector valued quantity because it has a magnitude, but also a direction, as it acts in the direction of the line joining the bodies.}
\begin{figure}[!!!h!!!tb]
\centering
\includegraphics[width=\ttp]{../Pictures/Three_body_problem.png}
\caption{\label{Three_body}Schematic diagram of the three-body problem.}
\end{figure}

\begin{example}[frametitle=Three body problem]\label{Three_body_problem}
We can combine the Second Law of Motion and the Law of Gravitation in order to predict the position of planets interacting through their gravitational fields \see{Three_body}. Consider three planets with the same mass, $m$, and positions $\bm{r}_1(t)$, $\bm{r}_2(t)$ and $\bm{r}_3(t)$, respectively. Further, we note that since we are dealing with acceleration (a second order equation) we will need to specify two initial conditions, the position and velocity. Let the initial positions be $\bm{r}_i(0)=\bm{r}_{i0}$ and the initial velocities $\dot{\bm{r}}_i(0)=\bm{v}_{i0}$.\COL{ The governing equations are
\begin{align}
&m\ddot{\bm{r}}_1=-G\frac{m^2}{|\bm{r}_1-\bm{r}_2|^3}\l\bm{r}_1-\bm{r}_2\r-G\frac{m^2}{|\bm{r}_1-\bm{r}_3|^3}\l\bm{r}_1-\bm{r}_3\r,\label{g1}\\
&m\ddot{\bm{r}}_2=-G\frac{m^2}{|\bm{r}_2-\bm{r}_1|^3}\l\bm{r}_2-\bm{r}_1\r-G\frac{m^2}{|\bm{r}_2-\bm{r}_3|^3}\l\bm{r}_2-\bm{r}_3\r\label{g2},\\
&m\ddot{\bm{r}}_3=-G\frac{m^2}{|\bm{r}_3-\bm{r}_1|^3}\l\bm{r}_3-\bm{r}_1\r-G\frac{m^2}{|\bm{r}_3-\bm{r}_2|^3}\l\bm{r}_3-\bm{r}_2\r\label{g3}.
\end{align}
where we remember that this is a vector equation, $\bm{r}_i=(x_i,y_i)$, so there are actually six, second order ODEs here, rather than three.}

The three body problem illustrates chaotic behaviour, in the sense that the outcome is extremely sensitive to the initial conditions. This can be seen in the simulations of \fig{Three_body_sim}.

Simulation tip:
\begin{itemize}
\item when solving \eqnto{g1}{g3} numerically we could separate each equation into its Cartesian components and reduce the second order equation to two first order equations. Namely, we would introduce $(v_{ix},v_{iy})=(\dot{x}_i,\dot{y}_i)$. Thus, we would have a system of twelve ODEs to solve, with variables
\bb
(x_1,y_1,v_{1x},v_{1y},x_2,y_2,v_{2x},v_{2y},x_3,y_3,v_{3x},v_{3y}).
\ee
However, since $x_i$ and $y_i$ are perpendicular Cartesian coordinates it turns out to be a good idea to use complex numbers. Specifically, instead of writing two ODEs for each of $x_i$ and $y_i$ we can simply solve one ODE in terms of the complex quantity $\bm{r}_i=x_i+Iy_i$, which can be handled by numerical solvers. Thus, we simplify the numerical solution from twelve to six equations.
\end{itemize}
\end{example}
\begin{figure}[!!!h!!!tb]
\centering
\includegraphics[width=\tp]{../Pictures/Three_body_sim.png}
\caption{\label{Three_body_sim}Two simulations of the three body problem. The green and red trajectories are identically initiated. The blue trajectory is initiated at $(-1,0.01)$ in the left figure and at $(-1,0)$ in the right figure.}
\end{figure}
\item \textbf{Hooke's law}

\textit{The force, $F$, needed to extend or compress a spring by some distance, $x$, scales linearly with respect to that distance,}
\COL{
\bb
F=kx.
\ee

The constant of proportionality, $k$, defined by this law is known as the spring constant and is measured in units of Force per distance, \eg N/m.

Depending on the material Hooke's law only holds true for small extensions and compressions. For example, this law suggests that given enough force a spring can pass through itself. Further, biological materials may not follow the law because they break if stretched too far (\eg bone), or they may grow, or permanently deform\footnote{Consider, for example, the ear. Small earrings to not stretch the skin very much and, thus, once the earring is removed the skin can heal. Alternatively, people who use large gauge earrings stretch their ear holes beyond the elastic limit of the skin so that they have a permanent hole. }, thus, reducing the force needed to give the same extension (\eg skin).
}

\end{itemize}
\subsubsection{Pendulums}\label{Pendulums_section}
In this section we take an extended look at pendulums depending on Hooke's law and simple Newtonian mechanics. Specifically, we consider a spring, oscillating up and down, and a bob, oscillating side to side \see{Pendulums}.
\begin{figure}[!!!h!!!tb]
\centering
\subfigure[\label{Spring}]{\includegraphics[width=\ttp]{../Pictures/Spring.png}}
\subfigure[\label{Bob}]{\includegraphics[width=\ttp]{../Pictures/Pendulum.png}}
\caption{Two types of pendulums: (a) an oscillating spring. (b) a weight on a string.\label{Pendulums}}
\end{figure}

\COL{Assuming that the spring conforms to Hooke's Law and applying Newton's Second Law of motion the equation of motion for the spring is
\bb
m\ddot{y}=-ky,\label{Spring_eqn}
\ee
where $y$ is the vertical displacement of the spring, $m$ is the mass attached to the spring and $k$ is the spring constant. The negative sign shows that the force is always directed to the resting position of the spring (here, taken to be the origin). If the negative sign was not there then we would be saying that the pendulums position would grow exponentially with an applied force, which is not very realistic.

The pendulum bob is slightly more complicated as we have to account for two-dimensional motion. Complicating the matter further is that the pendulum weight is confined to move on the arc of a circle, meaning that problem is easier to solve in polar coordinates.

To derive the equations of motion we split the component of force acting on the pendulum into components acting along the radial and angular directions of the system, as shown in \fig{Bob}. Critically, we only need to consider the angular acceleration, which can be derived to be $r\ddot{\theta}$. The derivation will be seen on problem sheet two. Using Newton's Second Law again we derive that
\bb
mr\ddot{\theta}=-mg\sin(\theta).\label{Bob_eqn}
\ee
One interesting point we can immediately see from \eqn{Bob_eqn} is that the mass of the pendulum does not influence the solution of the equation, which can be compared with the dependence of \eqn{Spring_eqn} on the mass.

Equation \ref{Bob_eqn} can be solved directly in terms of `elliptic integrals', but this accounts to little more than integrating the equation twice and leaving the equation written in integral form. More insight to the solution can be gained if the angle of oscillation is small. In this case we can linearise the right-hand side of \eqn{Bob_eqn} by using Taylor series about zero, \ie $\sin(\theta)\approx\theta$. Hence, \eqn{Bob_eqn} can be approximated by
\bb
r\ddot{\theta}=-g\theta\label{Bob_eqn_approx},
\ee
which can be seen to be analogous to \eqn{Spring_eqn}.

If the different pendulums are displaced and released from rest then the amount of error introduced into the equation is determined by the initial displacement. \fig{Different_ICs} compares\footnote{Comparing $y$ and $\theta$ is a little dodgy as $y$ is a dimensional length and the $\theta$ is dimensionless, as we work in radians. However, if this bothers you we can fix this is in either of two ways. Either, we consider $y$ normalised by its natural length (taken here to be of unit length, regardless of the dimensions involved), or, we can consider \fig{Different_ICs} comparing \eqn{Bob_eqn} and its approximation in \eqn{Bob_eqn_approx}.} \eqns{Spring_eqn}{Bob_eqn} with different initial conditions. Thus, we see that increasing the initial amplitude of the bob pendulum causes the wave length of the oscillation to increase, or frequency of oscillation to decrease.}
\begin{figure}[!!!h!!!tb]
\centering
\subfigure[\label{IC_0.1}]{\includegraphics[width=\ttp]{../Pictures/Comparing_pendulums_IC_1.png}}
\subfigure[\label{IC_1}]{\includegraphics[width=\ttp]{../Pictures/Comparing_pendulums_IC_10.png}}
\caption{\label{Different_ICs}Comparing \eqns{Spring_eqn}{Bob_eqn} with initial conditions (a) $y=0.1=\theta$ and (b) $y=1=\theta$. Parameter values $r=g=k=m=1$.}
\end{figure}

\begin{defin}
Any system defined by an equation of the form
\bb
\ddot{u}=-k^2u.\label{SHM}
\ee
is said to under go \textbf{simple harmonic motion}.
\end{defin}
Equation \ref{SHM} can be solved to produce the solution
\bb
u=A\cos(kt)+B\sin(kt),
\ee
where $A$ and $B$ are specified through the initial conditions.

%\subsection{Euler-Bernoulli beam}
\section{Law of Mass Action}
All of the above physical laws are very specific in their application. In this section we will learn about a much more general technique that will allow us to build an ODE system out of multiple interacting populations. These populations could represent chemical compounds, humans, cells or animals as well as different states within a population \ie infected humans and susceptible humans. The law presented in this section is applied whenever the populations of the system are able to: (i) change identities; (ii) create more population members; or (iii) cause populations to decay. Specific examples of each of these interactions are, respectively: (i) susceptible humans becoming infected through interactions with a diseased person; (ii) animals giving birth; (iii) predators eating prey. Note that a change-of-identity interaction can itself be thought as a combination of creation and degradation operations. For example, in the above case of infection a member of the susceptible human population is removed from the system, whilst an infected human is added to the system. Thus, all interactions can be made through combining creation and degradation operations.

We use chemical reaction notation to specify the outcomes of population interactions. Consider a system composed of $n$ different interacting populations $(u_1,\dots,u_n)$. We assume that all interactions between the population elements lead to the creation, or destruction, of one (or more) of the $n$ populations.
\begin{defin}
A \textbf{rate equation} specifies that an interaction involves $a_1$ members of population $u_1$, $a_2$ members of population $u_2$, etc. and produces $b_1$ members of population $u_1$, $b_2$ members of population $u_2$, etc. The equation is written as
\bb
a_1u_1+a_2u_2+\dots+a_nu_n \stackrel{r}{\rightarrow} b_1u_1+b_2u_2+\dots+b_nu_n,
\ee
where $r>0$ is the \textbf{reaction rate}.
\end{defin}
Note that some of the $a_i$ and $b_i$ values can be zero.

\begin{example}[frametitle=Reaction equation examples]\label{Reaction equation examples}
\begin{itemize}
\item \textbf{Birth}

Two agents of population $u$ come together to produce a third,
\COL{\bb
2u\stackrel{r}{\rightarrow}3u.
\ee}
\item \textbf{Death}

An agent of population $u$ dies (or is destroyed) due to natural causes,
\COL{
\bb
u\stackrel{r}{\rightarrow}\slashed{0}.
\ee}

\item \textbf{Predation}

A predator population, $v$, converts energy from eating prey, $u$, into offspring,
\COL{
\bb
u+v\stackrel{r}{\rightarrow}2v.
\ee}

\item \textbf{Infection}

Consider a population of infected people, $I$, who are able to infect a susceptible population, $S$. Further, over time, the infected people recover and become susceptible again,
\COL{\begin{align}
I+S&\stackrel{r_1}{\rightarrow}2I,\\
I&\stackrel{r_2}{\rightarrow}S.
\end{align}}
\end{itemize}
\end{example}

Rate equations provide a rigorous way of defining all of the interactions a system is assumed to undergo. However, we still require a method of converting the rate equation into an ODE. This is the power of the Law of Mass Action.
\begin{defin}
The \textbf{Law of Mass Action} states that production rate of a reaction is directly proportional to the product of the input population sizes. Specifically, if
\bb
a_1u_1+a_2u_2+\dots+a_nu_n \stackrel{r}{\rightarrow} b_1u_1+b_2u_2+\dots+b_nu_n \nonumber
\ee
is the reaction of interest then the production rate is proportional to
\bb
ru_1^{a_1}u_2^{a_2}\dots u_n^{a_n}
\ee
and the accompanying ODEs are
\begin{align}
&\dot{u}_1=(b_1-a_1)ru_1^{a_1}u_2^{a_2}\dots u_n^{a_n},\\
&\dot{u}_2=(b_2-a_2)ru_1^{a_1}u_2^{a_2}\dots u_n^{a_n},\\
&\vdots\\
&\dot{u}_n=(b_n-a_n)ru_1^{a_1}u_2^{a_2}\dots u_n^{a_n}.
\end{align}
\end{defin}
Note that in converting from reaction equation to the ODE of $u_i$ we to account for the stoichiometry, \ie $(a_i-b_i)$. Further, when multiple reactions are considered, the terms arising from the Law of Mass Action are simply added together as independent terms.

\begin{example}[frametitle=Law of Mass Action examples]\label{Law of Mass Action examples}
\begin{itemize}
\item \textbf{Birth}
\COL{\begin{align}
2u&\stackrel{r}{\rightarrow}3u,\nonumber\\
\implies &\dot{u}=ru^2.
\end{align}}
\item \textbf{Death}
\COL{\begin{align}
u&\stackrel{r}{\rightarrow}\slashed{0},\nonumber\\
\implies &\dot{u}=-ru.
\end{align}}

\item \textbf{Predation}
\COL{\begin{align}
u+v&\stackrel{r}{\rightarrow}2v,\nonumber\\
\implies &\dot{u}=-ruv,\\
&\dot{v}=ruv.
\end{align}}

\item \textbf{Infection}
\COL{\begin{align}
I+S&\stackrel{r_1}{\rightarrow}2I,\quad I\stackrel{r_2}{\rightarrow}S,\nonumber\\
\implies &\dot{S}=-r_1IS+r_2I,\\
&\dot{I}=r_1IS-r_2I.
\end{align}}
\end{itemize}
\end{example}

\begin{example}[frametitle=Zombies]\label{Zombies}
Humans, $H$, and zombies, $Z$, interact through the following three interactions \see{Zombie_picture}:
\begin{enumerate}
\item humans kill zombies at a rate $a$;
\item zombies kill humans at a rate $b$;
\item zombies infect humans at a rate $c$.
\end{enumerate}
The reaction equations for this system are,
\COL{\begin{align}
&H+Z\stackrel{a}{\rightarrow}H;\\
&H+Z\stackrel{b}{\rightarrow}Z;\\
&H+Z\stackrel{c}{\rightarrow}2Z.
\end{align}
}\COL{The ODE form of the system is
\begin{align}
&\dot{H}=-bHZ-cHZ=-\alpha HZ\\
&\dot{Z}=-aHZ+cHZ=\beta HZ.
\end{align}
Since $\alpha=b+c>0$ the population of $H$ is always decreasing. However, $\beta=c-a$, which could be either positive or negative. Critically, if $\beta>0\implies c>a$ then the zombie population will grow. Alternatively, if $\beta<0 \implies c<a$ then  the zombie population decreases. Thus, the survival of the human race all depends on the sign of $c-a$, which, explicitly, is the `net rate increase of zombies', \ie zombie production minus zombie destruction.}
\end{example}
\begin{figure}[!!!h!!!tb]
\centering
\includegraphics[width=\tp]{../Pictures/Zombies.png}
\caption{\label{Zombie_picture} Possible outcomes of human-zombie interactions.}
\end{figure}

\section{Check list}
By the end of this chapter you should be able to:
\begin{todolist}
\item define all of the constitutive laws;
\item solve problems involving Newton's laws, Hooke's law and simple harmonic motion;
\item convert a system of population interactions into reaction equations;
\item convert reaction equations into ODEs using the Law of Mass Action.
\end{todolist}


\subsection{AIC, AICc, Bayes factor, BIC}


\chapter{BATTERIES OF TESTS}

This chapter describes predefined batteries of tests available
in TestU01.
% Some batteries are more suitable for generators returning uniform
%  floating-point numbers in the interval $[0, 1)$,
% while others are more oriented towards random bit generators.
Some batteries are fast and small, and may be used as a first step in
detecting gross defects in generators or errors in their implementation.
Other batteries are more stringent and take longer to run.
%  They can often detect flaws in low or medium quality RNGs.
Special batteries are also available to test a stream of random bits
taken from a file.
% One has been designed specially for hardware random bit generators.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph*{An example: The battery SmallCrush applied to a generator.} \

Figure~\ref{fig:bat1.c} shows how to apply a battery of tests to a
generator. The function call to {\tt ulcg\_CreateLCG} creates and initializes the
generator {\tt gen} to the linear congruential generator (LCG) with
modulus $m$ = 2147483647, multiplier $a$ = 16807, additive constant $c=0$,
and initial state $x_0 = 12345$.
% This LCG is still widely used in commercial software.
Then the small battery {\tt SmallCrush}, defined in module
{\tt bbattery}, is applied on this generator.
Figure~\ref{fig:bat1.res} shows a summary report of the results
(assuming that 64-bits integers are available; otherwise, the results could
be slightly different).
Out of the 15 tests applied, the generator failed three with a $p$-value
practically equal to 0, so it is clear that it failed this battery.
It took 20.3 seconds to run this battery on a machine with a
2400 MHz Athlon processor running under Linux.

\setbox0=\vbox {\hsize = 6.0in
\smallc
\verbatiminput{../examples/bat1.c}
}

\begin{figure}[hbt] \centering \myboxit{\box0}
\caption{Applying the battery SmallCrush on a LCG generator.}
  \label{fig:bat1.c}
\end{figure}

\setbox1=\vbox {\hsize = 6.0in
\smallc
\verbatiminput{../examples/bat1.res}
}

\begin{figure}[ht] \centering \myboxit{\box1}
\caption{Results of applying SmallCrush.}  \label{fig:bat1.res}
\end{figure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph*{Another example: The battery Rabbit applied to a binary file.} \

Figure~\ref{fig:bat2.c} shows how to apply the battery
{\tt Rabbit} to a binary file (presumably, a file of random bits).
The tests will use at most 1048576 ($=2^{20}$) bits from the
binary file named {\tt vax.bin}.
(Incidentally, these bits were obtained by taking the 32 most significant
bits from each uniform number generated by
the well-known LCG with parameters $m = 2^{32}$, $a = 69069$ and $c=1$.
 This was the random number generator used under VAX/VMS.)
Since the variable {\tt swrite\_Basic} is set to {\tt FALSE},
no detailed output is written, only the summary report shown in
Figure~\ref{fig:bat2.res} is printed after running the tests.
Seven tests were failed with a $p$-value practically equal to 0 or 1.
It is clear that the null hypothesis $\cH_0$ must be rejected for
 this bit stream.
It took 1.9 seconds to run the entire battery on a machine with a
2400 MHz Athlon processor running under Linux.

\setbox0=\vbox {\hsize = 6.0in
\smallc
\verbatiminput{../examples/bat2.c}
}

\begin{figure} \centering \myboxit{\box0}
\caption{Applying the battery Rabbit on a file of random bits.}
  \label{fig:bat2.c}
\end{figure}

\setbox1=\vbox {\hsize = 6.0in
\smallc
\verbatiminput{../examples/bat2.res}
}

\begin{figure} \centering \myboxit{\box1}
\caption{Results of applying Rabbit.}  \label{fig:bat2.res}
\end{figure}