\chapter{Harmonic Analysis of Functions}
\label{ch:harmonic}

In this chapter, we describe some tools for analyzing functions over probability
spaces, for which the sample space exhibits a field structure, and the
distribution is defined in terms of the field operations.  In this case, we can
obtain decompositions with special properties, that helps in
analysing such distributions. We will be concerned with
the sample space which is the vector space of functions of the
form $f:\F_p^R \rightarrow \F_p$ ($p$ is a prime).
 We  extend these decompositions and the associated results
 to the case when the domain is the subspace of low degree
 polynomials over $\F_p$. We prove an  analogue of  hypercontractivity
  for functions over this subspace (\lref[Lemma]{thm:hyp-for-poly}), by
 reducing it to the result  on the full space (\lref[Lemma]{thm:mov-to-poly}).
 This enables us to prove an analogue of a
  result by Alon \etal ~\cite{AlonDFS2004} (\lref[Lemma]{thm:dict}), to
  functions on the subspace (\lref[Lemma]{thm:derand-dict}). We use
these results later in \lref[Chapter]{ch:graph-prod}, for proving some
derandomized graph product results.

\section{Harmonic Analysis for Fields}
\label{sec:harmonic}

Consider the probability space $(\F_p,\mu)$ where $\mu$ is the uniform
probability measure over $\F_p$. We will be working with functions of the form
$A:\F_p^R \rightarrow \C$. Note that all such functions form a vector space over
$\C$ with dimension $p^R$. Characters are a natural orthogonal basis for this
vector space.

\begin{definition}[Character] A \emph{character} of $\F_p^R$ is a function
$\chi:\F_p^R \rightarrow \C$ such that
$$\chi(0) = 1~~~~ \text{ and }~~~~ \forall f,g
\in \F_p^R,~ \chi(f+g) = \chi(f)\chi(g).$$
\end{definition}

The following lists the basic properties of characters, which can
be verified easily.

\begin{observation} \label{lem:fourier}
Let $\{1,\omega,\cdots,\omega^{p-1}\}$ be the
$p$th roots of unity and for $\beta,f \in \F_p^R$,
$$\chi_\beta(f) := \omega^{ \beta \cdot f } ~~~~\text{where}~~~~ \beta \cdot f := \sum_{i=1}^R \beta_i f_i \mod p.$$
\begin{itemize}
\item The characters of $\F_p^R$ are $\{ \chi_\beta : \beta \in \F_p^R\}$.
\item Characters forms an orthonormal basis for the vector space of
functions from $\F_p^R$ to $\C$, under the inner product
$$\langle A, B\rangle := \E_{f \in \F_p^R}\left[A(f)\overline{B(f)}\right].$$
\item Any function $A:\F_p^R \rightarrow \C$
can be uniquely decomposed as
$$A(f) = \sum_{\beta \in \F_p^R}\widehat{A}(\beta) \chi_\beta(f)~~~~\text{
where} ~~~~\widehat{A}(\beta) := \E_{g \in \F_p^R} \left[A(g)
\overline{\chi_\beta(g)}\right].$$
\item For any function $A:\F_p^R \rightarrow\C$,
\begin{equation}
\label{eqn:parseval}
\sum_{\beta \in \F_p^R} |\widehat A(\beta)|^2 = \E_{f\in \F_p^R} \left[|A(f)|^2\right].
\end{equation}
\item For any function $A:\F_p^R\rightarrow \{1,\omega,\cdots,\omega^{p-1}\}$,
\begin{equation}
\label{eqn:plancheral}
\sum_{\beta\in \F_p^R}|\widehat A(\beta)|^2 =1.
\end{equation}
\end{itemize}
\end{observation}

\begin{remark}
\label{rem:char-f2}
Note that when $p=2$, the roots of unity are $\{-1,+1\}$. Then the characters
are real valued functions. It is easy to see that they form a orthonormal
basis for the real vector space of functions of the form $A:\F_2^R \rightarrow
\R$. Hence all the above properties hold with respect to this vector space
as well.
\end{remark}

\begin{definition}[Generalized Dictator] A \emph{generalized dictator} function
$A:\F_p^R \rightarrow
\C$ is one that depends only on a single coordinate. That is, $\exists i \in
[R]$ such that for any $\beta \in \F_p^R$, if it has a non-zero entry in some
 coordinate $j\neq i$ then $\widehat A(\beta) = 0$.
\end{definition}

\begin{definition}[Fourier degree] The \emph{Fourier degree} of a function
$A:\F_p^R \rightarrow \C$ is the smallest number $d$ such that $A$ can be
written as $$A = \sum_{\beta: |\beta| \leq d} \widehat A(\beta) \chi_\beta$$
where $|\beta|$ is number of coordinates $i$ where $\beta_i \neq 0$.
\end{definition}

An interesting fact about functions of the form $A:\F_2^R \rightarrow \{0,1\}$
is that, if $A$ has Fourier degree $1$ then $A$ is a generalized dictator function.
The
following theorem over $\F_p$, says that if the sum of squares of absolute
values of Fourier coefficients of $A$ with $|\beta| >1$ is small, then it is
close to a generalized dictator function. It is a generalization of
the well known FKN Theorem (see \cite{FriedgutKN2002}), which
gives the result for $p=2$.

\begin{lemma}[Alon \etal\ \cite{AlonDFS2004}] \label{thm:dict}
For every prime $p$, there is constant
$K$ (that depends on $p$) such that the following holds:
If $A:\F_p^R \rightarrow \{0,1\}$ satisfies
$$\sum_{|\alpha| > 1} |\widehat A(\alpha)|^2 \leq \epsilon \text{ and } \widehat
A(0) =\delta$$
then there exists a generalized dictator $B:\F_p^R \rightarrow \{0,1\}$ such that
$$\|A-B\|_2 \leq \frac{K\epsilon}{\delta -\delta^2 -\epsilon}.$$
\end{lemma}
The above lemma is proved using the following hypercontractive
inequality.
\begin{lemma}[Hypercontractivity] \label{thm:hyp}
For every prime $p$, there is a
constant $C$ (that depends on $p$)
 such that for any function $A:\F_p^R \rightarrow \C$ with $\widehat
A(\alpha) = 0$ when $|\alpha| > t$,
$$ \|A\|_4 \leq C^t \|A\|_2.$$
\end{lemma}

In the next section we will prove derandomized versions of the above lemmas.

\section{Polynomial Subspaces}

Let $\Pe_{r,d}$ be the set of degree $d$
polynomials on $r$ variables over $\F_p$, with individual degrees $< p$
(the prime $p$ will be clear from the context). Let
$\mathfrak F_r := \Pe_{r,(p-1)r}$. Note that $\mathfrak F_r$ is the set of all
functions from $\F_p^r$ to $\F_p$. $\mathfrak F_r$ is a $\F_p$-vector space of
dimension $p^r$ and $\Pe_{r,d}$ is its subspace of dimension $r^{O(d)}$. The
\emph{Hamming distance} between $f$ and $g \in \mathfrak F_r$, denoted by
$\Delta(f,g)$, is the number of inputs on which $f$ and $g$ differ. When $S
\subseteq \mathfrak F_r$, $\Delta(f, S) := \min_{g\in S} \Delta(f,g)$. We say
$f$ is $\Delta$-far from $S$ if $\Delta(f,S) \geq \Delta$ and $f$ is
$\Delta$-close to $S$ otherwise. For a polynomial $\alpha \in \Pe_{r,d}$,
the support size of the polynomial is $|\alpha| := |\{ x: \alpha(x) \neq 0\}|$.
Given $f,g, \in \mathfrak F_r$, the \emph{dot
product} between them is defined as
$$ f \cdot g  := \sum_{x \in\F_p^r}f(x)g(x) \mod p.$$
For a subspace $S \subseteq \mathfrak F_r$, the \emph{dual subspace}
is defined as
$$S^{\perp} := \{ g \in \mathfrak F_r : \forall f \in S,  g \cdot f  = 0 \}.$$
The following theorem relating dual spaces is well known.

\begin{lemma} \label{lem:dual}
$\Pe_{r,d}^\perp =\Pe_{r,(p-1)r-d-1}$
\end{lemma}
\begin{proof}
First note that the dimensions of the two subspaces are equal by a counting argument.
Next we show that $\Pe_{r,d}^\perp \supseteq \Pe_{r,(p-1)r-d-1}$. We just need to show that for any
monomial of degree $(p-1)r - d -1$ with individual degrees $<p$, the dot product with any monomial of
degree $d$ with individual degrees $<p$ is $0$. The product of any such pair of monomials
is a monomial with total degree at most $(p-1)r - 1$, and hence has a variable with
degree $<p-1$. Without loss of generality, let this variable be $x_1$ with degree $t < p-1$.
 Notice that $\sum_{x_1 \in \F_p} x_1^t = 0 \mod p$  and hence the dot product is $0$.
\end{proof}

We need the following Schwartz-Zippel-like Lemma for degree $d$ polynomials.

\begin{lemma}[Schwartz-Zippel lemma~{\cite[Lemma~3.2]{HaramatySS2013}}]
\label{lem:SZ} Let $f\in \F_p[x_1,\cdots,x_r]$ be a
non-zero polynomial of degree at most $d$ with individual degrees at most $p-1$.
Then the support size ($|f| := |\{ x: f(x) \neq 0\}|$) satisfies
$$|f| \geq p^{r-d/(p-1)}.$$
\end{lemma}
 The following lemma is an easy consequence of \lref[Lemma]{lem:SZ}.

 \begin{lemma} \label{lem:low-deg-local-ind} Let $g$ be a
uniformly random polynomial from $\Pe_{r,d}$. Then its truth table as a random string of length $p^r$
over the alphabet $\F_p$, is $p^{\lfloor (d+1)/(p-1) \rfloor} - 1$-wise independent.
\end{lemma}
\begin{proof} From  \lref[Lemma]{lem:SZ} and
\lref[Lemma]{lem:dual}, we know that any non-zero polynomial in $\Pe^{\perp}_{r,d} = \Pe_{r,(p-1)r - d -1}$
has support size at least $p^{\lfloor (d+1)/(p-1) \rfloor}$. Suppose there is a subset $S$
of size $p^{\lfloor (d+1)/(p-1) \rfloor}-1$, where $g$ is not uniform. Let $V \subseteq \F_p^{|S|}$
 be the set of restrictions of
truth tables of polynomials in $\Pe_{r,d}$ to $S$. Note that $V$ is a subspace. Since
the truth table of $g$ restricted to S is not uniformly distributed, the dimension
of $V$ is $<|S|$. Then $V^{\perp} \subseteq \F_p^{|S|}$ is non-empty. Consider a
non-zero $v \in V^{\perp}$. Then the function $f$ which is zero outside $S$ and $f_{|S} = v$
corresponds to a non-zero polynomial which belongs to $\Pe^{\perp}_{r,d} = \Pe_{r,(p-1)r - d -1}$
with support $< p^{\lfloor (d+1)/(p-1) \rfloor}$ which is a contradiction.

\end{proof}

The following lemma is an easy consequence of \lref[Lemma]{lem:low-deg-local-ind}
\begin{lemma}
\label{lem:interpol}
 Let $d>1$, $X$ be a set of $p^{d}-1$ points in $\F^r_p$ and
$f:X \rightarrow \F_p$ an arbitrary function. Then there exists a polynomial $q$
of degree at most $(p-1)d$ such that $q$ agrees with $f$ on all points in $X$.
\end{lemma}
\begin{proof}
By \lref[Lemma]{lem:low-deg-local-ind},
the truth table of a random polynomial $g$ of degree $(p-1)d$ is $p^d -1$-wise independent.
Hence $g_{|X} = f$ with non-zero probability.
 \end{proof}

\section{Harmonic Analysis for Polynomial Subspaces}
\label{sec:har-poly}

We  define a
orthonormal basis set of characters for the vector space of functions of the
form $A:\Pe_{r,d} \rightarrow \C$.
\begin{definition}[Character]
A \emph{character} of $\Pe_{r,d}$ is a function $\chi:\Pe_{r,d} \rightarrow \C$
such that
$$\chi(0) = 1 \text{ and } \forall f,g \in \Pe_{r,d},~ \chi(f+g)=\chi(f)\chi(g).$$
\end{definition}

The following lists the basic properties of characters
(similar to \lref[Observation]{lem:fourier}).

\begin{observation}[{\cite[Section II C]{DinurG2013}}]
\label{lem:fourier-poly}
Let $\{1,\omega,\cdots,\omega^{p-1}\}$ be the $p$th roots of
unity and for $\beta \in \mathfrak F_r, f \in \Pe_{r,d}$,

 $$\chi_\beta(f) := \omega^{ \beta \cdot f } ~~~~\text{where}
 ~~~~ \beta \cdot f := \sum_{x \in \F_p^r} \beta(x) f(x) \mod p.$$

\begin{itemize} \item The characters of
$\Pe_{r,d}$ are $\{ \chi_\beta : \beta \in \mathfrak F_r\}$. \item For any
$\beta,\beta' \in \mathfrak F_r$, $\chi_\beta = \chi_\beta'$ if and only if
$\beta-\beta' \in \Pe_{r,d}^\perp$.
\item For $\beta \in \Pe_{r,d}^\perp$,
$\chi_\beta$ is the constant $1$ function.
\item\label{item:minsup} $\forall
\beta, \exists \beta'$ such that $\beta-\beta' \in \Pe_{r,d}^\perp$ and
$|\beta'| = \Delta(\beta, \Pe_{r,d}^\perp)$ (i.e., the constant $0$
function is (one of) the closest function to $\beta'$ in $\Pe_{r,d}^\perp$). We
call such a $\beta'$ a minimum support function for the coset $\beta +
\Pe_{r,d}^\perp$.
\item Characters forms an orthonormal basis for the vector
space of functions from $\Pe_{r,d}$ to $\C$, under the inner product
$$\langle A,
B\rangle := \E_{f \in \Pe_{r,d}} \left[A(f)\overline{B(f)}\right].$$

\item Any function $A:\Pe_{r,d} \rightarrow \C$ can be uniquely decomposed as
$$A(f) =\sum_{\beta \in \Lambda_{r,d}}\widehat{A}(\beta) \chi_\beta(f)
~~~~ \text{where} ~~~~ \widehat{A}(\beta) := \E_{g \in \Pe_{r,d}} \left[A(g)
\overline{\chi_\beta(g)}\right]$$
 and $\Lambda_{r,d}$ is the set of minimum
