% !TEX root = ../thesis.tex

\chapter{Preliminaries}
\label{chap:preliminaries}
There are several topological spaces that play a key role
when describing properties of the Hopf map $h : S^3 \to S^2\!$.
These include of course the domain $S^3$ and codomain $S^2\!$,
which are traditionally defined as subspaces of $\R^4$ and $\R^3$ respectively.
As we will see later,
it is useful to consider $S^3$ and $S^2$ as quotient spaces of $\C^2 \setminus \set{0}$
or subspaces of the quaternion algebra $\H$ instead.
Both $\C^2$ and $\H$ are isomorphic to $\R^4$ as a real vector space,
but their additional structure sheds light on various properties of the Hopf map.
We will therefore briefly examine these spaces before defining the Hopf map.
In later chapters we will explore the differentiable structure of these spaces,
but for now we focus on their topological properties.
Furthermore, group actions are used extensively throughout this chapter,
so we quickly recall some of the terms involved.

\section{Definitions}

\definition
Let $X$ be a set with additional structure,
such as a vector space or a topological space.
The \emph{automorphism group} of $X$,
denoted $\Aut(X)$ is the group of bijections $X \to X$
that preserve its structure.
More formally we can say that $X$ is an object in a concrete category $\mathcal{A}$,
a category equipped with a faithful functor $\mathcal{A} \to \mathbf{Sets}$,
the forgetful functor.
The automorphism group is the group of invertible morphisms $X \to X$
in this category.

\example
For a group $G$, $\Aut(G)$ is the group of group isomorphisms $G \to G$.
For a module $M$ over a ring $R$,
or for a vector space $V$ over a field $F$,
the automorphism group consists of $R$-linear bijections $M \to M$
and $F$-linear bijections $V \to V$ respectively.
For a topological space $X$,
$\Aut(X)$ is the group of homeomorphisms $X \to X$.
For sets, the automorphism group is simply the group of bijections
from the set to itself.

\definition
Let $G$ be a group and $X$ a set with additional structure.
A \emph{group action} of $G$ on $X$ is a group homomorphism $\phi : G \to \Aut(X)$.
If $X$ is an object in the category $\mathcal{A}$ \,(e.g. the category of vector spaces,
topological spaces, etc.),
we say $G$ acts on $X$ in this category.
Given an element $x \in X$ and $g \in G$,
we will often write $g \cdot x$ for the element $\phi(g)(x)$.

In the definition above,
the group $G$ acts \emph{from the left} on $X$.
For $g, h \in G$ and $x \in X$,
we have $(gh) \cdot x = g \cdot (h \cdot x)$.
Sometimes we encounter a natural \emph{antihomomorphism} $G \to \Aut(X)$.
In this case,
we say that $G$ acts \emph{from the right} on $X$ in the category $\mathcal{A}$,
and to make the notation more natural we write $x \cdot g$ instead of $g \cdot x$,
so that $x \cdot (gh) = (x \cdot g) \cdot h$.

\definition
Let $G$ and $X$ be as before, and $x \in X$.
The \emph{orbit} of $x$ is the set
\[ Gx = \set{ g \cdot x \mid g \in G } \]
Having the same orbit defines an equivalence relation on $X$,
and the quotient with respect to this relation is called the \emph{orbit space},
written $X/G$.

\definition
Let $G$ and $X$ be as before, and $x \in X$.
The \emph{stabiliser} of $x$ is the subgroup
\[ G_x = \set{ g \in G \mid g \cdot x = x } \]

\proposition[prop:open-quotient-map]
Let $X$ be a topological space, $G$ a group that acts on $X$.
When $X/G$ is endowed with the quotient topology,
the quotient map $q : X \surj X/G$ is an open map.

\proof
Let $U \subseteq X$ be open,
define $V = q(U)$.
$V$ is open if and only if $q^{-1}(V)$ is open
by definition of the quotient topology.
We have
\[ q^{-1}(V)
 = \bigcup_{g \in G} g \cdot U \]
Because the automorphisms of the group action are homeomorphisms,
they are open maps,
so $g \cdot U$ is open for all $g \in G$.
Hence, $q^{-1}(V)$ is the union of open sets,
so it is open.
\qed

\definition
A \emph{topological group} is a group $G$ that is also a Hausdorff space,
such that the map $G \times G \to G$, $(g, h) \mapsto gh^{-1}$ is continuous
when $G \times G$ is endowed with the product topology.
This is equivalent to the statement that multiplication and inversion are continuous;
see for example \parencite[ch.~\textsc{iii}, \S~1.1]{bourbaki1971} or \parencite[p.~276]{szekeres2004}.

\definition
Let $G$ be a topological group and $X$ a topological space,
such that $G$ acts on $X$ in the category of sets.
The action is said to be \emph{continuous} if
\[ G \times X \longto X,
\quad (g, x) \longmapsto g \cdot x \]
is a continuous map.
It follows immediately from this definition that
$x \mapsto g \cdot x$ is a homeomorphism for all $g \in G$ when $G$ acts continuously on $X$,
therefore $G$ acts on $X$ in the category of topological spaces;
the group homomorphism $G \to \Aut(X)$ is actually a group homomorphism $G \to \Homeo(X)$.

\theorem{Universal property of the quotient topology}[thm:universal-property-quotient-topology]
Let $X$ and $Y$ be topological spaces and $\sim$ an equivalence relation on $X$.
Denote by $q : X \surj X\modsim$ the quotient map.
Let $f : X \to Y$ be a continuous map such that
for all $x, y \in X$ it holds that $x \sim y$ implies $f(x) = f(y)$.
(Such $f$ is said to be \emph{compatible} with the equivalence relation.)
Then there exists a unique continuous map $g : X\modsim \to Y$ that makes the following diagram commute:
\vspace{-\parskip}
\begin{center}
\begin{tikzcd}[column sep = small] &
X        \ar[dl, two heads, swap, "q"]
         \ar[dr, "f"] & \\
X\modsim \ar[rr, dashed, swap, "\exists_! g"] & & Y
\end{tikzcd}
\end{center}

\proof
See for example \parencite[ch.~\textsc{i}, \S~3.4]{bourbaki1971}.

\proposition
Let $G_1$ and $G_2$ be groups, and $X$ a set.
Suppose that $G_1 \times G_2$ acts on $X$ in the category of sets.
This implies that the subgroups $G_1$ and $G_2$ act on $X$ individually as well.
Then the following holds:
\begin{enumerate}
\item There is a natural action of $G_1$ on $X / G_2$ in the category of sets.
\item If $X$ is a topological space and $G_1 \times G_2$ acts in the category of topological spaces,
      $G_1$ acts on $X / G_2$ in this category.
\item If $G_1$ and $G_2$ are topological groups such that $G_1 \times G_2$ acts continuously on $X$,
      then $G_1$ acts continuously on $X / G_2$.
\end{enumerate}

\proof
Let $x \in X$ such that $[x] \in X / G_2$, and $g \in G_1$.
Define $g \cdot [x] = [g \cdot x]$.
We have to show that this action is well-defined.
Suppose that $[x] = [y]$ for some $y \in X$.
Then there exists an $h \in G_2$ such that $x = h \cdot y$.
Because $h$ and $g$ commute in $G_1 \times G_2$,
we have
\[ g \cdot x = g \cdot (h \cdot y) = (g, h) \cdot y = h \cdot (g \cdot y) \]
Thus, we find $[g \cdot x] = [g \cdot y]$.
That this defines a homomorphism $G_1 \to \Aut(X / G_2)$ follows
from the fact that $G_1 \to \Aut(X)$ is a homomorphism.
This proves statement \textbbf{i}.

Suppose that $X$ is a topological space
and $G_1 \times G_2$ acts on $X$ in the category of topological spaces.
Let $g \in G_1$, then $g$ induces a homeomorphism $\phi : X \to X$,
and a bijection $\psi : X / G_2 \to X / G_2$.
Denote by $q : X \surj X / G_2$ the quotient map,
then $q \circ \phi$ is a continuous map $X \to X / G_2$
that satisfies $\psi \circ q = q \circ \phi$ due to statement \textbbf{i}.
This means that $q \circ \phi$ is compatible with the quotient map,
so by the universal property of the quotient topology (theorem \ref{thm:universal-property-quotient-topology}),
there exists a unique continuous map $\psi'$ such that $q \circ \phi = \psi' \circ q$.
Uniqueness implies that $\psi' = \psi$,
therefore $\psi$ is continuous.
The same argument applies to $\psi^{-1}$,
hence $\psi$ is a homeomorphism.
This shows that $G_1$ acts on $X / G_2$ in the category of topological spaces,
which proves statement \textbbf{ii}.

To prove statement \textbbf{iii},
we will use the following diagram:
\begin{center}
\begin{tikzcd}[row sep = large]
&
X \ar[dr, two heads, "q"] & \\
G_1 \times X           \ar[ur, "a"]
                       \ar[r, "f"]
                       \ar[dr, two heads, swap, "r"] &
G_1 \times {(X / G_2)} \ar[r, "T"] &
X / G_2 \\ &
{(G_1 \times X)} / G_2 \ar[u, dashed, "\exists_! \phi"]
                       \ar[ur, dashed, swap, "\exists_! \psi"] &
\end{tikzcd}
\end{center}
The map $a : G_1 \times X \to X$,
$(g, x) \mapsto g \cdot x$
is continuous because it is the restriction of $G_1 \times G_2 \times X \to X$
that is continuous by assumption.
Let $q: X \surj X / G_2$ denote the quotient map.
Define $f : G_1 \times X \to G_1 \times {(X / G_2)}$ as $f = (\id, q)$.
This map is continuous because both of its coordinates are.
(See proposition 1 of \parencite[ch.~\textsc{i}, \S~4.1]{bourbaki1971}.)
Furthermore $f$ is open,
for $\id$ and $q$ are open. (See proposition~\ref{prop:open-quotient-map}.)
Let $G_2$ act on $G_1 \times X$ by $h \cdot (g, x) = (g, h \cdot x)$
where $g \in G_1, h \in G_2, x \in X$,
and let $r$ denote the quotient map.
$f$ is compatible with $r$,
so by the universal property of the quotient topology (theorem \ref{thm:universal-property-quotient-topology}),
there exists a unique continuous map
$\phi$ that makes the bottom left triangle of the diagram commute.
The map is given by $[g, x] \mapsto (g, [x])$
and its inverse is given by $(g, [x]) \mapsto [g, x]$.
An open set in $(G_1 \times X) / G_2$
is the image under $r$ of an open set in $G_1 \times X$,
so from commutativity it follows that $\phi$ is an open map.
Hence, $\phi$ is a homeomorphism.

On the top of the diagram, we have the map $q \circ a : G_1 \times X \to X / G_2$,
given by $(g, x) \mapsto [g \cdot x]$.
As the composition of continuous maps it is continuous,
and it is compatible with $r$.
Thus, by the universal property of the quotient topology,
there exists a unique continuous map $\psi$
such that $\psi \circ r = q \circ a$.
Composing with $\phi^{-1}$, we find that the map
\[ T : G_1 \times {(X / G_2)} \longto X / G_2,
\quad (g, [x]) \longmapsto [g \cdot x] \]
is continuous,
which proves claim \textbbf{iii}.
Furthermore, the above diagram commutes.
\qed

\theorem[thm:quotient-map-factors]
Let $G_1$ and $G_2$ be groups and $X$ a topological space
such that $G_1 \times G_2$ acts on $X$.
Then $X / (G_1 \times G_2)$ is canonically homeomorphic to $(X / G_1) / G_2$.
In particular, the quotient map $X \surj X / (G_1 \times G_2)$ factors over $X / G_1$.

\proof
Let
$q_1 : X \surj X / G_1$,
$q_2 : (X / G_1) \surj (X / G_1) / G_2$,
and $q_{12} : X \surj X / (G_1 \times G_2)$ denote the quotient maps.
Then we have the following commutative diagram:
\begin{center}
\begin{tikzcd}[row sep = huge, column sep = large]
 X                    \ar[r,  two heads, "q_1"]
                      \ar[d,  two heads, "q_{12}"] &
 X / G_1              \ar[d,  two heads, "q_2"]
                      \ar[dl, dashed, bend right = 7, swap, "\phi_1"] \\
 X / (G_1 \times G_2) \ar[r,  dashed, bend left = 13, "\phi_{12}"] &
(X / G_1) / G_2       \ar[l,  dashed, bend left = 13, "\phi_2"]
\end{tikzcd}
\end{center}
The map $q_2 \circ q_1$ is continuous and compatible with $q_{12}$,
so by the universal property of the quotient topology (theorem \ref{thm:universal-property-quotient-topology})
there exists a unique continuous map $\phi_{12}$ that makes the diagram commute.
Because $q_{12}$ is compatible with $q_1$,
there exists a unique continuous map $\phi_1$ such that $q_{12} = \phi_1 \circ q_1$.
It follows that $\phi_1$ is compatible with $q_2$,
so there exists a unique continuous map $\phi_2$ that makes the diagram commute.
Now we see that $\phi_{12}$ and $\phi_2$ are continuous inverses of one another,
hence $X / (G_1 \times G_2)$ and $(X / G_1) / G_2$ are homeomorphic.
\qed

\subsection*{Physical interpretation}
Groups are prevalent in mathematics.
In physics, groups are often encountered in the context of symmetries.
In that case one may think of a group as a set of transformations of a system,
transformations under which a certain property is invariant.
For instance, angular momentum is invariant under rotation of space,
and four-momentum is invariant under Lorentz transformations.
A \emph{group action} generalises this idea.
Elements of the group induce a transformation of a system.
By applying all possible transformations to a point,
we obtain the \emph{orbit} of a point.
For instance, when we let the Lorentz group act on Minkowski space,
the orbit of a timelike vector is all of the light cone (past and future).
Often, a group encodes transformations that we are \emph{not} interested in.
The \emph{orbit space} is what remains if we consider points that differ
by such a transformation to be equal.
For example, the orbit space of the Lorentz group action on Minkowski space
consists of four elements:
the origin, the class of null (or light-like) vectors,
the class of timelike vectors,
and the class of spacelike vectors.
The \emph{stabiliser} of a point is the subgroup of transformations under which the point is invariant.

