% Chapter II: Groups, first encounter
\chapter{Groups, first encounter}

\section{Definition of group}
\begin{xca}
Let $(G, \bullet)$ be a group with $e$ denoting the identity element of $G$. We
construct a category $\mc{C}$ as follows:
\begin{itemize}
    \item $\opn{Obj}(\mc{C}) := \{ \ast \}$.
    \item $\Hom_{\mc{C}}(\ast, \ast) := G$.
    \item $1_{\ast} = e$.
\end{itemize}
Then, it is easy to check $\mc{C}$ is indeed a category. Also, since every
element $g \in G$ has an inverse, every morphism $\ast \to \ast$ has an inverse.
That is, every morphism $\ast \to \ast$ is an isomorphism. Thus, $\mc{C}$ is a
groupoid. Hence, we conclude every group is the group of isomorphisms of a
groupoid (with a single object.)

In particular, we note every group is the group of automorphisms of some object
in some category.
\end{xca}

\begin{xca}
$(\Z, +), (\Q, +), (\R, +)$, and $(\C, +)$ are all groups with (additive)
identity $0$.

$(\Q^*, \cdot), (\R^*, \cdot)$, and $(\C^*, \cdot)$ are all groups, with
(multiplicative) identity $1$.
\end{xca}

\begin{xca}
For all elements $g, h$ of a group $G$, we note $(gh)(h^{-1} g^{-1}) =
g(h h^{-1})g^{-1} = g 1_G g^{-1} = g g^{-1} = 1_G$, and $(h^{-1} g^{-1})(gh) =
h^{-1}(g^{-1}g)h = h^{-1} 1_G h = h^{-1} h = 1_G$. Therefore, we conclude
$(gh)^{-1} = h^{-1} g^{-1}$.
\end{xca}

\begin{xca}
Suppose $g^2 = e$ for all elements $g$ of a group $G$. Then, $g^{-1} = g^{-1} e
= g^{-1}(g^2) = (g^{-1} g) g = e g = g$. That is, every element $g$ of $G$ is
its own inverse. Therefore, for all $g, h \in G$, $gh = g^{-1} h^{-1} =
(hg)^{-1} = hg$, thereby implying $G$ is commutative.
\end{xca}

\begin{xca}
Suppose the row corresponding to an element $g$ in the multiplication table of
a group contains the same element in two different columns that correspond to
(distinct) elements, $h_1$ and $h_2$, say. Then, $g h_1 = g h_2$, which, by
cancellation on the left, implies $h_1 = h_2$, a contradiction. \\
The argument is similar for any column of the multiplication table of the group.

Hence, we conclude every row and column of the multiplication table of a group
contains all elements of the group exactly once. \\

The above statements can also be recast in the following form: \\
Let $g$ be an element of a group $G$. Then, the mappings given by \[ h \mapsto
gh \] and \[ h \mapsto hg, \] for all elements $h \in G$, are bijective.
\end{xca}

\begin{xca}
($G$ has one element) This element must be the identity $1_G$. And, thus,
there can be only one multiplication table for $G$: $1_G 1_G = 1_G$.

($G$ has two elements) Let these elements be $1_G$ and $a$. Now, if $a a = a$,
then by the cancellation property, $a = 1_G$, a contradiction. Thus, we must
have $a a = 1_G$. Therefore, there is only one multiplication table for $G$:
$1_G 1_G = 1_G, a a = 1_G$.

($G$ has three elements) Let these distinct elements be $1_G, a, b$. Now, $a b
\neq a$, for otherwise, by cancellation, $b = 1_G$, a contradiction. Similarly,
$ab \neq b$. Thus, we must have $a b = 1_G$. That is, $a$ and $b$ are inverses
of each other. Also, $a a \neq 1$, for otherwise, $a = a^{-1}$, which implies
$a = b$, a contradiction. Again, $a a \neq a$, for otherwise, by cancellation,
$a = 1_G$, a contradiction. Thus, $a a = b$. Similarly, $b b = a$. And, this
exhausts all the possible cases for the multiplication table of $G$. Hence,
there is only one multiplication table for $G$: $a b = 1_G, a a = b, b b = a$.

The above thus shows there is only \emph{one} possible multiplication table for
$G$ if $G$ has exactly 1, 2, or 3 elements.

($G$ has four elements) Let these distinct elements be $1_G, a, b, c$. There
are two possible choices for $ab$: $ab = 1_G$, or $ab = c$. Other choices for
$ab$, \emph{viz.} $a$, or $b$, lead to contradictions. The two choices are:
\begin{itemize}
    \item ($ab = 1_G$) Now, $ac \neq c$, for otherwise, we get $a = 1_G$, a
    contradiction. So, we must have $ac = b$, and thus, $aa = c$. Once we fill
    in the (partially-completed) multiplication table with the above data, we
    can fill in the rest of the multiplication table as follows. We can't have
    $ba = b$, so we must have $ba = 1_G$, and hence, $ca = b$. Then, $bb = c$,
    and $cb = a$, which forces $bc = a$, and $cc = 1_G$.

    The completed multiplication table in this case is as follows:
    \[
    \begin{array}{c || c | c | c | c |}
          & 1 & a & b & c \\
        \hline \hline
        1 & 1 & a & b & c \\
        \hline
        a & a & c & 1 & b \\
        \hline
        b & b & 1 & c & a \\
        \hline
        c & c & b & a & 1 \\
        \hline
    \end{array}
    \]
    A little bit of calculation shows $a^2 = c$, $a^3 = a^2 a = ca = b$, and
    $a^4 = cc = 1_G$. Therefore, the above multiplication table can be entirely
    rewritten in terms of $1_G$ and $a$ as follows:
    \[
    \begin{array}{c || c | c | c | c |}
            & 1   & a   & a^2 & a^3 \\
        \hline \hline
        1   & 1   & a   & a^2 & a^3 \\
        \hline
        a   & a   & a^2 & a^3 & 1 \\
        \hline
        a^2 & a^2 & a^3 & 1   & a \\
        \hline
        a^3 & a^3 & 1   & a   & a^2 \\
        \hline
    \end{array}
    \]
    The above table is precisely the one for the \emph{cyclic group} $C_4$ of
    order $4$.

    \item ($ab = c$) The elements of the group are $1, a, b$, and $ab$. There
    are two possible choices for $a^2$: $1$, or $b$. If $a^2 = b$, then we
    have $b = a^2, ab = a^3$, which reduces to the previous case above. So, we
    are left with the only case to consider: $a^2 = 1$. Then, this forces
    $a(ab) = b$. Now, if $b^2 = a$, then this again reduces to the previous
    case above (up to isomorphism.) So, we are left with only one case to
    consider: $b^2 = 1$. Therefore, $b(ab) = a$. Filling up the rest of the
    multiplication table, we finally obtain $(ab)a = b, (ab)b = a$, and $(ab)^2
    = 1$.

    So, the completed multiplication table looks as follows:
    \[
    \begin{array}{c || c | c | c | c |}
           & 1  & a  & b  & ab \\
        \hline \hline
        1  & 1  & a  & b  & ab \\
        \hline
        a  & a  & 1  & ab & b \\
        \hline
        b  & b  & ab & 1  & a \\
        \hline
        ab & ab & b  & a  & 1 \\
        \hline
    \end{array}
    \]
\end{itemize}
Hence, we conclude there are \emph{two} distinct tables, up to reordering the
elements of $G$, if $G$ has exactly four elements.

Using the tables above, it is also easy to verify all groups with $\le 4$
elements are commutative.
\end{xca}

\begin{xca}
(Prove Corollary 1.11) Let $g$ be an element of finite order, and let $N \in
\Z$. We claim
\[ g^N = e \iff N \text{ is a multiple of } |g|. \]

Note, if $N = 0$, then the statement
\[ g^0 = e \iff 0 \text{ is a multiple of } |g| \]
holds, since both sides of the double implication are trivially true. Hence,
the statement holds for $N = 0$.

We now need prove our claim only for all $N \ne 0$.

($\implies$) Suppose $g^N = e$, where $N \ne 0$. If $N > 0$, then by Lemma 1.10,
$N$ is a multiple of $|g|$. And, if $N < 0$, then $g^{-N} = g^{-N} e = g^{-N}
g^N = g^{-N + N} = g^0 = e$, and since $-N > 0$, again, by Lemma 1.10, $-N$ is
a multiple of $|g|$. Hence, for all $N \ne 0$, $N$ is a multiple of $|g|$.

($\impliedby$) Suppose $N$ is a multiple of $|g|$, where $N \ne 0$. Then, $N =
n |g|$, for some $0 \ne n \in \Z$. Therefore, $g^N = g^{n|g|} = (g^{|g|})^n =
e^n = e$.

Hence, our original claim is proved.
\end{xca}

\begin{xca}
Suppose $G$ is a finite abelian group with exactly one element $f$ of order $2$.
That is, $f^2 = 1_G$. Then, $f^{-1} = f$, which means $f$ is its own inverse.
Therefore, every element, other than $f$, of $G$, necessarily has an inverse
that is not $f$. And, since the elements of the group commute, every
(non-identity) term in $\prod_{g \in G} g$ can be placed next to its inverse,
and all these pairs reduce to $1_G$, except $f$. Hence, $\prod_{g \in G} g =
f$.
\end{xca}

\begin{xca}
Let $G$ be a finite group, of order $n$, and let $m$ be the number of elements
$g \in G$ of order exactly $2$. We claim $n-m$ is odd.