support functions, one for each of the cosets in $\mathfrak
F_r/\Pe_{r,d}^\perp$, with ties broken arbitrarily.
\item For any function $A:\Pe_{r,d} \rightarrow\C$,
$$\sum_{\beta \in \Lambda_{r,d}} \left|\widehat A(\beta)\right|^2 = \E_{f\in \Pe_{r,d}}
\left[\left|A(f)\right|^2\right].$$
\item For any function $A:\Pe_{r,d}\rightarrow\{1,\omega,\cdots,\omega^{p-1}\}$,
$$\sum_{\beta\in \Lambda_{r,d}}|\widehat A(\beta)|^2 =1.$$
\end{itemize}
\end{observation}
The following lemma relates
characters over different domains related by co-ordinate projections.
\begin{lemma} \label{lem:char-projection}
Let $m \leq r$ and $\pi:\F_p^r
\rightarrow \F_p^m$ be a (co-ordinate) projection i.e., there exist indices $1
\leq i_1 < \cdots < i_m \leq r$ such that $\pi(x_1,\dots,x_r) = (x_{i_1}, \cdots
,x_{i_m})$. Then for $f \in \Pe_{m,d}, ~\beta \in \Pe_{r,d}$,
$$\chi_\beta(f\circ \pi)= \chi_{\pi_p(\beta)}(f),$$
where $\pi_p(\beta)(y):=\sum_{x \in \pi^{-1}(y)} \beta(x)$.
\end{lemma}
\begin{proof}
Without loss of generality, let $\{i_1, \cdots, i_m\} = \{1,\cdots, m\}$. Then
\begin{align*}
 \chi_\beta(f\circ \pi) &= \omega^{\sum_{x \in \F_p^r} f\circ \pi(x) \cdot \beta(x)} \\
&= \omega^{\sum_{(x_1,\cdots,x_m) \in \F_p^m} f(x_1,\cdots,x_m)\cdot( \sum_{(x_{m+1},\cdots, x_n)} \beta(x))}\\
&= \chi_{\pi_p(\beta)}(f)
\end{align*}
\end{proof}

Influence and generalized dictators can be defined for functions on polynomial subspaces
similar to the product setting.

\begin{definition}[Influence] For a function $A:\Pe_{r,d} \rightarrow \C$ and a
number $k < p^{\lfloor (d+1)/(p-1) \rfloor}/2$, the degree $k$ influence of $a\in
\F_p^r$ is defined as
$$\Inf^{\leq k}_a(A) = \sum_{\beta \in \Lambda_{r,d}:\beta(a)
\neq 0 \text{ and } |\beta| \leq k} |\widehat A(\beta)|^2.$$
\end{definition}

\begin{definition}[Generalized Dictator] A function $A:\Pe_{r,d} \rightarrow \C$ is a
generalized dictator if there exists $x\in \F_p^r$ and $\widehat A_0, \widehat A_{1}, \cdots,
\widehat A_{p-1} \in \C$ such that $A$ can be written as $A = \widehat A_0 +
\sum_{i=1}^{p-1} \widehat A_{i}\chi_{ie_x}$ where $e_x:\F_p^r
\rightarrow \F_p$ the indicator function for $x$.
\end{definition}

\begin{lemma}\label{lem:level1-dict}
Let $A:\Pe_{r,d} \rightarrow \{0,1\}$ be such that all non-zero
Fourier coefficients have support size $\leq 1$. Then $A$ is a generalized dicator.
\end{lemma}
\begin{proof}
The proof is similar to the proof of \cite[Lemma 2.3]{AlonDFS2004}. Consider the function $(A(f))^2$. Since $A$ is $\{0,1\}$ valued $(A(f))^2 = A(f)$. Equation the Fourier coefficients on both sides will give that there is an $x\in \F_p^r$ such that $A(f)$ only depends on $f(x)$.
\end{proof}

We prove an analogue of \lref[Theorem]{thm:hyp}, to functions over polynomial
subspaces.

\begin{lemma}
\label{thm:hyp-for-poly} For every prime $p$,
there is a constant $C$ such that for $4t\leq p^{d-1}$
and any function $A:\Pe_{r,(p-1)d} \rightarrow \C$ with $\widehat A_\alpha = 0$ when
$|\alpha| > t$, $$\|A\|_4 \leq C^t \|A\|_2.$$
\end{lemma}
\begin{proof} Follows
from \lref[Lemma]{thm:mov-to-poly} and \lref[Lemma]{thm:hyp}.
\end{proof}

We prove an analogue of \lref[Theorem]{thm:dict}, to functions over polynomial
subspaces.
\begin{lemma} \label{thm:derand-dict}
For every prime $p$, there is a constant $K$ such
that the following holds: If $A:\Pe_{r,(p-1)d} \rightarrow \{0,1\}$ satisfies
$$\sum_{|\alpha| > 1} |\widehat A(\alpha)|^2 \leq \epsilon \text{ and } \widehat
A(0) =\delta$$
then there exists a generalized dictator $B:\Pe_{r,(p-1)d} \rightarrow \{0,1\}$
such that
$$\|A-B\|^2_2 \leq \frac{K\epsilon}{\delta -\delta^2-\epsilon}.$$
\end{lemma}
\begin{proof}
The proof of the lemma is similar to the proof of \cite[Lemma 2.4]{AlonDFS2004}.
Let $K = 2 + 32C^8$ where $C$ is the constant
from \lref[Lemma]{thm:hyp-for-poly}.
First if $\epsilon \geq \frac{1}{32C^8}$, then the lemma is true.
This is because, for any $B:\Pe_{r,(p-1)d}\rightarrow\{0,1\}$,
$A-B$ is a $\{-1, 0,1\}$ valued function and $\| A-B\|^2_2 \leq 1$.

Now assume $\epsilon < \frac{1}{32C^8}$. Let
$$A_S = \sum_{|\alpha| \leq 1} \widehat A(\alpha) \chi_\alpha \mbox{ and } A_L = \sum_{|\alpha| > 1} \widehat A(\alpha) \chi_\alpha.$$
($S$, $L$ stands for small and large). If $A_S$ were Boolean, it  has to be a dictator by \lref[Lemma]{lem:level1-dict}. Then the lemma follows by taking $B=A_S$. Consider the following function
which measures the farness of $A_S$ from being Boolean (it is identically $0$, for Boolean functions)
$$H := A_S^2 - A_S.$$
Since $A_S$ does not have any Fourier coeffcients with support $>1$, $H$ will have only Fourier coefficients  with $|\alpha$ to be $0,1$ and $2$.
Let $e_x$ be the function with $e_x(x)=1$ and $0$ otherwise. Then for $a,b \in F_p, x,y \in \F_p^r$
$$ \widehat H(ae_x + be_y) = 2 \widehat A(ae_x) \widehat A(be_y).$$
The following claim says that the norm of $H$ is small.
\begin{claim}\label{claim:h}
$$\|H\|^2 \leq 32C^8 \epsilon.$$
\end{claim}
The claim is proved later. Let $a_x := \sum_{ i \in \F_p} | \widehat A(i e_x)|$. Note that
\begin{equation}
\sum_{x,y \in \F_p^r, x\neq y} a_x a_y \leq \frac{\|H\|^2}{4} \leq 8 C^8 .
\end{equation}
Also from assumptions in the claim,
\begin{equation}
\sum_{x \in \F_p^r} a_x = \delta - \delta^2 - \epsilon.
\end{equation}
Let $y$ be such that $a_y$ is maximal. Then
$$(\sum_x a_x)^2 \leq \sum_x a_x^2 + 16C^8 \leq a_y \sum_x a_x + 16C^8.$$
This gives that $a_y \geq \delta -\delta^2 -\epsilon(1+ 16C^8/(\delta -\delta^2 - \epsilon))$.
If $B' := \widehat A(0) + \sum_{i \in \F_p} \widehat A(i e_y) \chi_{i e_y}$, then
we have that $\|A - B'\|^2_2 \leq \epsilon(1+ 16C^8/(\delta -\delta^2 - \epsilon)).$
Now rounding $B$ to the closest $[0,1]$ valued function $B'$ pointwise, we get that
$$\|A-B\|_2^2 \leq 2\|A-B'\|_2^2 \leq  \frac{K\epsilon}{\delta -\delta^2-\epsilon}.$$

\begin{proof}[Proof of Claim \ref{claim:h} ]
First notice,
$$H = A_S^2 - A_S = (A-A_L)^2- (A-A_L) = A_L^2 +A_L(1-2A).$$
Let $k=2C^4$ and $Z = \{f : | A_L(f) \leq k \sqrt{\epsilon}\}$. Since $\|A_L\|_2^2 \leq \epsilon$,
by a Markov argument, $\Pr_f[Z] \geq 1- 1/k^2$.  Also for every $f \in Z$,
$|H(f)| \leq 2 |A_L(f)| \leq 2k \sqrt{\epsilon}$. Since $H$ has only Fourier coefficients
with support size $0,1,2$, we can use \lref[Lemma]{thm:hyp-for-poly} with $t=2$. The claim follows from
the following
\begin{align*}
\|H\|_2^2 &= \E_f |H(f)|^2 = \Pr[Z] \E_{f \in Z} |H(f)|^2   + (1-\Pr[Z]) \E_{f \notin Z} |H(f)|^2\\
&\leq 4k^2\epsilon + \frac{1}{k^2} \sqrt{\E_{f \notin Z} |H(f)|^4}\\
&\leq 4k^2\epsilon + \frac{1}{k} \sqrt{\E_{f} |H(f)|^4}\\
&\leq 4k^2\epsilon + \frac{1}{k} C^4 \|H\|_2^2 \leq 32C^8\epsilon
\end{align*}
\end{proof}

\end{proof}
\begin{definition}[Lift]\label{def:lift} For a function $B:\Pe_{r,d}
\rightarrow \C$ with the Fourier decomposition $B= \sum_{\alpha \in
\Lambda_{r,d}} \widehat{B}(\alpha) \chi_\alpha$, the lift of $B$ denoted by $B'$
is a function $B':\Ef_r \rightarrow \C$ with the Fourier decomposition $B'=
\sum_{\alpha \in \Lambda_{r,d}} \widehat{B}(\alpha) \chi_\alpha$. In the
decomposition of $B'$, $\chi_\alpha$'s are functions with domain $\Ef_r$.
\end{definition}
\begin{lemma} \label{thm:mov-to-poly}
If $2kt \leq p^{d-1}$ and
$B:\Pe_{r,(p-1)d} \rightarrow \C$ be a function such that $\widehat B(\alpha) = 0$
when $|\alpha| > t$ then
$$\|B\|_{2k} = \|B'\|_{2k}.$$
\end{lemma}
\begin{proof}
From the \lref[Lemma]{lem:SZ} and \lref[Lemma]{lem:dual}, we have that $\forall
\alpha \in \Pe^{\perp}_{r,(p-1)d}\setminus \{0\},~|\alpha| > p^{d-1}$. So if
$\exists \{\alpha_i,\beta_i \}_{i \in [k]}$ with $|\alpha_i|,|\beta_i| \leq t$,
then \begin{equation} \label{eqn:small-sup-is-zero} \sum_{i \in [k]} \alpha_i
-\beta_i \in \Pe^{\perp}_{r,(p-1)d} \Rightarrow \sum_{i \in [k]} \alpha_i -\beta_i =
0. \end{equation} This is because $\sum_{i \in [t]} \alpha_i -\beta_i$ has
support size at most $2kt < p^{d-1}$. We use this fact to prove the theorem as
follows: \begin{align*} \|B\|_{2k}^{2k} & = \E_{f \in \Pe_{r,(p-1)d}} |B(f)|^{2k}
= \E_{f \in \Pe_{r,(p-1)d}} \prod_{i \in [k]}B(f)\overline {B(f)}\\
&=\sum_{\alpha_1,\beta_1,\cdots,\alpha_k,\beta_k \in \Lambda_{r,(p-1)d}}
\left(\prod_{i \in [k]}\widehat B_{\alpha_i} \overline{\widehat
B_{\beta_i}}\right) \E_{f \in \Pe_{r,(p-1)d}} \prod_{i \in [k]} \chi_{\alpha_i}(f)
\overline{\chi_{\beta_i}(f)}\\
&=\sum_{\substack{\alpha_1,\beta_1,\cdots,\alpha_k,\beta_k \in \Lambda_{r,(p-1)d}\\
\sum_i \alpha_i -\beta_i \in \Pe^{\perp}_{r,(p-1)d}}}~ \prod_{i \in [k]}\widehat
B_{\alpha_i} \overline{\widehat B_{\beta_i}} \\
&=\sum_{\substack{\alpha_1,\beta_1,\cdots,\alpha_k,\beta_k \in \Lambda_{r,(p-1)d} \\
\sum_i \alpha_i -\beta_i = 0}}~ \prod_{i \in [k]}\widehat B_{\alpha_i}
\overline{\widehat B_{\beta_i}}~~(\text{ from }
\eqref{eqn:small-sup-is-zero}~)\\ &=
\sum_{\alpha_1,\beta_1,\cdots,\alpha_k,\beta_k\in \Lambda_{r,(p-1)d}} \left(\prod_{i
\in [k]}\widehat B_{\alpha_i} \overline{\widehat B_{\beta_i}}\right) \E_{f \in
\Ef_r} \prod_{i \in [k]} \chi_{\alpha_i}(f) \overline{\chi_{\beta_i}(f)}\\ &=
\E_{f \in \Ef_r} \prod_{i \in[k]}B'(f)\overline {B'(f)} = \E_{f \in \Ef_r}
|B'(f)|^{2k}=\|B'\|_{2k}^{2k} \end{align*} \end{proof}

\subsection{Folding over Subspace}

