\subsection{Boolean algebras}\label{subsec:boolean_algebras}

\begin{definition}\label{def:heyting_algebra}\mcite[10]{BezhanishviliHolliday2019}
  A \term{Heyting algebra} is a \hyperref[def:semilattice/bounded]{bounded} \hyperref[def:semilattice/distributive_lattice]{distributive lattice} \( X \) with a binary operation \( \rightarrow \) defined as
  \begin{equation}\label{eq:def:heyting_algebra/conditional}
    (x \rightarrow y) \coloneqq \bigvee\set{ a \in X \given a \wedge x \leq y }.
  \end{equation}

  In order for the operation to be well-defined, we require that the corresponding join exists for all \( y \) and \( z \). We call this operation the \term{conditional} in analogy with the \hyperref[def:propositional_language/connectives/conditional]{propositional connective}, although this operation is often called \enquote{implication} because of \hyperref[def:material_implication]{material implication}.

  Heyting algebras are useful for defining truth values for intuitionistic logic -- see \fullref{def:propositional_heyting_algebra_semantics} --- and also appear as the \hyperref[def:lindenbaum_tarski_algebra]{Lindenbaum-Tarski algebra} for propositional intuitionistic theories --- see \fullref{thm:intuitionistic_lindenbaum_tarski_algebra}.

  \begin{thmenum}[series=def:heyting_algebra]
    \thmitem{def:heyting_algebra/pseudocomplement} For any element \( x \), we define its \term{pseudocomplement} as
    \begin{equation}\label{eq:def:heyting_algebra/pseudocomplement}
      \widetilde x
      \coloneqq
      (x \rightarrow \bot)
      =
      \bigvee\set{ a \in X \given a \wedge x = \bot }.
    \end{equation}
  \end{thmenum}

  Heyting algebras have the following metamathematical properties:
  \begin{thmenum}[resume=def:heyting_algebra]
    \thmitem{def:heyting_algebra/theory} We extend the language of the \hyperref[def:semilattice/theory]{theory of lattices} with the binary infix functional symbol \( \rightarrow \) and the unary functional symbol \( \widetilde{\anon} \). By adding the axiom \eqref{eq:def:heyting_algebra/conditional} to the theory of bounded distributive lattices, we obtain the theory of Heyting algebras.

    \thmitem{def:heyting_algebra/submodel} The Heyting subalgebras are the \hyperref[def:semilattice/submodel]{bounded sublattices} for which the conditional is well-defined.

    \thmitem{def:heyting_algebra/trivial} The \hyperref[thm:substructures_form_complete_lattice/bottom]{trivial} Heyting algebra is the \hyperref[def:semilattice/trivial]{one-element bounded lattice}.

    \thmitem{def:heyting_algebra/homomorphism} \hyperref[def:first_order_homomorphism]{Homomorphisms} between Heyting algebras are lattice homomorphisms with the additional requirement that homomorphisms preserve conditionals.

    \thmitem{def:heyting_algebra/category} The \hyperref[def:category_of_small_first_order_models]{category of \( \mscrU \)-small models} for Heyting algebras is denoted by \( \cat{Heyt} \). It is a full subcategory of the \hyperref[def:semilattice/category]{category \( \cat{Lat} \) of lattices}.

    \thmitem{def:heyting_algebra/opposite} The \hyperref[def:semilattice/duality]{principle of duality for lattices} does not hold for Heyting algebras.
  \end{thmenum}
\end{definition}

\begin{example}\label{ex:topological_space_is_heyting_algebra}
  Somewhat similar to how the power set of a nonempty set is a Boolean algebra, as shown in \fullref{thm:boolean_algebra_of_subsets}, the topology \( \mscrT \) of a \hyperref[def:topological_space]{topological space} \( (X, \mscrT) \) is a Heyting algebra. This is actually used in topological semantics --- see \fullref{def:propositional_topological_semantics}.

  Indeed,
  \begin{itemize}
    \item \hyperref[def:semilattice/join]{Arbitrary joins} are given by \hyperref[def:basic_set_operations/union]{unions \( \bigcap \)}.
    \item \hyperref[def:semilattice/meet]{Finite meets} are given by \hyperref[def:basic_set_operations/intersection]{intersections \( \bigcup \)}.
    \item The \hyperref[def:partially_ordered_set_extremal_points/top_and_bottom]{top} is the entire domain \( X \).
    \item The \hyperref[def:partially_ordered_set_extremal_points/top_and_bottom]{bottom} is the empty set.
    \item The \hyperref[eq:def:heyting_algebra/conditional]{conditional \( U \leadsto V \)} is then
    \begin{equation*}
      \bigcup\set[\Big]{ A \in T \given \underbrace{A \cap U}_{A \setminus (X \setminus U)} \subseteq V }
      =
      \bigcup\set[\Big]{ A \in T \given A \subseteq V \cup (X \setminus U) }
      =
      \Int((X \setminus U) \cup V),
    \end{equation*}
    which is actually similar to \fullref{thm:boolean_equivalences/conditional_as_disjunction} despite the fact that arbitrary topologies are not Boolean algebras.

    \item As a result, the \hyperref[def:heyting_algebra/pseudocomplement]{pseudocomplement} is
    \begin{equation*}
      \widetilde U = \Int(X \setminus U).
    \end{equation*}
  \end{itemize}
\end{example}

\begin{definition}\label{def:bounded_lattice_complement}
  Let \( X \) be a \hyperref[def:semilattice/bounded]{bounded} \hyperref[def:semilattice/lattice]{lattice} and fix an element \( x \in X \). A \term{complement} of \( x \) is an element \( y \) such that
  \begin{align}
    x \vee y = \top \label{def:bounded_lattice_complement/join}, \\
    x \wedge y = \bot \label{def:bounded_lattice_complement/meet}.
  \end{align}

  Due to the commutativity of both \( \vee \) and \( \wedge \), \( y \) is a complement of \( x \) if and only if \( x \) is a complement of \( y \).
\end{definition}

\begin{proposition}\label{thm:distributive_bounded_lattice_unique_complement}
  In a \hyperref[def:semilattice/bounded]{bounded} \hyperref[def:semilattice/distributive_lattice]{distributive lattice} \( X \), each \( x \in X \) has at most one complement.

  Thus, complementation can be regarded as a \hyperref[def:partial_function]{partial operation}.
\end{proposition}
\begin{proof}
  If \( y \) and \( z \) are both complements of \( x \), then
  \begin{balign*}
    y
    &\reloset {\eqref{eq:thm:binary_lattice_operations/identity/meet}} =
    y \wedge \top
    = \\ &\reloset {\eqref{def:bounded_lattice_complement/join}} =
    y \wedge (z \vee x)
    = \\ &\reloset {\eqref{eq:def:semilattice/distributive_lattice/finite/meet_over_join}} =
    (y \wedge z) \vee (y \wedge x)
    = \\ &\reloset {\eqref{def:bounded_lattice_complement/meet}} =
    y \wedge z
    = \\ &\reloset {\eqref{def:bounded_lattice_complement/meet}} =
    (x \wedge z) \vee (y \wedge z)
    = \\ &\reloset {\eqref{eq:def:semilattice/distributive_lattice/finite/meet_over_join}} =
    (x \vee y) \wedge z
    = \\ &\reloset {\eqref{eq:thm:binary_lattice_operations/identity/meet}} =
    z.
  \end{balign*}
\end{proof}

\begin{definition}\label{def:boolean_algebra}\mcite[48]{Gratzer1978}
  A \term{Boolean algebra} is a \hyperref[def:semilattice/bounded]{bounded} \hyperref[def:semilattice/distributive_lattice]{distributive lattice} in which every element has a \hyperref[def:bounded_lattice_complement]{complement}. The complement of each element is unique due to \fullref{thm:distributive_bounded_lattice_unique_complement}. We define a unary function that gives to every element \( x \) its complement \( \overline \anon \). By definition, this function is an \hyperref[def:set_with_involution]{involution}.

  \begin{thmenum}[series=def:boolean_algebra]
    \thmitem{def:boolean_algebra/conditional} We also define the binary operation \term{conditional} (\( \rightarrow \)) via
    \begin{equation}\label{eq:def:boolean_algebra/conditional}
      (x \rightarrow y) \coloneqq (\overline x \vee y)
    \end{equation}
    in analogy with \fullref{thm:boolean_equivalences/conditional_as_disjunction}. This operation highlights that Boolean algebras are a special case of \hyperref[thm:boolean_algebras_are_heyting_algebras]{Heyting algebras}.

    \thmitem{def:boolean_algebra/biconditional} It remains to define a binary operation corresponding to the \hyperref[def:propositional_language/connectives/biconditional]{propositional biconditional}. Inspired by \fullref{thm:boolean_equivalences/biconditional_via_conditionals}, define
    \begin{equation}\label{eq:def:boolean_algebra/biconditional}
      (x \leftrightarrow y) \coloneqq (x \rightarrow y) \wedge (y \rightarrow x).
    \end{equation}
  \end{thmenum}

  Boolean algebras have the following metamathematical properties:
  \begin{thmenum}[resume=def:boolean_algebra]
    \thmitem{def:boolean_algebra/theory} To obtain the theory of Boolean algebras, we replace the unary functional symbol \( \widetilde{\anon} \) with \( \overline \anon \) in the language of the \hyperref[def:heyting_algebra/theory]{theory of Heyting algebras} and then add the axioms \eqref{def:bounded_lattice_complement/join} and \eqref{def:bounded_lattice_complement/meet} to the theory. We may also replace \eqref{eq:def:heyting_algebra/conditional} defining \( \rightarrow \) with the simpler axiom \eqref{eq:def:boolean_algebra/conditional}.

    \thmitem{def:boolean_algebra/submodel} The Boolean subalgebras are the \hyperref[def:semilattice/submodel]{bounded sublattices} which are closed under compementation.

    \thmitem{def:boolean_algebra/trivial} The \hyperref[thm:substructures_form_complete_lattice/bottom]{trivial} Boolean algebra is the \hyperref[def:semilattice/trivial]{one-element bounded lattice}.

    \thmitem{def:boolean_algebra/homomorphism} \hyperref[def:first_order_homomorphism]{Homomorphisms} between Boolean algebras are simply lattice homomorphisms.

    Complements are automatically preserved because for any lattice homomorphism \( \varphi \) between the Boolean algebras \( X \) and \( Y \),
    \begin{equation*}
      \varphi(x) \vee_Y \varphi(\overline x) = \varphi(x \vee_X \overline x) = \varphi(\top_X) = \top_Y,
    \end{equation*}
    and similarly for \( \wedge \), hence, due to \fullref{thm:distributive_bounded_lattice_unique_complement},
    \begin{equation*}
      \varphi(\overline x) = \overline {\varphi(x)}.
    \end{equation*}

    Implications are also automatically preserved because of \eqref{eq:def:boolean_algebra/conditional}.

    \thmitem{def:boolean_algebra/category} The \hyperref[def:category_of_small_first_order_models]{category of \( \mscrU \)-small models} for Boolean algebras \( \cat{Bool} \) is a full subcategory the \hyperref[def:heyting_algebra/category]{category \( \cat{Heyt} \) of Heyting algebras}.

    \thmitem{def:boolean_algebra/opposite} The \hyperref[def:semilattice/duality]{principle of duality for lattices} holds for Boolean algebras without interchanging complements.
  \end{thmenum}
\end{definition}

\begin{example}\label{ex:boolean_algebras}
  Examples of \hyperref[def:boolean_algebra]{Boolean algebras} include:

  \begin{itemize}
    \thmitem{ex:boolean_algebras/lindenbaum_tarski} The \hyperref[def:lindenbaum_tarski_algebra]{Lindenbaum-Tarski algebras} for classical logic. We prove in \fullref{thm:intuitionistic_lindenbaum_tarski_algebra} that it is a Boolean algebra.
    \thmitem{ex:boolean_algebras/f2} The \hyperref[thm:finite_fields]{prime field} \( \BbbF_2 \) with suitably defined operations discussed in \fullref{thm:f2_is_boolean_algebra}.
    \thmitem{ex:boolean_algebras/power_set} The power set of any set, usually taken to be a space with additional structure (see \fullref{thm:boolean_algebra_of_subsets}).
  \end{itemize}
\end{example}

\begin{proposition}\label{thm:binary_boolean_algebra}
  All two-element \hyperref[def:boolean_algebra]{Boolean algebras} are isomorphic.
\end{proposition}
\begin{proof}
  Follows from the requirement that \hyperref[def:semilattice/homomorphism]{lattice homomorphisms} preserve constants.
\end{proof}

\begin{corollary}\label{thm:f2_is_boolean_algebra}
  For certain purposes, for example \hyperref[def:zhegalkin_polynomial]{Zhegalkin polynomials}, we may regard the \hyperref[thm:finite_fields]{prime field} \( \BbbF_2 \) as a Boolean algebra with \( 1 \) as the \hyperref[def:partially_ordered_set_extremal_points/top_and_bottom]{top} and \( 0 \) as the \hyperref[def:partially_ordered_set_extremal_points/top_and_bottom]{bottom element}.
\end{corollary}
\begin{proof}
  Follows from \fullref{thm:binary_boolean_algebra}.
\end{proof}

\begin{proposition}\label{thm:boolean_algebras_are_heyting_algebras}
  Every \hyperref[def:boolean_algebra]{Boolean algebra} is a \hyperref[def:heyting_algebra]{Heyting algebra} with an identification given by \eqref{eq:def:boolean_algebra/conditional}.
\end{proposition}
\begin{proof}
  Fix any \( x, y \in X \) in a Boolean algebra \( X \). We will show that \( x \rightarrow y \) as defined in \eqref{eq:def:boolean_algebra/conditional} satisfies \eqref{eq:def:heyting_algebra/conditional}.

  Let
  \begin{equation*}
   A \coloneqq \set{ a \in X \given a \wedge x \leq y }
  \end{equation*}
  be the set from \eqref{eq:def:heyting_algebra/conditional}.

  We will show that \( \overline x \vee y \) is an \hyperref[def:partially_ordered_set_extremal_points/upper_and_lower_bounds]{upper bound} of \( A \).

  Fix some \( a_0 \in A \). By definition of \( A \), we have
  \begin{equation*}
   a_0 \wedge x \leq y.
  \end{equation*}

  But this means that
  \begin{equation*}
   \underbrace{(a_0 \wedge x) \vee \overline x}_{a_0 \vee \overline x} \leq y \vee \overline x.
  \end{equation*}

  Since \( a_0 \leq a_0 \vee b \) for any \( b \in X \), it follows that \( a_0 \leq y \vee \overline x \). Therefore, \( \overline x \vee y \) is indeed an upper bound of \( A \).

  Also note that
  \begin{equation*}
   (\overline x \vee y) \wedge x = \underbrace{(\overline x \vee x)}_{\top} \wedge (y \wedge x) = y \wedge x \leq y,
  \end{equation*}
  hence \( \overline x \vee y \in A \).

  Thus, \( \overline x \vee y \) is both an upper bound of \( A \) and an element of \( A \), i.e. it is the least upper bound of \( A \). Therefore,
  \begin{equation*}
   \overline x \vee y = \bigvee A.
  \end{equation*}
\end{proof}