Topology is the branch of mathematics that studies abstract properties of space.
It gives us the tools to study properties that do not depend
on exact distances, but rather on overall shape.
For instance, one would like to think of a garden hose
as a one dimensional system where water can move back and forth,
regardless of how the hose is bent or twisted.
Topology allows us to ignore the bending and twisting.
Virtually all spaces that occur in physics are topological spaces:
$\R^3\!$, Minkowski space, Hilbert spaces, etc.
Often these spaces have additional structure
such as a metric or inner product,
but many properties can be derived from the topology alone.
An important example of such a property
is \emph{continuity} of a map between topological spaces,
a notion that is prevalent throughout physics.
Many of the groups encountered in this thesis happen to have a natural topology as well.
In this case, an action on another topological space can be \emph{continuous}.
The definition given in this section codifies our intuition:
if two group elements that are near act on a point,
the resulting points should be near as well.

\section{Projective space}
\label{sec:projective-space}
It is possible to identify $\R^4$ and $\C^2$ as four-dimensional real vector spaces,
by identifying the standard basis $(e_1, e_2, e_3, e_4)$ with the basis $((1, 0), (i, 0), (0, 1), (0, i))$.
The space $\C^2 \setminus \set{0}$ will be prevalent in the rest of this section,
so we introduce a shorthand notation. Furthermore, we embed $S^1$ in $\C$.

\definition
$\CZ = \C^2 \setminus \set{0}$.

\definition
The \emph{unit circle} is defined by
\[ S^1 = \set{ \, z \in \C \mid 1 = |\,z\,| \, } \]
This is a group under multiplication.

\definition[def:s3-real]
The \emph{three-sphere} is defined by
\[ S^3 = \set{ \, x \in \R^4 \mid 1 = \nsq{x} } \]
Here $\|{}\cdot{}\|$ denotes the regular Euclidean norm.
By identifying $\R^4$ with $\C^2$ as above,
we can consider $S^3$ to be a subset of $\CZ$.

Consider the multiplicative group $\Rpos$ of positive real numbers.
It acts continuously on $\C^2$ (in the category of real vector spaces) by scalar multiplication,
and this action can be restricted to $\CZ$ (in the category of sets).
This allows us to give an alternative definition of $S^3$ as a quotient:

\definition[def:s3-complex]
$\SC$ is the orbit space of $\CZ$ with respect to the $\Rpos$ action.
Denote by $r : \CZ \surj \SC$ the quotient map.
$\C^2$ is endowed with its regular topology induced by the Euclidean metric,
and $\SC$ is endowed with the quotient topology.

Intuitively, this definition is not that different from definition~\ref{def:s3-real}.
Every point $p$ at the three-sphere defines a ray from the origin through $p$.
This ray, except for the origin, is the orbit of $p$ under the $\Rpos$ action.
In other words, every orbit can be represented by a point at unit distance from the origin.
The quotient map $r$ corresponds to projection onto the sphere.

\proposition[prop:s3-equivalence]
$S^3$ and $\SC$ as defined in definition~\ref{def:s3-real} and \ref{def:s3-complex} are homeomorphic.

\proof
\newcommand*{\RZ}{{\R^4_{\,\circ}}}
Write $\R^4 \setminus \set{0} = \RZ$.
Let $i : S^3 \to \RZ$ be the inclusion,
and let $\phi : \R^4 \to \C^2$ be the vector space isomorphism
induced by the identification of the bases given earlier in this section.
The inclusion $i$ is continuous, and the restriction $\phi|_\RZ = \phi_\circ$ is a homeomorphism.
Therefore, the composition $\psi = r \circ \phi_\circ \circ i : S^3 \to \SC$ is continuous.
Consider the map
\vspace{-0.3\parskip}
\[ \CZ \longto S^3,
\quad x \longmapsto \phi_\circ^{-1} \left( \frac{x}{\|x\|} \right) \vspace{0.3\parskip} \]
which is continuous and compatible with $r$.
By the universal property of the quotient topology (theorem~\ref{thm:universal-property-quotient-topology}),
this map induces a unique continuous map $\psi^{-1} : \SC \to S^3$ that is the inverse of $\psi$.
Thus, $\psi$ is a homeomorphism.
\qed

Consider the multiplicative group $\C^*$ (the complex plane minus the origin).
It acts continuously on $\C^2$ (in the category of complex vector spaces) by scalar multiplication,
and this action can be restricted to $\CZ$.
This allows us to define the projective space:

\definition[def:complex-projective-line]
The \emph{complex projective line} $\PC$ is the orbit space of $\CZ$ with respect to the $\C^*$ action.
Denote by $q : \CZ \surj \PC$ the quotient map.
$\PC$ is endowed with the quotient topology.

Elements of $\PC$ are indicated by \emph{homogeneous coordinates}:
if $(z_1, z_2)  \in \C^2$ is nonzero,
then we write $(z_1 : z_2)$ for $q(z_1, z_2)$.
We can embed $\C$ in $\PC$ via $z \mapsto (z : 1)$.
The only point that is not reached in this manner is $(1 : 0)$.

\theorem[thm:s2-homeom-p1c]
There exists a homeomorphism between $S^2$ and $\PC$.

\proof
We will postpone the proof until section \ref{sec:hopf-quaternionic},
and prove this with the aid of quaternions in theorem~\ref{thm:hopf-map-equivalence}.
For an alternative proof, see \parencite[ch.~\textsc{viii}, \S~4.3]{bourbaki1974}.

The general linear group $\GLC$
of invertible complex $2 \times 2$ matrices
acts on $\C^2$ by matrix multiplication.
This induces a group action of $\GLC$ on $\CZ$.
Furthermore, the groups $\Rpos$, $S^1$, and $\C^*$ are isomorphic to subgroups of $\GLC$:
given an element $z \in \C^*\!$,
we can identify it with the matrix
\[ \begin{pmatrix} z & 0 \\ 0 & z \end{pmatrix} \]
in the centre of $\GLC$.
$\C^*$ is isomorphic to the direct product $\Rpos \times S^1$:
this is the decomposition of a complex number into its modulus and argument.
It follows that $S^1$ and $\Rpos$ are central in $\GLC$, because their elements correspond to scalar matrices.
Consequently, $S^1$ and $\Rpos$ are normal in $\GLC$.

\subsection*{Informal summary}
The \emph{projective space} $\PC$ is a construction with several interpretations.
For starters, $\PC$ can be thought of as $\C$ with one extra point,
a point “at infinity”.
This allows us to talk about $z_1/z_2$ even when $z_2$ is zero.
Instead of $z_1/z_2$, we write $(z_1 : z_2)$, called \emph{homogeneous coordinates}.
Secondly, theorem \ref{thm:s2-homeom-p1c} tells us that $\PC$ can be thought of as the unit sphere $S^2\!$.
(In fact, $\PC$ is sometimes called the \emph{Riemann sphere}.)
A \emph{homeomorphism} between two spaces
is a function, both one-to-one and onto,
that preserves all topological properties.
From a topological point of view, $\PC$ and $S^2$ are the same space.
This means that when we formulate the Hopf map later on
— a function from $S^3$ to $S^2$ —
we can express it as a function to $\PC$.
This expression is significantly simpler than the one involving Cartesian coordinates on $S^2\!$.

\section{Quaternions}
\label{sec:quaternions}

\definition
The \emph{quaternion algebra} $\H$ is
the real noncommutative algebra with basis $(1, i, j, k)$.
Multiplication is given by the identities
\[ i^2 = j^2 = k^2 = -1,
\quad i\!j =  k, \ jk =  i, \ ki =  j,
\quad j  i = -k, \ kj = -i, \ ik = -j \]
and $1$ commutes with all elements.
In particular, $\H$ is a ring and a four-dimensional real vector space.
Analogously to complex numbers,
this algebra has an involution $\overline{\raisebox{0pt}[0.5em]{${}\cdot{}$}}$
called \emph{conjugation}
that flips the sign of the $i$, $j$, and $k$ components.

\definition
The \emph{trace} is the map $\Tr : \H \to \R,\ q \mapsto q + \overline{q}$.
Because the imaginary parts cancel, the trace of a quaternion is real.
Furthermore, the trace is $\R$-linear.

The reals commute with all quaternions,
so $\Tr(q)$ commutes with $q$ for all $q \in \H$.
Because $\overline{q} = \Tr(q) - q$,
it follows that $q$ and $\overline{q}$ commute.

\definition
The standard inner product on $\H$ is given by
\[ \inp{{}\cdot{}}{{}\cdot{}} : \H \times \H \longto \R,
\quad (p, q) \longmapsto \tfrac{1}{2} \Tr(p\overline{q}) = \tfrac{1}{2}(p\overline{q} + q \overline{p}) \]
Symmetry is clear from the definition,
and bilinearity follows from the linearity of the trace.
For positive definiteness,
remark that for $q = a + bi + cj + dk$,
we have $q \overline{q} = a^2 + b^2 + c^2 + d^2\!$.
Therefore $\inp{q}{q} \geq 0$,
and $\inp{q}{q} = 0 \implies a = b = c = d = 0 \implies q = 0$.

\definition
The \emph{norm} of $q \in \H$ is given by $\nsq{q} = q \overline{q}$.
Because $q$ and $\overline{q}$ commute,
$q\overline{q} = \smallfrac{1}{2}(q \overline{q} + \overline{q} q)$,
so the norm is induced by the inner product.
This norm coincides with the Euclidean norm on $\H$
as real vector space with orthonormal basis $(1, i, j, k)$.
Therefore,
$\H$ with the topology induced by the norm is homeomorphic to $\R^4\!$.

Because conjugation reverses the order of multiplication,
the norm is multiplicative:
for $p, q \in \H$, we have
\[ \nsq{pq}
 = (pq)\overline{(pq)}
 = p \, q \overline{q} \, \overline{p}
 = p \nsq{q} \, \overline{p}
 = \nsq{q} p \overline{p}
 = \nsq{q} \nsq{p} \]

Because $q \overline{q} = \nsq{q}\!$,
we have $q^{-1} = \overline{q} \, \|q\|^{-2}$ for $\|q\| \neq 0$.
Therefore, $\H$ is a division algebra:
every nonzero element has an inverse.

\proposition
$\H^* = \H \setminus \set{0}$ is a topological group.

\proof
Multiplication is continuous,
because for $p, q \in \H^*\,$,
the components of the product $pq$
can be written as a polynomial in the components of $p$ and $q$.
Inversion is continuous,
because the components of $q^{-1}$ are rational functions of the components of $q$,
which do not vanish because $\nsq{q} \neq 0$.
See also \parencite[ch.~\textsc{viii}, \S~1.4]{bourbaki1974}.
\qed

With this machinery,
we can give a quaternionic definition of $S^3$ and $S^2\!$.
Whereas the definitions in section \ref{sec:projective-space} emphasise
how $S^3$ and $S^2$ are quotients with respect to a group action,
the quaternionic definitions emphasise the group structure on the three-sphere itself,
and the action of $S^3$ on $S^2\!$.

Let us revisit the three-sphere as defined in definition~\ref{def:s3-real}.
By identifying $\H$ with $\R^4$ as a normed real vector space
via the basis given earlier in this section,
we can consider $S^3$ to be a subset of $\H$,
the set of quaternions with unit norm:
\[ S^3 = \set{ q \in \H \mid 1 = \nsq{q} } \]
This set is closed under multiplication due to the multiplicativity of the norm,
and it contains $1$.
Therefore, this is a subgroup of $\H^*\!$.
We can embed $S^2$ in $S^3\!$,
but in $\R^4$ there is no preferred way of doing so.
For quaternions, there is one natural choice:

\definition[def:s2-quaternion]
The \emph{two-sphere} $S^2 = \set{ q \in S^3 \mid \Tr(q) = 0 }$,
the set of pure imaginary quaternions with unit norm.
This definition coincides with the conventional definition of $S^2$
when $\R^3$ is identified with the subspace of $\H$ spanned by $i$, $j$, and $k$.
$S^2$ may alternatively be written as $\set{q \in S^3 \mid \inp{1}{q} = 0} = 1^\perp \cap S^3$.

The group $\H^*$ acts
on $\H$ in the category of $\R$-algebras via the following homomorphism:
\[ \phi : \H^* \longto \Aut(\H),
   \quad
   p \longmapsto (q \mapsto p q p^{-1}) \]
Because quaternion multiplication is continuous,
this is a continuous action.
By restriction to the subgroup $S^3\!$,
we get a continuous action of $S^3$ on $\H$.

\proposition[prop:invariant-inner-product]
The inner product on $\H$ is invariant under the action of $\H^*\!$.

\proof
Let $p \in \H^*\!$, $q_1, q_2 \in \H$,
then we have
\begin{align*}
   2 \, \inp{p \cdot q_1}{p \cdot q_2}
&= p q_1 p^{-1} \overline{p q_2 p^{-1}} + p q_2 p^{-1} \overline{p q_1 p^{-1}} \\
&= p q_1 \nsq{p^{-1}} \overline{q_2} \, \overline{p} + p q_2 \nsq{p^{-1}} \overline{q_1} \, \overline{p} \\
&= \nsq{p^{-1}} p (q_1  \overline{q_2} + q_2 \overline{q_1}) \overline{p} \\
&= \nsq{p^{-1}} \, \nsq{p} \, 2 \, \inp{q_1}{q_2} \\
&= 2 \, \inp{q_1}{q_2}
   \tag*{\qed}
\end{align*}

\corollary[cor:s3-action]
Identify $\R^3$ with the subspace of $\H$ spanned by $i$, $j$, and $k$.
Then $\R^3 = 1^\perp$ and $S^2 = 1^\perp \cap S^3$ are invariant under the action of $\H^*\!$,
which means $\H^*$ and its subgroup $S^3$ act continuously on $\R^3$ and $S^2\!$.

$\C$ is a commutative subring of~$\H$.
As real vector spaces with bases $(1, i)$ and $(1, i, j, k)$,
$\C$ can be identified with the subspace of $\H$ spanned by $1$ and $i$.
The stabiliser of $i \in \H$ consists of the nonzero elements that commute with $i$.
These elements are linear combinations of $1$ and $i$,
so we have $\H^*_i = \C^*$ and $S^3_i = S^1\!$.

