\subsection{Introduction}

\begin{frame}
  \frametitle{Simplex Algorithm}

  Invented by Tobias Dantzig around 1950 \\
  \vfill
  Used to solve optimization problems in linear programming \\
  \vfill
  Greg Nelson was the first to employ it for
  constraint solving, AFAIK, around 1980
  \vfill
  Difference is that in linear programming input problem is
  feasible and one looks for optimum. In constraint solving
  problem can be infeasible, and we are interested in
  finding any solution

\end{frame}

\begin{frame}
  \frametitle{Introduction}

Linear Rational Arithmetic \Lra consists in solving
Boolean combinations of atoms of the form
$$
\sum_{j=1}^n a_j x_j \leq b\quad\quad\quad\quad\sum_{j=1}^n a_j x_j \geq b
$$
where $a_j$ are constants (coefficients), $x_j$ are
variables, and $b$ is a constant (bound). The domain
of $a_j$, $x_j$, $b$ is that of rationals.
\vfill
\pause
Notice that the following translations hold
\begin{itemize}
  \item $\sum_{j=1}^n a_j x_j = b     \quad\quad\Longrightarrow\quad\quad  (\sum_{j=1}^n a_j x_j \leq  b) \wedge (\sum_{j=1}^n a_j x_j \geq b)$
  \item $\sum_{j=1}^n a_j x_j < b     \quad\quad\Longrightarrow\quad\quad$ see later
  \item $\sum_{j=1}^n a_j x_j > b     \quad\quad\Longrightarrow\quad\quad$ see later
  \item $\sum_{j=1}^n a_j x_j \not= b \quad\quad\Longrightarrow\quad\quad  (\sum_{j=1}^n a_j x_j < b) \vee (\sum_{j=1}^n a_j x_j > b)$
\end{itemize}

\end{frame}

\subsection{Preprocessing}

\begin{frame}
  \frametitle{Preprocessing}

  In the SMT setting, we are given a formula $\varphi$ like
  $$
  (x \geq 0) \wedge ((x + y \leq 2) \vee (x + 2y - z \geq 6)) \wedge ((x + y \geq 2) \vee (2y - z \leq 4))
  $$
  We perform a {\bf preprocessing step}, in order to separate the formula into a set of {\bf equations}
  and a set of simple {\bf bounds}. This is done by introducing {\bf fresh} variables.
  \pause
  \vfill
  The formula above $\varphi$ is equivalent to (the conjunction of)
  $$
  \begin{array}{lc}
    (x \geq 0) \wedge ((s_1 \leq 2) \vee (s_2 \geq 6)) \wedge ((s_1 \geq 2) \vee (s_3 \leq 4)) & \onslide<3>{\varphi'} \\
    \\
    (s_1 = x + y) \\
    (s_2 = x + 2y - z) & \onslide<3>{A\vec{x}=\vec{0}} \\
    (s_3 = 2y - z)
  \end{array}
  $$

\end{frame}

\begin{frame}
  \frametitle{Preprocessing}
  In general, from a formula $\varphi$, we end up in a rewritten formula of the kind
  $$
  \varphi' \ \wedge\ A\vec{x} = \vec{0}
  $$
  where $\varphi'$ is a Boolean combination of {\bf bounds}, while $A\vec{x}=\vec{0}$ is a system of {\bf equations}
  of the form
  $$
  \begin{array}{rcl}
    a_{11} x_1 + \ldots + a_{1n} x_n & = & 0 \\
    a_{21} x_1 + \ldots + a_{2n} x_n & = & 0 \\
    \ldots \\
    a_{i1} x_1 + \ldots + a_{in} x_n & = & 0 \\
    \ldots \\
    a_{m1} x_1 + \ldots + a_{mn} x_n & = & 0 \\
  \end{array}
  $$
  \vfill
  \pause
  Now we detach $A\vec{x} = \vec{0}$ from the formula, and we store it into the \tsolver
  permanently. The \satsolver will work only on $\varphi'$. Therefore {\bf the constraints
  that are pushed into and popped from the \tsolver are just bounds}

\end{frame}


\section{Groups}
\subsection{Basics}
\begin{definition}
    A group is a pair $(G,\cdot)$ consisting of a set $G$ and a function $\cdot:G\times G\to G$ such that\\
    1. $\forall a,b,c\in G,(a\cdot b)\cdot c=a\cdot(b\cdot c)$.\\
    2. $\exists e\in G,\forall g\in G,e\cdot g=g\cdot e=e$.\\
    3. $\forall g\in G,\exists g^{-1}\in G,g\cdot g^{-1}=g^{-1}\cdot g=e$.
\end{definition}
\begin{remark}
    1. To check something is a group, we need also to check that $\cdot$ is well-defined.
    This is sometimes called the closure axiom, i.e. $\forall a,b\in G, a\cdot b\in G$.\\
    2. If using additive notation, we write $0$ for $e$ and if we are using mutiplicative notation we write $1$ for $e$.
\end{remark}
\begin{definition}
    For a group $(G,\cdot)$, a subset $H\subset G$ is a subgroup if $(H,\cdot|_{H\times H})$ is a well-defined group.
    In this case, we write $H\le G$.
\end{definition}
\begin{remark}
    A nonempty subset $H\subset G$ is a subgroup iff $\forall a,b\in H,ab^{-1}\in H$.
\end{remark}
\begin{example}
    1. $(\mathbb Z,+)\le(\mathbb Q,+)\le(\mathbb R,+)\le\cdots$.\\
    2. The cyclic group $C_n$ of order $n$ and the dihedral group $D_{2n}$ of isometries preserving a regular $n$-gon.\\
    3. Symmetric groups $S_n$ of the permutations of $n$ letters and alternatig groups $A_n$ containing all even permutations of those $n$ letters.\\
    4. The quarterion group $Q_8=\{\pm 1,\pm i,\pm j,\pm k\}$.\\
    5. The group $\operatorname{GL}_n(\mathbb F)$ of all invertible matrices over a field $\mathbb F$ is a group.
    We can also get $\operatorname{SL}_n(\mathbb F)$ consisting of those with determinant $1$.
\end{example}
\begin{definition}
    The direct product of groups $G,H$ is a group $G\times H$ under the operation
    $$\forall g,g'\in G,h,h'\in H,(g,h)(g',h')=(gg',hh')$$