\begin{theorem}[De Morgan's laws]\label{thm:de_morgans_laws}
  If \( X \) is a \hyperref[def:boolean_algebra]{Boolean algebra}, the following hold for any finite \hyperref[def:cartesian_product/indexed_family]{family} \( \{ x_k \}_{k \in \mscrK} \subseteq X \):
  \begin{align}
    \overline{\bigvee_{k \in \mscrK} x_k} = \bigwedge_{k \in \mscrK} \overline{x_k} \label{eq:thm:de_morgans_laws/complement_of_join} \\
    \overline{\bigwedge_{k \in \mscrK} x_k} = \bigvee_{k \in \mscrK} \overline{x_k} \label{eq:thm:de_morgans_laws/complement_of_meet}
  \end{align}

  If \( X \) is \hyperref[def:semilattice/complete]{complete}, \( \mscrK \) may be taken to be any family, not necessarily finite.
\end{theorem}
\begin{proof}
  We will only show \eqref{eq:thm:de_morgans_laws/complement_of_join} since \eqref{eq:thm:de_morgans_laws/complement_of_meet} is dual.

  In order for \( \wedge_{k \in \mscrK} \overline{x_k} \) to be the complement of \( \vee_{k \in \mscrK} x_k \), the conditions \eqref{def:bounded_lattice_complement/join} and \eqref{def:bounded_lattice_complement/meet} need to be satisfied.

  From distributivity, we have
  \begin{equation*}
    \parens*{ \bigwedge_{k \in \mscrK} \overline{x_k} } \vee \parens*{ \bigvee_{m \in \mscrK} x_m }
    \reloset {\eqref{eq:def:semilattice/distributive_lattice/arbitrary/join_over_meet}} =
    \bigwedge_{k \in \mscrK} \parens*{ \overline{x_k} \vee \bigvee_{m \in \mscrK} x_m }
    =
    \bigwedge_{k \in \mscrK} \parens*{ \underbrace{\overline{x_k} \vee x_k}_{\top} \vee \bigvee_{\mathclap{m \in K \setminus \set{k}}} x_m }
    =
    \bigwedge_{k \in \mscrK} \top
    =
    \top,
  \end{equation*}
  which proves \eqref{def:bounded_lattice_complement/join}. The proof of \eqref{def:bounded_lattice_complement/meet} is analogous.
\end{proof}


\documentclass{report}
\usepackage{amsmath}
\usepackage{amssymb}
\begin{document}
\section{Perspective of Probability}
\subsection{Abstract}
In this issue, we will observe multivariate Gaussian distribution from the perspective of probability.
\subsection{Prior Knowledge}
$$
\begin{aligned}
x &\backsim N(\mu, \sigma^2)\\
\mu &\in \mathcal{R}^p, \sigma \in \mathcal{R}^p\\
x_i &\backsim N(\mu_i, \sigma_i)\\
p(x_i) &= \frac{1}{\sqrt{2\pi}\sigma_i} \exp(-\frac{(x_i - \mu_i)^2}{2\sigma_i^2})
\end{aligned}
$$
\subsection{Derivation}
First, let's assume that each $x_i$ is $iid (independent\ identically\ distribution)$ as below:
$$
\begin{aligned}
p(x)
&=\prod_{i=1}^p p(x_i)\\
&=\frac{1}{(2\pi)^{\frac{p}{2}}\prod_{i=1}^p \sigma_i} \exp(-\frac{1}{2}\sum_{i=1}^p (\frac{(x_i-\mu_i)^2}{\sigma_i^2}))\\
&=\frac{1}{(2\pi)^{\frac{p}{2}}|\Sigma|^{\frac{1}{2}}} \exp(-\frac{1}{2}
\left (
\begin{matrix}
x_1-\mu_1 & x_2-\mu_2 & ... & x_p - \mu_p
\end{matrix}
\right )
\left (
\begin{matrix}
\frac{1}{\sigma_1^2} & 0 & ... & 0 \\
... & ... & ... & ... \\
0 & ... & 0 & \frac{1}{\sigma_p^2}
\end{matrix}
\right )
\left (
\begin{matrix}
x_1-\mu_1\\
...&\\
...&\\
x_p-\mu_p
\end{matrix}
\right ))\\
&=\frac{1}{(2\pi)^{\frac{p}{2}}|\Sigma|^{\frac{1}{2}}} \exp(-\frac{1}{2}(x-\mu)^T \Sigma^{-1}(x-\mu))
\end{aligned}
\label{Gau.pdf}
$$
The above is the probability density function of multivariate Gaussian distribution.\\\\
We know that $\sigma$ is a positive semidefinite matrix, so we can perform singular value decomposition. So we have:
$$
\begin{aligned}
\Sigma
&=UVU^T\\
&=
\left ( \begin{matrix}
u_1 & ... & u_p
\end{matrix} \right )
\left ( \begin{matrix}
\lambda_1 & 0 & ... & 0\\
... & 0 & ... & ...\\
0 & ... & ... & \lambda_p
\end{matrix} \right )
\left ( \begin{matrix}
u_1^T \\
.\\
.\\
u_p^T
\end{matrix} \right )\\
&=
\left ( \begin{matrix}
u_1\lambda_1 & ... & u_p \lambda_p\\
\end{matrix} \right )
\left ( \begin{matrix}
u_1^T\\
.\\
.\\
u_p^T
\end{matrix} \right )\\
&=\sum_{i=1}^p u_i\lambda_i u_i^T
\end{aligned}
$$
then
$$
\begin{aligned}
\Sigma^{-1}
&=(UVU^T)^{-1}\\
&=(U^T)^{-1}V^{-1}U^{-1}\\
&=UV^{-1}U^T\\
&=\sum_{i=1}^p u_i \frac{1}{\lambda_i} u_i^T
\end{aligned}
$$
Let's set $\Delta = (x-\mu)^T \Sigma^{-1} (x-\mu)$\\\\
Substitute the results derived above into:
$$
\begin{aligned}
\Delta
&=(x-\mu)^T \Sigma^{-1} (x-\mu)\\
&=(x-\mu)^T \sum_{i=1}^p u_i \frac{1}{\lambda_i}u_i^T (x-\mu)\\
&=\sum_{i=1}^p(x-\mu)^T u_i \frac{1}{\lambda_i}u_i^T (x-\mu)
\end{aligned}
$$
Let's set $y_i=(x-\mu)^T u_i$\\\\
Here, $y_i $ represents the coordinate value of $x$ projected onto the new orthogonal basis $u_i$ after centralization.\\
so:
$$
\Delta=\sum_{i=1}^p \frac{y_i^2}{\lambda_i}
$$
Next, let's look at the probability density function of multivariate Gaussian distribution:
$$
p(x)=\frac{1}{(2\pi)^{\frac{p}{2}}|\Sigma|^{\frac{1}{2}}} \exp(-\frac{1}{2}(x-\mu)^T \Sigma^{-1}(x-\mu))
$$
You can see that only the exponential part of the formula is related to the variable $x $. The previous factor is to make the probability sum $1 $.\\
Therefore, the probability of Gaussian distribution is directly related to the value of $\Delta $.\\\\
We assume $p=2$, then:
$$
\frac{y_1^2}{\lambda_1}+\frac{y_2^2}{\lambda_2}=\Delta
$$
We were surprised to find that this is very similar to the elliptic equation. The value of $\Delta$ is not fixed, so for different $x$, these sample points form concentric ellipses in the plane. This is one of the properties of Gaussian distribution.
\end{document}


\documentclass{article}
\usepackage{tocloft}
\include{common_symbols_and_format}
\renewcommand{\cfttoctitlefont}{\Large\bfseries}
\begin{document}
\logo
\rulename{True Strength Index} %Argument is name of rule
\tblofcontents

\ruledescription{The True Strength Index (TSI) is a momentum indicator used to identify overbought and oversold signals. It can also be used to indicate trend reversals using bullish and bearish crossover of the signal line and TSI line.}

\howtotrade
{The strategy is to identify the oversold and overbought markets and a momentum indicator. \\
Bullish Momentum: when the TSI line cross above signal line. \\
Bearish Momentum: when signal line cross above TSI line
}

\ruleparameters
{Window size for Exponential moving average}{25}{This is the number of time steps over which exponential contributions are sourced.}{$\lookbacklength$}
{Window size for Exponential moving average}{13}{This is the number of time steps over which exponential contributions are sourced for smoothed momentum.}{$\lookbacklength_{s}$}
{Smoothing Factor}{2}{Smoothing factor represents the weighting applied to the most recent period’s value.}{$S$}
\stoptable

\newpage
\section{Equation}
\begin{equation}
    \Delta p_{\currenttime} = p_{\currenttime} - p_{\currenttime - 1}
\end{equation}

\begin{equation}
    EMA(\Delta p_{\currenttime}, S, \lookbacklength) = \Big(\Delta p_{\currenttime} * \Big(\frac{S}{1 + \lookbacklength}\Big)\Big) + EMA_{\currenttime-1} * \Big(1-\frac{S}{1 + \lookbacklength}\Big)
\end{equation}

\begin{equation}
    EMA(|\Delta p_{\currenttime}|, S, \lookbacklength) = \Big(|\Delta p_{\currenttime}| * \Big(\frac{S}{1 + \lookbacklength}\Big)\Big) + EMA_{\currenttime-1} * \Big(1-\frac{S}{1 + \lookbacklength}\Big)
\end{equation}

\begin{equation}
    TSI = 100 \times \frac{EMA(EMA(\Delta p_{\currenttime}, S, \lookbacklength), S, \lookbacklength_{s})}{EMA(EMA(|\Delta p_{\currenttime}|, S, \lookbacklength), S,\lookbacklength_{s})}
\end{equation}
\\

where: \\

$TSI$ is True Strength Index. \\

$EMA$ is exponentially weighted moving average. \\

$p_{\currenttime}$ is current stock value. \\

$\Delta p_{\currenttime}$ is change in stock value. \\

$|\Delta p_{\currenttime}|$ is absolute change in stock value. \\

$\lookbacklength$ \ is look back length. \\

$\lookbacklength_{s}$ is look back length for smoothed momentum. \\

$S$ is the smoothing factor.

\hspace{200mm}
\hspace{200mm}
\keyterms
\furtherlinks
\end{document}


\section{Outlook}

\begin{frame}{Motivation}
    \begin{block}{Verlinde Formula}
        \begin{itemize}
            \item Let $\mc{N}$ be the moduli space of stable $\SL_{2}$-bundles over $\Sigma_{2}$; it is isomorphic to $\CC\PP^{3}$ \cite{NR1969}.
            \item Geometric quantisation $\mc{Q}(\mc{N}) := H^{0}(\mc{N}; \mc{L}^{\otimes k})$; its dimension equals the \textbf{Verlinde formula} \cite{verlinde1988, JW1992},
            \begin{equation*}
                \begin{split}
                    \dim \mc{Q}(\mc{N}) = \Ver(k) &= \frac{k^{3}}{6} + k^{2} + \frac{11k}{6} + 1 \\
                    &= \frac{(k+1)(k+2)(k+3)}{3!}.
                \end{split}
            \end{equation*}
            \item Named after Dutch physicist Erik Verlinde, who was working on conformal field theories.
        \end{itemize}
    \end{block}
\end{frame}

\begin{frame}{Integration}
    \begin{block}{Riemann-Roch-Hirzebruch Theorem}
        For toric $X$, lattice point count of $\Delta_{X}$ equals to Euler characteristic,
        \vspace*{-12pt}
        $$ \chi(X) = \int_{X} e^{c_{1}( \mc{O}(k) )}\cdot \Td{(TX)}. $$
        \vspace*{-12pt}
    \end{block}
    \begin{block}{Example}
        For $X = \CC\PP^{3}$, have $e^{c_{1}(\mc{O}(k))} = 1 + kH + \tfrac{k^{2}}{2}H^{2} + \tfrac{k^{3}}{6}H^{3}$, and $\Td{(T\CC\PP^{3})} = 1 + 2H + \tfrac{11}{6}H^{2} + H^{3}$:
        $$ \chi(\CC\PP^{3}) = \int_{\CC\PP^{3}} \big( \tfrac{k^{3}}{6} + k^{2} + \tfrac{11k}{6} + 1 \big) \cdot H^{3} + \ldots = \Ver(k). $$
    \end{block}
\end{frame}

\begin{frame}{Our Direction}
    \begin{block}{Lattice Points}
    \begin{itemize}
        \item So $\Ver(k) = \chi(\CC\PP^{3}) = \#( k\cdot \Delta_{3} \cap \ZZ^{3} )$.
        \item Anything similar for hypertoric manifolds?
    \end{itemize}
    \end{block}
    \begin{block}{Equivariant Verlinde Formula}
        \begin{itemize}
            \item Recently, equivariant Verlinde formula for moduli spaces Higgs bundle, $\mc{M}$, popped up \cite{GP2017}.
            \item $\dim \mc{Q} (\mc{M}) = \infty$, but $\mc{M}$ has a $\CC^{\ast}$-action.
            \item Decompose into $\CC^{\ast}$-weight spaces:
            \vspace*{-6pt}
                $$ \dim \mc{Q} (\mc{M}) = \sum_{n} t^{n} \cdot \dim \mc{Q}_{n}(\mc{M}), $$
            \vspace*{-6pt}
            but now $\dim \mc{Q}_{n}(\mc{M}) < \infty$.
        \end{itemize}
    \end{block}
\end{frame}

\begin{frame}{Localisation}
    \begin{block}{Fixed-Point Formula}
        \vspace*{-12pt}
        $$ \sum_{q \in \Delta} e^{\langle q, \phi \rangle} = \sum_{p \in M^{T}} \frac{ e^{\langle p, \phi \rangle} }{ \prod_{k = 1}^{n} (1- e^{\langle \alpha_{k}^{p}, \phi \rangle}) }, $$
            with edge vectors $\alpha_{k}^{p}$, and $\langle \alpha_{k}^{p}, \phi \rangle \neq 0$, \cite{barvinok1993}.
        \begin{itemize}
            \item Letting $\phi \ra 0$ gets the lattice point count (for Delzant $\Delta$).
        \end{itemize}
    \end{block}
    \begin{block}{Example for $T^{\ast}\CC\PP^{3}$}
        For $(T^{\ast}\CC\PP^{3})_{\epsilon-\text{cut}}$, get
        $$ \frac{(\epsilon + 1)(\epsilon + 2)(\epsilon + 3)}{3!} \cdot \frac{(k + \epsilon +1)(k + \epsilon + 2)(k + \epsilon + 3)}{3!}. $$
        Observe that for $\epsilon = 0$, it becomes $\Ver(k)$.
    \end{block}
\end{frame}

\begin{frame}{Other Hypertoric Manifolds}
    \begin{block}{Non-Convex Core}
        Want to see what happens for hypertoric manifolds with non-convex cores.
    \end{block}
    \begin{block}{Example}
        \vspace{136pt}
    \end{block}
\end{frame}


% Written by Anders Sjoqvist and Ulf Lundstrom, 2009
% The main sources are: tinyKACTL, Beta and Wikipedia
% Transcribed to plain TeX by Richard Hladík in 2018.
{
% Quick fixes
\let\section\header
\def\subsection#1{\header{\subsectionfont#1}}
\def\subsubsection#1{\header{\subsubsectionfont#1}}
\chapter{Mathematics (text)}

\section{Equations}

$$ax^2+bx+c=0 \Rightarrow x = \frac{-b\pm\sqrt{b^2-4ac}}{2a}$$

The extremum is given by $x = -b/2a$.
%
$$\eqalign{ax+by&=e\cr cx+dy&=f\cr}
\,\Rightarrow\,
\eqalign{x&=\frac{ed-bf}{ad-bc}\cr y&=\frac{af-ec}{ad-bc}\cr}$$
%
In general, given an equation $Ax = b$, the solution to a variable $x_i$ is given by
$$x_i = \frac{\det A_i'}{\det A} $$
where $A_i'$ is $A$ with the $i$'th column replaced by $b$.

\section{Recurrences}

If $a_n = c_1 a_{n-1} + \dots + c_k a_{n-k}$, and $r_1, \dots, r_k$ are distinct roots of $x^k + c_1 x^{k-1} + \dots + c_k$, there are $d_1, \dots, d_k$ s.t.
$$a_n = d_1r_1^n + \dots + d_kr_k^n. $$
Non-distinct roots $r$ become polynomial factors, e.g. $a_n = (d_1n + d_2)r^n$.

\section{Trigonometry}

$$\eqalign{
\sin(v+w)&=\sin v\cos w+\cos v\sin w\cr
\cos(v+w)&=\cos v\cos w-\sin v\sin w\cr
\tan(v+w)&=\frac{\tan v+\tan w}{1-\tan v\tan w}\cr
\sin v+\sin w&=2\sin\frac{v+w}{2}\cos\frac{v-w}{2}\cr
\cos v+\cos w&=2\cos\frac{v+w}{2}\cos\frac{v-w}{2}\cr
(V+W)\tan(v-w)/2&=(V-W)\tan(v+w)/2\cr}$$
where $V, W$ are lengths of sides opposite angles $v, w$.
$$\eqalign{a\cos x+b\sin x&=r\cos(x-\phi)\cr
a\sin x+b\cos x&=r\sin(x+\phi)\cr}$$
where $r=\sqrt{a^2+b^2}$, $\phi=\mathop{\rm atan2}(b,a)$.

\section{Geometry}

\subsection{Triangles}

Side lengths: $a,b,c$

Semiperimeter: $p=\frac{a+b+c}{2}$

Area: $A=\sqrt{p(p-a)(p-b)(p-c)}$

Circumradius: $R=\frac{abc}{4A}$

Inradius: $r=\frac{A}{p}$

Length of median (divides triangle into two equal-area triangles):$$m_a=\frac{1}{2}\sqrt{2b^2+2c^2-a^2}$$

Length of bisector (divides angles in two): $s_a=\sqrt{bc\left[1-\left(\frac{a}{b+c}\right)^2\right]}$

Law of sines: $\frac{\sin\alpha}{a}=\frac{\sin\beta}{b}=\frac{\sin\gamma}{c}=\frac{1}{2R}$

Law of cosines: $a^2=b^2+c^2-2bc\cos\alpha$

Law of tangents: $$\frac{a+b}{a-b}=\frac{\tan\frac{\alpha+\beta}{2}}{\tan\frac{\alpha-\beta}{2}}$$

\subsection{Quadrilaterals}

With side lengths $a,b,c,d$, diagonals $e, f$, diagonals angle $\theta$, area $A$ and
magic flux $F=b^2+d^2-a^2-c^2$:

$$ 4A = 2ef \cdot \sin\theta = F\tan\theta = \sqrt{4e^2f^2-F^2} $$

 For cyclic quadrilaterals the sum of opposite angles is $180^\circ$,
$ef = ac + bd$, and $A = \sqrt{(p-a)(p-b)(p-c)(p-d)}$.

\subsection{Spherical coordinates}
\figure{kactl/math/sphericalCoordinates.pdf}

$$\eqalign{x &= r\sin\theta\cos\phi\cr
y &= r\sin\theta\sin\phi\cr
z &= r\cos\theta\cr}\;\eqalign{
	r &= \sqrt{x^2+y^2+z^2}\cr
\theta &= \mathop{\rm acos}\left(z/\sqrt{x^2+y^2+z^2}\right)\cr
\phi &= \mathop{\rm atan2}\left(y,x\right)\cr}$$

\section{Derivatives/Integrals}

$$\eqalign{
\frac{d}{dx}\arcsin x &= \frac{1}{\sqrt{1-x^2}}\cr
\frac{d}{dx}\arccos x &= -\frac{1}{\sqrt{1-x^2}}\cr
\frac{d}{dx}\tan x &= 1+\tan^2 x\cr
\frac{d}{dx}\arctan x &= \frac{1}{1+x^2}\cr}\;\;\;\eqalign{
\int\tan ax &= -\frac{\ln|\cos ax|}{a}\cr
\int x\sin ax &= \frac{\sin ax-ax \cos ax}{a^2} \cr
\int e^{-x^2} &= \frac{\sqrt \pi}{2} \text{erf}(x)\cr
\int xe^{ax}dx &= \frac{e^{ax}}{a^2}(ax-1)\cr}$$

Integration by parts:
$$\int_a^bf(x)g(x)\d x = [F(x)g(x)]_a^b-\int_a^bF(x)g'(x)\d x$$

\section{Sums}

$$c^a + c^{a+1} + \dots + c^{b} = \frac{c^{b+1} - c^a}{c-1},\; c \neq 1$$
%
$$\eqalign{
	1 + 2 + 3 + \dots + n &= \frac{n(n+1)}{2} \cr
	1^2 + 2^2 + 3^2 + \dots + n^2 &= \frac{n(2n+1)(n+1)}{6} \cr
	1^3 + 2^3 + 3^3 + \dots + n^3 &= \frac{n^2(n+1)^2}{4} \cr
	1^4 + 2^4 + 3^4 + \dots + n^4 &= \frac{n(n+1)(2n+1)(3n^2 + 3n - 1)}{30} \cr
}$$

\section{Series}

$$e^x = 1+x+\frac{x^2}{2!}+\frac{x^3}{3!}+\dots,\quad(-\infty<x<\infty)$$
$$\ln(1+x) = x-\frac{x^2}{2}+\frac{x^3}{3}-\frac{x^4}{4}+\dots,\quad(-1<x\leq1)$$
$$\sqrt{1+x} = 1+\frac{x}{2}-\frac{x^2}{8}+\frac{2x^3}{32}-\frac{5x^4}{128}+\dots,\quad(-1\leq x\leq1)$$
$$\sin x = x-\frac{x^3}{3!}+\frac{x^5}{5!}-\frac{x^7}{7!}+\dots,\quad(-\infty<x<\infty)$$
$$\cos x = 1-\frac{x^2}{2!}+\frac{x^4}{4!}-\frac{x^6}{6!}+\dots,\quad(-\infty<x<\infty)$$

\section{Probability theory}

Let $X$ be a discrete random variable with probability $p_X(x)$ of assuming the
value $x$. It will then have an expected value (mean)
$\mu={\mathbb E}(X)=\sum_xxp_X(x)$ and variance
$\sigma^2=V(X)={\mathbb E}(X^2)-({\mathbb E}(X))^2=\sum_x(x-{\mathbb E}(X))^2p_X(x)$
where $\sigma$ is the standard deviation. If $X$ is instead continuous it will
have a probability density function $f_X(x)$ and the sums above will instead be
integrals with $p_X(x)$ replaced by $f_X(x)$.

Expectation is linear:
$${\mathbb E}(aX+bY) = a{\mathbb E}(X)+b{\mathbb E}(Y)$$
For independent $X$ and $Y$, $$V(aX+bY) = a^2V(X)+b^2V(Y).$$

\subsection{Discrete distributions}

\subsubsection{Binomial distribution}
The number of successes in $n$ independent yes/no experiments, each of which yields success with probability $p$ is $\textrm{Bin}(n,p),\,n=1,2,\dots,\, 0\leq p\leq1$.
$$p(k)=\binom{n}{k}p^k(1-p)^{n-k}$$
$$\mu = np,\,\sigma^2=np(1-p)$$
$\textrm{Bin}(n,p)$ is approximately $\textrm{Po}(np)$ for small $p$.

\subsubsection{First success distribution}
The number of trials needed to get the first success in independent yes/no experiments, each of which yields success with probability $p$, is $\textrm{Fs}(p),\,0\leq p\leq1$.
$$p(k)=p(1-p)^{k-1},\,k=1,2,\dots$$
$$\mu = \frac1p,\,\sigma^2=\frac{1-p}{p^2}$$

\subsubsection{Poisson distribution}
The number of events occurring in a fixed period of time $t$ if these events occur with a known average rate $\kappa$ and independently of the time since the last event is $\textrm{Po}(\lambda),\,\lambda=t\kappa$.
$$p(k)=e^{-\lambda}\frac{\lambda^k}{k!}, k=0,1,2,\dots$$
$$\mu=\lambda,\,\sigma^2=\lambda$$

\subsection{Continuous distributions}

\subsubsection{Uniform distribution}
If the probability density function is constant between $a$ and $b$ and 0 elsewhere it is $\textrm{U}(a,b),\quad a<b$.
$$f(x) = \cases{{1\over b-a}& $a<x<b$;\cr0& otherwise.\cr}$$
%
$$\mu=\frac{a+b}{2},\quad\sigma^2=\frac{(b-a)^2}{12}$$

\subsubsection{Exponential distribution}
The time between events in a Poisson process is $\textrm{Exp}(\lambda),\lambda>0$.
$$f(x) = \cases{\lambda e^{-\lambda x}&$x\ge 0$,\cr 0&otherwise.\cr}$$
$$\mu=\frac{1}{\lambda},\quad\sigma^2=\frac{1}{\lambda^2}$$

\subsubsection{Normal distribution}
Most real random values with mean $\mu$ and variance $\sigma^2$ are well described by ${\cal N}(\mu,\sigma^2)$, where $\sigma>0$.
$$ f(x) = \frac{1}{\sqrt{2\pi\sigma^2}}e^{-\frac{(x-\mu)^2}{2\sigma^2}} $$
If $X_1 \sim {\cal N}(\mu_1,\sigma_1^2)$ and $X_2 \sim {\cal N}(\mu_2,\sigma_2^2)$ then
$$ aX_1 + bX_2 + c \sim {\cal N}(\mu_1+\mu_2+c,a^2\sigma_1^2+b^2\sigma_2^2) $$

\section{Markov chains}
A \emph{Markov chain} is a discrete random process with the property that the next state depends only on the current state.
Let $X_1,X_2,\ldots$ be a sequence of random variables generated by the Markov process.
Then there is a transition matrix $\mathbf{P} = (p_{ij})$, with $p_{ij} = \Pr(X_n = i | X_{n-1} = j)$,
and $\mathbf{p}^{(n)} = \mathbf P^n \mathbf p^{(0)}$ is the probability distribution for $X_n$ (i.e., $p^{(n)}_i = \Pr(X_n = i)$),
where $\mathbf{p}^{(0)}$ is the initial distribution.

% \subsubsection{Stationary distribution}
$\mathbf{\pi}$ is a stationary distribution if $\mathbf{\pi} = \mathbf{\pi P}$.
If the Markov chain is \emph{irreducible} (it is possible to get to any state from any state),
% TODO: make \scriptfont\mathbb work
then $\pi_i = \frac{1}{{\mathbb E}(T_i)}$ where ${\mathbb E}(T_i)$  is the expected time between two visits in state $i$.
$\pi_j/\pi_i$ is the expected number of visits in state $j$ between two visits in state $i$.

For a connected, undirected and non-bipartite graph, where the transition probability is uniform among all neighbors, $\pi_i$ is proportional to node $i$'s degree.

% \subsubsection{Ergodicity}
A Markov chain is \emph{ergodic} if the asymptotic distribution is independent of the initial distribution.
A finite Markov chain is ergodic iff it is irreducible and \emph{aperiodic} (i.e., the gcd of cycle lengths is 1).
$\lim_{k\rightarrow\infty}\mathbf{P}^k = \mathbf{1}\pi$.

% \subsubsection{Absorption}
A Markov chain is an A-chain if the states can be partitioned into two sets $\mathbf{A}$ and $\mathbf{G}$, such that all states in $\mathbf{A}$ are absorbing ($p_{ii}=1$), and all states in $\mathbf{G}$ leads to an absorbing state in $\mathbf{A}$.
The probability for absorption in state $i\in\mathbf{A}$, when the initial state is $j$, is $a_{ij} = p_{ij}+\sum_{k\in\mathbf{G}} a_{ik}p_{kj}$.
The expected time until absorption, when the initial state is $i$, is $t_i = 1+\sum_{k\in\mathbf{G}}p_{ki}t_k$.

\section{Number-theoretical}

\subsection{Pythagorean Triples}
 The Pythagorean triples are uniquely generated by
 $$a=k\cdot (m^{2}-n^{2}),\ \,b=k\cdot (2mn),\ \,c=k\cdot (m^{2}+n^{2}),$$
 with $m > n > 0$, $k > 0$, $m \bot n$, and either $m$ or $n$ even.

\subsection{Primes}
	$p=962592769$ is such that $2^{21} \mid p-1$, which may be useful. For hashing
	use 970592641 (31-bit number), 31443539979727 (45-bit), 3006703054056749
	(52-bit). There are 78498 primes less than $1\,000\,000$.

	Primitive roots exist modulo any prime power $p^a$, except for $p = 2, a > 2$, and there are $\phi(\phi(p^a))$ many.
	For $p = 2, a > 2$, the group ${\mathbb Z}_{2^a}^\times$ is instead isomorphic to ${\mathbb Z}_2 \times {\mathbb Z}_{2^{a-2}}$.

\subsection{Estimates}
	$\sum_{d|n} d = O(n \log \log n)$.

	The number of divisors of $n$ is at most around 100 for $n < 5e4$, 500 for $n < 1e7$, 2000 for $n < 1e10$, $200\,000$ for $n < 1e19$.

\chapter{Combinatorial (text)}

%\section{The Twelvefold Way}
%	\import{twelvefoldway.tex}

\section{Permutations}
%\subsection{Factorial}
%	\import{factorial.tex}
%	\kactlimport{intperm.h}

\subsection{Cycles}
	Let the number of $n$-permutations whose cycle lengths all belong to the set $S$ be denoted by $g_S(n)$. Then
	$$\sum_{n=0} ^\infty g_S(n) \frac{x^n}{n!} = \exp\left(\sum_{n\in S} \frac{x^n} {n} \right)$$

\subsection{Involutions}
	An involution is a permutation with maximum cycle length 2, and it is its own inverse.
	$$a(n) = a(n-1) + (n-1)a(n-2)$$
	$$a(0) = a(1) = 1$$
	1, 1, 2, 4, 10, 26, 76, 232, 764, 2620, 9496, 35696, 140152

\subsection{Stirling numbers of the first kind}
	$$s(n,k) = (-1)^{n-k}c(n,k)$$
	$c(n,k)$ is the unsigned Stirling numbers of the first kind, and they count the number of permutations on $n$ items with $k$ cycles.
	$$s(n,k) = s(n-1,k-1) - (n-1) s(n-1,k)$$
	$$s(0,0) = 1, s(n,0) = s(0,n) = 0$$
	$$c(n,k) = c(n-1,k-1) + (n-1) c(n-1,k)$$
	$$c(0,0) = 1, c(n,0)=c(0,n)=0$$

\subsection{Eulerian numbers}
	Number of permutations $\pi \in S_n$ in which exactly $k$ elements are greater than the previous element. $k$ $j$:s s.t. $\pi(j)>\pi(j+1)$, $k+1$ $j$:s s.t. $\pi(j)\geq j$, $k$ $j$:s s.t. $\pi(j)>j$.
	$$E(n,k) = (n-k)E(n-1,k-1) + (k+1)E(n-1,k)$$
	$$E(n,0) = E(n,n-1) = 1$$
	$$E(n,k) = \sum_{j=0}^k(-1)^j\binom{n+1}{j}(k+1-j)^n$$

\subsection{Burnside's lemma}
	Given a group $G$ of symmetries and a set $X$, the number of elements of $X$ \emph{up to symmetry} equals
	 $$ {\frac {1}{|G|}}\sum _{{g\in G}}|X^{g}|, $$
	 where $X^{g}$ are the elements fixed by $g$ ($g.x = x$).

	 If $f(n)$ counts "configurations" (of some sort) of length $n$, we can ignore rotational symmetry using $G = \mathbb Z_n$ to get
	 $$ g(n) = \frac 1 n \sum_{k=0}^{n-1}{f(\text{gcd}(n, k))} = \frac 1 n \sum_{k|n}{f(k)\phi(n/k)}. $$

\section{Partitions and subsets}
\subsection{Partition function}
	Partitions of $n$ with exactly $k$ parts, $p(n,k)$, i.e., writing $n$ as a sum of $k$ positive integers, disregarding the order of the summands.
	$$p(n,k) = p(n-1,k-1)+p(n-k,k)$$
	$$p(0,0)=p(1,n)=p(n,n)=p(n,n-1)=1$$

	For partitions with any number of parts, $p(n)$ obeys
	$$ p(0) = 1,\ p(n) = \sum_{k \in \mathbb Z \setminus \{0\}}{(-1)^{k+1} p(n - k(3k-1) / 2)} $$
	$$ p(n) \sim 0.145 / n \cdot \exp(2.56 \sqrt{n}) $$

%	\begin{center}
%	\begin{tabular}{c|c@{\ }c@{\ }c@{\ }c@{\ }c@{\ }c@{\ }c@{\ }c@{\ }c@{\ }c@{\ }c@{\ }c@{\ }c}
%		$n$    & 0 & 1 & 2 & 3 & 4 & 5 & 6  & 7  & 8  & 9  & 20  & 50  & 100 \\ \hline
%		$p(n)$ & 1 & 1 & 2 & 3 & 5 & 7 & 11 & 15 & 22 & 30 & 627 & $\mathtt{\sim}$2e5 & $\mathtt{\sim}$2e8 \\
%	\end{tabular}
%	\end{center}
%
%
\subsection{Stirling numbers of the second kind}
	Partitions of $n$ distinct elements into exactly $k$ groups.
	$$S(n,k) = S(n-1,k-1) + k S(n-1,k)$$
	$$S(n,1) = S(n,n) = 1$$
	$$S(n,k) = \frac{1}{k!}\sum_{j=0}^k (-1)^{k-j}\binom{k}{j}j^n$$

\subsection{Bell numbers}
	Total number of partitions of $n$ distinct elements.
	$$B(n) = \sum_{k=1}^n \binom{n-1}{k-1}B(n-k) = \sum_{k=1}^n S(n,k)$$
	$$B(0) = B(1) = 1$$
	The first are 1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147, 115975, 678570, 4213597.
	For a prime $p$
	$$B(p^m+n)\equiv mB(n)+B(n+1) \pmod{p}$$

\subsection{Triangles}
	Given rods of length $1,\ldots,n$,
	$$T(n) = \frac{1}{24} \cases{n(n-2)(2n-5) & $n$ even\cr(n-1)(n-3)(2n-1) & $n$ odd}.$$
	is the number of distinct triangles (positive are) that can be constructed, i.e., the \# of 3-subsets of $[n]$ s.t. $x\leq y\leq z$ and $z\neq x+y$.

\section{General purpose numbers}
\subsection{Catalan numbers}
	$$C_n=\frac{1}{n+1}\binom{2n}{n}= \binom{2n}{n}-\binom{2n}{n+1} = \frac{(2n)!}{(n+1)!n!}$$
	$$C_{n+1} = \frac{2(2n+1)}{n+2}C_n$$
	$$C_0=1, C_{n+1}=\sum C_iC_{n-i}$$
	First few are 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900.
		\itemitem{$\bullet$}\# of monotonic lattice paths of a $n\times n$-grid which do not pass above the diagonal.
		\itemitem{$\bullet$}\# of expressions containing $n$ pairs of parenthesis which are correctly matched.
		\itemitem{$\bullet$}\# of full binary trees with with $n+1$ leaves (0 or 2 children).
		\itemitem{$\bullet$}\# of non-isomorphic ordered trees with $n+1$ vertices.
		\itemitem{$\bullet$}\# of ways a convex polygon with $n+2$ sides can be cut into triangles by connecting vertices with straight lines.
		\itemitem{$\bullet$}\# of permutations of $[n]$ with no three-term increasing subsequence.

\subsection{Super Catalan numbers}
	The number of monotonic lattice paths of a $n\times n$-grid that do not touch the diagonal.
	$$S(n) = \frac{3(2n-3)S(n-1)-(n-3)S(n-2)}{n}$$
	$$S(1)=S(2)=1$$
	1, 1, 3, 11, 45, 197, 903, 4279, 20793, 103049, 518859

\subsection{Motzkin numbers}
	Number of ways of drawing any number of nonintersecting chords among $n$ points on a circle. Number of lattice paths from $(0,0)$ to $(n,0)$ never going below the $x$-axis, using only steps NE, E, SE.
	$$M(n) = \frac{3(n-1)M(n-2)+(2n+1)M(n-1)}{n+2}$$
	$$M(0) = M(1) = 1$$
	1, 1, 2, 4, 9, 21, 51, 127, 323, 835, 2188, 5798, 15511, 41835, 113634

\subsection{Narayana numbers}
	Number of lattice paths from $(0,0)$ to $(2n,0)$ never going below the $x$-axis, using only steps NE and SE, and with $k$ peaks.
	$$N(n,k) = \frac{1}{n}\binom{n}{k}\binom{n}{k-1}$$
	$$N(n,1) = N(n,n) = 1$$
	$$\sum_{k=1}^n N(n,k) = C_n$$
	1, 1, 1, 1, 3, 1, 1, 6, 6, 1, 1, 10, 20, 10, 1, 1, 15, 50

\subsection{Schr\" oder numbers}
	Number of lattice paths from $(0,0)$ to $(n,n)$ using only steps N,NE,E, never going above the diagonal. Number of lattice paths from $(0,0)$ to $(2n,0)$ using only steps NE, SE and double east EE, never going below the $x$-axis. Twice the Super Catalan number, except for the first term.
	1, 2, 6, 22, 90, 394, 1806, 8558, 41586, 206098

}


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

\section*{Fri Oct 18 2019}

\subsection{Covariant differentiation}

We want to do derivatives, as in:
%
\begin{equation}
  f(x + \dd{x} ) = f(x) + \pdv{f}{x} \dd{x}
\,,
\end{equation}
%
which allow us to ``move around''. In more dimensions,  the rule will be
%
\begin{equation}
  f(x + \dd{x} ) - f(x) = \dd{x^{\mu }} \partial_\mu f
\,.
\end{equation}
%

Now, we want to prove that, in Minkowski spacetime, \(\partial_\mu f\) is a rank (0,1) tensor if \(f\) is a function.

Under a change of variables \(x^{\mu } \rightarrow x^{\prime \mu }\) we have
%
\begin{equation}
  \pdv{f(x)}{x^{\mu }} \rightarrow \pdv{f (x')}{x^{\prime \mu }} = \pdv{x^{\alpha }}{x^{\prime \mu }} \pdv{}{x^{\alpha }} f(x) \marginnote{Since \(x'\) and \(x\) represent the same point, \(f(x') = f(x)\).}
\,,
\end{equation}
%
which is the transformation law of a covariant vector, or (0,1) tensor.

However, for a vector \(\dd{x^{\nu }} \partial_\nu A^{\mu }\) is \emph{not} a tensor!

Under a change of coordinates,
%
\begin{subequations}
\begin{align}
  \pdv{A^{\mu}(x)}{x^{\nu }}  &= \pdv{}{x^{\prime \nu }} A^{\mu }(x')   \\
  &= \pdv{x^{\alpha }}{x^{\prime \nu }} \pdv{}{x^{\alpha }} \qty(\pdv{x^{\prime \mu }}{x^{\beta }} A^{\beta  }(x))    \\
  &= \pdv{x^{\alpha }}{x^{\prime \nu }} \pdv[2]{x^{\prime \mu }}{x^{\alpha }}{x^{\beta }} +
  \pdv{x^{\alpha }}{x^{\prime \nu }} \pdv{x^{\prime \mu }}{x^{\beta }} \pdv{A^{\beta }}{x^{\alpha }}
  \,,
\end{align}
\end{subequations}
%
and we can see that the second term is the transformation we want, but the first term spoils the transformation.

This is not an issue in SR: there, the second derivative vanishes:
%
\begin{equation}
    \pdv[2]{x^{\prime \mu }}{x^{\alpha }}{x^{\beta }}
    = \pdv{}{x^{\alpha }} \Lambda^{\mu }_{\beta } = 0
\,,
\end{equation}
%
since Lorentz matrices are constant.

So, we construct a \emph{Covariant Derivative} which transforms as a tensor under diffeomorphisms.

We denote it as \(\nabla_\nu A^{\mu }\).
For any tensor \(T\) of arbitrary rank \((p, q)\) we request \(\nabla_\nu T\) to be a tensor of rank \((p, q+1)\).
Also, we request \(\nabla_\mu \rightarrow \partial_\mu\) for flat spacetime.

We define the Christoffel symbols:
%
\begin{equation}
  \Gamma_{\mu \nu }^{\alpha } =
  \frac{1}{2} g^{\alpha \lambda } \qty(g_{\lambda \mu , \nu }+ g_{\lambda \nu , \mu } - g_{\mu \nu , \lambda })
\,,
\end{equation}
%
where we introduced comma notation for partial non-covariant differentiation: the full notation is
%
\begin{align}
\partial_{\mu } x_{A} \overset{\text{def}}{=}  x_{A, \mu }
\qquad \text{and} \qquad
\nabla_{\mu } x_{A} \overset{\text{def}}{=} x_{A;\mu }
\,.
\end{align}
%

They are symmetric in the two lower indices and they are not tensors. Their transformation law is:
%
\begin{equation}
  \Gamma_{\nu \kappa }^{\mu }\rightarrow
  \pdv{x^{\prime \mu }}{x^{\alpha }} \pdv{x^{\beta }}{x^{\prime \nu }} \pdv{x^{\gamma }}{x^{\prime \mu }} \Gamma^{\alpha }_{\beta \gamma } +
  \pdv{x^{\prime \mu }}{x^{\alpha }} \pdv[2]{x^{\prime \mu }}{x^{\prime \nu }}{x^{\prime \kappa }}
\,,
\end{equation}
%
which we note is \emph{not tensorial}!

We define
%
\begin{equation}
    \nabla_{\nu }V_{\mu } = \partial_{\nu } V_\mu - \Gamma_{\nu \mu }^{\alpha } V_{\alpha }
\,,
\end{equation}
%
and
%
\begin{equation}
    \nabla_{\nu }V^{\mu} = \partial_{\nu } V_\mu + \Gamma_{\nu \alpha  }^{\mu  } V^{\alpha }
\,.
\end{equation}
%

How does it transform? For the covariant derivative, we have:
%
\begin{subequations}
\begin{align}
  \nabla_{\nu }' V_{\kappa }' &= \pdv{}{x^{\prime \nu }} V'_\kappa - \Gamma^{\prime \mu }_{\nu \kappa } V_{\mu  }'  \\
  &= \pdv{x^{\mu }}{x^{\prime \nu }} \partial_{\mu } \qty(\pdv{x^{\lambda }}{x^{\prime \kappa}}V_{\lambda }) -  \qty(\pdv{x^{\prime \mu }}{x^{\alpha }} \pdv{x^{\beta }}{x^{\prime \nu }} \pdv{x^{\gamma }}{x^{\prime \mu }} \Gamma^{\alpha }_{\beta \gamma } +
  \pdv{x^{\prime \mu }}{x^{\alpha }} \pdv[2]{x^{\prime \mu }}{x^{\prime \nu }}{x^{\prime \kappa }} )
  \qty(\pdv{x^{\lambda }}{x^{\prime \mu }} V_{\lambda })  \\
  &= \pdv[2]{x^{\lambda }}{x^{\prime \nu }}{x^{\prime \mu }} V_{\lambda }
  + \pdv{x^{\lambda }}{x^{\prime \mu}} \pdv{V_{\lambda }}{x^{\prime \nu }} -
  \pdv{x^{\prime \mu }}{x^{\alpha }} \qty(\pdv{x^{\beta }}{x^{\prime \nu }}\pdv{x^{\gamma }}{x^{\prime \kappa }} \Gamma^{\alpha }_{\beta \gamma }  + \pdv[2]{x^{\alpha }}{x^{\prime \nu }}{x^{\prime \kappa }} ) \pdv{x^{\lambda }}{x^{\prime \mu }} V_{\lambda }   \\
  &=\pdv[2]{x^{\lambda }}{x^{\prime \nu }}{x^{\prime \mu }} V_{\lambda }
  + \pdv{x^{\lambda }}{x^{\prime \kappa }} \pdv{x^{\alpha }}{x^{\prime \nu }} \pdv{V_{\lambda } }{x^{\alpha }}
  - \qty(\pdv{x^{\beta }}{x^{\prime \nu }}\pdv{x^{\gamma }}{x^{\prime \kappa }} \Gamma^{\alpha }_{\beta \gamma }  + \pdv[2]{x^{\alpha }}{x^{\prime \nu }}{x^{\prime \kappa }} ) \delta^{\lambda }_{\alpha }V_{\lambda }   \\
  &= \pdv[2]{x^{\lambda }}{x^{\prime \nu }}{x^{\prime \kappa }} V_{\lambda }
  + \pdv{x^{\lambda }}{x^{\prime \kappa }} \pdv{x^{\alpha }}{x^{\prime \nu }} \pdv{V_{\lambda } }{x^{\alpha }}
  - \pdv{x^{\beta }}{x^{\prime \nu }} \pdv{x^{\gamma }}{x^{\prime \kappa }} \Gamma^{\alpha }_{\beta \gamma }V_{\alpha }
  -\pdv[2]{x^{\lambda }}{x^{\prime \nu }}{x^{\prime \kappa }} V_{\lambda }   \\
  &= \pdv{x^{\lambda }}{x^{\prime \kappa }} \pdv{x^{\alpha }}{x^{\prime \nu }}
  \qty(\pdv{V_{\lambda }}{x^{\alpha } } - \Gamma^{\sigma }_{\alpha \lambda }V_{\sigma })  \\
  &= \pdv{x^{\lambda }}{x^{\prime \kappa }} \pdv{x^{\alpha }}{x^{\prime \nu }}
  \nabla_{\alpha }V_{\lambda }
  \,,
\end{align}
\end{subequations}
%
where we used: relabeling of indices, contraction of the Jacobian matrix with its inverse, the chain rule, the product rule, the transformation law of the Christoffel symbols (some steps are wrong... oh well, there's more interesting stuff to do).

The derivative of a contravariant tensor is a tensor: this can be proven by noticing that \(\nabla_\mu (A^{\alpha }B_{\alpha }) = \partial_\mu (A^{\alpha }B_{\alpha }) = B_{\alpha } \nabla_{\mu }A^{\alpha } + A^{\alpha }\nabla_{\mu }B_{\alpha }\). Otherwise, we can compute away:
%
\begin{subequations}
\begin{align}
  \nabla_{\nu }' V^{\prime \mu }  &= \pdv{V^{\prime \mu }}{x^{\prime \nu }} + \Gamma^{\prime \mu }_{\nu \kappa }V^{\prime \kappa }  \\
  &= \pdv{x^{\lambda }}{x^{\prime \nu }} \pdv{}{x^{\lambda }} \qty(\pdv{x^{\prime \mu }}{x^{\alpha }} V^{\alpha })
  +   \qty(\pdv{x^{\prime \mu }}{x^{\alpha }} \pdv{x^{\beta }}{x^{\prime \nu }} \pdv{x^{\gamma }}{x^{\prime \mu }} \Gamma^{\alpha }_{\beta \gamma }
  + \pdv{x^{\prime \mu }}{x^{\alpha }} \pdv[2]{x^{\prime \mu }}{x^{\prime \nu }}{x^{\prime \kappa }}) \qty(\pdv{x^{\prime \kappa }}{x^{\sigma }} V^{\sigma })  \\
  &= \pdv{x^{\lambda }}{x^{\prime \nu }} \pdv[2]{x^{\prime \mu }}{x^{\lambda }}{x^{\alpha }} V^{\alpha }
  + \pdv{x^{\lambda }}{x^{\prime \nu }} \pdv{x^{\prime \mu }}{x^{\alpha }} \pdv{V^{\alpha }}{x^{\lambda }}
  + \pdv{x^{\prime \mu }}{x^{\alpha }} \pdv{x^{\beta }}{x^{\prime \nu }} \delta^{\gamma}_{\sigma } \Gamma^{\alpha }_{\beta \gamma } V^{\gamma }+ \pdv{x^{\prime \mu }}{x^{\alpha }} \pdv[2]{x^{\prime \alpha }}{x^{\nu }}{x^{\mu }} \pdv{x^{\prime \kappa }}{x^{\sigma}} V^{\sigma }    \\
  &= \pdv{x^{\lambda }}{x^{\prime \nu }} \pdv[2]{x^{\prime \mu }}{x^{\lambda }}{x^{\alpha }} V^{\alpha }
  + \pdv{x^{\lambda }}{x^{\prime \nu }} \pdv{x^{\prime \mu }}{x^{\alpha }} \qty(\pdv{V^{\alpha }}{x^{\lambda }} + \Gamma^{\alpha }_{\lambda \gamma } V^{\gamma })  \\
  & = \pdv{x^{\lambda }}{x^{\prime \nu }} \pdv{x^{\prime \mu }}{x^{\alpha }} \nabla_{\lambda }V^{\alpha }
  + \pdv{x^{\lambda }}{x^{\prime \nu }} \pdv[2]{x^{\prime \mu }}{x^{\lambda }}{x^{\alpha }}V^{\alpha }
  + \pdv{x^{\prime \mu }}{x^{\alpha }} \pdv[2]{x^{ \alpha }}{x^{\prime \sigma }}{x^{\prime \kappa }} \pdv{x^{\prime \kappa }}{x^{ \sigma }} V^{\sigma }
  \,,
\end{align}
\end{subequations}
%
and we would like to see that the two last terms cancel: is
%
\begin{equation}
    \pdv{x^{\lambda }}{x^{\prime \nu }} \pdv[2]{x^{\prime \mu }}{x^{\lambda }}{x^{\alpha }}V^{\alpha }
    + \pdv{x^{\prime \mu }}{x^{\alpha }} \pdv[2]{x^{ \alpha }}{x^{\prime \sigma }}{x^{\prime \kappa }} \pdv{x^{\prime \kappa }}{x^{ \sigma }} V^{\sigma }  \overset{?}{=} 0
\,,
\end{equation}
%
for all \(V^{\mu }\)? Let us factor the vector, changing the indices:
%
\begin{equation}
    \pdv{x^{\lambda }}{x^{\prime \nu }} \pdv[2]{x^{\prime \mu }}{x^{\lambda }}{x^{\alpha }}
    + \pdv{x^{\prime \mu }}{x^{\sigma }} \pdv[2]{x^{ \sigma }}{x^{\prime \alpha }}{x^{\prime \kappa }} \pdv{x^{\prime \kappa }}{x^{ \alpha }} \overset{?}{=} 0
\,,
\end{equation}
%
we can rewrite it as
%
\begin{equation}
    \pdv{x^{\lambda }}{x^{\prime \nu }} \pdv{}{x^{\alpha }}  \pdv{x^{\prime \mu }}{x^{\lambda }}
    + \pdv{x^{\prime \mu }}{x^{\lambda  }}
    \pdv{}{x^{\prime \kappa }}
    \pdv{x^{ \lambda }}{x^{\prime \nu }}
    \pdv{x^{\prime \mu }}{x^{ \alpha }} \overset{?}{=} 0
\,,
\end{equation}
%
which can be recombined into:
%
\begin{equation}
    \pdv{x^{\lambda }}{x^{\prime \nu }} \pdv{}{x^{\alpha }}  \pdv{x^{\prime \mu }}{x^{\lambda }}
    + \pdv{x^{\prime \mu }}{x^{\lambda  }}
    \pdv{}{x^{\alpha  }}
    \pdv{x^{ \lambda }}{x^{\prime \nu }}
    \overset{?}{=} 0
\,,
\end{equation}
%
and becomes
%
\begin{equation}
  \pdv{}{x^{\alpha }} \qty(\pdv{x^{\lambda }}{x^{\prime \nu }} \pdv{x^{\prime \mu }}{x^{\lambda }} )
  = \pdv{}{x^{\alpha }} \delta^{\mu}_{\nu }
  = 0
\,.
\end{equation}
%

For any order tensor,  we add a Christoffel symbol for every index, such as in:
%
\begin{equation}
  \nabla_{\mu }V_{\alpha \beta }
  = \partial_\mu V_{\alpha \beta } - \Gamma^{\lambda}_{\mu \alpha }V_{\lambda \beta }- \Gamma^{\lambda }_{\mu \beta }V_{\alpha \lambda }
\,,
\end{equation}
%
or
%
\begin{equation}
    \nabla_{\mu }V_{\alpha}^{\beta }
    = \partial_\mu V_{\alpha}^{\beta }
    - \Gamma^{\lambda}_{\mu \alpha }V_{\lambda}^{\beta }
    + \Gamma^{\beta }_{\mu \lambda  }V_{\alpha}^{\lambda }
\,.
\end{equation}

\begin{bluebox}

The general formula reads:
%
\begin{align}
\nabla_{\pi } T^{\mu_1 \dots \mu_{n}}_{\nu_1 \dots \nu_{p}} =
\partial_{\pi } T^{\mu_1 \dots \mu_{n}}_{\nu_1 \dots \nu_{p}}
+ \sum_{i=1}^{n} \Gamma^{\mu_{i}}_{\pi \sigma }
T^{\mu_1 \dots \mu_{i-1} \sigma \mu_{i+1} \dots \mu_{n}}_{\nu_1 \dots \nu_{p}}
- \sum _{j=1}^{p} \Gamma^{\sigma }_{\pi \nu_j}
T^{\mu_1 \dots \mu_{n}}_{\nu_1 \dots \nu_{i-1} \sigma \nu_{i+1} \dots \nu_{p}}
\,,
\end{align}
%
which can be generalized to tensor \emph{densities} beyond regular tensors: any tensor tensity \(\mathfrak{T}\) (with any amount of indices, omitted for simplicity) of weight \(w\) (that is, which transforms almost like a tensor, with an extra factor of the determinant of the metric raised to the \(w\)th power) can be written as \(\mathfrak{T} = \sqrt{-g}^{w} T\), where \(g\) is the determinant of the metric while \(T\) is a regular tensor, since as we saw \(\sqrt{-g}\) is a density of weight +1.

Then, we can use this to calculate the covariant derivative of a tensor density: we can express \(T = \sqrt{-g}^{-w} \mathfrak{T}\), so the rule will be
%
\begin{align}
\nabla_{\mu } \mathfrak{T} = \nabla_{\mu } \qty(\sqrt{-g}^{w} \sqrt{-g}^{-w} \mathfrak{T})
= \sqrt{-g}^{w} \nabla_{\mu } \qty(\sqrt{-g}^{-w} \mathfrak{T})
\,,
\end{align}
%
where we used the fact that
%
\begin{align}
\nabla_{\mu} \sqrt{-g} = 0
\,,
\end{align}
%
which follows from the fact that the metric is covariantly constant: if we compute it in a LIF, then we get
%
\begin{align}
\nabla_{ \mu} \qty(\sqrt{-g})^{w} = \partial_{\mu } \sqrt{-g}^{w}
- w \Gamma^{\alpha }_{\alpha \mu} \sqrt{-g}
\,,
\end{align}
%
which is zero:\footnote{For a derivation of this expression, see \url{https://yarikraak.nl/tensor_densities.pdf}} there, the coordinate derivatives of the metric are zero, and therefore the Christoffel symbols are also zero.
\end{bluebox}

\subsection{Properties of the covariant derivative}

\begin{itemize}
    \item The covariant derivative of a tensor is a tensor;
    \item the covariant derivative obeys the Leibniz rule: \(\nabla_{\mu} (AB) = B \nabla_{\mu }A + A \nabla_{\mu }B\);
    \item the metric is covariantly constant: \(\nabla_{\mu }g_{\alpha \beta }=0\).
\end{itemize}

The fact that the metric is covariantly constant could actually be assumed instead of the explicit expression of the Christoffel symbols in terms of derivatives of the metric. These two are equivalent.

Notice that covariant derivatives do \emph{not} commute!

We can check that \(\partial_{\mu} (A^{\alpha }B_{\alpha }) = \nabla_{\mu } (A^{\alpha }B_{\alpha })\). It is
%
\begin{equation}
  \qty(\partial_{\mu }A_{\alpha } - \Gamma^{\lambda }_{\mu \alpha }A_{\lambda })B^{\alpha }
  + A_{\alpha }\qty(\partial_{\mu }B^{\alpha } + \Gamma_{\mu \lambda }^{ \alpha }B^{\lambda })
\,,
\end{equation}
%
expanding and relabeling indices we get the desired cancellation. Now, for parallel transport:

\subsection{Parallel transport}

Take a curve \(x^{\alpha }(\lambda )\) and a vector \(V^{\mu }\) defined at a certain point along the curve.
For infinitesimal displacement we will have \(\dd{V} = \dd{x} \cdot \nabla V\): in components \(\dd{V^{\mu }} = \dd{x^{\alpha }} \nabla_{\alpha }V^{\mu }\).

Parallel transport means that the vector does not change when it is transported: \(\nabla_t V^{\mu }=0\) where the index \(t \) indicates derivation along the curve's tangent vector \(t^{\alpha }= \dv*{x^{\alpha }}{\lambda } \): more explicitly, \(V^{\mu }\) is parallel-transported along the curve parametrized as \(x^{\alpha }(\lambda )\) if
%
\begin{align}
\dv{}{\lambda } \qty(V^{\mu }(x^{\alpha }(\lambda )))
=
\dv{x^{\alpha }}{\lambda } \nabla_{\alpha } V^{\mu }
= 0
\,.
\end{align}

\end{document}


\subsection{Analytical solution}
Will not work because of how \eqref{line} is defined. It would have to be the line between the two points that is on the spherical surface.
\begin{equation}
    g(s)=K(f(s))
\end{equation}
\begin{equation}
    g'(s) = \underbrace{K'(f(s))}_{\text{3$\times$2 Matrix}}\cdot\underbrace{f'(s)}_{\text{2$\times$1 Vector}}=\text{3$\times$1 Vector}
\end{equation}
\begin{equation}
    g'(a,b,s) = K'(f(a,b,s))\cdot f'(a,b,s)
\end{equation}
\begin{equation}
    g'(\theta_1,\varphi_1,\theta_2,\varphi_2,s)
\end{equation}
\begin{equation}
    K'(f(s))=K'
    \begin{pmatrix}
        (1-s)\cdot\theta_1 + s\cdot\theta_2 \\
        (1-s)\cdot\varphi_1 + s\cdot\varphi_2
    \end{pmatrix}
\end{equation}
given:
\begin{equation*}
    A\dbinom{\theta_1}{\varphi_1}, B\dbinom{\theta_2}{\varphi_2}, C\dbinom{\theta_3}{\varphi_3}
\end{equation*}
\begin{equation}
    \cos(\alpha)=\frac{\langle g'(A,B,0), g'(A,C,0)\rangle}{\|g'(A,B,0))\|\cdot \|g'(A,C,0)\|}
\end{equation}
\begin{equation}
    \cos(\beta)=\frac{\langle g'(B,A,0), g'(B,C,0)\rangle}{\|g'(B,A,0))\|\cdot \|g'(B,C,0)\|}
\end{equation}
\begin{equation}
    \cos(\gamma)=\frac{\langle g'(C,A,0), g'(C,B,0)\rangle}{\|g'(C,A,0))\|\cdot \|g'(C,B,0)\|}
\end{equation}
\begin{lstlisting}[escapechar=@]
    def f(a, b, s) {
        res[0] = (1-s)@$\cdot$@a[0] + s@$\cdot$@b[0]
        res[1] = (1-s)@$\cdot$@a[1] + s@$\cdot$@b[1]
        return res
    }
\end{lstlisting}
Parameters tp representing $\theta\varphi$
\begin{lstlisting}[escapechar=@]
    def k'(tp) {
        res[0, 0] = cos(tp[0])@$\cdot$@cos(tp[1])
        res[1, 0] = cos(tp[0])@$\cdot$@sin(tp[1])
        res[2, 0] = -sin(tp[0])
        res[0, 1] = -sin(tp[0])@$\cdot$@sin(tp[1])
        res[1, 1] = sin(tp[0])@$\cdot$@cos(tp[1])
        return res
    }
\end{lstlisting}
\begin{lstlisting}[escapechar=@]
    def f'(a, b) {
        res[0] = b[0] - a[0]
        res[1] = b[1] - a[1]
        retrun res
    }
\end{lstlisting}
Use $f(a, b, s)$ as tp in $k'(tp)$ and multiply resulting matrix with vector $f'(a, b)$. Resulting in an 3$\times$1 Vector.
\begin{equation*}
    \begin{pmatrix}
        a && b \\
        c && d \\
        e && f
    \end{pmatrix}
    \cdot
    \begin{pmatrix}
        u \\
        v
    \end{pmatrix}
    =
    \begin{pmatrix}
        a\cdot u + b\cdot v \\
        c\cdot u + d\cdot v \\
        e\cdot u + f\cdot v
    \end{pmatrix}
\end{equation*}
\begin{lstlisting}[escapechar=@]
    def g'(a, b, s) {
        res[0] = (cos((1-s)@$\cdot$@a[0] + s@$\cdot$@b[0])@$\cdot$@
            cos((1-s)@$\cdot$@a[1] + s@$\cdot$@b[1]))@$\cdot$@(b[0] - a[0]) +
            (-sin((1-s)@$\cdot$@a[0] + s@$\cdot$@b[0])@$\cdot$@
            sin((1-s)@$\cdot$@a[1] + s@$\cdot$@b[1]))@$\cdot$@(b[1] - a[1])
        res[1] = (cos((1-s)@$\cdot$@a[0] + s@$\cdot$@b[0])@$\cdot$@
            sin((1-s)@$\cdot$@a[1] + s@$\cdot$@b[1]))@$\cdot$@(b[0] - a[0]) +
            (sin((1-s)@$\cdot$@a[0] + s@$\cdot$@b[0]))@$\cdot$@
            cos((1-s)@$\cdot$@a[1] + s@$\cdot$@b[1]))@$\cdot$@(b[1] - a[1])
        res[2] = (-sin((1-s)@$\cdot$@a[0] + s@$\cdot$@b[0]))@$\cdot$@
            (b[0] - a[0])
        return res
    }
\end{lstlisting}
For $s$ being $0$:
\begin{lstlisting}[escapechar=@]
    def g'(a, b) {
        res[0] = cos(a[0])@$\cdot$@cos(a[1])@$\cdot$@(b[0] - a[0]) -
            sin(a[0])@$\cdot$@sin(a[1])@$\cdot$@(b[1] - a[1])
        res[1] = cos(a[0])@$\cdot$@sin(a[1])@$\cdot$@(b[0] - a[0]) +
            sin(a[0])@$\cdot$@cos(a[1])@$\cdot$@(b[1] - a[1])
        res[2] = -sin(a[0])@$\cdot$@(b[0] - a[0])
        return res
}
\end{lstlisting}


\section{The Euler-Lagrange Equation}
\subsection{Derivation of the Equation}
We now move on to the most important theorem of the course, which gives a necessary condition to extremise a functional in the form
$$F[y]=\int_\alpha^\beta f(x,y,y^\prime)\,\mathrm dx$$
where $f$ is given.
In contexts of geometry and physics, $y$ is most likely to carry the meaning of the trajectory of some point.\\
We first assume that a extremum $y$ exists, then when we apply a small perturbation $y\mapsto y+\epsilon\eta$ with $\eta(\alpha)=\eta(\beta)=0$ to keep the endpoint fixed.
Now we want to compute $F[y+\epsilon\eta]$, but first of all we will need a lemma.
\begin{lemma}\label{fund_lemma}
    If $g:[\alpha,\beta]\to\mathbb R$ is continuous on $[\alpha,\beta]$ and
    $$\int_\alpha^\beta g(x)\eta(x)\,\mathrm dx=0$$
    for all $\eta\in C([\alpha,\beta])$ with $\eta(\alpha)=\eta(\beta)=0$, then $\forall x\in [\alpha,\beta],g(x)=0$.
\end{lemma}
\begin{proof}
    Assume for sake of contradiction that there exists some $\bar{x}$ on $(\alpha,\beta)$ such that $g(\bar{x})\neq 0$.
    WLOG $g(\bar{x})>0$, then by continuity there is an interval $[x_1,x_2]\subset[\alpha,\beta]$ such that $\exists c>0,\forall x\in [x_1,x_2],g(x)>c$.
    Set
    $$\eta(x)=\begin{cases}
        (x-x_1)(x_2-x)\text{, if $x\in [x_1,x_2]$}\\
        0\text{, otherwise}
    \end{cases}$$
    Then
    \begin{align*}
        \int_\alpha^\beta g(x)\eta(x)\,\mathrm dx
        &=\int_{x_1}^{x_2}g(x)(x-x_1)(x_2-x)\,\mathrm dx\\
        &\ge\int_{x_1}^{x_2}c(x-x_1)(x_2-x)\,\mathrm dx\\
        &>0
    \end{align*}
    Contradiction.
    So $g$ is zero on $(\alpha,\beta)$, and it is also zero at $\alpha,\beta$ by continuity, hence $g$ is zero on $[\alpha,\beta]$.
\end{proof}
The $\eta$ we have used above is called a bump function, which is $C^2$ as one can verify.
One can also make a $C^k$ bump function by considering
\footnote{It is also easy to construct a $C^{\infty}$ bump function.}
$$\eta(x)=\begin{cases}
    ((x-x_1)(x_2-x))^{k+1}\text{, if $x\in [x_1,x_2]$}\\
    0\text{, otherwise}
\end{cases}$$
Now back at $F[y+\epsilon\eta]$, then
\begin{align*}
    F[y+\epsilon\eta]&=\int_\alpha^\beta f(x,y+\epsilon\eta,y^\prime+\epsilon\eta^\prime)\,\mathrm dz\\
    &=F[y]+\epsilon\int_\alpha^\beta\left( \frac{\partial f}{\partial y}\eta+\frac{\partial f}{\partial y^\prime}\eta^\prime \right)\,\mathrm dx+O(\epsilon^2)
\end{align*}
We will analysis the $O(\epsilon^2)$ remainder later.
For now, we just observe that for $y$ to be an extremum, the first-order term shall vanish, so we want something like $\partial F[y+\epsilon\eta]/\partial\epsilon=0$.\\
Integrate the vanishing first-order coefficient by part,
\begin{align*}
    0&=\left.\frac{\partial f}{\partial y^\prime}\eta\right|_\alpha^\beta+\int_\alpha^\beta\left( \frac{\partial f}{\partial y}\eta-\frac{\mathrm d}{\mathrm dx}\frac{\partial f}{\partial y^\prime}\eta \right)\,\mathrm dx\\
    &=\int_\alpha^\beta\left( \frac{\partial f}{\partial y}-\frac{\mathrm d}{\mathrm dx}\frac{\partial f}{\partial y^\prime} \right)\eta\,\mathrm dx
\end{align*}
By the preceding lemma, we must have
$$\frac{\partial f}{\partial y}-\frac{\mathrm d}{\mathrm dx}\frac{\partial f}{\partial y^\prime}=0$$
This is known as the Euler-Lagrange equation, which is the necessary condition for an extremum.
This equation is first developed in 1745 in a letter from Lagrange to Euler.
\begin{remark}
    1. The Euler-Lagrange equation is a second-order ODE with initial conditions $y(\alpha)=y_1,y(\beta)=y_2$.\\
    2. Sometimes the LHS is denoted $\delta F[y]/\delta y(x)$ and is called the functional derivative.
    Some author also write $\epsilon\eta=\delta y$, allowing one to write $F[y+\delta y]=F[y]+\delta F[y]$ where
    $$\delta F[y]=\int_\alpha^\beta\frac{\delta F[y]}{\delta y(x)}\delta y(x)\,\mathrm dx$$
    3. Other kinds of boundary conditions (e.g. those on $y^\prime$) are possible.\\
    4. Be careful with derivatives as the notation can be a bit confusing.
    The $x,y,y^\prime$ are independent variables when we are talking about partial derivatives of $f(x,y,y^\prime)$.\\
    5. For any $h(x,y,y^\prime)$, a somewhat useful formula is
    $$\frac{\mathrm dh(x,y(x),y^\prime(x))}{\,\mathrm dx}=\frac{\partial h}{\partial x}+\frac{\partial h}{\partial y}y^\prime+\frac{\partial h}{\partial y^\prime}y^{\prime\prime}$$
    For example, for $f(x,y,y^\prime)=x((y^\prime)^2-y^2)$, we have $\mathrm df/\mathrm dx=(y^\prime)^2-y^2-2xyy^\prime+2y^{\prime\prime}y^\prime x$.
\end{remark}
\subsection{First Integrals of the Euler-Lagrange Equation}
Now we move on to solve the Euler-Lagrange equations, which is a second order ODE.
In some special cases, this is a quite easy thing to do.
In particular, if $f$ does not explicitly depend on some of its variables, then we can simplify the equation to something that is easier to solve.
These simplifications are often in the form of something being constant.
Expressions like these are called the first integrals of the equations.
Not only are they tools we can use to solve the equation, we can also view them as a conserved quantity of something that is described by a variational problem.
We will discuss the former in this section.
The latter will be mentioned later, when we discuss Noether's Theorem.\\
Assume that $f$ does not explicitly depend on $y$, then $\partial f/\partial y=0$, so the Euler-Lagrange equation can be rewritten as
$$\frac{\mathrm d}{\mathrm dx}\frac{\partial f}{\partial y^\prime}=0\implies \frac{\partial f}{\partial y^\prime}=\text{const.}$$
which is a first order ODE.
\begin{example}[Geodesics on the Plane]
    Consider the geodesics on the Euclidean plane.
    We know that we want to extremise the functional
    $$F[y]=\int_\alpha^\beta\sqrt{1+(y^\prime)^2}\,\mathrm dx$$
    In this case, the apparent $f$ does not depend on $y$, hence we can obtain the solution by just solving the first integral
    $$\frac{y^\prime}{\sqrt{1+(y^\prime)^2}}=\text{const.}$$
    One can solve this to get $y=mx+c$ for some constants $m,c$, which is our familiar formulation of a straight line, the geodesics of the plane.
\end{example}
\begin{example}[Geodesics on a Sphere]
    Consider the two-dimensional unit sphere $S^2$ in $\mathbb R^3$.
    Use the spherical polar coordinates
    $$x=\sin\theta\sin\phi,y=\sin\theta\cos\phi,z=\cos\theta,\theta\in [0,2\pi),\theta\in[0,\pi)$$
    The line element on $S^2$ inherited from $\mathbb R^3$ then gives
    $$\mathrm ds^2=\mathrm dx^2+\mathrm dy^2+\mathrm dz^2=\mathrm d\theta^2+\sin^2\theta\,\mathrm d\phi^2$$
    A path restricted on the sphere can then be parameterized in terms of $\theta,\phi$.
    Suppose we parameterize the curve by $\phi(\theta)$, then the length functional is
    $$F[\phi]=\int_{\theta_1}^{\theta_2}\sqrt{1+(\phi^\prime)^2\sin^2\theta}\,\mathrm d\theta$$
    We observe that the integral inside does not depend on $\phi$, so we can rewrite the equation to
    $$\frac{\phi^\prime\sin^2\theta}{\sqrt{1+(\phi^\prime)^2\sin^2\theta}}=\frac{\partial f}{\partial \phi^\prime}=\text{const.}=K$$
    Seperating the variables yields
    $$(\phi^\prime)^2=\frac{K^2}{\sin^2\theta(\sin^2\theta-K^2)}\implies\phi=\pm\int\frac{K\,\mathrm d\theta}{\sin\theta\sqrt{\sin^2\theta-K^2}}$$
    which shall produce two solutions, each going one way round.
    To evaluate the integral, we do the substitution $u=\cot\theta$ which produces
    $$\pm\frac{\sqrt{1-K^2}}{K}\cos(\phi-\phi_0)=\cot\theta$$
    for a constant $\phi_0$.
    By considering the geometrical meaning of this equation, it then follows that $\phi(\theta)$ describes a great circle, i.e. a circle in $\mathbb R^3$ which exists as the intersection of $S^2$ and a plane that goes through the origin.
\end{example}
Now, consider for general case of $f(x,y,y^\prime)$, we have
\begin{align*}
    \frac{\mathrm d}{\mathrm dx}\left( f-y^\prime\frac{\partial f}{\partial y^\prime} \right)&=\frac{\partial f}{\partial x}+\frac{\partial f}{\partial y}y^\prime+\frac{\partial f}{\partial y^\prime}y^{\prime\prime}-y^\prime\frac{\mathrm d}{\mathrm dx}\frac{\partial f}{\partial y^\prime}-y^{\prime\prime}\frac{\partial f}{\partial y^\prime}\\
    &=y^\prime\left( \frac{\partial f}{\partial y}-\frac{\mathrm d}{\mathrm dx}\frac{\partial f}{\partial y^\prime} \right)+\frac{\partial f}{\partial x}
    &=\frac{\partial f}{\partial x}
\end{align*}
If $y$ satisfies the Euler-Lagrange Equation.
So if $f$ does not depend explicitly on $x$, then the above indicates that
$$f-y^\prime\frac{\partial f}{\partial y^\prime}=\text{const.}$$
\begin{example}[The Brachistochrone Problem]
    Consider the functional in the Brachistochrone Problem we defined before, with the initial point assumed to be the origin:
    $$F[y]=\frac{1}{\sqrt{2g}}\int_0^\beta\frac{\sqrt{1+(y^\prime)^2}}{\sqrt{-y}}\,\mathrm dx$$
    As $f$ in this case does not depend explicitly on $x$, we know that
    $$\frac{\sqrt{1+(y^\prime)^2}}{\sqrt{-y}}-y^\prime\frac{y^\prime}{\sqrt{1+(y^\prime)^2}\sqrt{-y}}=\text{const.}=K$$
    So
    $$y^\prime=\pm\frac{\sqrt{1+K^2y}}{K\sqrt{-y}}\implies x=\pm K\int\frac{\sqrt{-y}}{\sqrt{1+K^2y}}\,\mathrm dy$$
    Set $y=-K^{-2}\sin^2(\theta/2)$, so $\mathrm dy=-K^{-2}\sin(\theta/2)\cos(\theta/2)$, so
    \begin{align*}
        x&=\pm K\int(-1)\frac{1}{K^3}\frac{\sin^2(\theta/2)\cos(\theta/2)}{\sqrt{1-\sin^2(\theta/2)}}\,\mathrm d\theta\\
        &=\mp\frac{1}{2K^2}\int(1-\cos\theta)\,\mathrm d\theta\\
        &=\mp\frac{1}{2K^2}(\theta-\sin\theta)+C
    \end{align*}
    where $C$ is a constant.
    By our initial condition, $\theta(0)=0$, so $C=0$.
    Substitute $\theta$ for both $x,y$ and we get the parameterised equations
    $$\begin{cases}
        x=(\theta-\sin\theta)/(2K^2)\\
        y=-K^{-2}\sin^2(\theta/2)
    \end{cases}$$
    which is the equation of a cycloid, i.e. the path traversed by a fixed point on a wheel which rolls on the $x$-axis.
    Hence, the Brachistochrone is a cycloid.
\end{example}
\subsection{Fermat's Principle}
Fermat's Principle postulates that light (or sound) travels along paths between two points that are stationary points of the time variation.
\footnote{In its original form, however, it said that light travels the path that requires the least time, which is not necessarily true in all cases.}
Suppose the light way is described by $y=y(x)$, then the time functional is
$$F[y]=\int\frac{\mathrm dl}{c}=\int_\alpha^\beta\frac{\sqrt{1+(y^\prime)^2}}{c(x,y)}\,\mathrm dx$$
where $c$ is the speed of light in the medium.\\
First assume that $c=c(x)$ does not depend on $y$, then $f$ does not explicitly depend on $y$, in which case we have the first integral
$$\frac{y^\prime}{\sqrt{1+(y^\prime)^2}c(x)}=\frac{\partial f}{\partial y^\prime}=\text{const.}$$
Suppose the light ray has an initial incident angle of $\theta_1$ upwards, then $\tan\theta_1=y^\prime(\alpha)$.
Let $\tan\theta=y^\prime$ in general, then the above first integral indicates that $\sin\theta/c(x)$ is constant.
This is Snell's Law.
If $c$ is increasing, then the path is concave and if $c$ is decreasing it is convex.
Also, if the light goes through a barrier, to the left of which $c$ is constant at $c_F$ and to the right of which $c_S$ with $c_S<c_F$, then the light ray will refract in the way we all expect.


\documentclass{report}

\title{explain-math example}
\author{Matt Gleich}
\date{}

\makeindex

\usepackage{explain-math}

\begin{document}
    \maketitle

    \section{Basic Math Example}
        \begin{align*}
            \statement{-2(x + 2) = 6}
            \explain{Distribute out -2}{
                \step{ (-2 \cdot x) + (-2 \cdot 2) = 6 }
                \step{ -2x - 4 = 6 }
            }
            \explain{Remove -4 from the left}{
                \step{ -2x - 4 + 4 = 6 + 4 }
                \step{ -2x = 10 }
            }
            \explain{Remove -2 from the left}{
                \step{ \frac{-2x}{-2} = \frac{10}{-2} }
                \step{ \frac{-2x}{-2} = \frac{10}{-2} }
                \step{ x = -5 }
            }
            \sol{x = -5}
        \end{align*}

    \section{Basic Physics Example}
        \begin{align*}
            \formula{p = mv}
            \variables{
                \varu{p}{\text{?}}{kg $ \frac{\text{m}}{\text{s}} $}
                \varu{m}{3.0}{k.g}
                \varu{v}{5.0}{m/s East}
            }
            \explain{Plug \& solve}{p = 3 \cdot 5}
            \sol{p = 15 \text{ kg} \cdot \frac{\text{m}}{\text{s}}}
        \end{align*}
\end{document}


\section{Application: Cryptography}

Cryptography is about encoding a message so that it is hard for a
third party to read. The original message is called the
\textbf{plaintext}%
\index{plaintext}%
\index{cipher!plaintext} and the encrypted message is called the
\textbf{ciphertext}%
\index{ciphertext}%
\index{cipher!ciphertext}. The process of turning a plaintext into the
corresponding ciphertext is called \textbf{encryption}%
\index{encryption}%
\index{cipher!encryption}, and the process of turning a ciphertext
into the corresponding plaintext is called \textbf{decryption}%
\index{decryption}%
\index{cipher!decryption}. An encryption and decryption method is also
called a \textbf{cipher}%
\index{cipher}.  Modern ciphers are designed in such a way that the
cipher itself is not secret, but the encryption depends on a secret
\textbf{key}%
\index{key!cryptography}%
\index{cipher!key}. A cipher should be designed so that decryption is
easy for a person who knows the key, but difficult for everybody
else. The art of designing ciphers is called \textbf{cryptography}%
\index{cryptography}, and the art of breaking ciphers is called
\textbf{cryptanalysis}%
\index{cryptanalysis}.

In order to be able to define ciphers using algebraic operations, we
start by encoding strings as sequences of numbers. To that end, we
assign a number to each letter of the alphabet, as well as the special
symbols ``space'', ``comma'', and ``period'', according to the
following scheme.
\begin{center}
  \tabcolsep=2.5ex
  \begin{tabular}{|c|c|c|c|c|c|c|c|c|}
    \hline
    Space & \qq{A} & \qq{B} & \qq{C} & \qq{D} & \ldots & \qq{Z} & Comma & Period \\\hline
    0 & 1 & 2 & 3 & 4 & \ldots & 26 & 27 & 28 \\\hline
  \end{tabular}
\end{center}
In practical applications, one would probably use a larger set of
symbols and a standard encoding such as ASCII or UTF-8. But the above
29 symbols will be sufficient for our purposes. It will also come in
handy that 29 is prime.

\begin{example}{Representing strings as sequences of numbers}{string-encoding}
  Convert the string ``Attack at dawn'' to a sequence of
  numbers. Convert the sequence of numbers
  $9,0,12,9,11,5,0,3,15,4,5,19,28$ to a string.
\end{example}

\begin{solution}
  We have $\qq{A}=1$, $\qq{T}=20$, $\qq{T}=20$, $\qq{A}=1$, $\qq{C}=3$,
  $\qq{K}=11$, $\mbox{Space}=0$, and so on. Continuing in this way, the
  encoding of ``Attack at dawn'' is
  $1,20,20,1,3,11,0,1,20,0,4,1,23,14$.  Conversely, we have $9=\qq{I}$,
  $0=\mbox{Space}$, $12=\qq{L}$, $9=\qq{I}$, $11=\qq{K}$, $5=\qq{E}$, and so
  on. We find that the decoded string is ``I like codes.''
\end{solution}

There are many different ways to define ciphers. Some of the oldest
known ciphers date back thousands of years.  An example of such a
``classic'' cipher is a \textbf{substitution cipher}%
\index{substitution cipher}%
\index{cipher!substitution cipher}, where each letter of the alphabet
is replaced by a different letter, for example $\qq{A}\mapsto\qq{D}$,
$\qq{B}\mapsto\qq{E}$, and so on. Substitution ciphers have the property
that changing one letter of the plaintext always changes exactly one
letter of the ciphertext. This is not a desirable property, because it
makes the cipher easy to break. Therefore, modern ciphers are designed
to satisfy a property called \textbf{diffusion}%
\index{diffusion}%
\index{cipher!diffusion}: changing one letter of the plaintext should
change many letters of the ciphertext.

In a \textbf{block cipher}%
\index{block cipher}%
\index{cipher!block cipher}, the plaintext is first divided into
blocks of equal size, and then each block is encrypted separately. The
\textbf{block size}%
\index{block size} is the number of plaintext symbols in each
block. If the length of the plaintext is not divisible by the block
size, we pad the final block with additional spaces. In the context of
a block cipher, the diffusion property means that changing one symbols
of a plaintext block potentially affects every symbol of the
ciphertext block. The following is an example of a block cipher.

\begin{definition}{Hill cipher}{hill-cipher}
  The \textbf{Hill cipher}%
  \index{Hill cipher}%
  \index{cipher!Hill cipher} of block size $n$ has as its key an
  invertible $n\times n$-matrix $A$ with scalars from $\Z_{29}$. Each
  ciphertext block $c_1,\ldots,c_n$ is computed from the corresponding
  plaintext block $p_1,\ldots,p_n$ by matrix multiplication modulo
  $\Z_{29}$:
  \begin{equation*}
    \begin{mymatrix}{c}c_1\\\vdots\\c_n\end{mymatrix}
    ~=~ A\begin{mymatrix}{c}p_1\\\vdots\\p_n\end{mymatrix}.
  \end{equation*}
  The matrix $A$ is called the \textbf{encryption matrix}%
  \index{encryption matrix}%
  \index{matrix!encryption} of the cipher. It inverse $A^{-1}$ is
  called the \textbf{decryption matrix}%
  \index{decryption matrix}%
  \index{matrix!decryption}.
\end{definition}

\begin{example}{Hill cipher: encryption}{hill-cipher-encryption}
  Encrypt the message ``Meet me tomorrow'' using the Hill cipher with
  block size $3$ and encryption matrix
  \begin{equation*}
    A ~=~ \begin{mymatrix}{ccc}
      2 & 4 & 1 \\
      3 & 1 & 5 \\
      1 & 3 & 2 \\
    \end{mymatrix}.
  \end{equation*}
\end{example}

\begin{solution}
  We start by converting the message ``Meet me tomorrow'' to a sequence
  of scalars. We have $\qq{M}=13$, $\qq{E}=5$, and so on. The encoded
  plaintext is $13,5,5,20,0,13,5,0,20,15,13,15,18,18,15,23$. Next, we
  divide the plaintext into blocks of length 3. Since the length of
  the plaintext is not a multiple of three, we pad the final block
  with spaces, i.e., with zeros.
  \begin{equation*}
    \mbox{Plaintext blocks:}\quad
    (13,5,5),\
    (20,0,13),\
    (5,0,20),\
    (15,13,15),\
    (18,18,15),\
    (23,0,0).
  \end{equation*}
  To compute the ciphertext, we regard each plaintext block as a
  $3$-dimensional column vector and multiply by the encryption matrix
  $A$. All calculations are done modulo $29$. For example, for the
  first block, we have
  \begin{equation*}
    A \begin{mymatrix}{c} 13 \\ 5 \\ 5 \end{mymatrix}
    ~=~ \begin{mymatrix}{ccc}
      2 & 4 & 1 \\
      3 & 1 & 5 \\
      1 & 3 & 2 \\
    \end{mymatrix}
    \begin{mymatrix}{c} 13 \\ 5 \\ 5 \end{mymatrix}
    ~=~ \begin{mymatrix}{c} 22 \\ 11 \\ 9 \end{mymatrix},
  \end{equation*}
  so the first ciphertext block is $(22,11,9)$. We repeat the same
  with the remaining plaintext blocks.
  \begin{align*}
    &A \begin{mymatrix}{c} 20 \\ 0 \\ 13 \end{mymatrix}
    = \begin{mymatrix}{c} 24 \\ 9 \\ 17 \end{mymatrix},
    \quad
    A \begin{mymatrix}{c} 5 \\ 0 \\ 20 \end{mymatrix}
    = \begin{mymatrix}{c} 1 \\ 28 \\ 16 \end{mymatrix},
    \quad
    A \begin{mymatrix}{c} 15 \\ 13 \\ 15 \end{mymatrix}
    = \begin{mymatrix}{c} 10 \\ 17 \\ 26 \end{mymatrix},
    \\\\[-2ex]
    &A \begin{mymatrix}{c} 18 \\ 18 \\ 15 \end{mymatrix}
    = \begin{mymatrix}{c} 7 \\ 2 \\ 15 \end{mymatrix},
    \quad
    A \begin{mymatrix}{c} 23 \\ 0 \\ 0 \end{mymatrix}
    = \begin{mymatrix}{c} 17 \\ 11 \\ 23 \end{mymatrix}.
  \end{align*}
  Therefore, we have found the following ciphertext blocks:
  \begin{equation*}
    \mbox{Ciphertext blocks:}\quad
    (22,11,9),\
    (24,9,17),\
    (1,28,16),\
    (10,17,26),\
    (7,2,15),\
    (17,11,23).
  \end{equation*}
  Finally, we can convert the ciphertext to a list of symbols:
  $\q{VKIXIQA.PJQZGBOQKW}$.
\end{solution}

\begin{example}{Hill cipher: decryption}{hill-cipher-decryption}
  Decrypt the message $\q{RNOLFPHHCIGH DE}$ using the Hill cipher with
  block size $3$ and encryption matrix
  \begin{equation*}
    A ~=~ \begin{mymatrix}{ccc}
      2 & 4 & 1 \\
      3 & 1 & 5 \\
      1 & 3 & 2 \\
    \end{mymatrix}.
  \end{equation*}
\end{example}

\begin{solution}
  The process is analogous to encryption, except that we need to use
  the decryption matrix $A^{-1}$ instead of $A$. We first calculate
  $A^{-1}$, keeping in mind that scalars are from the field $\Z_{29}$.
  The method is the same as in Example~\ref{exa:matrix-inverse-z7}; we
  skip the individual steps in the interest of brevity.
  \begin{equation*}
    \mat{A\mid I}
    ~=~
    \begin{mymatrix}{ccc|ccc}
      2 & 4 & 1  &  1 & 0 & 0 \\
      3 & 1 & 5  &  0 & 1 & 0 \\
      1 & 3 & 2  &  0 & 0 & 1 \\
    \end{mymatrix}
    ~\roweq~\ldots~\roweq~
    \begin{mymatrix}{ccc|ccc}
      1 & 0 & 0  &  23 & 20 & 11 \\
      0 & 1 & 0  &   4 & 17 & 28 \\
      0 & 0 & 1  &  26 &  8 & 11 \\
    \end{mymatrix}
    ~=~
    \mat{I\mid A^{-1}}.
  \end{equation*}
  Next, we convert the 15 ciphertext symbols $\q{RNOLFPHHCIGH DE}$ to
  scalars and divide them into blocks of length 3:
  \begin{equation*}
    \mbox{Ciphertext blocks:}\quad
    (18,14,15),\
    (12,6,16),\
    (8,8,3),\
    (9,7,8),\
    (0,4,5).
  \end{equation*}
  Now we decrypt each ciphertext block by a matrix multiplication
  with $A^{-1}$.
  \begin{align*}
    &A^{-1} \begin{mymatrix}{c} 18 \\ 14 \\ 15 \end{mymatrix}
    = \begin{mymatrix}{c} 18 \\ 5 \\ 20 \end{mymatrix},
    \quad
    A^{-1} \begin{mymatrix}{c} 12 \\ 6 \\ 16 \end{mymatrix}
    = \begin{mymatrix}{c} 21 \\ 18 \\ 14 \end{mymatrix},
    \quad
    A^{-1} \begin{mymatrix}{c} 8 \\ 8 \\ 3 \end{mymatrix}
    = \begin{mymatrix}{c} 0 \\ 20 \\ 15 \end{mymatrix},
    \\\\[-2ex]
    &A^{-1} \begin{mymatrix}{c} 9 \\ 7 \\ 8 \end{mymatrix}
    = \begin{mymatrix}{c} 0 \\ 2 \\ 1 \end{mymatrix},
    \quad
    A^{-1} \begin{mymatrix}{c} 0 \\ 4 \\ 5 \end{mymatrix}
    = \begin{mymatrix}{c} 19 \\ 5 \\ 0 \end{mymatrix}.
  \end{align*}
  This yields the following plaintext blocks:
    \begin{equation*}
    \mbox{Plaintext blocks:}\quad
    (18,5,20),\
    (21,18,14),\
    (0,20,15),\
    (0,2,1),\
    (19,5,0).
  \end{equation*}
  Converting these back to letters, and omitting the trailing space,
  we find that the plaintext is ``return to base''.
\end{solution}

It is important to note that, despite its good diffusion properties,
the Hill cipher is not secure. The cipher has many weaknesses. For
one, because $A\vect{0}=\vect{0}$, a block of spaces in the plaintext
will always be encrypted as a block of spaces in the ciphertext,
regardless of the encryption matrix $A$. More importantly, the cipher
is subject to a so-called \textbf{known plaintext attack}%
\index{known plaintext attack}%
\index{cipher!known plaintext attack}.  If an eavesdropper intercepts
some ciphertext for which a small amount of the corresponding
plaintext happens to be known, it is immediately possible to recover
the key and therefore decrypt the rest of the ciphertext. Carrying out
this attack only requires some basic knowledge of linear algebra. The
following example illustrates how this is done.

\begin{example}{Cryptanalysis of the Hill cipher: known plaintext attack}{hill-cipher-cryptanalysis}
  Eve intercepts the following encrypted message sent by Alice:%
  \index{cryptanalysis}
  \begin{center}
    \q{EFNOR.AHIFNEPL.TSZS,RSKT.ZBBRFVUPFVZLFHNTV}.
  \end{center}
  Eve knows that Alice uses a Hill cipher with block length 3, but she
  does not know the secret encryption matrix. Eve also knows that
  Alice begins all of her correspondence with ``My dear
  love''. Decrypt the message.
\end{example}

\begin{solution}
  The first three blocks of the ciphertext are $\q{EFNOR.AHI}$, i.e.,
  \begin{equation*}
    \mbox{Ciphertext blocks:}\quad
    (5,6,14),\
    (15,18,28),\
    (1,8,9).
  \end{equation*}
  Eve also knows that the first three blocks of the plaintext are
  $\q{MY DEAR L}$, i.e.,
  \begin{equation*}
    \mbox{Plaintext blocks:}\quad
    (13,25,0),\
    (4,5,1),\
    (18,0,12).
  \end{equation*}
  These facts allow Eve to deduce the following information about the
  unknown decryption matrix $A^{-1}$:
  \begin{equation*}
    A^{-1} \begin{mymatrix}{c} 5 \\ 6 \\ 14 \end{mymatrix}
    = \begin{mymatrix}{c} 13 \\ 25 \\ 0 \end{mymatrix},\quad
    A^{-1} \begin{mymatrix}{c} 15 \\ 18 \\ 28 \end{mymatrix}
    = \begin{mymatrix}{c} 4 \\ 5 \\ 1 \end{mymatrix},\quad
    A^{-1} \begin{mymatrix}{c} 1 \\ 8 \\ 9 \end{mymatrix}
    = \begin{mymatrix}{c} 18 \\ 0 \\ 12 \end{mymatrix}.
  \end{equation*}
  Since Eve remembers the column method of matrix multiplication, she
  knows that these three equations can be written as a single equation
  in matrix form:
  \begin{equation*}
    A^{-1} \begin{mymatrix}{ccc}
      5 & 15 & 1 \\
      6 & 18 & 8 \\
      14 & 28 & 9 \\
    \end{mymatrix}
    = \begin{mymatrix}{ccc}
      13 & 4 & 18 \\
      25 & 5 & 0 \\
      0 & 1 & 12 \\
    \end{mymatrix}.\quad
  \end{equation*}
  Note that this equation is of the form $A^{-1}C=P$. (Here, $C$
  stands for ``ciphertext'' and $P$ for ``plaintext''). Multiplying
  both sides of the equation by $C^{-1}$ on the right, we get
  $A^{-1} = PC^{-1}$. Thus, assuming that $C$ is invertible, Eve can
  easily compute the decryption matrix $A^{-1}$. Eve computes:
  \begin{equation*}
    C^{-1}
    =
    \begin{mymatrix}{ccc}
      5 & 15 & 1 \\
      6 & 18 & 8 \\
      14 & 28 & 9 \\
    \end{mymatrix}^{-1}
    =
    \begin{mymatrix}{ccc}
      19 & 8 & 23 \\
      0 & 5 & 2 \\
      22 & 1 & 0 \\
    \end{mymatrix}.
  \end{equation*}
  This allows Eve to compute the decryption matrix:
  \begin{equation*}
    A^{-1}
    =
    PC^{-1}
    =
    \begin{mymatrix}{ccc}
      13 & 4 & 18 \\
      25 & 5 & 0 \\
      0 & 1 & 12 \\
    \end{mymatrix}
    \begin{mymatrix}{ccc}
      19 & 8 & 23 \\
      0 & 5 & 2 \\
      22 & 1 & 0 \\
    \end{mymatrix}
    =
    \begin{mymatrix}{ccc}
      5  & 26 & 17 \\
      11 & 22 & 5 \\
      3  & 17 & 2 \\
    \end{mymatrix}.
  \end{equation*}
  Armed with the decryption matrix $A^{-1}$, Eve can now decrypt
  Alice's entire message, using the same method as in
  Example~\ref{exa:hill-cipher-decryption}. The plaintext is ``My dear
  love, run away with me at midnight''.
\end{solution}

As the example shows, the Hill cipher is not secure at all. The main
problem is that the cipher is {\em linear}, i.e., each component of a
ciphertext block is a simple linear combination of the components of
the plaintext block. This linearity property enables Eve to break the
cipher by solving a system of linear equations.

For this reason, all modern block ciphers have a non-linear
component. Often this takes the form of so-called \textbf{S-boxes}%
\index{S-box}%
\index{cipher!S-box}. An S-box is an operation that scrambles the
symbols of the alphabet in a non-linear way.  For example, consider
the following S-box, which is an operation from $\Z_{29}$ to
$\Z_{29}$:
\begin{center}
  \tabcolsep=0.4ex\def\arraystretch{1.4}
  \begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|}
    \hline
    $x$ & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 20 & 21 & 22 & 23 & 24 & 25 & 26 & 27 & 28 \\\hline
    ~$S(x)$~ & 17 & 9 & 27 & 2 & 20 & 12 & 21 & 26 & 16 & 18 & 4 & 24 & 23 & 7 & 19 & 14 & 28 & 29 & 1 & 15 & 10 & 22 & 6 & 5 & 25 & 11 & 13 & 3 & 8 \\\hline
  \end{tabular}
\end{center}
The inputs of the S-box are shown in the top row, and the
corresponding outputs in the bottom row.  For example, this S-box maps
the input $7$ to the output $26$. We write $S(7)=26$.

\begin{definition}{A toy block cipher}{toy-block-cipher}
  Consider the following block cipher on the alphabet $\Z_{29}$ with
  block size $3$. The key consists of $12$ elements $k_1,\ldots,k_{12}$
  of $\Z_{29}$. To encrypt a plaintext block, regard the block as a
  $3$-dimensional column vector. Then repeat the following steps $3$
  times. All operations are carried out modulo $29$.
  \begin{itemize}
  \item Key mixing: add the next three components of the key to the
    components of the vector.
  \item Diffusion: multiply the vector by the fixed $3\times 3$-matrix
    $A = \begin{mymatrix}{ccc}
      1 & 2 & 3 \\
      3 & 1 & 2 \\
      2 & 3 & 1 \\
    \end{mymatrix}$.
  \item S-box application: apply the S-box to each component of the
    vector.
  \end{itemize}
  Finally, apply one more key mixing step at the end. The resulting
  vector is the ciphertext block. The cipher can be visualized as
  follows:
  \begin{equation*}
    \begin{tikzpicture}[xscale=1.1,
      a/.style={fill=red!20},
      s/.style={fill=yellow!30},
      k/.style={fill=green!20}]
      \draw (0,2) -- (11,2);
      \draw (0,1) node[left=2ex]{plaintext} -- (11,1) node[right=2ex]{ciphertext};
      \draw (0,0) -- (11,0);
      \draw[k] (1,2) +(-0.35,-0.35) rectangle node{$k_1$} +(0.35,0.35);
      \draw[k] (1,1) +(-0.35,-0.35) rectangle node{$k_2$} +(0.35,0.35);
      \draw[k] (1,0) +(-0.35,-0.35) rectangle node{$k_3$} +(0.35,0.35);
      \draw[a] (2,0) +(-0.35,-0.35) rectangle node{$A$} +(0.35,2.35);
      \draw[s] (3,2) +(-0.35,-0.35) rectangle node{$S$} +(0.35,0.35);
      \draw[s] (3,1) +(-0.35,-0.35) rectangle node{$S$} +(0.35,0.35);
      \draw[s] (3,0) +(-0.35,-0.35) rectangle node{$S$} +(0.35,0.35);
      \draw[k] (4,2) +(-0.35,-0.35) rectangle node{$k_4$} +(0.35,0.35);
      \draw[k] (4,1) +(-0.35,-0.35) rectangle node{$k_5$} +(0.35,0.35);
      \draw[k] (4,0) +(-0.35,-0.35) rectangle node{$k_6$} +(0.35,0.35);
      \draw[a] (5,0) +(-0.35,-0.35) rectangle node{$A$} +(0.35,2.35);
      \draw[s] (6,2) +(-0.35,-0.35) rectangle node{$S$} +(0.35,0.35);
      \draw[s] (6,1) +(-0.35,-0.35) rectangle node{$S$} +(0.35,0.35);
      \draw[s] (6,0) +(-0.35,-0.35) rectangle node{$S$} +(0.35,0.35);
      \draw[k] (7,2) +(-0.35,-0.35) rectangle node{$k_7$} +(0.35,0.35);
      \draw[k] (7,1) +(-0.35,-0.35) rectangle node{$k_8$} +(0.35,0.35);
      \draw[k] (7,0) +(-0.35,-0.35) rectangle node{$k_9$} +(0.35,0.35);
      \draw[a] (8,0) +(-0.35,-0.35) rectangle node{$A$} +(0.35,2.35);
      \draw[s] (9,2) +(-0.35,-0.35) rectangle node{$S$} +(0.35,0.35);
      \draw[s] (9,1) +(-0.35,-0.35) rectangle node{$S$} +(0.35,0.35);
      \draw[s] (9,0) +(-0.35,-0.35) rectangle node{$S$} +(0.35,0.35);
      \draw[k] (10,2) +(-0.35,-0.35) rectangle node{$k_{10}$} +(0.35,0.35);
      \draw[k] (10,1) +(-0.35,-0.35) rectangle node{$k_{11}$} +(0.35,0.35);
      \draw[k] (10,0) +(-0.35,-0.35) rectangle node{$k_{12}$} +(0.35,0.35);
  \end{tikzpicture}
\end{equation*}
\end{definition}

Note that the three basic steps (key mixing, diffusion, and S-box
application) are repeated several times; each such repetition is
called a \textbf{round}%
\index{cipher!round}%
\index{rounds of a cipher} of the block cipher.  The more rounds a
block cipher has, the better its diffusion and non-linearity
properties. The final round is short: it only consists of a key
mixing step, with no final diffusion or S-box application. The reason
is that performing a final diffusion and S-box application would not
add anything to the security of the cipher. An attacker could simply
undo these last two steps, since they do not depend on the key.

The matrix $A$ is called the \textbf{diffusion matrix}%
\index{diffusion matrix}%
\index{matrix!diffusion}%
\index{cipher!diffusion matrix} of the cipher. Note that, unlike for
the Hill cipher, the matrix $A$ is fixed once and for all and is not
part of the key. Instead, the key consists of scalars that are added
to the current block at the beginning of each round.

\begin{example}{Toy block cipher: encryption}{toy-block-cipher-encryption}
  Encrypt the message ``I like math'' using the block cipher of
  Definition~\ref{def:toy-block-cipher} and the key
  $1,1,3,3,5,5,7,7,9,9,11,11$.
\end{example}

\begin{solution}
  We first represent the plaintext as a sequence of blocks, padding
  the final block with zeros:
  \begin{equation*}
    \mbox{Plaintext blocks:}\quad
    (9,0,12),\
    (9,11,5),\
    (0,13,1),\
    (20,8,0).
  \end{equation*}
  To encrypt the first block, we start with the vector
  $\mat{9,0,12}^T$ and apply the following steps:

  \noindent{\bf Round 1:}
  \begin{itemize}
  \item Key mixing: the first three components of the key are
    $1,1,3$. We add them to the plaintext.
    \begin{equation*}
      \begin{mymatrix}{c} 9 \\ 0 \\ 12 \end{mymatrix}
      +
      \begin{mymatrix}{c} 1 \\ 1 \\ 3 \end{mymatrix}
      ~=~
      \begin{mymatrix}{c} 10 \\ 1 \\ 15 \end{mymatrix}.
    \end{equation*}
  \item Diffusion: multiply by the matrix $A$.
    \begin{equation*}
      \begin{mymatrix}{ccc}
        1 & 2 & 3 \\
        3 & 1 & 2 \\
        2 & 3 & 1 \\
      \end{mymatrix}
      \begin{mymatrix}{c} 10 \\ 1 \\ 15 \end{mymatrix}
      ~=~
      \begin{mymatrix}{c} 28 \\ 3 \\ 9 \end{mymatrix}.
    \end{equation*}
  \item S-box application: apply the S-box to each component of the
    vector.
    \begin{equation*}
      \begin{mymatrix}{c} S(28) \\ S(3) \\ S(9) \end{mymatrix}
      ~=~
      \begin{mymatrix}{c} 8 \\ 2 \\ 18 \end{mymatrix}.
    \end{equation*}
  \end{itemize}

  \noindent{\bf Round 2:}
  \begin{itemize}
  \item Key mixing: the next three components of the key are
    $3,5,5$.
    \begin{equation*}
      \begin{mymatrix}{c} 8 \\ 2 \\ 18 \end{mymatrix}
      +
      \begin{mymatrix}{c} 3 \\ 5 \\ 5 \end{mymatrix}
      ~=~
      \begin{mymatrix}{c} 11 \\ 7 \\ 23 \end{mymatrix}.
    \end{equation*}
  \item Diffusion:
    \begin{equation*}
      \begin{mymatrix}{ccc}
        1 & 2 & 3 \\
        3 & 1 & 2 \\
        2 & 3 & 1 \\
      \end{mymatrix}
      \begin{mymatrix}{c} 11 \\ 7 \\ 23 \end{mymatrix}
      ~=~
      \begin{mymatrix}{c} 7 \\ 28 \\ 8 \end{mymatrix}.
    \end{equation*}
  \item S-box application:
    \begin{equation*}
      \begin{mymatrix}{c} S(7) \\ S(28) \\ S(8) \end{mymatrix}
      ~=~
      \begin{mymatrix}{c} 26 \\ 8 \\ 16 \end{mymatrix}.
    \end{equation*}
  \end{itemize}

  \noindent{\bf Round 3:}
  \begin{itemize}
  \item Key mixing: the next three components of the key are
    $7,7,9$.
    \begin{equation*}
      \begin{mymatrix}{c} 26 \\ 8 \\ 16 \end{mymatrix}
      +
      \begin{mymatrix}{c} 7 \\ 7 \\ 9 \end{mymatrix}
      ~=~
      \begin{mymatrix}{c} 4 \\ 15 \\ 25 \end{mymatrix}.
    \end{equation*}
  \item Diffusion:
    \begin{equation*}
      \begin{mymatrix}{ccc}
        1 & 2 & 3 \\
        3 & 1 & 2 \\
        2 & 3 & 1 \\
      \end{mymatrix}
      \begin{mymatrix}{c} 4 \\ 15 \\ 25 \end{mymatrix}
      ~=~
      \begin{mymatrix}{c} 22 \\ 19 \\ 20 \end{mymatrix}.
    \end{equation*}
  \item S-box application:
    \begin{equation*}
      \begin{mymatrix}{c} S(22) \\ S(19) \\ S(20) \end{mymatrix}
      ~=~
      \begin{mymatrix}{c} 6 \\ 15 \\ 10 \end{mymatrix}.
    \end{equation*}
  \end{itemize}

  \noindent{\bf Round 4 (the final round is abbreviated):}
  \begin{itemize}
  \item Key mixing: the next three components of the key are
    $9,11,11$.
    \begin{equation*}
      \begin{mymatrix}{c} 6 \\ 15 \\ 10 \end{mymatrix}
      +
      \begin{mymatrix}{c} 9 \\ 11 \\ 11 \end{mymatrix}
      ~=~
      \begin{mymatrix}{c} 15 \\ 26 \\ 21 \end{mymatrix}.
    \end{equation*}
  \end{itemize}
  Therefore, the first ciphertext block is $(15,26,21)$. We repeat the
  same procedure with the remaining plaintext blocks, and obtain the
  following ciphertext blocks:
  \begin{equation*}
    \mbox{Ciphertext blocks:}\quad
    (15,26,21),\
    (7,24,1),\
    (2,16,23),\
    (7,20,22).
  \end{equation*}
  The corresponding ciphertext is $\q{OZUGXABPWGTV}$.
\end{solution}

Are ciphers like this actually used in the real world? The answer
is yes. While the cipher of Definition~\ref{def:toy-block-cipher} is
greatly simplified, it has the same basic structure as modern
real-world block ciphers (such as AES, the Advanced Encryption
Standard). Naturally, these real-world ciphers differ in some details,
such as the alphabet size, the block size, the number of rounds, the
design of the S-boxes, the way the key is computed, and the precise
order in which the operations are applied. However, their basic
structure is very similar to our toy cipher, and indeed, all such
ciphers rely on key mixing, diffusion, and non-linear S-boxes as
their key components.

For example, AES uses an alphabet size of $256$ instead of $29$ (i.e.,
it operates on bytes%
\index{byte}, rather than elements of $\Z_{29}$). Although $\Z_{256}$
is not a field (because $256$ is not prime), it nevertheless turns out
that there exists a field with $256$ elements, and AES uses it for its
algebraic operations. Our toy cipher's block size of $3$ is much too
small to achieve effective diffusion; modern real-world ciphers use
block sizes between $16$ and $32$ bytes ($128$ to $256$ bits). The
design of the S-boxes is a bit of a black art; at minimum, they must
be designed to withstand two common types of cryptanalysis known as
\textbf{linear cryptanalysis}%
\index{cryptanalysis!linear}%
\index{linear cryptanalysis} and \textbf{differential cryptanalysis}%
\index{cryptanalysis!differential}%
\index{differential cryptanalysis}.  Among other things, this means
that the S-box should be ``as far from linear'' as possible.

A detailed discussion of the design and cryptanalysis of modern block
ciphers is far beyond the scope of this book, but we hope that you
have gotten a taste of this fascinating subject, and the role that
linear algebra over finite fields plays in it.