First, note the identity element is the only element in a group with order $1$.
Also, it is easy to check, for all non-identity elements $g \in G$,
\[ |g| = 2 \iff g = g^{-1}. \] That is, elements with order $2$ are precisely
the elements that are their own inverses (not counting the identity element.)
This implies elements $g$ with order $> 2$ have inverses that are distinct from
themselves. Thus, these elements come in pairs, which all have distinct
components. So,
\begin{align*}
    |G| &= \sum_{|g| \in \Z^+} \text{ \# of elements with order } |g| \\
        &= \sum_{|g| = 1} \bullet + \sum_{|g| = 2} \bullet + \sum_{|g| > 2}
        \bullet \\
        &= 1 + m + 2k,
\end{align*}
for some $k \in \N$. Therefore, $n - m = 2k + 1$, showing $n-m$ is indeed odd.

In addition, if $n$ is even, then using the previous equation, it is easy to
see $m = n - 2k + 1$ is odd, and hence $G$ necessarily contains elements of
order 2.
\end{xca}

\begin{xca}
Suppose the order of $g$ is odd. Then $|g| = 2n + 1$, for some $n \in \N$.
Using Proposition 1.13, we have
\begin{align*}
    |g^2| &= \frac{\lcm(2, |g|)}{2} \\
          &= \frac{\lcm(2, 2n + 1)}{2} \\
          &= \frac{2(2n + 1)}{2} \\
          &= 2n + 1
\end{align*}
We thus conclude $|g^2| = |g|$.
\end{xca}

\begin{xca}
We claim for all $g, h$ in a group $G$, $|gh| = |hg|$.

To that end, we first prove the following ancillary claim:
\[ |aga^{-1}| = |g| \text{ for all } a, g \in G. \]

Indeed, suppose $a, g$ are elements of a group $G$, and let $|g| = n$, where
$n \in \Z^+$. Then, $(aga^{-1})^n = \underbrace{(aga^{-1}) \cdots
(aga^{-1})}_{n \text{ times}} = a g^n a^{-1} = a 1_G a^{-1} = aa^{-1} = 1_G$.

So, $m = |aga^{-1}|$ divides $n$. Now, suppose $m < n$. Then, $(aga^{-1})^m =
1_G = (aga^{-1})^n$, which implies $a g^m a^{-1} = a g^n a^{-1}$, which by
cancellation, $g^m = g^n$, which implies $g^{n-m} = 1_G$, which (since
$n-m < n$) contradicts the assumption $|g| = n$. Therefore, $|aga^{-1}| = n =
|g|$, which proves our ancillary claim.

Now, using the previous result, we immediately conclude, for all $g, h$ in a
group $G$, $|gh| = |gh1_G| = |gh(gg^{-1})| = |g(hg)g^{-1}| = |hg|$, and we are
done.
\end{xca}

\begin{xca}
In the group of invertible $2 \times 2$ matrices, consider
\begin{center}
    $g =
    \left( \begin{array}{cc}
        0 & -1 \\
        1 & 0 \end{array} \right)$,
    $h =
    \left( \begin{array}{cc}
        0  & 1 \\
        -1 & -1 \end{array} \right)$.
\end{center}
We verify below $|g| = 4, |h| = 3$, and $|gh| = \infty$.

It is easy to verify
\begin{center}
    $g^2 = \left( \begin{array}{cc}
               -1 & 0 \\
                0 & -1 \end{array} \right)$,
    $g^3 = \left( \begin{array}{cc}
        0 & 1 \\
        -1 & 0 \end{array} \right)$, and
    $g^4 = \left( \begin{array}{cc}
        1 & 0 \\
        0 & 1 \end{array} \right)$.
\end{center}
Therefore, $|g| = 4$.

Again, it is easy to verify
\begin{center}
    $h^2 = \left( \begin{array}{cc}
               -1 & -1 \\
                1 & 0 \end{array} \right)$, and
    $h^3 = \left( \begin{array}{cc}
               1 & 0 \\
               0 & 1 \end{array} \right)$.
\end{center}
Therefore, $|h| = 3$. \\

Now, note
\[
gh = \left( \begin{array}{cc}
          1 & 1 \\
          0 & 1 \end{array} \right).
\]
Using induction on $n$, it is easy to show
\[
(gh)^n = \left( \begin{array}{cc}
             1 & n \\
             0 & 1 \end{array} \right),
\]
for all $n \in \Z^+$. Therefore, $(gh)^n \neq I_n$ for any positive integer
$n$, where $I_n$ is the $n \times n$ (identity) diagonal matrix. Hence, $|gh| =
\infty$.
\end{xca}

\begin{xca}
Consider the cyclic group $C_4$ of order $4$, generated by $a$. Choose elements
$g = a$ and $h = a^3$. Then, it is easy to verify $|g| = 4$ and $|h| = 4$.
Therefore, $\lcm(|g|, |h|) = \lcm(4, 4) = 4$. And, $|gh| = |aa^3| = |a^4| =
|e| = 1$. Also, $g$ and $h$ commute. Thus, we have an example wherein $g, h$
commute but $|gh| = 1 \ne 4 = \lcm(|g|, |h|)$.
\end{xca}

\begin{xca}
Suppose $g$ and $h$ commute \emph{and} $\gcd(|g|, |h|) = 1$. We claim $|gh| =
|g||h|$.

To that end, let $m = |g|$ and $n = |h|$, so that $\gcd(m, n) = 1$. By
Proposition 1.14, $|gh|$ divides $\lcm(|g|, |h|) = \lcm(m, n) = mn/\gcd(m, n) =
mn/1 = mn$. Let $N = |gh|$. So, $N \mid mn$.

Now, $(gh)^N = 1$
\begin{align*}
    &\implies g^N = (h^{-1})^N \\
    &\implies |g^N| = |(h^{-1})^N| \\
    &\implies |g^N| = |h^N| \\
    &\implies \frac{|g|}{\gcd(N, |g|)} = \frac{|h|}{\gcd(N, |h|)} \\
    &\implies \frac{m}{\gcd(N, m)} = \frac{n}{\gcd(N, n)} \\
    &\implies m \cdot \gcd(N, n) = n \cdot \gcd(N, m)
\end{align*}
Since $\gcd(m, n) = 1$, it follows $m \mid \gcd(N, m)$ and $n \mid \gcd(N, n)$.
And, since $\gcd(N, m) \mid N$ and $ \gcd(N, n) \mid N$, it follows $m \mid N$
and $n \mid N$, whence $mn \mid N$. But, $N \mid mn$, and so, $N = mn$. Thus,
$|gh| = N = mn = |g||h|$, and this proves our claim.
\end{xca}

\begin{xca}
%TODO: Chapter II - Exercise 1.15
\end{xca}

\section{Examples of groups}
\begin{xca}
One can associate an $n \times n$ matrix $M_{\sigma}$ with a permutation
$\sigma \in S_n$ by letting the entry at $(i, (i)\sigma)$ be $1$ and letting
all other entries be 0. For example, the matrix corresponding to the
permutation
\[
\sigma =
    \begin{pmatrix}
        1 & 2 & 3 \\
        3 & 1 & 2
    \end{pmatrix}
        \in S_3
\]
would be
\[
M_{\sigma} =
    \begin{pmatrix}
        0 & 0 & 1 \\
        1 & 0 & 0 \\
        0 & 1 & 0
    \end{pmatrix}.
\]
We show that, with this notation, \[ M_{\sigma \tau} = M_{\sigma} M_{\tau} \]
for all $\sigma, \tau \in S_n$, where the product on the right is the ordinary
product of matrices.

First, note $\sigma \tau \in S_n$, and so, $M_{\sigma \tau}$ is an $n \times n$
matrix where the entry at $(i, (i)\sigma \tau)$ is $1$ and all other entries is
$0$. And, $M_{\sigma} M_{\tau}$ is an $n \times n$ matrix whose entry at
$(i, j)$ equals \[ \sum_{r=1}^{n} (M_{\sigma})_{i,r} (M_{\tau})_{r,j}, \] which
equals $1$ (otherwise, $0$) iff $(M_{\sigma})_{i,r} = 1 = (M_{\tau})_{r,j}$ for
some $1 \le r \le n$ iff $(i)\sigma = r$ and $(r)\tau = j$ for some $1 \le r \le
n$ iff $((i)\sigma) \tau = j$ iff $(i)(\sigma \tau) = j$ iff the entry at
$(i, (i)\sigma \tau)$ of $M_{\sigma \tau}$ equals $1$ and all other entries is
$0$. We thus conclude $M_{\sigma \tau} = M_{\sigma} M_{\tau}$, and we are done.
\end{xca}

\begin{xca}
Suppose $d \le n$. Then, consider the permutation $\sigma \in S_n$ given by
\[ 1 \to 2 \to 3 \to \ldots \to d \to 1 \] and all positive integers $> d$
being mapped to themselves. Then, clearly, $|\sigma| = d$. This shows $S_n$
contains elements of order $d$.
\end{xca}

\begin{xca}
For every positive integer $n$, an element $\sigma \in S_{\N}$ of order $n$ is
given by the mapping \[ 1 \to 2 \to 3 \to \ldots \to n \to 1 \] such that
$n+i$ is mapped to itself, for all positive integers $i \ge 1$.
\end{xca}

\begin{xca}
%TODO - Chapter II: Ex 2.4
\end{xca}

\begin{xca}
%TODO - Chapter II: Ex 2.5
\end{xca}

\begin{xca}
%TODO - Chapter II: Ex 2.6
\end{xca}

\begin{xca}
%TODO - Chapter II: Ex 2.7
\end{xca}

\begin{xca}
%TODO - Chapter II: Ex 2.8
\end{xca}

\begin{xca}
We verify `congruence mod $n$' is an equivalence relation. Indeed, let $n$ be a
positive integer.

(Reflexivity) For all $a \in \Z$, $n \mid (a - a)$, and so, $a \equiv a \mod n$.

(Symmetry) For all $a, b \in \Z$, if $a \equiv b \mod n$, then $n \mid (b - a)$,
which implies $n \mid (a - b)$, and thus, $b \equiv a \mod n$.

(Transitivity) For all $a, b, c \in \Z$, if $a \equiv b \mod n$ and $b \equiv c
\mod n$, then $n \mid (b - a)$ and $n \mid (c - b)$, and since $c - a = (c - b)
+ (b - a)$, $n \mid (c - a)$, and so, $a \equiv c \mod n$.

And, we are done.
\end{xca}

\begin{xca}
We claim $\Z/{n\Z}$ consists precisely of $n$ elements, for all positive
integers $n$.

Indeed, suppose $n$ is a positive integer. First, note the $n$ equivalence
classes \[ [0]_n, [1]_n, \ldots, [n-1]_n \] are all distinct, for if $[i]_n =
[j]_n$ for some $0 \le i < j < n$, then $n \mid (j-i)$, a contradiction, since
$0 < j-i < n$.

Next, note for any $a \in \Z$, by the Euclidean algorithm, $a = qn + r$, for
some $q, r \in \Z$, where $0 \le r < n$. That is, $n \mid (a-r)$, and so,
$a \equiv r \mod n$, and thus, $[a]_n = [r]_n$. That is to say, the equivalence
class of any integer equals one of the $n$ equivalence classes stated above.

Hence, we conclude $\Z/{n\Z}$ consists precisely of $n$ elements.
\end{xca}

\begin{xca}
We show the square of every odd integer is congruent to $1$ modulo $8$.

Indeed, consider $\Z/{8\Z}$. Suppose $a \in \Z$ is an odd integer. Then, $[a]_8$
equals $[1]_8, [3]_8, [5]_8$, or $[7]_8$. But, $[1]_8^2 = [3]_8^2 = [5]_8^2 =
[7]_8^2 = [1]_8$, which implies $[a]_8^2 = [1]_8$, and hence, $a^2 \equiv 1 \mod
8$, and we are done.
\end{xca}

\begin{xca}
We show there are no \emph{nonzero} integers $a, b, c$ such that $a^2 + b^2 =
3c^2$.

To that end, we study the solutions to the equation $[a]_4^2 + [b]_4^2 =
3[c]_4^2$ in $\Z/{4\Z}$. First, note $[0]_4^2 = [0]_4, [1]_4^2 = [1]_4, [2]_4^2
= [0]_4$, and $[3]_4^2 = [1]_4$. This implies $3[c]_4^2$ equals $[0]_4$ or
$[3]_4$. Thus, any solutions to the equation above exist only when $[a]_4^2 +
[b]_4^2 = [0]_4 = 3[c]_4^2$, and this is possible precisely when $a, b, c$ are
all even.

Now, assume, for the sake of contradiction, triples $(a, b, c)$ (where $a, b,
c$ are all nonzero) exist that are solutions to the original equation $a^2 +
b^2 = 3c^2$. Then, from the foregoing argument, $a, b, c$ must all be even. Let
us restrict our attention, for the moment, only to positive integer solutions
$c$. Using the well-ordering principle, there must exist a smallest $c$, such
that with the corresponding $a$ and $b$, the triple $(a, b, c)$ is a solution
to the original equation. Now, let $a = 2k, b = 2l$, and $c = 2m$, for some
integers $k, l, m$. Therefore, $(2k)^2 + (2l)^2 = 3(2m)^2$, which implies
$k^2 + l^2 = 3m^2$, which is of the same form as the original equation. But,
this implies there exists some triple $(k, l, m)$ that is a solution to the
original equation, where $m = c/2 < c$, thus contradicting the assumption that
$c$ is the smallest positive integer such that $(a, b, c)$ (for some $a, b$) is
a solution to the original equation.

We obtain a similar result if we assume $c$ is a negative integer. Hence, we
conclude there do not exist nonzero integers $a, b, c$, such that $a^2 + b^2 =
3c^2$.
\end{xca}

\begin{xca}
Suppose $\gcd(m, n) = 1$. Then, by Corollary 2.5, the class $[m]_n$ generates
$\Z/{n\Z}$. Therefore, there exists some $a \in \Z$ such that $a[m]_n = [1]_n$,
which implies $[am]_n = [1]_n$, and thus, $am \equiv 1 \mod n$, which implies
$n \mid am - 1$. Therefore, there exists some integer $k$ such that $am - 1 =
kn$, and thus, $am + (-k)n = 1$. We thus conclude there exist integers $a$ and
$b = -k$ such that $am + bn = 1$.

Conversely, suppose $am + bn = 1$ for some integers $a$ and $b$. Then, since
$\gcd(m, n)$ divides both $m$ and $n$, $\gcd(m, n) \mid am + bn = 1$. This
forces $\gcd(m, n) = 1$, and we are done.
\end{xca}

\begin{xca}
(Analog of Lemma 2.2) If $a \equiv a' \mod n$ and $b \equiv b' \mod n$, then
\[ ab \equiv a'b' \mod n. \]

(Proof) Suppose $a \equiv a' \mod n$ and $b \equiv b' \mod n$. Then, $n \mid
a' - a$ and $n \mid b' - b$, and since $a'b' - ab = a'(b' - b) + b(a' - a)$,
$n \mid a'b - ab$, which implies $ab \equiv a'b' \mod n$. And, this completes
our proof.

The above statement shows if $[a]_n = [a']_n$ and $[b]_n = [b']_n$, then
$[ab]_n = [a'b']_n$, thus showing the multiplication on $\Z/{n\Z}$ is a
well-defined operation.
\end{xca}

\begin{xca}
Let $n > 0$ be an odd integer.
\begin{itemize}
\item We show if $\gcd(m, n) = 1$, then $\gcd(2m + n, 2n) = 1$. We prove the
contrapositive of our claim. Indeed, suppose $\gcd(2m + n, 2n) = d \ne 1$. Then,
$d \mid 2m + n$ and $d \mid 2n$. Now, since $n$ is an odd integer, $2m + n$ is
odd and $2n$ is even. Therefore, $d$ must be an odd integer, and since $d$ and
$2$ are relatively prime, $d \mid n$. In addition, $d \mid 2m$, which implies
$d \mid m$, and since $\gcd(m, n) \ge d$, $\gcd(m, n) \ne 1$, and we are done.

\item Suppose $\gcd(r, 2n) = 1$. Then, $r$ must be an odd integer, and hence,
$(r - n)$ is an even integer. Furthermore, there exist integers $a, b$ such that
$ar + b(2n) = 1$, which implies \[ 2a\left(\frac{r - n}{2}\right) + (a + 2b)n =
1. \] Therefore, $\gcd(\frac{r - n}{2}, n) = 1$, and we are done.

\item We now show the function given by \[ [m]_n \mapsto [2m + n]_{2n} \] is a
bijection between $(\Z/{n\Z})^*$ and $(\Z/{2n\Z})^*$.

Indeed, note that since $\gcd(m, n) = 1$ implies $\gcd(2m + n, 2n) = 1$, the
above mapping does define a function between $(\Z/{n\Z})^*$ and $(\Z/{2n\Z})^*$.

(Injective) Suppose for any $[m_1]_n, [m_2]_n \in (\Z/{n\Z})^*$, $[2m_1 + n] =
[2m_2 + n]$. Then, $2m_1 + n \equiv 2m_2 + n \mod 2n$, which implies $2n \mid
(2m_2 + n) - (2m_1 + n)$, and thus, $2n \mid 2(m_2 - m_1)$, and so, $n \mid
(m_2 - m_1)$, from which we conclude $m_1 \equiv m_2 \mod n$, and hence,
$[m_1]_n = [m_2]_n$, thereby proving the aforesaid mapping is injective.

(Surjective) Let $[2m + n]_{2n} \in (\Z/{2n\Z})^*$, where $m \in \Z$. Then,
$\gcd(2m + n, 2n) = 1$, which, by one of the previous results, implies
$\gcd(\frac{2m + n - n}{2}, n) = 1$, and thus, $\gcd(m, n) = 1$. Therefore,
$[m]_n \in (\Z/{n\Z})^*$. Hence, $[m]_n$ in $(\Z/{n\Z})^*$ maps to
$[2m + n]_{2n}$ in $(\Z/{2n\Z})^*$. Thus, the aforesaid function is surjective.

Thus, from the foregoing arguments, it follows the defined function between
$(\Z/{n\Z})^*$ and $(\Z/{2n\Z})^*$ is a bijection.

~\\
The number $\phi(n)$ of elements of $(\Z/{n\Z})^*$ is \emph{Euler's}
$\phi$-\emph{function}. We have just proved if $n$ is odd, then $\phi(2n) =
\phi(n)$.
\end{itemize}
\end{xca}

\begin{xca}
We show the last digit of $1238237^{18238456}$ is $1$. Working in $\Z/{10\Z}$,
we first note $[1238237]_{10} = [1238230 + 7]_{10} = [1238230]_{10} + [7]_{10}
= [0]_{10} + [7]_{10} = [0 + 7]_{10} = [7]_{10}$. Therefore, $[1238237]_{10}^4
= [7]_{10}^4 = [7^2]_{10}^2 = [49]_{10}^2 = [-1]_{10}^2 = [1]_{10}$. Thus,
$[1238237]_{10}^{4 \cdot 4559614} = [1]_{10}^{4559614}$. That is,
$[1238237]_{10}^{18238456} = [1]_{10}$. Hence, $1238237^{18238456} \equiv 1
\mod 10$, whence the last digit of $1238237^{18238456}$ is $1$.
\end{xca}

\begin{xca}
Suppose $m \equiv m' \mod n$. Then, $[m]_n = [m']_n$
\begin{align*}
&\implies m \cdot [1]_n = m' \cdot [1]_n \\
&\implies |m \cdot [1]_n| = |m' \cdot [1]_n| \\
&\implies \frac{|[1]_n|}{\gcd(m, |[1]_n|)} = \frac{|[1]_n|}{\gcd(m', |[1]_n|)}
\q (\text{by Proposition 1.13}) \\
&\implies \frac{n}{\gcd(m, n)} = \frac{n}{\gcd(m', n)} \\
&\implies \gcd(m, n) = \gcd(m', n).
\end{align*}
Hence, $\gcd(m, n) = 1$ iff $\gcd(m', n) = 1$.
\end{xca}

\begin{xca}
% TODO - Chapter II: Ex 2.18
\end{xca}

\begin{xca}
$(\Z/{5\Z})^* = \{ [1]_5, [2]_5, [3]_5, [4]_5 \}$ and its multiplication table
is given below:
\[
\begin{array}{c || c  c  c  c |}
          & [1]_5 & [2]_5 & [3]_5 & [4]_5 \\
    \hline \hline
    [1]_5 & [1]_5 & [2]_5 & [3]_5 & [4]_5 \\
    \hline
    [2]_5 & [2]_5 & [4]_5 & [1]_5 & [3]_5 \\
    \hline
    [3]_5 & [3]_5 & [1]_5 & [4]_5 & [2]_5 \\
    \hline
    [4]_5 & [4]_5 & [3]_5 & [2]_5 & [1]_5 \\
    \hline
\end{array}
\]
~\\
$(\Z/{12\Z})^* = \{ [1]_{12}, [5]_{12}, [7]_{12}, [11]_{12} \}$, and its
multiplication table is given below:
\[
\begin{array}{c || c  c  c  c |}
              & [1]_{12}  & [5]_{12}  & [7]_{12}  & [11]_{12} \\
    \hline \hline
    [1]_{12}  & [1]_{12}  & [5]_{12}  & [7]_{12}  & [11]_{12} \\
    \hline
    [5]_{12}  & [5]_{12}  & [1]_{12}  & [11]_{12} & [7]_{12} \\
    \hline
    [7]_{12}  & [7]_{12}  & [11]_{12} & [1]_{12}  & [5]_{12} \\
    \hline
    [11]_{12} & [11]_{12} & [7]_{12}  & [5]_{12}  & [1]_{12} \\
    \hline
\end{array}
\]

It is easy to see all the elements of $(\Z/{12\Z})^*$ have order $2$, whereas
at least one element ($[2]_5, [3]_5$, to be precise) in $(\Z/{5\Z})^*$) have
order greater than $2$. So, no reordering of elements in $(\Z/{12\Z})^*$ will
make them match the elements in $(\Z/{5\Z})^*$.
\end{xca}


\subsection{Topological continuity}\label{subsec:topological_continuity}

\begin{definition}\label{def:local_continuity}
  We say that the function \( f: X \to Y \) between topological spaces is \term{continuous} at the point \( x_0 \in X \) if \( f(x_0) \) is a limit \hyperref[def:local_convergence]{point} of \( f \) at \( x_0 \).

  If limit point is unique (e.g. in \hyperref[def:separation_axioms/T2]{Hausdorff spaces}), this condition can be formulated by \enquote{interchanging} \( \lim \) and \( f \) as follows:
  \begin{equation*}
    f(x_0) = f\left( \lim_{x \to x_0} x \right) = \lim_{x \to x_0} f(x).
  \end{equation*}
\end{definition}

\begin{definition}\label{def:global_continuity}
  We say that the function \( f: X \to Y \) between topological spaces is \term{everywhere continuous} or simply \term{continuous} if and of the following conditions hold:
  \begin{thmenum}
    \thmitem{def:global_continuity/limits} \( f \) is continuous at every point of \( X \) in the sense of \fullref{def:local_continuity}.
    \thmitem{def:global_continuity/open} For every open set \( V \in T \), the \hyperref[thm:def:function/preimage]{preimage} \( f^{-1}(V) \) is open.
    \thmitem{def:global_continuity/closed} For every closed set \( F \in F_{\mscrT_Y} \), the preimage \( f^{-1}(F) \) is closed.
    \thmitem{def:global_continuity/base} There exists a \hyperref[def:topological_base]{base} \( \mscrB_{\mscrT_Y} \subseteq T_Y \), such that for every \( V \in B_{\mscrT_Y} \), the preimage \( f^{-1}(V) \) is open.
    \thmitem{def:global_continuity/subbase} There exists a \hyperref[def:topological_subbase]{subbase} \( P_{\mscrT_Y} \subseteq T_Y \), such that for every \( V \in P_{\mscrT_Y} \), the preimage \( f^{-1}(V) \) is open.
    \thmitem{def:global_continuity/closure} For every set \( A \subseteq X \), \( f(\cl(A)) \subseteq \cl(f(A)) \).
  \end{thmenum}

  We denote the set of all continuous functions from \( X \) to \( Y \) by \( C(X, Y) \).
\end{definition}
\begin{proof}
  \ImplicationSubProof{def:global_continuity/limits}{def:global_continuity/open} Follows from \fullref{def:local_convergence/neighborhoods}.
  \ImplicationSubProof{def:global_continuity/open}{def:global_continuity/closed} If \( F \in F_{\mscrT_Y} \) is a closed set, \( Y \setminus F \) is open, therefore \( f^{-1}(Y \setminus F) = X \setminus f^{-1}(F) \) is also open. Hence, \( f^{-1}(F) \) is closed.
  \ImplicationSubProof{def:global_continuity/open}{def:global_continuity/base} \( \mscrT \) is a base of itself.
  \ImplicationSubProof{def:global_continuity/base}{def:global_continuity/subbase} Every base is also a subbase.
  \ImplicationSubProof{def:global_continuity/subbase}{def:global_continuity/limits} Follows from the equivalences in \fullref{def:local_convergence}.
  \ImplicationSubProof{def:global_continuity/closed}{def:global_continuity/closure} Note that
  \begin{equation*}
    A
    \reloset {\ref{thm:function_image_preimage_composition/preimage_of_image}} \subseteq
    f^{-1}(f(A))
    \reloset {\ref{thm:def:function_preimage/monotonicity}} \subseteq
    f^{-1}(\cl(f(A))).
  \end{equation*}

  Apply \( f \circ \cl \) to the above chain of inclusions to obtain
  \begin{equation*}
    f(\cl(A))
    \subseteq
    f(\underbrace{\cl}_{\ref{def:global_continuity/closed}}(f^{-1}(\cl(f(A)))))
    \reloset {\ref{thm:function_image_preimage_composition/image_of_preimage}} \subseteq
    \cl(f(A)),
  \end{equation*}
  which proves the implication.

  \ImplicationSubProof{def:global_continuity/closure}{def:global_continuity/closed} Fix a closed set \( F \subseteq Y \). Then
  \begin{equation}\label{def:global_continuity/closure_implies_closed_right}
    f(\cl(f^{-1}(F)))
    \reloset {\ref{def:global_continuity/closure}} \subseteq
    \cl(f(f^{-1}(F)))
    \reloset {\ref{thm:function_image_preimage_composition/image_of_preimage}} \subseteq
    \cl(F)
    =
    F.
  \end{equation}

  Since \( \cl \) is monotone, we have
  \begin{equation}\label{def:global_continuity/closure_implies_closed_left}
    f(\cl(f^{-1}(F)))
    \supseteq
    f(f^{-1}(F))
    \reloset {\ref{thm:function_image_preimage_composition/preimage_of_image}} \supseteq
    F.
  \end{equation}

  From \eqref{def:global_continuity/closure_implies_closed_right} and \eqref{def:global_continuity/closure_implies_closed_left} it follows that
  \begin{equation*}
    F = f(\cl(f^{-1}(F))).
  \end{equation*}

  By taking the preimage, we obtain
  \begin{equation*}
    f^{-1}(F)
    =
    f^{-1}(f(\cl(f^{-1}(F))))
    \reloset {\ref{thm:function_image_preimage_composition/image_of_preimage}} \supseteq
    \cl(f^{-1}(F)).
  \end{equation*}

  Therefore, \( f^{-1}(F) \) is closed.
\end{proof}

\begin{definition}\label{def:homeomorphism}
  We say that the continuous function \( f: X \to Y \) is \term{open} (resp. \term{closed}), if the image \( f(U) \) of an open (resp. closed) in \( \mscrT_X \) set is open (resp. closed) in \( \mscrT_Y \).

  If \( f \) is an open bijection, we say that \( f \) is a \term{homeomorphism}. If \( f \) is only an open injection, we say that \( f \) is a \term{homeomorphic embedding}.
\end{definition}

\begin{definition}\label{def:parametric_curve}
  Let \( I \) be an interval (of any type) in \( \BbbR \) with endpoints \( a < b \), not necessarily finite. Depending on the use case, we define a \term{parametric curve} on \( I \) by any of the non-equivalent definitions

  \begin{thmenum}
    \thmitem{def:parametric_curve/function} A continuous function \( \gamma: I \to X \) is called a parametric curve.

    \thmitem{def:parametric_curve/image} The image \( \img(\gamma) \) of a parametric curve \( \gamma \) is also called a parametric curve.

    \thmitem{def:parametric_curve/equivalence_class} The equivalence class of all continuous functions from \( I \) to \( X \) with
    \begin{equation*}
      \gamma \cong \beta \iff \img(\gamma) = \img(\beta) \text{ and the endpoints of } \gamma \text{ and } \beta \text{ coincide}
    \end{equation*}
    is also called a parametric curve.
  \end{thmenum}

  The points \( \gamma(a) \) and \( \gamma(b) \) are called the \term{endpoints} of the curve, \( \gamma(a) \) is the \term{start} and \( \gamma(b) \) is the \term{end}. We say that \( \gamma \) \term{connects} \( a \) and \( b \).

  Parametric curves on \( I = [0, 1] \) are also called \term{paths}.

  We define some fundamental types of curves:
  \begin{thmenum}
    \thmitem{def:parametric_curve/closed} The curve \( \gamma \) is called \term{closed} if its endpoints coincide, i.e. \( \gamma(a) = \gamma(b) \).

    \thmitem{def:parametric_curve/simple} The curve \( \gamma \) is called \term{simple} if the function \( \gamma: I \to Y \) is injective with the possible exception of the endpoints (in which case we speak of \term{simple closed curves}.
  \end{thmenum}

  If \( \gamma: I \to X \) is a parametric curve, related curves are:
  \begin{thmenum}
    \thmitem{def:parametric_curve/function_graph}\mcite[def. 1.20]{ИвановТужилин2017}The \hyperref[def:multi_valued_function/graph]{graph} \( \gph(\gamma) \) of \( \gamma \) is a the image of the curve \( \overline{\gamma}(t, x) \coloneqq (t, \gamma(x)) \) in the topological space \( I \times X \).

    \thmitem{def:parametric_curve/implicit}\mcite[def. 1.24]{ИвановТужилин2017}If \( M \) is a subset of \( X \) and if there exists a curve \( \gamma: I \to X \) such that \( \imag(\gamma) = M \), we call \( M \) an \term{implicit parametric curve}.
  \end{thmenum}
\end{definition}

\begin{definition}\label{def:parametric_hypersurface}
  In analogy to \fullref{def:parametric_curve} (and with the caveats of \fullref{def:parametric_curve}), we define \term{parametric hypersurfaces} as follows:

  Let \( \xi \) is a potentially infinite cardinal number, let \( \card \mscrK = \xi \) and let \( \{ I_\alpha \}_{\alpha \in \mscrK} \) be a family of intervals in \( \BbbR \). We define a parametric hypersurface to be a continuous image from the \hyperref[def:topological_product]{product space} \( \prod_{\alpha \in \mscrK} I_\alpha \) to \( Y \).

  We call \( \xi \) the \term{dimension} of the hypersurface.
\end{definition}

\begin{definition}\label{def:fundamental_groupoid}
  \todo{Define fundamental groupoids}.
\end{definition}


\chapter{Drawing the Planets and Orbits}
\section{Planets}
Drawing the planets is generally very simple: at first we create a sphere using the {\em sphere} function which gives us the coordinates {\em x}, {\em y} and {\em z} of each corner of the sphere while taking the  resolution as the argument. The resolution defines how smooth the surface of the sphere is. For example: a resolution of 50 generates a spheres composed of 50 equally wide rings with 50 equally big surfaces. Following images shows how the spheres react to different resolutions:

\begin{center}
\includegraphics[width=0.45\textwidth]{imgs/drawing_planets_orbits/spheres_res4.jpg}
\includegraphics[width=0.45\textwidth]{imgs/drawing_planets_orbits/spheres_res5.jpg}\\
\textit{Left: a sphere with resolution 4 (top and side view); Right: a sphere with resolution 5 (top and side view)}
\end{center}

We then manipulate the position values to scale and move the sphere as needed. We do this by multiplying the position matrices with a factor and then adding a constant to move it to the correct position.\\

Next we load the textures from an image using various \matlab{} functions: first we read the image of a map of the planet using {\em imread}. Then we convert the data to doubles using {\em im2double}. Next we use {\em imresize} to adjust the image the size of the planet. We then only have to flip the data upside down to correctly display the texture. This is done using the {\em flipud} function. Further details for these function can be found in the help pages of each function.\\

Last but no least we create a surface object, or the 'planet', using the {\em surf} function by passing the position matrices and the textures. We can then use this object to move and rotated the planet.

Following {\em MatLab} code draws a simple sphere with a texture as described above:
\begin{framed}\begin{verbatim}
[x,y,z] = sphere(resolution);
x = x*scale(1) + pos(1); y = y*scale(2) + pos(2); z = z*scale(3) + pos(3);

texture = flipud(imresize(im2double(imread(imgFile)),size(x)));
planet = surf(x, y, z, texture,'EdgeColor', 'none');
\end{verbatim}\end{framed}
{\em pos} and {\em scale} are vectors containing the coordinates of the center of the planet and factors for stretching the planet in {\em x}, {\em y} and {\em z} directions. {\em imgFile} is a string containing the path to the textures of the planet and {\em resolution} is an integer for the resolution of the sphere as described above.

\subsection{Rings}
Some planets, in our case only saturn, have rings of water ice and rocky material circling them. To respect the thickness of such rings and for simplicity we created them using a torus and squished it to make it thiner. A torus is described as follows:

\begin{align*}
x(\theta, \varphi) = (R + r*\cos(\theta) * \cos(\varphi)\\
y(\theta, \varphi) = (R + r*\cos(\theta) * \sin(\varphi)\\
x(\theta, \varphi) = r*\sin(\varphi)
\end{align*}

$\theta$, $\varphi$ are angles which make a full circle, so that their values start and end at the same point,\\
R is the distance from the center of the tube to the center of the torus,\\
r is the radius of the tube.

In \matlab{} this looks some what like this:
\begin{framed}\begin{verbatim}
phi = linspace(0,2*pi,resolution)';
alpha = linspace(0,2*pi,resolution*10)';

tmp = [R + r*cos(phi), r*sin(phi)];
x = cos(alpha)*tmp(:,1)';
y = sin(alpha)*tmp(:,1)';
z = tmp(:,2)';
z = z(ones(1,length(alpha)),:);
\end{verbatim}\end{framed}
The rest for drawing the ring is the same as for drawing the planets: we move and scale the ring using vectors containing the position of the center and factors for each direction and then create a surface object with the textures of the ring. Only the textures are slightly different compared to the planets. While the planets use a map, the ring has to be only a portion of the ring mirrored along the inner {\em 'edge'} of the ring.

\section{Orbits}
We decided to use circular orbits for our modle, instead of simulating them and calculating the trajectory. Thus drawing the trajectory of each planet was made a lot easier.  The function {\em getPlanetOrbit} draws a simple circle in a 3 dimensional room. The function has the following attributes:
\begin{itemize}
  \item Center: stands for the middle of the circle
  \item Normal: stands for direction that the circle is tilted
  \item Radius: radius of the circle
  \item Color: color of the circle \ldots
\end{itemize}

\begin{framed}\begin{verbatim}
theta = 0:0.01:(2*pi+0.01);
v = null(normal);
points = repmat(center',1,size(theta,2))+radius*(v(:,1)*cos(theta)+v(:,2)*sin(theta));
orbit = plot3(points(1,:),points(2,:),points(3,:),'Color',color);
\end{verbatim}\end{framed}

% ================================
\pagebreak
\section{Key Values for the Modle}
As said before, we tryed to use realistic values and scales for the modle. Listed in the following two chapters are the values we ended up using. We gathered the data from: \hyperref[space-facts.com]{http://space-facts.com/}

\subsection{Distances}
\begin{center}
    \begin{tabular}{| l | l | l | l |}
    \hline
    Planet & Distance [km] & Distance [AU] & Scale Factor \\ \hline
    Mercury & 57'909'227 & 0.39 & 0.39 \\ \hline
    Venus & 108'209'475 & 0.73 & 0.73 \\ \hline
    Earth & 149'598'262 & 1 & 1 \\ \hline
    Moon & 384'400 & 0.0025 & 0.0025 \\ \hline
    Mars & 227'943'824 & 1.38 & 1.38 \\ \hline
    Jupiter & 778'340'821 & 5.20 & 5.20 \\ \hline
    Saturn & 1'426'666'422 & 9.58 & 9.58 \\ \hline
    Uranus & 2'870'658'186 & 19.22 & 19.22 \\ \hline
    Neptune & 4'498'396'441 & 30.10 & 30.10 \\
    \hline
    \end{tabular}\\
    \textit{The distances are all the average distance from the sun.\\
    Except for the moon, whose distance is the average distance from earth.}
\end{center}

\subsection{Sizes}
\begin{center}
    \begin{tabular}{| l | l | l |}
    \hline
    Planet & Diameter [km] & Scale Factor \\ \hline
    Sun & 1'392'684 & 109.18 \\ \hline
    Mercury & 4'879 & 0.38 \\ \hline
    Venus & 12'104 & 0.95 \\ \hline
    Earth & 12'756 & 1 \\ \hline
    Moon & 3'475 & 0.27 \\ \hline
    Mars & 6'805 & 0.53 \\ \hline
    Jupiter & 142'984 & 11.21 \\ \hline
    Saturn & 120'536 & 9.45 \\ \hline
    Uranus & 51'118 & 4.01 \\ \hline
    Neptune & 49'528 & 3.88 \\
    \hline
    \end{tabular}\\
    \textit{The diameters used are the equatorial diameters of the planets.\\
    The scale factor used is log10 of the value in this table.}
\end{center}

\subsection{Speed}
\begin{center}
    \begin{tabular}{| l | l | l |}
    \hline
    Planet & Earth days / year & Scale Factor \\ \hline
    Mercury & 87.97 & 4.1521 \\ \hline
    Venus & 224.7 & 1.6255 \\ \hline
    Earth & 365.26 & 1 \\ \hline
    Moon & 27.3 & 13.3795 \\ \hline
    Mars & 686.98 & 0.5317 \\ \hline
    Jupiter & 4332.82 & 0.0843 \\ \hline
    Saturn & 10755.7 & 0.034 \\ \hline
    Uranus & 30687.15 & 0.0119 \\ \hline
    Neptune & 60190.03 & 0.0061 \\
    \hline
    \end{tabular}\\
    \textit{The scale factor is used for the orbit speed.\\
    Therefor less days mean faster orbit speed.}
\end{center}


\section{Relaxed and Regularized Transport}

In the previous section, we introduced the Monge-Kantorovich formulation for the computation of the OT between two distributions, as the minimization of the energy~\eqref{eqMK}. In this section, we modify this energy in order to obtain a regular OT mapping, which is important for applications such as color transfer.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Relaxed Transport}
\label{subsec-relaxed-transport}

Section~\ref{sec-appli-color} tackles the color transfer problem, where, as in many applications in imaging, strict mass conservation should be avoided.  As a consequence, it is not desirable to impose a one-to-one mapping between the points in $X$ and $Y$.

The relaxation we propose allows each point of $X$ to be transported to multiple points of $Y$ and vice versa. This corresponds to imposing the constraints
\eq{
	k_X \U \leq \Sig \U \leq K_X \U
	\qandq
	k_Y \U \leq \Sigma^* \U \leq K_Y \U
}
on the matrix $\Sig$, where
$\kappa=(k_X,K_X,k_Y,K_Y) \in (\RR^+)^4$
are the parameters of the method.
% Note that these restrictions on $\Sig$ do not rule out the option of decreasing the overall mass.
To impose the total amount of mass $M$ transported between the densities, we further impose the constraint $\U^* \Sig\U=M$, where $M>0$ is a parameter.
The initial OT problem~\eqref{eqMK} now becomes:
\eql{\label{eq-relax-map}
	\umin{\Sigma \in \Matr_\kappa} \dotp{C_{X,Y}}{\Sig}
}
\eq{
	\qwhereq
	\Matr_\kappa = \enscond{\Sig \in [0,1]^{N \times N}}{
		\begin{array}{ll}
			k_X \U \leq \Sigma \U \leq K_X \U, \\
			k_Y \U \leq \Sigma^* \U \leq K_Y \U,
		\end{array} \:
		\U^* \Sig\U=M
		 }.
}
To ensure that $\Matr_\kappa$ is non empty, we impose that
\eq{
 	\max(k_X,k_Y) \leq \frac{M}{N} \leq \min(K_X,K_Y)
}
For the application to the color manipulations considered in this paper, we set once and for all this parameter to $M=N$.

%\todo{Either provide a mathematical statement with a proof, or remove this sentence. }
Note that if $\min(K_X,K_Y) \geq N$, there is no restriction on the number of connections of each element of $X$ or $Y$, then the optimal solution increases (always under the constraints $\U^*\Sig\U=M$) the weight given to the connection between the closest points in $X$ to the closest points in $Y$, that is to say, the minima $(C_{X,Y})_{i,j}$ are assigned the maximum possible weight, see Fig.~\ref{im:relaxationk} for an example.

%the solution of \eqref{eq-relax-map} is the nearest neighbor assignment.

Problem~\eqref{eq-relax-map} is  a convex linear program, which can be solved using standard linear programming algorithms.

\paragraph{Relaxed OT map} Optimal matrices $\Sig$ minimizing~\eqref{eq-relax-map} are in general non binary and furthermore their non zero entries do not define one-to-one maps between the points of $X$ and $Y$. It is however possible to define a map $T$ from $X$ to $Y$ by mapping each point $X_i$ to a weighted barycenter of its neighbors in $Y$ as defined by $\Sig$. This corresponds to defining
\eq{\label{eqT}
T(X_i) = \frac{\sum_{j=1}^N \Sig_{i,j} Y_j }{\sum_{j=1}^N \Sig_{i,j}}\\
} which in vectorial form can be expressed as $T(X_i)= Z_i $, where $Z=(\diag(\Sig \U))^{-1} \Sig Y$, and where the operator $\diag(v)$ creates a diagonal matrix in $\RR^{N \times N}$ with the vector $v \in \RR^N$ on the diagonal.
To insure that the map is well defined, we impose that $k_X > 0$.  Note that it is possible to define a map from $Y$ to $X$ by replacing $\Sig$ by $\Sig^*$ in the previous formula and exchanging the roles of $X$ and $Y$.

The following proposition shows that an optimal $\Sig$ is binary when the parameters $\kappa$ are integers. Such a binary $\Sig$ can be interpreted as a set of pairwise assignments between the points in $X$ and $Y$. Note that this is not true in general when the parameters $\kappa$ are not integers.

\begin{prop}\label{prop}
	For $(k_X,K_X,k_Y,K_Y,M) \in (\NN^*)^5$, there exists a solution  $\tilde{\Sig}$ of~\eqref{eq-relax-map} which is binary, i.e. $\tilde \Sigma \in \{0,1\}^{N \times N}$.
	\if 0
		which is solution of
	\eql{\label{eq-relaxed-transport}
		\umin{\Sig \in \Bb_{\kappa}} \dotp{C_{X,Y}}{\Sig},
	}
	\eq{
		\qwhereq
		\Bb_{\kappa}= \enscond{\Sig \in \{0,1\}^{N \times N}}{
		\begin{array}{ll}
			k_X \U \leq \Sigma \U \leq K_X \U, \\
			k_Y \U \leq \Sigma^* \U \leq K_Y \U,
		\end{array} \:
		\U^* \Sig\U=M
		}
	}
	that is to say, $\tilde{\Sig}$ is a binary matrix.
	\fi
\end{prop}
\begin{proof}
	One can write
	$\Matr_\kappa = \enscond{ \Sig \in \RR^{N \times N} }{ \Aa(\Sig) \leq b_\kappa }$
	where $\Aa$ is the linear mapping
	$\Aa(\Sig)=(-\Sig,\Sig \U,-\Sig \U,\Sig^* \U,-\Sig^* \U,\U\Sig\U,-\U\Sig\U)$, where $\Sig^* \U, \Sig\U \in \RR^{N}$ and $\U\Sig\U \in \RR$, 	and $b_\kappa = (0_{N,N}, K_X\U, -k_X\U, K_Y\U, -k_Y\U, M, -M)$. A standard result shows that $\Aa$ is a totally unimodular matrix~\cite{schrijver-book}. For any $(k_X,K_X,k_Y,K_Y,M) \in (\NN^*)^5$, the vector $b_\kappa$ has integer coefficients, and thus the polytope $\Matr_\kappa$ has integer vertices. Since there is always a solution of the linear program~\eqref{eq-relax-map} which is a vertex of $\Matr_\kappa$, it has coefficients in $\{0,1\}$.
\end{proof}

\begin{figure}
\centering
\begin{tabular}{@{}|@{}c@{}|@{}c@{}|@{}c@{}|}
\hline
\includegraphics[width=.33\linewidth]{cluster_matching_l0kx1KX1ky1KY1_nn4.eps} &
\includegraphics[width=.33\linewidth]{cluster_matching_l0kx1KX1ky0KY2_nn4.eps} &
\includegraphics[width=.33\linewidth]{cluster_matching_l0kx1KX1ky01KY10_nn4.eps} \\
$\kappa=(1,1,1,1)$ & $\kappa=(1,1,0,2)$ & $\kappa=(1,1,0.1,10)$ \\\hline
\includegraphics[width=.33\linewidth]{cluster_matching_l0kx1KX1ky01KY15_nn4.eps} &
\includegraphics[width=.33\linewidth]{cluster_matching_l0kx0KX2ky1KY1_nn4.eps} &
\includegraphics[width=.33\linewidth]{cluster_matching_l0kx01KX10ky01KY10_nn4.eps}  \\
  $\kappa=(1,1,0.1,1.5)$ & $\kappa=(0,2,1,1)$  & $\kappa=(0.1,10,0.1,10)$ \\\hline
\end{tabular}
\caption{\label{im:relaxationk}Relaxed transport computed between $X$ (blue dots) and $Y$ (red dots) for different values of $\kappa$.
Note that $\kappa=(1,1,1,1)$ corresponds to classical OT. The mappings $\Sigma_{i,j}$ that relate $X_i$ and $Y_j$ are plotted as line segments connecting which are dashed if $\Sigma_{i,j} \in ]0.1,1[$  and solid if $\Sigma_{i,j}=1$.  }
\end{figure}

%%%
\paragraph{Numerical Illustrations}

In Fig.~\ref{im:relaxationk}, we show a simple example to illustrate the properties of the method proposed so far. Given a set of points $X$ (in blue) and $Y$ (in red), we compute the optimal $\Sig$ solving~\eqref{eq-relax-map} for different values of $\kappa$. For each values of $\kappa$, we draw a line between $X_i$ and $Y_j$ if the value of the associated optimal $\Sigma_{i,j} > 0.1$, solid if $\Sigma_{i,j}=1$, and dashed otherwise.

As we prove in the Proposition~\ref{prop}, for non integer values of $K_X,K_Y$, the mappings $\Sigma_{i,j}$ are in $[0,1]$ while for integer values, $\Sigma_{i,j} \in \{0,1\}$. Note that as we increase the values of $K_X,K_Y$ (Fig.~\ref{im:relaxationk}, right), the points in $X$ tend to be mapped to the closer points in $Y$.

%\begin{figure}
%\begin{tabular}{cccc}
%\includegraphics[height=2.5cm]{./images/k1_l0.png} &
%\includegraphics[height=2.5cm]{./images/k2_l0.png} &
%\includegraphics[height=2.5cm]{./images/k25_l0.png}&
%\includegraphics[height=2.5cm]{./images/k10_l0.png} \\
% (a) & (b) & (c) & (d)
% \end{tabular}
%\caption{Relaxed transport computed between $X$ (blue dots) and $Y$ (red dots) with the parameters \textbf{(a)} $k=1$ (classical OT) \textbf{(b)} $k=2$ \textbf{(c)} $k=2.5$ \textbf{(d)} $k=10$. The color of the line between $X_i$ and $Y_j$ indicates the value of the mapping $\Sigma_{i,j}$. }
%\label{im:relaxationk}
%\end{figure}


From Equation~\ref{eq:theta_ls} we can begin to derive the recursive least
squares estimator. It is worth noting at the beginning of this derivation that
we have implicitly assumed that our data is already centered; in other words,
the relationship between $\varphi_t$ and $y_t$ has no offset term, and so our
model $\hat\Theta_{LS}$ will always predict an output vector of all zeros for
an input vector of all zeros. This is a fine assumption when all of the data
has been collected ahead of time, but breaks down when we want to do recursive
least squares because we cannot estimate the means of our inputs and outputs
ahead of time. Since it is easier to derive the recursive least squares
estimator in the centered case than in the uncentered case, we tackle this
limited version first.

\subsection{Breaking Up the Normal Equation}
We begin by writing out the normal equation as two sums multiplied together
\begin{align}
  \hat\Theta_{LS}(T) &= (X_T^\top X_T)^{-1} X_T^\top Y_T \\
                  &= \left[\sum_{t=1}^T \varphi_t^\top \varphi_t\right]^{-1} \left[\sum_{t=1}^T\varphi_t^\top y_t \right] \label{eq:two_sums}
\end{align}
Let us define the inverse sample covariance matrix $P_T$ to be the left-hand
term in Equation~\ref{eq:two_sums}, so that we then have
\begin{align}
  \label{eq:sample_covar_split}
  P_T^{-1} &= \sum_{t=1}^T \varphi_t^\top \varphi_t \\
           &= P_{T - 1}^{-1} + \varphi_T^\top \varphi_T \label{eq:p_inv_update}\\
  \implies P_{T - 1}^{-1} &= P_T^{-1} - \varphi_T^\top \varphi_T \label{eq:sub_covar}
\end{align}
Similarly, we can break up the right-hand term in Equation~\ref{eq:two_sums}:
\begin{equation}
  \sum_{t=1}^T\varphi_t^\top y_t = \sum_{t=1}^{T-1} \varphi_t^\top y_t + \varphi_T^\top y_T
\end{equation}

\subsection{Deriving the $\hat\Theta$ Update}
Our normal equation is now
\begin{equation}
  \hat\Theta_{LS} = P_T \cdot \left[\sum_{t=1}^{T-1} \varphi_t^\top y_t + \varphi_T^\top y_T
\right]
\end{equation}
Using the definition of $\hat\Theta_{LS}(T - 1)$, we get
\begin{equation}
  \hat\Theta_{LS}(T) = P_T \cdot \left[P_{T-1}^{-1} \hat\Theta_{LS}(T-1) + \varphi_T^\top y_T\right]
\end{equation}
Substituting in Equation~\ref{eq:sub_covar}:
\begin{align}
  \hat\Theta_{LS}(T) &= P_T \cdot \left[(P_{T}^{-1} - \varphi_T^\top \varphi_T) \hat\Theta_{LS}(T-1) + \varphi_T^\top y_T\right] \\
                     &= \hat\Theta_{LS}(T - 1) - P_T \varphi_T^\top \varphi_T \hat\Theta_{LS}(T-1) + P_T \varphi_T^\top y_T \\
                     &= \hat\Theta_{LS}(T - 1) + P_T \varphi_T^\top \left[y_T - \varphi_T\hat\Theta_{LS}(T-1)\right] \label{eq:centered_theta_update}
\end{align}
Note that the last term in Equation~\ref{eq:centered_theta_update} ($y_T -
\varphi_T\hat\Theta_{LS}(T-1)$) is the prediction error of our model at
timestep $T-1$ on the new datum, so the new estimate of $\Theta$ that we get is
the old estimate plus the prediction error on a new datum filtered by $P_T
\varphi_T^\top$. Intuitively, this represents a reweighting of the prediction
error using our existing estimate of the sample covariance, which effectively
rescales the update to $\Theta$ to take into account the scales of the
coordinates of the features.

\subsection{Deriving the $P_T$ Update}
Though Equation~\ref{eq:p_inv_update} gives us a way to update $P_T^{-1}$
easily with each new datum, to recover $P_T$ and update
$\hat\theta_{LS}$ we would need to invert an $n\times n$ matrix at on each time
step. This is not only computationally expensive for all but small values of
$n$, it also introduces the risk of running into floating-point errors if
$P_T^{-1}$ ever becomes ill-conditioned\footnote{These sorts of issues can also
be dealt with using any number of techniques from numerical linear
algebra~\cite{trefethen1997numerical}}.

We can get around these issues by doing away with $P_T^{-1}$ all together and
deriving a direct update for $P_T$. We do this with the Woodbury matrix
identity~\cite{woodbury1950inverting}, also known as the ``matrix inversion
lemma.'' This result tells us that for matrices $A, U, C, V$ such that $UCV$ has rank $k$, the inverse of the rank-$k$ update is given by:
\begin{equation}
  \label{eq:woodbury}
  \left(A + UCV\right)^{-1} = A^{-1} - A^{-1}U\left(C^{-1} + VA^{-1}U\right)^{-1}VA^{-1}
\end{equation}
In our case, since we are doing a rank-1 update $\varphi_T^\top \varphi_T$ to
$P_{T-1}^{-1}$, this lemma gives us that
\begin{equation}
  \label{eq:centered_p_update}
  P_T = \left(P_{T-1}^{-1} + \varphi_T^\top \varphi_T\right)^{-1} = P_{T - 1} - \frac{P_{T-1}\varphi_T^\top \varphi_T P_{T-1}}{1 + \varphi_T P_{T-1} \varphi_T^\top}
\end{equation}
And just like that, we're done! Equations~\ref{eq:centered_theta_update}
and~\ref{eq:centered_p_update} give us the update equations that define the
recursive least squares algorithm. At each timestep $t$, we simply need to:
\begin{enumerate}
\begin{singlespace}
  \item Record $\varphi_t$ and $y_t$ from our datastream $F(t)$.
  \item Calculate $P_t$ from $P_{t-1}$ and $\varphi_t$ using Equation~\ref{eq:centered_p_update}.
  \item Calculate $\hat\Theta_{LS}(t)$ from $\hat\Theta_{LS}(t-1)$, $\varphi_t$, $y_t$, and $P_{t}$ using Equation~\ref{eq:centered_theta_update}.
\end{singlespace}
\end{enumerate}


%!TEX root = 497Notes-Temple.tex
\newpage
\section{Initialization for deep neural networks}
To build a machine learning algorithm, we need to define an architecture (e.g. Logistic regression, Support Vector Machine, Neural Network) and train it to learn parameters. The training of neural network is actually the iteration of the above architecture. Then, the initial guess of the parameters is important in avoiding gradient vanishing or blowup.
The initialization step deals with the initial guess of parameters, and can be critical to the model's ultimate performance.

\subsection{Xavier's Initialization with $\sigma  = id$}
The goal of Xavier initialization~\cite{glorot2010understanding} is to initialize the deep neural network to avoid gradient vanishing or blowup when the input is white noise.

Recall the DNN models in Section \ref{sec:DNN}
\begin{equation}
\begin{cases}
f^1(x) &= W^1 x + b^1 \\
f^{\ell}(x) &= W^\ell \sigma(f^{\ell-1}(x)) + b^\ell \quad \ell = 2:L \\
f(x) &= f^{L} \\
\end{cases},
\end{equation}
with $x \in \mathbb{R}^{n_0}$ and $f^{\ell} \in \mathbb{R}^{n_\ell}$. More precisely, we have
\begin{equation}\label{key}
W^\ell \in \mathbb{R}^{n_{\ell} \times n_{\ell-1}}.
\end{equation}
We make the basic assumptions below:
\begin{itemize}
%% \item The input $x$ is a mean $0$ random variable with identity covariance, i.e. $\mathbb{E}(x) = 0$, $\mathbb{E}(x_ix_j) = 0$ if $i\neq j$, and $\mathbb{E}(x_i^2) = 1$
% \item \blue{The input $x$ is a mean $0$ random vector with the same variance for each component, i.e. $\mathbb{E}[[x]_i] = 0$ and $\mathbb{E}[[x]_i^2] (\mathbb{V} [[x]_i]) =$ $\mathbb{E}[[x]_j^2] (\mathbb{V}[[x]_j])$. }
% (Here we notice that, after data normalization as discussed before this assumption holds directly.)
 \item The initial weights $W^\ell_{ij}$ are i.i.d symmetric random variables with mean $0$, namely the
 probability distribution of $W^\ell_{ij}$ is even.
 \item The initial bias $b^\ell = 0$.
\end{itemize}
The goal of Xavier's initialization is to ensure that the features $f^\ell$ and gradients do not blow up or vanish. Note that the mean of $f^\ell$ is zero, we only need to choose appropriate initial parameters to bound the variance of $f^\ell$. To this end we have the following lemma.

\begin{lemma}\label{lemm:init}
Under the previous assumptions $f^\ell_i$  is a symmetric random variable with $\mathbb{E}[f^\ell_i] = 0$.
Moreover, we have the following identity
\begin{equation}\label{eq:FWini}
\mathbb{V}[f^{\ell}_i] = \mathbb{E}[(f^{\ell}_i)^2] = \sum_{k}\mathbb{E}[(W^\ell_{ik})^2]\mathbb{E}[\sigma(f^{\ell-1}_k)^2].
\end{equation}
where the subscript $i$ of the notation $f_i^\ell$ represents the $i$-th component of $f^\ell$.
% \begin{equation}\label{key}
%  \mathbb V [f^{L}_i] =  \left(\Pi_{\ell=1}^{L} n_{\ell-1} {\rm Var} [W^\ell_{st}] \right)\mathbb V [x_j].
% \end{equation}
\end{lemma}
%Note that here we don't have that $f^\ell_i$ and $f^\ell_j$ are actually independent, just that they are `linearly' independent.
\begin{proof}
For general activation function $\sigma(x)$, let first prove that
$f^\ell_i$  is a symmetric random variable with $\mathbb{E}[f^\ell_i] = 0$.
For any $\ell$ and $1\le i \le n_\ell$, we have
\begin{equation}
 f^\ell_i = \sum_k W^\ell_{ik}\sigma(f^{\ell-1}_k) + b_i^\ell,
\end{equation}
Taking expectations, noting that $W^\ell_{ik}$ are independent of $f^{\ell-1}_k$ and that $b_i^\ell = 0$, we get
\begin{equation}
 \mathbb{E}[f^\ell_i] = \sum_k \mathbb{E}[W^\ell_{ik}]\mathbb{E}[\sigma(f^{\ell-1}_k)] = 0.
\end{equation}
since $\mathbb{E}[W^\ell_{ik}] = 0$.
Moreover, if the $W_{ij}^\ell$ are symmetric, it is clear that $f^\ell_i$ will be.

% Furthermore, we calculate (note that $b^\ell = 0$)
% \begin{equation}
% \begin{split}
%  \mathbb{E}(f^\ell_if^\ell_j) &= \mathbb{E}\left(\left(\sum_k W^\ell_{ik}\sigma(f^{\ell-1}_k)\right)\left(\sum_l W^\ell_{jl}\sigma(f^{\ell-1}_l)\right)\right) \\ &= \sum_{k,l}\mathbb{E}(W^\ell_{ik}W^\ell_{jl}\sigma(f^{\ell-1}_k)\sigma(f^{\ell-1}_l))
%  \end{split}
% \end{equation}
%
%Since $W^\ell_{ik}$ and $W^\ell_{jl}$ are independent ($i\neq j$), and are both independent of $f^{\ell-1}$, each of the terms above is
%\begin{equation}
% \mathbb{E}(W^\ell_{ik})\mathbb{E}(W^\ell_{jl})\mathbb{E}(\sigma(f^{\ell-1}_k)\sigma(f^{\ell-1}_l)) = 0.
%\end{equation}
Next we calculate the variance of $f^{\ell}_i$, we get
\begin{equation}
\begin{split}
 \mathbb{V}[f^{\ell}_i] = \mathbb{E}[(f^{\ell}_i)^2] &= \mathbb{E}\left[\left(\sum_k W^\ell_{ik}\sigma(f^{\ell-1}_k)\right)\left(\sum_l W^\ell_{il}\sigma(f^{\ell-1}_l)\right)\right]\\ &= \sum_{k,l}\mathbb{E}[W^\ell_{ik}W^\ell_{il}\sigma(f^{\ell-1}_k)\sigma(f^{\ell-1}_l)].
 \end{split}
\end{equation}
In the above sum we will have $W^\ell_{ik}$ and $W^\ell_{il}$ independent unless $k = l$. In this case the term will be $0$. So the only terms which remain are those for which $k=l$ and we get
\begin{equation}\label{eq:FWini}
  \mathbb{E}[(f^{\ell}_i)^2] = \sum_{k}\mathbb{E}[(W^\ell_{ik})^2]\mathbb{E}[\sigma(f^{\ell-1}_k)^2].
\end{equation}

\end{proof}

Now, we assume $\sigma  = id$. This assumption is pretty reasonably since most activation functions in use at the time (such as the hyperbolic tangent) were close to the identity near $0$.

\begin{lemma}\label{th:idnormal}
If $\sigma  = id$,
 \begin{equation}\label{key}
	\mathbb V [f^{L}_i] =  \left(\Pi_{\ell=2}^{L} n_{\ell-1} {\rm Var} [W^\ell_{st}] \right) \left(\mathbb{V}[W^1_{st}]\sum_{j}\mathbb{E}[( x^j)^2] \right),
	\end{equation}
where $x^j$ represents the $j$-th component of data $x$.
\end{lemma}
\begin{proof}
Let us prove it this by induction. For $\ell=1$,
\begin{equation}\label{key}
\mathbb{V}[f^{1}_i] = \sum_{j}\mathbb{E}[(W^1_{ij})^2]\mathbb{E}[( x^j)^2] = {\mathbb{E}[(W^1_{ij})^2]  \left(\sum_{j}\mathbb{E}[( x^j)^2] \right)},
\end{equation}
for any $i = 1,2,\cdots, n_1$. Since we already know that
	\begin{equation}\label{key}
	\mathbb{V}[f^1_i ] = {\mathbb{E}[(W^1_{ij})^2]  \left(\sum_{j}\mathbb{E}[( x^j)^2] \right)} = \mathbb{V}[f^1_k], \quad \forall i,k,
	\end{equation}
	from derivation in $\ell=1$. We will see that for $\ell=2$,
	\begin{equation}\label{key}
	\mathbb{V}[f^2_i ] = n_1 \mathbb{V}[W^2_{ij}]  \mathbb{V}[ f^1_j] = n_1     \mathbb{V}[W^2_{st}] \left(\mathbb{V}[W^1_{st}]\sum_{j}\mathbb{E}[(x^j)^2] \right).
	\end{equation}
By induction,
\begin{equation}\label{key}
	\mathbb V [f^{L}_i] =  \left(\Pi_{\ell=2}^{L} n_{\ell-1} {\rm Var} [W^\ell_{st}] \right) \left(\mathbb{V}[W^1_{st}]\sum_{j}\mathbb{E}[( x^j)^2] \right).
	\end{equation}
\end{proof}
%We will see in the next section that (as first observed by Kaiming He) if $\sigma$ is the ReLU function,
%then we don't need to make this assumption since the left hand side in \eqref{eq:FWini} can be calculated exactly.

According to Theorem \ref{th:idnormal}, one way to make sure the variance $\mathbb{V}[W^\ell_{ik}]$ does not blow up is to set
\begin{equation}\label{varianceW}
\mathbb{V}[W^\ell_{ik}] = \frac{1}{n_{\ell-1}}, \quad \forall \ell \ge 2.
\end{equation}
In this case,
\begin{equation}
\mathbb V [f^{L}_i] = \mathbb V [f^{L-1}_j] = \cdots =  \mathbb V [f^{1}_k] = \mathbb{V}[W^1_{st}]\sum_{j}\mathbb{E}[(x^j)^2].
\end{equation}
Thus, in pure DNN models, it is enough to just control $\displaystyle \sum_{j}\mathbb{E}[(x^j)^2]$.
The choice \eqref{varianceW} comes from the analysis of $\mathbb{V}[W^\ell_{ik}]$. We can also consider the propagation of the gradient $\frac{\partial L(\theta)}{\partial f^\ell}$. A similar analysis suggests the choice
$
\mathbb{V}[W^\ell_{ik}] = \frac{1}{n_{\ell}}.
$
Thus, the {\bf Xavier's initialization} suggests to initialize $W^\ell_{ik}$ with variance as:
\begin{itemize}
	\item To control $\mathbb V [f^{\ell}_i] $:
	\begin{equation}\label{key}
	{\rm Var}[W^\ell_{ik}] = \frac{1}{n_{\ell-1}}.
	\end{equation}
	\item To control $\mathbb{V}[\frac{\partial L(\theta)}{\partial f_i^\ell}]$:
	\begin{equation}\label{key}
	{\rm Var}[W^\ell_{ik}] = \frac{1}{n_{\ell}}.
	\end{equation}
	\item Trade-off to control $\mathbb{V} [\frac{\partial L(\theta)}{\partial W_{ik}^\ell}]$:
	\begin{equation}\label{key}
	{\rm Var}[W^\ell_{ik}] = \frac{2}{n_{\ell-1} + n_\ell}.
	\end{equation}
\end{itemize}
Here we note that, this analysis works for all symmetric type distribution around zero. We often just choose uniform distribution $\mathcal U(-a,a)$ and normal distribution $\mathcal N(0,s^2)$. Since the expectation of $W^{\ell}_{ik}$ is zero and the variance is given above, the final version of Xavier's initialization takes the trade-off type as
\begin{equation}
W^{\ell}_{ik} \sim \mathcal{U}(-\sqrt{\frac{6}{n_\ell+n_{\ell-1}}}, \sqrt{\frac{6}{n_\ell+n_{\ell-1}}}),\quad \text{ or }\quad
W^{\ell}_{ik} \sim \mathcal{N}(0,  {\frac{2}{n_\ell+n_{\ell-1}}}).
\end{equation}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\input{6DL/Init_Backward}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%See the separate file in {``6DL/HandWrittenNotes/InitBackward.pdf''}
%\includepdf[pages=-,pagecommand={}]{HandWrittenNotes/InitBackward.pdf}
%\includepdf[pages=-,pagecommand={}]{497/handwritten_notes/MultidimensionalVariance.pdf}

\subsection{Kaiming's initialization}
In~\cite{he2015delving}, Kaiming He and others extended this analysis to get an \textit{exact} result when the activation function is the  ReLU.

We first have the following lemma for symmetric distribution.
\begin{lemma}
	If $X_i \in \mathbb{R}$ for $i=1:n$ are i.i.d with symmetric probability density function $p(x)$, i.e. $p(x)$ is even.
	Then for any nonzero random vector $Y = (Y_1, Y_2, \cdots, Y_n) \in \mathbb{R}^n$ which is independent with $X_i$,
	the following random variable
	\begin{equation}\label{key}
	Z = \sum_{i=1}^n X_i Y_i,
	\end{equation}
	is also symmetric.
\end{lemma}

\begin{proof}
	Let us denote the joint distribution function for $Y$ as $q(y_1, \cdots, y_n)$. Then
	the joint distribution density function for $(X,Y)$ is
	\begin{equation}\label{key}
	f(x_1, \cdots, x_n,y_1, \cdots, y_n) = p(x_1)p(x_2)\cdots p(x_n)q(y_1, y_2, \cdots, y_n).
	\end{equation}
	Then we have the following probability function
	\begin{equation}\label{key}
	\begin{aligned}
	\mathbb P(Z < z) &= \int_{\sum_{i=1}^n {x_i y_i} < z} f(x_1, \cdots, x_n,y_1, \cdots, y_n)  dxdy \\
	&= \int_{\sum_{i=1}^n {x_i y_i} < z} p(x_1)p(x_2)\cdots p(x_n)q(y_1, y_2, \cdots, y_n)  dxdy \\
	&= \int_{\sum_{i=1}^n {-x_i y_i} > -z} p(x_1)p(x_2)\cdots p(x_n)q(y_1, y_2, \cdots, y_n)  dxdy\\
	&= \int_{\sum_{i=1}^n {-x_i y_i} > -z} p(-x_1)p(-x_2)\cdots p(-x_n)q(y_1, y_2, \cdots, y_n)  dxdy\\
	&= \int_{\sum_{i=1}^n \tilde x_i y_i >-z} p(\tilde x_1)p(\tilde x_2)\cdots p(\tilde x_n)q(y_1, y_2, \cdots, y_n)  d\tilde xdy\\
	&= \mathbb P(Z >- z).
	\end{aligned}
	\end{equation}
\end{proof}

Then state the following result for ReLU function and random variable with
symmetric distribution around $0$.
\begin{lemma}
If $X$ is a random variable on $\mathbb{R}$ with symmetric probability density $p(x)$ around zero, i.e.,
\begin{equation}\label{key}
p(x) = p(-x).
\end{equation}
Then we have $\mathbb{E} X = 0$ and
\begin{equation}\label{key}
\mathbb{E}[[{\rm ReLU}(X)]^2] = \frac{1}{2}{\rm Var}[X].
\end{equation}
\end{lemma}
\begin{proof}
	By definition
	\begin{equation}\label{key}
	\mathbb{E} X = \int_{-\infty}^\infty xp(x)d(x) = 0. \quad (\text{since } p(x) = p(-x)).
	\end{equation}
	Furthermore,
	\begin{equation}\label{key}
	\begin{aligned}
	\mathbb{E}[[{\rm ReLU}(X)]^2] &= \int_{-\infty}^\infty ({\rm ReLU}(x))^2 p(x)  dx \\
	&= \int_{0}^\infty x^2 p(x) dx \\
	&= \frac{1}{2} \int_{-\infty}^\infty (x - \mathbb E [x])^{2} p(x) dx \\
	& = \frac{1}{2} \mathbb E[ [X - \mathbb{E} X]^2] \\
	& = \frac{1}{2}\mathbb{V}[X].
	\end{aligned}
	\end{equation}
\end{proof}

Based on the previous Lemma~\ref{lemm:init}, we know that $f^{\ell-1}_k$ is a symmetric distribution around $0$.
The most important observation in Kaiming's paper~\cite{he2015delving} is that:
\begin{equation*}\label{key}
\mathbb{V}[ f^\ell_i ] = n_{\ell-1}  \mathbb{V}[W^\ell_{ij}] {\mathbb{E}[[\sigma(f^{\ell-1}_j)]^2]} = n_{\ell-1} \mathbb{V}[W^\ell_{ik}] {\frac{1}{2} \mathbb{V}[f^{\ell-1}_k]},
\end{equation*}
{if $\sigma = {\rm ReLU}$}.
Thus, Kaiming's initialization suggests to take:
\begin{equation}\label{key}
\mathbb{V}[W^\ell_{ik}] = \frac{2}{n_{\ell-1}}, , \quad \forall \ell \ge 2.
\end{equation}

For the first layer $\ell=1$, by definition
\begin{equation}\label{key}
f^1 = W^1 x + b^1,
\end{equation}
there is no ReLU, thus it should be $\mathbb{V}[W^1_{ik}] = \frac{1}{d}$.
For simplicity, they still use $\mathbb{V}[W^1_{ik}] = \frac{2}{d}$ in the paper~\cite{he2015delving}. Similarly, an analysis of the propagation of the gradient suggests that we set
\begin{equation}\label{key}
\mathbb{V}[W^\ell_{ik}] = \frac{2}{n_{\ell}}.
\end{equation}
However, in paper~\cite{he2015delving} authors did not suggest to take the trade-off version, they just chose
\begin{equation}\label{key}
\mathbb{V}[W^\ell_{ik}] = \frac{2}{n_{\ell-1}},
\end{equation} as default.
Thus, the final version of Kaiming's initialization takes the forward type as
\begin{equation}
W^{\ell}_{ik} \sim \mathcal{U}(-\sqrt{\frac{6}{n_{\ell-1}}}, \sqrt{\frac{6}{n_{\ell-1}}}),
\quad\text{ or }\quad
W^{\ell}_{ik} \sim \mathcal{N}(0,  {\frac{2}{n_{\ell-1}}}).
\end{equation}

%And another difference is that Xavier use the uniform distribution but He use the Gaussian distribution.
%More precisely,
%However, in Pytorch implementation, the uniform distribution is also applied.

\section{Data normalization in CNNs}
For CNN models, following the analysis above we have the next iterative scheme in CNNs
\begin{equation}\label{key}
f^{\ell,\nu} = K^{\ell,\nu} \ast \sigma (f^{\ell,\nu-1}),
\end{equation}
where the previous step $f^{\ell,\nu-1} \in \mathbb{R}^{c_\ell\times n_\ell \times m_\ell }$, the current step $f^{\ell,\nu} \in \mathbb{R}^{h_\ell\times n_\ell \times m_\ell}$ and $K \in \mathbb{R}^{(2k+1) \times (2k+1) \times h_\ell \times c_\ell}$.
Thus we have
\begin{equation}\label{key}
f^{\ell,\nu}_{h;p,q} = \sum_{c=1}^{c_\ell}\sum_{s,t=-k}^k K^{\ell,\nu}_{h,c;s,t} \ast \sigma (f^{\ell,\nu-1}_{c;p+s,q+t}).
\end{equation}
Take variance on both sides, we will get
\begin{equation}\label{key}
\mathbb{V} [f^{\ell,\nu}_{h;p,q}] = c_\ell (2k+1)^2 \mathbb{V}[K^{\ell,\nu}_{h,o;s,t}] \mathbb{E}[(f^{\ell,\nu-1}_{o;p+s,q+t})^2],
\end{equation}
thus we have the following initialization strategies:
\begin{description}
	\item[Xavier's initialization]
	\begin{equation}\label{key}
	\mathbb{V}[K^{\ell,\nu}_{h,o;s,t}] = \frac{2}{ (c_\ell + h_\ell) (2k+1)^2}.
	\end{equation}
	\item[Kaiming's initialization]
	\begin{equation}\label{key}
	\mathbb{V}[K^{\ell,\nu}_{h,o;s,t}] = \frac{2}{c_\ell (2k+1)^2}.
	\end{equation}
\end{description}

Here we can take this Kaiming's initialization as:
\begin{itemize}
	\item Double the Xavier's choice, and get
	\begin{equation}\label{key}
	\mathbb{V}[K^{\ell,\nu}_{h,o;s,t}] = \frac{4}{(c_\ell + h_\ell )(2k+1)^2}.
\end{equation}
	\item Then pick $c_\ell$ or $h_\ell$ for final result
	\begin{equation}\label{key}
	\mathbb{V}[K^{\ell,\nu}_{h,o;s,t}] = \frac{2}{c_\ell (2k+1)^2}.
	\end{equation}
\end{itemize}

And they have the both uniform and normal distribution type.
\begin{figure}[H]
	\begin{center}
		\includegraphics[width=0.45\linewidth]{converge_22layers}
	\end{center}
	\caption{The convergence of a \textbf{22-layer} large model. The $x$-axis is the number of training epochs. The y-axis is the top-1 error of 3,000 random val samples, evaluated on the center crop. Use ReLU as the activation for both cases. Both Kaiming's initialization (red) and ``\emph{Xavier's}'' (blue) \cite{glorot2010understanding} lead to convergence, but Kaiming's initialization starts reducing error earlier.}
	\label{fig:converge_22layers}
\end{figure}
\begin{figure}[H]
	\begin{center}
		\includegraphics[width=0.5\linewidth]{converge_30layers}
	\end{center}
	\caption{The convergence of a \textbf{30-layer} small model (see the main text). Use ReLU as the activation for both cases. Kaiming's initialization (red) is able to make it converge. But ``\emph{Xavier's}'' (blue) \cite{glorot2010understanding} completely stalls - It is also verified that that its gradients are all diminishing. It does not converge even given more epochs.}
	\label{fig:converge_30layers}
\end{figure}

Given a 22-layer model, in Cifar10 both Kaiming's and Xavier's initialization are able to converge and the validation accuracies with two different initialization
are about the same(error is 33.82,33.90). But the convergence with Kaiming's initialization is faster than Xavier's.
With extremely deep model with up to 30 layers,
Kaiming's initialization is able to make the model convergence. On the contrary, Xavier's method completely stalls the learning.

\endinput

\iffalse
\subsection{Basic assumptions in Xavier's and Kaiming's initialization.}
\begin{itemize}
	\item $f^\ell$ is a random vector and $f^\ell_i$ for $i = 1:n_\ell$ are i.i.d.
	\item $W^\ell$ should be initialized as random vector independently to $f^{\ell-1}$ and $W^\ell_{ij}$
	for all $i = 1:n_{\ell},j=1:n_{\ell-1}$ are i.i.d with a zero mean and symmetric distribution around zero.
	\item $b^\ell = 0$.
\end{itemize}

\subsection{Xavier's initialization.} There are two steps in Xavier's initialization:
\begin{itemize}
	\item forward variance formula,
	\item backward variance formula.
\end{itemize}
For forward variance:
\begin{equation}\label{key}
f^\ell_i = W^\ell_i \cdot \sigma(f^{\ell-1}) + b^\ell_i.
\end{equation}
Take variance on both sides:
\begin{equation}\label{key}
{\rm Var}[ f^\ell_i ] = {\rm Var}[W^\ell_i \cdot \sigma(f^{\ell-1})]+ b^\ell _i].
\end{equation}
They further \red{assume that $\sigma  = id$} and
\begin{equation}\label{key}
\mathbb{E}[ f^{\ell-1}_j] = 0.
\end{equation}
Then, they have
\begin{equation}\label{key}
{\rm Var}[ f^\ell_i ]  = n_{\ell-1}{\rm Var}[W^\ell_{ij}] {\rm Var}[f^{\ell-1}_j],
\end{equation}
for all $i=1:n_\ell$ and $j = 1:n_{\ell-1}$.
This leads to
\begin{equation}\label{key}
{\rm Var}[ f^L_i ]  = {\rm Var}[f^{0}_j] (\prod_{\ell=1}^L n_{\ell-1}{\rm Var}[W^\ell_{st}] ),
\end{equation}
for any $i,j,s,t$.
The ideal situation to control the variance of $f^{\ell}$ during forward propagation is that
\begin{equation}\label{key}
{\rm Var}[ f^L_i ] =  {\rm Var}[f^{0}_j].
\end{equation}
Thus, a sufficient condition from forward analysis is:
\begin{equation}\label{key}
{\rm Var}[W^\ell_{ij}] = \frac{1}{n_{\ell-1}}.
\end{equation}

The so-called backward variance analysis is to investigate the variance of $\frac{\partial L(\theta)}{\partial f^\ell}$ and $\frac{\partial L(\theta)}{\partial f^{\ell-1}}$
where $L(\theta)$ is the loss function.
This is important because that:
\begin{equation}\label{key}
\frac{\partial L(\theta)}{\partial f^{\ell-1}} = \frac{\partial L(\theta)}{\partial f^\ell} \frac{\partial f^\ell}{\partial f^{\ell-1}}.
\end{equation}
We have the next backward propagation case:
\begin{equation}\label{key}
\frac{\partial L(\theta)}{\partial f^{\ell-1}} = [W^\ell]^T \cdot \frac{\partial L(\theta)}{\partial f^\ell}.
\end{equation}
Similarly, we can derive the condition to keep
\begin{equation}\label{key}
{\rm Var}[ \frac{\partial L(\theta)}{\partial f^\ell}] = {\rm Var} [ \frac{\partial L(\theta)}{\partial f^{\ell-1}}],
\end{equation}
as
\begin{equation}\label{key}
{\rm Var}[W^\ell_{ij}] = \frac{1}{n_{\ell}}.
\end{equation}

Thus, the {\bf Xavier's initialization} suggests to initialize $W^\ell_{ij}$ with variance as:
\begin{itemize}
	\item Forward based:
	\begin{equation}\label{key}
	{\rm Var}[W^\ell_{ij}] = \frac{1}{n_{\ell-1}}.
	\end{equation}
	\item Backward based:
	\begin{equation}\label{key}
	{\rm Var}[W^\ell_{ij}] = \frac{1}{n_{\ell}}.
	\end{equation}
	\item Trade-off:
	\begin{equation}\label{key}
	{\rm Var}[W^\ell_{ij}] = \frac{2}{n_{\ell-1} + n_\ell}.
	\end{equation}
\end{itemize}
\fi

\iffalse
In order to find a good initialization for deep networks, Kaiming He (2015) followed Xavier (2010) to study the propagations of signals from input layer to output layer.
Some preliminary results about variance of random variable.
\begin{itemize}
	\item Definition:
	\begin{equation}\label{key}
	{\rm Var}[X] = \mathbb{E}[X - \mathbb{E}[X]]^2 = \mathbb{E}[X^2] - (E[X])^2.
	\end{equation}
	\item Summation:
	\begin{equation}\label{key}
	{\rm Var}[X + Y] = {\rm Var}[X] + {\rm Var}[Y],
	\end{equation}
	if $X$ and $Y$ are independent.
	\item Multiplication:
	\begin{equation}\label{key}
	{\rm Var}[XY] = \mathbb{E}[X^2]\mathbb{E}[Y^2] - (\mathbb{E}[X])^2(\mathbb{E}[Y])^2,
	\end{equation}
	if $X$ and $Y$ are independent. Furthermore, if $\mathbb{E}[X] = 0$,
	\begin{equation}\label{key}
	{\rm Var}[XY] = {\rm Var}[X]\mathbb{E}[Y^2].
	\end{equation}
\end{itemize}
\fi


\section{Group cohomology}
Today we will connect the two topics of 2-cocycles and group extensions and finally define the notion of group cohomology.
% \begin{mdframed}
\begin{mdframed}
  \adjustbox{scale=1,center}{%
    \begin{tikzcd}
      &\mbox{Adding 2-digit numbers}
        \ar[ddddl, leftrightarrow]
        \ar[ddddr, leftrightarrow, end anchor={[xshift=0ex]}]
        &
      \\\\\\\\
      \mbox{2-cocycle condition}\ar[rr, dashed, leftrightarrow]& & \text{Group extensions}
    \end{tikzcd}
  }
\end{mdframed}

In the case of two digit numbers, this correspondence looks as follows.

\begin{mdframed}
  \adjustbox{scale=0.73,center}{%
    \begin{tikzcd}
      &\bbz/100 \mbox{ under standard addition}
        \ar[ddddl, leftrightarrow]
        \ar[ddddr, leftrightarrow, end anchor={[xshift=0ex]}]
        &
      \\\\\\\\
      \mbox{carry: }\bbz/10 \times \bbz/10 \rightarrow \bbz/10\ar[rr, dashed, leftrightarrow]& & {0 \rightarrow \bbz/10 \rightarrow \bbz/100 \rightarrow \bbz/10 \rightarrow 0}
    \end{tikzcd}
  }
\end{mdframed}

But there is nothing special about $\bbz/10$ or $\bbz/100$ and all our proofs and correspondences can be generalized to arbitrary group extensions.

\begin{mdframed}
  \adjustbox{scale=0.88,center}{%
    \begin{tikzcd}
      & G = \set{\tens{a}\units{b} : a \in H, b \in K}
        \ar[ddddl, leftrightarrow]
        \ar[ddddr, leftrightarrow, end anchor={[xshift=0ex]}]
        &
      \\\\\\\\
      c:K \times K \rightarrow H \ar[rr, dashed, leftrightarrow]& & {0 \rightarrow H \rightarrow (G,+_c) \rightarrow K \rightarrow 0}
    \end{tikzcd}
  }
\end{mdframed}

% \end{mdframed}
% \subsection{Warmup: Commutative diagrams}
% Algebraists love commutative diagrams.
% Commutative diagrams simplify a lot of complex arguments and allow us to ``visualize'' how elements move around but
%
% The following \emph{commutative diagram} represents the equation $i_2 = \varphi \circ i_1$.
% \begin{equation*}
%   \begin{tikzcd}
%         & G_1 \ar[dd, "\varphi"] \\
%     H \ar [ru, "i_1"] \ar[dr, "i_2", swap]& \\
%         & G_2
%   \end{tikzcd}
% \end{equation*}
%
% \begin{qbox}[Practice problems]
%   For the following commutative diagram, find the homomorphism (if possible)
%   \begin{equation*}
%     \begin{tikzcd}
%           & \bbz/100 \ar[dd, "\varphi"] \\
%       \bbz/10 \ar [ru, "i_1"] \ar[dr, "i_2", swap]& \\
%           & \bbz/100
%     \end{tikzcd}
%   \end{equation*}
%   \begin{enumerate}
%     \item $i_1 : $
%   \end{enumerate}
% \end{qbox}

\newpage
\subsection{Maps between extensions}
We will fix two abelian groups $H$ and $K$.

Let $G_c$ and $G_d$ be two group extensions of $H$ and $K$, given by the 2-cocycles $c: K \times K \rightarrow H$ and $d:K \times K \rightarrow H$.
This means that in $G_c$ and $G_d$ the additions are given by
\begin{align}
  \label{eq:groupAdditionGroups}
  \begin{split}
    \tens{a_1}\units{b_1} +_c \tens{a_2}\units{b_2}
      &=
      \tens{a_1 + a_2 + c(b_1, b_2)}\units{b_1 + b_2} \\
    \tens{a_1}\units{b_1} +_d \tens{a_2}\units{b_2}
      &=
      \tens{a_1 + a_2 + d(b_1, b_2)}\units{b_1 + b_2}
  \end{split}
\end{align}
where $a_1$, $a_2 \in H$ and $b_1$, $b_2 \in K$. And there are short exact sequences
\begin{equation*}
  \begin{tikzcd}
    0 \ar[r] & H \ar[r,"i_c"] &  (G_c,+_c)  \ar[r, "p_c"] & K \ar[r] & 0, \\
    0 \ar[r] & H \ar[r,"i_d"] &  (G_d,+_d)  \ar[r, "p_d"] & K \ar[r] & 0.
  \end{tikzcd}
\end{equation*}

% Set $S_{100}$ be the set of two digit numbers and let $c: \bbz/10 \times \bbz/10 \rightarrow \bbz/10$ be normalized symmetric 2-cocycle.
% Denote by $(S_{100}, +_c)$ the abelian group with addition given by
% \begin{equation*}
%   \tens{a_1}\units{b_1} + \tens{a_2}\units{b_2}
%   =
%   \tens{a_1 + a_2 + c(b_1, b_2)}\units{b_1 + b_2}
% \end{equation*}
% Hence $(S_{100}, +_c)$ sits in a short exact sequence
% \begin{equation*}
%   \begin{tikzcd}
%     0 \ar[r] & H \ar[r,"i"] &  (S_{100}, +_c)  \ar[r, "p"] & K \ar[r] & 0
%   \end{tikzcd}
% \end{equation*}
%
%
% A group homomorphism between $(S_{100}, +_c)$ and $(S_{100}, +_d)$ is a map $\varphi: (S_{100}, +_c) \rightarrow (S_{100}, +_d)$ that satisfies
% \begin{align*}
%   \varphi([a_1][b_1] +_c [a_2][b_2])
%   &=
%   \varphi([a_1][b_1]) +_d \varphi([a_2][b_2])
% \end{align*}
% There is nothing more we can do here as we do not know anything about the right hand side. So we need to put more restrictions on what group homomorphisms are allowed.

\begin{definition}
  A \emph{morphism between extensions} is a group homomorphism $\varphi: G_c \rightarrow G_d$ which satisfies the following properties:
  \begin{enumerate}
    \item $\varphi$ restricted to $H$ is just the identity map,
    \item the map induced by $\varphi$ on $K$ is the identity map.
  \end{enumerate}
  % If further $\varphi$ is bijective (=one-to-one and onto), we say that $\varphi$ is an \emph{isomorphism}.

  In the language of short exact sequences, this is written as
  \begin{equation*}
    \begin{tikzcd}
      0 \ar[r] & H \ar[r,"i_c"] \ar[d,"\id_H"] &  G_c \ar[d,"\varphi"] \ar[r, "p_c"] & K \ar[r] \ar[d,"\id_K"]& 0 \\
      0 \ar[r] & H \ar[r,"i_d"] &  G_d  \ar[r, "p_d"] & K \ar[r] & 0
    \end{tikzcd}
  \end{equation*}
\end{definition}

\begin{qbox}
  What are all the group homomorphisms $\bbz/100 \rightarrow \bbz/100$?
  Of these, which group homomorphisms are also morphisms from the standard extension $0 \rightarrow \bbz/10 \rightarrow \bbz/100 \rightarrow \bbz/10 \rightarrow 0$ to itself.
\end{qbox}

\begin{qbox}
  What are all the group homomorphisms $\bbz/10 \times \bbz/10 \rightarrow \bbz/10 \times \bbz/10$?
  Of these, which group homomorphisms are also morphisms from the extension $0 \rightarrow \bbz/10 \rightarrow \bbz/10 \times \bbz/10 \rightarrow \bbz/10 \rightarrow 0$ to itself.
\end{qbox}

\begin{qbox}
  For $a \in H$ and $b \in K$, show that $\varphi(\tens{a}\units{0}) = \tens{a}\units{0}$ and $\varphi(\tens{0}\units{b}) = \tens{a'}\units{b}$ for some $a' \in H$.
\end{qbox}
For each $b \in K$, let $\alpha(b)$ be the element in $H$ such that $p(\tens{0}\units{b}) = \tens{\alpha(b)}\units{b}$, so that $\alpha$ is a function (not a group homomorphism) $K \rightarrow H$.

\begin{qbox}
  For $a \in H$ and $b \in K$, show that $\varphi(\tens{a}\units{b}) = \tens{a + \alpha(b)}\units{b}$.
\end{qbox}

\begin{qbox}
  Show that every morphism between extensions $G_c$ and $G_d$ is bijective.
\end{qbox}

As we did with group axioms, we want to rewrite what a group homomorphism means in terms of the 2-cocycles $c$ and $d$.
The group homomorphism $\varphi: (G_1,+_c) \rightarrow (G_2,+_d)$ satisfies the identity
\begin{align}
  \label{eq:groupHom}
  \varphi(\tens{a_1}\units{b_1} +_c \tens{a_2}\units{b_2}) = \varphi(\tens{a_1}\units{b_1}) +_d \varphi(\tens{a_2}\units{b_2})
\end{align}
% And the additions $+_c$ and $+_d$ are given by the identities in Equation \eqref{eq:groupAdditionGroups}.

\begin{qbox}
  \label{q:2coboundaryIdentity}
  Expand the identity \eqref{eq:groupHom} using the equation \eqref{eq:groupAdditionGroups} and find a new identity involving the functions $c$, $d$, and $h$.
\end{qbox}

\begin{definition}
  A \emph{normalized 2-coboundary} is a map $e(b_1,b_2): K \times K \rightarrow H$ such that
  \begin{equation*}
    e(b_1, b_2) = \alpha(b_1 + b_2) - \alpha(b_1) - \alpha(b_2)
  \end{equation*}
  for some function $h:K \rightarrow H$.
\end{definition}

\begin{qbox}
  Check that the identity in Q.\ref{q:2coboundaryIdentity} is saying that $c - d$ is a normalized 2-coboundary.
\end{qbox}

\begin{qbox}
  Show that a normalized 2-coboundary is also a normalized, symmetric, 2-cocycle.
\end{qbox}

\newpage
\subsection{Group cohomology}
\begin{qbox}
  Show that the set of normalized, symmetric, 2-cocycles $c:K \times K \rightarrow H$ forms a group under addition.
  This group is denoted $\calz^2(K;H)$.
\end{qbox}

\begin{qbox}
  Show that the set of normalized 2-coboundaries $c: K \times K \rightarrow H$ forms a group under addition.
  This group is denoted $\calb^2(K; H)$.
\end{qbox}

\begin{qbox}
  Show that $\calb^2(K; H)$ is a subgroup of $\calz^2(K;H)$.
\end{qbox}

\begin{definition}
  The second cohomology group of $K$ with coefficients $H$ is defined as
  \begin{align*}
    H^2(K;H) := \calz^2(K;H) / \calb^2(K; H)
  \end{align*}
\end{definition}

We say that two extensions are equivalent if there is a morphism between them.
\begin{qbox}
  Show that this defines an equivalence relation on the set of group extensions.
\end{qbox}

Denote by $\ext^1(K;H)$ the equivalence classes of extensions under this equivalence relation.

\begin{qbox}
  Prove that there is a 1-1 correspondence between $H^2(K;H)$ and $\ext^1(K;H)$.
\end{qbox}


%---------------------------Skew-----------------------------
\section{Skew}

First define normalized principal axes
\[
\begin{array}{lcl}
\hat X_1 &=& \frac {\vec X_1} {\normvec{X_1}}\\
\hat X_2 &=& \frac {\vec X_2} {\normvec{X_2}}.
\end{array}
\]

The skew is then
\[
q = | \hat X_1 \cdot \hat X_2 |.
\]
A geometric intepretation of the skew is that it measures the angle between the principal axes.
In fact, it is the absolute value of the cosine of the angle between the principal axes.

Note that if $\normvec{X_1}$ or $\normvec{X_2} < DBL\_MIN$, we set $q = 0$.

\quadmetrictable{skew}%
{$1$}%                                      Dimension
{$[0.5,1]$}%                                Acceptable range
{$[0,1]$}%                                  Normal range
{$[0,1]$}%                                  Full range
{$1$}%                                      Unit square
{Adapted from \cite{rob:87}}%               Citation
{v\_quad\_skew}%                            Verdict function name


% !TEX root =../thesis-letomes.tex

\chapter{Miscellaneous Derivations}

\section{Nondimensionalization of H-R4B Equations of Motion} \label{apx:hr4b-nondimensionalization}

Looking at the \(p_j\) from \cref{eq:pr,eq:ptheta,eq:pphi} we can infer their units:

\begin{align}
    k_{pr} &= \frac{k_m}{k_r k_t} \\[0.2cm]
    k_{p\theta} &= \frac{k_m k_r^2}{k_t} = k_{pr} k_r \\[0.2cm]
    k_{p\phi} &= k_{p\theta}
\end{align}

We now introduce the quantity

\begin{equation}
    b_j = \frac{p_j}{m_s}.
\end{equation}
It proves useful to introduce into Hamilton's equations because the mass cancels out (as expected from Newton's 2nd Law, and thus removes the need for introducing  selecting characteristic mass \(k_m\). We will treat the interpretation later. \(b_j\) has units:

\begin{align}
    k_{br} = \frac{k_{pr}}{k_m} = \frac{k_r}{k_t} \\[0.2cm]
    k_{b\theta} = \frac{k_{p\theta}}{k_m} = \frac{k_r^2}{k_t} \\[0.2cm]
    k_{b\phi} = \frac{k_{p\phi}}{k_m} = \frac{k_r^2}{k_t}
\end{align}

For the \(q_j\), \cref{eq:rdot,eq:thetadot,eq:phidot}, we set \(b_j = \frac{p_j}{m_s}\) and nondimensionalize:

\begin{align}
    \od{r}{t} &= \frac{\blue{p_r}}{\blue{p_r}} \\[0.2cm]
    \Leftrightarrow \od{r}{t} &= \blue{b_r} \\[0.2cm]
    \Leftrightarrow \frac{k_r}{k_t} \od{R}{T} &= k_{br} B_R,
\end{align}
so we get
\begin{empheq}[box=\widefbox]{align}
    \label{eq:Rdot}
    \dot{R} = B_R
\end{empheq}

\begin{align}
    \od{\theta}{t} &= \frac{\blue{p_\theta}}{\blue{m_s} r^2} \\[0.2cm]
    \Leftrightarrow \od{\theta}{t} &= \frac{\blue{b_\theta}}{r^2} \\[0.2cm]
    \Leftrightarrow \od{\theta}{t} &= \frac{b_\theta}{r^2} \\[0.2cm]
    \Leftrightarrow \frac{1}{k_t} \od{\theta}{T} &= \frac{k_{b\theta}}{k_r^2} \frac{B_\theta}{R^2},
\end{align}
so we get
\begin{empheq}[box=\widefbox]{align}
    \label{eq:thetadot-nondim}
    \dot{\theta} = \frac{B_\theta}{R^2}
\end{empheq}

\begin{align}
    \od{\phi}{t} &= \frac{\blue{p_\phi}}{{\blue{m_s}} r^2 \sin^2{\theta}} \\[0.3cm]
    \Leftrightarrow \od{\phi}{t} &= \frac{\blue{b_\phi}}{r^2 \sin^2{\theta}}  \\[0.3cm]
    \Leftrightarrow \frac{1}{k_t} \frac{\Phi}{T} &= \frac{k_\phi}{k_r^2} \frac{B_\phi}{R^2 \sin^2{\theta}},
\end{align}
so we get
\begin{empheq}[box=\widefbox]{align}
    \label{eq:phidot-nondim}
    \dot{\phi} = \frac{B_\phi}{R^2 \sin^2{\theta}}
\end{empheq}

For the \(p_j\), \cref{eq:prdot,eq:pthetadot,eq:pphidot} , we first divide by \(m_s\) then set \(b_j = \frac{p_j}{m_s}\) (\blue{relevant terms marked in blue}) and \(\mu_k = G M_k\):

\begin{align}
    \begin{split}
        \od{\blue{p_r}}{t} = -\pd{H}{q_r} &= \frac{\blue{p_\theta^2}}{\blue{m_s} r^3} + \frac{\blue{p_\phi^2}}{\blue{m_s} r^3 \sin^2{\theta} } \\
        &+ G \blue{m_s} \\
        &\sum\limits_{k} M_k \frac{-r + r_k \left(\cos{\theta}\cos{\theta_k} + \sin{\theta}\sin{\theta_k}\cos{(\phi - \phi_k)}\right)}{\left[r^2 + r_k^2 - 2 r r_k \left(\cos{\theta}\cos{\theta_k} + \sin{\theta}\sin{\theta_k}\cos{(\phi - \phi_k)} \right) \right]^{3/2}},
    \end{split} \\[0.3cm]
    \begin{split}
        \Leftrightarrow \od{\blue{b_r}}{t} &= \frac{\blue{b_\theta^2}}{r^3} + \frac{\blue{b_\phi^2}}{r^3 \sin^2{\theta} } \\
        &+ \sum\limits_{k} \mu_k \frac{-r + r_k \left(\cos{\theta}\cos{\theta_k} + \sin{\theta}\sin{\theta_k}\cos{(\phi - \phi_k)}\right)}{\left[r^2 + r_k^2 - 2 r r_k \left(\cos{\theta}\cos{\theta_k} + \sin{\theta}\sin{\theta_k}\cos{(\phi - \phi_k)} \right) \right]^{3/2}}.
    \end{split}
\end{align}
And finally we nondimensionalize:
\begin{align}
    \begin{split}
        \Leftrightarrow \teal{\frac{k_{br}}{k_t}} \od{B_R}{T} &= \orange{\frac{k_{b\theta}^2}{k_r^3}} \frac{B_\theta^2}{R^3} + \orange{\frac{k_{b\phi}^2}{k_r^3}} \frac{B_\phi^2}{R^3 \sin^2{\theta} } \\
        &+ \red{\frac{k_r^3}{k_t^2}\frac{1}{k_r^2}} \\
        & \sum\limits_{k} \eta_k \frac{-R + R_k \left(\cos{\theta}\cos{\theta_k} + \sin{\theta}\sin{\theta_k}\cos{(\phi - \phi_k)}\right)}{\left[R^2 + R_k^2 - 2 R R_k \left(\cos{\theta}\cos{\theta_k} + \sin{\theta}\sin{\theta_k}\cos{(\phi - \phi_k)} \right) \right]^{3/2}},
    \end{split}
\end{align}

where the characteristic units are colored teal, orange and red, \([\mu] = [G] [M_k] = \frac{k_r^3}{k_m k_t^2} k_m = \frac{k_r^3}{k_t^2} \), the first part of the red factor, the second part being from the fraction inside the summation, having units \(\frac{1}{k_r^2}\). Dividing \(\frac{k_{br}}{k_t}\) over from the left-hand side the units cancel as they must:

\begin{align}
    & \teal{\frac{k_t}{k_{br}}} \orange{\frac{k_{b\theta}^2}{k_r^3} } \\
    = &\frac{k_t^2}{k_r} \frac{k_r^4}{k_t^2 k_r^3}\\
    = &1,
\end{align}
and equivalently same for the second orange since \(k_\theta\) and \(k_\phi\) have the same units. And the red terms:

\begin{align}
    & \teal{\frac{k_t}{k_{br}}} \red{\frac{k_r^3}{k_t^2}\frac{1}{k_r^2}} \\
    = &\frac{k_t^2}{k_r} \frac{k_r^3}{k_t^2} \frac{1}{k_r^2} \\
    = &1
\end{align}

and we are left with the nondimensionalized equation:
\begin{empheq}[box=\widefbox]{align}
    \label{eq:Brdot}
    \dot{B}_r = &\frac{B_\theta^2}{R^3} + \frac{B_\phi^2}{R^3 \sin^2{\theta}} \\
    & + \sum\limits_{k} \eta_k \frac{-R + R_k \left(\cos{\theta}\cos{\theta_k} + \sin{\theta}\sin{\theta_k}\cos{(\phi - \phi_k)}\right)}{\left[R^2 + R_k^2 - 2 R R_k \left(\cos{\theta}\cos{\theta_k} + \sin{\theta}\sin{\theta_k}\cos{(\phi - \phi_k)} \right) \right]^{3/2}} \notag
\end{empheq}

The exact same procedure for \(\dot{p_\theta}\) and \(\dot{p}_\phi\) gives us:

\begin{empheq}[box=\widefbox]{align}
    \label{eq:Bthetadot}
    \dot{B}_\theta = &\frac{B_\phi^2}{R^2 \sin^2{\theta} \tan{\theta}} \\
    &+ \sum\limits_{k} \eta_k \frac{R R_k \left[-\sin{\theta}\cos{\theta_k} + \cos{\theta}\sin{\theta_k}\cos{(\phi - \phi_k)} \right]}{\left[R^2 + R_k^2 - 2 R R_k \left(\cos{\theta}\cos{\theta_k} + \sin{\theta}\sin{\theta_k}\cos{(\phi - \phi_k)} \right) \right]^{3/2}} \notag
\end{empheq}

\begin{empheq}[box=\widefbox]{align}
    \label{eq:Bphidot}
    \dot{B}_\phi = &\sum\limits_{k} \eta_k \frac{- R R_k \sin{\theta}\sin{\theta_k}\sin{(\phi - \phi_k)}}{\left[R^2 + R_k^2 - 2 R R_k \left(\cos{\theta}\cos{\theta_k} + \sin{\theta}\sin{\theta_k}\cos{(\phi - \phi_k)} \right) \right]^{3/2}}
\end{empheq}

Finally we can get the nondimensionalize \(H\) by the same method. Again we first divide by \(m_s\) to obtain ``Hamiltonian per spacecraft mass'', \(H/m_s = H_m\):

\begin{equation*}
    \begin{aligned}
        H &= \frac{p_r^2}{2 m_s} + \frac{p_\theta^2}{2 m_s r^2} + \frac{p_\phi^2}{2 m_s r^2 \sin^2{\theta}} \\
        &- G m_s \sum\limits_{k} \frac{M_k}{\sqrt{r^2 + r_k^2 - 2 r r_k \left[\cos{\theta}\cos{\theta_k}+\sin{\theta}\sin{\theta_k}\cos{(\phi - \phi_k})\right]}},
    \end{aligned}
\end{equation*}

\begin{equation}
    \begin{aligned}
        \Leftrightarrow H_m &= \frac{b_r^2}{2} + \frac{b_\theta^2}{2 r^2} + \frac{b_\phi^2}{2 r^2 \sin^2{\theta}} \\
        &- \sum\limits_{k} \mu_k \frac{1}{\sqrt{r^2 + r_k^2 - 2 r r_k \left[\cos{\theta}\cos{\theta_k}+\sin{\theta}\sin{\theta_k}\cos{(\phi - \phi_k})\right]}},
    \end{aligned}
\end{equation}

and then use our characteristic units to get \(\mathcal{H}_m\):

\begin{equation}
    \begin{aligned}
        \Leftrightarrow \mathcal{H}_m &= \frac{B_r^2}{2} + \frac{B_\theta^2}{2 R^2} + \frac{B_\phi^2}{2 R^2 \sin^2{\theta}} \\
        &- \sum\limits_{k} \eta_k \frac{1}{\sqrt{R^2 + R_k^2 - 2 R R_k \left[\cos{\theta}\cos{\theta_k}+\sin{\theta}\sin{\theta_k}\cos{(\phi - \phi_k})\right]}}. \label{eq:HH_m}
    \end{aligned}
\end{equation}

\section{Symplectic Verlet Derivations}\label{apx:symplectic-verlet-derivations}

\subsection{Symplectic Verlet Derivations (Handwritten)}
\includepdf[scale=1.0,pages={-}]{pdf/r4b-symplectic-verlet-handwritten.pdf}

\subsection{Symplectic Verlet Derivations (Mathematica Check)}
\includepdf[scale=1.0,pages={-}]{pdf/r4b-symplectic-verlet-mathematica.pdf}


\subsection{part d}
\begin{itemize}
    \item sensitivity function
    $$
    S_G^{G_{cl}} = \dfrac{1}{1+C(s)G(s)}
    $$
    \begin{figure}[H]
        \caption{sensitivity function bode magnitude}
        \centering
        \includegraphics[width=12cm]{../Figure/Q1/Q1_d/sensitivity_func.png}
    \end{figure}
    System sensitivity is very hight at high frequency but low at low frequency.
    \item complementary sensitivity function
    $$
    S_G^{G_{cl}} = \dfrac{C(s)G(s)}{1+C(s)G(s)}
    $$
    \begin{figure}[H]
        \caption{complementary sensitivity function bode magnitude}
        \centering
        \includegraphics[width=12cm]{../Figure/Q1/Q1_d/com_sensitivity_func.png}
    \end{figure}
    \item Nichols chart for sensitivity function and complementary sensitivity function
    \begin{figure}[H]
        \caption{nyquist chart}
        \centering
        \includegraphics[width=12cm]{../Figure/Q1/Q1_d/nyquist.png}
    \end{figure}
\end{itemize}