\proposition[prop:s3-isom-su2c]
$S^3$ is isomorphic to $\SUC$, the group of unitary $2 \times 2$ matrices with determinant $1$.

\proof
Define the unitary matrices
\[            I = \begin{pmatrix}1 &  0 \\ 0 &  1\end{pmatrix}
\qquad \sigma_1 = \begin{pmatrix}0 &  1 \\ 1 &  0\end{pmatrix}
\qquad \sigma_2 = \begin{pmatrix}0 & -i \\ i &  0\end{pmatrix}
\qquad \sigma_3 = \begin{pmatrix}1 &  0 \\ 0 & -1\end{pmatrix} \]
These matrices are sometimes called the \emph{Pauli spin matrices}.
Let $\phi: \H \to \Mat(2 \times 2, \C)$ be the $\R$-linear extension of
\[    1 \longmapsto I,
\quad i \longmapsto i \sigma_1,
\quad j \longmapsto i \sigma_2,
\quad k \longmapsto i \sigma_3 \]
Let $\psi$ be the restriction of $\phi$ to $S^3\!$.
All matrices in the image of $\psi$ are unitary,
and a little computation shows that for $q \in S^3\!$,
$\det \psi(q) = 1$.
The matrices $I, i\sigma_1, i\sigma_2, i\sigma_3$
satisfy the same multiplication rules as $1, i, j, k$.
That is, $i\sigma_1 i\sigma_2 = i\sigma_3$, etc.
Therefore, $\psi$ is a group homomorphism $S^3 \to \SUC$.
This homomorphism is surjective (see \parencite[p.~173]{szekeres2004}),
and $1$ is the only element in its kernel.
Therefore, $\psi$ is an isomorphism.
\qed

\theorem[thm:s3-surj-so3r]
The map
\[ \phi: S^3 \longto \SOR,
\quad q \longmapsto (x \mapsto q \cdot x) \]
is a surjective group homomorphism with kernel $\set{\pm 1}$.
Here $x \in \R^3 \cong \Span(i, j, k)$.

\proof
The map $x \mapsto q \cdot x$ is linear,
and orthogonality follows from
the fact that the inner product is invariant under the action,
as shown in proposition~\ref{prop:invariant-inner-product}.
To show that $x \mapsto q \cdot x$ is not a reflection,
note that $\det : \textup{O}_3(\R) \to \R$ is a continuous map
(see for example \parencite[p.~281]{hatcher2002}).
We can express $\phi$ as a polynomial on all coordinates
when elements of $\SOR$ are written as matrices,
so $\phi$ is continuous.
By composition we get a continuous map $S^3 \to \set{\pm 1}$.
Because $S^3$ is connected, this map must be constant.
% (See for instance proposition~3.4.11 of \parencite[p.~92]{runde2005}.)
The determinant of $\id$ is $1$,
so all $q \in S^3$ induce an orthogonal map with positive determinant.

To show that the kernel of $\phi$ is $\set{\pm 1}$,
suppose that $q \in S^3$ is such that $q \cdot x = q x q^{-1} = x$ for all $x \in \R^3\!$.
Then $q$ commutes with all $x \in \R^3\!$,
so $q$ must be real.
Because $\nsq{q} = 1$, it follows that $q = 1$ or $q = -1$.

To prove surjectivity,
suppose that $\rho \in \SOR$ is an anticlockwise rotation
of $\alpha$ radians about an axis spanned by $u \in \R^3\!$,
where $\nsq{u} = 1$.
Then the quaternion $q = \cos(\smallfrac{1}{2}\alpha) + u \sin(\smallfrac{1}{2}\alpha)$
will map to $\rho$.
To see this,
note that all points on the axis of rotation are fixed points,
for $u$ commutes with $q$.
Furthermore, suppose that $v \in \R^3$ is such that $\inp{u}{v} = 0$.
Set $q_0 = \cos(\smallfrac{1}{2}\alpha)$ and $\vec{q} = u \sin(\smallfrac{1}{2}\alpha)$.
By using identities from \parencite[p.~157]{szekeres2004},
we find
\newcommand*{\vv}{v} % Could switch to v with an arrow, but to me it is just noise.
\newcommand*{\vq}{\vec{q}}
\begin{align*}
q \cdot v &= (q_0 + \vq) \vv (q_0 - \vq)
%      \\ &= (q_0 + \vq) (\inp{v}{\vq} + q_0 \vec{v} - \vec{v} \times \vq)
       \\ &= (q_0 + \vq) (q_0 \vv - \vv \times \vq)
       \\ &= -\inp{\vq}{q_0 \vv - \vv \times \vq} + q_0 (q_0 \vv - \vv \times \vq) + \vq \times {(q_0 \vv - \vv \times \vq)}
       \\ &= q_0^2 \vv - q_0 \vv \times \vq + q_0 \vq \times \vv - \vq \times {(\vv \times \vq)}
       \\ &= q_0^2 \vv - 2 q_0 \vv \times \vq - \vv \inp{\vq}{\vq} + \vq \inp{\vq}{\vv}
       \\ &= (\cos^2(\smallfrac{1}{2}\alpha) - \sin^2(\smallfrac{1}{2}\alpha)) v
           - 2 \cos(\smallfrac{1}{2}\alpha)\sin(\smallfrac{1}{2}\alpha) \, {v \times u}
       \\ &= \cos(\alpha) v + \sin(\alpha) \, {u \times v}
\end{align*}
This demonstrates that $q$ rotates $v$ anticlockwise by $\alpha$ radians about $u$.
We saw already that $x \mapsto q \cdot x$ is an orthogonal map with determinant $1$.
Therefore, $q$ maps to $\rho$.
\qed

\corollary[cor:transitive-s3-action]
$S^3$ acts transitively on $S^2\!$,
for every point on $S^2$ can be mapped
into any other point on $S^2$ by a rotation of the sphere.

The proof of theorem~\ref{thm:s3-surj-so3r} gives us a way to explicitly get
a $q \in S^3$ such that $q \cdot i = p$ for any $p \in S^2$:
we rotate $i$ onto $p$ with a rotation of $\R^3\!$.
If $p = -i$, $q = j$ will suffice,
so suppose $p \neq -i$.
Then an axis that we can rotate about is the one spanned by $i + p$,
which bisects the angle between $i$ and $p$,
so we need to rotate by $\pi$ radians.
We find
\begin{equationref}
\label{eqn:transitive-s3-action}
q = \frac{i + p}{\|i + p\|}
\end{equationref}
To verify that this works,
note that for $p \in S^2$ we have
$p \overline{p} = 1$ and $\overline{p} = -p$,
so $p^2 = -1$.
It then follows that
\[ p^2 = -1
% \implies pi + p^2 = -1 + pi
\enskip \implies \enskip p(i + p) = (i + p)i
\enskip \implies \enskip p(i + p)\overline{(i + p)} = (i + p)i\overline{(i + p)} \]
Multiplying by $\|i + p\|^{-2}$ on both sides then yields $p = q i q^{-1}$.

\subsection*{Physical interpretation}
Just like complex numbers are an extension of the real numbers,
quaternions are an extension of the complex numbers.
These extensions come at a cost:
when going from $\R$ to $\C$, you have to give up the ordering.
When going from $\C$ to $\H$, you have to give up commutativity.
Apart from their rich structure that is interesting in its own right,
quaternions have many useful applications.
By considering $S^3$ as a subset of $\H$,
it inherits a group structure.
Theorem~\ref{thm:s3-surj-so3r} tells us that this group is in a sense
twice $\SOR$: every rotation of $\R^3$ is represented by two antipodal quaternions.
When traversing a great circle through $1$ in $S^3\!$,
the points $1$ and $-1$ both correspond to the identity in $\SOR$.
This path in $S^3$ corresponds to a $4\pi$ rotation of $\R^3\!$,
and after a $2\pi$ rotation we will have moved from $1 \in S^3$ to $-1 \in S^3\!$.
This property is reminiscent of \emph{spinors},
and indeed proposition~\ref{prop:s3-isom-su2c} links the unit quaternions to the Pauli spin matrices.


\chapter{Filtering}
\label{sec:filtering}
\section{Curvelet Coefficient Thresholding}
We now apply our digital transforms for
removing noise from image data. The methodology is standard and is
outlined mainly for the sake of clarity and self-containedness.

Suppose that one is given noisy data of the form
\[
x_{i,j} = f(i,j) + \sigma z_{i,j},
\]
where $f$ is the image to be recovered and $z$ is white noise, i.e.
$z_{i,j} \stackrel{i.i.d.}{\sim} N(0,1)$. Unlike FFTs or FWTs, our
discrete ridgelet (resp. curvelet) transform is not norm-preserving
and, therefore, the variance of the noisy ridgelet (resp. curvelet)
coefficients will depend on the ridgelet (resp. curvelet) index
$\lambda$.  For instance, letting $F$ denote the discrete curvelet
transform matrix, we have $F z \stackrel{i.i.d.}{\sim} N(0,F F^T)$
where $T$ denotes transpose.
Because the computation of $F F^T$ is prohibitively expensive, we
calculated an approximate value $\tilde{\sigma}^2_\lambda$ of the
individual variances using Monte Carlo simulations where the diagonal
elements of $F F^T$ are simply estimated by evaluating the curvelet
transforms of a few standard white noise images.

Let $y_\lambda$ be the noisy curvelet coefficients
($y = F x$). We use the following hard-thresholding rule
for estimating the unknown curvelet coefficients:
\begin{eqnarray}
\hat y_\lambda = y_\lambda  & \mbox{ if }  &
|y_\lambda|/\sigma \geq k \tilde{\sigma}_\lambda\\
\hat y_\lambda = 0 &  \mbox{ if } & |y_\lambda|/\sigma
<  k \tilde{\sigma}_\lambda.
\end{eqnarray}
In our experiments, we actually chose a scale-dependent value for $k$;
we have $k = 4$ for the first scale $(j = 1)$ while $k = 3$
for the others $(j > 1)$.

\subsection*{Poisson Observations}
Assume now that we have Poisson data $x_{i,j}$ with unknown mean
$f(i,j)$.  The Anscombe transformation \cite{rest:anscombe48}
\begin{equation}
\tilde{x} = 2\sqrt{x + \frac{3}{8}}
\end{equation}
stabilizes the variance and we have $\tilde{x} = 2 \sqrt{f} +
\epsilon$ where $\epsilon$ is a vector with independent and
approximately standard normal components. In practice, this is a good
approximation whenever the number of counts is large enough, greater
than 30 per pixel, say.

For a small number of counts, a possibility is to compute the Radon
transform of the image, and then to apply the Anscombe transformation
to the Radon data. The rationale is that, roughly speaking, the
Radon transform corresponds to a summation of pixel values over lines
and  the sum of independent Poisson random variables is a Poisson
random variable with intensity equal to the sum of the individual
intensities. Hence, the intensity of the sum may be quite large (hence
validating the Gaussian approximation) even though the individual
intensities may be small. This might be viewed as an interesting
feature  since,  unlike wavelet transforms, the ridgelet and curvelet
transforms tend to average data over elongated and rather large
neighborhoods.

\section{Filtering Experiments}

\subsubsection{Recovery of Linear Features}

\begin{figure}[htb]
\centerline{
\vbox{
\hbox{
\psfig{figure=fig_line.ps,bbllx=1cm,bblly=12cm,bburx=15cm,bbury=26cm,width=7.5cm,height=7.5cm,clip=}
\psfig{figure=fig_line_g0p5.ps,bbllx=1cm,bblly=12cm,bburx=15cm,bbury=26cm,width=7.5cm,height=7.5cm,clip=}
}
\hbox{
\psfig{figure=fig_f24_line_g0p5.ps,bbllx=1cm,bblly=12cm,bburx=15cm,bbury=26cm,width=7.5cm,height=7.5cm,clip=}
\psfig{figure=fig_cur_line_g0p5.ps,bbllx=1cm,bblly=12cm,bburx=15cm,bbury=26cm,width=7.5cm,height=7.5cm,clip=}
}}}
\caption{The top panels display a geometric image and that same image
      contaminated with Gaussian white noise. The bottom left and right
      panels display the restored images using the undecimated wavelet
      transform and the curvelet transform, respectively.}
\label{fig_cur_line}
\end{figure}
The next experiment (Figure~\ref{fig_cur_line}) consists of an
artificial image containing a few bars, lines and a square.  The
intensity is constant along each individual bar; from left to right,
the intensities of the ten vertical bars (these are in fact thin
rectangles which are 4 pixels wide and 170 pixels long) are equal to
${32 \over 2^i}$, $i = 0, \ldots 9$.  The intensity along all the
other lines is equal to 1, and the noise standard deviation is $1/2$.
Displayed images have been log-transformed in order to better see
the results at low signal to noise ratio.

The curvelet reconstruction of the nonvertical lines is obviously
sharper than that obtained using wavelets.  The curvelet transform
also seems to go one step further as far as the reconstruction of the
vertical lines is concerned. Roughly speaking, for those templates,
the wavelet transform stops detecting signal at a SNR equal to 1
(we defined here the SNR as the intensity level of the pixels
on the line, divided by the noise standard deviation of the noise)
while the cut-off value equals 0.5 for the curvelet approach.  It is
important to note that the horizontal and vertical lines correspond to
privileged directions for the wavelet transform, because the underlying
basis functions are direct products of functions varying solely in
the horizontal
and vertical directions.  Wavelet methods will
give even poorer results on lines
of the same intensity but tilting substantially
away from the Cartesian axes.
Cf.\ the reconstructions of the faint diagonal
lines in the image.

\subsubsection{Recovery of Curves}
In this experiment (Figure~\ref{fig_cur_picasso}), we have added
Gaussian noise to ``War and Peace,'' a drawing from Picasso which
contains many curved features.  Figure \ref{fig_cur_picasso} bottom left
and right show respectively the restored images by the undecimated
wavelet transform and the curvelet transform. Curves are more sharply
recovered with the curvelet transform.