\begin{definition}[Folded function over a subspace]
For any set $S$, a function $A: \Pe_{r,(p-1)d} \rightarrow S$ is said
to be folded over a subspace $J \subseteq \Pe_{r,(p-1)d}$ if $A$ is constant
over cosets of $J$ in $\Pe_{r,(p-1)d}$.
\end{definition}
\begin{fact}
\label{fact:ideallift} Given a function $A:\Pe_{r,(p-1)d}/J \rightarrow S$ there
is a unique function $A':\Pe_{r,(p-1)d} \rightarrow S$ that is folded over $J$
such that for $g \in \Pe_{r,(p-1)d}, A'(g) = A(g + J)$.
\end{fact}
Given
$q_1,\cdots, q_k \in \Pe_{r,3(p-1)}$, let
$$J(q_1,\dots,q_k): = \left\{ \sum_i r_i q_i : r_i \in \Pe_{r,(p-1)(d-3)}\right\}.$$
The following lemma shows that
if a function is folded over $J=J(q_1,\dots,q_k)$, then it cannot have weight on
small support characters that are non-zero on $J$ (this is a generalization of
the corresponding lemma by Dinur \& Guruswami~\cite{DinurG2013} to arbitrary
fields).
\begin{lemma} \label{lem:goodsupport} Let $\beta \in \mathfrak F_r$ is
such that $|\supp(\beta)| < p^{d-3}$, and there exists $x \in \supp(\beta)$ with
$q_i(x) \neq 0$ for some $i$. Then if $A:\Pe_{r,d}\rightarrow \C$ is folded over
$J=J(q_1,\dots,q_k)$, then $\widehat{A}(\beta) = 0$.
\end{lemma}
\begin{proof}
Construct a polynomial $t$ which is zero at all points in support of $\beta$
except at $x$. From \lref[Lemma]{lem:interpol}, its possible to construct such a
polynomial of degree at most $(p-1)(d -3)$. Then we have that $tq_i \in J$ and
$\langle\beta,tq_i\rangle \neq 0$. Now
\begin{align*}
\E_h\left[A(h)\chi_\beta(h)\right] &=\frac{1}{p} \E_h[ A(h)\chi_\beta(h) +
A(h+tq_i)\chi_\beta(h+tq_i)+\cdots \\
&\qquad + A(h+(p-1)tq_i)\chi_\beta(h+(p-1)tq_i)]\\
 &=\frac{1}{p}\E_h[ A(h)\chi_\beta(h) + A(h)\chi_\beta(h+tq_i) +\cdots\\
 & \qquad + A(h)\chi_\beta(h+(p-1)tq_i)]\\
 &=\frac{1}{p}\E_h[ A(h)\chi_\beta(h)(1+\chi_\beta(tq_i)+\cdots+\chi_\beta((p-1)tq_i)) ]\\
 &=\frac{1}{p}\E_h[ A(h)\chi_\beta(h)(1+\omega^{t(\beta \cdot q_i)}+\cdots+\omega^{(p-1)t(\beta \cdot q_i)}) ] =0\qquad \qedhere
\end{align*}
The last step is due to the fact that  the sum $(1+\omega+\cdots+ \omega^{p-1}) =0$. Since $t(\beta\cdot q_i) \neq 0$,
the previous equation contains this sum.
\end{proof}


\subsection*{Binary boolean operators}