\end{definition}
For a subgroup $H$ of $G$, the left cosets of $H$ are the sets of the form $gH,g\in G$.
It is obvious that cosets partition $G$ and all of them have the same cardinality.
So immediately,
\begin{theorem}[Lagrange's Theorem]
    If $G$ is finite and $H\le G$, then $|H|$ divides $|G|$.
\end{theorem}
\begin{definition}
    The value $|G|/|H|$ is called the index $|G:H|$ of $H$ in $G$.
\end{definition}
There is a partial converse to this theorem which we shall introduce soon.
\begin{theorem}[First Sylow's Theorem]
    If $|G|=p^nm$ where $p$ is a prime and $p\nmid m$, there is a subgroup $H\le G$ such that $|H|=p^n$
\end{theorem}
From which Cauchy's Theorem is immediate.
\begin{definition}
    For $g\in G$, the least $n$ such that $g^n=1$ is called the order of $g$.
    If there is no such integer, we say $g$ has infinite order.
\end{definition}
\begin{remark}
    1. If $g$ has order $d$, then $g^n=1\iff d|n$.\\
    2. $n||G|$ by considering the subgroup $\langle g\rangle$ generated by $g$.
\end{remark}
\begin{definition}
    A subgroup $H\le G$ is called a normal subgroup of $G$, written as $H\unlhd G$, if $\forall g\in G,gHg^{-1}=H$.
\end{definition}
\begin{proposition}
    If $H\unlhd G$, then the set of left cosets $gH$ is a group, called the quotient group $G/H$, under the operation $(aH)(bH)=abH$.
\end{proposition}
\begin{proof}
    Trivial to check that the operation is well-defined.
    The rest is obvious.
\end{proof}
\subsection{The Isomorphism Theorems}
\begin{definition}
    Let $G,H$ be groups, a map $\phi:G\to H$ is called a group homomorphism if $\forall g,g'\in G,\phi(gg')=\phi(g)\phi(g')$.
    The kernel of $\phi$ is defined as $\ker\phi=\{g\in G:\phi(g)=1\}\le G$.
    The image of $\phi$ is defined as $\operatorname{Im}\phi=\{h\in H:\exists g\in G,\phi(g)=h\}\le H$
\end{definition}
Obviously $\ker\phi\unlhd G$.
The concerse is also true: any normal subgroup of $G$ is the kernel of the canonical projection of $G$ to the quotient.
\begin{definition}
    An isomorphism is a bijective homomorphism.
    We say $G,H$ are isomorphic, or $G\cong H$, if there is an isomorphism between them.
\end{definition}
\begin{proposition}
    If $\phi$ is an isomorphism, so is $\phi^{-1}$.
\end{proposition}
\begin{proof}
    Trivial.
\end{proof}
\begin{theorem}[Isomorphism Theorem]
    Let $\phi:G\to H$ be a group homomorphism, then $G/\ker\phi\cong\operatorname{Im}\phi$.
\end{theorem}
\begin{proof}
    The map $\tilde{\phi}:G/\ker\phi\to\operatorname{Im}\phi$ by $g\ker\phi\mapsto \phi(g)$ is a well-defined isomorphism.
\end{proof}
\begin{example}
    The function $\exp:\mathbb C\to\mathbb C^\star$ is a homomorphism with kernel
    $$\ker\exp=\{z\in C:\exp(z)=1\}=2\pi\mathbb Z,\operatorname{Im}\exp=\mathbb C^\star$$
    Hence $\mathbb C/2\pi\mathbb Z=\mathbb C^\star$.
\end{example}
The Isomorphism Theorem is sometimes called the \textit{First} Isomorphism Theorem.
There are other isomorphism theorems, but they are all corollaries of the first one.
\begin{corollary}[Second Isomorphism Theorem]
    Consider a group $G$ and subgroups $H\le G,K\unlhd G$, then the set $HK=\{hk:h\in H,k\in K\}$ is a subgroup of $G$.
    Also $H\cap K\unlhd H$.
    Then we have $HK/K\cong H/H\cap K$.
\end{corollary}
\begin{proof}
    The (normal) subgroup conditions in $HK,H\cap K$ are trivial.
    The function $\phi:H\to G/K$ by $h\mapsto hK$ is a homomorphism because it is the composition of the inclusion $H\to G$ and the projection $G\to G/K$.
    So immediately $\ker\phi=H\cap K$ and $\operatorname{Im}\phi=HK/K$, and the result follows.
\end{proof}
\begin{remark}
    There is a natural bijection from the subgroups of $G/K$ and the subgroups of $G$ containing $K$ by $X\mapsto \{g\in G:gK\in X\}$.
    How about normal subgroups?
    Turns out we have the Third Isomorphism Theorem to describe this.
\end{remark}
\begin{corollary}[Third Isomorphism Theorem]
    Suppose $H,K$ are normal subgroups of $G$ and $K\subset H$.
    SO $K\unlhd H$ and $H/K\unlhd G/K$ with
    $$G/H\cong (G/K)/(H/K)$$
\end{corollary}
\begin{proof}
    Let $\phi:G/K\to G/H$ by $gK\mapsto gH$ which is clearly a well-defined surjective homomorphism, then $\ker\phi=H/K$.
    The result then follows from the First Isomorphism Theorem.
\end{proof}
\subsection{Simple Groups}
If $K\unlhd G$, then the study of the groups $K$ and $G/K$ gives some information about $G$ (but not all).
But sometimes this approach fails due to the lack of normal proper subgroups.
\begin{definition}
    A group is called simple if it has no normal proper subgroups.
\end{definition}
\begin{lemma}
    An abelian group $G$ is simple if and only if it is isomorphic to the cyclic group of order $p$ for some prime $p$.
\end{lemma}
\begin{proof}
    An abelian group is simple if and only if it has no proper subgroup.
    It is obvious that for any prime $p$, $C_p$ has no proper subgroup due to Lagrange's Theorem, so we proceed to the converse.
    Note that any nonidentity element $x\in G$ must generate $G$, otherwise the subgroup generated by it will be a proper subgroup of $G$.
    However this means that $G$ is cyclic, so immediately to make it simple $G$ must be isomorphic to $C_p$ for some prime $p$.
\end{proof}
\begin{lemma}
    If $G$ is a finite group, then it has a composition series
    $$1=G_0\lhd G_1\lhd\cdots\lhd G_m=G$$
    with $G_{i}/G_{i-1}$ is simple for each $i$.
\end{lemma}
\begin{proof}
    Induction on $|G|$.
    By the Third Isomorphism Theorem a normal proper subgroup $H\lhd G$ with maximal order must have $G/H$ simple, so the result follows.
\end{proof}


\documentclass{article}

\usepackage[margin=1.0in]{geometry}
\usepackage{amsmath}
\author{Aayush Arya \\ (tomriddle257@gmail.com)}
\title{Introduction to Ordinary Differential Equations}
\begin{document}
	\maketitle

	\section*{Introduction}
	When we solve an algebraic equation $3x + 5 = 8$, we seek a number. The purpose of solving a differential equation such as
	$$ \frac{d^2x}{dt^2} = -\frac{k}{m}x$$

	the goal is to find a function x(t) that satisfies it.

	A differential equation of a function y(x) has the general form
	$$ f(x, y(x), y'(x), y''(x), ..., y^n(x)) = 0$$

	\section*{Nature of Solutions}
	For a differential equation
		$$ y'' + 2y' + 5 = 0$$
		if $y = g(x)$ and $y = h(x)$ are two solutions of the differential equations, then a linear combination of these, i.e.,
		$$ y = c_1g(x) + c_2h(x) $$
		is also a solution.\\

	A differential equation of order $n$ has $n$ independent solutions. We can form a general solution of the differential equation using a linear combination of the independent solutions. \textit{Picard's Existence and Uniqueness Theorem} helps us figure out that our general solution is complete and no other solutions are left.\\ But how do we know that we have found a solution? The hallmark sign is that no derivatives of $y$ will be left in the equation.
	The equation will relate $y$ with $x$ at least implicitly.

	\section*{Geometric interpretation}
	The geometric interpretation of differential equations is easier to see for linear equations.\\
	A differential equation of the form
	\begin{equation}
	\frac{dy}{dx} = f(x,y)
	\end{equation}
	can be interpreted as a set of vectors pointing towards (1, $\frac{dy}{dx}$) at every point (x,y) in the plane.% \footnote{See Chapter 1 of Simmons-Krantz}
	\\
	Consider the differential equation
	$$ y''(x) = 0 $$
	Solving this gives
	$$ y' = c_1 $$
	$$ y = c_1x + c_2 $$
	Which looks like the familiar equation $y = mx + c$. We note that these arbitrary constants can take up any value. So the equation doesn't represent 'a' straight line but a family of straight lines (of having all possible slope values and y-intercepts).

	\section{Solving first order equations}
	\subsection*{Exact Differential Equations}
	An exact or total differential of a function $\psi(x,y)$ is one which shows the contribution of all independent variables for an infinitesimal change
	\begin{equation}
	d\psi = \frac{\partial\psi}{\partial x}dx + \frac{\partial\psi}{\partial y}dy
	\end{equation}
	A differential equation
	$$M(x,y)dx + N(x,y)dy = 0$$ is exact if it can be represented as the exact differential of some function $\psi (x,y)$ where $d\psi (x,y) = 0$
	\\
	That is,
	$$M = \frac{\partial\psi}{\partial x} $$ and $$N = \frac{\partial\psi}{\partial y} $$
	A natural question arises: it would be easy to check the exactness for trivial expressions. Can we write a criterion that assures the exactness of the equation?\\

	The equation is exact if
	$$ \frac{\partial M}{\partial y} = \frac{\partial N}{\partial x}$$
	because, if we substitute the expressions for M and Y as partial derivatives of $\psi$, the criterion becomes

	$$\frac{\partial^2\psi}{\partial y \partial x} = \frac{\partial^2\psi}{\partial x \partial y}$$

	If the equation is exact, it is easy to see that if $d\psi=0$, the solution is just $\psi = constant$

	To find $\psi$, we first integrate M(x,y)

	$$\psi = \int \frac{\partial \psi}{\partial x}dx + f(y)$$
	The constant of integration is a function of y alone. This can be justified by intuition, because if we take the partial derivative of $\psi$ with respect to x, the terms containing functions of y alone would have vanished. We'd like to bring those back.
	\\

	A more rigorous proof for this justification is as follows\\

	As we said before,
	$$ N(x,y) = \frac{\partial\psi}{\partial y}$$

	substituting the value of $\psi$ from the equation above

	$$ N(x,y) = \frac{\partial\psi}{\partial y} \left(\int \frac{\partial \psi}{\partial x}dx + f(y)\right) $$

	taking the partial derivative and transposing for f'(y)

	$$ f'(y) = N(x,y) - \frac{\partial\psi}{\partial y} \left(\int \frac{\partial \psi}{\partial x}dx\right) $$

	taking the partial derivate with respect to x leaves us with

	$$ \frac{\partial f'(y)}{\partial x} = \frac{\partial N}{\partial x} - \frac{\partial^2 }{\partial x \partial y}\left(\int \frac{\partial \psi}{\partial x}dx \right) $$

	which on simplifying, becomes

	$$ \frac{\partial f'(y)}{\partial x} = \frac{\partial N}{\partial x} - \frac{\partial M}{\partial y} $$

	from the condition of exactness, we know that the R.H.S. is zero. Therefore

	$$f'(y) = C$$

	which means $f(y)$ is some function of y alone.

	\subsection{First Order Linear Differential Equations}
	These are a special case of inexact equations. The general form of these equations is
		\begin{equation}
	\label{lineareqn}
	y'(x) + P(x)y = Q(x)
	\end {equation}
	These equations can be made exact by multiplying by a suitable integrating factor such that

	$$ \frac{d(\mu y)}{dx} = \mu Q(x)$$
	and the solution is just
	\begin{equation}
	\boxed{\mu (x) y = \int \mu(x) Q(x)}
	\end{equation}
	$\mu$ can be calculated easily. If we multiply equation
	\ref{lineareqn} with $\mu(x)$

	$$ \frac{d(\mu y)}{dx} = \mu \frac{dy}{dx} + \mu (x) P(x) $$
	which becomes
	$$ \mu\frac{dy}{dx} + y\frac{d\mu}{dx} = \mu \frac{dy}{dx} + \mu (x) P(x) $$
	simplifying gives
	$$y\frac{d\mu}{dx} = \mu (x)P(x) $$
	Rearranging and integrating gives
	\begin{equation*}
		\boxed{\mu = e^{\int P(x)dx}}
	\end{equation*}
	\subsection{Homogenous equations}
	An equation $f(x,y)=0$
	\subsection{Special Forms}
	If the equation is of the form $$yM(xy)dx + xN(xy)dy)$$

then its integrating factor $\mu$ is given by

$$ \mu = \frac{1}{M(xy) - N(xy)dy} $$

\subsubsection{Example}
The differential equation
$$ y(1+xy)dx + x(1-xy)dy = 0$$
has the integrating factor
 $$ \mu = \frac{1}{Mx+ Ny} $$
$$\mu = \frac{1}{xy + (xy)^2 + xy - (xy)^2} $$

$$\mu = \frac{1}{2xy} $$

Multiplying our equation with $\mu$ gives

$$\frac{1}{2xy}\cdot \left(y(1+xy)dx + x(1-xy)dy\right) = 0 $$

which can be written as

$$\frac{(y + xy^2)dx + (x - x^2y)dy}{2xy} = 0 $$

simplifying and integrating,

$$ \int(\frac{1}{2x} + \frac{y}{2})dx + \int(\frac{1}{2y} + \frac{x}{2})dy = 0$$

which is

$$log(x) + \frac{y^2}{2} + log(y) + \frac{x^2}{2} = 0 $$

which can be further condensed

$$ log(xy)^2 + x^2 + y^2 = C$$

or

$$ log(x^2y^2) + x^2 + y^2 = C$$
\end{document}


\subsection{Variance of the IV estimator}

In OLS we had:

\(\hat {\theta_{OLS}} = (X^TX)^{-1}X^Ty\)

\(Var [\hat {\theta_{OLS}}]=(X^TX)^{-1}X^T\Omega X(X^TX)^{-1}\)

With IV we have

\(\hat {\theta_{IV}} = (Z^TX)^{-1}Z^Ty\)

\(Var [\hat {\theta_{IV}}]=(Z^TX)^{-1}Z^T\Omega Z(Z^TX)^{-1}\)

We can use weighted least squares for \(\Omega \).


\documentclass[twoside]{MATH77}
\usepackage{multicol}
\usepackage[fleqn,reqno,centertags]{amsmath}
\begin{document}
\hyphenation{DRDHERQL DRSHERQL ODSHERQL}
\begmath 5.2 Eigenvalues and Eigenvectors of a Hermitian Complex Matrix

\silentfootnote{$^\copyright$1997 Calif. Inst. of Technology, \thisyear \ Math \`a la Carte, Inc.}

\subsection{Purpose}

Compute the N eigenvalues and right eigenvectors of an N $\times $ N
complex Hermitian matrix $A$. A complex matrix is Hermitian if its
diagonal elements are real and its off-diagonal pairs $a_{i,j}$ and
$a_{j,i}$ are complex conjugates of each other. Such a matrix will
have real eigenvalues. The eigenvectors will in general be complex
but can be chosen so that the matrix $V$ of N eigenvectors is
unitary, $i.e.$, the conjugate transpose of $V$ is the inverse of
$V$.

\subsection{Usage}

\subsubsection{Program Prototype}

\begin{description}
\item[REAL]  \ {\bf AR}(LDA,$\geq $N){\bf , AI}(LDA,$\geq $N) [LDA$\geq $N]
{\bf ,\newline
EVAL}($\geq $N)

\item[REAL]  \ {\bf VR}(LDA,$\geq $N){\bf , VI}(LDA,$\geq $N){\bf , WORK}($%
\geq $3N)

\item[INTEGER]  \ {\bf LDA, N, IERR}
\end{description}

Assign values to AR(,), AI(,), LDA, and N.

\begin{center}
\fbox{\begin{tabular}{@{\bf }c}
CALL SHERQL(AR, AI, LDA, N,\\
EVAL, VR, VI, WORK, IERR)\\
\end{tabular}}
\end{center}

Results are returned in EVAL(), VR(,), VI(,), and IERR.

\subsubsection{Argument Definitions}

\begin{description}
\item[AR(,), AI(,)]  \ [inout] On entry the locations on and below the
diagonal of these arrays must contain the lower-triangular elements of the
N $\times $ N complex Hermitian matrix $A$ with the real part in AR(,) and
the imaginary part in AI(,). On return AR(,) and AI(,) contain information
about the unitary transformations used in the reduction of $A$ in their lower
triangle. The strict upper triangles of AR(,) and AI(,), and the diagonal of
AR(,) are unaltered.

\item[LDA]  \ [in] Dimension of the first subscript of the arrays AR(,),
AI(,), VR(,), and VI(,). Require LDA $\geq $ N.

\item[N]  \ [in] Order of the complex Hermitian matrix $A$. N $\geq 1.$

\item[EVAL()]  \ [out] Array in which the N real eigenvalues of $A$ will be
stored by the subroutine. The eigenvalues will be sorted with the
algebraically smallest eigenvalues first.

\item[VR(,), VI(,)]  \ [out] On return contains the real and imaginary parts
of the eigenvectors with column $k$ corresponding to the eigenvalue in EVAL$%
(k)$. These N eigenvectors will be mutually orthogonal and will have a
unit unitary norm.

\item[WORK()]  \ [scratch] An array of at least 3N locations used as
temporary space.

\item[IERR]  \ [out] On exit this is set to~0 if the QL algorithm converges,
otherwise see Section E.
\end{description}

\subsubsection{Modifications for Double Precision}

Change SHERQL to DHERQL, and the REAL type statement to DOUBLE PRECISION.

\subsection{Examples and Remarks}

Consider the following complex Hermitian matrix:
\begin{equation*}
A=\left[
\begin{array}{cccc}
\phantom{-}25 & -3-4i & -8+6i & \phantom{-}0 \\
-3+4i & \phantom{-}25 & \phantom{-}0 & -8-6i \\
-8-6i & \phantom{-}0 & \phantom{-}25 & \phantom{-}3-4i \\
\phantom{-}0 & -8+6i & \phantom{-}3+4i & \phantom{-}25
\end{array}
\right]
\end{equation*}
unit unitary norm associated with these eigenvalues are column
vectors with the following quadruples of elements:
($0.5i$, $0.4+0.3i$, $-0.3+0.4i$, 0.5),
($-0.5i$, $0.4+0.3i$, $0.3-0.4i$, 0.5),
($-0.5i$, $-0.4-0.3i$, $-0.3+0.4i$, 0.5),  and
($0.5i$, $-0.4-0.3i$, $0.3-0.4i$, 0.5), respectively.

The code in DRSHERQL, given below, computes the eigenvalues and eigenvectors
of this matrix. Output from this program is given in the file ODSHERQL.

Before the call to SHERQL, the matrix is saved in order to compute the
relative residual matrix $D$ defined as%
\begin{equation*}
C=\left( AW-W\Lambda \right) /\gamma
\end{equation*}
where $W$ is the matrix whose columns are the computed eigenvectors
of $A$, $\Lambda $ is the diagonal matrix of eigenvalues, and $\gamma $ is the
maximum-row-sum norm of $A$.

Recall that if ${\bf v}$ is an eigenvector, then so is $\alpha{\bf
v}$ for any nonzero complex scalar $\alpha$.  More generally, if an
eigenvalue, $\lambda$, of a complex Hermitian matrix occurs with
multiplicity $k$, there will be an associated  $k$-dimensional
complex subspace in which every vector is an eigenvector for
$\lambda$.  This subroutine will return eigenvectors constituting an
orthogonal basis for such an eigenspace.

\subsection{Functional Description}

Householder complex unitary similarity transformations are used to transform
the matrix $A$ to a Hermitian tridiagonal matrix. Additional unitary
similarity transformations are used to transform the matrix to a real
tridiagonal matrix. From this point, this subroutine uses the same method as
the subroutine SSYMQR of Chapter~5.1. As was the case there, all routines
are minor modifications of EISPACK routines, \cite{Smith:1974:MER}.

\bibliography{math77}
\bibliographystyle{math77}

\subsection{Error Procedures and Restrictions}

If the QL algorithm fails to converge in 30~iterations on the $J^{th}$
eigenvalue the subroutine sets IERR = $J$. In this case $J-1$ eigenvalues
are computed correctly but the eigenvalues are not ordered, and the
eigenvectors are not computed. If N $\leq $ 0 on entry, IERR is set to $-$1.
In either case an error message is printed using IERM1 of Chapter 19.2
with an error level of 0, before the return.

\subsection{Supporting Information}

The source language is ANSI Fortran~77.

\begin{tabular}{@{\bf}l@{\hspace{5pt}}l}
\bf Entry & \hspace{.35in} {\bf Required Files}\vspace{2pt} \\
DHERQL & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
DHERQL, DIMQL, ERFIN, ERMSG, IERM1, IERV1\rule[-5pt]{0pt}{8pt}}\\
SHERQL & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
ERFIN, ERMSG, IERM1, IERV1, SHERQL, SIMQL}\\
\end{tabular}

Converted by: F. T. Krogh, JPL, October~1991.

\begcodenp

\lstset{language=[77]Fortran,showstringspaces=false}
\lstset{xleftmargin=.8in}

\centerline{\bf \large DRSHERQL}\vspace{10pt}
\lstinputlisting{\codeloc{sherql}}

\vspace{30pt}\centerline{\bf \large ODSHERQL}\vspace{10pt}
\lstset{language={}}
\lstinputlisting{\outputloc{sherql}}
\end{document}


Previous chapters introduced several algorithms for robot localization and state estimation that are based on a probabilistic framework. In particular, the Bayes filter was first introduced as a fundamental approach to the problem, which uses a probabilistic state transition model and a measurement model to recursively update a belief distribution over possible states. A set tractable implementations of the Bayes filter that model the belief distribution in a \textit{parametric} way, for example using Gaussian distributions, was then presented (in particular the Kalman and extended Kalman filters).
These filters leverage the structure of the parametric belief distribution to provide a computationally efficient approach to dealing with continuous state spaces (which have an infinite number of states). For example the Gaussian distribution represents a continuous distribution through a \textit{finite} set of parameters: the mean and covariance.
However there are also other implementations of Bayes filter that can be efficiently used in continuous state spaces that are \textit{non-parametric}.

\notessection{Nonparametric Filters}
\cite{ThrunBurgardEtAl2005}
In contrast to parametric filters, \textit{non-parametric} filters do not make assumptions on the structure of the belief distribution. This can be a desirable property for applications in robotics where rigid structures in the belief distribution may result in poor performance. A classic example is that the Gaussian distributions used in the Kalman filter and EKF are unimodal, which cannot express the possibility that two distinct ``high probability'' states might exist at the same time.
Non-parametric filters on the other hand generally represent the belief distribution in an unstructured way, for example through a finite number of samples drawn from the distribution, which allows for more expressive distributions.
This chapter introduces two main approaches for non-parametric filtering: the \textit{histogram filter} and the \textit{particle filter}.

\subsection{Histogram Filter}
The histogram filter is essentially a modification of the discrete Bayes filter presented earlier to work in continuous state spaces. In particular, the continuous state space is decomposed into a finite number of regions and the belief is represented over the discretized space by collecting the finite number of probabilities of the state being in each discretized region.

In particular for the random state variable $X_t$, the continuous state space $\text{dom}(X_t)$ is decomposed into a finite set of regions (often called \textit{bins} in the context of histogram filters):
\begin{equation}
\text{dom}(X_t) = \x_{1,t} \cup \x_{2,t} \cup ... \cup \x_{K,t},
\end{equation}
where $\x_{k,t}$ is the $k$-th ``bin''.
For example, if the one-dimensional random variable $X$ could take on values in the interval $[a,b]$ then one possible decomposition would be to split the interval into a finite number of sub-intervals with equal width.
The belief distribution is then defined in non-parametric way by simply specifying a probability $p_{k,t}$ to each bin $\x_{k,t}$. A probability density function can then be defined in a piecewise manner:
\begin{equation}
p(\x_t) = \frac{p_{k,t}}{\lvert \x_{k,t} \rvert}, \quad \x_t \in \x_{k,t},
\end{equation}
where $\lvert \x_{k,t} \rvert$ denotes the ``area'' or ``volume'' of the bin.
This definition implies that the probability that the random variable $X_t$ takes on \textit{any} value in the bin $\x_{k,t}$ is equal to $p_{k,t}$.

The prediction and measurement update steps of the Bayes filter are then accomplished by also discretizing the state transition and measurement models by computing a representative ``mean'' state for each bin:
\begin{equation}
\hat{\x}_{k,t} = \lvert \x_{k,t} \rvert ^{-1}\int_{\x_{k,t}}x_t d \x_t.
\end{equation}
The state transition model $p(\x_{k,t} \mid \bu_t, \x_{i,t-1})$ that defines the probability of transitioning from one bin to another is then approximated in terms of the mean bin states by:
\begin{equation}
p(\x_{k,t} \mid \bu_t, \x_{i,t-1}) \approx \eta \lvert \x_{k,t} \rvert p(\hat{\x}_{k,t} \mid \bu_t, \hat{\x}_{i,t-1}),
\end{equation}
where $p(\hat{\x}_{k,t} \mid u_t, \hat{\x}_{i,t-1})$ is the original (non-discretized) state transition model evaluated at the mean bin states $\hat{\x}$ and $\eta$ is a normalization constant\footnote{In the case that the bin areas $\lvert \x_{k,t} \rvert$ are equal, these terms can be absorbed into the normalization constant.}.

The discretization of the measurement model is accomplished in a similar manner, with the discretized model given by:
\begin{equation}
p(\z_t \mid \x_{k,t}) \approx p(\z_t \mid \hat{\x}_{k,t}).
\end{equation}
In other words, the measurement probability associated with a bin is approximated by the measurement probability associated with the mean bin state $\hat{\x}_{k,t}$.

After the discretization has been performed, the discrete Bayes filter algorithm from before can be directly applied by iterating over each bin and updating the probability $p_{k,t}$.

\subsection{Particle Filter}
The particle filter is another non-parametric filter that provides a computationally tractable implementation of the Bayes filter for continuous state spaces. This filter represents the belief distribution by a finite set of random samples called particles, which are denoted by:
\begin{equation}
\mathcal{X}_t \coloneqq \{\x_t^{[1]}, \x_t^{[2]},..., \x_t^{[M]}\}.
\end{equation}
Each particle $\x_t^{[m]}$ represents a hypothesis about the true state $\x_t$, and therefore regions of the state space with more particles correspond to regions of high probability.
Ideally, the particles are distributed according to the current belief:
\begin{equation}
    \x_t^{[m]} \sim p(\x_t \mid \z_{1:t}, \bu_{1:t}) = bel(\x_t),
    \label{bel-posterior}
\end{equation}
but theoretically this only occurs as $M\rightarrow \infty$. Instead the set of particles approximately represents the belief distribution, and in practice around $M\approx 1000$ samples tends to be sufficient (but of course this depends on the application).

The particle filter updates the belief (via a prediction and measurement correction step) by manipulating the prior set of particles $\mathcal{X}_{t-1}$ to yield a new set of particles $\mathcal{X}_t$. The prediction step is implemented by considering each particle $\x_{t-1}^{[m]}$ in the prior set $\mathcal{X}_{t-1}$ and sampling from the state transition model a new ``predicted'' sample $\bar{\x}_{t}^{[m]} \sim p(\x_t \mid \bu_t, \x_{t-1}^{[m]})$. An importance factor $w_{t}^{[m]}$ is then defined for the predicted sample $\bar{\x}_{t}^{[m]}$ based on how well the observed measurement matches the prediction. Specifically, the importance factor is computed as $w_t^{[m]} = p(\z_t \mid \bar{\x}_t^{[m]})$. The predicted particles $\bar{\x}_{t}^{[m]}$ and their associated weights $w_t^{[m]}$ can then be collected in a new particle set $\bar{\mathcal{X}}_t$, which represents the predicted belief distribution $\overline{bel}(\x_t)$.
The correction step is then accomplished by simply resampling (with replacement) a new set of $M$ particles from the predicted set $\bar{\mathcal{X}}_t$ with a probability proportional to the weights $w_t^{[m]}$. This procedure performs the measurement correction by giving preference in the new sample set to those predicted particles that showed higher correlation to the measurement $\z_t$.
The resampled points are then collected in a new set $\mathcal{X}_t$ that defines the posterior belief distribution.
This algorithm is also outlined in Algorithm \ref{alg:particle} and a few iterations of the algorithm for a simple robot localization problem are shown in Figure \ref{fig:Particle_filter}.

\begin{algorithm}[ht]
 \KwData{$\mathcal{X}_{t-1}, \bu_{t}, \z_{t}$}
 \KwResult{$\mathcal{X}_{t}$}
 $\bar{\mathcal{X}}_{t} = \mathcal{X}_t = \emptyset$\\
 \For{$m=1$ \KwTo $M$}{
  Sample $\bar{\x}_{t}^{[m]} \sim p(\x_t\mid \bu_t, \x_{t-1}^{[m]})$\\
  $w_t^{[m]} = p(\z_t \mid \bar{\x}_{t}^{[m]})$\\
  $\bar{\mathcal{X}}_{t} = \bar{\mathcal{X}}_{t} \cup \big(\bar{\x}_{t}^{[m]}, w_t^{[m]} \big)$\\
 }
 \For{$m=1$ \KwTo $M$}{
  Draw $i$ with probability $\propto w_t^{[i]}$\\
  Add $\bar{\x}_{t}^{[i]}$ to $\mathcal{X}_t$
 }
 \Return $\mathcal{X}_t$
 \caption{Particle Filter Algorithm}
 \label{alg:particle}
\end{algorithm}

Note that the concept of resampling in the correction step can be quite important for reasons beyond just updating the belief for the measurement correction. In particular, without the resampling step over time some of the particles would drift to regions of low probability and there would be fewer particles to represent the regions of high probability. The resampling step can therefore be viewed as a probabilistic implementation of the Darwinian idea of survival of the fittest: it refocuses the particle set to regions in state space with high posterior probability. This helps from a computational efficiency standpoint because it reduces the number of particles that are needed by focusing them on the regions of the state space that matter (i.e. regions of high probability).
\begin{figure}[ht]
\centering
\includegraphics[width=0.8\linewidth]{tex/figs/ch15_figs/particlefilter.png}
\caption{Particle filter used for robot localization. The initial set of particles are first updated according to the transition model, and then weighted according to the observation. Finally, a new set of particles is generated through weighted resampling.}
\label{fig:Particle_filter}
\end{figure}

\subsection{Exercises}
\subsubsection{Monte Carlo Localization}
Complete \textit{Extra Credit: Monte Carlo Localization} located in the online repository:

\vspace{\baselineskip}

\url{https://github.com/PrinciplesofRobotAutonomy/AA274A_HW4},

\vspace{\baselineskip}

where you will implement a particle filter for localizing a robot with line feature extraction, similar to the exercise on EKF localization from the previous chapter.


% !TEX root = Main.tex
\section{Gaussian Mixture Models (GMM)}
For GMM let $\boldsymbol{\theta}_k = (\boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k)$; $p_{\theta_k}(\mathbf{x}) = \mathcal{N}(\mathbf{x} | \boldsymbol{\mu}_k, \Sigma_k)$\\
\textbf{Mixture Models:} $p_\theta(\mathbf{x}) = \sum_{k=1}^K \pi_k p_{\theta_k}(\mathbf{x})$\\
\textbf{Assignment variable (generative model):} $z_{ij} \in \{0, 1\}$, $\sum_{j=1}^k z_{ij} = 1$\\
$\operatorname{Pr}(z_k = 1) = \pi_k \Leftrightarrow p(\mathbf{z}) = \prod_{k=1}^K \pi_k^{z_k}$\\
\textbf{Complete data distribution:}\\
$p_\theta(\mathbf{x}, \mathbf{z}) = \prod_{k=1}^K \left( \boldsymbol{\pi}_k p_{\theta_k}(\mathbf{x})\right)^{z_k}$\\
\textbf{Posterior Probabilities:} $\\\operatorname{Pr}(z_k = 1 | \mathbf{x}) = \frac{\operatorname{Pr}(z_k = 1) p(\mathbf{x} | z_k = 1)}{\sum_{l=1}^K \operatorname{Pr}(z_l = 1) p(\mathbf{x} | z_l = 1)} = \frac{\boldsymbol{\pi}_k p_{\theta_k}(\mathbf{x})}{\sum_{l=1}^K \boldsymbol{\pi}_l p_{\theta_l}(\mathbf{x})}$
\textbf{Likelihood of observed data $\mathbf{X}$:}\\
$p_\theta(\mathbf{X}) = \prod_{n=1}^N p_\theta(\mathbf{x}_n) = \prod_{n=1}^N \left(\sum_{k=1}^K \pi_k p_{\theta_k}(\mathbf{x}_n)\right)$
\textbf{Max. Likelihood Estimation (MLE):}\\
$\argmax_\theta\sum_{n=1}^N \log \left( \sum_{k=1}^K \pi_k p_{\theta_k}(\mathbf{x}_n)\right)\\
\ge \sum_{n=1^N} \sum_{k=1}^K{q_k[\log p_{\theta_k}(\mathbf{x}_n) + \log \pi_k - \log q_k]}$\\
with $\sum_{k=1}^K{q_k} = 1$ by Jensen Inequality.

\subsection*{Generative Model}
1. sample cluster index $j \sim Categorical(\pi)$\\
2. given $j$, sample data $x \sim \text{Normal}(\mu_j, \Sigma_j)$

\subsection*{Expectation-Maximization (EM) for GMM}
\textbf{E-Step: }\\
Pr$[z_{k,n} = 1 | \mathbf{x}_n] = q_{k, n} = \frac{\boldsymbol{\pi}_k^{(t-1)} \mathcal{N}(\mathbf{x}_n | \boldsymbol{\mu}_k^{(t-1)}, \boldsymbol{\Sigma}_k^{(t-1)})}{\sum_{j=1}^K \boldsymbol{\pi}_j^{(t-1)} \mathcal{N}(\mathbf{x}_n | \boldsymbol{\mu}_j^{(t-1)}, \boldsymbol{\Sigma}_j^{(t-1)})}$\\
\textbf{M-Step: } $\boldsymbol{\mu}_k^{(t)} := \frac{\sum_{n=1}^N q_{k,n} \mathbf{x}_n}{\sum_{n=1}^N q_{k,n}}$
$, \boldsymbol{\pi}_k^{(t)} := \frac{1}{N} \sum_{n=1}^N q_{k,n}$\\
$\Sigma_k^{(t)} = \frac{\sum_{n=1}^N q_{k, n} (\mathbf{x}_n - \boldsymbol{\mu}_k^{(t)})(\mathbf{x}_n - \boldsymbol{\mu}_k^{(t)})^\top}{\sum_{n=1}^N q_{k,n}}$

\subsection*{Discussion K-means vs. EM}
hard assignment vs soft. sherical clusters shapes vs covariance matrix. fast vs slow and more iteration. K-means can be used as initialization for EM.\\
K-means os a special case of GMM with covariances $\Sigma_j = \sigma^2 * I$. in the limit of $\sigma \rightarrow 0$, recover K-means.

\subsection*{Model Order Selection (AIC / BIC for GMM)}
Trade-off between data fit (i.e. likelihood $p(\mathbf{X} | \theta)$) and complexity (i.e. \# of free parameters $\kappa(\cdot)$). For choosing $K$:\\
\textbf{Akaike Information Criterion}: $\operatorname{AIC}(\theta | \mathbf{X}) = -\log p_\theta(\mathbf{X}) + \kappa(\theta)$\\
\textbf{Bayesian Information Criterion}: $\operatorname{BIC}(\theta | \mathbf{X}) = -\log p_\theta(\mathbf{X}) + \frac{1}{2} \kappa(\theta) \log N$\\
\# of free params, fixed covariance matrix: $\kappa(\theta) = K \cdot D + (K - 1)$ ($K$: \# clusters, $D$: $\mathsf{dim}\text{(data)}=\mathsf{dim}(\mu_i)$, $K-1$: \# free clusters, full covariance matrix: $\kappa(\theta) = K(D + \frac{D(D+1)}{2}) + (K - 1)$.\\
Compare AIC/BIC for different $K$ -- the smaller the better. BIC penalizes complexity more.


\lab{Algorithms}{Speeding up Python Functions: Vectorization}{Vectorization One}

\objective{Demonstrates the importance of vectorization.}

This lab introduces two applied topics (Information Entropy and Chebyshev Polynomials) to explore how we can make functions faster through vectorization. There are two primary motivations behind vectorizing code.  First, vectorized code is often much clearer and more concise than non-vectorized code.  The second, much larger motivation is performance.  Vectorized code is often order of magnitudes faster than non-vectorized code.

The concept behind vectorizing code is to move away from operating on one element at a time and toward operating on entire collections of elements at once.  This section will focus specifically on reducing the number of for loops in our program.  Let us look at two examples of functions that would be really useful if we could vectorize them to make them execute faster.

\section*{Information Entropy}

Information entropy is loosely defined as the amount of information that is encoded in each bit of a signal. It is directly related to amount of compression that a signal can undergo without loss.

For example, suppose that our signal is an infinite string ``AAAAAAA..." Since the signal never changes, each additional bit after the first adds no new information. Thus the entropy in this case is zero.

On the other hand, we assign a truly random uniform binary source an entropy of one (such a source may be related to radioactive decay). The reason for this is that we have absolutely no clue what the next symbol we read will be.

We can empirically calculate the entropy of a given signal by the following:
\begin{equation*}
H = -\sum_k{p_k \log_2(p_k)}
\end{equation*}
where $p_k$ is the probability of the $k^{th}$ symbol. Note that this fits the two conventions we set above since
\begin{equation*}
-(1\cdot\log_2(1) + 0\cdot\log_2(0)) = 0
\end{equation*}
\begin{equation*}
-(\frac{1}{2}\log_2\frac{1}{2} + \frac{1}{2}\log_2\frac{1}{2}) = 1
\end{equation*}

\begin{problem}
Write a function that finds the entropy of an array (you will need to flatten multidimensional arrays to a single dimension). You will also need to empirically calculate the probability of a given symbol (start by letting your symbols be integers). To find the probability of a symbol, you need to find the number of times it occurs in the sequence and divide it by the length of the sequence (watch out for integer division).
\end{problem}

We can test the entropy function that you just wrote against bit sources to see how ``random" they are. For example, we can read an image into Python and view it using the following code:
\begin{lstlisting}
>>> from scipy import misc
>>> img = misc.imread('cameraman.tif'); img
\end{lstlisting}

\begin{figure}[h!]
\begin{center}
\includegraphics{cameramanClean.pdf}
\end{center}
\caption{The MIT Camera Man, a classic image used in image processing}
\label{fig:cameramanclean}
\end{figure}

You should see the image shown in Figure \ref{fig:cameramanclean}. Now pass this image to the entropy function to calculate the entropy of the image data. We get a value of $7.009$. If this image were uniformly random, the entropy would be $8$, since each pixel is an $8$ bit number, and a uniform distribution of integers between $1$ and $128 = 2^8$ will have an entropy of $8$ (you can verify this fairly easily using the equation we defined above). This implies that the image has potential for compression.

\begin{problem}
Now rewrite your function without using loops. This is an example of using vectorization to speed up algorithms. Use the cameraman image to test how much faster this function is than the original.
\end{problem}

\section*{Chebyshev Polynomials}

One special class of polynomials is the class of Chebyshev Polynomials. They are useful in a variety of applications including:
\begin{itemize}
\item Evaluating integrals using Gaussian quadrature
\item Solving partial differential equations
\item Speeding up iterative matrix methods (finding eigenvalues and solving large systems)
\item Minimizing interpolation error
\end{itemize}

We will explain many of the these applications in subsequent chapters. The purpose of this example is for you to investigate ways to speed up a process on your own.

The $n^{th}$ Chebyshev Polynomial can be described equivalently by the following formulae\footnotemark :
\begin{equation*}
T_n(x) = \cos(n \cos^{-1}(x))
\end{equation*}
\begin{equation*}
T_n(x) = 2xT_{n-1}(x) - T_{n-2}(x), T_0(x) = 1, T_1(x) = x
\end{equation*}

\footnotetext{Technically the first formula only works on the interval $[-1,1]$. There are more general formulae, piecewise defined using $\cosh$ outside of $[-1,1]$, but for this exercise we'll stick to the above definition.}

\begin{problem}
Write a function that accepts a vector $x_0$ of values in $[-1,1]$ and a degree $n$.  The function should return an array evaluating each entry of $x_0$ at every Chebyshev polynomial of degree $n$ or less. The $n^{th}$ column of the output should contain the $n-1$ Chebyshev Polynomial evaluated at $x_0$.

You will be writing four slightly different versions of this function.
\begin{enumerate}
\item Use only for-loops and Python's \li{math} library (you will need two for-loops).
\item Same as previous, but replacing one for loop with a list comprehension.
\item Use \li{numpy.vectorize} to vectorize Python's \li{math} functions.  Keep the list comprehension from previous part.
\item Use SciPy's optimized $\cos$ and $\arccos$ functions.
\end{enumerate}

Make this function as fast as possible. You should investigate both mathematical definitions, and how the number of for-loops affects the speed.
\end{problem}


\subsection{Angles on a sphere}
Given two spherical segments $[AB]$ and $[AC]$ on a sphere around the origin $O$, how do we compute the amplitude of the angle they form on the surface of the sphere at $A$? This angle is equal to the angle between planes $OAB$ and $OAC$, or more precisely between their normals $A \times B$ and $A \times C$. Thus we can find the angle using \lstinline|angle()|, which gives values in $[0,\pi]$.

\centerFig{sph-13}

\begin{lstlisting}
double angleSph(p3 a, p3 b, p3 c) {
    return angle(a*b, a*c);
}
\end{lstlisting}

If instead of values in $[0,\pi]$, we want to know the oriented angle\footnote{We defined a similar notion in 2D in section~\ref{ss:cross}.} between $[AB]$ and $[AC]$, that is, how much we rotate if we go from $B$ to $C$ around $A$ counterclockwise, then we need to know on which side of plane $OAB$ point $C$ lies. If $C$ lies ``to the left'' of plane $OAB$, the angle given by \lstinline|angle()| is correct, but if $C$ lies ``to the right'', we should subtract it from $2\pi$.

\centerFig{sph-14}

\begin{lstlisting}
double orientedAngleSph(p3 a, p3 b, p3 c) {
    if ((a*b|c) >= 0)
        return angleSph(a, b, c);
    else
        return 2*M_PI - angleSph(a, b, c);
}
\end{lstlisting}

\subsection{Spherical polygons and area}
Given points $P_1,\ldots,P_n$ on a sphere, let's call spherical polygon $P_1\cdots P_n$ the region on the sphere delimited by spherical segments $[P_1P_2]$, $[P_2P_3]$, \ldots, $[P_nP_1]$, and which is on the left when travelling from $P_1$ to $P_2$. The counterclockwise order is important here because both ``sides'' on the contour are valid candidates.

\centerFig{sph-15}

Computing the area of such a spherical polygon is surprisingly simple. First, let's consider the case of a spherical triangle $ABC$. It's area is given by
\[r^2(\alpha + \beta + \gamma - \pi)\]
where $r$ is the radius of the sphere and $\alpha,\beta,\gamma$ are the amplitudes of the three interior angles of $ABC$. Note that this would be equal to $0$ if $ABC$ were on a plane, but because of the curvature of the sphere, the angles of a triangle actually add up to more than $\pi$.

This is actually pretty easy to prove. If we prolong the segments $[AB]$, $[BC]$, and $[CA]$ into their full great-circles, this splits the sphere into 8 parts, of which four are the direct image of each other by point reflection around the center of the sphere. Let's call $S$ the area of triangle $ABC$, and $S_A$ (resp. $S_B,S_C$ the area of the triangle on the other side of $[BC]$ (resp. $[CA],[AB]$).

\centerFig{sph-16}

Since those four triangles cover half of the sphere together, we have $S+S_A+S_B+S_C = 2\pi r^2$.
Besides if we take triangle $ABC$ and the triangle on the other side of $[BC]$, together they form a whole \emph{spherical wedge}\footnote{A section of a sphere determined by two flat cuts going through the center. \url{https://en.wikipedia.org/wiki/Spherical_wedge}} of angle $\alpha$.
So their combined area should be $\alpha/2\pi$ of the total area, that is $S+S_A = 2\alpha r^2$.
Similarly, we obtain $S+S_B = 2\beta r^2$ and $S+S_C = 2\gamma r^2$.

Combining those four equations, we obtain
\begin{align*}
2S &= (S+S_A) + (S+S_B) + (S+S_C) - (S+S_A+S_B+S_C)\\
&= r^2(2\alpha + 2\beta + 2\gamma - 2\pi)
\end{align*}
which is the desired result.

The formula can be extended to an arbitrary spherical polygon $P_1\cdots P_n$. The area is given by
\[r^2\,\big[\,\mathrm{sum\ of\ interior\ angles} - (n - 2)\pi\,\big]\]
This can be proven by decomposing the $n$-gon into $n-2$ triangles.

\begin{lstlisting}
double areaOnSphere(double r, vector<p3> p) {
    int n = p.size();
    double sum = -(n-2)*M_PI;
    for (int i = 0; i < n; i++)
        sum += orientedAngleSph(p[(i+1)%n], p[(i+2)%n], p[i]);
    return r*r*sum;
}
\end{lstlisting}

\subsection{Solid angle}
The \emph{solid angle} subtended by an object at an observation point $O$ is the apparent size of the object when looking at it from $O$. For example, the Sun and the Moon have roughly the same apparent size when watched from the Earth, even though their actual sizes are very different. So we say that they subtend the same solid angle at the observation point that is Earth.

Let's define it more precisely. Just like the planar angle subtended by an object is the length of the object once it is projected onto a unit circle around the point, the solid angle subtended by an object is the area of the object once it is projected onto a unit sphere around the point.

\centerFig{sph-17}

The unit for solid angles is the steradian (\steradian), and because the area of a unit sphere is $4\pi$, a solid angle of $4\pi$ means that the observation point is completely surrounded, while a solid angle of $2\pi$ means that half of the view is covered.

We can easily find the solid angle subtended by a polygon by using the function \lstinline|areaOnSphere()| we just defined and setting $r=1$. Indeed, all it does is compute angles, so the distance from the origin $O$ doesn't matter.

\centerFig{sph-18}

This also allows us to find the solid angle subtended by a polyhedron if we know which faces are visible from $O$.

\begin{mathy}
The solid angle subtended by a small surface $d\vv{S}$ at a position $\vv{r}$ is inversely proportional to the square of the distance from the origin, $\normv{r}^2$, and proportional to the cosine of the angle between $\vv{r}$ and $d\vv{S}$, because a surface seen from sideways occupies less of the view. So we can also find solid angles with the following integral:
\[\Omega = \int \frac{\vv{r}\cdot d\vv{S}}{\normv{r}^3}\]
\end{mathy}

\subsection{3D winding number}\label{ss:wind-3d}
We can use this notion of solid angle to implement a 3D version of winding number that we defined in section~\ref{ss:wind-2d}. Given an observation point $O$ and a polyhedron, we will compute an integer that is
\begin{itemize}
\item $0$ if $O$ is outside the polyhedron;
\item $1$ if $O$ is inside the polyhedron, and the vector areas $\vv{S}$ of the faces are oriented towards the outside;
\item $-1$ if $O$ is inside the polyhedron, and the vector areas $\vv{S}$ of the faces are oriented towards the inside.
\end{itemize}

To do this, we will consider the faces one by one.
For each one, if its vector area $\vv{S}$ points away from $O$, we add the solid angle it subtends to the total, and otherwise we subtract it.
That way:
\begin{itemize}
\item if $O$ is outside of the polyhedron, the solid angles will cancel out;
\item if $O$ is inside of the polyhedron and the $\vv{S}$ point towards the outside, there will mostly be additions and the total will add up to $4\pi$;
\item if $O$ is inside the polyhedron and the $\vv{S}$ point towards the inside, there will mostly be subtractions and the total will add up to $-4\pi$.
\end{itemize}
Dividing this total angle by $4\pi$ gives the desired winding number.

\centerFig{sph-19}

To find what quantity to add or subtract for each face, we will use function \lstinline|areaOnSphere()| directly. If $\vv{S}$ points away from $O$, it will return a value in $(0,2\pi)$, the area of the projection of the face a unit sphere, which we should keep. If $\vv{S}$ points towards $O$, it will return a value in $(2\pi,4\pi)$, the area of the \emph{rest} of the unit sphere, so we should remove $4\pi$ to get the subtraction we want.

Since we always want the value to be in $(-2\pi,2\pi)$ in the end, we can use function \lstinline|remainder()|, giving the simple implementation below.
\begin{lstlisting}
int windingNumber3D(vector<vector<p3>> fs) {
    double sum = 0;
    for (vector<p3> f : fs)
        sum += remainder(areaOnSphere(1, f), 4*M_PI);
    return round(sum / (4*M_PI));
}
\end{lstlisting}


\section{Introduction}
This paper discusses the symmetric groups as an idexed category and their representations on finitely generated modules of some unital commutative ring ($\zz$, $\zz_n$, ...). Firstly, we will briefly discuss (finite groups) and their representations.
\subsection{Representation theory}
Given a unital commutative ring $R$ and a group $G$, a representation is some $R$ module $M$ with a group homomorphism
$$\rho : G \longrightarrow \mathrm{Gl}_R(M).$$
It is denoted by the pair $(M, \rho)$. Alternatively, the module $M$ for every representation $(M, \rho)$ is called a $G$ module, with $G$ action induced via $\rho$:
$$\alpha : G \times M \longrightarrow M,\ (g, m) \longmapsto \rho(g)(m).$$
Thus, discussing $G$ modules or representations $(M, \rho)$ is identical. If the representation homomorphism $\rho$ is clear from the context, we will omit it and simply write
$$G.M = \{g \cdot m: g \in G, m \in M\} \subset M \equiv \{\rho(g)(m) : g \in G, m \in M\} \subset M$$
if we wish to define a $G$ module.
\begin{defi}
Given a representation $(M, \rho)$ for a group $G$. We call a submodule $N \subset M$ a sub representation or a $G$ submodule if
$$\rho : G \longrightarrow \mathrm{Gl}_R(N) \Leftrightarrow G.N \subset N.$$
For two representations $(M, \rho_M)$, $(N, \rho_N)$, we call a module homomorphism $\phi \in \mathrm{Hom}_R(M,N)$ a $G$ module homomorphism if the following diagram commutes:
$$\xymatrix{
M \ar[r]^{g}\ar[d]_\phi &M\ar[d]^\phi\\
N \ar[r]_g & N.\\
}$$
\end{defi}
\paragraph{Remark}
For a given $G$ module $M$ and a $G$ submodule $N \subset M$, the quotient module
$$M/N := \{m + N : m \in M\}$$
is a $G$ module via:
$$\rho_{M/N} := \left[m + N \longmapsto g m + N\right],$$
as $G. N \subset N$. In particular, the projections $\pi_N, \pi_{M/N} \equiv id_{M/N} - \pi_N$ are $G$ module homomorphisms. Therefore, the decomposition:
$$M \simeq M/N \oplus N$$
is a decomposition in 'smaller' $G$ modules. Furthermore, for any given $G$ submodule $N \subset M$, we get the complement as a $G$ submodule $N' \simeq M/N$. In addition, we have for to $G$ modules $M_1, M_2$ the sum
$$M_1 + M_2 = \left\{\sum_i \lambda_i m_i : \lambda_i \in R, m_i \in M_i\right\},$$
as $G$ module via $\rho_{M_1 + M_2} := (\rho_1 + id_{M_2}) \circ (\rho_2 + id_{M_1})$, with $id_{M_i}$ as trivial representation. This gets us to a very important classification of $G$ modules:
\begin{defi}
We call a $G$ module simple/irreducible if the only $G$ submodules are $\{0\}$ and $M$ (i.e. the trivial $G$ submodules). Thus, its representation is called irreducible. A $G$ module is indecomposible if $M$ does not admit a decomposition in non-trivial $G$ submodules:
$$M = M' \oplus M'' \Leftrightarrow (M' = \{0\} \vee M'' = \{0\}.$$
Aquivalently, every decomposition into $G$ submodules is trivial if and only if $M$ is indecomposible.
\end{defi}
Firstly, we remark that any $G$ module with a non-trivial submodule is called reducible. Secondly, we remark that a simple $G$ module is clearly indecomposible but, there are indecomposible modules that are reducible: $M = R.e_1 \oplus R.e_2$, $G \subset \mathrm{Gl}_2(R)$ the group of upper triangular matrices and $A \in G \backslash \{I_2\}$ than clearly, $R.e_1$ is a non-trivial $G$ submodule, hence, reducible but indecomposible: basis $B = \{b_1 = e_1, b_2 = e_1 + e_2\}$
$$A = \left(\begin{array}{cc}\lambda & \mu\\ &\lambda'\\\end{array}\right)\ \mathrm{with}\ \lambda, \lambda' \in R^\times \wedge \mu \in R$$$$ \Rightarrow\ A.b_1 = \lambda b_1,\ A.b_2 = (\lambda + \mu) e_1 + \lambda' e_2 = (\lambda + \mu - \lambda' ) b_1 + \lambda' b_2$$
Clearly, $b_2$ does not span its own $G$ submodule. Thus, our decomposition is not direct but also not trivial and therefore not simple but also indecomposible.
$$\{b_1 = e_1, b_2 = (\lambda + \mu - \lambda') e_1 + \lambda e_2\} = M_0 \supset M_1 = \{b_1\} \supset 0$$


\section{Polar Functions}
\subsection{Polar Coordinates}
Up to this point, we've mostly described points in the plane by listing two numbers: the distance along the $x$-axis and the distance along the $y$-axis.
This description, called ``rectangular coordinates" is pretty simple and has the advantage that every pair of coordinates describes a unique point on the plane. \\

However, we could instead describe points in the plane with two numbers $(r,\theta)$, where $r$ is the point's distance from the origin and $\theta$ is the point's angle of inclination.
This system is more suited to describing points related to trig functions.
For example the point at $x=\cos{\frac{\pi}{4}}$ and $y=\sin{\frac{\pi}{4}}$ is $\left(\frac{\sqrt{2}}{2},\frac{\sqrt{2}}{2}\right)$ in rectangular coordinates but $\left(1,\frac{\pi}{4}\right)$ in polar coordinates. \\

Note that unlike rectangular coordinates, multiple pairs of numbers can describe the same point.
For example $\left(1,0\right)$ is the same point as $\left(1,2\pi\right)$ is the same point as $\left(1,-2\pi\right)$ is the same point as $\left(1,4\pi\right)$. \\

We can easily convert between polar and rectangular coordinates.
\begin{align*}
	\left(r,\theta\right) \text{polar} &= \left(r\cos{\theta}, r\sin{\theta}\right) \text{rectangular} \\
	\left(x,y\right) \text{rectangular} &= \left(\sqrt{x^2+y^2}, \arctan{\frac{y}{x}}\right) \text{polar}.
\end{align*}

\subsection{Polar Functions}
Polar functions are written in the form $r = f(\theta)$.
Using our polar coordinate conversion formulas, we can convert any polar function to a parametric function.
\begin{align*}
	x(\theta) &= r\cos{\theta} = f(\theta)\cos{\theta} \\
	y(\theta) &= r\sin{\theta} = f(\theta)\sin{\theta}.
\end{align*}

Now we can use our parametric function formulas to get $\dd{y}{x}$.
\begin{equation*}
	\dd{y}{x} = \frac{\d{y}/\d{\theta}}{\d{x}/\d{\theta}}.
\end{equation*}

\begin{example}
	A cardioid is defined by $r=1-\cos{\theta}, 0 \leq \theta \leq 2\pi$.
	Find $\dd{y}{x}$.
\end{example}
\begin{answer}
	\begin{align*}
		x(\theta) &= \left(1-\cos{\theta}\right)\cos{\theta} \\
		\dd{x}{\theta} &= \sin{(2\theta)} - \sin{\theta} \\
		y(\theta) &= \left(1-\cos{\theta}\right)\sin{\theta} \\
		\dd{y}{\theta} &= \cos{\theta} - \cos{(2\theta)} \\
		\dd{y}{x} &= \frac{\cos{\theta} - \cos{(2\theta)}}{\sin{(2\theta)} - \sin{\theta}} \\
		&= \tan{\frac{3\theta}{2}}.
	\end{align*}
\end{answer}

\subsubsection{Area Enclosed}
When a polar function is changed by $\d{\theta}$, it sweeps out an circular sector.

\begin{figure}[H]
	\label{polar_area}
	\centering
	\includegraphics[width=0.66\textwidth]{./parametric_vector_polar/polar_area.png}
	\caption{\hyperref{}{}{}{Polar Area}}
\end{figure}

This circular sector has area $\d{A} = \frac{\d{\theta}}{2}r^2$.
Integrating $\d{A}$ for $\alpha \leq \theta \leq \beta$,
\begin{equation*}
	A = \int_{\alpha}^{\beta}{\frac{1}{2}r^2\d{\theta}} = \int_{\alpha}^{\beta}{\frac{1}{2}f^2(\theta)\d{\theta}}.
\end{equation*}

\begin{example}
	Find the area inside the smaller loop of the lima\c{c}on $r=2\cos{\theta}+1$.
\end{example}
\begin{answer}
	First, we need to find our bounds on $\theta$.
	We know that the small loop begins and ends when $r=0$.
	\begin{align*}
		0 &= 2\cos{\theta}+1 \\
		-\frac{1}{2} &= \cos{\theta} \\
		\theta &= \frac{\pi}{3}, \frac{4\pi}{3}.
	\end{align*}

	Now that we have our bounds, we can integrate.
	\begin{align*}
		A &= \int_{2\pi/3}^{4\pi/3}{\frac{1}{2}\left(2\cos{\theta}+1\right)^2\d{\theta}} \\
		&= \frac{1}{2}\left(\int_{\frac{2\pi}{3}}^{\frac{4\pi}{3}}4\cos^{2}\left(\theta\right)\d{\theta}+4\int_{\frac{2\pi}{3}}^{\frac{4\pi}{3}}\cos\left(\theta\right)\d{\theta}+\int_{\frac{2\pi}{3}}^{\frac{4\pi}{3}}\d{\theta}\right) \\
		&= \int_{\frac{2\pi}{3}}^{\frac{4\pi}{3}}\left(1+\cos\left(2\theta\right)\right)\d{\theta}+2\int_{\frac{2\pi}{3}}^{\frac{4\pi}{3}}\cos\left(\theta\right)\d{\theta}+\frac{1}{2}\int_{\frac{2\pi}{3}}^{\frac{4\pi}{3}}\d{\theta} \\
		&= \int_{\frac{2\pi}{3}}^{\frac{4\pi}{3}}\cos\left(2\theta\right)\d{\theta}+2\int_{\frac{2\pi}{3}}^{\frac{4\pi}{3}}\cos\left(\theta\right)\d{\theta}+\frac{3}{2}\int_{\frac{2\pi}{3}}^{\frac{4\pi}{3}}\d{\theta} \\
		&= \frac{1}{2}\sin\left(2\theta\right)+2\sin\left(\theta\right)+\frac{3}{2}\theta \biggr\rvert_{2\pi/3}^{4\pi/3} \\
		&= \left(\frac{1}{2}\sin\left(\frac{8\pi}{3}\right)+2\sin\left(\frac{4\pi}{3}\right)+\frac{3}{2}\frac{4\pi}{3}\right)-\left(\frac{1}{2}\sin\left(\frac{4\pi}{3}\right)+2\sin\left(\frac{2\pi}{3}\right)+\frac{3}{2}\frac{2\pi}{3}\right) \\
		&= \left(\frac{\sqrt{3}}{4}-\sqrt{3}+2\pi\right)-\left(-\frac{\sqrt{3}}{4}+\sqrt{3}+\pi\right) \\
		&= \frac{\sqrt{3}}{2}-2\sqrt{3}+\pi \\
		&= \pi-\frac{3\sqrt{3}}{2}.
	\end{align*}
\end{answer}

\subsubsection{Area Between Curves}
The area between $r_1(\theta)$ and $r_2(\theta)$ is simply the difference between the areas.
\begin{equation*}
	A = \frac{1}{2}\int_{\alpha}^{\beta}{r_1^2(\theta)\d{\theta}} - \frac{1}{2}\int_{\alpha}^{\beta}{r_2^2(\theta)\d{\theta}} = \frac{1}{2}\int_{\alpha}^{\beta}{(r_1^2(\theta)-r_2^2(\theta))\d{\theta}}.
\end{equation*}

\begin{example}
	Find the area that lies inside the circle $r=1$ and outside the cardioid $r=1-\cos{\theta}$.
\end{example}
\begin{answer}
	To find the bounds, we need to find where these curves intersect.
	\begin{align*}
		1 &= 1-\cos{\theta} \\
		\cos{\theta} &= 0 \\
		\theta &= \frac{\pi}{2}, \frac{-\pi}{2}.
	\end{align*}

	Since we want the area inside of the circle and outside of the cardioid, our bounds are $\frac{-\pi}{2} \leq \theta \leq \frac{\pi}{2}$.
	We'll also have $r_1(\theta)$ be the circle and $r_2(\theta)$ be the cardioid, since we are effectively finding the area inside the circle and subtracting away the area that is also in the cardioid.
	\begin{align*}
		A &= \frac{1}{2}\int_{-\pi/2}^{\pi/2}{\left(1^2 - \left(1-\cos{\theta}\right)^2\right)\d{\theta}} \\
		&= \frac{1}{2}\int_{\frac{-\pi}{2}}^{\frac{\pi}{2}}\left(2\cos\left(\theta\right)-\cos^{2}\theta\right)\d{\theta} \\
		&= \frac{1}{2}\int_{\frac{-\pi}{2}}^{\frac{\pi}{2}}\left(2\cos\left(\theta\right)-\frac{1+\cos\left(2\theta\right)}{2}\right)\d{\theta} \\
		&= \frac{1}{2}\left(2\sin\left(\theta\right)-\frac{\theta}{2}-\frac{\sin\left(2\theta\right)}{4}\right) \biggr\rvert_{\frac{-\pi}{2}}^{\frac{\pi}{2}} \\
		&= \frac{1}{2}\left(\left(2\sin\left(\frac{\pi}{2}\right)-\frac{\pi}{4}-\frac{\sin\left(\pi\right)}{4}\right)-\left(2\sin\left(\frac{-\pi}{2}\right)+\frac{\pi}{4}-\frac{\sin\left(-\pi\right)}{4}\right)\right) \\
		&= \left(2\sin\left(\frac{\pi}{2}\right)-\frac{\pi}{4}-\frac{\sin\left(\pi\right)}{4}\right) \\
		&= 2-\frac{\pi}{4}.
	\end{align*}
\end{answer}

\begin{example}
	Find the area that lies outside the circle $r=1$ and inside the cardioid $r=1-\cos{\theta}$.
\end{example}
\begin{answer}
	We need to make sure our bounds are sweeping out the correct area.
	If we did $\frac{-\pi}{2} \leq \theta \leq \frac{\pi}{2}$, we'd get the area inside the circle and outside the cardioid, which isn't what we want here.
	We know that for polar coordinates, $\frac{-\pi}{2} \equiv \frac{3\pi}{2}$.
	So, out bounds are $\frac{\pi}{2} \leq \theta \leq \frac{3\pi}{2}$.
	Since we want the area inside the cardioid and outside the circle, effectively taking the cardioid and subtracting away the intersection, so $r_1(\theta)$ is the cardioid, and $r_2(\theta)$ is the circle.
	\begin{align*}
		A &= \frac{1}{2}\int_{\pi/2}^{3\pi/2}{\left(\left(1-\cos{\theta}\right)^2-1^2\right)\d{\theta}} \\
		&= \frac{1}{2}\int_{\frac{\pi}{2}}^{\frac{3\pi}{2}}\left(\cos^{2}\left(\theta\right)-2\cos\left(\theta\right)\right)\d{\theta} \\
		&= \frac{1}{2}\int_{\frac{\pi}{2}}^{\frac{3\pi}{2}}\left(\frac{1+\cos\left(2\theta\right)}{2}-2\cos\left(\theta\right)\right)\d{\theta} \\
		&= \frac{1}{2}\left(\frac{\theta}{2}+\frac{\sin\left(2\theta\right)}{4}-2\sin\left(\theta\right)\right)\biggr\rvert_{\pi/2}^{3\pi/2} \\
		&= \frac{1}{2}\left(\left(\frac{3\pi}{4}+\frac{\sin\left(3\pi\right)}{4}-2\sin\left(\frac{3\pi}{2}\right)\right)-\left(\frac{\pi}{4}+\frac{\sin\left(\pi\right)}{4}-2\sin\left(\frac{\pi}{2}\right)\right)\right) \\
		&= \frac{\pi}{4}+2.
	\end{align*}
\end{answer}

\begin{example}
	Find the area inside both the circle $r=1$ and the cardioid $r=1-\cos{\theta}$.
\end{example}
\begin{answer}
	This area isn't between two polar curves like the previous examples in the sense that we can't define it as one region minus another.
	We'll prove it it two ways: logically and by using two regions.
	Logically, we know that the total area of the circle is $\pi$.
	We also know the area inside the circle but outside the cardioid is $2-\frac{\pi}{4}$.
	So,
	\begin{equation*}
		A_{\text{both}} = \pi - \left(2-\frac{\pi}{4}\right) = \frac{5\pi}{4} - 2.
	\end{equation*}

	We can break the region inside both curves into two parts: a half circle for $x\leq 0$ and the two cardioid bulges for $x\geq 0$.
	The area of the half-circle is $\pi/2$.
	We can find the area of the two cardioid bulges.
	\begin{align*}
		A_{\text{bulges}} &= 2A_{\text{bulge}} \\
		&= \int_{0}^{\pi/2}{\left(1-\cos{\theta}\right)^2\d{\theta}} \\
		&= \int_{0}^{\frac{\pi}{2}}\left(\cos^{2}\theta-2\cos\left(\theta\right)+1\right)\d{\theta} \\
		&= \int_{0}^{\frac{\pi}{2}}\left(\frac{1+\cos\left(2\theta\right)}{2}-2\cos\left(\theta\right)+1\right)\d{\theta} \\
		&= \frac{3\theta}{2}+\frac{\sin\left(2\theta\right)}{4}-2\sin\left(\theta\right)\biggr\rvert_{0}^{\frac{\pi}{2}} \\
		&= \frac{3\pi}{4}+\frac{\sin\left(\pi\right)}{4}-2\sin\left(\frac{\pi}{2}\right) \\
		&= \frac{3\pi}{4}-2 \\
	\end{align*}

	Adding in the area of the half-circle,
	\begin{align*}
		A_{\text{both}} &= A_{\text{half}} + A_{\text{bulges}} \\
		&= \frac{\pi}{2} + \frac{3\pi}{4} - 2 \\
		&= \frac{5\pi}{4} - 2.
	\end{align*}

	We see that we get the same answer either way.
\end{answer}

\subsubsection{Arc Length}
Since we know how to convert polar functions to parametric, we can simply adapt the parametric arc length formula.
\begin{equation*}
	s = \int_{\alpha}^{\beta}{\sqrt{\left(\dd{x}{\theta}\right)^2 + \left(\dd{y}{\theta}\right)^2}\d{\theta}}.
\end{equation*}

However, there is an alternate form that works just for polar functions.
For some small change $\d{\theta}$, we see a corresponding small changes $\d{r}$ and $r\d{\theta}$.
\begin{figure}[H]
	\label{polar_area}
	\centering
	\includegraphics[width=0.66\textwidth]{./parametric_vector_polar/polar_length.png}
	\caption{\hyperref{}{}{}{Polar Arc Length}}
\end{figure}
We see that these changes for a right triangle with hypotenuse $\d{s}$.
\begin{align*}
	(\d{s})^2 &= (r\d{\theta})^2 + (\d{r})^2 \\
	&= \left(r^2 + \left(\dd{r}{\theta}\right)^2\right)\left(\d{\theta}\right)^2 \\
	\d{s} &= \sqrt{r^2 + \left(\dd{r}{\theta}\right)^2}\d{\theta} \\
	s &= \int_{\alpha}^{\beta}{\sqrt{r^2 + \left(\dd{r}{\theta}\right)^2}\d{\theta}}.
\end{align*}

Giving us an alternate formula for polar arc length.

\begin{example}
	Find the arc length of the cardioid $r=1-\cos{\theta}$.
\end{example}
\begin{answer}
	The bounds are $0 \leq \theta \leq 2\pi$.
	Using the polar arc length formula,
	\begin{align*}
		\dd{r}{\theta} &= \sin{\theta} \\
		\left(\dd{r}{\theta}\right)^2 &= \sin^2{\theta} \\
		s &= \int_{0}^{2\pi}{\sqrt{\left(1-\cos{\theta}\right)^2+\sin^2{\theta}}\d{\theta}} \\
		&= \int_{0}^{2\pi}{\sqrt{2+2\cos{\theta}}\d{\theta}} \\
		&= \int_{0}^{2\pi}{2\sin{\left(\frac{\theta}{2}\right)}\d{\theta}} \\
		&= -4\cos{\left(\frac{\theta}{2}\right)}\biggr\rvert_{0}^{2\pi} \\
		&= 8.
	\end{align*}
\end{answer}


\section{Operations on FSA}

\subsection{Reflection}
Given a \textbf{deterministic} FSA.
\begin{itemize}
    \item Switch initial and final states
    \item Reverse the arrow directions
\end{itemize}

\subsection{Complement}
Given a \textbf{deterministic} FSA.
\begin{itemize}
    \item Extend the FSA with the error state: $Q' = Q \cup \{p\}$
    \item Make $\delta$ complete (using $p$)
    \item Switch final and non-final states
\end{itemize}

\subsection{Intersection}
$M_1 \cap M_2 = M_3$ where the states of $M_3$ are the cartesian product of the original 2 sets (i.e. all the pairs).
\[
    <q_1, q_2> \xrightarrow{a} <r_1,r_2> \iff q_1\xrightarrow{a} r_1 \land q_2 \xrightarrow{a} r_2
\]

The final/initial states are the ones final/initial for both.


\label{sec:general}

%-----------------------------------------------------------------

\subsection{Identities for real vector operations}
\label{sec:RX}

See \cite[p. 85, ex. 4-9]{spivak-1965}.

Let $\p, \q \in \Reals^{n}$.
Let $\theta(\p,\q)$ be the angle between $\p$ and $\q$.

\begin{itemize}
\item The inner (dot) product:
\begin{equation}
\p \bullet \q \; \equiv \; \sum_{i=0}^{n-1} p_i q_i
\end{equation}

\item The euclidean ($l_2$) norm:
\begin{equation}
\| \p \|^2 \; \equiv \; \p \bullet \p
\end{equation}
\begin{equation}
\p \bullet \q \; = \; \| \p \| \| \q \| \cos(\theta(\p,\q))
\end{equation}

\item Orthogonal complement:
\begin{equation}
\p \perp \q \; \equiv \; \p \; - \; \left( \p \bullet \frac{\q}{\|\q\|}\right) \frac{\q}{\|\q\|}
\end{equation}

\item The tensor product

Let $\p \in \Reals^m, \q, \r \in \Reals^n.$
$\p \otimes \q$ is a rank 1 linear transformation
from $\Reals^n$ to $\Reals^m$, defined by:
\begin{equation}
(\p \otimes \q)(\r) \; \equiv \; \p (\q \bullet \r)
\end{equation}

\end{itemize}

%-----------------------------------------------------------------

\subsection{Identities for 3-dimensional vector operations}
\label{sec:R3X}

See \cite[p. 85, ex. 4-9]{spivak-1965}.

Let $\p, \q, \r \in \Reals^3$.
Let $(p_0,p_1,p_2), (q_0,q_1,q_2), (r_0,r_1,r_2), $ be their coordinates
in some orthonormal basis.

The cross product:
\begin{equation}
\p \times \q  \; \equiv \; (p_1 q_2 - p_2 q_1, \; p_2 q_0 - p_0 q_2, \; p_0 q_1 - p_1 q_0)
\end{equation}
\begin{equation}
\p \times \q  \; = \; - \; \q \times \p
\end{equation}
\begin{equation}
\| \p \times \q \| \; = \; \| \p \| \; \| \q \| \; \sin(\theta(\p,\q))
\end{equation}
\begin{equation}
\| \p \times \q \|  \; = \;  \sqrt{\| \p \|^2 \| \q \|^2 \; - \; (\p \bullet \q)^2}
\end{equation}
\begin{equation}
\p \bullet ( \p \times \q ) \; = \; ( \p \times \q ) \bullet \q \; = \; 0
\end{equation}
\begin{equation}
\label{eq:dot_cross}
\p \bullet ( \q \times \r ) \; = \; ( \p \times \q ) \bullet \r \; = \; \q \bullet ( \r \times \p )
\end{equation}
\begin{equation}
\p \times ( \q \times \r ) \; = \; ( \p \bullet \r ) \q \; - \; (\p \bullet \q) \r
\end{equation}
\begin{equation}
( \p \times \q ) \times \r \; = \; ( \p \bullet \r ) \q \; - \; (\q \bullet \r) \p
\end{equation}
\begin{equation}
( \p \times \q ) \times \r \; = \; \left((\q \otimes \p) - (\p \otimes \q)\right) \r
\end{equation}

%------------------------------------------------------------------

\subsection{Functions on real vector spaces}
\label{sec:functions}

This paper describes functions of triangular meshes.

Interesting functions usually depend, directly or indirectly,
on the positions of some subset of the vertices.
I consider the vertex positions to be elements of $\Reals^3$,
with an (implied) universal origin,
and thus do not distinguish points and vectors.

In general, the functions discussed here map between real vector spaces:
$\f:{\Re}^{n} \mapsto \Reals^{m}$, where $\Reals^n$ is the
{\it domain} and $\Reals^m$ is the {\it codomain}.
Strictly speaking, the {\it range} of $\f$ is the set $\f(\Reals^n)$,
which may be a proper subset of its codomain $\Reals^m$.

I typically use $\p$, $\q$, $\r$, etc., for elements of $\Reals^n$
and
$\f$, $\g$, $\h$ for vector-valued functions.
I generally do not distinguish $\Re$, the real numbers,
and $\Reals^1$, the 1-dimensional real vector space.
I sometimes use $f$, $g$, $h$ for extra clarity in the special
case of real-valued functions.

The domains of many interesting functions,
such as those that depend on vertex positions,
are direct sums of $\Reals^3$.
The {\it direct sum} $\Reals^n \oplus \Reals^m$ is the cartesian product
of $\Reals^n$ and $\Reals^m$ --- the set of ordered pairs $(\p,\q)$
where $\p \in \Reals^n$ and $\q \in \Reals^m$ ---
with the restriction that the inner product is the obvious extension of the
inner products on $\Reals^n$ and $\Reals^m$:
$(\p_0,\q_0) \bullet (\p_1,\q_1) = (\p_0 \bullet \p_1) + (\q_0 \bullet \q_1).$
For simplicity, I identify
$\Reals^{3n} = \Reals^3 \oplus \Reals^3 \oplus \cdots \oplus \Reals^3 = \oplus^n \Reals^3
= \Reals^n \oplus \Reals^n \oplus \Reals^n $.
I will usually write an element of $\oplus^n \Reals^3$ as
$(\p_0,\ldots,\p_{n-1})$
and use
$\f(\p_0,\p_1,\ldots,\p_{n-1})$
for a function that depends on $n$ vertices.
Sometimes it will be useful to separate the $x,$ $y,$ and $z$ coordinates:
$\p = (\x,\y,\z),$
where $\x =(x_0, \ldots x_{n-1}) \in \Reals^n$, are the $x$-coordinates
of the positions of the vertices, and similarly for $y$ and $z$.

%------------------------------------------------------------------

\subsection{Derivatives}
\label{sec:derivatives}

One way to view the derivative of a function
$\f:{\Re}^{n} \mapsto \Reals^{m}$,
at a point $\p$,
is as the linear transformation $\L:{\Re}^{n} \mapsto \Reals^{m}$,
that best approximates the local 'slope' of $\f$ at $\p$.
To be a little more precise, we want
\begin{displaymath}
\lim_{ \|{\bf \delta}  \| \mapsto 0}
{ \|{\f(\p + {\bf \delta}) - (\f(\p) + \L({\bf\delta})) \|}
  \over  \|{\bf \delta}  \|}
 = 0
\end{displaymath}
For a concise and correct discussion, see \cite{spivak-1965}.

\begin{itemize}

\item $\Da{\f}$

In its most general form,
I denote the derivative of $\f$ by $\Da{\f}$.
Note that this is
linear-transformation-valued function of the domain of $\f$.

\item $\Db{\f}{\p}$

I denote the derivative of $\f$ at $\p$ by $\Db{\f}{\p}$.
$\Db{\f}{\p}$ is a specific linear transformation from
the domain of $\f$ to the codomain of $\f$.

\item $\Dc{\f}{\p}{\q}$

The derivative is most often represented by the {\it Jacobian},
the $m \times n$ matrix of partial derivatives
with respect to some bases for $\Reals^n$ and $\Reals^m$.
However, it's often easier to express the derivative clearly if we
explicitly include the argument of the linear transformation.
In this case, I write $\Dc{\f}{\p}{\q}$
for the derivative of $f$ at the point $\p$
applied to the vector $\q$.

\item $\Dd{\p_i}{\f}{(\q_0,\ldots,\q_{n-1})}{\r_i}$

For functions on direct sum spaces,
$\f(\p_0,\p_1,\ldots,\p_{n-1})$, $\p_i \in \Reals^{n_i}$,
it's often easier to consider the derivative
with respect to one argument at a time.
I write $\Dd{\p_i}{\f}{(\q_0,\ldots,\q_{n-1})}{\r_0,\ldots,\r_{n-1}}$
for the derivative of $\f$ with respect to $\p_i$,
at the point $(\q_0,\ldots,\q_{n-1}) \in \oplus_{i=0}^{n-1} \Reals^{m_i}$,
applied to the vector $\r_i \in \Reals^{n_i}$.
Note that, if you consider $\f$ to be a function
of direct sums of $\Reals^1$, we have the usual
partial derivatives.

\end{itemize}

%------------------------------------------------------------------

\subsubsection{Gradients of real-valued functions}
\label{sec:gradients}

\begin{itemize}

\item $\Ga{f}$

In minimizing real-valued functions, $f(\p)$, $\p \in \Reals^n$,
we frequently need
the {\it gradient,} $\Ga{f} \in \Reals^n$,
the vector pointing in the direction of most rapid increase of $f$,
whose magnitude is the rate of increase, or slope,
of $f$ in that direction.

The gradient, $\Ga{f}$,
has a close relationship to the derivative, $\Da{f}$,
and the two are often confused.
Recall that the derivative is a linear transformation
from the domain of $f$ to its codomain.
In the case of real-valued functions,
this means the derivative is a linear function on $\Reals^n$,
an element of the dual space of $\Reals^n$, a 'row' vector.
It's easy to see that the gradient is simply the dual (the 'transpose')
of the derivative, $\Ga{f} = (\Da{f})^{\dagger}$
(see \cite[p. 96, ex. 4-18]{spivak-1965}).

Notation for the various versions of the gradient
follows that for derivatives:

\item $\Gb{f}{\q}$

The gradient of $f$ at $\q$.

\item $\Gc{\p_i}{f}{\q}$

The gradient of $f$
with respect to $\p_i$ at $\q$.

\item $(\Gb{f}{\q}) \bullet \; \r$

The analog to exressing the derivative as a linear transformation
with an explicit argument is to write expressions for
the inner product of the gradient and an arbitrary other vector $\r$

\item $(\Gc{\p_i}{f}{\q}) \bullet \;\r_i$

See above.

\end{itemize}

%------------------------------------------------------------------

\subsubsection{Chain rule}
\label{sec:chain}

The most general identity used in computing derivatives is the {\it chain rule.}
Suppose
$\f:\Reals^{n_0} \mapsto \Reals^{n_1}$,
$\g:\Reals^{n_1} \mapsto \Reals^{n_2}$,
and
$\h = \g \circ \f : \Reals^{n_0} \mapsto \Reals^{n_2}.$
Then
\begin{equation}
\label{eq:chain-rule}
\Db{\h}{\p} \;\; = \;\; \Db{(\g \circ \f)}{\p}
            \;\; = \;\; \Db{\g}{\f(\p)} \; \circ \; \Db{\f} {\p}.
\end{equation}

See \cite[Theorem~2-2]{spivak-1965}, .

%------------------------------------------------------------------

\subsubsection{Derivatives of multilinear functions}
\label{sec:multilinear}

A function $\f(\p_0,\ldots,\p_k):\Reals^{n_0} \oplus \Reals^{n_k} \mapsto \Reals^m$
is {\it multilinear} if
\begin{equation}
\f(a_{00} \p_{00} + a_{01} \p_{01}, \ldots, a_{k0} \p_{k0} + a_{k1} \p_{k1})
\; = \; \sum_{i_0,\ldots,i_k = 0,1} \;(a_{0i_0} \cdots a_{ki_k}) \f(\p_{0i_0}, \ldots, \p_{ki_k}).
\end{equation}

The derivative of $\f$
at the point $(\p_0,\ldots,\p_k)$, applied to the vector $(\q_0,\ldots,\q_k)$ is

\begin{equation}
\Dc{\f}{(\p_0,\ldots,\p_k)}{\q_0,\ldots,\q_k}
\; = \; \sum_{i=0,k} \f(\p_0,\ldots,\p_{i-1},\q_i,\p_{i+1},\ldots,\p_k).
\end{equation}

See \cite[ex.~2-14]{spivak-1965}, .

%------------------------------------------------------------------

\subsubsection{Derivatives of bilinear functions}
\label{sec:bilinear}

Bilinear functions are a useful special case of multilinear functions.

A function $\f(\p,\q):\Reals^{n_0} \oplus \Reals^{n_1} \mapsto \Reals^m$
is {\it bilinear} if
\begin{eqnarray}
\f(a_0 \p_0 + a_1 \p_1, b_0 \q_0 + b_1 \q_1) & = & a_0 b_0 f(\p_0,\q_0)  \\
                                             & + & a_0 b_1 f(\p_0,\q_1) \nonumber \\
                                             & + & a_1 b_0 f(\p_1,\q_0) \nonumber \\
                                             & + & a_1 b_1 f(\p_1,\q_1).\nonumber
\end{eqnarray}

The derivative of $\f$
at the point $(\p_0,\q_0)$, applied to the vector $(\p,\q)$ is

\begin{equation}
\Dc{\f}{(\p_0,\q_0)}{\p,\q} = \f(\p_0,\q) + \f(\p,\q_0).
\end{equation}

See \cite[ex.~2-12]{spivak-1965}.

%------------------------------------------------------------------

\subsubsection{Derivatives of linear functions}
\label{sec:Derivatives-of-linear-functions}

Linear functions are another useful special case of multilinear functions.
A function $\f(\p):\Reals^{n} \mapsto \Reals^m$
is {\it linear} if
\begin{equation}
\f(a_0 \p_0 + a_1 \p_1)
 =
a_0 \f(\p_0) + a_1 \f(\p_1)
\end{equation}

The derivative of $\f$ is simply $\f$ itself.

%------------------------------------------------------------------

\subsubsection{Derivatives of inner products}
\label{sec:inner}

We can view the inner product on $\Reals^m$, $\p \bullet \q$,
as a bilinear function $d(\p,\q) : \Reals^m \oplus \Reals^m \mapsto \Re$.
Thus
\begin{equation}
\Dc{d}{(\p_0,\q_0)}{\p,\q} = \p_0 \bullet \q + \p \bullet \q_0.
\end{equation}

Suppose
$\f:\Reals^{n} \mapsto \Reals^{m}$, and
$\g:\Reals^{n} \mapsto \Reals^{m}$.
The derivative of $\f \bullet \g$ is:
\begin{eqnarray}
\label{eq:dot_derivative}
\Dc{(\f \bullet \g)}{\p_0}{\p}
& =
& \Db{d}{(\f(\p_0),\g(\p_0))} \;\circ \;(\Dc{\f}{\p_0}{\p}, \Dc{\g}{\p_0}{\p})
\\
& =
& \f(\p_0) \bullet \Dc{\g}{\p_0}{\p} \; + \; \g(\p_0) \bullet \Dc{\f}{\p_0}{\p} \nonumber
\end{eqnarray}

See \cite[ex.~2-13]{spivak-1965}.

%------------------------------------------------------------------

\subsubsection{Derivatives of cross products}
\label{sec:cross}

We can view the 3-dimensional cross product
$ \times $
as a bilinear function
$\c(\p,\q) = \p \times \q : \Reals^3 \oplus \Reals^3 \mapsto \Reals^3$.
As with the inner product,
the derivative is
\begin{equation}
\Dc{c}{(\p_0,\q_0)}{\p,\q} = \p_0 \times \q + \p \times \q_0.
\end{equation}

Suppose
$\f:\Reals^{n} \mapsto \Reals^3$, and
$\g:\Reals^{n} \mapsto \Reals^3$.
The derivative of $\f \times \g$ is:
\begin{eqnarray}
\Dc{(\f \times \g)}{\p_0}{\p}
& =
& \Db{\c}{(\f(\p_0),\g(\p_0))} \;\circ \;(\Dc{\f}{\p_0}{\p}, \Dc{\g}{\p_0}{\p})
\\
& =
& \f(\p_0) \;\times \;\Dc{\g}{\p_0}{\p} \;+ \;\Dc{\f}{\p_0}{\p} \;\times \;\g(\p_0) \nonumber
\end{eqnarray}

%------------------------------------------------------------------

\subsubsection{Derivatives of scalar products}
\label{sec:scalar}

Suppose
$f:\Reals^{n} \mapsto \Re$, and
$\g:\Reals^{n} \mapsto \Reals^m$.
It follows from the chain rule that the derivative of $\h = f\g$ is:
\begin{eqnarray}
\label{eq:scalar_product_derivative}
\Db{(f\g)}{\p}
& = & f(\p) \;\Db{\g}{\p} \;+ \g(\p) \; \Db{f}{\p}  \\
& = & f(\p) \;\Db{\g}{\p} \;+ \g(\p) \otimes \Gb{f}{\p} \; \nonumber
\end{eqnarray}

%------------------------------------------------------------------

\subsubsection{Derivatives of euclidean norms}
\label{sec:norms}

Let $l_2(\p) = \; \| \p  \|: \Reals^n \mapsto \Re$
be the usual euclidean norm on $Re^n$.
Let $l_2^2(\p) = \; \| \p  \|^2 $
be its square
($ \| \p  \|^2  = \sum_{i=0,n-1} \p_i^2$),
and $ \| \p  \|^3$ the cube.
\begin{eqnarray}
\label{eq:l2-gradient}
\Gb{l_2}{\p} & = & {{ \p } \over { \| \p  \|}} \\
\Db{l_2}{\p} & = &{{ \p^\dagger } \over { \| \p  \|}} \nonumber \\
\Gb{l_2^2}{\p} & = & 2\p \nonumber \\
\Db{l_2^2}{\p} & = & 2\p^\dagger \nonumber \\
\Gb{l_2^3}{\p} & = & 3 \| \p  \| \p \nonumber \\
\Db{l_2^3}{\p} & = & 3 \| \p  \| \p^\dagger \nonumber
\end{eqnarray}

Let $\f(\p) : \Reals^n \mapsto \Reals^m$.
By the chain rule:
$\Db{\| \f \|^2}{\p}  =  2 {\f(\p)}^{\dagger} \Db{\f}{\p} $.

\begin{equation}
\Gb{\| \f \|^2}{\p}  =  2 \;\Db{\f}{\p}^\dagger \;\circ \;\f(\p)
\end{equation}

\begin{eqnarray}
\label{eq:norm_derivative}
\Db{\| \f \|}{\p}
& = &
{{\f(\p)^\dagger} \over {\| \f(\p) \|}} \Db{\f}{\p}  \\
\Gb{\| \f \|}{\p}
& = &
\left(\Db{\f}{\p}\right)^\dagger \;\circ \;{{\f(\p)} \over { \| \f(\p)  \|}}
\label{eq:norm_gradient}
\end{eqnarray}

%------------------------------------------------------------------

\subsubsection{Derivatives of normalized functions}
\label{sec:Derivatives-of-normalized-functions}

Let $\tilde{\f}$ be the normalized version of $\f$:
\begin{equation}
\tilde{\f} \;= \;{{\f} \over {\| \f \|}}
\end{equation}

Then, from equations \ref{eq:scalar_product_derivative}
and \ref{eq:norm_derivative}:
\begin{eqnarray}
\Dc{\tilde{\f}}{\p}{\q}
& = &
\Dc{\left({\f \over {\| \f \|}}\right)}{\p}{\q}
\\
& = &
{\Dc{\f}{\p}{\q} \over {\| \f(\p) \|}}
\; + \;
\f(\p) \; \Dc{\left({1 \over {\| \f \|}}\right)}{\p}{\q} \nonumber \\
& = &
{\Dc{\f}{\p}{\q} \over {\| \f(\p) \|}}
\; - \;
\f(\p) {{\Dc{\| \f \|}{\p}{\q}} \over {\|\f(\p)\|^2}} \nonumber \\
& = &
{\Dc{\f}{\p}{\q} \over {\| \f(\p) \|}}
\; - \;
\f(\p) \left( {{\f(\p)^\dagger} \over {\| \f(\p) \|^3}} \;\Dc{\f}{\p}{\q} \right) \nonumber \\
& = &
{\| \f(\p) \|^2 \Dc{\f}{\p}{\q}
\; - \;
\f(\p)\left( \f(\p) \bullet \Dc{\f}{\p}{\q} \right) }
\over {\| \f(\p) \|^3}  \nonumber \\
& = &
{{\| \f(\p) \|^2 \I_{\Reals^3} \;- \;\left( \f(\p) \otimes \f(\p) \right)  }
\over {\| \f(\p) \|^3} }
\;\Dc{\f}{\p}{\q} \nonumber \\
& = &
{{\I_{\Reals^3} \;- \;\left( \tilde{\f}(\p) \otimes \tilde{\f}(\p) \right)  }
\over {\| \f(\p) \|} }
\;\Dc{\f}{\p}{\q} \nonumber
\end{eqnarray}

$\otimes$ is the elementary tensor product operation.
If you are stuck thinking in terms of row and column vectors,
$\p \otimes \q \;= \;\p\q^\dagger$.
More generally, if $\p \in \Reals^m$ and $\q \in \Reals^n$,
then $\p \otimes \q$ is the rank 1 linear transformation from $\Reals^n \mapsto \Reals^m$:
$\left(\p \otimes \q\right) (\r) \;= \;\p \left(\q \bullet \r\right)$.

We can write the derivative above without reference to the argument $\q$:
\begin{eqnarray}
\label{eq:normalized_function_derivative}
\Db{\tilde{\f}}{\p}
& = &
\Db{\left({\f \over {\| \f \|}}\right)}{\p}  \\
& = &
{{\I_{\Reals^3} \;- \;\left( \tilde{\f}(\p) \otimes \tilde{\f}(\p) \right) }
\over {\| \f(\p) \|} }
\;\Db{\f}{\p} \nonumber
\end{eqnarray}

A common, trivial, normalized function is the normalized version of
a vector:
\begin{equation}
\tilde{\p} \;= \;{{\p} \over {\| \p \|}}
\end{equation}

From equation \ref{eq:normalized_function_derivative}
it follows that:
\begin{eqnarray}
\label{eq:normalized_vector_derivative}
\Db{\tilde{\p}}{\q}
& = &
\Db{\left({\p \over {\| \p \|}}\right)}{\q}
\\
& = &
{{\I_{\Reals^3} \;- \;\left( \tilde{\q} \otimes \tilde{\q} \right) }
\over {\| \q \|} }
\nonumber
\\
& = &
{{\| \q \|^2 \I_{\Reals^3} \;- \;\left( \q \otimes \q \right) }
\over {\| \q \|^3} }
\nonumber
\end{eqnarray}

%------------------------------------------------------------------

\subsubsection{Derivatives of angles}
\label{sec:derivatives-of-angles}

The angle between 2 vectors $\p_0, \p_1 \in \Reals^m$, is the inverse cosine
of their normalized inner product:
\begin{equation}
\theta(\p_0,\p_1)
=
\cos^{-1}
\left(
{ \p_0 \bullet \p_1 } \over {\|\p_0\| \|\p_1\|}
\right)
\end{equation}
Recall that the derivative of the $\cos^{-1}$ is:
\begin{equation}
\frac{d}{\mathit dx} \cos^{-1}(x) = { -1 \over \sqrt{1 - x^2} }
\end{equation}
It follows that:
\begin{eqnarray}
\label{eq:angle_gradient}
\Gc{\p_0}{\theta(\p_0,\p_1)}{\q}
& = &
{{-1} \over
{ \sqrt{1 - \left( {\q_0 \bullet \q_1} \over {\| \q_0 \| \| \q_1 \|} \right)^2 }}}
\Gc{\p_0}{\left( {\q_0 \bullet \q_1} \over {\| \q_0 \| \| \q_1 \|} \right)} {\q}
\\
& = &
{
{-\|\q_0\|\|\q_1\|}
\over
{ \sqrt{\|\q_0\|^2\|\q_1\|^2 - \left( \q_0 \bullet \q_1 \right)^2 }}
}
\left[
{{\q_1} \over {\|\q_0\|\|\q_1\|}}
+
{{\left( \q_0 \bullet \q_1 \right)} \over {\| \q1 \|}}
\Gc{\p_0}{\left( {1} \over {\| \p_0 \|} \right)} {\q}
\right]
\nonumber
\\
& = &
{
{-\|\q_0\|\|\q_1\|}
\over
{ \sqrt{\|\q_0\|^2\|\q_1\|^2 - \left( \q_0 \bullet \q_1 \right)^2 }}
}
\left[
{{\q_1} \over {\|\q_0\|\|\q_1\|}}
-
{{\left( \q_0 \bullet \q_1 \right) \q0} \over {\| \q1 \| \|\q_0\|^3}}
\right]
\nonumber
\\
& = &
{
{-1}
\over
{ \sqrt{\|\q_0\|^2\|\q_1\|^2 - \left( \q_0 \bullet \q_1 \right)^2 }}
}
\left[
\q_1
-
{{\left( \q_0 \bullet \q_1 \right) \q0} \over {\|\q_0\|^2}}
\right]
\nonumber
\\
& = &
{
{- \q_1 \perp \q_0}
\over
{ \sqrt{\|\q_0\|^2\|\q_1\|^2 - \left( \q_0 \bullet \q_1 \right)^2 }}
}
\nonumber
\\
&  &
\nonumber
\\
\Gc{\p_1}{\theta(\p_0,\p_1)}{\q}
& = &
{
{- \q_0 \perp \q_1}
\over
{ \sqrt{\|\q_0\|^2\|\q_1\|^2 - \left( \q_0 \bullet \q_1 \right)^2 }}
}
\nonumber
\end{eqnarray}