\documentclass{article}
\usepackage{parskip}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{cancel}
\usepackage{algorithmic}
\usepackage{xfrac}

\setlength{\parindent}{0cm}

\begin{document}

\title{CPSC 413 \\ Assignment 3 - Divide and Conquer}
\author{Andrew Helwer}
\date{March 2011}
\maketitle

\section{Bank Fraud Detection}

This problem is simply a restatement of the majority element problem - given a
collection of elements of size $n$, does there exist an element equivalent to
more than $n/2$ elements of the collection? The obvious (and correct) algorithm
is to simply check equality between all elements of the collection, but this
requires $O(n^2)$ checks no matter how many heuristic optimizations are
included.

If a divide and conquer algorithm is employed, however, the algorithm becomes
much faster - on the order of $O(n \log n)$, as we shall prove. An even faster
algorithm exists using the Monte Carlo method, running in $O(n)$.

The name of the function implementing the algorithm will be $majElem(S)$, which
returns $S' \subseteq S$ if a majority element exists. If $x \in S'$ then $x$ is
equivalent to the majority element. If no majority element exists, $\emptyset$
is returned. The answer to the original problem is Yes if a nonempty set is
returned, and No otherwise.

If two cards $a$ and $b$ are found equivalent using the card scanner, write $(a
\sim b)$. Note that by the transitivity property of an equivalence relation, if
$(a \sim b)$ and $(b \sim c)$, then $(a \sim c)$.

\textbf{1) Describe how to solve trivial instances:}

The trivial case occurs when $n=1$ - that is, there exists only a single element
in the set. Clearly, that element is the majority element of its set. So
$majElem(\{a\})$ returns $\{a\}$.

\textbf{2) Describe how sub-instances should be formed from nontrivial ones:}

Consider the case for an arbitrary set of elements, $S$, of size $n>1$. A
sub-instance of the problem will be constructed by forming two sets $S_L$ and
$S_R$ that partition $S$, where $|S_L| = \sfrac{n}{2}$ and $|S_R| = n - |S_L|$.
The function will then recurse on $S_L$ and $S_R$ by calling $majElem(S_L)$ and
$majElem(S_R)$.

\textbf{3) Describe how a solution is formed from sub-instance solutions:}

Claim 3.1: If $S$ contains a majority element $a$, then $a$ is a majority
element in $S_L$ or $a$ is a majority element in $S_R$.

Proof: Assume that $S$ contains a majority element $a$, where $|S|=n$. Then
$a$ is equivalent to more than $\sfrac{n}{2}$ elements of $S$. If $S$ is
partitioned into
two disjoint sets $S_L$ and $S_R$, we have:
\begin{equation}
\label{eq:sumSize}
|S_L|+|S_R|=n
\end{equation}
Since $S = S_L \sqcup S_R$, $a$ must be equivalent to more than $\sfrac{n}{2}$
elements
of $S_L \sqcup S_R$, so we have:
\begin{equation}
\label{eq:sumEquiv}
|\{s \in S_L : a \sim s\}| + |\{s \in S_R : a \sim s\}| > \frac{n}{2}
\end{equation}
Without loss of generality, consider the case where $a$ is not a majority
element of $S_L$. Then:
\begin{align*}
|\{s \in S_L : a \sim s\}| &\leq \frac{|S_L|}{2} \\
|\{s \in S_L : a \sim s\}| - \frac{n}{2} &\leq \frac{|S_L|}{2} - \frac{n}{2} \\
\frac{n}{2} - |\{s \in S_L : a \sim s\}| &\geq \frac{n}{2} - \frac{|S_L|}{2} \\
|\{s \in S_R : a \sim s\}| &> \frac{n}{2} - \frac{|S_L|}{2}
&\text{by eqn (\ref{eq:sumEquiv})} \\
|\{s \in S_R : a \sim s\}| &> \frac{n}{2} - \frac{n-|S_R|}{2}
&\text{by eqn (\ref{eq:sumSize})} \\
|\{s \in S_R : a \sim s\}| &> \frac{|S_R|}{2}
\end{align*}
Therefore if $a$ is not a majority element of $S_L$ then $a$ must be a majority
element of $S_R$. By symmetry, if $a$ is not a majority element of $S_R$ then
$a$ must be a majority element of $S_L$. Thus we have proved Claim 3.1.
$\square$

Claim 3.2: If $majElem(S_L)$ returns the nonempty set $S_L' \subseteq S_L$,
$majElem(S_R)$ returns $\emptyset$, and
\begin{equation}
\label{eq:notMaj}
|S_L' \sqcup \{s \in S_R : a \sim s, a \in S_L'\}| \leq \frac{|S|}{2}
\end{equation}
then $S$ does not contain a majority element.

Proof: Assume the antecedent. We wish to prove that $S$ then does not contain a
majority element, so assume that $S$ does contain a majority element, $a$, where
$|S|=n$. Then by Claim 3.1, $a$ is a majority element of $S_L$ or $a$ is a
majority element of $S_R$. Since $S_R$ does not contain a majority element,
$S_L$ must contain a majority element. As it is impossible for there to be two
majority elements in a set, the majority element of $S_L$ must be $a$, where
$a$ is equivalent to every element in $S_L'$. Note:
\begin{align*}
|\{s \in S_L : a \sim s\}| + |\{s \in S_R : a \sim s\}| &> \frac{n}{2}
&\text{by eqn (\ref{eq:sumEquiv})} \\
|S_L'| + |\{s \in S_R : a \sim s\}| &> \frac{n}{2} \\
|S_L' \sqcup \{s \in S_R : a \sim s\}| &> \frac{n}{2}
\end{align*}
However, by equation ($\ref{eq:notMaj}$), this cannot be true, and so by
contradiction $S$ must not contain a majority element. Thus we have proved
Claim 3.2. $\square$

Claim 3.3: If $majElem(S_L)$ returns the nonempty set $S_L' \subseteq S_L$,
$majElem(S_R)$ returns the nonempty set $S_R' \subseteq S_R$, and
\begin{align*}
|S_L' \sqcup \{s \in S_R : a \sim s, a \in S_L'\}| &\leq \frac{|S|}{2} \\
|S_R' \sqcup \{s \in S_L : a \sim s, a \in S_R'\}| &\leq \frac{|S|}{2}
\end{align*}
then $S$ does not contain a majority element.

Proof: Assume the antecedent. We wish to prove that $S$ then does not contain a
majority element, so assume that $S$ does contain a majority element, $a$, where
$|S|=n$. Then by Claim 3.1, $a$ is a majority element of $S_L$ or $a$ is a
majority element of $S_R$. So we have two cases:
\begin{itemize}
\item Case One: $a$ is a majority element of $S_L$. Then $a$ is equivalent
to every element of $S_L'$, and this case is equivalent to the one considered
in Claim 3.2, so a contradiction arises.
\item Case Two: $a$ is a majority element of $S_R$. Then $a$ is equivalent
to every element of $S_R'$, and this case is symmetric to the one considered
in Claim 3.2, so a contradiction arises.
\end{itemize}
Since a contradiction results in every case when $S$ is assumed to contain a
majority element, $S$ must not contain a majority element. Thus we have proved
Claim 3.3. $\square$

Upon recursing on $S_L$ and $S_R$, where $|S|=n$, there are four possible cases:

\begin{enumerate}
\item $majElem(S_L)$ returns $S_L'$ and $majElem(S_R)$ returns $\emptyset$

Execute the following algorithm, for any $b \in S_L'$:
\begin{algorithmic}
\FOR {$a \in S_R$}
	\IF {$(a \sim b)$}
		\STATE $S_L' \gets S_L' \cup \{a\}$
	\ENDIF
\ENDFOR
\end{algorithmic}
If $|S_L'| > \sfrac{n}{2}$, then $S$ contains a majority element by definition,
and since a set may only have one majority element $S_L'$ should be returned.
If $|S_L'| \leq \sfrac{n}{2}$, then by Claim 3.2 no majority element exists in
$S$, so $\emptyset$ should be returned.

\item $majElem(S_L)$ returns $\emptyset$ and $majElem(S_R)$ returns $S_R'$

This case is symmetric to case 1, with Claim 3.2 able to be used as the proof
for it is also symmetric.

\item $majElem(S_L)$ returns $S_L'$ and $majElem(S_R)$ returns $S_R'$

Execute the following algorithm, for any $b \in S_L'$:
\begin{algorithmic}
\FOR {$a \in S_R$}
	\IF {$(a \sim b)$}
		\STATE $S_L' \gets S_L' \cup \{a\}$
	\ENDIF
\ENDFOR
\end{algorithmic}
If $|S_L'| > \sfrac{n}{2}$, then $S$ contains a majority element by definition,
and since a set may only have one majority element $S_L'$ should be returned.
If $|S_L'| \leq \sfrac{n}{2}$, then execute the following algorithm, for any $b
\in S_R'$:
\begin{algorithmic}
\FOR {$a \in S_L$}
	\IF {$(a \sim b)$}
		\STATE $S_R' \gets S_R' \cup \{a\}$
	\ENDIF
\ENDFOR
\end{algorithmic}
If $|S_R'| > \sfrac{n}{2}$, then $S$ contains a majority element by definition,
and since a set may only have one majority element $S_R'$ should be returned.
If $|S_R'| \leq \sfrac{n}{2}$, then by Claim 3.3 no majority element exists in
$S$, so $\emptyset$ should be returned.

\item Both $majElem(S_L)$ and $majElem(S_R)$ return $\emptyset$

By the converse of Claim 3.1, $S$ does not contain a majority element, and so
$\emptyset$ should be returned.

\end{enumerate}

\textbf{4) Describe a recursive algorithm implementation:}

$majElem(S)$
\begin{algorithmic}[1]
\IF {$|S|=1$}
	\RETURN $S$
\ELSE
	\STATE Split $S$ into $S_L$ and $S_R$
	\STATE $S_L' \gets majElem(S_L)$ and $S_R' \gets majElem(S_R)$
	\IF {$S_L' \neq \emptyset$ and $S_R' = \emptyset$}
		\STATE $S_L' \gets appendEq(S_L',S_R)$
		\IF {$|S_L'| > \sfrac{|S|}{2}$}
			\RETURN $S_L'$
		\ELSE
			\RETURN $\emptyset$
		\ENDIF
	\ELSIF {$S_L' = \emptyset$ and $S_R' \neq \emptyset$}
		\STATE $S_R' \gets appendEq(S_R',S_L)$
		\IF {$|S_R'| > \sfrac{|S|}{2}$}
			\RETURN $S_R'$
		\ELSE
			\RETURN $\emptyset$
		\ENDIF
	\ELSIF {$S_L' \neq \emptyset$ and $S_R' \neq \emptyset$}
		\STATE $S_L' \gets appendEq(S_L',S_R)$
		\IF {$|S_L'| > \sfrac{|S|}{2}$}
			\RETURN $S_L'$
		\ELSE
			\STATE $S_R' \gets appendEq(S_R',S_L)$
			\IF {$|S_R'| > \sfrac{|S|}{2}$}
				\RETURN $S_R'$
			\ELSE
				\RETURN $\emptyset$
			\ENDIF
		\ENDIF
	\ELSE
		\RETURN $\emptyset$
	\ENDIF
\ENDIF
\end{algorithmic}

$appendEq(S_1, S_2)$
\begin{algorithmic}[1]
\STATE $b \gets \text{any } s \in S_1$
\FOR {$a \in S_2$}
	\IF {$(a \sim b)$}
		\STATE $S_1 \gets S_1 \cup \{a\}$
	\ENDIF
\ENDFOR
\RETURN $S_1$
\end{algorithmic}

\textbf{5) Prove correctness of the algorithm using induction:}

Base Case: Calling $majElem(\{a\})$ results in the If statement in instruction
1 being satisfied, which causes instruction 2 to execute, which would return
$\{a\}$. Thus the base case holds.

Inductive Case:

Inductive Hypothesis - assume $majElem(S)$ is correct for all sets of size
greater than 1 to $n$. Consider the case where $|S| = n+1$:

Instruction 1 will not be satisfied, so instruction 4 will be executed - this
partitions $S$ into $S_L$ and $S_R$, where both $|S_L| \geq 1$ and $|S_R| \geq
1$. Thus both $|S_L| \leq n$ and $|S_R| \leq n$, so in instruction 5
$majElem(S_L)$ and $majElem(S_R)$ return correct output by the Inductive
Hypothesis. The If statements in instructions 6, 13, 20, and 32 branch into the
cases 1, 2, 3, and 4 (outlined in part 3 above) respectively, and implement the
algorithms described for each case; the correctness of these algorithms was also
proved in part 3 above, and so $majElem(S)$ returns correct output for case
$|S|=n+1$.

Thus correctness of $majElem(S)$ has been proved by induction. $\square$