\subsubsection*{\href{https://sourceacademy.org/sicpjs/1.1.6\#p4}{Conjunction}}

\[
\textit{expression}_1 \ \textbf{\texttt{\&\&}} \ \textit{expression}_2
\]
stands for
\[
\textit{expression}_1 \ \textbf{\texttt{?}} \ \textit{expression}_2 \ \textbf{\texttt{:}}\ \textbf{\texttt{false}}
\]

\subsubsection*{\href{https://sourceacademy.org/sicpjs/1.1.6\#p4}{Disjunction}}

\[
\textit{expression}_1 \ \textbf{\texttt{||}} \ \textit{expression}_2
\]
stands for
\[
\textit{expression}_1 \ \textbf{\texttt{?}}\ \textbf{\texttt{true}}\  \textbf{\texttt{:}}\ \textit{expression}_2
\]


\lab{The SVD and Image Compression}{SVD}

\objective{Learn how to compute the compact SVD. Explore the SVD as a method of matrix approximation, and use it to perform image compression.}
\label{lab:SVD}
% TODO
% - write a more compelling objective.
% - add references to the textbook and determine what is actually needed here. The explanation in the textbook is way better.
%

The \emph{Singular Value Decomposition} or \emph{SVD} is a matrix decomposition that is widely used in both theoretical and applied mathematics.
Originally discovered by theoretical mathematicians, it is a canonical way to decompose a matrix.
Its practical use became apparent later on when Erhard Schmidt showed that the SVD could be a computational tool for providing low-rank matrix approximations.
Modern developments continue to confirm the importance of the SVD in both computational and theoretical applications.

\begin{comment}
The theoretical use of the \emph{Singular Value Decomposition} or \emph{SVD} has long been appreciated.
In fact, the idea of a canonical way of decomposing a matrix was so alluring that the SVD was independently discovered by at least four people through use of both  integral equations and systems of linear equations.

However, it wasn't until Erhard Schmidt showed how the SVD could be a computational tool for providing low-rank approximations that the practical applications became apparent.
Since Schmidt's work, further developments have confirmed the importance of the SVD in both computational and theoretical applications.
\end{comment}

\begin{comment}

The \emph{Singular Value Decomposition} is so important that it was discovered multiple times by different people in different ways.
The foundation work in systems of linear equations was laid by Gauss and Cauchy in the 1820's and later by Jacobi in 1846 with his work with the LU decomposition; however, the real work with the SVD started with Eugenio Beltrami.
In a paper he published in 1873 he was the first to work with the SVD; he was limited to real, square, nonsingular matrices with distinct singular values.
A year later, Camille Jordan published his own independent work that was more rigourus and avoided some of the pitfalls of Beltrami's research.
Together, Beltrami and Jordan are considered the codiscoverers of the singular value decomposition.

Later, James Joseph Sylvester independently presented an iterative algorithm and a rule for carrying out the reduction in 1889.
This rule was essentially Beltrami's work.
However Sylvester sent the note detailing his rule to the same journal Jordan had published in, showing not just his ignorance of Jordan's and Beltrami's works but his perception of the importance of the SVD.

The concurrent and independant derivation of the SVD shows its importance as a theoretical tool, but it wasn't until Erhard Schmidt discovered a computational use for the SVD that the practical applications became apparent.
Schmidt used integral equations rather than linear equations to derive the SVD, but
his most important contribution was showing how the SVD could be a computational tool to obtain optimal, low-rank approximations.

Since Schmidt's work, further devolpments have confirmed the importance of the SVD in both practical and theoretical applications.
\end{comment}

\subsection*{Computing the SVD}

The Singular Value Decomposition decomposes an $m \times n$ matrix $A$ into the form
\begin{equation*}
A = U \Sigma V\hrm
\end{equation*}
where $U$ and $V$ are square and unitary of sizes $m$ and $n$ respectively, and $\Sigma$ is diagonal and of size $m \times n$.
The values along the diagonal of $\Sigma$ are called the \emph{singular values} of $A$.
These are also the square roots of the eigenvalues of $A\hrm A$.
% if $A$ is square, are equal to the absolute value of the eigenvalues of $A$.
Commonly the singular values are listed in decreasing order. Thus we have

\begin{equation*}
\Sigma = \mbox{diag}(\sigma_1,\sigma_2,\ldots,\sigma_n)
\end{equation*}
where $\sigma_1 \geq \sigma_2 \geq \ldots \geq \sigma_n \geq 0$ are the singular values of $A$.

If A is of rank $r$, then A has exactly $r$ nonzero singular values.
The first $r$ columns of U span the range of A, and the last $n -r$ columns span the null space of $A\hrm$.
Likewise, the first $r$ columns of V span the range of $A\hrm$ and the last $m - r$ span the null space of $A$.

For a more in-depth definition and proof that the SVD exists for every matrix, refer to the section on the SVD in the text.
Here we will focus on computing the SVD.

First, we define two modifications of the regular SVD.
In the \emph{compact SVD}, we only keep the $r$ nonzero singular values.
Only $r$ column vectors of $U$ and $r$ row vectors of $V\hrm$, corresponding to the $r$ singular values, are calculated.
The compact SVD takes the form $A= U_r \Sigma_r V_r\hrm$ where $U_r$ is $m\times r$, $\Sigma_r$ is $r\times r$ and diagonal, and $V_r\hrm$ is $r\times n$.
Although we drop the decompositions of the nullspaces, by calculating $U_1 \Sigma_1 V_1\hrm$ we can still recover the full matrix $A$.

The \emph{truncated SVD} is similar to the compact SVD, but instead of keeping all the nonzero singular values, we only keep the $k$ largest.
While this saves space, it means that we cannot recover the whole matrix.
Instead we end up with $\widehat A_k = U_k\Sigma_k V_k\hrm$ where $\widehat A_k$ is a rank $k$ approximation of $A$, $U_k$ is $m\times k$, $\Sigma$ is $k \times k$ and diagonal, and $V_k\hrm$ is $k \times n$.

The components of the compact or truncated SVD can be calculated as follows:
\begin{itemize}
\item The singular values of $A$, which form the diagonal of $\Sigma$, are the square roots of the eigenvalues of $A\hrm A$.
These are sorted in descending order.
For the compact SVD, keep all of the nonzero singular values.
For the truncated SVD, keep only the largest $k$.
\item The columns of $V$ are the eigenvectors of $A\hrm A$, where the $i$th column $V_i$ matches the $i$th singular value.
\item The columns of $U$ are $U_i = \frac{1}{\sigma_i} AV_i$.
\end{itemize}

\begin{comment}
\begin{algorithm}
\begin{algorithmic}[1]
\caption{SVD for Full Rank Square Matrices.}
\label{Alg:svd_full}
\Procedure{SVD}{$A$}
\State $n \gets \shape{A}$
\State $eigs \gets$ eigenvalues$(A\hrm A)$
\State sort$(eigs)$
\State $V \gets$ eigenvectors$(A\hrm A)$
\State $svals \gets \sqrt{eigs}$
\State $U \gets \allocate{n,n}$
\For{$i=0\ldots n-1$}
      \State $U_i \gets  \frac{1}{svals[i]} AV_i$
\EndFor
\State $S \gets$ diag$(svals)$
\State \pseudoli{return} $U, S, V\hrm$
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{comment}

\begin{problem}
Write a function \li{truncated_svd} that accepts a matrix $A$ and an optional integer \li{k = None}.
If \li{k} is \li{None}, calculate the compact SVD. If \li{k} is an integer, calculate the truncated SVD, keeping only the \li{k} largest singular values.
(Note: if there are fewer than \li{k} nonzero singular values, the truncated SVD will come out the same as the compact SVD.)
Since the only difference between these two processes is the number of singular values we keep, we only need to write one function.

Here's an outline to follow:

\begin{enumerate}
\item Find the eigenvalues and eigenvectors of $A\hrm A$.
\item Find the singular values of $A$. Keep only the greatest \li{k}, and discard any that are equal to zero.
\item Calculate $V$.
\item Calculate $U$.
\end{enumerate}

Return U, the diagonal of $\Sigma$, and V.
Check your function by calculating the compact SVD and seeing if $U\Sigma V\hrm = A$ using \li{np.allclose()}.

Hint: When calculating the SVD, you will need to sort the eigenvalues while keeping track of their associated eigenvectors.
Consider using the function \li{np.argsort} for keeping the eigenvalues and eigenvectors in the same order while sorting.
%Look for places to use fancy indexing when sorting and selecting the singular values.
\label{prob:calc_svd}
\end{problem}

\begin{comment}
%Commented out because finding the rest of the SVD isn't important to this lab.
%They can reference the book for this information.
If $A$ is full rank and square, it has $n$ nonzero singular values and this process gives us the full SVD.
If $A$ is not full rank or not square, we can compute the first $r$ columns of $U$ and $V$, where $r$ is the number of nonzero singular values, in this way.
To find the rest of the SVD, we can use Gram-Schmidt orthonormalization.

More specifically, let $r$ be the number of nonzero singular values.
If $r<n$ then we only have $r$ eigenvectors to fill $V$, which is a $n\times n$ matrix.
We find the remaining columns of $V$ by using Gram-Schmidt orthonormalization to finish the basis for $n$-space.
Similarly, if $r<m$ then we only have $r$ columns of $U$ so we use Gram-Schmidt orthonormalization to finish the basis for $m$-space.
The compact SVD does not have this problem because $U_1$ and $V_1$ only have $r$ columns, the remaining columns are the decomposition of the null space of the matrix, and are not included.
In this lab we calculate the compact SVD for simplicity.
\end{comment}

\begin{info}
In practice, calculating $A\hrm A$ in order to find its eigenvalues is unstable.
We use this method here because it is mathematically the simplest.
However, industrial SVD solvers use different methods that avoid computing $A\hrm A$.
\end{info}

\subsection*{Visualizing the SVD}
Recall that a matrix is a way to express a linear transformation.
An $m\times n$ matrix defines a linear transformation that sends points from $\mathbb{R}^n$ to $\mathbb{R}^m$.

Intuitively, the SVD can be thought of as breaking a linear transformation into more basic steps.
The SVD decomposes a given matrix into two rotations and a scaling.
$V\hrm$ represents a rotation, $\Sigma$ represents a rescaling along the principal axes, and $U$ represents another rotation.

\begin{figure}
\centering
\begin{subfigure}[b]{.49\textwidth}
  \centering
  \includegraphics[width=\textwidth]{unit_circle.pdf}
  \caption{The unit circle, $S$, with two unit vectors.}
\end{subfigure}
\begin{subfigure}[b]{.49\textwidth}
  \centering
  \includegraphics[width=\textwidth]{vcircle.pdf}
  \caption{$V\hrm S$}
  %\label{fig:svals_plot}
\end{subfigure}
\begin{subfigure}[b]{.49\textwidth}
  \centering
  \includegraphics[width=\textwidth]{svcircle.pdf}
  \caption{$\Sigma V\hrm S$}
  %\label{fig:svals_plot}
\end{subfigure}
\begin{subfigure}[b]{.49\textwidth}
  \centering
  \includegraphics[width=\textwidth]{full_transformation.pdf}
  \caption{$U \Sigma V\hrm S$}
  %\label{fig:svals_plot}
\end{subfigure}
\caption{Each step in transforming the unit circle and two unit vectors using the matrix $A$.}
\label{fig:sol1}
\end{figure}

\begin{problem}
In this problem we will use the SVD to visualize how the matrix
\[A =  \left[\begin{array}{cc}3 & 1\\1 & 3\end{array}\right]\]
acts on points in $\mathbb{R}^2$.
Given a set of points $S$ in $\mathbb{R}^2$, we can calculate the transformation $AS$ in steps by using the SVD $A = U\Sigma V\hrm$.

Specifically, let $S$ be a set of points on the unit circle.
To generate the $x$- and $y$-coordinates of $S$, recall the equation for the unit circle in polar coordinates:
\begin{align*}x = \cos(\theta) && y = \sin(\theta),\end{align*}
where $\theta \in [0, 2\pi].$

Plot four separate subplots to demonstrate each step of the transformation, plotting $S$, $V\hrm S$, $\Sigma V\hrm S$, then $U\Sigma V\hrm$.
Do the same for the standard basis vectors $\e_1 = [1, 0]\trp$ and $\e_2 = [0, 1]\trp$.
Your solution should look similar to Figure \ref{fig:sol1}.
\\(Hint: Force the plot to use the same scale on each of the axes with \li{plt.axis("equal")}. Otherwise, the circle will appear elliptical.)
\end{problem}

\begin{comment}
\subsection*{Image Data Compression}
In this lab, we explore how the SVD can be used to compress image data.
Recall that an image is simply a matrix where each position is the color value for the pixel in that position.
The SVD lets us choose how much information to keep, and what information is most important.
Larger eigenvalues correspond to columns of $U$ and $V$ that contain more information, while smaller eigenvalues correspond to less important columns.
This idea is used in many areas of applied mathematics including signal processing, statistics, semantic indexing (search engines), and control theory.
\end{comment}

\subsection*{The SVD and Data Compression}
We now turn to computational uses of the SVD. We will explore how the SVD is useful for matrix approximations, and use it to compress images.

\subsection*{Low-Rank Matrix Approximation}
If the rank $r$ of a matrix $A$ is significantly smaller than its dimensions, the compact SVD offers a way to store $A$ with less memory.
Storing an $m\times n$ matrix requires storing $mn$ values.
By decomposing the original matrix into the compact SVD, $U_r$, $\Sigma_r$ and $V_r$ together require $mr+r+nr$ values.
This is an efficient storage method if $r$ is much smaller than both $m$ and $n$.
For example, suppose $m=100$, $n=200$ and $r=20$.
Then the original matrix would require storing $20,000$ values, whereas the compact SVD only requires storing $6020$.

The truncated SVD allows even greater efficiency.
By only keeping the first $k$ singular values, we can create an approximation $\widehat A_k = U_k\Sigma_k V_k\hrm$.
This requires storing only $mk+k+nk$ values.
As we make $k$ small, we eventually require very little storage. This comes at the cost of losing information from the original matrix.

The beauty of the SVD is that it makes it easy to only keep the information that is most important.
Larger singular values correspond to columns of $U$ and $V$ that contain more information, so dropping the smallest singular values retains as much information as possible.
In mathematical terms, given a matrix $A$ and its rank-k truncated SVD approximation $\widehat A_k = U_k\Sigma_k V_k\hrm$, the matrix $\widehat A_k$ is the \emph{best rank $k$ approximation} to $A$ (with respect to the induced 2-norm and Frobenius norm).
This is a very significant concept in applied mathematics, appearing in areas including signal processing, statistics, semantic indexing, and control theory.

\begin{comment}
We can also calculate $\widehat A$ by finding the full SVD, and setting all singular values after the $k$th to zero.
Thus the modified $\Sigma$ would be
\begin{equation*}
\Sigma_{\widehat A} = \mbox{diag}(\sigma_1,\sigma_2,\ldots,\sigma_s,0,\ldots,0).
\end{equation*}
Multiplying this matrix with the original $U$ and $V\hrm$ will give the same $\widehat A$ that was found by computing the truncated SVD directly.
\end{comment}

\subsection*{Implementation}
We can use SciPy's linear algebra module to create low-rank SVD approximations or a given matrix.
The code below computes the SVD of \li{A}.
\begin{lstlisting}
>>> import numpy as np
>>> import scipy.linalg as la
>>> A = np.array([[1,1,3,4], [5,4,3,7], [9,10,10,12], [13,14,15,16], [17,18,19,20]])
>>> U,s,Vh = la.svd(A, full_matrices=False)
\end{lstlisting}
In the last line of code, we included the keyword argument \li{full_matrices=False} to calculate the
compact SVD rather than the full SVD. The arrays \li{U} and \li{Vh} correspond to the matrices
$U_r$ and $V_r\hrm$ discussed earlier. The array \li{s} gives the nonzero singular values
of the matrix \li{A}, and we can find the rank of \li{A} by inspecting the number of entries in \li{s} (here we have a rank 4 matrix).

Next, we calculate a rank 3 approximation.
We take the first three singular values, first three columns of \li{U}, and first three rows of \li{Vh}.
We omit the last singular value from the calculation along with the last column of \li{U} and last row of \li{Vh}.

\begin{lstlisting}
>>> S = np.diag(s[:3])
>>> Ahat = U[:,:3].dot(S).dot(Vh[:3,:])
>>> la.norm(A-Ahat)
\end{lstlisting}
Note that $\widehat A$ is ``close'' to the original matrix $A$, but that its rank is 3 instead of 4.

\begin{problem}
Write a function \li{svd_approx} that takes as input a matrix $A$ and a positive integer $k$ and returns
the best rank $k$ approximation to $A$ (with respect to the induced 2-norm and Frobenius norm).
Use \li{scipy.linalg.svd}.
\label{prob:svd_approx}
\end{problem}

\subsection*{Error of Low-Rank Approximations}

Recall that the error between the best rank $s$ approximation $\widehat{A_s}$ of $A$ with respect to the induced 2-norm is given by
$$
\|A - \widehat{A_s}\|_2 = \sigma_{s+1},
$$
where $\sigma_{s+1}$ is the $(s+1)$-th singular value of $A$.
(See the proof of the Schmidt-Eckard-Young-Mirsky theorem in the text).

This offers a way to approximate a matrix within an error tolerance:
choose the truncated SVD approximation such that the largest discarded singular value is less than the error tolerance.

\begin{problem}
Using \li{scipy.linalg.svd}, write a function \li{lowest_rank_approx} that takes as input a matrix $A$ and a positive number $e$ and returns
the lowest rank approximation of $A$ with error less than $e$ (with respect to the induced 2-norm).
You should only calculate the SVD once.
\end{problem}

\begin{comment}
The reduced form of the SVD also provides a way to approximate a matrix with another one of lower rank.
This idea is used in many areas of applied mathematics including signal processing, statistics, semantic indexing (search engines), and control theory.
If we are given a matrix $A$ of rank $r$, we can find an approximate matrix $\widehat A$ of rank $s<r$ by taking the SVD of $A$ and setting all of its singular values after $\sigma_s$ to zero, that is,
\begin{equation*}
\Sigma_{s} = \mbox{diag}(\sigma_1,\sigma_2,\ldots,\sigma_s,0,\ldots,0)
\end{equation*}
and then multiplying the matrix back together again.
The more singular values we keep, the closer our approximation is to $A$.
The number of singular values we decide to preserve depends on how close of an approximation we need and what our size requirements are for $U_1$, $\Sigma_{\widehat A}$, and $V_1$.
Try plotting the singular values.
\end{comment}

\subsection*{Application to Image Compression}

Sometimes there is not enough available bandwidth to transmit a full resolution photograph.
Suppose you need to transmit an image from a remote location.
You might aim to reduce the amount of data being sent, while also minimizing the loss of detail in the image.

This can be done using the SVD.
An image is just a matrix of pixel values, which means it has a singular value decomposition.
Computing and sending a low-rank SVD approximation of the image can considerably reduce the amount of data sent, while retaining a high level of image detail.
Additionally, successive levels of detail can be sent after the inital low-rank approximation by sending additional singular values and their corresponding columns of V and U.

Examining the singular values of an image gives us an idea of how low-rank the approximation can be.
Figure \ref{fig:hubble} presents an image and a log plot of its singular values from greatest to least.
The plot in \ref{fig:svals_plot} is typical for a photograph---the singular values start out large but drop off rapidly.
In this rank $670$ image, $624$ of the singular values are $50$ or more times smaller than the largest singular value.
By discarding these relatively small singular values, we can retain all but the finest image details, while storing only a rank 46 image!
This is a huge reduction in data size.

Figure \ref{fig:rankvalues} shows several low-rank approximations of the image in Figure \ref{fig:hubble_original}.
Even at a low rank the image is recognizable.
By rank 40, the approximation visibly differs very little from the original.

\begin{figure}
\centering
\begin{subfigure}[b]{.49\textwidth}
\centering
\includegraphics[width=\textwidth*5/6]{hubble_red}
\caption{NGC 3603 (Hubble Space Telescope).}
\label{fig:hubble_original}
\end{subfigure}
\begin{subfigure}[b]{.49\textwidth}
\centering
\includegraphics[width=\textwidth]{hubble_svals}
\caption{Singular values from greatest to smallest on a log scale}
\label{fig:svals_plot}
\end{subfigure}
\caption{An image and its singular values.}
\label{fig:hubble}
\end{figure}

\begin{figure}
\centering
\begin{subfigure}[b]{.35\textwidth}
\centering
\includegraphics[width=\textwidth]{rank1.jpg}
\caption{Rank 1}
\end{subfigure}
\begin{subfigure}[b]{.35\textwidth}
\centering
\includegraphics[width=\textwidth]{rank14.jpg}
\caption{Rank 14}
\end{subfigure}

\begin{subfigure}[b]{.35\textwidth}
\centering
\includegraphics[width=\textwidth]{rank27.jpg}
\caption{Rank 27}
\end{subfigure}
\begin{subfigure}[b]{.35\textwidth}
\centering
\includegraphics[width=\textwidth]{rank40.jpg}
\caption{Rank 40}
\end{subfigure}
\caption{Different rank approximations for SVD-based compression.  Notice that higher rank is needed to resolve finer detail.}
\label{fig:rankvalues}
\end{figure}

The following code demonstrates how to use \li{plt.imread} and \li{plt.imshow} to read in an image, convert it to black and white, and show it.
The function from Problem \ref{prob:svd_approx} can then be used to calculate an approximation of \li{X}.
%Enter the following into IPython (note that any image you might have will work):
\begin{lstlisting}
>>> import matplotlib.pyplot as plt
>>> # Take only one layer (layer 0) of the image
>>> X = plt.imread('hubble_image.jpg')[:,:,0].astype(float)
% >>> X.nbytes     #number of bytes needed to store X  <---- this seems superfluous
>>> plt.imshow(X, cmap="gray")
>>> plt.show()
\end{lstlisting}
\begin{comment}
Computing the SVD of your image is simple.
Remember to make the singular values a diagonal matrix before multiplying.
\begin{lstlisting}
>>> U,s,Vt = svd(X, full_matrices=False)
>>> S = sp.diag(s)
\end{lstlisting}
In the next code block, $k$ represents the desired rank of the output.
\begin{lstlisting}
>>> k = 50
>>> u1, s1, vt1 = U[:,0:n], S[0:n,0:n], Vt[0:n,:]
>>> Xhat = u1.dot(s1).dot(vt1)
>>> (u1.nbytes + np.diag(s1).nbytes + vt1.nbytes) - X.nbytes   #should be negative
>>> plt.imshow(Xhat)
>>> plt.show()
\end{lstlisting}
\end{comment}

\begin{problem}
Using the \li{svd_approx} function from Problem \ref{prob:svd_approx}, write a function \li{compress_img} that accepts two parameters \li{filename} and \li{k}. The function should plot the original image and the best rank k approximation of the original image.

While \li{svd_approx} worked for grayscale images, the \li{compress_img} function should work on color images.
You may split the image into its three RGB layers and approximate each layer separately, then recombine them.
Test your function on \li{hubble_image.jpg}
Your output should be similar to Figure \ref{fig:compressed_image}.

Hints:
\begin{itemize}
\item Sometimes \li{plt.imshow} does not behave as expected when being passed RGB values between 0 and 255. It behaves much better when being passed values between 0 and 1.
\item Since the SVD provides an approximation, it is possible that the SVD will generate values slightly outside the valid range of RGB values.
To fix this, use fancy indexing (as discussed in the NumPy and SciPy lab) to set values greater than 1 to 1 and values less than 0 to 0.
\end{itemize}

\begin{figure}[H]
\includegraphics[width=\textwidth]{compressed.jpg}
\caption{Correct output for the best rank 20 approximation.}
\label{fig:compressed_image}
\end{figure}
\end{problem}


\section{Natural Numbers}
Now that we can write code in Agda, our next goal to understand why we
want to write code in Agda, i. e. we want to write code that can’t be
written in other languages. Not even in Haskell—well not yet, or at
least not in an easy way.

I mean of course the best feature a programming language can have
\emph{Dependent Types}. The standard example for Dependent Types is the
fix length vector. Don’t worry, if you don’t know what I mean by that
you will soon.

Before we can describe fix length vectors we have to be able to describe
length. What better way to describe length but with natural numbers.

\begin{code}
module Naturals where

data ℕ : Set where
  zero : ℕ
  suc  : ℕ → ℕ
\end{code}

What's that? This is an algebraic data type similar to those we wrote and use
in Haskell all day long, or at least we would if we could use Haskell all day
long at work.

\begin{haskell}
data Nat = Zero | Suc Nat
\end{haskell}

If we write the same with Haskells GADTs extension we see the resemblance:

\begin{haskell}
{-# LANGUAGE GADTs #-}

data Nat where
  Zero  :: Nat
  Suc   :: Nat -> Nat
\end{haskell}

So far so good, but what about \verb+Set Whe state that \verb+Bool+ is of type
\verb+Set+, which is the type of small types. In Haskell we would say of kind `*`.
\verb+Set+ itself is of type \verb+Set1+ which is of type \verb+Set2+ and so on. In Haskell
we would say nothing like this.

\begin{haskell}
{-# LANGUAGE GADTs          #-}
{-# LANGUAGE KindSignatures #-}

data Bool :: * where
  Zero  :: Nat
  Suc   :: Nat -> Nat
\end{haskell}

In other word being a Type in Haskell means being a Set in Agda.

That's all we need, to describe every natural number possible.
Quite cool when you think about it. No little-endian, no big-endian.

Let's define some natural numbers:
\begin{code}
null : ℕ
null = zero

three : ℕ
three = suc (suc (suc zero))
\end{code}

\begin{exercise}
  Define \verb+fivethousand+.
\end{exercise}

GOTCHA!!!

We can tell Agda to use Haskell builtin Integers as \verb+ℕ+.

\begin{code}
{-# BUILTIN NATURAL ℕ #-}

fivethousand : ℕ
fivethousand = 5000000
\end{code}

Let's define additions.
\begin{code}
infixl 6 _+_
_+_ : ℕ → ℕ → ℕ
\end{code}

Using \verb+_+ in function names we define pre/in/mix/post-fix operators. The
$n$-th arguments takes place of the $n$-th \verb+_+.
\begin{code}
zero + m = m
suc n + m = suc (n + m)
\end{code}

Since we are all good programmers we want to test this function.
We need some imports.
\begin{code}
import Relation.Binary.PropositionalEquality as Eq
open Eq using (_≡_; refl; cong; sym)
open Eq.≡-Reasoning using (begin_; _≡⟨⟩_; _≡⟨_⟩_; _∎)
\end{code}

With this we can write a unit test for \verb=_+_=.
\begin{code}
_ : 2 + 3 ≡ 5
_ = refl
\end{code}
For now (this little moment in time) you can think of \verb+refl+ as
of an assertion.

We can write the test in a more human readable form.
\begin{code}
_ : 2 + 3 ≡ 5
_ =
  begin
    2 + 3
  ≡⟨⟩
    suc (1 + 3)
  ≡⟨⟩
    suc (suc (0 + 3))
  ≡⟨⟩
    suc (suc 3)
  ≡⟨⟩
    5
  ∎
\end{code}

This is a lot to take in. First we notice that \verb=2 + 3 ≡ 5= is a type signature.
And \verb+_≡_+ is a type constructor.
\begin{verbatim}
infix 4 _≡_
data _≡_ {a} {A : Set a} (x : A) : A → Set a where
  instance refl : x ≡ x
\end{verbatim}
\verb+x ≡ x+ is the type of elements which are equal to itself, hence \emph{reflexive}.

So
\begin{verbatim}
_ : 2 + 3 ≡ 5
\end{verbatim}
defines a variable \verb+_+ containing the information that $2+3$ is equal to $5$.
The variable name \verb+_+ states, that we don't care about the variable itself.

\begin{verbatim}
_ = refl
\end{verbatim}
tells Agda to construct an inhabitant of the type \verb=2 + 3 ≡ 5=, if it can
we know they are equal.

\begin{exercise}
Write a test for $3+4$.
\end{exercise}

Unlike humans, Agda does not need every step. In fact everything Agda
does is to check, that every expression between \verb+≡⟨⟩+ reduces to the
same expression. Since Agda can reduce the expressions without further
information we could just use \verb+refl+.

\subsection{Proofs}
With that in mind we could say that we did not just write a test $2+3$ but a proof.
The cool thing is, that Agda verifies the proof at compile time, so no need to
actually “run the test”.

We just experienced the phenomenon of “Proposition as Types” — whooooo

If course this proof is as useful as any other unit test … not at all.
We test addition for 2 natural numbers this is a fraction of 0\,\%.

We could use QuickCheck to run property test. But again 0\,\% coverage.

In Agda we can do better but first we need some properties for addition to fulfill:
\begin{itemize}
  \item $0$ is the neutral element
  \item assosiative
  \item commutative
\end{itemize}

First we show that \verb+zero+ is the neutral element. We already
know that \verb+zero+ is neutral rom the left by definition.
\begin{code}
+-rightNeutral : ∀ (m : ℕ) → m + zero ≡ m
+-rightNeutral zero =
  begin
    zero + zero
  ≡⟨⟩
    zero
  ∎
+-rightNeutral (suc m) =
  begin
    suc m + zero
  ≡⟨⟩
    suc (m + zero)
  ≡⟨ cong  suc (+-rightNeutral m) ⟩ -- congruent
    suc m
  ∎
\end{code}
Agda makes it explicit that proof by induction is the same a programming
with recursion.

\begin{exercise}
We defined \verb=_+_= left inductive. Proof that it \verb=_+_= is to right inductive, too.
I. e. proof

\begin{code}
+-suc : ∀ (m n : ℕ) → m + suc n ≡ suc (m + n)
\end{code}
\end{exercise}

\begin{code}
+-suc zero n =
  begin
    zero + suc n
  ≡⟨⟩
    suc n
  ≡⟨⟩
    suc (zero + n)
  ∎
+-suc (suc m) n =
  begin
    suc m + suc n
  ≡⟨⟩
    suc (m + suc n)
  ≡⟨ cong suc (+-suc m n) ⟩
    suc (suc (m + n))
  ≡⟨⟩
    suc (suc m + n)
  ∎
\end{code}

Whit that lemma we can proof commutativity.
\begin{code}
+-commutative : ∀ (n m : ℕ) → n + m ≡ m + n
+-commutative zero m =
  begin
    zero + m
  ≡⟨⟩
    m
  ≡⟨ sym (+-rightNeutral m) ⟩
    m + zero
  ∎
+-commutative (suc n) m =
  begin
    suc n + m
  ≡⟨⟩
    suc (n + m)
  ≡⟨ cong suc (sym (+-commutative m n)) ⟩
    suc (m + n)
  ≡⟨ sym (+-suc m n) ⟩
    m + suc n
  ∎
\end{code}

\begin{exercise}
Show that $+$ is assosiative, i.e.

\begin{code}
+-associativ : ∀ (l : ℕ) (m : ℕ) (n : ℕ)
  → (l + m) + n ≡ l + (m + n)
\end{code}
\end{exercise}

\begin{code}
+-associativ zero m n =
  begin
    (zero + m) + n
  ≡⟨⟩
    m + n
  ≡⟨⟩
    zero + (m + n)
  ∎
+-associativ (suc l) m n =
  begin
    ((suc l) + m) + n
  ≡⟨⟩
    (suc (l + m)) + n
  ≡⟨⟩
    suc ((l + m) + n)
  ≡⟨ cong suc (+-associativ l m n) ⟩ -- congruent
    suc (l + (m + n))
  ≡⟨⟩
   suc l + (m + n)
  ∎
\end{code}

Now we have tests for addition with 100\,\% coverage over the input domain,
also called \emph{proofs}.

Maybe we should stop here.

\begin{code}
infixl 7 _*_
_*_ : ℕ → ℕ → ℕ
zero * _ = zero
suc m * n = n + (m * n)
\end{code}

\begin{exercise}
Proof that $*$ distributive on $+$, .i.e.

\begin{code}
*-distributive : ∀ (l : ℕ) (m : ℕ) (n : ℕ)
  → l * (m + n) ≡ l * m + l * n
\end{code}
\end{exercise}

\begin{code}
*-distributive zero m n =
  begin
    zero * (m + n)
  ≡⟨⟩
    zero
  ≡⟨⟩
    zero * zero
  ≡⟨⟩
    zero * m + zero * n
  ∎
*-distributive (suc l) m n =
  begin
    (suc l) * (m + n)
  ≡⟨⟩
    (m + n) + (l * (m + n))
  ≡⟨ cong ((m + n) +_) (*-distributive l m n) ⟩
    (m + n) + (l * m + l * n)
  ≡⟨ +-associativ m n (l * m + l * n) ⟩
    m + (n + (l * m + l * n))
  ≡⟨ cong (m +_) (sym (+-associativ n (l * m) (l * n))) ⟩
    m + ((n + l * m) + l * n)
  ≡⟨ cong (m +_) (cong (_+ l * n) (+-commutative n (l * m))) ⟩
    m + ((l * m + n) + l * n)
  ≡⟨ cong (m +_) (+-associativ (l * m) n (l * n)) ⟩
    m + (l * m + (n + l * n))
  ≡⟨ sym (+-associativ  m (l * m) (n + l * n)) ⟩
    (m + l * m) + (n + l * n)
  ≡⟨⟩
    suc l * m + suc l * n
  ∎
\end{code}


\chapter{Signal Processing}
\section*{Introduction}
\newpage
\cex
\inputminted[linenos=true,resetmargins=true]{c}{./c_examples/example18.c}
\newpage
\section*{Fourier Transforms}\addcontentsline{toc}{section}{Fourier Transforms}
\subsection*{Vector FFT}\addcontentsline{toc}{subsection}{Vector FFT}
\subsection*(Vector FFT by Row or Column}\addcontentsline{toc}{subsection}{Vector FFT by Row or Column}
\section*{Convolution, Correlation and FIR Filtering}\addcontentsline{toc}{section}{Convolution, Correlation and FIR Filtering}
\section*{Window Creation}\addcontentsline{toc}{section}{Window Creation}
VSIPL provides functions to create Blackman, Chebyshev, Hanning and Kaiser windows.
Unlike most functions in C VSIPL the window creation routines do not use an already
created vector and fill it. Instead they actually create a block, allocate data for the
block, create a unit stride full length vector on the block, fill the vector with the window
coefficients, and then return the pointer to the vector view. The return value will be
\ilCode{NULL} on an allocation failure.

For pyJvsip the windows are defined as a method on a view so the functionality, from a
user perspective, is to create a vector of a certain type and length and then fill the
vector with a window.  Size information are taken from the calling view. Under the covers
the C VSIPL window functions are used so a copy is actually taking place to meet the
requirements of pyJvsip.}

\section*{Miscellaneous}\addcontentsline{toc}{section}{Miscellaneous}
\subsection*{Histogram}\addcontentsline{toc}{subsection}{Histogram}
\subsection*{Data Reorganization}\addcontentsline{toc}{subsection}{Data Reorganization}
\subsubsection*{Frequency Swapping}\addcontentsline{toc}{subsubsection}{Frequency Swapping}


\documentclass[11pt]{amsbook}

\usepackage{../HBSuerDemir}	% ------------------------

\begin{document}

% ++++++++++++++++++++++++++++++++++++++
\hPage{b2p1/122}
% ++++++++++++++++++++++++++++++++++++++

% =======================================
    \[ \left| C \right| = \sqrt[]{(\frac{1}{9})^2 + (\frac{4}{9})^2 + (\frac{8}{9})^2 } = \frac{1}{9} \sqrt[]{1 + 16 + 64} = 1 \]

    \\ A vector having lenth equal to 1 is called a \underline{unit vector}. In the above example, C is seen to be a unit vector.

% =======================================

    \subsection*{\underline{B. ALGEBRA OF VECTORS}}~

    \\1. \underline{Addition}:
    \\ The sum \(\vec{a} + \vec{b}\) of two vectors \(\vec{a}\) and \(\vec{b}\), in this order, is the vector whose initial point is that of the first and the extremity is that of the second when the latter is translated to have its initial point at the extremity of the first vector:
    \\\includegraphics[scale = 0.7]{images/b2p1-122_fig1} \includegraphics[scale=0.7]{images/b2p1-122_fig2}

    \\ The following figures are the commutative law
     \[ \vec{a} + \vec{b} =  \vec{b} + \vec{a}\]
    \\ and the parallelogram law:
    \\\includegraphics[scale=0.7]{images/b2p1-122_fig3}
    \includegraphics[scale = 0.7]{images/b2p1-122_fig4}

    \\ The associative law
     \[ (\vec{a} + \vec{b}) + \vec{c} =  \vec{a} + (\vec{b} + \vec{c})\]
     \\ is the result congruency of the following pyramids:
      \\\includegraphics[scale=0.7]{images/b2p1-122_fig5}
      \includegraphics[scale = 0.7]{images/b2p1-122_fig6}

      \\ Because of this law the sum \( \vec{a} + \vec{b} + \vec{c} \) has a meaning as defined by \( (\vec{a} + \vec{b}) + \vec{c} \) or by \( \vec{a} + (\vec{b} + \vec{c}) \)

% =======================================

% =======================================================
\end{document}

%==== templates ====

%==== environments ====

%\begin{figure}[htb]
%	\centering
%	\includegraphics[width=0.9\textwidth]{images/SD-1-1p15A}
%	\caption{Classification of complex numbers}
%	\label{fig:classificationOfComplexNumbersA}
%\end{figure}

%\begin{center}
%\begin{tabular}{cc}
%\end{tabular}
%\end{center}

%\begin{exmp}
%\begin{hSolution}
%\end{hSolution}
%\end{exmp}

%\begin{hEnumerateAlpha}
%\end{hEnumerateAlpha}

%\begin{hEnumerateRoman}
%\end{hEnumerateRoman}

%$
%\begin{bmatrix}
%\end{bmatrix}
%$

%\frac{aaaa}{bbb}
%\frac{a_{n}}{b_{n}}
%\left( aaaa \right)
%\Longrightarrow

%\begin{multicols}{2}
%	bb
%\columnbreak
%	aa
%\end{multicols}


\subsection{Inducing a topology}

Metric spaces can be used to induce a topology.


\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{geometry}
\usepackage[utf8]{inputenc}
\usepackage{enumitem}
\usepackage{physics}
\setlength{\parindent}{0pt}
\setlength{\parskip}{1em}
\relpenalty=10000
\binoppenalty=10000

\begin{document}

\section*{Jensen's inequality}
Given positive real numbers $\lambda_1,\hdots,\lambda_n$ for which $\lambda_1+\hdots+\lambda_n=1$  and a convex function $f(x)$ the following holds:
$$f(\lambda_1 x_1 + \hdots + \lambda_n x_n) \leq \lambda_1 f(x_1) + \hdots + \lambda_n f(x_n)$$
Similarly, when $f(x)$ is a concave function, then
$$f(\lambda_1 x_1 + \hdots + \lambda_n x_n) \geq \lambda_1 f(x_1) + \hdots + \lambda_n f(x_n)$$

\subsection*{Problems}
\begin{enumerate}
	\item
	Prove Jensen's inequality. When does the equality hold?

	\item
	Prove the power mean inequality using Jensen's inequality.

	\item % Vorratused 96
	Prove for positive real numbers $a,b,c$
	$$\frac{9}{2(a+b+c)} \leq \frac{1}{a+b} + \frac{1}{b+c} + \frac{1}{c+a} \leq \frac{1}{2} \left( \frac{1}{a} + \frac{1}{b} + \frac{1}{c} \right) $$

	\item % vorrat 118
	$\alpha,\beta,\gamma$ are angles of an acute triangle. Prove that
	$$\cos \alpha + \cos \beta + \cos \gamma \leq \frac{3}{2}$$

	\item % vorrat p68
	$a_1,\hdots,a_n$ are positive real numbers and $b_1, \hdots, b_n$ their permutation. Prove that
	$$\left( a_1 + \frac{1}{b_1} \right)
	\left( a_2 + \frac{1}{b_2} \right)
	\hdots
	\left( a_n + \frac{1}{b_n} \right)
	\geq 2^n
	$$
	Prove that for odd $n$, the equality holds if there exists $i$ for which $a_i=1$.

	\item % BW2002-4
	Let $n$ be a positive  integer. Prove that
	\[
	\sum_{i=1}^{n}x_i(1-x_i)^2 \leq \left(1-\frac{1}{n}\right)^2
	\]
	for all nonnegative real numbers $x_1,x_2,\ldots,x_n$ such that $x_1+x_2+\cdots+x_n=1$.

	\item % http://www.math.olympiaadid.ut.ee/eng/archive/prob1213.pdf S-6
	A class consists of 7 boys and 13 girls. During the first three months of the school year, each boy has communicated with each girl at least once. Prove that there exist two boys and two girls such that both boys communicated with both girls first time in the same month.

\end{enumerate}
\end{document}


\subsection*{Q. 5}
\paragraph{(a)}$F(A, B, C, D)=\Sigma(1, 3, 5, 8, 10, 14)$
\par D1, D3, D5, D8, D10, D14 should be connected to \emph{signal 1}, while the left ports (D0, D2, D4, D6, D7, D9, D11, D12, D13, D15) should be connected to \emph{signal 0}.
\paragraph{(b)}$F(A, B, C, D)=\Pi(4, 7, 11)=\Sigma(0, 1, 2, 3, 5, 6, 8, 9, 10, 12, 13, 14, 15)$
\par D4, D7, D11 should be connected to \emph{signal 0}, while the left ports (D0, D1, D2, D3, D5, D6, D8, D9, D10, D12, D13, D14, D15) should be connected to \emph{signal 1}.


%!TEX root = TDT4265-Summary.tex
\section{Image segmentation}\label{sec:segmentation}
Segmentation divides an image into the regions or objects it consists of. Most algorithms here are based on intensity discontinuity and similarity.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Fundamentals}\label{ssec:segmentation-fundamentals}
We pretty much just divide an image $R$ into $n$ regions $R_1 \dots R_n$, and require the following to hold:
\begin{itemize}
    \item The union of all regions is equal to the whole image.
    \item Each region is a connected set.
    \item Each region is disjoint from the others.
    \item Some predicate\footnote{A predicate can be e.g. the that the intensity of each pixel is in a certain range.} is true for each region separately.
    \item The predicate is false for the union of any adjacent regions.
\end{itemize}

Segmentation is either edge-based or region-based.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Point, line, and edge detection}\label{ssec:edge-detection}
These methods detect sharp, local changes in intensity. First- and second-order derivatives are used, and can detect intensity ramps, isolated points, and steps. Simple spatial filters can realize 1st and 2nd derivatives.

\subsubsection{Point detection}
Points can be detected by a 2nd derivative mask and thresholding. (If the absolute value of the output of a 2nd derivative mask is above a threshold, that pixel is marked as a point.) A suitable mask for this is
\begin{equation}\label{eq:2nd-derivative-mask}
    \begin{BMAT}(e){|c|c|c|}{|c|c|c|}
        1 &  1 & 1 \\
        1 & -8 & 1 \\
        1 &  1 & 1
    \end{BMAT}.
\end{equation}

\subsubsection{Line detection}
The same mask \eqref{eq:2nd-derivative-mask} can be used. However, this mask gives a double line effect---negative values on one side of the line and positive on the other. This can be avoided by only keeping the positive-valued pixels. Note that lines must be thin compared to the mask in order to be detected as lines. Fat lines should be treated as regions.

\eqref{eq:2nd-derivative-mask} is independent of line direction. Other masks can be used to detect lines in specified directions.

\subsubsection{Edge models}
Edges can be steps (sudden change), ramps (gradual change), or roofs (gradual increase followed by gradual decrease).

The magnitude of the 1st derivative can indicate an edge, and the sign of the 2nd derivative reveals if you are on the bright or dark side of it. Because this is done with derivatives, noisy edges are problematic.

\subsubsection{Basic edge detection}\label{sssec:edge-detection}
The image gradient is
\begin{equation}
    \nabla f = \mathrm{grad}(f)
    =
    \begin{bmatrix} g_x \\ g_y \end{bmatrix}
    =
    \begin{bmatrix} \pd{f}{x} \\[6pt] \pd{f}{y} \end{bmatrix}.
\end{equation}
The magnitude is the value of the rate of change in the direction of the gradient (steepness of the ramp). The direction of $\nabla f$ is
\begin{equation}
    \alpha(x,y) = \arctan \left( \frac{g_y}{g_x} \right).
\end{equation}
This is the direction of the steepest ascent, and is therefore orthogonal to the edge.

A often used and pretty good gradient mask is the Sobel operator
\begin{equation}\label{eq:sobel}
    \begin{BMAT}(e){|c|c|c|}{|c|c|c|}
        -1 & -2 & -1 \\
         0 &  0 &  0 \\
         1 &  2 & 1
    \end{BMAT}
    \text{ and }
    \begin{BMAT}(e){|c|c|c|}{|c|c|c|}
        -1 & 0 & 1 \\
        -2 & 0 & 2 \\
        -1 & 0 & 1
    \end{BMAT}.
\end{equation}
These two masks can detect vertical and horizontal edges, respectively. The same operator but with ones instead of twos is called the Prewitt operator. However, the $\pm 2$-elements give some smoothing, which is good as derivatives are sensitive to noise. Sobel and Prewitt masks can also be rotated $45 \degree$ to detect diagonal edges.

To maintain connectivity of edges and highlight the strongest edges, you must smoothe the image first, then extract edges, then threshold it.

\subsubsection{More advanced edge detection}\label{sssec:adv-edge-detection}
These methods worry more about noise and the nature of edges.

\paragraph{The Marr-Hildreth edge detector (Laplacian of Gaussian)} This method uses masks of variant scales to detect edges of variant sharpness. The filter is the Laplacian of the Gaussian (LoG):
\begin{equation}
    \nabla^2 G(x,y)
    =
    \left[
        \frac{x^2 + y^2 - 2 \sigma^2}{\sigma^4}
    \right]
    \euler^{-\frac{x^2 + y^2}{2 \sigma^2}}
\end{equation}
This formula can be used to create LoG masks of any size. The Gaussian part $G$ blurs the image without ringing. The Laplacian part $\nabla^2$ is used to detect edges because it is isotropic\footnote{Invariant to rotation.}, as opposed to the 1st derivative. In reality, Marr-Hildreth edge detection is done by
\begin{enumerate}
    \item Filter image with a Gaussian lowpass filter.
    \item Compute the Laplacian of the image.
    \item Find the zero-crossings.
\end{enumerate}

Note that it is usually better to use a small positive threshold for zero-crossing. This removes a lot of ``noisy'' edges. Also note that the LoG can be approximated with a difference of Gaussian (DoG) operator.

\paragraph{The Canny edge detector} The Canny edge detector is an attempt to
\begin{enumerate}
    \item find all edges without false positives,
    \item locate edge points near the true edge, and
    \item return edges only one pixel wide.
\end{enumerate}
This is fairly well achieved by the following:
\begin{enumerate}
    \item Smooth the image with a Gaussian filter.
    \item Compute gradient magnitude and gradient angle images (using e.g. Sobel).
    \item Apply nonmaxima suppression to the gradient magnitude image.
    \item Detect and link edges with double thresholding and connectivity analysis.
    \item (Optional.) Use edge thinning to make sure all edges are one pixel wide.
\end{enumerate}

Canny is generally better than Marr-Hildreth/LoG, but more complex and computationally heavy.

\paragraph{Edge linking and boundary detection} Done with local processing, regional processing, or global processing with the Hough transform.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Corner detection}
The dominant method for corner detection is the Harris corner detector.

\subsubsection{Harris corner detector}
Define a box of some size to move around the image and look for corners inside of. For each location, calculate gradients in $x$ and $y$ directions. In a flat area, the gradients will all be close to zero. Edges and corners give different non-zero responses. This can be quantified by
\begin{equation}
    M = \sum_{(x,y) \in W}
    \begin{bmatrix}
        I_x^2   & I_x I_y \\
        I_x I_y & I_y^2
    \end{bmatrix}
\end{equation}
where $I_x$ and $I_y$ are gradients in the $x$ and $y$ directions. $W$ is the window we are looking for corners in. We can then use the Harris corner response function
\begin{equation}
    R = \det(M) - \alpha \trace(M)^2 = \lambda_1 \lambda_2 - \alpha (\lambda_1 + \lambda_2)^2
\end{equation}
to assume features:
\begin{center}
\begin{tabular}{lll}
    Response function & Eigenvalues & Feature found \\
    \hline
    $R > 0$           & Both large  & Corner        \\
    $R < 0$           & One large   & Edge          \\
    $\abs{R}$ small   & Both small  & Flat area
\end{tabular}
\end{center}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Thresholding}
Thresholding is an attempt to separate the image into object points and background points. It can be done globally (same threshold value everywhere) or variably (different value in different areas). Sometimes multiple thresholding is done, to separate the image into more than two parts.

Thresholding is made harder by noise, and uneven illumination/reflectance. Remedies are direct correction of shading pattern, top-hat transformation (Paragraph \ref{par:top-hat}), and variable thresholding.

\subsubsection{Basic global thresholding}
Easy, but we need a way to select the threshold automatically:
\begin{enumerate}
    \item Select initial threshold $T$.
    \item Segment image with $T$.
    \item Compute mean intensities $m_1$ and $m_2$ of the two groups.
    \item Compute new threshold $T = \frac{1}{2}(m_1 + m_2)$.
    \item Repeat until convergence.
\end{enumerate}
This works well when there is a clear valley in the histogram.

\subsubsection{Otsu's method}
Otsu's method is based on maximizing between-class variance:
\begin{enumerate}
    \item Compute normalized histogram $p_0 \dots p_{L-1}$.
    \item Compute cumulative sums $P_1(0) \dots P_1(L-1)$.
    \item Compute cumulative means $m(0) \dots m(L-1)$.
    \item Compute global intensity mean $m_G$.
    \item Compute between-class variance $\sigma_B^2(0) \dots \sigma_B^2(L-1)$.
    \item Obtain Otsu threshold $k^* = \argmax_k \sigma_B^2(k)$.
    \item Obtain separability measure $\eta^*$.
\end{enumerate}

\subsubsection{Improving global thresholding with smoothing}
Even with Otsu's, noise can make thresholding fail completely. Smoothing the image first can often make it succeed again, although heavy smoothing can distort the edges.

\subsubsection{Improving global thresholding with edges}
In some cases, such as with noisy images where the object is very small compared to the background, edge detection-based thresholding is useful.
\begin{enumerate}
    \item Compute an edge image with some method from Section \ref{ssec:edge-detection}.
    \item Set a threshold $T$.
    \item Threshold the edge image.
    \item Compute a histogram of only the pixels in the original that are 1-valued in the edge image.
    \item Segment this histogram globally, with e.g. Otsu.
\end{enumerate}

\subsubsection{Multiple thresholds}
Sometimes you want to segment an image into three groups of intensity values, which is done with two thresholds. (More than two thresholds usually consider other properties than intensity.) A modification of Otsu exists for dual thresholds.

\subsubsection{Variable thresholding}

\paragraph{Image partitioning} Partition the image into smaller images, and threshold each part separately. Sufficiently small parts should make, say, uneven illumination appear fairly even within the part.

\paragraph{Thresholding based on local properties} Threshold based on a predicate of the neighborhood. If a function $Q$ of pixels near the current is true, mark that pixel a 1.

\paragraph{Moving average} Good for stuff such as images of text with uneven illumination. As long as the text stands out from the local background, it's marked a 1. Global thresholding will fail in such situations.

\subsubsection{Multivariable thresholding}
With color images, we might want to do thresholding based on more variables than greyscale intensity. An example is the ``distance'' from a pixel in the image to a given color, such as only keeping pixels that are sufficiently red.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Region-based segmentation}
Instead of finding edges or doing thresholding, let's just find the regions directly.

\subsubsection{Region growing}
Start with a ``seed'' of points, and grow these into regions:
\begin{enumerate}
    \item Erode connected components of the seeds to single pixels.
    \item Compute a predicate image with ones for all pixels that satisfy the predicate.
    \item Append to the seed points all ones from the predicate image that are 8-connected to the seed.
    \item Label each disjoint region.
\end{enumerate}

\subsubsection{Region splitting and merging}
Based on splitting the image into sections and merging them to satisfy the list in Section \ref{ssec:segmentation-fundamentals}. One method is to divide the image into quadrants, and for each quadrant that fails a predicate, divide it further. Afterwards, adjacent regions that fulfill the predicate are merged.

\subsubsection{$k$-means clustering}
$k$-means clustering is a method that can be used to segment an image into $k$ categories based on some vector of quantitative features (such as color levels). The algorithm is
\begin{enumerate}
    \item In the space of all possible feature vectors, initialize the centers of $k$ categories randomly.
    \item Assign each pixel to the nearest category.
    \item Based on the pixels assigned to each category, compute the centroid of their feature vectors as the new category centroids.
    \item Repeat until convergence.
\end{enumerate}
This method

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Morphological watersheds}
We look at the image in a topographic way, letting intensity levels represent heights. Then ``fill'' the image by rising water from below. Notice which areas fill separately, and where the water connects when the level is high enough.

As the water fills, build pixel-wide dams to keep the basins from connecting. These dams define the segmentation boundaries between regions. Noise and irregularities lead to oversegmentation. You can preprocess to generate markers, which are the only allowable starting points for the water filling, in order to reduce this.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Using motion for segmentation}
It can be useful to detect regions by looking at movement.

\subsubsection{Spatial techniques}
A very simple method is to look at pixel-level differences: Make a difference image of ones for all pixels that have changed significantly. To suppress noise, you can form 4- or 8-connected regions in the difference image and ignore too small regions.

Another method is accumulative differences: Increment a counter for a given pixel when a significant change occurs there.

For these methods to work, we need a good reference image. The best is one with only stationary elements. If you cannot capture a stationary image directly, it can be constructed from several images.


\subsection{Cook's distance}

Cook's distance measures the effect of deleting outliers.
work out predictions if outlier was removed, sum all differneces in y hat

Outliers have a high Cook's distance.


\documentclass{article}

\usepackage{style/preamble}
\usepackage{style/mytikz}
\usepackage{parskip}

\newcounter{dip}

\begin{document}
  \title{Problem Set 3 - Riemann--Hurwitz}
  \date{}
  \maketitle

For today, let $X$ and $Y$ be compact connected Riemann surfaces with atlases $(U_i, \varphi_i)$ and $(V_j,\psi_j)$, respectively, and let $f: X \rightarrow Y$ be a non-constant complex differentiable function between them.

\section{Ramified coverings of Riemann surfaces}
\begin{definition}
  Pick a chart $\varphi_i$ around a point $z \in X$ and a chart $\psi_j$ around $f(z) \in Y$.
  Define
  \begin{align*}
    \mathrm{index}_f (z) := \mathrm{index}_{\psi_j \circ f \circ \varphi_i^{-1}}( \varphi(z))
  \end{align*}
\end{definition}

\begin{qbox}
  Let $g(z)$ be a non-constant holomorphic function on $U \subseteq \bbc$ with Taylor expansion
  \begin{align*}
    g(z) - g(z_0) = a_k (z - z_0)^k + a_{k+1} (z-z_0)^{k+1} + \dots
  \end{align*}
  Let $\psi$ be a biholomorphic function with Taylor expansion
  \begin{align*}
    \psi(w) - \psi(w_0) = a_1 (z - w_0) + a_2 (z - w_0)^2 + \dots
  \end{align*}
  where $w_0 = g(z_0)$.
  Find the first term in the Taylor expansions of $\psi \circ g $ at $z_0$ and argue that $f$ and $\psi \circ f $  have the same index at $z_0$.
  Similar statement is true if we pre-compose instead of post-compose with a biholomorphic function.
\end{qbox}

\begin{qbox}
  Show that $\mathrm{index}_f (z)$ does not depend on the choice of charts, and hence is well defined.
\end{qbox}

\begin{qbox}
  Using the fact that $X$ and $Y$ are compact, show that the ramification and branch loci of $f$ are finite.\hint{Use the fact that ramification and branch locus are isolated.}
\end{qbox}

\begin{definition}
  For a point $ w \in Y$, define
  \begin{align*}
    \mathrm{deg}_f(w) = \sum_{z \in f^{-1}(w)} \mathrm{index}_f (z)
  \end{align*}
  This definition makes sense as the right-hand side is finite.
\end{definition}

Let $w_0$ be a point in $Y$.
Suppose $f^{-1}(w_0) = \set{z_1, \dots, z_k}$ with ramification indices $\set{e_1, \dots, e_k}$. We can choose sufficiently small neighborhoods $W_i \subseteq X$ around each $z_i$ such that
\begin{enumerate}
  \item $f:W_i \rightarrow f(W_i) \subseteq Y$ is a ramified covering of degree $e_i$ (so that  $f(z) \approx z^{e_i}$)
  \item $f(W_i) = f(W_j)$ for all $1 \le i,j \le k$.
\end{enumerate}
Let $W = f(W_i)$.

\begin{qbox}
  Show that \begin{align*}
    \mathrm{deg}_f: W &\longrightarrow \bbz \\
    w &\longmapsto \mathrm{deg}_f w
  \end{align*} is a constant function.
\end{qbox}

Thus, for each point $w \in Y$, we can find an open neighborhood $W$ on which $\mathrm{deg}_f$ is a constant function. Because $Y$ is connected this gives us,

\begin{theorem}
  $\mathrm{deg}_f$ is constant function on $Y$.
\end{theorem}
This constant is called the \emph{degree/order} of the ramified covering.

\begin{qbox}
  Find the degree of a non-constant rational function
  \begin{align*}
    f: \bbp^1 &\longrightarrow \bbp^1 \\
    z &\longmapsto \dfrac{p(z)}{q(z)}
  \end{align*}
\end{qbox}

\begin{qbox}
  Show that every non-constant meromorphic function $f:X \rightarrow \bbp^1$ has the same number of zeroes and poles, counting multiplicities.
\end{qbox}

\section{Riemann–Hurwitz formula}

\emph{General philosophy:}\footnote{Not to be taken seriously.} Algebra and analysis are used for constructing maps and algebraic topology is used for proving non-existence, by providing obstructions to existence of maps.

\begin{theorem}[Riemann--Hurwitz]
  \label{thm:RiemannHurwitz}
  Let $X$ and $Y$ be compact Riemann surfaces and let $f:X \rightarrow Y$ be a non-constant complex differentiable map which is a ramified covering of order $N$ with ramification points $z_1, \dots, z_k$. Then,
  \begin{align*}
    \chi (X)=N\cdot \chi (Y)-\sum_{i = 1}^k\left(\mathrm{index}_f(z_i) - 1\right)
  \end{align*}
  where $\chi$ is the Euler characteristic.
\end{theorem}

  \begin{lemma}
      If we have a covering map $f:X \rightarrow Y$ of degree $N$ of compact topological surfaces then $\chi(X) = N \cdot \chi(Y)$.
  \end{lemma}

  \begin{proof}
    Put a triangulation on $Y$ which is fine enough that its lift is a triangulation on $X$.
    If the original triangulation had $V, E, F$ vertices, edges, and faces, respectively, then the lifted triangulation will have $N V, NE, NF$ vertices, edges, and faces, respectively.
    The result follows.
  \end{proof}

  \begin{proof}[Proof of Theorem \ref{thm:RiemannHurwitz}]
    Put a triangulation on $Y$ which is fine enough that its lift is a triangulation on $X$.
    Assume further that all the branch and ramification points are vertices in this triangulation.

    Suppose the triangulation on $X$ has $V, E, F$ vertices, edges, and faces, respectively.
    If there were no ramification points the triangulation on $Y$ would have $NV, NE, NF$ vertices, edges, and faces, respectively.

    But now consider a ramified point $z \in X$ with ramification degree $e$ and let $w = f(z) \in Y$ be the corresponding branch point. Suppose there are $k$ triangles with vertex $w$.
    \begin{qbox}
      Show that the triangles around $w$ have a total of $1 + k$ vertices, $2k$ edges, and $k$ faces.
    \end{qbox}
    \begin{qbox}
      Show that in the lifted triangulation, the triangles around $z$ have a total of $1 + k \cdot e$ vertices, $ 2k \cdot e$ edges, and $k \cdot e$ faces.
    \end{qbox}

    If there was no ramification at $z$ then $f$ should have been an $e:1$ mapping and hence we should have had $(1 + k)\cdot e$ vertices, $2k\cdot e$ edges, and $k\cdot e$ faces.
    Hence, a ramification of index $e$ at $z$ results in a drop in the Euler characteristic by
    \begin{align*}
      ((1 + k)\cdot e - 2k\cdot e + k\cdot e) - ((1 + k \cdot e) - 2k \cdot e + k\cdot e) = e - 1
    \end{align*}

    The result follows.
  \end{proof}

\begin{qbox}
  Explicitly lift the following triangulation for the function $f:\bbp^1 \rightarrow \bbp^1$, $f(z) = z^2$ and verify the proof of the Riemann--Hurwitz formula.
  \begin{figure}[H]
  \centering
    \includegraphics[width=0.25\textwidth]{images/octahedron.jpg}
    \caption{Triangulation of $\bbp^1$: the north pole is $\infty$, the south pole is $0$, ``the square equator" is the unit circle in $\bbc$.}
  \end{figure}

\end{qbox}

Using $\chi(X) = 2 - 2g(X)$, we can rewrite Theorem \ref{thm:RiemannHurwitz} as
  \begin{align*}
    g(X) - 1 = N \cdot (g(Y) - 1) + \sum_{i = 1}^k \left(\mathrm{index}_f(z_i) - 1\right) \cdot 1/2
  \end{align*}
  where $g$ is the genus.

  \begin{corollary}
    For a compact Riemann surface $Y$, there are no non-constant differentiable functions $f: \bbp^1 \rightarrow Y$ if $Y \not \cong \bbp^1$.
  \end{corollary}

\begin{corollary}
  If $X$ and $Y$ are complex tori (genus=1) then any non-constant complex differentiable map $f:X \rightarrow Y$ has no ramification points, i.e. the only maps between complex tori are (genuine) covering maps.
\end{corollary}

\begin{corollary}
  If $X$ and $Y$ are compact Riemann surfaces and there is a non-constant complex differentiable map $f:X \rightarrow Y$ which is not an isomorphism, then $g(X) \ge g(Y)$.
\end{corollary}

\begin{qbox}
  Prove the above corollaries using Theorem \ref{thm:RiemannHurwitz}.
\end{qbox}

\section{Elliptic curves}
  \emph{Analogy:} We can construct 1-dimensional real manifolds by looking at solutions to equations $f(x,y) = 0$ inside $\bbr^2$.

  We can do a similar thing for complex manifolds.
  Consider
  \begin{align*}
    S_p = \set{(z,w) : p(z,w) = 0} \subseteq \bbc^2
  \end{align*}

  Under certain restrictions on $p$, this defines a Riemann surface. In particular, this is true when $p(z,w) = z^2 - q(w)$ where $q(w)$ is a degree three polynomial with distinct roots. Further, it is possible to compactify this object by adding a single point at $\infty$ and the resulting object is called an \emph{elliptic curve}.
  \begin{align*}
    \cale ll_q = \set{(z,w) : z^2 = q(w) } \cup \set{\infty}
  \end{align*}

  There is a natural map
  \begin{align*}
    \cale ll_q &\longrightarrow \bbp^1 \\
    (z,w) &\longmapsto w \\
    \infty &\longmapsto \infty
  \end{align*}
  Turns out this is a complex differentiable map of degree 2 which has exactly 4 distinct ramification points, the three roots of $q$ and the point at infinity.
  Plugging in the Riemann--Hurwitz formula we get
  \begin{align*}
    \chi(\cale ll_q)
    &= 2 \chi(\bbp^1) \: + \sum_{\mbox{ 4 points}} (2 - 1) \\
    &= 4 - 4 \\
    &= 0
  \end{align*}
  Hence, $\cale ll_q$ is homeomorphic to a torus.

  \begin{mdframed}
    Almost nothing in this section generalizes arbitrarily.
    Not all compact Riemann surfaces can be embedded in $\bbp^2$, not all non-compact Riemann surfaces can be compactified by adding a single point at infinity.\\

    But things DO generalize with some effort. All compact Riemann surfaces can be embedded in $\bbp^3$, many non-compact Riemann surfaces of interest can be compactified by adding multiple points at infinity.
    It is a very non-trivial theorem in complex analysis that every Riemann surface admits a non-constant meromorphic function.\\

    It is a remarkable accident that things work out to be so nice for elliptic curves.\\

    We will make all of this rigorous (as much as possible) in the next two classes.
  \end{mdframed}
\end{document}


\section{Syntax}

%The symbol {\tt\char32} indicates a mandatory space.

\begin{center}
\begin{tabular}{clll}
{\it Math} & & {\it Eigenmath} & {\it Alternate form and/or comment} \\
\\
$-a$ & & {\tt -a} \\
\\
$a+b$ & & {\tt a+b} \\
\\
$a-b$ & & {\tt a-b} \\
\\
$ab$ & & {\tt a*b} & \verb$a b$ \hspace{10pt}
{\it with a space in between} \\
\\
$\displaystyle{a\over b}$ & & {\tt a/b} \\
\\
$\displaystyle{a\over bc}$ & & {\tt a/b/c} \\
\\
$a^2$ & & {\tt a{\char94}2} \\
\\
$\sqrt{a}$ & & {\tt a{\char94}(1/2)} & {\tt sqrt(a)} \\
\\
$\displaystyle{1\over\sqrt a}$ & & {\tt a{\char94}(-1/2)} & {\tt 1/sqrt(a)} \\
\\
$a(b+c)$ & & {\tt a*(b+c)} & \verb$a (b+c)$
\hspace{10pt} {\it with a space in between} \\
\\
$f(a)$ & & {\tt f(a)} \\
\\
$
\begin{pmatrix}a\\ b\\ c\end{pmatrix}
$
& & {\tt (a,b,c)}
\\
\\
$\begin{pmatrix}a&b\\ c&d\end{pmatrix}$ & & {\tt ((a,b),(c,d))} \\
\\
$T^{12}$ & & {\tt T[1,2]} & {\it tensor component access} \\
\\
$2\,\rm km$ & & {\tt 2*"km"} & {\it units of measure are quoted} \\
\end{tabular}
\end{center}


\chapter{Limits in categories (TO DO)}
We saw near the start of our category theory chapter
the nice construction of products by drawing
a bunch of arrows.
It turns out that this concept can be generalized immensely,
and I want to give a you taste of that here.

To run this chapter, we follow the approach of \cite{ref:msci}.
\todo{write introduction}

\section{Equalizers}
\prototype{The equalizer of $f,g : X \to Y$ is the set of points with $f(x) = g(x)$.}
Given two sets $X$ and $Y$, and maps $X \taking{f,g} Y$, we define their \vocab{equalizer} to be
\[ \left\{ x \in X \mid f(x) = g(x) \right\}. \]
We would like a categorical way of defining this, too.

Consider two objects $X$ and $Y$ with two maps $f$ and $g$ between them.
Stealing a page from \cite{ref:msci}, we call this a \vocab{fork}:
\begin{diagram}
	X & \pile{\rTo^f \\ \rTo_g} & Y
\end{diagram}
A cone over this fork is an object $A$ and arrows over $X$ and $Y$ which make the diagram commute, like so.
\begin{diagram}
	A && \\
	\dTo^q & \rdDashed^{f \circ q = g \circ q} & \\
	X & \pile{\rTo^f \\ \rTo_g} & Y
\end{diagram}
Effectively, the arrow over $Y$ is just forcing $f \circ q = g \circ q$.
In any case, the \vocab{equalizer} of $f$ and $g$ is a ``universal cone'' over this fork:
it is an object $E$ and a map $E \taking{e} X$ such that
for each $A \taking q X$ the diagram
\begin{diagram}
	& A & \\
	\ldTo(1,3)^q & \dTo~{!\exists h} & \rdDashed(1,3) \\
	& E & \\
	X \ldTo(1,1)^e & \pile{\rTo^f \\ \rTo_g} & \rdDashed(1,1) Y \\
\end{diagram}
commutes for a unique $A \taking h E$.
In other words, any map $A \taking{q} X$ as above
must factor uniquely through $E$.
Again, the dotted arrows can be omitted,
and as before equalizers may not exist.
But when they do exist:
\begin{exercise}
	If $E \taking{e} X$ and $E' \taking{e'} X$ are equalizers,
	show that $E \cong E'$.
\end{exercise}

\begin{example}
	[Examples of equalizers]
	\listhack
	\begin{enumerate}[(a)]
		\ii In $\catname{Set}$, given $X \taking{f,g} Y$
		the equalizer $E$ can be realized as $E = \{x \mid f(x) = g(x)\}$,
		with the inclusion $e : E \injto X$ as the morphism.
		As usual, by abuse we'll often just refer to $E$ as the equalizer.

		\ii Ditto in $\catname{Top}$, $\catname{Grp}$.
		One has to check that the appropriate structures are preserved
		(e.g.\ one should check that $\{\phi(g) = \psi(g) \mid g \in G\}$ is a group).

		\ii In particular, given a homomorphism $\phi : G \to H$, the inclusion
		$ \ker\phi \injto G $
		is an equalizer for the fork $G \to H$ by $\phi$ and the trivial homomorphism.
	\end{enumerate}
\end{example}

According to (c) equalizers let us get at the concept of a kernel
if there is a distinguished
``trivial map'', like the trivial homomorphism in $\catname{Grp}$.
We'll flesh this idea out in the chapter on abelian categories.

\section{Pullback squares (TO DO)}
\todo{write me}
Great example: differentiable functions on $(-3,1)$ and $(-1,3)$

\begin{example}
	\label{ex:diff_pullback}
\end{example}

\section{Limits}
We've defined cones over discrete sets of $X_i$ and over forks.
It turns out you can also define a cone over any general \vocab{diagram} of objects and arrows;
we specify a projection from $A$ to each object and
require that the projections from $A$ commute with the arrows in the diagram.
(For example, a cone over a fork is a diagram with two edges and two arrows.)
If you then demand the cone be universal,
you have the extremely general definition of a \vocab{limit}.
As always, these are unique up to unique isomorphism.
We can also define the dual notion of a \vocab{colimit} in the same way.

\section{\problemhead}
\begin{sproblem}[Equalizers are monic]
	Show that the equalizer of any fork is monic.
	\label{prob:equalizer_monic}
\end{sproblem}

pushout square gives tenor product

p-adic

relative Chinese remainder theorem!!


\subsection{Implicit Matrix Representation}
\label{adap_sec:mrep}
\paragraph{}
Matrix representation of a parameterized algebraic curve allows an easy way to calculate the intersection between it with another curve and find the corresponding parameter based on the given point on the curve.
For an algebraic curve $t \in \mathbf{R}^1 \xrightarrow{\phi} \left( \frac{f_1(t)}{f_0(t)}, \frac{f_2(t)}{f_0(t)},\frac{f_3(t)}{f_0(t)} \right) \in \mathbf{R}^3$, $f_0,f_1,f_2$ and $f_3$ are polynomials functions in parameter $t$ with degree $\leq p$.
The procedure of constructing the matrix representation for NURBS curves are explained detail in \citep{Laurent2014}.
\paragraph{}
The aim of this method is to find 4-tuples of polynomials\\
$\left( g_0(t),g_1(t),g_2(t),g_3(t) \right)$ with order $v$ so that
\begin{equation}
    \sum_{i=0}^3 g_i(t) f_i(t) \equiv 0
    \label{adap_eq_mRep_eq0}
\end{equation}
%
who is a vector space and one of its bases can be:
\begin{equation}
    \mathbf{L_j}(t,X,Y,Z) = g_0(t) + Xg_1(t) + Yg_2(t) + Zg_3(t)
    \label{adap:eq:mrep_eq1}
\end{equation}
Since $g$ is also a polynomial based function, it can be expressed in the vector space with a set of bases of $\left\{\psi_1(t), \psi_2(t), \dots, \psi_{m_v}(t) \right\}$ and the bases $\mathbf{L_j}$ can be expressed as
    \begin{equation}
        \begin{aligned}
            \mathbf{L_j} &= \sum^{m_v}_{i=1}\left( \lambda_{0,i}^{(j)} + \lambda_{1,i}^{(j)}X + \lambda_{2,i}^{(j)}Y + \lambda_{3,i}^{(j)}Z\right)\psi_i(t)\\
        & = \sum_{i=1}^{m_v} \Lambda_{i,j}(X,Y,Z)\psi_i(t)
        \end{aligned}
    \end{equation}
%
Finally, a matrix which represents the mapping of $\phi$ in a $m_v \times r_v$-matrix $\mathbf{M_v}$ with order $v$
    \begin{equation}
        \mathbf{M_v}(\phi) =
        \begin{bmatrix}
        \Lambda_{1,1} & \Lambda_{1,2} & \dots & \Lambda_{1,r_v} \\
        \Lambda_{2,1} & \Lambda_{2,2} & \dots & \Lambda_{2,r_v} \\
        \vdots 		  & \vdots 		  &  	  &\vdots			\\
        \Lambda_{m_v,1}&\Lambda_{m_v,2}&\dots &\Lambda_{m_v,r_v}
        \end{bmatrix}
    \end{equation}

%=====================================================================================================================%
\subsection{Matrix Representation for Rational Bézier Curves}
An rational bézier curves can be defined by
\begin{equation}
	\phi:t\in \mathbf{R}\rightarrow \frac{\sum_{i=0}^pw_i\mathbf{P}_iB_i^p(t) }{\sum_{i=0}^pw_iB_i^p(t)}
\end{equation}
where
\begin{equation}
    B_i^p(t) = \mathbf{C}_i^dt^i(1-t)^{d-i}
    \label{adap_eq_mrep_bbasis}
\end{equation}
%
The aim is to find a matrix whose vector is in the form of
\begin{equation}
    [\alpha] =
    \begin{bmatrix}
        \alpha_{0,0} & \alpha_{0,1}&  \dots&  \alpha_{0,v} & \alpha_{1,0} & \dots & \alpha_{3,v}
    \end{bmatrix}^T
\end{equation}
%
where $g_j(t)$ in Eq.~\ref{adap:eq:mrep_eq1} can be expressed as
\begin{equation}
    g_j(t) = \sum_{i=0}^v \alpha_{j,i}B_i^v(t)
\end{equation}
%
Based on Eq.~\eqref{adap_eq_mRep_eq0}, it can be concluded that $\mathbf{R}\times\left[\alpha\right]=0$
\begin{equation}
    \mathbf{R} =
    \begin{bmatrix}
        B_0^v(t)f_0(t) & \dots & B_v^v(t)f_0(t) & B_0^v(t)f_1(t) & \dots & B_v^v(t)f_3(t)
    \end{bmatrix}
\end{equation}
%
By having another set of basis $\mathbf{L_v}$ and the transformation matrix $\mathbf{S}$ so that $\mathbf{L_vS}=\mathbf{R}$ where
\begin{equation}
    \mathbf{L_v}=
    \begin{bmatrix}
        B_0^{v+d}(t) & B_1^{v+d}(t) & \dots & B_{v+d}^{v+d}(t)
    \end{bmatrix}
\end{equation}
%
This leads to
\begin{equation}
    \begin{bmatrix}
        B_0^{v+d}(t) & B_1^{v+d}(t) & \dots & B_{v+d}^{v+d}(t)
    \end{bmatrix}\times \mathbf{S}\times\left[\alpha\right] = R\times\left[\alpha\right] = 0
\end{equation}
which indicates $\left[ \alpha\right]$ is in the null space of $\mathbf{S}$

After substituting $f(t) = \sum_{i=0}^dc_iB_i^d(t)$ into $\mathbf{R}$, the following can be deduced
\begin{equation}
    B_j^v(t)f(t) =  \sum_{i=0}^dc_iB_i^d(t)B_j^v(t) =\sum_{i=0}^d \frac{\mathbf{C}^v_j\mathbf{C}^d_i}{\mathbf{C}^{d+v}_{i+j}}c_i B_{i+j}^{d+v}(t)
\end{equation}
%
which indicates that
\begin{equation}
    \mathbf{S}_{i+j,j} = \frac{\mathbf{C}^v_j\mathbf{C}^d_i}{\mathbf{C}^{d+v}_{i+j}}c_i
\end{equation}
%
Finally, the null space of $\mathbf{S_v}$, $\mathbf{M_v}$ is the matrix representation of the rational bézier curve.

%=====================================================================================================================%
\subsection{Intersection}
The calculation of the intersection is described in detail in \citep{Buse2010, Ba2009}.
All intersections can be calculated at once by using matrix representation of the algebraic curve.
\paragraph{}
Given a rational curve/surface $C1$
\begin{equation}
    \mathbf{P}^1 \xrightarrow{\phi_1} \mathbf{P}^n: (u,v) \rightarrow(f_0,f_1,f_2,f_3)(u,v)
\end{equation}
%
the aim is to find the intersection between it with another rational curve $C2$ via matrix representation
\begin{equation}
    \mathbf{P}^1 \xrightarrow{\phi_2} \mathbf{P}^n: (t) \rightarrow(g_0,g_1,g_2,g_3)(t)
\end{equation}
%
is to find
\begin{equation}
    \mathbf{M}_{v1}(\phi_2(t) = 0
\end{equation}
%
which leads to
\begin{equation}
    \mathbf{M_0}g_0 + \mathbf{M_1}g_1 + \mathbf{M_2}g_2 + \mathbf{M_3}g_3 = 0
    \label{mRep_intec_base}
\end{equation}
%
By knowing $g_n$ is a polynomial function with order $p$, Eq.~\eqref{mRep_intec_base} can be rearranged as
\begin{equation}
    \mathbf{M}(t) = \sum_{i=0}^p \mathbf{M_i}t^i
\end{equation}
%
After that, the generalized companion $q \times p$-matrices $A, B$ with rank $\rho$ are introduced
\begin{equation}
    A =
    \begin{bmatrix}
        0		&I 		&\dots 		&\dots 		&0 		\\
        0 		&0 		&I 			&\dots 		&0 		\\
        \vdots 	&\vdots &\vdots 	&\vdots 	&\vdots \\
        0 		&0 		&\dots		&\dots 		&I 		\\
        M_0^t 	&M_1^t 	&\dots 		&\dots 		&M_{d-1}^t
    \end{bmatrix}
\end{equation}
%
\begin{equation}
    B =
    \begin{bmatrix}
        I 		&0 		&\dots 		&\dots 		&0 		\\
        0 		&I 		&0 			&\dots 		&0 		\\
        \vdots 	&\vdots &\vdots 	&\vdots 	&\vdots \\
        0 		&0 		&\dots 		&I 			&0 		\\
        0 		&0 		&\dots 		&\dots 		&-M_d^t \\
    \end{bmatrix}
\end{equation}
%
Before the eigenvalues are calculated, the regular part of a non-square pencil of the matrices shall be extracted first which is done by the following step
%
\paragraph{Step 1}
Transform $B$ into its column echelon form:
SVD-decomposition is adopted to perform the task.
\begin{equation}
\begin{aligned}
    B_1 = BV_0 = [\underbrace{B_{1,1}}_{\rho} |\underbrace{0}_{q-\rho}]	\\
    A_1 = AV_0 = [\underbrace{A_{1,1}}_{\rho} |\underbrace{A_{1,2}}_{q-\rho}]
\end{aligned}
\end{equation}
%
\paragraph{Step 2}
Transform $A_{1,2}$ into its row echelon form:
\begin{equation}
    U_1A_{1,2} =
    \begin{bmatrix}
        \underline{A^\prime_{1,2}}\\
        0
    \end{bmatrix}
\end{equation}
%
where $A^\prime_{1,2}$ is in full row rank.\\
At the end of step 2, matrix $A$ and $B$ can be represented as
\begin{equation}
\begin{aligned}
    A^\prime_1 &=
    \begin{bmatrix}
        A^\prime_{1,1} & A^\prime_{1,2} \\
        \cmidrule(lr){1-2}
        A_2 & 0
    \end{bmatrix}\\
    B^\prime_1 &=
    \begin{bmatrix}
        B^\prime_{1,1} & 0\\
        \cmidrule(lr){1-2}
        B_2 & 0
    \end{bmatrix}
\end{aligned}
\end{equation}
where $A^\prime_{1,2}$ has full row rank\\
$
\begin{bmatrix}
\underline{B^\prime_{1,1}}\\
B_2
\end{bmatrix}
$ has full column rank\\
$
\begin{bmatrix}
\underline{B^\prime_{1,1}}\\
B_2
\end{bmatrix}
$
and $B_2$ are in echelon form
\paragraph{}
$A_2$ and $B_2$ will be the new $A$ and $B$ matrices for next iteration until $B$ has full rank.
If $B$ has full row rank but not full rank, $A=A^T$ and $B=B^T$ are conducted.
\paragraph{}
After these processes, $A$ and $B$ become two square matrices and $B$ is invertible so that the solution for the intersection parameter $t$ can be determined from the eigenvalue of the matrix $AB^{-1}$
\paragraph{}
However, the method may fail when the intersection is under the case where nearly tangential geometric conditions happens and return two empty matrices.
It is addressed by adding another step after extracting the real part of the $A$ and $B$ if the results are empty matrices\citep{Shen2016}.
If the input matrix $A$ and $B$ are not in full row rank or full column rank, it is considered that $C1 \cap C2 = C2$.
If the input matrix $A$ and $B$ are in full row rank or full column rank, a rank $m$ square sub-pencil is extracted assuming $A$ and $B$ have a rank of $m$.
Then the eigenvalues yield the intersections.