The authors are working on new methods (some of which will be based on
the curvelet transform) to extract and recover curves from noisy data
with greater accuracy and, therefore, this example is merely to be
taken for illustrative purposes.

\begin{figure}[htb]
\centerline{
\vbox{
\hbox{
\psfig{figure=fig_picasso.ps,bbllx=2cm,bblly=13cm,bburx=18cm,bbury=25cm,width=8cm,height=6cm,clip=}
\psfig{figure=fig_picasso_g50.ps,bbllx=2cm,bblly=13cm,bburx=18cm,bbury=25cm,width=8cm,height=6cm,clip=}
}
\hbox{
\psfig{figure=fig_picasso_wt.ps,bbllx=2cm,bblly=13cm,bburx=18cm,bbury=25cm,width=8cm,height=6cm,clip=}
\psfig{figure=fig_picasso_cur.ps,bbllx=2cm,bblly=13cm,bburx=18cm,bbury=25cm,width=8cm,height=6cm,clip=}
}}}
\caption{The top panels display a Picasso picture (War and Peace)
      and that same image
      contaminated with Gaussian white noise. The bottom left and right
      panels display the restored images using the undecimated wavelet
      transform and the curvelet transform respectively.}
\label{fig_cur_picasso}
\end{figure}

\subsubsection{Denoising of a Color Image}

\begin{figure}[htb]
\centerline{
\vbox{
\hbox{
\psfig{figure=fig_cmp_lena_rgb.ps,bbllx=2cm,bblly=13cm,bburx=20cm,bbury=25cm,width=6cm,height=4.cm,clip=}
\psfig{figure=fig_cmp_pepper_rgb.ps,bbllx=2cm,bblly=13cm,bburx=20cm,bbury=25cm,width=6cm,height=4.cm,clip=}
}
\hbox{
\psfig{figure=fig_cmp_mandrill_rgb.ps,bbllx=2cm,bblly=13cm,bburx=20cm,bbury=25cm,width=6cm,height=4.cm,clip=}
\psfig{figure=fig_cmp_barbara_rgb.ps,bbllx=2cm,bblly=13cm,bburx=20cm,bbury=25cm,width=6cm,height=4.cm,clip=}
}
}}
\caption{PSNR versus noise standard deviation using different filtering
      methods. YUV and curvelet, YUV and undecimated wavelet, and YUV and
      decimated wavelet transforms are represented respectively with a
      continuous, dashed, and dotted line.  The upper left panel
      corresponds to {\tt Lena} (RGB), the upper right to {\tt pepper}
      (RGB), the bottom left to {\tt Baboon} (RGB), and the bottom right
      to {\tt Barbara} (RGB).}
\label{fig_exp_rgb_curv}
\end{figure}

In a wavelet based denoising scenario, color RGB images are generally
mapped into the YUV space, and each YUV band is then filtered
independently from the others. The goal here is to see whether the
curvelet transform would give improved results. We used four of the
classical color images, namely {\tt Lena}, {\tt Peppers}, {\tt
      Baboon}, and {\tt Barbara} (all images except perhaps {\tt Barbara}
are available from the USC-SIPI Image Database \cite{SIPI}).
We performed
a series of experiments and
summarized our findings in Figure~\ref{fig_exp_rgb_curv} which again
displays the PSNR (peak signal-to-noise ratio)
versus the noise standard deviation for the four
images.

In all cases, the curvelet transform outperforms the wavelet
transforms in terms of PSNR -- at least for moderate and large values
of the noise level.  In addition, the curvelet transform outputs
images that are visually more pleasant.

\subsubsection{Saturn Rings}