\textbf{6) Find and prove a recurrence for cost of the algorithm:}

The recurrence in this case will not focus on the number of instructions
executed; rather, the object of concern is the number of equivalence tests
performed. The only point in the algorithm where an equivalence test is
performed is in the sub-function $appendEq(S_1,S_2)$, which is called from
instructions 7, 14, 21, and 25 in $majElem(S)$.

Within $appendEq(S_1,S_2)$, an equivalence test is performed on every element of
$S_2$ - thus it is performed $|S_2|$ times. If we look at every point
$appendEq(S_1,S_2)$ is called from $majElem(S)$, we see it is in all cases
called with the size of $S_2$ decided by instruction 4. The intention of
instruction 4 is to split $S$ into $S_L$ and $S_R$ - two sets of nearly equal
size, so the size of $S_2$ will be either $\lfloor \sfrac{|S|}{2} \rfloor$ or
$\lceil \sfrac{|S|}{2} \rceil$.

If the If statements on lines 20 and then 22 are not satisfied, we see that on a
single level of recursion $appendEq(S_1,S_2)$ will be called twice - first with
$S_2=S_R$ and then with $S_2=S_L$. Since $S_L$ and $S_R$ partition $S$, this
means exactly $|S|$ equivalence tests will be performed per level of recursion
in the worst case.

$majElem(S)$ recurses on both $S_L$ and $S_R$, which if $|S|$ is even are of
size exactly $\sfrac{|S|}{2}$. It suffices to consider only cases in which
$|S|$ is even, as when $|S|$ is odd the number of equivalence tests performed
will be bounded above by the number of tests performed on some set $S'$ where
$|S'| = |S|+1$. Coupled with the observation that in the base case no
equivalence tests are performed, we define the function $T(n)$, the number of
equivalence tests performed on input of size $n$:

\begin{displaymath}
T(n) \leq \left\{
	\begin{array}{lr}
	0 & : n=1 \\
	2T(\sfrac{n}{2}) + n & : n>1
	\end{array}
	\right.
\end{displaymath}

\textbf{7) Use the recurrence to prove an upper bound on running time:}

The Master Theorem allows us to easily bound recurrences of the following form:
\begin{equation*}
T(n) = a \; T\!\left(\frac{n}{b}\right) + f(n)  \;\;\;\; \mbox{where} \;\; a
\geq 1 \mbox{, } b > 1
\end{equation*}
The recurrence we found is indeed of this form, so we may use the theorem.

In our recurrence, $a=2$, $b=2$, and $f(n) = n$. First, we must show that $f(n)
\in \Theta(n^{\log_ba})$:
\begin{align*}
f(n) &\in \Theta(n^{\log_ba}) \\
n &\in \Theta(n^{\log_22}) \\
n &\in \Theta(n)
\end{align*}
This is clearly true. Thus from the Master Theorem, we know the following:
\begin{align*}
E(n) &\in \Theta(n^{\log_ba}\log n) \\
E(n) &\in \Theta(n^{\log_22}\log n) \\
E(n) &\in \Theta(n \log n)
\end{align*}
So, $E(n) \in O(n \log n)$.

\section{Minimal Vertex Cover}

Building on the material covered in the handout on an efficient algorithm to
calculate the minimal vertex cover of a graph,$^{[1]}$ we may consider an
algorithm identical to the one outlined, but where specific conditions are
placed on the vertex $v$ selected. The vertex $v$ selected for an input graph
$G$ should be the vertex of highest order in that graph - that is, the vertex
touching the most edges.

If the highest-order vertex is of order one, then $G$ must consist only of
disjoint pairs of vertices (vertices of order one) and vertices without any
edges (vertices of order zero). $G$ would be very easy to find a minimal vertex
cover for in this special case (simply pick one vertex in each disjoint pair),
so the algorithm should detect this and correctly select the minimal vertex
cover. Given the ease of computation in this case, it will not be considered for
bounding the worst case above.

If the highest-order vertex is of order two, then $G$ must consist only of
$n\geq1$ disjoint subgraphs that are either a perfect cycle (so every vertex in
that subgraph is exactly of order two) or a chain of vertices (so every vertex
has order two except for the ends of the chain, which have order one). A minimal
vertex cover is also simple to compute in this case using a greedy algorithm,
which was produced in tutorial 6.