\begin{figure}[htb]
\centerline{
\vbox{
\hbox{
\psfig{figure=SATURN/fig_sat_sub_g20.ps,bbllx=1.8cm,bblly=12.7cm,bburx=14.5cm,bbury=25.4cm,width=7cm,height=7cm,clip=}
\psfig{figure=SATURN/fig_sat_sub_fil_owt.ps,bbllx=1.8cm,bblly=12.7cm,bburx=14.5cm,bbury=25.4cm,width=7cm,height=7cm,clip=}
}
\hbox{
\psfig{figure=SATURN/fig_sat_sub_fil_atrou.ps,bbllx=1.8cm,bblly=12.7cm,bburx=14.5cm,bbury=25.4cm,width=7cm,height=7cm,clip=}
\psfig{figure=SATURN/fig_sat_sub_fil_cur.ps,bbllx=1.8cm,bblly=12.7cm,bburx=14.5cm,bbury=25.4cm,width=7cm,height=7cm,clip=}
}}
}
\caption{Top left, part of Saturn image with Gaussian noise. Top right,
filtered image using the undecimated bi-orthogonal wavelet transform.
Bottom left and right, filtered image by the \`a trous wavelet
transform algorithm and the curvelet transform.}
\label{fig_sub_saturn_cur_filter}
\end{figure}

Gaussian white noise with a standard deviation
fixed to 20 was added to the {\tt Saturn} image.
We employed several methods to filter the noisy image:
\begin{enumerate}
\item Thresholding of the Curvelet transform.
\item Bi-orthogonal undecimated wavelet de-noising methods using  the
      Dau\-che\-chies-An\-to\-ni\-ni 7/9 fil\-ters (FWT-7/9) and hard thresholding.
\item A trous wavelet transform algorithm and hard thresholding.
\end{enumerate}
Our experiments are reported in Figure~\ref{fig_sub_saturn_cur_filter}.  The
curvelet reconstruction does not contain the quantity of disturbing artifacts
along edges that one sees in wavelet reconstructions. An examination
of the details of the restored images % (Figure~\ref{fig_sub_saturn_cur_filter})
is instructive. One notices that the decimated wavelet transform
exhibits distortions of the boundaries and suffers substantial loss of
important detail.  The \`a trous  wavelet transform gives better
boundaries, but completely omits to reconstruct certain ridges.
In addition, it exhibits numerous small-scale embedded
blemishes; setting higher thresholds to avoid these blemishes would
cause even more of the intrinsic structure to be missed.

% Further results are visible at the following URL: {\tt
%  http://www-stat.stanford.edu/$\sim$jstarck}.

\subsubsection{Supernova with Poisson noise}

\begin{figure}[htb]
\centerline{
\hbox{
\psfig{figure=fig_kepler1604.ps,bbllx=1.8cm,bblly=12.7cm,bburx=14.5cm,bbury=25.4cm,width=7cm,height=7cm,clip=}
\psfig{figure=fig_kepler1604_fil_rid.ps,bbllx=1.8cm,bblly=12.7cm,bburx=14.5cm,bbury=25.4cm,width=7cm,height=7cm,clip=}
}}
\caption{Left, XMM/Newton image of the Kepler SN1604 supernova.
Right, ridgelet filtered image.}
\label{fig_sn1604}
\end{figure}
Figure~\ref{fig_sn1604} shows an example of an X-ray image filtering by the
ridgelet transform using such an approach. Figure~\ref{fig_sn1604} left
and right
show respectively the  XMM/Newton image of the Kepler SN1604 supernova and
the ridgelet filtered image (using a five sigma hard thresholding).

% \clearpage
% \newpage


\BoSSSopen{tutorial9-SIP/sip}
\graphicspath{{tutorial9-SIP/sip.texbatch/}}

\BoSSScmd{
/// \section*{What's new}
/// \begin{itemize}
/// \item Symmetric Interior Penalty method (SIP)
/// \item investigating matrix properties
/// \end{itemize}
/// \section*{Prerequisites}
/// \begin{itemize}
/// \item basics SIP method
/// \item spatial operator, chapter \ref{SpatialOperator}
/// \item implementing numerical fluxes and convergence study, chapter \ref{NumFlux}
/// \end{itemize}
///
///%_______________________________________________________
/// \section{Problem statement}
/// We consider the 2D Poisson problem:
/// \begin{equation*}
/// \Delta u = f(x,y)
/// \end{equation*}
/// with $f(x,y)\neq 0$ is an arbitrary function of $x$ and/or $y$ or constant.
/// Within this exercise, we are going to investigate the Symmetric Interior Penalty discretization method (SIP) for the Laplace operator:
/// \begin{equation*}
/// a_{\text{sip}}(u,v)
/// = \int_{\domain} \underbrace{\nabla u \cdot \nabla v}_{\text{Volume\ term}}\dV
///   - \oint_{\Gamma \setminus \Gamma_{N }} \underbrace{
///         \mean {\nabla u} \cdot n_{\Gamma}\jump{v}
///      }_{\text{consistency\ term}} + \underbrace{
///         \mean{\nabla v} \cdot \vec{n}_{\Gamma} \jump{u}
///      }_{\text{symmetry\ term}} \dA
///   + \oint_{\Gamma \setminus \Gamma_{N}} \underbrace{
///        \eta \jump{u}\jump{v}
///     }_{\text{penalty\ term}} \dA
/// \end{equation*}
/// The use of these fluxes including a penalty term stabilizes the DG-discretization for the Laplace operator.
///
///%____________________________________________
/// \section{Solution within the \BoSSS{} framework}
/// First, we initialize the new worksheet:
 }
\BoSSSexeSilent
\BoSSScmd{
restart;
 }
\BoSSSexeSilent
\BoSSScmd{
/// We need the following packages:
using ilPSP.LinSolvers;\newline
using ilPSP.Connectors.Matlab;
 }
\BoSSSexe
\BoSSScmdSilent{
/// BoSSScmdSilent BoSSSexeSilent
using NUnit.Framework;
 }
\BoSSSexeSilent
\BoSSScmd{
/// \subsection{Implementation of the SIP fluxes}
/// We are going to implement the SIP-form
/// \begin{equation*}
/// a_{\text{sip}}(u,v)
/// = \int_{\domain} \underbrace{\nabla u \cdot \nabla v}_{\text{volume\ term}}\dV
///   - \oint_{\Gamma \setminus \Gamma_{N }} \underbrace{
///         \mean {\nabla u} \cdot n_{\Gamma}\jump{v}
///      }_{\text{consistency\ term}} + \underbrace{
///         \mean{\nabla v} \cdot \vec{n}_{\Gamma} \jump{u}
///      }_{\text{symmetry\ term}} \dA
///   + \oint_{\Gamma \setminus \Gamma_{N}} \underbrace{
///        \eta \jump{u}\jump{v}
///     }_{\text{penalty\ term}} \dA
/// \end{equation*}
/// First, we need a class in which the integrands are defined.
/// This also includes some technical aspects like the \code{TermActivationFlags}.
public class SipLaplace :\newline
\btab \btab BoSSS.Foundation.IEdgeForm,   // edge integrals\newline
\btab \btab BoSSS.Foundation.IVolumeForm  // volume integrals\newline
\{\newline
    /// We do not use parameters (e.g. variable viscosity, ...)
    /// at this point: so this can be null
\btab public IList<string> ParameterOrdering \{ \newline
\btab \btab get \{ return new string[0]; \} \newline
\btab \} \newline
    /// but we have one argument variable, $u$ (our trial function)
\btab public IList<String> ArgumentOrdering \{ \newline
\btab \btab get \{ return new string[] \{ "u" \}; \} \newline
\btab \}\newline
    /// The \code{TermActivationFlags} tell \BoSSS\ which kind of terms,
    /// i.e. products of $u$, $v$, $\nabla u$, and $\nabla v$
    /// the \code{VolumeForm(...)} actually contains.
    /// This additional information helps to improve the performance.
\btab public TermActivationFlags VolTerms \{\newline
\btab \btab get \{\newline
\btab \btab \btab return TermActivationFlags.GradUxGradV;\newline
\btab \btab \}\newline
\btab \}\newline
    /// activation flags for the 'InnerEdgeForm(...)'
\btab public TermActivationFlags InnerEdgeTerms \{\newline
\btab \btab get \{\newline
\btab \btab \btab return (TermActivationFlags.AllOn);\newline
\btab \btab \btab // if we do not care about performance, we can activate all terms.\newline
\btab \btab \}\newline
\btab \}\newline
\btab public TermActivationFlags BoundaryEdgeTerms \{\newline
\btab    get \{\newline
\btab \btab    return TermActivationFlags.AllOn;\newline
\btab \btab \}\newline
\btab \}\newline
    /// For the computation of the penalty factor $\eta$,
    /// we require
    /// some length scale for each cell and
    /// the polynomial degree of the DG approximation.
\btab public MultidimensionalArray cj;\newline
\btab public int PolynomialDegree;\newline
    /// The safety factor for the penalty factor should be in the order of 1.
    /// A very large penalty factor increases the condition number of the
    /// system, but without affecting stability.
    /// A very small penalty factor yields to an unstable discretization.
\btab public double PenaltySafety = 2.2; \newline
    /// The actual computation of the penalty factor, which should be
    /// used in the \code{InnerEdgeForm} and \code{BoundaryEdgeForm} functions.
    /// Hint: for the parameters \code{jCellIn}, \code{jCellOut} and \code{g},
    /// take a look at
    /// \code{CommonParams} and \code{CommonParamsBnd}.
\btab double PenaltyFactor(int jCellIn, int jCellOut) \{\newline
\btab \btab double cj\_in        = cj[jCellIn];\newline
\btab \btab double penalty\_base = PenaltySafety*PolynomialDegree*PolynomialDegree;\newline
\btab \btab double eta          = penalty\_base * cj\_in;\newline
\btab \btab if(jCellOut >= 0) \{\newline
\btab \btab \btab double cj\_out = cj[jCellOut];\newline
\btab \btab \btab eta           = Math.Max(eta, penalty\_base * cj\_out);\newline
\btab \btab \}\newline
\btab \btab return eta;\newline
\btab \}\newline
    /// The following functions cover the actual math.
    /// For any discretization of the Laplace operator, we have to specify:
    /// \begin{itemize}
    /// \item a volume integrand,
    /// \item an edge integrand for inner edges, i.e. on $\Gamma_i$,
    /// \item an edge integrand for boundary edges, i.e. on $\partial \Omega$.
    /// \end{itemize}
    /// The integrand for the volume integral:
\btab public double VolumeForm(ref CommonParamsVol cpv, \newline
\btab \btab    double[] U, double[,] GradU, // the trial-function u \newline
\btab \btab    //            (i.e. the function we search for) and its gradient\newline
\btab \btab    double V, double[] GradV     // the test function; \newline
\btab \btab    ) \{\newline
 \newline
\btab \btab double acc = 0;\newline
\btab \btab for(int d = 0; d < cpv.D; d++)\newline
\btab \btab \btab acc += GradU[0, d] * GradV[d];\newline
\btab \btab return acc;\newline
\btab \}\newline
    /// The integrand for the integral on the inner edges,
    /// \[
    ///   -( \mean{ \nabla u } \jump{ v }) \cdot \vec{n}_{\Gamma}
    ///   -( \mean{ \nabla v } \jump{ u }) \cdot \vec{n}_{\Gamma}
    ///   + \eta \jump{ u }  \jump{v} :
    /// \]
\btab public double InnerEdgeForm(ref CommonParams inp, \newline
\btab \btab double[] U\_IN, double[] U\_OT, double[,] GradU\_IN, double[,] GradU\_OT, \newline
\btab \btab double V\_IN, double V\_OT, double[] GradV\_IN, double[] GradV\_OT) \{\newline
 \newline
\btab \btab double eta = PenaltyFactor(inp.jCellIn, inp.jCellOut);\newline
 \newline
\btab \btab double Acc = 0.0;\newline
\btab \btab for(int d = 0; d < inp.D; d++) \{ // loop over vector components \newline
\btab \btab \btab // consistency term: -(\{\{ \textbackslash /u \}\} [[ v ]])*Normale\newline
\btab \btab \btab // index d: spatial direction\newline
\btab \btab \btab Acc -= 0.5 * (GradU\_IN[0, d] + GradU\_OT[0, d])*(V\_IN - V\_OT)\newline
\btab \btab \btab \btab \btab    * inp.Normale[d];\newline
 \newline
\btab \btab \btab // symmetry term: -(\{\{ \textbackslash /v \}\} [[ u ]])*Normale\newline
\btab \btab \btab Acc -= 0.5 * (GradV\_IN[d] + GradV\_OT[d])*(U\_IN[0] - U\_OT[0])\newline
\btab \btab \btab \btab \btab    * inp.Normale[d];;\newline
\btab \btab \}\newline
 \newline
\btab \btab // penalty term: eta*[[u]]*[[v]]\newline
\btab \btab Acc += eta*(U\_IN[0] - U\_OT[0])*(V\_IN - V\_OT);\newline
\btab \btab return Acc;\newline
\btab \} \newline
    /// The integrand on boundary edges, i.e. on $\partial \Omega$, is
    /// \[
    ///   -( \mean{ \nabla u } \jump{ v }) \cdot \vec{n}_{\Gamma}
    ///   -( \mean{ \nabla v } \jump{ u }) \cdot \vec{n}_{\Gamma}
    ///   +  \eta \jump{ u }  \jump{v} .
    /// \]
    /// For the boundary we have to consider the special definition for
    /// the mean-value operator $\mean{-}$ and the jump operator
    /// $\jump{-}$ on the boundary.
\btab public double BoundaryEdgeForm(ref CommonParamsBnd inp, \newline
\btab \btab double[] U\_IN, double[,] GradU\_IN, double V\_IN, double[] GradV\_IN) \{\newline
 \newline
\btab \btab double eta = PenaltyFactor(inp.jCellIn, -1);\newline
\btab \btab double Acc = 0.0;\newline
\btab \btab for(int d = 0; d < inp.D; d++) \{ // loop over vector components \newline
\btab \btab \btab // consistency term: -(\{\{ \textbackslash /u \}\} [[ v ]])*Normale\newline
\btab \btab \btab // index d: spatial direction\newline
\btab \btab \btab Acc -= (GradU\_IN[0, d])*(V\_IN) * inp.Normale[d];\newline
 \newline
\btab \btab \btab // symmetry term: -(\{\{ \textbackslash /v \}\} [[ u ]])*Normale\newline
\btab \btab \btab Acc -= (GradV\_IN[d])*(U\_IN[0]) * inp.Normale[d];;\newline
\btab \btab \}\newline
 \newline
\btab \btab // penalty term: eta*[[u]]*[[v]]\newline
\btab \btab Acc += eta*(U\_IN[0])*(V\_IN);\newline
 \newline
 \newline
\btab \btab return Acc;\newline
\btab \}\newline
\}
 }
\BoSSSexe
\BoSSScmd{
/// \subsection{Evaluation of the poisson operator in 1D}
/// We consider the following problem:
/// \begin{equation*}
/// \Delta u = 2,\quad -1<x<1,\quad u(-1)=u(1)=0.
/// \end{equation*}
/// The solution is $u_{ex}(x) = 1 - x^2$. Since this is quadratic, we can represent it \emph{exactly} in a DG space of order 2.
/// As usual, we have to set up a grid and a basis:
var grd1D                     = Grid1D.LineGrid(GenericBlas.Linspace(-1,1,10));\newline
var gdata1D                   = new GridData(grd1D);\newline
var DGBasisOn1D               = new Basis(gdata1D,2);\newline
/// and a right-hand-side:
var RHS                       = new SinglePhaseField(DGBasisOn1D, "RHS");\newline
RHS.ProjectField((double x) => 2);\newline
/// We have to ensure to set the \code{PolynomialDegree} in the \emph{SipLaplace}-object.
var i\_SipLaplace              = new SipLaplace();\newline
i\_SipLaplace.PolynomialDegree = DGBasisOn1D.Degree;\newline
i\_SipLaplace.cj               = gdata1D.Cells.cj;\newline
var Operator\_SipLaplace       = i\_SipLaplace.Operator();\newline
/// We now want to calculate the residual after inserting the exact solution as well as a wrong solution.
/// The implementation of the exact solution:
var u\_ex         = new SinglePhaseField(DGBasisOn1D, "$u\_\{ex\}$");\newline
u\_ex.ProjectField((double x) => 1.0 - x*x);\newline
/// The implementation of a spurious, i.e. a wrong solution; we take the exact solution and add random values in each cell:
var u\_wrong      = new SinglePhaseField(DGBasisOn1D, "$u\_\{wrong\}$");\newline
u\_wrong.ProjectField((double x) => 1.0 - x*x);\newline
Random R         = new Random();\newline
for(int j = 0; j < gdata1D.Cells.NoOfLocalUpdatedCells; j++)\{\newline
\btab double ujMean = u\_wrong.GetMeanValue(j);\newline
\btab ujMean += R.NextDouble();\newline
\btab u\_wrong.SetMeanValue(j, ujMean);\newline
\btab \}\newline
/// Evaluating the Laplace operator using the different solutions:
var Residual     = new SinglePhaseField(DGBasisOn1D,"Resi1");\newline
var ResidualNorm = new List<double>();\newline
foreach(var u in new DGField[] \{u\_ex, u\_wrong\}) \{\newline
\btab Residual.Clear();\newline
\btab Operator\_SipLaplace.Evaluate(u, Residual);  // evaluate\newline
\btab Residual.Acc(-1.0, RHS);    \newline
\btab double ResiNorm = Residual.L2Norm();\newline
\btab ResidualNorm.Add(ResiNorm);\newline
\btab Console.WriteLine("Residual for " + u.Identification + " = " + ResiNorm);  \newline
\}
 }
\BoSSSexe
\BoSSScmdSilent{
/// tests BoSSScmdSilent
Assert.LessOrEqual(ResidualNorm[0], 1e-10);\newline
Assert.GreaterOrEqual(ResidualNorm[1], 1e-1);
 }
\BoSSSexe
\BoSSScmd{
/// \subsection{The matrix of the Poisson Operator}
/// If we do not know the exact solution, we have to solve a linear system.
/// Therefore, we not only need to evaluate the operator,
/// but we need its matrix.
/// The \emph{Mapping} controls which degree-of-freedom of the DG approximation
/// is mapped to which row, resp. column of the matrix.
var Mapping           = new UnsetteledCoordinateMapping(DGBasisOn1D);\newline
var Matrix\_SipLaplace = Operator\_SipLaplace.ComputeMatrix(Mapping,null,Mapping);
 }
\BoSSSexe
\BoSSScmd{
Matrix\_SipLaplace.NoOfCols;
 }
\BoSSSexe
\BoSSScmd{
Matrix\_SipLaplace.NoOfRows;
 }
\BoSSSexe
\BoSSScmd{
/// We see that the matrix has 27 rows and columns.
 }
\BoSSSexe
\BoSSScmd{
/// \paragraph{Matrix rank and determinant of the matrix
///  \code{Matrix\_SipLaplace}:}
/// Use the functions \emph{rank} and \emph{det} to analyze the matrix (warning: this can get costly
/// for larger matrices!).\\
///Interpret the results:
/// \begin{itemize}
/// \item What does it mean, when a matrix has full rank?
/// \item How many solutions can a linear system have?
/// \end{itemize}
double rank = Matrix\_SipLaplace.rank(); \newline
Console.WriteLine("Matrix rank = " + rank);\newline
 \newline
double det = Matrix\_SipLaplace.det();   \newline
Console.WriteLine("Determinante = " + det);\newline
/// So the matrix of the SIP discretization has a unique solution.
 }
\BoSSSexe
\BoSSScmdSilent{
/// tests BoSSScmdSilent
Assert.AreEqual(rank, Matrix\_SipLaplace.NoOfCols);\newline
Assert.Greater(det, 1.0);
 }
\BoSSSexe
\BoSSScmd{
 %
 }
\BoSSSexe
\BoSSScmd{
/// \section{Advanced topics}
 }
\BoSSSexe
\BoSSScmd{
/// %========================================================
/// %========================================================
/// \subsection{The penalty parameter of the SIP and stability in 2D}
/// %========================================================
/// %========================================================
 }
\BoSSSexe
\BoSSScmd{
/// We define a two-dimensional grid:
var grd2D       = Grid2D.Cartesian2DGrid(GenericBlas.Linspace(-1,1,21), \newline
\btab \btab \btab \btab \btab \btab \btab \btab \btab \btab  GenericBlas.Linspace(-1,1,16));\newline
var gdata2D     = new GridData(grd2D);\newline
var DGBasisOn2D = new Basis(gdata2D, 5);\newline
var Mapping2D   = new UnsetteledCoordinateMapping(DGBasisOn2D);
 }
\BoSSSexe
\BoSSScmd{
/// We are going to choose the \code{PenaltySafety} for the \code{SipLaplace}
/// from the following list
double[] SFs = new double[] \newline
\btab   \{0.001, 0.002, 0.01, 0.02, 0.1, 0.2, 1, 2, 10, 20, 100\};\newline
/// and compute the condition number as well as the determinate.
 }
\BoSSSexe
\BoSSScmd{
/// We consider the example
/// \[
///     -\Delta u = \pi^2 0.5 \cos(x/2) \cos(y/2)
///       \text{ with }
///       (x,y) \in (-1,1)^2
/// \]
/// and $u = 0$ on the boundary.
/// The exact solution is $u_{Ex}(x,y) = \cos(x/2) \cos(y/2)$.
Func<double[], double> exSol = \newline
\btab \btab (X => Math.Cos(X[0]*Math.PI*0.5)*Math.Cos(X[1]*Math.PI*0.5));\newline
Func<double[], double> exRhs = \newline
\btab \btab (X => (Math.PI*Math.PI*0.5*Math.Cos(X[0]*Math.PI*0.5)\newline
\btab \btab \btab \btab \btab   *Math.Cos(X[1]*Math.PI*0.5))); // == - /\textbackslash  exSol\newline
SinglePhaseField RHS = new SinglePhaseField(DGBasisOn2D, "RHS");\newline
RHS.ProjectField(exRhs);\newline
double[] RHSvec = RHS.CoordinateVector.ToArray();
 }
\BoSSSexe
\BoSSScmd{
/// We check our discretization once more in 2D; the residual should be low,
/// but not exactly (resp. up to $10^{-12}$) since the solution is not
/// polynomial and cannot be fulfilled exactly.
SinglePhaseField u = new SinglePhaseField(DGBasisOn2D,"u");\newline
u.ProjectField(exSol);\newline
i\_SipLaplace.PolynomialDegree = DGBasisOn2D.Degree;\newline
i\_SipLaplace.cj               = gdata2D.Cells.cj;\newline
var Matrix\_SIP\_sf     = Operator\_SipLaplace.ComputeMatrix(Mapping2D,\newline
\btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab   null,\newline
\btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab   Mapping2D);\newline
SinglePhaseField Residual = new SinglePhaseField(DGBasisOn2D,"Residual");\newline
Residual.Acc(1.0, RHS);\newline
Matrix\_SIP\_sf.SpMV(-1.0, u.CoordinateVector, 1.0, Residual.CoordinateVector);\newline
Console.WriteLine("Residual L2 norm: " + Residual.L2Norm());
 }
\BoSSSexe
\BoSSScmd{
/// We also check that the matrix is symmetric:
var checkMatrix = Matrix\_SIP\_sf - Matrix\_SIP\_sf.Transpose();\newline
checkMatrix.InfNorm();
 }
\BoSSSexe
\BoSSScmdSilent{
/// tests BoSSScmdSilent
Assert.LessOrEqual(checkMatrix.InfNorm(), 1e-8);
 }
\BoSSSexe
\BoSSScmd{
/// \paragraph{Matrix properties for different penalty factors.}
/// Now, we assemble the matrix of the SIP for different
/// \code{PenaltySafety}-factors. We also try to solve the linear system
/// using an iterative method. As Matlab is called multiple times during this
/// command, it can take some minutes until it is done.
int cnt     = 0;\newline
var Results = new List<Tuple<double,double,int,double,bool>>();\newline
foreach(double sf in SFs) \{\newline
\btab cnt++;\newline
\btab i\_SipLaplace.PenaltySafety    = sf;\newline
\btab i\_SipLaplace.PolynomialDegree = DGBasisOn2D.Degree;\newline
\btab var Matrix\_SIP\_sf             = Operator\_SipLaplace.ComputeMatrix(\newline
\btab \btab \btab \btab \btab \btab \btab \btab \btab Mapping2D,null,Mapping2D);\newline
\btab double condNo1                = Matrix\_SIP\_sf.condest();  \newline
\btab bool definite                 = Matrix\_SIP\_sf.IsDefinite();\newline
 \newline
    /// We solve the system
    /// \[
    ///    \text{\tt Matrix\_SIP\_sf} \cdot u = \text{\tt RHS}
    /// \]
    /// using a an iterative solver, the so-called
    /// conjugate gradient (CG) method.
    /// CG requires a positive definite matrix.
    /// The function \code{Solve\_CG} returns the number of iterations.
\btab SinglePhaseField u = new SinglePhaseField(DGBasisOn2D,"u");\newline
\btab u.InitRandom();\newline
\btab int NoOfIter = Matrix\_SIP\_sf.Solve\_CG(u.CoordinateVector, RHSvec);\newline
 \newline
\btab SinglePhaseField Error = new SinglePhaseField(DGBasisOn2D,"Error");\newline
\btab Error.ProjectField(exSol);\newline
\btab Error.Acc(-1.0, u);\newline
 \newline
\btab double L2err = u.L2Error(exSol);\newline
 \newline
\btab Console.WriteLine(sf + "\textbackslash t" + condNo1.ToString("0.#E-00") \newline
\btab \btab \btab \btab \btab \btab  + "\textbackslash t" + NoOfIter \newline
\btab \btab \btab \btab \btab \btab  + "\textbackslash t" + L2err.ToString("0.#E-00") \newline
\btab \btab \btab \btab \btab \btab  + "\textbackslash t" + definite);\newline
\btab Results.Add(new Tuple<double,double,int,double,bool>(sf, condNo1, NoOfIter,\newline
\btab \btab \btab \btab \btab \btab \btab \btab  L2err, definite));\newline
\}
 }
\BoSSSexe
\BoSSScmdSilent{
/// tests BoSSScmdSilent
foreach(var r in Results) \{\newline
\btab if(r.Item1 >= 1 && r.Item1 <= 20) \{\newline
\btab \btab Assert.LessOrEqual(r.Item2, 1e7); // cond No.\newline
\btab \btab Assert.LessOrEqual(r.Item3, 6000); // iter\newline
\btab \btab Assert.LessOrEqual(r.Item4, 1e-4); // L2 err\newline
\btab \btab Assert.IsTrue(r.Item5); // definite   \newline
\btab \}\newline
\btab if(r.Item1 <= 0.1) \{\newline
\btab \btab Assert.IsFalse(r.Item5); // indefinite   \newline
\btab \}\newline
\}
 }
\BoSSSexe
\BoSSScmd{
/// \paragraph{Plotting:}
/// Plot the number of conjugate gradient iterations versus the
/// \code{PenaltySafety}.
/// A logarithmic scale is used for the horizontal axis.
var format = new PlotFormat(lineColor: LineColors.Blue, \newline
\btab \btab \btab \btab \btab \btab \btab pointSize: 2, \newline
\btab \btab \btab \btab \btab \btab \btab dashType: DashTypes.DotDashed, \newline
\btab \btab \btab \btab \btab \btab \btab Style: Styles.LinesPoints, \newline
\btab \btab \btab \btab \btab \btab \btab pointType:PointTypes.OpenCircle);\newline
Gnuplot gp = new Gnuplot(baseLineFormat:format);\newline
gp.PlotLogXY(Results.Select(r => r.Item1), \newline
\btab \btab \btab  Results.Select(r => ((double)(r.Item3))));\newline
gp.PlotNow();
 }
\BoSSSexe
\BoSSScmd{
/// \paragraph{Convergence study, indefinite vs. definite.}
/// We are going to solve the SIP-system for different grid resolutions,
/// comparing an insufficient penalty to a penalty which is large enough.
double[] Resolution = new double[] \{ 8, 16, 32, 64, 128, 256 \};\newline
List<double> L2Error\_indef  = new List<double>();\newline
List<double> L2Error\_posdef = new List<double>();\newline
foreach(int Res in Resolution) \{\newline
\btab var grd2D = Grid2D.Cartesian2DGrid(GenericBlas.Linspace(-1,1,(int)Res + 1), \newline
\btab \btab \btab \btab \btab \btab \btab \btab \btab    GenericBlas.Linspace(-1,1,(int)Res + 1));\newline
\btab var gdata2D = new GridData(grd2D);\newline
\btab var DGBasisOn2D = new Basis(gdata2D, 2);\newline
\btab var Mapping2D  = new UnsetteledCoordinateMapping(DGBasisOn2D);\newline
 \newline
\btab SinglePhaseField RHS = new SinglePhaseField(DGBasisOn2D, "RHS");\newline
\btab RHS.ProjectField(exRhs);\newline
\btab SinglePhaseField uEx = new SinglePhaseField(\newline
\btab \btab    new Basis(gdata2D, DGBasisOn2D.Degree*2),\newline
\btab \btab    "Error");\newline
\btab uEx.ProjectField(exSol);\newline
 \newline
\btab i\_SipLaplace.PolynomialDegree = DGBasisOn2D.Degree;\newline
\btab i\_SipLaplace.cj               = gdata2D.Cells.cj;\newline
 \newline
\btab i\_SipLaplace.PenaltySafety    = 0.01;\newline
\btab var Matrix\_SIP\_indef          = Operator\_SipLaplace.ComputeMatrix(\newline
\btab \btab \btab \btab \btab \btab \btab \btab \btab Mapping2D,null,Mapping2D);\newline
 \newline
\btab SinglePhaseField u\_indef = new SinglePhaseField(DGBasisOn2D,"u");\newline
\btab Matrix\_SIP\_indef.Solve\_Direct(u\_indef.CoordinateVector, \newline
\btab \btab \btab \btab \btab \btab \btab \btab   RHS.CoordinateVector);\newline
\btab var Error\_indef = uEx.CloneAs();\newline
\btab Error\_indef.AccLaidBack(-1.0, u\_indef);\newline
\btab L2Error\_indef.Add(Error\_indef.L2Norm());\newline
 \newline
    /// In order to have a positive definite system, we are
    /// using $\text{\tt PenaltySafety} = 2$!
\btab i\_SipLaplace.PenaltySafety = 2.0;\newline
\btab var Matrix\_SIP\_posdef         = Operator\_SipLaplace.ComputeMatrix(\newline
\btab \btab \btab \btab \btab \btab \btab \btab \btab Mapping2D,null,Mapping2D);\newline
 \newline
\btab SinglePhaseField u\_posdef = new SinglePhaseField(DGBasisOn2D,"u");\newline
\btab Matrix\_SIP\_posdef.Solve\_Direct(u\_posdef.CoordinateVector, \newline
\btab \btab \btab \btab \btab \btab \btab \btab    RHS.CoordinateVector);\newline
\btab var Error\_posdef = uEx.CloneAs();\newline
\btab Error\_posdef.AccLaidBack(-1.0, u\_posdef);\newline
\btab L2Error\_posdef.Add(Error\_posdef.L2Norm());\newline
 \newline
\btab Console.WriteLine(L2Error\_indef.Last().ToString("0.#E-00") \newline
\btab \btab \btab \btab \btab   + "\textbackslash t" + L2Error\_posdef.Last().ToString("0.#E-00"));\newline
\}
 }
\BoSSSexe
\BoSSScmd{
/// \paragraph{Convergence plot:}
/// The convergence plot unveils that there is something wrong if the
/// penalty factor is set too low.
/// While the solution of the indefinite system may look right at the first
/// glance, we see that we do not obtain grid convergence for
/// \code{Error\_indef}.
/// The error of the positive definite system, \code{Error\_posdef}, where the
/// penalty is chosen sufficiently large converges with the expected
/// rate.
var format = new PlotFormat(lineColor: LineColors.Blue, \newline
\btab \btab \btab \btab \btab \btab \btab pointSize: 2, \newline
\btab \btab \btab \btab \btab \btab \btab dashType: DashTypes.DotDashed, \newline
\btab \btab \btab \btab \btab \btab \btab Style: Styles.LinesPoints, \newline
\btab \btab \btab \btab \btab \btab \btab pointType:PointTypes.OpenCircle);\newline
Gnuplot gp = new Gnuplot(baseLineFormat:format);\newline
gp.PlotLogXLogY(Resolution, L2Error\_indef);\newline
gp.PlotLogXLogY(Resolution, L2Error\_posdef);\newline
gp.PlotNow();
 }
\BoSSSexe
\BoSSScmd{
/// Finally, we are going to compute the convergence rate of the SIP
/// discretization: we take the logarithm of the resolution and the error:
 }
\BoSSSexe
\BoSSScmd{
var log\_h   = Resolution.Select(h => Math.Log10(h)).ToArray();\newline
var log\_Err = L2Error\_posdef.Select(h => Math.Log10(h)).ToArray();
 }
\BoSSSexe
\BoSSScmd{
var LeastSquaresSys = MultidimensionalArray.Create(log\_h.Length,2);\newline
LeastSquaresSys.SetColumn(0, log\_h.Length.ForLoop(i => 1.0));\newline
LeastSquaresSys.SetColumn(1, log\_h);\newline
double[] dk = new double[2]; // intercept and slope of best-fit line\newline
LeastSquaresSys.LeastSquareSolve(dk, log\_Err);
 }
\BoSSSexe
\BoSSScmd{
dk;
 }
\BoSSSexe
\BoSSScmdSilent{
/// tests BoSSScmdSilent
Assert.LessOrEqual(dk[1], -2.9);
 }
\BoSSSexe
\BoSSScmd{
/// \section{Further reading}
/// \begin{itemize}
/// \item
/// \bibentry{DiPietroErn2011}
/// \item
/// \bibentry{Arnold_1982}
/// \end{itemize}
 }
\BoSSSexe


\section{Asymmetric-Key Encryption (Public-Key Encryption)}\label{s:public-key-encryption}
The major problem that traditional symmetric-key algorithms face is that they require the parties to exchange keys, or more specifically to agree to a secret common private key, prior to communicating.
This needs a secure (physical or digital) communication channel accessible even for a short period of time, in order for the secret key to be exchanged. Such a channel could be a piece of paper delivered by hand or by a trusted courier, or a short in person communication.

An asymmetric or public-key cryptosystem is one where different keys are employed for the operations in the cryptosystem (\textit{e.g.}, encryption and decryption), and where one of the keys can be made public without compromising the secrecy of the other key \cite{Kaliski2011}. That way there is no need for a secure channel or prior agreement.

Usually three algorithms are needed to define a public key cryptosystem. These are a \textit{Key Generation} algorithm which produces the public and private keys (based on a security parameter $\eta$), an \textit{Encryption} and a \textit{Decryption} algorithm.

In the classic model such an encryption scheme involves a \textit{public key} which is used for encryption and a \textit{private key} which is used for decryption.
More formally, given a plaintext $P$, a public key $PubKey$, and a private key $PrivKey$ we can produce a ciphertext $C$ such that:
\begin{equation}
  \label{eq:public-key-encryption}
  \begin{aligned}
    C = Enc(P, PubKey)\\
    P = Dec(C, PrivKey)
  \end{aligned}
\end{equation}

Where $Enc$ and $Dec$ are the \textit{Encryption} and \textit{Decryption} algorithms respectively.

If Alice wants to send a message to Bob, she needs Bob's public key which could be publicly available (\textit{e.g.} on Bob's website).
Alice encrypts her message with Bob's public key and sends the resulted ciphertext to Bob. Bob then needs his private key which is only in his possession in order to decrypt the ciphertext Alice sent and retrieve the original message.
If Bob wishes to reply, he needs Alice's public key etc.
Generally, the public (encryption) key could be shared to any party that wants to communicate with the owner of that public key, while the private (decryption) key should be kept secret and used by the recipient of the ciphertext to recover the original plaintext.

The ability for two users to establish a shared secret over an insecure communication channel, despite having no prior communication or information exchange was first proposed in 1976 by Whitfield Diffie and Martin Hellman.
That method is named Diffie-Hellman (DH) key exchange \cite{diffie1976new} after its authors. The security of the algorithm is based on the difficulty of solving the Discrete Log Problem (DLP). The DLP is stated as follows: Given $g, p$ and $g^k \pmod{p}$, find $k$.

Some of the most widely used public key cryptosystems are the RSA (Rivest -- Shamir -- Adleman) \cite{rivest1978method}, the El Gamal \cite{elgamal1985public} and the Paillier \cite{paillier1999public} Cryptosystems.

\subsection{RSA}\label{s:pk-rsa}

One if the first and most widely adopted public key cryptosystem is RSA . The algorithm's difficulty reduces to the difficulty of factoring the product of two large prime numbers. Its three basic algorithms are described below.

\begin{itemize}
  \item \textit{Key Generation}
  \begin{itemize}
    \item Randomly select two large primes $p$ and $q$.
    \item Calculate modulus $n = p \cdot q$.
    \item Calculate $\phi(n) = (p-1) \cdot (q-1)$.
    \item Randomly select $e : gcd(e,\phi(n)) = 1$.
    \item Calculate reverse $d  = e ^ {-1} \pmod{\phi(n)}$.\\
    $d \cdot e = 1 \pmod{\phi(n)}$.
    \item The public key is $(n, e)$, while the private key is $d$. $p, q$ and $\phi(n)$ must also be kept private.
  \end{itemize}
  \item \textit{Encryption}
  \begin{itemize}
    \item Given a plaintext message $m$, we get a ciphertext $c = m ^ e \pmod{n}$.
  \end{itemize}
  \item \textit{Decryption}
  \begin{itemize}
    \item Given a ciphertext $c$, we get back the plaintext $m$ as follows: $c^d \pmod{n} = m^{ed} \pmod{n} = m$.
  \end{itemize}
\end{itemize}

\subsection{ElGamal}\label{s:pk-elgamal}
The ElGamal encryption system is a public-key cryptosystem based on the Diffie-Hellman key exchange. Its three basic algorithms are described below.

\begin{itemize}
  \item \textit{Key Generation}
  \begin{itemize}
    \item Select two large primes $p$ and $ q : q \mid (p - 1)$.
    \item Select a generator $g$ of group $\mathbb{G}$ which is a large enough order-$q$ subgroup of the multiplicative group $\mathbb{Z}_{p}^{*}$ of integers between $1$ and $p - 1$.
    \item Randomly select $ x \in_{R} \mathbb{Z}_{q}$.
    \item Calculate $y = g^{x} \pmod{p}$.
    \item The public key is $y$, while the private key is $x$.
  \end{itemize}
  \item \textit{Encryption}
  \begin{itemize}
    \item Given a plaintext message $m$, we get a ciphertext $c$ as follows.
    \item Randomly select $r \in_{R} \mathbb{Z}_{q}$.
    \item Calculate $G = g^{r} \pmod{p}$.
    \item Calculate $M = m \cdot y^{r} \pmod{p}$.
    \item Return $c = (G,M)$
  \end{itemize}
  \item \textit{Decryption}
  \begin{itemize}
    \item Given a ciphertext $c = (G,M)$, we get back the plaintext $m$ as follows: \\$m = M/G^{x}\pmod{p}$.
  \end{itemize}
\end{itemize}

\subsection{Paillier}\label{s:pk-paillier}
The Pallier cryptosystem is a public-key cryptosystem that relies its security upon the decisional composite residuosity assumption \cite{paillier1999public}.

\begin{itemize}
  \item \textit{Key Generation}
  \begin{itemize}
    \item Randomly and independently select two large primes $p$ and \\$ q :gcd(p-1, q-1) = 1$ If both primes have the same length this property holds.
    \item Calculate the RSA modulus $n = p \cdot q$.
    \item Calculate $\lambda = lcm(p-1, q-1)$.
    \item Select generator $g \in \mathbb{Z}_{n^{2}}^{*}$ such that the order of $g$ is a non zero multiple of $n$.
    \item Calculate $\mu = (L(g^{\lambda} \pmod{n^{2}}))^{-1} \pmod{n}$, where $L(x) = \frac{x-1}{n}$.
    \item The public key is $ (n,g) $, while the private key is $ (\lambda, \mu) $.
  \end{itemize}
  \item \textit{Encryption}
  \begin{itemize}
    \item Given a plaintext message $m$, we get a ciphertext $c$ as follows.
    \item Encode $m$ into $\mathbb{Z}_{n}$.
    \item Randomly select $ r \in_{R} \mathbb{Z}_{n}^{*}$.
    \item Return $c = g^{m} \cdot r^{n} \pmod{n^{2}}$.
  \end{itemize}
  \item \textit{Decryption}
  \begin{itemize}
    \item Given a ciphertext $c \in \mathbb{Z}_{n^{2}}$, we get back the plaintext $m$ as follows: \\$m = L(c^{\lambda} \pmod{n^{2}}) \cdot \mu \pmod{n}$.
  \end{itemize}
\end{itemize}


% Part: first-order-logic
% Chapter: introduction
% Section: formulas

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

\begin{document}

\olfileid{fol}{int}{fml}

\section{\usetoken{P}{formula}}

Here is the approach we will use to rigorously specify !!{sentence}s
of first-order logic and to deal with the issues arising from the use
of !!{variable}s. We first define a \emph{different} set of
expressions: !!{formula}s. Once we've done that, we can consider the
role !!{variable}s play in them---and on the basis of some other
ideas, namely those of ``free'' and ``bound'' !!{variable}s, we can
define what !!a{sentence} is (namely, !!a{formula} without free
!!{variable}s). We do this not just because it makes the definition of
``!!{sentence}'' more manageable, but also because it will be crucial
to the way we define the semantic notion of satisfaction.

Let's define ``!!{formula}'' for a simple first-order language, one
containing only a single !!{predicate}~$\Obj P$ and a single
!!{constant}~$\Obj a$, and only the logical symbols $\lnot$, $\land$,
and~$\lexists$. Our full definitions will be much more general:
we'll allow infinitely many !!{predicate}s and !!{constant}s. In fact,
we will also consider !!{function}s which can be combined with
!!{constant}s and !!{variable}s to form ``terms.'' For now, $\Obj a$
and the variables will be our only terms.  We do need infinitely many
!!{variable}s.  We'll officially use the symbols $\Obj v_0$, $\Obj
v_1$, \dots, as variables.

\begin{defn}
The set of \emph{!!{formula}s}~$\Frm$ is defined as follows:
\begin{enumerate}
\item\ollabel{fmls-atom} $\Atom{\Obj P}{\Obj a}$ and $\Atom{\Obj
  P}{\Obj v_i}$ are !!{formula}s ($i \in \Nat$).

\tagitem{prvNot}{If $!A$ is !!a{formula}, then $\lnot !A$ is
  !!{formula}.}{}

\tagitem{prvAnd}{If $!A$ and $!B$ are !!{formula}s, then $(!A \land
  !B)$ is !!a{formula}.}{}

\tagitem{prvEx}{If $!A$ is !!a{formula} and $x$ is !!a{variable},
  then $\lexists[x][!A]$ is !!a{formula}.}{}

\tagitem{limitClause}{\ollabel{fmls-limit}Nothing else is !!a{formula}.}{}
\end{enumerate}
\end{defn}

\olref{fmls-atom} tell us that $\Atom{\Obj P}{\Obj a}$ and $\Atom{\Obj
P}{\Obj v_i}$ are !!{formula}s, for any $i \in \Nat$. These are
the so-called \emph{atomic} !!{formula}s. They give us something to
start from.  The other clauses give us ways of forming new
!!{formula}s from ones we have already formed. So for instance, we get
that $\lnot \Atom{\Obj P}{\Obj v_2}$ is !!a{formula}, since
$\Atom{\Obj P}{\Obj v_2}$ is already !!a{formula} by
\olref{fmls-atom}, and then we get that $\lexists[\Obj v_2][\lnot
\Atom{\Obj P}{\Obj v_2}]$ is another !!{formula}, and so on.
\olref{fmls-limit} tells us that \emph{only} strings we can form in
this way count as !!{formula}s. In particular, $\lexists[\Obj
v_0][\Atom{\Obj P}{\Obj a}]$ and $\lexists[\Obj v_0][\lexists[\Obj
v_0][\Atom{\Obj P}{\Obj a}]]$ \emph{do} count as !!{formula}s, and
$(\lnot \Atom{\Obj P}{\Obj a})$ does not.

This way of defining !!{formula}s is called an \emph{inductive
definition}, and it allows us to prove things about !!{formula}s using
a version of proof by induction called \emph{structural induction}.
These are discussed in a general way in \olref[mth][ind][idf]{sec} and
\olref[mth][ind][sti]{sec}, which you should review before delving
into the proofs later on. Basically, the idea is that if you want to
give a proof that something is true for all !!{formula}s you show
first that it is true for the atomic !!{formula}s, and then that
\emph{if} it's true for any !!{formula}~$!A$ (and~$!B$), it's
\emph{also} true for $\lnot !A$, $(!A \land !B)$, and
$\lexists[x][!A]$. For instance, this proves that it's true for
$\lexists[\Obj v_2][\lnot \Atom{\Obj P}{\Obj v_2}]$: from the first
part you know that it's true for the atomic !!{formula}~$\Atom{\Obj
P}{\Obj v_2}$. Then you get that it's true for $\lnot \Atom{\Obj
P}{\Obj v_2}$ by the second part, and then again that it's true for
$\lexists[\Obj v_2][\lnot \Atom{\Obj P}{\Obj v_2}]$ itself. Since all
!!{formula}s are inductively generated from atomic !!{formula}s, this
works for any of them.

\end{document}


\documentclass[../PHYS306Notes.tex]{subfiles}

\begin{document}
\subsection{Worksheet - Review of Damped \& Driven Harmonic Oscillators}
\begin{p}
An undamped harmonic oscillator has general solution $x(t) = B_1\cos(\omega t) + B_2\sin(\omega t)$. Show this can be recast as $x(t) = A\cos(\omega t - \delta)$, where $A = \sqrt{A^2 + B^2}$.
\end{p}
\begin{s}
Consider a right-angle triangle with side lengths $B_1$, $B_2$ and hypotenuse $A + \sqrt{B_1^2 + B_2^2}$. Then, let $\cos\delta = \frac{B_1}{A}$ and $\sin\delta = \frac{B_2}{A}$. Looking at the equation for $x(t)$ above, we can multiply it by one in a clever way:
\[x(t) = A\left[\frac{B_1}{A}\cos\omega t + \frac{B_2}{A}\sin\omega t\right] = A\left[\cos\delta\cos\omega t + \sin\delta \sin\omega t\right] = A\cos(\omega t - \delta)\]
Where the last equality follows by a trigonometric identity.
\end{s}

\begin{p}
Show that the kinetic and potential energy of a simple undamped oscillator have the same amplitude but are out of phase, such that the total energy is conserved.
\end{p}
\begin{s}
Using our equation for $x(t)$ above (where $x(t)$ is the displacement from equilibrium), the total energy is given by:
\[E = U + T = \frac{1}{2}kx^2 + \frac{1}{2}m\dot{x}^2 = \frac{1}{2}kA^2\cos^2(\omega t - \delta) + \frac{1}{2}mA^2\omega^2\sin^2(\omega t - \delta)\]
We have that $\omega^2 = \frac{k}{m}$ so:
\[\frac{k}{2}A^2\cos^2(\omega t - \delta) + \frac{k}{2}A^2\sin^2(\omega t - \delta) = \frac{kA^2}{2}\]
And hence the total energy is a constant; we can therefore see that $\od{E}{t} = 0$ and that the total energy is conserved.
\end{s}

\begin{p}
Damped oscillatons are described by the ODE $\ddot{x} + 2\beta\dot{x} + \omega_0^2x = 0$. Show that for overdamped motion, the decay "constant" (decay parameter) for a damped oscillator decreases with increasing friction. Sketch the decay parameters vs. $\beta$ for the whole range of $\beta$.
\end{p}
\begin{s}
We guess a solution of the form $x(t) = \exp(rt)$. We can therefore generate a characteristic equation for $r$, by substituting in the solution into the ODE and then cancelling out the exponential terms (as these can never be zero). Hence, we have:
\[r^2 + 2\beta r  + \omega_0^2 = 0\]
This is a quadratic equation with solutions:
\[r = -\beta \pm \sqrt{\beta^2 - \omega_0^2}\]
There are now different possibilities for the damping depending on the ratio of $\beta$ and $\omega_0$. For $\beta > \omega_0$, the oscillator is overdamped. For $\beta < \omega_0$, the oscillator is underdamped. For $\beta = \omega_0$, the oscillator is critically damped. Let us now consider the overdamped case. Then, we have that the discriminant in the equation above $\beta^2 - \omega_0^2$ is positive, and the general solution is the sum of decaying exponentials:
\[x(t) = C_1\exp(r_1 t) + C_2\exp(r_2 t) \]
where $r_1 = -\beta + \sqrt{\beta^2 - \omega^2}, r_2 = -\beta - \sqrt{\beta^2 - \omega^2}$. The dominant term will be the $\exp(r_1 t)$ term as this decays more slowly. Therefore, the decay parameter is:
\[-\beta + \sqrt{\beta^2 -\omega_0^2}\]
Which decreases with increasing $\beta$. We will also have a decay parameter for the underdamped case. In this case, the decay parameter is just $\beta$, as the discriminant is negative, and hence the solution $x(t)$ is composed of an oscillating part (the imaginary exponential part from the square root) and a real exponentially decaying part ($\exp(-\beta t)$). Hence the decay parameter increases linearly in this regime. Overall, we obtain a plot that looks like the follows:

Where the decay parameter is maximized when $\beta = \omega_0$, where we have critical damping.
\begin{center}
    \includegraphics[scale=0.5]{Lecture-9/w9-img1.png}
\end{center}
\end{s}

\begin{p}
Show that for critical damping, the solution $x(t) = te^{rt}$ solves the ODE $\ddot{x} + 2\beta\dot{x} + \omega_0^2x = 0$. Find $r$. Write the general solution.
\end{p}
\begin{s}
For critical damping, we have that $\beta = \omega_0$ and we can calculate $r$ to be:
\[r = -\beta \pm \sqrt{\beta^2 - \beta^2} = -\beta\]
Hence one solution is given by:
\[x_1(t) = C_1\exp(-\beta t)\]
And we check the Ansatz provided in the question to verify it as a second solution. The first time derivative is given by:
\[\dot{x} = \exp(rt) + rt\exp(rt)\]
And the second time derivative as:
\[\ddot{x} = r\exp(rt) + r\exp(rt) + r^2t\exp(rt) = 2r\exp(rt) + r^2t\exp(rt)\]
So substituting this into the ODE, with $r = -\beta$:
\[-2\beta\exp(-\beta t) + \beta^2t\exp(-\beta t) + 2\beta\left(\exp(-\beta t) - \beta t\exp(-\beta t)\right) + \beta^2t\exp(-\beta t) = 0\]
So we can see that this is indeed a solution! Hence the general solution is given by:
\[x(t) = C_1\exp(-\beta t) + C_2 t\exp(-\beta t)\]
\end{s}

\begin{p}
Find the condition on $C$ for $z(t) = Ce^{i\omega t}$ to be a solution to $\ddot{z} + 2\beta\dot{z} + \omega_0^2z = f_0e^{i\omega t}$. Express the coefficient $C$ as $Ae^{-i\delta}$ and find $A$ and $\delta$.
\end{p}
\begin{s}
Let us plug in $z(t)$ into the ODE:
\[(-\omega^2 + 2i\beta\omega + \omega_0^2)C\exp(i\omega t) = f_0\exp(i\omega t)\]
We may cancel out the exponentials on both sides:
\[(-\omega^2 + 2i\beta\omega + \omega_0^2) =f_0\]
Hence we find:
\[C = \frac{f_0}{-\omega^2 + 2i\beta\omega + \omega_0^2} = A\exp(i\delta)\]
We have that $A^2 = CC^*$ and so:
\[A^2 = \frac{f_0}{-\omega^2 + 2i\beta\omega + \omega_0^2}\frac{f_0}{-\omega^2 - 2i\beta\omega + \omega_0^2} = \frac{f_0^2}{(\omega_0^2 - \omega^2)^2 + 4\beta^2\omega^2}\]
To get the phase, we see that:
\[f_0\exp(i\delta) = A(\omega_0^2 - \omega^2 + 2i\beta\omega)\]
And solving (using some knowledge of complex numbers and extracting their phase) we get:
\[\delta = \arctan(\frac{\Im}{\Re}) = \arctan(\frac{2\beta \omega}{\omega_0^2 - \omega^2})\]
\end{s}

\begin{p}
Write the general solution to $\ddot{x} + 2\beta\dot{x} + \omega_0^2x = f_0\cos(\omega t)$ for the underdamped case. What are the two undetermined constants?
\end{p}
\begin{s}
Writing the general solution, we have:
\[x(t) = A\cos(\omega t -\delta) + C_1\exp(r_1 t) + C_2\exp(r_2 t)\]
Where the first term is the particular solution (comes from the driving) and the exponentials come from the homogenous solution. The former is the long-term oscillatory behavior, the latter is the transient solution (the exponentials decay away with time).
\end{s}

\begin{p}
Show that the Q-factor given by the ratio of the width to the mean of the resonance curve is equal to $\pi$ times the number of oscillations in one decay time.
\end{p}
\begin{s}
When $\omega \sim \omega_0$ (when the driving frequency approaches the natural frequency), we notice a resonance phenomenon, where the amplitude of the driven oscillator gets very large (technically it is slightly off from $\omega = \omega_0$, but to good approximation it is at the natural frequency of the system). This is pictured below:
\begin{center}
    \includegraphics[scale=0.5]{Lecture-9/w9-img2.png}
\end{center}
We can also consider the width of the resonance peak. It is defined as:
\[Q = \frac{\omega_0}{2\beta}\]
Stronger damping implies a smaller quality factor and a narrower peak, and weak damping implies a larger quality factor and a broadened peak. To prove the claim provided in the question, we consider that the decay time is given by $\tau = \frac{1}{\beta}$ and the period is $T = \frac{2\pi}{\omega_0}$, so plugging this in we can immediately see that:
\[Q = \frac{\omega_0}{2\beta} = \frac{\frac{1}{\beta}\pi}{\frac{2\pi}{\omega_0}} = \pi\frac{\tau}{T}\]
which is the desired result.
\end{s}

\begin{p}
Find the phase shift at resonance when the driving frequency $\omega$ is varied. Sketch the phase shift $\delta$ vs. $\omega$.
\end{p}
\begin{s}
The phase shift is given by $\delta = \frac{\pi}{2}$ (perfectly out of phase). We can see this as the phase shift is given above by:
\[\delta = \arctan(\frac{2\beta\omega}{\omega_0^2 - \omega^2})\]
And at resonance we have $\omega \sim \omega_0$, so therefore the argument fo the arctan goes to infinity, and hence the value of $\delta$ goes to $\frac{\pi}{2}$.
\end{s}
\end{document}


\hypertarget{haskell}{%
\section{Haskell - Recursive Functions}\label{haskell}}

In Haskell, functions can also be defined in terms of themselves. Such
functions are called recursive.

\begin{itemize}
\tightlist
\item
  Some functions, such as factorial, are simpler to define in terms of
  other functions.
\item
  As we shall see, however, many functions can naturally be defined in
  terms of themselves.
\item
  Properties of functions defined using recursion can be proved using
  the simple but powerful mathematical technique of induction.
\end{itemize}

\hypertarget{recursion-on-lists}{%
\subsection{Recursion on Lists}\label{recursion-on-lists}}

\begin{lstlisting}[language=Haskell]
product :: Num a -> [a] -> a
product [] = 1
product (n:ns) = n * product ns

In execution:

product [2,3,4]
2 * product [3,4]
2 * (3 * product [4])
2 * (3 * (4 * product []))
2 * (3 * (4 * 1))
24

--or
-- The maximum function takes a list of things that can be ordered (e.g. instances of the Ord typeclass) and returns the biggest of them.

maximum' :: (Ord a) => [a] -> a
maximum' [] = error "maximum of empty list"
maximum' [x] = x
maximum' (x:xs)
    | x > maxTail = x
    | otherwise = maxTail
    where maxTail = maximum' xs
\end{lstlisting}

\hypertarget{multiple-arguments}{%
\subsection{Multiple Arguments}\label{multiple-arguments}}

Functions with more than one argument can also be defined using
recursion.

\begin{lstlisting}[language=Haskell]
zip :: [a] -> [b] -> [(a,b)]
zip []    _       = []
zip  _    []      = []
zip (x:xs) (y:ys) = (x,y) : zip xs ys
\end{lstlisting}

\subsection{Some Examples}

\begin{lstlisting}[language=Haskell]
take' :: (Num i, Ord i) => i -> [a] -> [a]
take' n _
    | n <= 0   = []
take' _ []     = []
take' n (x:xs) = x : take' (n-1) xs

reverse' :: [a] -> [a]
reverse' [] = []
reverse' (x:xs) = reverse' xs ++ [x]
\end{lstlisting}

\clearpage
\subsection{Quick Sort}

We have a list of items that can be sorted. Their type is an instance of the \textbf{Ord} typeclass. The edge condition is the empty list. Now a sorted list is a list that has all the values smaller than (or equal to) the head of the list in front (and those values are sorted), then comes the head of the list in the middle and then come all the values that are bigger than the head (they're also sorted).

\begin{lstlisting}[language=Haskell]
quicksort :: (Ord a) => [a] -> [a]
quicksort [] = []
quicksort (x:xs) =
    let smallerSorted = quicksort [a | a <- xs, a <= x]
        biggerSorted = quicksort [a | a <- xs, a > x]
    in  smallerSorted ++ [x] ++ biggerSorted
\end{lstlisting}

So if we have, say [5,1,9,4,6,7,3] and we want to sort it, this algorithm will first take the head, which is 5 and then put it in the middle of two lists that are smaller and bigger than it. So at one point, you'll have [1,4,3] ++ [5] ++ [9,6,7]. We know that once the list is sorted completely, the number 5 will stay in the fourth place since there are 3 numbers lower than it and 3 numbers higher than it. Now, if we sort [1,4,3] and [9,6,7], we have a sorted list! We sort the two lists using the same function. Eventually, we'll break it up so much that we reach empty lists and an empty list is already sorted in a way, by virtue of being empty.

\clearpage


\chapter{Dimension Reduction}
\emph{“你们都不是49年的国军，你们是50年的国军。”}
\newpage

\section{Principal Component Analysis}
    \emph{aka. Discrete Karhunen Loeve Transform; Hotelling Transform}

    Let $y \in \mathbb{R}^K$ be a feature vector computed from $x \in \mathbb{R}^D$ where $K \ll D$. PCA determines an orthogonal matrix $W \in \mathbb{R}^{D \times K}$ s.t.
    \[ y = W^Tx \]

    Assume in this section that $x$ has a distribution with mean $\mu=0$ and covariance matrix $\Sigma$.

    \subsection{Objective Funtion}
        PCA minimizes the reconstruction error between original $x$ and reconstructed $\hat{x}$.

        Notice that $W$ is orthogonal, so $x$ can be reconstructed by
        \[ \hat{x} = Wy \]

        Let the reconstruction error be
        \[ \epsilon = x - \hat{x} = x - WW^Tx \]

        We want to minimize the reconstruction error
        \[ \|\epsilon\|^2 = \epsilon^T\epsilon = x^Tx - x^TWW^Tx \]

        The minimization is performed over all possible $x$
        \[ \int p(x)\epsilon(x)^T\epsilon(x)\mathrm{d}x \]

        \[ \mathbb{E}[\epsilon^T\epsilon] = \mathbb{E}[x^Tx] - \mathbb{E}[x^TWW^Tx]\]

    \subsection{Simple Case}
        If $K=1$, $w$ is a vector and $y$ is a scalar.
        \[\mathbb{E}[\epsilon^T\epsilon] = \mathbb{E}[x^Tx]-w^T\mathbb{E}[xx^T]w = \mathbb{E}[x^Tx] - w^T \Sigma w\]
        where $\Sigma = XX^T$ (recall that $X$ is assumed to have zero mean).

        We maximize the normalized second term
        \[ \max_w \quad J \triangleq \frac{w^TXX^Tw}{w^Tw} \]

        Or it can also be formulated as a equality constrained maximization and solved by Lagrangian.

        The solution is
        \[ XX^Tw=Jw \]

        So the problem becomes finding the eigenvalues of $XX^T$, to maximize $J$, we find the largest eigenvalue of $XX^T$.

        Other dimensions turn out to be the remaining eigenvectors.

    \subsection{Geometric Interpretation}
        \begin{itemize}
            \item PCA is a shift and rotation of axis.
            \item $w_1$ is the direction of greatest elongation
            \item $w_2$ is the second largest elongation that is orthogonal to $w_1$
        \end{itemize}

\section{Multi-Dimensional Scaling}
    MDS aims to keep the distances of samples in the mapped space that same as the distances in the original space.

    In this section we also assume the data samples have $\mu=0$.

    \[ \min_{\hat{x}} \left(\sum_{i<j}\left( \|\hat{x}_i-\hat{x}_j\| - d_{ij} \right)^2\right)^{1/2} \]
    where $\hat{x}$ are mapped (instead of reconstructed) samples.

    Let $X$ be the unknown \emph{dimension-reduced} samples. Let $T=XX^T$ and $t_{ij}=\hat{x}_i^T\hat{x}_j$.

    The distance between $\hat{x}_i$ and $\hat{x}_j$ is given by
    \[d_{ij}^2 = (\hat{x}_i-\hat{x}_j)^T(\hat{x}_i-\hat{x}_j)\]

    Solving for $t_{ij}$ yields
    \[ t_{ij} = -\frac{1}{2}(d_{ij}^2\hat{x}_i^2-\hat{x}_j^2) \]

    We also have
    \[\sum_j d_{ij}^2 = n\hat{x}_i^2 + \sum_j\hat{x}_j^2 - 2\hat{x}_i\sum_j\hat{x}_j = n\hat{x}_i^2 + \sum_j\hat{x}_j^2\]
    \[\sum_i d_{ij}^2 = n\hat{x}_j^2 + \sum_i\hat{x}_i^2 - 2\hat{x}_j\sum_i\hat{x}_i = n\hat{x}_j^2 + \sum_i\hat{x}_i^2\]
    \[\sum_{i}\sum_{j}d_{ij}^2=n\sum_i\hat{x}_i^2 + n\sum_j\hat{x}_j^2 \]
    Again recall that the data are assumed to have zero mean so the sum over all data samples yields zero.

    So we can solve for $t_{ij}$
    \[ t_{ij} = -\frac{1}{2}\left(d_{ij}^2-\frac{1}{n}\sum_kd_{ik}^2-\frac{1}{n}\sum_kd_{kj}^2-\frac{1}{n}\sum_{k}\sum_{l}d_{kl}^2\right) \]
    where $d_{ij}$ can be computed from the original dataset.

    Since $T=XX^T$, we can solve for $X$ by decomposing $T$.
    \[T = U\Lambda U^T = U\Lambda^{1/2}\Lambda^{1/2}U^T\]

    Therefore $X=U\Lambda^{1/2}$

\section{Non-Linear Dimension Reduction}
    \subsection{ISOmetric feature MAPping}
        Uses the geodesic distance (shortest distance on a plane) instead of Euclidean distance. The remaining work is similar to MDS and is pigeoned in this note.

    \subsection{Locally Linear Embedding}
        Assumes that a data sample can be represented by some linear combination of its local neighbors\footnote{This assumption is reasonable because the distribution can be somehow ``linearized'' in a sufficiently small area, just like derivatives.}, and that this relationship does not change after dimension reduction.

        LLE can basically be divided into two steps. We first solve for the locally linear combination relationship\footnote{Actually we will first have to find the local nearest neighbours for each data sample $x_i$.} in the original space (solve for a coefficient matrix $W$), and then reconstruct the relationship in a mapped space (solve for mapped data $Y$).

        \subsubsection{Solving Linear Combination}
        In the original space, let $x_j$ be some neighbors of $x_i$. Let $W \in \mathbb{R}^{N \times N}$.
        \[ \epsilon(W) = \sum_i \left|x_i - \sum_jW_{ij}x_j\right|^2 \]
        \begin{align*}
            \min_W &\quad \epsilon(W)\\
            \text{s.t.} &\quad W_{ij} = 0 \quad x_j \notin \mathcal{N}(x_i)\\
            &\quad \sum_j W_{ij}=1
        \end{align*}

        Can be solved by Lagrangian multipliers.

        \subsubsection{Solving Mapping}
        In the mapped space
        \[ \phi(y) = \sum_i\left| y_i - \sum_j W_{ij}y_j \right|^2 \]
        \begin{align*}
            \min_W &\quad \phi(y)\\
            \text{s.t.} &\quad \sum_i y_i = 0\\
            &\quad \frac{1}{N} YY^T = I
        \end{align*}
        where $Y$'s columns are $y_i$.

        To make the problem easier, we convert it into a quadratic form. Define an error matrix
        \[ \delta = Y-YW \triangleq [\Delta y_1,\Delta y_2,\cdots,\Delta y_N]\]

        The total error is given by
        \[ \sum_{i=1}^N \Delta y_i^T \Delta y_i \]

        Notice that
        \[ \delta^T\delta = \begin{bmatrix}
            \Delta y_1^T \Delta y_1 & \Delta y_1^T \Delta y_2 & \cdots & \Delta y_1^T \Delta y_N\\
            \Delta y_2^T \Delta y_1 & \Delta y_2^T \Delta y_2 & \cdots & \Delta y_2^T \Delta y_N\\
            \vdots & \vdots & \ddots & \vdots\\
            \Delta y_N^T \Delta y_1 & \Delta y_N^T \Delta y_2 & \cdots & \Delta y_N^T \Delta y_N
        \end{bmatrix} \]

        So the error can be expressed as
        \[ Tr[\delta^T\delta] = Tr[(Y(I-W))^T(Y(I-W))] = Tr[(I-W)^TY^TY(I-W)] = Tr[Y^T(I-W)^T(I-W)Y] \]

        Let $M = (I-W)^T(I-W)$, the objective function becomes
        \[ \min Tr(Y^TMY) \]

        So we can solve for $Y$ by Lagrangian multipliers.