If the highest-order vertex is of order three, the problem becomes vastly more
complex. In this case, then, the algorithm defined in the minimal vertex cover
handout will be used. However, since we need not worry about cases where only
two or three vertices are removed when recursing the second time, we may
rewrite the recurrence thus:
\begin{displaymath}
T(n) \leq \left\{
	\begin{array}{lr}
	2 & : n=0\text{ or }n=1 \\
	T(n-1) + T(n-4) + cn^3 	& : n>1
	\end{array}
	\right.
\end{displaymath}
This has an associated characteristic polynomial:
\begin{equation*}
x^4 - x^3 - 1
\end{equation*}
This has only one real root - $\gamma \approx 1.38028$. Using the same reasoning
as in the handout, we arrive at the following:
\begin{equation*}
T(n) \in O(\gamma^n)
\end{equation*}
Which of course is equivalent to saying given a graph $G(V,E)$, the algorithm
runs in:
\begin{equation*}
O\left( \gamma^{|V|} \right)
\end{equation*}

\section{References}

\begin{enumerate}
\item Dr. Wayne Eberly, \textit{An Asymptotically Faster Algorithm for Vertex
Cover}. CPSC 413, Winter 2011
\end{enumerate}

\end{document}


\documentclass[11pt, twoside, a4paper]{article}

\usepackage[top=15mm, bottom=15mm, left=25mm, right=25mm]{geometry}
\usepackage{tikz}
\usepackage{amssymb,amsmath, amsthm}
\usepackage[labelsep=period]{caption}

\newtheorem{theorem}{Theorem}[section]
\newtheorem{corollary}{Corollary}[theorem]
\newtheorem{lemma}[theorem]{Lemma}

\theoremstyle{definition}
\newtheorem{definition}{Definition}[section]

\newcommand{\Fig}[1]{Figure~\ref{fig:#1}}
\newcommand{\Eq}[1]{Eq.~(\ref{eq:#1})}
\newcommand{\Eqs}[2]{Eqs.~(\ref{eq:#1})--(\ref{eq:#2})}
\newcommand{\set}[1]{\mathbb{#1}}
\newcommand{\Th}[1]{Theorem~\ref{th:#1}}
\newcommand{\Le}[1]{Lemma~\ref{le:#1}}

\newcommand{\xpm}{\ensuremath{x_\pm}}
\newcommand{\xmp}{\ensuremath{x_\mp}}
\newcommand{\ypm}{\ensuremath{y_\pm}}
\newcommand{\ymp}{\ensuremath{y_\mp}}

\newcommand{\inv}[1]{#1^{-1}}

\begin{document}
\title{Euler 94: Almost equilateral triangles}
\date{}
\author{Didier Pieroux}
\maketitle

%===============================================================================
\begin{abstract}

It is easily proved that no equilateral triangle exists with integral length sides and integral area. However, the almost equilateral triangle 5-5-6 has an area of 12 square units. We shall define an almost equilateral triangle to be a triangle for which two sides are equal and the third differs by no more than one unit. Find the sum of the perimeters of all almost equilateral triangles with integral side lengths and area and whose perimeters do not exceed one billion (1,000,000,000).
\end{abstract}

%===============================================================================
\section{Formulation}

We have to find all isosceles triangles whose base $b\in\set N$ and equal sides $c \in\set N$ differ by one. Let's label $h$ the height of such a triangle (\Fig{triangle1}).
\begin{figure}[h]
    \begin{center}
        \begin{tikzpicture}[scale=0.5]
        \draw (0, 0)
           -- (6, 0) node[midway, below] {b}
           -- (3, 4)
           -- (0, 0) node[midway, above left] {c} ;
        \draw[dotted] (3, 0) -- (3, 4) node[pos=0.33, right] {h};
        \end{tikzpicture}
        \caption{Illustration of the Labels used.}
        \label{fig:triangle1}
    \end{center}
\end{figure}

The problem is then described by the following equations, with $P$ the perimeter and $S$ the area:
\begin{align}
    c^2 & = (b/2)^2 + h^2 \label{eq:pytha1} \\
    c & = b \pm 1 \label{eq:ab} \\
    P & = 2c+b \\
    S & = h b/2 \text{ with } S \in\set N \label{eq:S1}
\end{align}

\begin{lemma}$b$ is even.\end{lemma}
\begin{proof} Suppose $b$ odd. \Eq{S1} implies that $h$ must be even and integer for $S$ to be integral. But then $c^2 = b^2/4 + h^2$ is not integral, which is impossible as $c$ is integral.
\end{proof}

For ease, let us pose $b=2a$ with $a\in\set N$ (\Fig{triangle2}).
\begin{figure}
    \begin{center}
        \begin{tikzpicture}[scale=0.5]
        \draw          (0, 0) -- (3, 0) node[midway, below] {a};
        \draw [dashed] (3, 0) -- (6, 0) -- (3, 4);
        \draw          (3, 4) -- (0, 0) node[midway, above left] {c} ;
        \draw [dotted] (3, 0) -- (3, 4) node[pos=0.33, right] {h};
        \end{tikzpicture}
        \caption{Illustration of the Labels used (reduced problem).}
        \label{fig:triangle2}
    \end{center}
\end{figure}

The \Eqs{pytha1}{S1} read now:
\begin{eqnarray}
c^2 & = & a^2 + h^2  \label{eq:pytha2} \\
  c & = & 2a \pm 1  \label{eq:ac} \\
  P & = & 2(a+c) \\
  S & = & ah \label{eq:S2}
\end{eqnarray}

The triple $(a, h, c)$ is thus Pythagorean\footnote{https://en.wikipedia.org/wiki/Pythagorean\_triple}.

\begin{lemma}
	$a$, $h$ and $c$ are pair-wise coprime.
\end{lemma}

\begin{proof}
	Suppose that $a$ and $c$ have a common factor $\lambda\in\set N$: $a=\lambda a'$ and $c=\lambda c'$. From \Eq{ac}, it comes $c' = 2 a' \pm 1/\lambda$, which would imply that $c'$ is not integral.

	Suppose now that $c$ and $h$ have a common factor. \Eq{pytha2} implies that it must be a factor of $a$; therefore $c$ and $a$ would not be coprime. Similarly if $a$ and $h$ have a common factor, \Eq{pytha2} implies that it must be a factor of $c$; therefore $c$ and $a$ would not be coprime.
\end{proof}

As a consequence valid triple $(a, h, c)$ are primitive Pythagorean triple (PPT).

\section{Tree of primitive Pythagorean triples}
Let the infinitely-deep ternary tree whose root is the Pythagorean triple $p=(3, 4, 5)$ and whose internal nodes are the left-multiplication of their parent node, interpreted as a column vector, by one of the following matrices:
		\[
		A = \left(\begin{matrix}  1 & -2 & 2 \\  2 & -1 & 2 \\  2 & -2 & 3 \end{matrix} \right)\!,\
		B = \left(\begin{matrix}  1 &  2 & 2 \\  2 &  1 & 2 \\  2 &  2 & 3 \end{matrix} \right)\!\text{ and }
		C = \left(\begin{matrix} -1 &  2 & 2 \\ -2 &  1 & 2 \\ -2 &  2 & 3 \end{matrix} \right)\!.
		\]
Otherwise said, the nodes of the tree are, in breadth-first order:
\[p, Ap, Bp, Cp, A\!Ap, B\!Ap, C\!Ap, A\!Bp, B\!Bp, C\!Bp, \ldots\]

\begin{theorem}\label{th:tree}
It can be shown that all the PPTs, and only them, are generated without duplication by this tree\footnote{For a proof see https://en.wikipedia.org/wiki/Tree\_of\_primitive\_Pythagorean\_triples}.
\end{theorem}

Because of \Eq{ac}, we are only interested in PPTs of the form $\xmp\!=\!(x, y, 2x\!\pm\!1)$ and $\ypm\!=\!(x, y, 2y\!\pm\!1)$.
\begin{lemma}
The product of $A$ with a PPT \ypm\ produces a PPT \xmp. Similarly, the product of $C$ with a PPT \xpm\ produces a PPT \ymp.
\end{lemma}
\begin{proof}
By \Th{tree}, the product of a PPT by $A$ or $B$ is also a PPT. Calculating explicitly $A \ypm$ shows that the result has the form \xmp. Idem for $B \xpm$ which produces a \ymp.
\end{proof}

\begin{lemma}\label{le:inverse}
Given a PPT $\pi\!=\!(x, y, z)$,
\begin{itemize}
\item $\inv A\pi$ is a PPT iff $4x<3y$;
\item $\inv B\pi$ is a PPT iff $3x<4y \text{ and } 3y<4x$;
\item $\inv C\pi$ is a PPT iff $4y<3x$.
\end{itemize}
\end{lemma}
\begin{proof}
By carrying out the calculation, it is easily shown that
\begin{itemize}
    \item the Pythagorean relation $x^2+y^2=z^2$ is preserved by multiplication of the inverse matrices,
    \item the components of the result are positive iff the inequalities of the lemme are fulfilled.
\end{itemize}
We still have to show that the result is primitive. If it wasn't, the components of $\inv A\pi$ would have a common factor. But then the components of $A\inv A\pi=\pi$ would also have that common factor, which is forbidden as $\pi$ is primitive.
\end{proof}

\begin{lemma}
Assuming the inequalities of \Le{inverse}, the product of $\inv A$ with a PPT of the form \xpm\ gives a PPT of the form \ymp. Similarly, the product of $\inv C$ with a PPT of the form \ypm\ gives a PPT of the form \xmp.
\end{lemma}
\begin{proof}
From \Le{inverse}, the result is a PPT. The relations between the components are verified through explicit calculation.
\end{proof}

\begin{lemma}\label{le:predecessor1}
Excepted for $(3, 4, 5)$ which has no predecessor, all PPTs of the form \xpm\ have a predecessor of the form \ymp.
\end{lemma}
\begin{proof}
Since \xpm\ is a PPT different of $(3, 4, 5)$, \Th{tree} implies that it has a parent node. If that parent is not of the form \ymp, then \xpm\ is not generated from it by the matrix $A$, but by the matrix $B$ or $C$. Then (\Le{inverse}) implies that $3y<4x$. As \xpm\ fulfils the Pythagorean equality, it comes $x^2+y^2=z^2=4x^2\pm4x+1$, or $y^2=3x^2\pm4x+1$. $3y<4x$ then implies that $11x^2\pm36x+9<0$. This inequality can be verified only for the $-$ sign and if $3/11<x<3$. But this asks for $x=1$ or $x=2$, for which there is no PPT. This contradiction implies that the parent node must be of the from \ymp.
\end{proof}

\begin{lemma}\label{le:predecessor2}
All PPTs of the form \ypm\ have a predecessor of the form \xmp.
\end{lemma}
\begin{proof}The demonstration is similar to the previous one.\end{proof}

\begin{theorem}\label{th:PPT_branch}
Starting from $(3,4,5)$, multiplying it by C, then multiplying the result by A, then multiplying again that result by C\ldots generates all the PPTs satisfying \Eq{ac}.
\end{theorem}
\begin{proof}
Excepted for $(3, 4, 5)$ which has no predecessor, Lemmas~\ref{le:predecessor1} and \ref{le:predecessor2} show that any other PPT satisfying \Eq{ac} must have a predecessor also satisfying \Eq{ac}. Therefore, starting from any of such a PPT and going from predecessor to predecessor, we move towards the root of the PPT tree, until $(3, 4, 5)$ is finally reached.
\end{proof}

\section{Iterative process}
\subsection{Reduction to a unique transformation}
As seen above, multiplying $C$ with a PPT of the form $(a, h, 2a\pm1)$, gives a solution of the form $(h', a', 2a'\mp1)$. Multiplying that result with $A$ produces a result of the form $(a'', h'', 2a''\pm1)$. The $a$ and $h$ components are thus swapped at each iteration. Exchanging the first and second line of $C$, and the first and second line of $A$ avoids this. Interestingly these operations on $C$ and $A$ lead to a same single matrix $D$:
\[D = \left(\begin{matrix} -2 &  1 & 2 \\ -1 &  2 & 2 \\ -2 &  2 & 3 \end{matrix} \right)\]

\Th{PPT_branch} implies that multiplying $(3, 4, 5)$ by D iteratively produces a sequence of all the PPTs satisfying \Eq{ac}.

\subsection{Triangles of increasing size}
Given a element $(a, h, c)$ of the sequence defined above and its successor $(a', h', c')$, it is trivial to show that $a'+c'>2(a+c)$. Otherwise said, the perimeter of the triangles increases by more than a factor 2 after multiplication by $D$.

\section{Solution}
The solution to the original problem is now trivial to state. Starting from $p=(3, 4, 5)$, we generate the sequence $(D^np)_{n\in\set N}$. We compute the corresponding perimeters, i.e.\ two times the sum of the first and the third component, and we sum the perimeters which do not exceed one billion.

Expressed in Haskell, this leads to the following code:
\begin{verbatim}
nextTriangle :: (Int, Int, Int) -> (Int, Int, Int)
nextTriangle (x, y, z) = (-2*x+y+2*z, -x+2*y+2*z,-2*x+2*y+3*z)

main = do
    putStrLn $ show
        $ sum $ takeWhile (<=10^9)
        $ map (\(a, h, c) -> 2*(a+c))
        $ iterate nextTriangle (3, 4, 5)
\end{verbatim}
\end{document}


\documentclass[../main.tex]{subfiles}
\begin{document}
\chapter{Hands-on Algorithmic Problem Solving}
The purpose of this chapter to see how our learned algorithm design principle can be applied into problem solving. We approach problems  using different algorithm design principle and step by step to see how the change in the time and space complexity.

\paragraph{Longest Increasing Subsequence (300L)} Given an unsorted array of integers, find the length of longest increasing subsequence.
\begin{lstlisting}[numbers=none]
Example:

Input: [10,9,2,5,3,7,101,18]
Output: 4
Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
\end{lstlisting}
% \textit{Note: (1) There may be more than one LIS combination, it is only necessary for you to return the length. (2) Your algorithm should run in $O(n^2)$ complexity. Follow up: Could you improve it to $O(n\log  n)$ time complexity?}

% \textbf{Brute-force DFS.} Using the knowledge from DFS and backtracking, the subsequence is similar to the combination: for each element, it has two choice, either be used if it is larger than last element in the path or not be skipped. We can use the following DFS code to generate all possible LIS and obtain the maximum lenghth. This way, because it has two choice at each step to add the element or not, this makes it a binary tree, with the height of the tree to be n. And the worst time complexity is $O(2^n)$.
% \begin{lstlisting}[language=Python]
% # DFS brute-force (using choice-tree)
% def subsequence(self, nums):
%     def dfs(idx, prev):
%         if idx == len(nums):
%             return 0
%         # choice one: use current element: [pre]+
%         taken = 0
%         if nums[idx] > prev:
%             taken = 1 + dfs(idx + 1, nums[idx])
%         # choice two: skip current element
%         not_taken = dfs(idx + 1, prev)
%         return max(taken, not_taken)
% \end{lstlisting}
% However, the above solution makes it difficult to optimize with memoization, , as the solution it uses previous index and current index as keyes. because we have no sense of subproblems, it was simply doing search at the solution space.
\section{Direct Approach}
\subsection{Search in Graph}
In a subsequence, an item can only have two choice: either in or out of the resulting subsequence, this makes our total subsequence $O(2^n)$. As we know the searching process shall always be a search tree, we now start to generate this subsequence, which starts from \texttt{[]}. At the first level, we have item 10 that have two actions: not adding or adding, which makes it a two branches. At the second level, we consider item 9. This makes a search space of a binary tree, and we generate node implicitly since we only need to track the length of the path so far, and we need value of the last item to decide the children of current level. So far, we managed to model our problem as finding the longest path in the search tree, which is a binary tree and with height $n$. We can have the Python code:
\begin{lstlisting}[language=Python]
def lengthOfLIS(self, nums: List[int]) -> int:
    def dfs(nums, idx, cur_len, last_num, ans):
        if idx >= len(nums):
            ans[0] = max(ans[0], cur_len)
            return
        if nums[idx] > last_num:
            dfs(nums, idx+1, cur_len + 1, nums[idx], ans)
        dfs(nums, idx+1, cur_len, last_num, ans)
    ans = [0]
    last_num = -sys.maxsize
    dfs(nums, 0, 0, last_num, ans)
    return ans[0]
\end{lstlisting}
\subsection{Self-Reduction}
 Now, let us us an example smaller than before, say [2, 5, 3, 7], which has the LIS 3 with [2, 3, 7]. Let us consider each state not atomic but as a subproblem. The same tree, but we translate each node differently. We start to consider the problem top down: we have problem [2, 5, 3, 7], and our start index = 0, meaning start from item 2, then our problem is can be divided into different situations:
\begin{itemize}
    \item not take 2: we find the LIS length of subproblem [5, 3, 7]. In this case, our subsequence can start from any of these 3 items, we indicate this case by not changing the previous value. Use \texttt{idx} to indicate the subproblem/subarray, we call dfs that \texttt{idx+1}.
    \item take 2: we need to find  the LIS length of subproblem [5, 3, 7] whose subsequence must start from 5. Thus, we set the \texttt{last\_num} to 5 in the recursive call.
\end{itemize}
Therefore, our code becomes:
\begin{lstlisting}[language=Python]
def lengthOfLIS(self, nums: List[int]) -> int:
    def dfs(nums, idx, last_num):
        if not nums:
            return 0
        if idx >= len(nums):
            return 0
        len1 = 0
        if nums[idx] > last_num:
            len1 = 1 + dfs(nums, idx+1, nums[idx])
        len2 =  dfs(nums, idx+1, last_num)
        return max(len1, len2)

    last_num = -sys.maxsize
    return dfs(nums, 0, last_num)
\end{lstlisting}
In this solution, the time complexity has not improved yet, but from this approach, we can further increase the efficiency with dynamic programming.
% In the example of  Fibonacci Sequence, we discussed four steps to come out with a bottom-up tabulation dynamic programming solution and the key words of each step is highlighted with italic -- state, initialization, recurrence function, and answer. With the example of longest increasing subsequence (LIS), we would further enhance the comparison of complete search with dynamic programming and the tabulation method with the step of formulating the recurrence relation (or state transfer function) by ourselves.

% \textit{Note: (1) There may be more than one LIS combination, it is only necessary for you to return the length. (2) Your algorithm should run in $O(n^2)$ complexity. Follow up: Could you improve it to $O(n\log  n)$ time complexity?}

\subsection{Dynamic Programming}
\paragraph{Memoization} We have known that the recurrence relation takes $LIS(i, prev) = max(LIS(i+1, prev), LIS(i+1, nums[i])$. How many possible states for $LIS(i, prev)$? $i \in [0, n-1]$, and $prev$ can have $n$ candidates too, this makes the whole state space only $n^2$. While, using the depth-first tree search we revisited a state multiple times, which eventually make the time complexity to $O(2^n)$. Now, let us modify the approach and use \texttt{memo} which is a dictionary and takes a tuple \texttt{(i, prev)} as key. If we found the state is not computed, we compute as we do in the previous implementation, if it exists in the memory, however, we just directly return the value and avoid recomputing again:
\begin{lstlisting}[language=Python]
def lengthOfLIS(self, nums: List[int]) -> int:
    def dfs(nums, idx, last_num, memo):
        if idx >= len(nums):
            return 0
        if (idx, last_num) not in memo:
            len1 = 0
            if nums[idx] > last_num:
                len1 = 1 + dfs(nums, idx+1, nums[idx], memo)
            len2 =  dfs(nums, idx+1, last_num, memo)
            memo[(idx, last_num)] = max(len1, len2)
        return memo[(idx, last_num)]

    last_num = -sys.maxsize
    memo = {}
    return dfs(nums, 0, last_num, memo)
\end{lstlisting}

\section{A to B} Another approach is to use the concept of ``prefix'' or ``suffix''. The LIS must start from one of the items in the array. Finding the length of the LIS in the original array can be achieved by comparing n subproblems, the length of LIS of:
\begin{lstlisting}
[2, 5, 3, 7], LIS starts at 2,
[5, 3, 7], LIS starts at 5,
[3, 7], LIS starts at 3
[7], LIS starts at 7
\end{lstlisting}
\subsection{Self-Reduction}
We model the problem as in Fig.~\ref{fig:tree_lis}.
\begin{figure}[h]
    \centering
    \includegraphics[width=0.8\columnwidth]{fig/LIS_tree.png}
    \caption{Graph Model for LIS, each path represents a possible solution. %Each arrow represents an move: find an element in the following elements %that's larger than the current node.
    }
    \label{fig:tree_lis}
\end{figure}
Same here, our problem become finding the longest path in a N-ary tree instead of a binary tree. Define $f(i)$ as the LIS starting with index $i$ in the array. then, its relation with other state will be $f(i) = \max_j(f(j))+1, j>i$, $a[j]>a[i]$, and $f[n]=0$. Here, the base case is when there has element to start from which will have 0 LIS.
\begin{lstlisting}[language=Python]
def lengthOfLIS(self, nums: List[int]) -> int:
    def dfs(nums, idx, cur_num):
        max_len = 0
        # Generate the next node
        for i in range(idx+1, len(nums)):
            if nums[i] > cur_num:
                max_len = max(max_len, 1 + dfs(nums, i, nums[i]))
        return max_len
    return dfs(nums, -1, -sys.maxsize)
\end{lstlisting}
\subsection{Dynamic Programming}
\paragraph{Memoization} Similar to the last approach, we can write code:
\begin{lstlisting}[language=Python]
def lengthOfLIS(self, nums: List[int]) -> int:
    def dfs(nums, idx, cur_num, memo):
        max_len = 0
        # Generate the next node
        if (idx, cur_num) not in memo:
            for i in range(idx+1, len(nums)):
                if nums[i] > cur_num:
                    max_len = max(max_len, 1 + dfs(nums, i, nums[i], memo))
            memo[(idx, cur_num)] = max_len
        return memo[(idx, cur_num)]
    memo = {}
    return dfs(nums, -1, -sys.maxsize, memo)
\end{lstlisting}
\paragraph{Tabulation}  With the bottom-up manner, we need to tweet our above recurrence function and definition of state. The subproblem $f(i)$ here will be defined as the LIS ending at index $i$. We shall pay attention that with $n$ elements there should exist $n+1$ states in total, that there is an empty state with empty array $[]$. The recurrence function will be shown in Eq.~\ref{LIS_equation}. It can be explained the LIS ending at index $i$ will be transitioned from LIS ending at any previous index by plusing one.  The whole analysis process is illustrated in Fig~\ref{fig:lis}.
\begin{equation}
\label{LIS_equation}
    f(i) = \begin{cases}
    1 + max(f(j)),& -1\leq j<i < n, arr[j] < arr[i];\\
    0& \text{i=-1}
    \end{cases}
\end{equation}
\begin{figure}[!ht]
    \centering
    \includegraphics[width=0.8\columnwidth]{fig/LIS.png}
    \caption{The solution to LIS. }
    \label{fig:lis}
\end{figure}
To simply the implementation, we insert a $-\infty$ value at the beginning of the array. To initialize we set $dp[0] = 0$, and the answer is $max(dp)$.  The time complexity is $O(n^2)$ because we need two for loops: one outsider loop with $i$, and another inside loop with $j$. The space complexity is $O(n)$. The Python code is:
\begin{lstlisting}[language= Python]
def lis(a):
  # define the dp array
  dp = [0] * (len(a)+1)
  a = [-sys.maxsize] + a
  print(a)
  for i in range(len(a)): # end with index i-1
    for j in range(i):
      if a[j] < a[i]:
        dp[i] = max(dp[i], dp[j] + 1)
  print(dp)
  return max(dp)
\end{lstlisting}
\subsection{Divide and Conquer}
We can even speedup further by using binary search, the second loop we can use a binary search to make the time complexity $O(logn)$, and the dp array used to save the maximum ans. Each time we use binary search to find an insertion point, if it is at the end, then the length grow.
[4]->[4,10],->[4,10],[3,10],->[3,8]->[3,8,9]
\begin{lstlisting}[language = Python]
def lengthOfLIS(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        def binarySearch(arr,l,r,num):
            while l<r:
                mid = l+(r-l)//2
                if num>arr[mid]:
                    l=mid+1
                elif num<arr[mid]:
                    r=mid
                else:
                    return mid
            return l
        max_count = 0
        if not nums:
            return 0
        dp =[0 for _ in range(len(nums))]#save the maximum till now
        maxans =1
        length=0
        for idx in range(0,len(nums)): #current combine this to this subsequence, 10 to [], 9 to [10]
            pos = binarySearch(dp,0,length,nums[idx]) #find insertion point
            dp[pos]= nums[idx] #however if it is not at end, we replace it, current number
            if pos==length:
                length+=1
        print(dp)
        return length
\end{lstlisting}
% Note: dp array does not result in longest increasing subsequence, but length of dp array will give you length of LIS. A dynamic demo can be found here: https://en.wikipedia.org/wiki/Longest\_increasing\_subsequence
% \#/media/File:LISDemo.gif. The code:
\begin{lstlisting}[language=Python]
    def lengthOfLIS(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        def binarySearch(arr,l,r,num):
            while l<r:
                mid = l+(r-l)//2
                if num>arr[mid]:
                    l=mid+1
                elif num<arr[mid]:
                    r=mid
                else:
                    return mid
            return l
        max_count = 0
        if not nums:
            return 0
        dp =[0 for _ in range(len(nums))]
        LIS=0
        for i in range(len(nums)): #current combine this to this subsequence, 10 to [], 9 to [10]
            pos = binarySearch(dp,0,length,nums[i])
            dp[pos]= nums[i]
            if pos==LIS:
                LIS += 1
        return LIS
\end{lstlisting}
\end{document}


%\documentclass[11pt,emulateapj,apjfonts,epsf]{aastex}
\documentclass[preprint,12pt,longnamesfirst]{aastex}
%\documentclass[manuscript,12pt]{aastex}
%\documentclass[longnamesfirst]{emulateapj}

%\usepackage{emulateapj5}
\usepackage{latexsym}

\shorttitle{}
\shortauthors{}

\newcommand {\kms}{km~s$^{-1}$}
\newcommand{\ud}{\mathrm{d}}

\begin{document}

\title{A Check of the Analytical Model of the Adiabatic
Expansion Cluster Wind}

\author{Wei Sun}
% \affil{Purple Mountain Observatory, CAS}

\begin{abstract}

The power-law approximation is solid, which means there is barely
deceleration toward the expanding wind.

\end{abstract}

% \keywords{Cluster Wind, NGC 3603}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Introduction} %Section 1

The check the spectral calculation code of my version, we set up a
benchmark model as presented in Ji et al (2006, \S3.4):
\begin{quote}
\textit{As an illustration, we consider a toy model for an
adiabatically expanding stellar cluster wind with a constant mass
input rate $\dot{M}\sim3\times10^{-5}\,M_\odot$~yr$^{-1}$ and a
constant velocity $v=1000$~km~s$^{-1}$. We assume that the wind is
injected at an initial radius of $r_0=0.3$~pc and is heated up to
a CIE state with an equilibrium temperature $T_0=5\times10^6$~K.
As the wind expands adiabatically,
\textbf{its temperature drops as $T=T_0(r/r_0)^{-4/3}$}.}
\end{quote}

However, The exponential expression looks too simple to me. The
fluid follows a static solution that should be more sophisticated.
Besides that, I still need to check the exact form describing the
adiabatic expansion motion, as well as its approximation in this
case, instead of guessing the expression for pressure, velocity
and other physical parameters blindly.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{A Solution to the Adiabatic Expansion}\label{sec2}

Starting from the basic equations of the fluid mechanics describing
a steady flow:
\begin{eqnarray}
  \frac{1}{r^2}\frac{\ud}{\ud r}\left(\rho ur^2\right) &=& 0, \\
  \rho u\frac{\ud u}{\ud r}+\frac{\ud P}{\ud r} &=& 0, \\
  \frac{1}{r^2}\frac{\ud}{\ud r}\left[\rho{}ur^2\left(\frac{1}{2}u^2 +
    \frac{5}{2}\frac{P}{\rho}\right) \right] &=& 0.
\end{eqnarray}
The right hands of those equations are all set to zero because we
assume no further mass and energy supply/loss during the adiabatic
expansion.

We could easily find that, from Eqn.(1)
\begin{equation}
  \rho{}ur^2\equiv c_1,
\end{equation}
in which the constant $c_1$ could be derived based on the boundary
condition at $r=r_0$ (but unimportant), and from Eqn.(3),
\begin{eqnarray}
  \frac{1}{2}u^2+\frac{5}{2}\frac{P}{\rho}\equiv c_2 \Rightarrow \\
  P=\frac{c_1}{5}\frac{1}{ur^2}\left(2c_2-u^2\right),
\end{eqnarray}
and the constant $c_2$ follows
\begin{eqnarray*}
  2c_2 &=& u^2_0+3c^2_{s0} \\
  {\rm or} &=& u^2_0+5kT/\mu{}m_{\rm H},
\end{eqnarray*}
in which $c_{s0}=\sqrt{\gamma{}P_0/\rho_0}=221.66$~km~s$^{-1}$
is the sound speed at $r_0$.

Substituting Eqn.(6) into Eqn.(2), we have
\begin{eqnarray*}
 & \rho{}ur^2\cdot\frac{1}{r^2}\frac{\ud u}{\ud r} +
    \frac{c_1}{5}\frac{\ud}{\ud r}
      \left[\frac{1}{ur^2}\left(2c_2 - u^2\right)\right] = 0 \\
\Leftrightarrow & \frac{1}{r^2}\frac{\ud u}{\ud r} +
    \frac{1}{5}\frac{\ud}{\ud r}
      \left[\frac{2c_2}{r^2u}-\frac{u}{r^2}\right] = 0 \\
\Leftrightarrow & \frac{5}{r^2}\frac{\ud u}{\ud r} +
    \left[-\frac{4c_2}{r^3u}-\frac{2c_2}{r^2u^2}\frac{\ud u}{\ud r}
      +\frac{2u}{r^3}-\frac{1}{r^2}\frac{\ud u}{\ud r}\right] = 0 \\
\Leftrightarrow & \frac{2}{r^2}\left[2-\frac{c_2}{u^2}\right]
  \frac{\ud u}{\ud r} = \frac{2}{r^3}\left[\frac{2c_2}{u}-u\right] \\
\Rightarrow & \frac{2u^2-c_2}{u^2(2c_2-u^2)}\ud u^2 =
  \frac{2}{r}\ud r \\
\Leftrightarrow & \left[\frac{3}{2c_2-u^2}-\frac{1}{u^2}\right]\ud u
  = \frac{4}{r}\ud r\\
\Rightarrow & -3\ln(2c_2-u^2) - \ln u^2 + C = 4\ln r.
\end{eqnarray*}
Again, constant $C$ is derived based on the boundary condition at $r_0$:
\begin{equation}
  C=3\ln(2c_2-u^2_0)+\ln u^2_0 + 4\ln r_0,
\end{equation}
which means the flow's motion follows
\begin{equation}
  \left[\frac{2c_2-u^2}{3c^2_{s0}}\right]^3
  \left[\frac{u^2}{2c_2-3c^2_{s0}}\right]
  = \left(\frac{r}{r_0}\right)^{-4},
\end{equation}
or
\begin{equation}
  \left[1+\frac{u^2_0-u^2}{3c^2_{s0}}\right]^3
    \left[\frac{u^2}{u^2_0}\right]
    = \left(\frac{r}{r_0}\right)^{-4}
\end{equation}

Replacing $u^2=2c_2-5kT/\mu{}m_{\rm H}$ and $c^2_{s0}=5kT/3\mu{}m_{\rm H}$,
we have
\begin{equation}
  \left(\frac{T}{T_0}\right)^3\left(\frac{T_c-T}{T_c-T_0}\right)
  = \left(\frac{r}{r_0}\right)^{-4},
\end{equation}
in which $T_0=5\times10^6$~K at $r_0$, and
$T_c=T_0+\mu{}m_{\rm H}u^2_0/5k=3.9\times10^7$~K.

\section{Diagram and Approximation}\label{sec3}

In Eqn.(9), if the expression of flow velocity\footnote{The solution
of $u\leq{}u_0$ is discarded due to weird variation as a function of
radius: thanks for Niu Shu pointing that out! However, in the
$u\geq{}u_0$ case, $u$ should not exceed
$\sqrt{u^2_0+3c^2_{s0}}=1071.17$~\kms.}, $u$, is adopted as in
1,000~\kms--1,070~\kms, the first and second brackets will be in
the range of 1.0--1.145 and 1.0--$4.77\times10^{-6}$,
respectively. Therefore, we could make a approximation as
\begin{equation}
  u=\left[u^2_0+3c^2_{s0}-3c^2_{s0}\left(r/r_0\right)^{-4/3}\right]^{0.5}.
\end{equation}
Similar analysis to the solution of flow temperature returns
\begin{equation}
  T=T_0\left(\frac{r}{r_0}\right)^{-4/3}.
\end{equation}

Actually, they are quite good approximation of less than 5\% accuracy,
as seen in Fig.~\ref{fig1}.

\begin{figure}
  \centering
  \includegraphics{figures/adia.exp_phy.eps}
  \caption{Radial profile of the flow velocity (red), density (green),
  and temperature (blue) of the adiabatic expansion. The solid lines
  follows the analytical solution in \S\ref{sec2}, and the dashed lines
  present the exponential approximations in \S\ref{sec3}. Their
  differences are plotted in the lower panel. The black
  dashed line marks the beginning radius $r_0$ of 0.3~pc.\label{fig1}}
\end{figure}

\end{document}


%-----------------------------------------------------------------------------------------------
\section{Polyekthetic Processes}

    %---------------------------------------------------------------------------------------
    \subsection{Literature Generalizations of Polytropic Processes}

    Concerning  isentropic  processes  written  in  the   form   of   Eq.~(\ref{eq:polytropic}),
    Bejan~\cite{2006-BejanA-Wiley} derives  a  definition  for  the  \emph{isentropic  expansion
    exponent}, $k$, by differentiating an $s\!:\!s(P, v)$ function and describing the isentropic
    process as:
    %
    \begin{equation}
        ds = \parxyz sPv\,dP + \parxyz svP\,dv = 0.
        \label{eq:iso-sPv}
    \end{equation}

    It is worth noting that Eq.~(\ref{eq:iso-sPv}) is valid  in  general,  and  not  only  to  a
    particular substance equation of state. By a suitable manipulation of the $\inlxyz sPv$  and
    $\inlxyz svP$ coefficients, Eq.~(\ref{eq:iso-sPv}) can be recast into:
    %
    \begin{equation}
        \frac{dP}{P} = -k\frac{dv}{v},
        \label{eq:iso-sPv.ODE}
    \end{equation}
    %
    \noindent with
    %
    \begin{equation}
        k \equiv \frac{-v}{P}\parxyz Pvs.
        \label{eq:iso-sPv.k}
    \end{equation}

    As $k\!:\!k(P, v)$ in general, let $k$ be taken as a  \emph{constant}  in  the  \emph{local}
    vicinity   of   a   state   of   interest;   therefore,   the   \emph{local}   solution   of
    Eq.~(\ref{eq:iso-sPv.ODE}) is the local polytropic process $Pv^k = \mbox{const.}$

    Bejan~\cite{2006-BejanA-Wiley} also derives an \emph{isothermal-expansion exponent},  $k_T$,
    so that $Pv^{k_T} = \mbox{const.}$,  for  isothermal  processes  in  a  similar  fashion  as
    Eqs.~(\ref{eq:iso-sPv})--(\ref{eq:iso-sPv.k}).

    In this type of generalization, one \emph{defines new polytropic  exponents},  so  that  the
    resulting  process,  \emph{written  as  a  polytropic  relationship},  has  a  predetermined
    \emph{behavior} in terms of a third thermodynamic property.

    In this ``Bejan'' generalization of polytropic process, one has
    %
    \begin{equation}
        Pv^{k_{\alpha}} = \mathsf{c} = \mbox{const.},
        \label{eq:gen.Bejan}
    \end{equation}
    %
    \noindent for const-$\alpha$ processes.

    Moreover,  Nederstigt~\cite{2017-NederstigtP-TUDelft}   generalizes   isentropic   exponents
    $\gamma_{Pv}$, $\gamma_{Tv}$,  and  $\gamma_{PT}$,  so  that  isentropic  processes  can  be
    described as either $Pv^{\gamma_{Pv}} = \mbox{const}$, or $Tv^{\gamma_{Tv}} = \mbox{const}$,
    or $PT^{\gamma_{PT}} = \mbox{const}$.

    In this ``Nederstigt'' generalization of isentropic processes,  exponents  $k_{\alpha\beta}$
    are such that
    %
    \begin{equation}
        \alpha\beta^{k_{\alpha\beta}} = \mathsf{c} = \mbox{const.},
        \label{eq:gen.Nederstigt}
    \end{equation}
    %
    \noindent for const-$s$ processes.

    %---------------------------------------------------------------------------------------
    \subsection{Polyekthetic Processes Definition}

    The generalization of these ideas follows:

    \begin{definition}\label{def:polyekthetic}
        Let property indices $i$, and $j$  be  any  intensive  system  properties  or  algebraic
        expressions thereof, then any process relation in the form
        %
        \begin{equation}
            ij^n = \mathsf{c} = \mbox{const.},
            \label{eq:def.polyekthetic}
        \end{equation}
        %
        \noindent for constant or variable $n$, is a \textbf{polyekthetic process}.
    \end{definition}

    Therefore, polyekthetic processes allow any property pair---and not just  $Pv$---to  be  the
    ``base,'' for its power relationship with exponent $n$  of  Eq.~(\ref{eq:def.polyekthetic}).
    Moreover, since polytropic processes are recovered by setting $i \equiv P$,  $j  \equiv  v$,
    and letting the exponent $n$ have any real value, clearly the polytropic process  set  is  a
    proper subset of the polyekthetic process set.

    A useful application is to  find  suitable  exponents  $k_{ij\ell}$,  so  that  polyekthetic
    processes in base $ij$ with exponent $k_{ij\ell}$ describe const-$\ell$ processes; hence the
    following:

    \begin{definition}\label{def:polyekthetic.exponent}
        Let \textbf{constant-property process  polyekthetic  exponents  $k_{ij\ell}$}---or  just
        polyekthetic exponents $k_{ij\ell}$, for short---be such that the  polyekthetic  process
        relation
        %
        \begin{equation}
            ij^{k_{ij\ell}} = \mathsf{c} = \mbox{const.},
            \label{eq:polyekthetic}
        \end{equation}
        %
        \noindent holds, at least locally, for an iso-$\ell$ process, in  which  $\ell$  is  any
        intensive system property or algebraic expressions thereof.
    \end{definition}

    Thus, the following are all (simple) examples of polyekthetic process descriptions:
    %
    \begin{align}
        Tv^{k_{Tvs}} = \mathsf{c_1} = \mbox{const.}, \label{eq:ex.Tvs} \\
        sP^{k_{sPT}} = \mathsf{c_2} = \mbox{const.}, \label{eq:ex.sPT} \\
        sv^0 = \mathsf{c_3} = \mbox{const.}, \label{eq:ex.sv0} \\
        \lim_{n \to +\infty}hv^n = \mathsf{c_4} = \mbox{const.}, \label{eq:ex.hvh}
    \end{align}
    %
    \noindent in which Eq.~(\ref{eq:ex.Tvs}) describes an isentropic process  in  a  $Tv$  base;
    Eq.~(\ref{eq:ex.sPT}) describes an isothermal process in an $sP$ base; Eq.~(\ref{eq:ex.sv0})
    describes an isentropic process in an $sv$ base, and  Eq.~(\ref{eq:ex.hvh})  an  isenthalpic
    process in an $hv$ base.

    \begin{theorem}\label{the:const-prop}
        Any constant-property process is a polyekthetic process.
    \end{theorem}

    \begin{proof}
        Let  $\ell$  be  the  property  held  constant  for  a  constant-property   process   of
        Theorem~\ref{the:const-prop}. Then, by  Definition~\ref{def:polyekthetic.exponent},  the
        polyekthetic process of Eq.~(\ref{eq:polyekthetic}) is a const-$\ell$ process.
    \end{proof}

    %---------------------------------------------------------------------------------------
    \subsection{Polyekthetic Processes Etymology}

    Following the etymology of polytropic processes~\cite{2020-NaaktgeborenC-engrXiv}, and given
    that     this     generalization     engenders      many      (\GRtxt{pol'u})      exponents
    (\GRtxt{ekj'ethc})~\cite{1997-ManiatoglouMPF-Porto}, the set of processes described  by  the
    relation  of   Eq.~(\ref{eq:def.polyekthetic})   are   named   ``polyekthetic'',   i.e.,   a
    \emph{multiple exponent} process set.

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


% !TEX root = scombinatorics.tex
\documentclass[scombinatorics.tex]{subfiles}
\begin{document}

\chapter{Zarankiewicz problem(s)}

\label{zarankiewicz}

% \sections{The Beineke-Schwenk problem}

% Determine the quadruples $m,n;s,t$ of positive integers such that for every $\phi(x\,;z)$ and every pair $A\subseteq\U$ and $B\subseteq\V$ of cardinality $m$ respectively $n$ there are $A'\subseteq A$ and $B'\subseteq B$ such that either $A'\times B'\subseteq\phi(x\,;z)$ or $A'\times B'\subseteq\neq\phi(x\,;z)$.

% Sometimes the problem is formulated as follows.
% Given $s,t$ determine the least $n$ such that $n,n;s,t$ is a quadruple as above.

\def\medrel#1{\parbox[t]{6ex}{$\displaystyle\hfil #1$}}
\def\ceq#1#2#3{\parbox[t]{25ex}{$\displaystyle #1$}\medrel{#2}{$\displaystyle #3$}}

Let us start with presenting Zarankiewicz problem.
Let $A\subseteq\U$ and $B\subseteq\V$ have cardinality $m$ respectively $n$.
Given two integers $s,t$ (both at least $2$ to avoid trivialities), what is the maximal cardinality of a graph $\phi(A\,;B)$ that does not contain $A'\times B'$  for any $A'\subseteq A$ and $B'\subseteq B$ of cardinality $s$ respectively $t$?
Denote this maximum by \emph{$z(m, n; s, t)$.}
Define also \emph{$z(n; t)$} $=z(m, n; s, t)$.
In 1951 Zarankiewicz posed the problem of determining $z(n; 3)$ for $n = 4, 5, 6$ and the  general problem has also become known as the problem of Zarankiewicz.

\section{The K\H{o}v\'ari-S\'os-Tur\'an Theorem}
The theorem in this section is a classical result of K\H{o}v\'ari, S\'os, and Tur\'an.
They actually proved it for $z(n;t)$ but the proof easily generalizes.

In the proof we need the following generalization of the binomial coefficient.
For any positive integer $t$ and any real $x$ we define

\ceq{\hfill\binom{x}{t}}{=}{\frac{x(x-1)\cdots(x-t+1)}{t!}.}

It is easy to verify that for any fixed $t$, this is a convex and strictly increasing function of $x$.

\begin{void_thm}[K\H{o}v\'ari-S\'os-Tur\'an Theorem]\label{thm_KST}
  For all $2\le s\le m$ and $2\le t\le n$
  \smallskip

  \ceq{\hfill z(m, n; s, t)}{<}{(s-1)^{1/t}(n-t+1)m^{1-1/t}+(t-1)m}

  or also

  \ceq{}{<}{c\,\big(n\,m^{1-1/t}+m\big)}

  for some $c = c(s,t)$.

\end{void_thm}
\begin{proof}
  Let $A\subseteq\U$ and $B\subseteq\V$ have cardinality $m$ respectively $n$.
  Let $P$ be the set of pairs $\<a,B'\>$ such that $B'\subseteq\phi(a\,;B)$ and $B'$ has cardinality $t$.
  Writing $d_a$ for the cardinality of $\phi(a\,;B)$, we have

  \ceq{\hfill|P|}{=}{\sum_{a\in A}\binom{d_a}{t}}

  Write $z$ for $|\phi(A\,;B)|$, and note that

  \ceq{\hfill z}{=}{\sum_{a\in A}d_a.}

  As \smash{$\displaystyle\binom{x}{t}$} is a convex function of $x$,
  \smallskip

  \ceq{(1)\hfill\binom{z/m}{t}}{\le}{\frac1m\,|P|.}
  \smallskip

  Now, suppose that $z=z(m, n; s, t)$.
  Then for any fixed $B'\subseteq B$ there are at most $s-1$ pairs $\<a,B'\>\in P$.
  Hence

  \ceq{(2)\hfill|P|}{\le}{(s-1)\binom{n}{t}.}

  Together, (1) and (2) yield
  \smallskip

  \ceq{\hfill m\binom{z/m}{t}}{\le}{(s-1)\binom{n}{t}.}
  \smallskip

  As \smash{$\displaystyle\binom{x}{t}$} is strictly increasing function of $x$, and $z/m<n$,
  \smallskip

  \ceq{\hfill m \Big(\frac{z}{m}-t+1\Big)^t}{<}{(s-1)\big(n-t+1\big)^t}
  \medskip

  which easily yields the theorem.
\end{proof}

\section{The {\sc nip} case}

\end{document}


\section{Surfaces and symmetric nondegenerate bilinear forms}
Never mind, I decided to TeX this.
\todo[inline]{I think that this should be in an appendix...}
\subsection{Case of Poincar\'{e} duality}
Let $M$ be a compact manifold of dimension $n$ (this is the kind of thing most of the world cares about). This whole lecture will be happening with coefficients in $\FF_2$. There is a cup product pairing $ H^p(M)\otimes H^q(M)\to H^{p+q}(M)$.
\begin{theorem}
There exists a unique class $[M]\in H_n(M)$, called the fundamental class, such that there's a pairing $ H^p(M)\otimes H^q(M)\xrightarrow{\cup} H^n(M)\xleftarrow{\langle -,[M]\rangle}\FF_2$ when $p+q=n$. This pairing is a nonsingular (aka perfect) pairing, i.e., the map $ H^p(M)\to\Hom( H^q(M),\FF_2)$ defined by $x\mapsto(y\mapsto\langle x\cup y,[M]\rangle)$ is an isomorphism.
\end{theorem}
One thing I should say is that $ H_\ast(M)$ is zero in dimensions above $n$, and $ H_i(M)$ are finitely generated.
\begin{corollary}
From the UCT, it follows that $ H^p(M)\xrightarrow{\cong}\Hom( H^q(M),\FF_2)= H_q(M)$. The corresponding classes under this association are said to be Poincar\'{e} dual to each other.
\end{corollary}
\begin{remark}
This means I get a pairing $ H_q(M)\otimes H_q(M)\to \FF_2$ in homology when $p+q=n$. This is called the \emph{intersection pairing}. If you have one $p$-cycle and one $q$-cycle, this means that I can make them intersect transversely, i.e., they intersect in points -- or not at all. I count the number of points modulo $2$, and this is the element in $\FF_2$.
\end{remark}
\begin{example}
Let $M=T^2=S^1\times S^1$. We know that $ H^1(M)=\FF_2\times\FF_2=\langle x,y\rangle$ where $x^2=0$ and $y^2=0$, and $xy\neq 0$ (this is the generator of $ H^2(M)$). In terms of homology, this means that I can pick cycles on the torus so that they intersect on one point. This makes sense. Also, $x^2=0=y^2$ because the second copy of $x$ and $y$ can just be moved so that they don't intersect.
\end{example}
\begin{example}[Especially interesting case]
If $n=2k$, I get a symmetric bilinear form on $ H_k(M)$ via $ H_k(M)\otimes H_k(M)\to \FF_2$ that is nondegenerate.
\end{example}
\subsection{Symmetric bilinear forms}
I've really screwed up when TeXing this.

If $W\subseteq V$ is a subspace, then a bilinear form on $V$ gives a bilinear form on $W$, but this need not be nonsingular even if the bilinear form on $V$ is. Observe though that the induced bilinear form on $W$ is nonsingular if and only if $W\cap W^\perp=0$. This result should be obvious, but I don't want to say why.

I have a map $V\xrightarrow{\cong} V^\ast$ by sending $v\mapsto(v^\prime\mapsto v\cdot v^\prime)$. Inside $V$, I have $W^\perp$, and I have a quotient $V^\ast\xrightarrow{\Res} W^\ast$, with kernel $\ker\Res$. I want to claim that $V/W^\perp\cong W^\ast$. We know that $\dim W+\dim W^\perp=\dim V=:n$. If $W$ is nonsingular, then $V=W\oplus W^\perp$. This is true as vector spaces, but also as quadratic forms.

To see this, pick a basis $e_1,\cdots,e_k$ for $W$. The quadratic form is represented by some matrix $\begin{pmatrix} e_i\cdot e_j \end{pmatrix}$. It's nonsingular exactly when its determinant is nonzero, i.e., $1$. Then I can also pick a basis for $W^\perp$. Then I have nondegenerate quadratic form on $V$ given by $\begin{pmatrix}\begin{pmatrix} e_i\cdot e_j \end{pmatrix} & 0 \\ 0 & \begin{pmatrix} ... \end{pmatrix}\end{pmatrix}$ where the bottom left thing is that of $W^\perp$. Since the quadratic form on $V$ has determinant $1$, the matrix for $W^\perp$ also has determinant $1$, and hence $V=W\oplus W^\perp$ as quadratic forms.

Now:
\begin{enumerate}
\item Suppose $v\in V$ has $v\cdot v=1$. Then $V=\langle v\rangle\oplus\langle v\rangle^\perp$. This is an orthogonal splitting into nonsingular forms.
\item Assume $v\cdot v=0$ for all\footnote{Your intuition about dot products isn't very useful here, for example the torus. This is because the matrix for the quadratic form on the torus is $\begin{pmatrix}0 & 1 \\ 1 & 0\end{pmatrix}$. Also, $(x+y)\cdot(x+y)=0$. This means that every cycle on the torus can be moved to have no self-intersection, at least modulo $2$.} $v\in V$. Anyway, in our general situation, $v$ might be zero -- so pick $v\neq 0$. But the form is nonsingular, so there exists $w$ such that $v\cdot w\neq 0$, i.e., $v\cdot w=1$. Then $\langle v,w\rangle$ is a $2$-dimensional subspace sitting inside $V$, so it splits off. Thus we get a hyperbolic form $\begin{pmatrix}0 & 1 \\ 1 & 0\end{pmatrix}$ (or something like this). (Example: torus)
\end{enumerate}
Thus we conclude:
\begin{prop}
Any nonsingular symmetric bilinear form on a finite dimensional vector space over $\FF_2$ is isomorphic to: $\begin{pmatrix}\begin{pmatrix}1 & \cdots & 0\\ \vdots & \ddots & \vdots \\ 0 & \cdots & 1 \end{pmatrix} & 0 \\ 0 & \begin{pmatrix}\begin{pmatrix}0 & 1 \\ 1 & 0\end{pmatrix} & \\ & \begin{pmatrix}0 & 1 \\ 1 & 0\end{pmatrix}\end{pmatrix}\end{pmatrix}$ where the bottom left thing has some number of the hyperbolic $\begin{pmatrix}0 & 1 \\ 1 & 0\end{pmatrix}$.
\end{prop}
Let $\mathbf{Bil}$ be the set of nonsingular symmetric bilinear forms that are finite dimensional over $\FF_2$ modulo isomorphisms. I've just given a classification of these things. This is a commutative monoid under $\bigoplus$. This $\mathbf{Bil}$ is $\{\text{nonsingular matrices}\}/\text{similarity}$ where similarity means $M\sim N$ if $N=AMA^T$ for some nonsingular $A$.
\begin{claim}
\begin{equation*}
\begin{pmatrix}
 & 1 & \\
1 & & \\
 & & 1
\end{pmatrix}
\sim
\begin{pmatrix}
1 & & \\
& 1 & \\
& & 1
\end{pmatrix}
\end{equation*}
This is the same thing as saying that $\begin{pmatrix}
 & 1 & \\
1 & & \\
 & & 1
\end{pmatrix}=AA^T$ for some nonsingular $A$.
\end{claim}
\begin{proof}
Let $A=\begin{pmatrix}1 & 1 & 1 \\ 1 & 0 & 1 \\ 0 & 1 & 1 \end{pmatrix}$.
\end{proof}
So, $\mathbf{Bil}$ is the commutative monoid generated by $(1)$, $\begin{pmatrix}0 & 1 \\ 1 & 0\end{pmatrix}$ with relation $\begin{pmatrix}
 & 1 & \\
1 & & \\
 & & 1
\end{pmatrix}
\sim
\begin{pmatrix}
1 & & \\
& 1 & \\
& & 1
\end{pmatrix}$.
\subsection{Connected surfaces}
Let's go back to topology. Let $n=2$, $k=1$ (so that $2k=n$). Then you get an intersection pairing on $ H_1(M)$. Suppose I consider $\RP^2$. We know that $ H_1(\RP^2)=\FF_2$. This must be that ``$(1)$''-form. This says that anytime you have a cycle on a projective plane, there's nothing I can do to remove its self interesections. This is also seen on a M\"obius band that I will not draw because I don't know how to draw fundamental polygons in LaTeX. But this can be seen by looking at the projection from the M\"obius band to the circle.

We also had the notion of connected sums. Let's compute the homology of $\Sigma_1\#\Sigma_2$ using Mayer-Vietoris, like you did in the exercise. You get $ H_2(\Sigma_1\#\Sigma_2)\to H_1(S^1)\to H_1(\Sigma_1-D^2)\oplus H_1(\Sigma_2-D^2)\to H_1(\Sigma_1\#\Sigma_2)$. When you pierce a surface, it collapses down into its $1$-skeleton, so $ H_2(\Sigma_1-D^2)\oplus H_2(\Sigma_2-D^2)\cong 0$. Let's restrict ourselves to connected surfaces. The map $ H_1(S^1)\to H_1(\Sigma_1-D^2)\oplus H_1(\Sigma_2-D^2)$ is the zero map when you think of it as a boundary. Actually, that argument needs to use something, because we know that with integer coefficients, this isn't right -- you need to find an oriented chain in that case -- but this orientation issue goes away in $\FF_2$ because there's no issues with $\pm$. And actually, the map $ H_1(\Sigma_1\#\Sigma_2)\to H_0(S^1)$ is zero as well, so $ H_1(\Sigma_1-D^2)\oplus H_1(\Sigma_2-D^2)\cong H_1(\Sigma_1)\oplus H_1(\Sigma_2)\cong H_1(\Sigma_1\#\Sigma_2)$ that respect the intersection pairing.

Let $\mathbf{Surf}$ be the commutative monoid of connected compact surfaces modulo homeomorphism. What we've proved is that there's a map $\mathbf{Surf}\to\mathbf{Bil}$ that takes connected sums to direct sums of orthogonal bilinear forms. And this map is an isomorphism. I.e.:
\begin{theorem}
There is an isomorphism of commutative monoids $\mathbf{Surf}\to\mathbf{Bil}$.
\end{theorem}
This is a little strange. What is our claim $\begin{pmatrix}
 & 1 & \\
1 & & \\
 & & 1
\end{pmatrix}
\sim
\begin{pmatrix}
1 & & \\
& 1 & \\
& & 1
\end{pmatrix}$ saying in this context? It's saying that $T^2\#\RP^2\cong(\RP^2)^{\# 3}$.
\begin{claim}
If $\Sigma$ is nonoriented, then $\Sigma\# T^2\cong\Sigma\# K$ (where $K$ is the Klein bottle $\RP^2\#\RP^2$).
\end{claim}
If it's nonoriented, then a M\"obius strip $M$ sits into $\Sigma$. Uh I guess you're dragging your attachment of the torus along $M$?

There's more to be said about this. You can think of quadratic forms if you're working over the reals or something. You can't do this modulo $2$. Under the story we talked about, the oriented surfaces are the ones where the dot product is always zero. A better thing to ask over $\FF_2$ are quadratic forms such that $q(x+y)=q(x)+q(y)+x\cdot y$. This leads to the Kervaire invariant, etc.

Happy Thanksgiving! See you on Monday.


\documentclass[10pt, a4paper, twoside]{basestyle}

\usepackage[backend=biber,firstinits=true,maxnames=100,style=alphabetic,maxalphanames=4,doi=true,isbn=false,url=false,eprint=true]{biblatex}
\bibliography{bibliography}

\usepackage[Mathematics]{semtex}
\usepackage{chngcntr}
\counterwithout{equation}{section}

%%%% Shorthands.

%%%% Title and authors.

\title{%
\textdisplay{%
On an Algorithm by Fukushima%
}%
}
\author{Pascal~Leroy (phl)}
\begin{document}
\maketitle
\begin{sloppypar}
\noindent
This document describes an algorithm used by Fukushima in his implementation of the complete elliptic integrals of the second kind $B$ and $D$ (\cite{Fukushima2018}).  It
follows the notation and conventions of \cite{Fukushima2011a}, but effectively replaces section 2.2.
\end{sloppypar}

\subsection*{Definitions}
Jacobi's nome $q\of{m}$ is defined as a function of the elliptic integral $K\of{m}$ as:
\[
q\of{m} \DefineAs\exp\of{\frac{-\Pi K\of{1 - m}}{K\of{m}}}
\]
Changing the variable to be $m_c = 1 - m$ and solving for $K\of{m}$ yields:
\[
K\of{m} = \pa{\frac{K\of{m_c}}{\Pi}}\pa{-\log q\of{m_c}}
\]
Now let's split this expression in two terms to separate out the logarithmic part:
\[
\begin{dcases}
X\of{m_c} &\DefineAs -\log q\of{m_c}\\
K_X\of{m_c} &\DefineAs \frac{K\of{m_c}}{\Pi}
\end{dcases}
\]
We have: $K\of{m} = K_X\of{m_c} X\of{m_c}$.

An expression for $E\of{m}$ can be obtained from Legendre's relation (\cite{Fukushima2011a} equation 2.11):
\[
E\of{m} = \pa{1 - \frac{E\of{m_c}}{K\of{m_c}}} K\of{m} + \frac{\Pi}{2 K\of{m_c}}
\]
Similarly, let's split this expression using the terms:
\[
\begin{dcases}
E_X\of{m_c} &\DefineAs \pa{1 - \frac{E\of{m_c}}{K\of{m_c}}}K_X\\
E_0\of{m_c} &\DefineAs \frac{1}{2 K_X\of{m_c}}
\end{dcases}
\]
We have: $E\of{m} = E_X\of{m_c} X\of{m_c} + E_0\of{m_c}$.

\subsection*{Integrals of the second kind for $m$ close to $1$}
Fukushima defines $B\of{m}$ as:
\[
B\of{m} \DefineAs \frac{E\of{m} - m_c K\of{m}}{m}
\]
This expression can be rewritten using the definitions above:
\begin{align*}
B\of{m} &= \frac{1}{m}\pa{E_X\of{m_c} X\of{m_c} + E_0\of{m_c} - m_c K_X\of{m_c} X\of{m_c}}\\
&= \frac{1}{m}\pa{X\of{m_c}\pa{E_X\of{m_c} - m_c K_X\of{m_c}} + \frac{1}{2 K_X\of{m_c}}}
\end{align*}
Similarly the definition of $D\of{m}$:
\[
D\of{m} \DefineAs \frac{K\of{m} - E\of{m}}{m}
\]
can be rewritten as:
\begin{align*}
D\of{m} &= \frac{1}{m}\pa{K_X\of{m_c} X\of{m_c} - E_X\of{m_c} X\of{m_c} - E_0\of{m_c}}\\
&= \frac{1}{m}\pa{X\of{m_c}\pa{K_X\of{m_c} - E_X\of{m_c}} - \frac{1}{2 K_X\of{m_c}}}
\end{align*}
These formulæ provide a means to compute $B\of{m}$ and $D\of{m}$ for $m$ close to $1$.  First, a polynomial approximation of $q\of{m_c}$
is computed, whose leading term is of order $m_c / 16$.  Then the $\log$ of that approximation is evaluated, yielding $X\of{m_c}$ (this
is the part that carries the logarithmic singularity).  Finally, $E_X\of{m_c}$ and $K_X\of{m_c}$ are computed using
Taylor or Maclaurin approximations.

It is easy to see that $B\of{m} = X\of{m_c} K_X\of{m_c} - D\of{m}$, which provides a simpler formula for computing $B\of{m}$ once
$D\of{m}$ is known.

\subsection*{Integrals of the second kind when $m$ tends towards $1$}
We are now interested in computing the leading term of $B\of{m}$ and $D\of{m}$ when $\conv m 1$.  First, we have $B\of{1} = 1$
(\cite{Fukushima2011a}, equation 1.9).  However, $\conv {D\of{m}} +\infty$ when $\conv m 1$.  To deal with this singularity we recall that:
\[
B\of{m} = X\of{m_c} K_X\of{m_c} - D\of{m}
\]
thus:
\[
D\of{m} = X\of{m_c} K_X\of{m_c} - B\of{m}
\]
Remember that $X\of{m_c} = -\log q\of{m_c}$ and that $q\of{m_c} = m_c / 16 + \BigO\of{m_c^2}$.  Therefore:
\[
X\of{m_c} = \log 16 - \log\of{m_c} + \BigO\of{m_c^2}
\]
Furthermore $K\of{0} = \Pi / 2$, so $K_X\of{0} = 1/2$.  Putting all these relations together we obtain the following equation:
\[
D\of{m} = 2 \log 2 - 1 - \frac{\log m_c}{2} + \BigO\of{m_c^2}
\]

\subsection*{Integrals of the second kind for $m$ close to $0$}
The expressions defined in the first section can be rewritten by changing the variable to be $m = 1 - m_c$.  In particular:
\begin{align*}
E_X\of{m} &= \pa{1 - \frac{E\of{m}}{K\of{m}}K_X\of{m}}\\
&= \frac{1}{\Pi}\pa{K\of{m} - E\of{m}}
\end{align*}
Thus:
\[
D\of{m} = \frac{\Pi}{m}E_X\of{m}
\]
Similarly, define $B_X^*\of{m}$ as follows:
\[
B_X^*\of{m} \DefineAs E_X\of{m} - m K_X\of{m}
\]
We have:
\begin{align*}
B_X^*\of{m} &= \frac{1}{\Pi}\pa{K\of{m} - E\of{m} - m K\of{m}}\\
&= \frac{1}{\Pi}\pa{m_c K\of{m} - E\of{m}}\\
&= -\frac{m B\of{m}}{\Pi}
\end{align*}
Therefore:
\[
B\of{m} = -\frac{\Pi}{m} B_X^*\of{m}
\]
These formulæ make it possible, by computing a Maclaurin approximation of $B_X^*\of{m}$ and $E_X\of{m}$, to evaluate $B\of{m}$
and $D\of{m}$ for $m$ close to $0$.

\subsection*{Conclusion}
We have demonstrated how \cite{Fukushima2018} uses different techniques from the ones detailed in \cite{Fukushima2011a} in order to handle the
logarithmic singularities of the $B$ and $D$ complete integrals of the second kind: while \cite{Fukushima2011a} divides the leading logarithmic term $\log \frac{m_c}{16}$, \cite{Fukushima2018} divides the complete logarithmic term $\log q\of{m_c}$.
\printbibliography
\end{document}


\section{Quantum Monte Carlo}
\subsection{Introduction}
Consider the Schr\"{o}dinger equation for a molecular system whose $3n$ electronic
and $3N$ nuclear degrees of freedom will be denoted collectively by the position
vectors $\vec{r}$ and $\vec{R}$, respectively,
%
\begin{equation}\label{eq::ElectronicSchrodinger}
  \elecham\elecwfn = E\elecwfn \,,
\end{equation}
%
where $\elecwfn$ is the (unknown) electronic wavefunction and $\elecham$ is the
electronic Hamiltonian operator, whose functional form can be written
%
\begin{align}\label{eq::ElectronicHamiltonian}
  \elecham &=
  - \frac{\hbar^2}{2m}\nabla^2_{\vec{r}}
  - \sum_{I=0}^N\sum_{i=0}^n \frac{Z_I}{|\vec{R}_I - \vec{r}_i|}
  + \sum_{i<j}^n \frac{1}{|\vec{r}_i - \vec{r}_j|}
  + \sum_{I<J}^N \frac{Z_IZ_J}{|\vec{R}_I - \vec{R}_J|} \nonumber \\
  &= \telec + \vne + \vee + \vnn \,,
\end{align}
%
where $\telec, \vne, \vee, \vnn$ correspond to the electronic kinetic, electron-
nuclear attractive potential, electron-electron repulsive potential and nuclear-
nuclear repulsive potential, respectively.
We draw the reader's attention to the parametric dependence on the nuclear
degrees of freedom, which results from application of the Born-Oppenheimer
approximation (the electrons adjust instantaneously to any nuclear motion, and
consequently the nuclei can be thought of as static). The application of the
Born-Oppenheimer approximation also permits our omission of the nuclear kinetic
energy from \eqref{eq::ElectronicHamiltonian}.

\subsection{Variational Monte Carlo}

\subsection{Diffusion Monte Carlo}
The time-dependent Schr\"{o}dinger equation (TDSE) for a molecular system is written
%
\begin{equation}\label{eq::TDSE}
  \im\hbar\ddt{t}\tdelecwfn{t} = \elecham\tdelecwfn{t} \,,
\end{equation}
%
where $\tdelecwfn{t}$ is the time-dependent electronic wavefunction. General solutions
to the TDSE can be written as the product of a complex exponential and the starting
condition $\tdelecwfn{t_0}$
%
\begin{equation}
  \tdelecwfn{t} = \tdelecwfn{t_0}\exp[-\im E t / \hbar] \,,
\end{equation}
%
where $E$ is the energy of $\tdelecwfn{t_0}$. Given Euler's identity, it is clear that
this solution is oscillatory in time. Beginning with the TDSE may seem
an odd starting point given we have no intentions of simulating the dynamics of
the molecular system characterised by $\tdelecwfn{t}$, but we'll find that it allows
us to evolve $\tdelecwfn{t}$ to the ground state of $\elecham$ through the
dynamics codified in \eqref{eq::TDSE}.

Our first step is to perform a Wick rotation, making the substitution $\tau = \im t$
(to ``imaginary time'') in \eqref{eq::TDSE}, yielding
%
\begin{equation}\label{eq::WickTDSE}
  -\hbar\ddt{\tau}\tdelecwfn{\tau} = \elecham\tdelecwfn{\tau} \,.
\end{equation}
%
This seemingly innocuous operation results in solutions to \eqref{eq::WickTDSE} having
decaying exponentials in imaginary time, rather than complex exponentials in ``real''
time. This gives rise to a particaularly useful property: in the infinite imaginary
time limit (i.e. as $\tau \rightarrow \infty$), contributions to $\tdelecwfn{\tau}$
from all but the lowest energy eigenfunctions of $\elecham$ tend to zero. Indeed, we can
add an offset to the right-hand side of \eqref{eq::WickTDSE} without loss of generality,
%
\begin{equation}{\label{eq::WickTDSEOffset}
  -\hbar\ddt{\tau}\tdelecwfn{\tau} = (\elecham - E_R)\tdelecwfn{\tau} \,.
\end{equation}

So evolving
\eqref{eq::WickTDSE} in imaginary time rather magically leaves us with the ground state
solution of $\elecham$!


\subsection*{Nondeterminism support}

The following primitives and builtins are supported:

\begin{itemize}
\item \lstinline{amb(e1, e2, ..., en)}: \textit{primitive}, creates a choice point whose value is chosen, at runtime,
from the set \lstinline{e1, e2, ..., en}. Note: although the usage of this primitive syntactically resembles a function application, \textit{amb} is not a function value. It is simply an operator, like the binary and unary operators.
\item \lstinline{amb()}: applying \textit{amb} without any arguments forces the language processor to backtrack to the most recent \textit{amb expression}.
\item \lstinline{cut()}: \textit{primitive}, prevents the language processor from backtracking any further beyond the current statement. Note: \textit{cut} is also not a function value. It is simply an operator, like the binary and unary operators.
\item \lstinline{require(pred)}: \textit{builtin}, forces the language processor to backtrack to the most recent \textit{amb expression}, if and only if \lstinline{pred} is false.
\item \lstinline{an_element_of(xs)}: \textit{builtin}, nondeterministically returns an element from the list \lstinline{xs}.
\item \lstinline{an_integer_between(n, m)}: \textit{builtin}, nondeterministically returns an integer between integers \lstinline{n} and \lstinline{m} (inclusively).
\item \lstinline{implication(p, q)}: \textit{builtin} returns true if and only if the boolean values \lstinline{p} and \lstinline{q} satisfy the equation $P \Rightarrow Q$
\item \lstinline{bi_implication(p, q)}: \textit{builtin} returns true if and only if the boolean values \lstinline{p} and \lstinline{q} satisfy the equation $P \Leftrightarrow Q$
\end{itemize}


\documentclass[a4paper,12pt]{scrartcl}

\usepackage{bm,amsmath,url,graphicx}
\usepackage{palatino}
\usepackage{color, xcolor}
\usepackage{listings}

\newcommand{\n}{\mathbf{ n}}
\newcommand{\h}{\mathbf{ h}}
\newcommand{\x}{\mathbf{ x}}
\newcommand{\y}{\mathbf{ y}}
\newcommand{\w}{\mathbf{ w}}
\newcommand{\HH}{\mathbf{ H}}
\newcommand{\R}{\mathbf{ R}}
\newcommand{\C}{\mathbf{ C}}
\newcommand{\thb}{{\boldsymbol{\theta}}}
\newcommand{\mub}{{\boldsymbol{\mu}}}
\newcommand{\python}{{\fbox{\texttt{\bfseries python}}\quad}}
\newcommand{\pen}{{\fbox{\texttt{\bfseries pen\&paper}}\quad}}

\renewcommand{\familydefault}{\rmdefault}

\begin{document}
\section*{SGN-41007 Pattern Recognition and Machine Learning}
\emph{Exercise Set 7: February 22--February 24, 2017}
\bigskip
\sloppy

\lstdefinestyle{mystyle}{
  belowcaptionskip=1\baselineskip,
  breaklines=true,
  frame=single,
  xleftmargin=\parindent,
  language=Python,
  showstringspaces=false,
  basicstyle=\ttfamily,
  keywordstyle=\bfseries\color{green!40!black},
  commentstyle=\itshape\color{purple!40!black},
  identifierstyle=\color{blue},
  stringstyle=\color{orange},
  moredelim=**[is][\color{red}]{@}{@},
}

\lstset{language=Python,style=mystyle}

\noindent
Exercises consist of both pen\&paper and computer assignments.
Pen\&paper questions are solved at home before exercises, while
computer assignments are solved during exercise hours. The
computer assignments are marked by  \python and
Pen\&paper questions by  \pen

\begin{enumerate}

\item \pen \emph{Error rate confidence limits.}

We train a classifier with a set of training examples, and test the
accuracy of the resulting model with a set of $N=100$ test samples.
The classifier misclassifies $K=5$ of those.

\begin{enumerate}
	\item Find the 90\% confidence interval of the result.
Hint: The classification accuracy can be modeled using
binomial distribution, whose confidence intervals are
discussed here:

\url{https://en.wikipedia.org/wiki/Binomial_distribution#Confidence_intervals}

\item Another classifier misclassifies only 3 test samples.
Is it better than the first one with statistical significance
at 90\% confidence level?

\end{enumerate}

\item \pen  In Exercise set 5 (question 2a), we derived the formula for the gradient of log-loss.
\begin{enumerate}
	\item Compute the gradient for $L_2$ penalized log-loss.
	\item Study also the gradient for $L_1$ penalized log-loss. Propose an approximation, whose gradient would be defined for all $\w$.
\end{enumerate}

\item \python Implement the $L_2$ penalized log-loss minimizer in Python. You can use
the template of Question 3 at Exercise set 5.

\item \python Apply the recursive feature elimination approach
(\verb+sklearn.feature_selection.RFECV+) with logistic regression classifier
for the arcene dataset. The data can
be downloaded in \verb+*.mat+ format from:

{\small \url{http://www.cs.tut.fi/courses/SGN-41007/exercises/arcene.zip}}

Use \verb+scipy.io.loadmat+ to open the file. Note that your have to ravel
\verb+y_train+ and \verb+y_test+ so that \texttt{sklearn} will accept them.

\begin{enumerate}
	\item Instantiate an RFECV selector (call it \texttt{rfe} from now on). To speed up computation, set \texttt{step = 50} in the constructor.
	Also set \texttt{verbose = 1} to see the progress.
	\item Fit the RFECV to \verb+X_train+ and \verb+y_train+.
	\item Count the number of selected features from \verb+rfe.support_+.
	\item Plot the errors for different number of features:\\
	 \verb+plt.plot(range(0,10001,50), rfe.grid_scores_)+
	\item Compute the accuracy on \verb+X_test+ and \verb+y_test+. You can use \texttt{rfe} as any other classifier.
\end{enumerate}

\item \python Apply $L_1$ penalized Logistic Regression for feature selection with the arcene dataset.
Find a good value for parameter $C$ by 10-fold cross-validating the accuracy.
Study the sparseness of the solution: how many features were selected?

\begin{enumerate}
	\item Instantiate a LogisticRegression classifier. Set \texttt{penalty = 'l1'} in the constructor.
	\item Cross validate the accuracy of a range of \texttt{C} values (see earlier exercises).
	\item Fit the LogisticRegression to \verb+X_train+ and \verb+y_train+.
	\item Count the number of selected features from \verb+rfe.coef_+.
	\item Compute the accuracy on \verb+X_test+ and \verb+y_test+.
\end{enumerate}

\end{enumerate}

\end{document}


\documentclass[nofootinbib,amssymb,amsmath]{revtex4}
\usepackage{mathtools}
\usepackage{amsthm}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{lmodern}
\usepackage{graphicx}
\usepackage{color}

%Put an averaged random variable between brackets
\newcommand{\ave}[1]{\left\langle #1 \right\rangle}

\newcommand{\vzero}{{\bf 0}}
\newcommand{\vI}{{\bf I}}
\newcommand{\vb}{{\bf b}}
\newcommand{\vd}{{\bf d}}
\newcommand{\vc}{{\bf c}}
\newcommand{\vv}{{\bf v}}
\newcommand{\vz}{{\bf z}}
\newcommand{\vn}{{\bf n}}
\newcommand{\vm}{{\bf m}}
\newcommand{\vG}{{\bf G}}
\newcommand{\vQ}{{\bf Q}}
\newcommand{\vM}{{\bf M}}
\newcommand{\vW}{{\bf W}}
\newcommand{\vX}{{\bf X}}
\newcommand{\vPsi}{{\bf \Psi}}
\newcommand{\vSigma}{{\bf \Sigma}}
\newcommand{\vlambda}{{\bf \lambda}}
\newcommand{\vpi}{{\bf \pi}}
\newcommand{\valpha}{{\bf \alpha}}
\newcommand{\vLambda}{{\bf \Lambda}}
\newcommand{\vA}{{\bf A}}

\newcommand{\code}[1]{\texttt{#1}}

\newtheorem{lemma}{Lemma}
\newtheorem{corollary}{Corollary}

\def\SL#1{{\color [rgb]{0,0,0.8} [SL: #1]}}
\def\DB#1{{\color [rgb]{0,0.8,0} [DB: #1]}}

\newcommand{\HOM}{$\mathsf{Hom}$}
\newcommand{\HET}{$\mathsf{Het}$}
\newcommand{\REF}{$\mathsf{Ref}$}
\newcommand{\epss}{\varepsilon}

\begin{document}

\title{GATK variant quality score model}
\author{David Benjamin}
\email{davidben@broadinstitute.org}
\affiliation{Broad Institute, 75 Ames Street, Cambridge, MA 02142}

\date{\today}

\maketitle

\section{Introduction}\label{introduction}

The variant quality model is used in both \code{HaplotypeCaller} and \code{GenotypeGVCFs}.  In \code{HaplotypeCaller} the model is applied we have decided which alleles to consider and calculated the likelihoods of each possible genotype.   In \code{GenotypeGVCFs} the model is applied after merging input GVCFs and harmonizing their allele representations, and the resulting score is used to decide which variants to emit. Let $\ell_{sg} \equiv P({\rm reads}_s | g)$ be the likelihood of genotype $g$ in sample $s$.  Then a simple model for the genotypes and observed reads is
\begin{equation}
P({\rm reads}, \vz) = \prod_s P(\vz_s) \prod_g \ell_{sg}^{z_{sg}}, \label{likelihood}
\end{equation}
where $z_{sg}$ is a binary indicator variable for sample $s$ exhibiting genotype $g$.  We can represent $P(\vz_s)$ in terms of a latent vector $\vpi$ of population allele frequencies.  Assuming independent alleles, the probability $P(\vz_s | \vpi)$ of a genotype $g$ containing $n_{ga}$ copies of allele $a$ (i.e. for tetraploid genotype AABC, $n_A = 2$, $n_B = n_C = 1$) is
\begin{equation}
P(\vz_s| \vpi) = \prod_g \left[ C_g \prod_a \left( \pi_a \right)^{n_{ga}} \right]^{z_{sg}},
\label{indicator}
\end{equation}
where $C_g = {\rm ploidy}! / \left(\prod_a n_{ga}! \right)$ is the number of phased genotypes corresponding to unphased genotype $g$.  For example, $C_{AAB} = 3$ because $AAB$, $ABA$, and $BAA$ are distinct phased genotypes, while $C_{AAA} = 1$.

Because sites vary in their allele frequencies, $\vpi$ is not a constant.  Rather, it is a random variable whose prior distribution should have the correct mean alt allele frequency (roughly 1 in 1000 for SNPs) and roughly the correct standard deviation.  That is, the prior probabilities that a site has a rare alt with, say $\pi_B = 10^{-5}$ or a common alt with $\pi_B = 0.25$ should match the empirical distribution of allele frequencies.  Note that the old model essentially assumed that every site had an alt allele with a constant allele frequency of 1/1000.  We can achieve this by setting $\vpi \sim {\rm Dir}(\valpha)$, where $\valpha$ is a vector with one component per allele, and $\valpha_a$ is a prior pseudocount for allele $a$.  We can set these pseudocounts separately for the ref allele, SNPs, and indels\footnote{In principle we could have a complicated model for prior pseudocounts depending on context.  Furthermore, if we have a big call set like gnomAD, the best prior pseudocounts are simply the allele counts from that call set.  \code{HaplotypeCaller} and \code{GenotypeGVCFs} do not currently exploit gnomAD for this.} to obtain the desired mean and standard deviation of allele frequencies.  Unlike previous approaches which shoehorned multiallelic sites into  biallelic model, this method extends naturally to multiallelic sites.  Since $\valpha$ is not a random variable we can ignore the normalization constant of the Dirichlet, which depends only on $\valpha$, to obtain $P(\vpi | \valpha) \propto  \prod_a \pi_a^{\alpha_a - 1}$.  This, combined with Equations \ref{likelihood} and \ref{indicator}, gives the joint posterior distribution
\begin{equation}
P(\vpi, \vz) \propto \left( \prod_a \pi_a^{\alpha_a - 1} \right) \prod_{sg} \left[ C_g \ell_{sg} \prod_a \pi_a^{n_{ga}} \right]^{z_{sg}}
\label{joint-posterior}
\end{equation}

We perform mean-field variational Bayesian inference on this posterior.  In this framework we posit a factorized approximation: $P(\vpi, \vz) \approx q(\vpi) q(\vz)$ and iterate, alternating between the following two updates:
\begin{align}
q(\vpi) \propto& \exp E_{q(\vz)}[\ln P(\vpi, \vz)] \propto \prod_a \pi_a^{\alpha_a + \sum_{sg} E[z_{sg}] n_{ga} - 1} \label{M}\\
q(\vz) \propto& \exp E_{q(\vpi)}[\ln P(\vpi, \vz)] \propto \prod_g \left[ C_g \ell_{sg} \prod_a e^{ E[\ln \pi_a] n_{ga}} \right]^{z_{sg}} \label{E}
\end{align}
The expectations required have analytic expressions.  Using a well-known (i.e. see the Wikipedia entry for Dirichlet distribution) result:
\begin{align}
E_{q(\vpi)} [ \ln \pi_a ] =& \psi( \alpha_a + \sum_{sg} E[z_{sg}] n_{ga}) - \psi (\sum_a \alpha_a + \sum_{sg} E[z_{sg}]  \sum_a n_{ga}) \\
=& \psi( \alpha_a + \sum_{sg} E[z_{sg}] n_{ga}) - \psi (\sum_a \alpha_a + {\rm total~ploidy~of~all~samples}),
\label{pi_update}
\end{align}
where $\psi$ is the digamma function, and
\begin{align}
E_{q(\vz)}[z_{sg}] = \frac{C_g \ell_{sg} \prod_a e^{ E[\ln \pi_a] n_{ga}}}{\rm normalizing~constant},
\label{z_update}
\end{align}
where the constant of normalization is chosen so that $\sum_g E_{q(\vz)}[z_{sg}] = 1$.  Learning this model is just a matter of iterating Equations \ref{M} and \ref{E}.

Once our iteration converges, we can extract several interesting things, some of which we already report and some of which we do not.  First, we have the Dirichlet posterior $q(\vpi)$ on allele frequencies.  Letting $N_a \equiv \alpha_a + \sum_{sg} E[z_{sg}] n_{ga}$ be the prior plus observed pseudocounts for allele $a$, we have
\begin{equation}
\vpi \sim {\rm Dir}(N_0, N_1 \ldots)
\end{equation}
If we want a univariate allele frequency for a single allele (i.e. not the joint Dirichlet posterior on all allele frequencies) we simply marginalize, which is arithmetically easy:
\begin{equation}
\pi_a \sim {\rm Beta}(N_a, \sum_{a^\prime \ne a} N_{a^\prime})
\end{equation}
From this we can get the mean posterior allele frequency, which is $\bar{\pi}_a = N_a / \sum_{a^\prime} N_a$, but we also have error bars on this estimate.

We can also obtain the variant quality score trivially.  The probability that no variant exists among the samples is (we use the convention that $g=0$ for the hom ref genotype)
\begin{equation}
P({\rm no~variants}) = \prod_s P(z_{sg} = 0) = \prod_s E_{q(\vz)}[z_{s, g=0}]
\end{equation}

We can easily extend this to a per-allele variant quality score by considering not just the hom ref genotype, but all genotypes in which that allele is absent:
\begin{equation}
P({\rm no~allele}~a) = \prod_s \left( \sum_{g : n_{ga} = 0} E_{q(\vz)}[z_{s, g}] \right)
\end{equation}

Let's now consider the run time of this algorithm.  Suppose there are $S$ samples, $G$ genotypes, and $A$ alleles.  The cost of calculating Equation \ref{pi_update} for all values of $a$ is $O(S G A)$.  The cost of calculating Equation \ref{z_update} for all $s$ and $g$ is also $O(S G A)$.  These things usually converge in not too many iterations so we have an $O(S G A)$ algorithm that yields genotype calls, genotype quals (these are the posteriors $E[z_{sg}])$, allele frequencies, per-site variant quality scores, and per-allele variant quality scores.  The algorithm has two equations to learn the model and one equation to translate that into a variant quality score.

For some purposes we may need to calculate a quality score for a single sample.  In this case we drop the sample subscript from Equation \ref{joint-posterior} to get:
\begin{equation}
P(\vpi, z_g = 1) \propto C_g \ell_g \prod_a \pi_a^{\alpha_a + n_{ga} - 1}
\end{equation}
and integrate out $\vpi$ to obtain
\begin{equation}
P(z_g = 1) \propto C_g \ell_g \prod_a \Gamma(n_{ga} + \alpha_a).
\end{equation}
\end{document}