% !TEX root = apxthy.tex

\section{Trigonometric Polynomials}
%
\label{sec:trig}
%
In this chapter we consider approximation of periodic functions by trigonometric
polynomials (aka Fourier spectral methods). Throughout this chapter, let $\TT :=
(-\pi, \pi]$ and we identify $C^j(\TT) = C^j_{\rm per}(\TT)$, $A_{\rm per}(\TT) =
A(\TT)$, $L^p(\TT)$ to be the spaces of $2\pi$-periodic functions on $\R$ that
are, respectively, $j$ times  continuously differentiable, analytic, belong
to $L^p(-\pi, \pi)$. Similarly, $H^j_{\rm per}(\TT) = H^j(\TT)$ denotes the
space of $2\pi$ periodic functions on $\R$ such that their restriction to {\em
any} interval $(a, a+2\pi)$ belongs to $H^j(a,a+2\pi)$.

Examples of periodic functions:
\begin{itemize}
  \item $\sin(nx) \in A(\TT)$
  \item $|\sin(nx)| \in C^{0,1}(\TT)$
  \item $|\sin(nx)|^3 \in C^{2,1}(\TT)$
  \item $e^{-\cos x} \in A(\TT)$
  \item $(c^2+\sin^2 x)^{-1} \in A(\TT)$
  \item \dots
\end{itemize}

Applications:
\begin{itemize}
  \item BVPs with periodic boundary conditions and periodic data, e.g.,
  \begin{align*}
      - (p(x) u_{x})_x + q(x) u &= f(x), \qquad x \in (-\pi, \pi), \\
      u(-\pi) &= u(\pi), \\
      u'(-\pi) &= u'(\pi),
  \end{align*}
  where $p, q, f$ are $2\pi$-periodic, then under suitable conditions on
  $p, q, f$ there exists a unique solution which is also $2\pi$-periodic.
  %
  \item Functions represented in polar coordinates: $u(x, y) = v(r, \theta)$
  then, for $r$ fixed, $\theta \mapsto v(r, \theta)$ is periodic.

  There are many other examples of naturally ``periodic'' coordinate systems,
  including e.g. spherical coordinates, or the dihedral angle.
  %
  % \item Bond-angles and dihedral
\end{itemize}

Approximation by trigonometric polynomials is based on the idea of Fourier
series representation of periodic functions. Talking about Fourier series
becomes much more convenient if we extend the admissible range of all functions
to $\C$;, i.e. $f : \R \to \C$, still $2\pi$-periodic. The following definition then becomes natural.

\begin{definition}
  A trigonometric polynomial of degree $N$ is any function of the form
  \[
    \sum_{k = -N}^N a_k e^{i k x}
  \]
  The space of all such polynomials is denoted by  $\Trig_N$.
  The canonical basis is
  \[
     \b\{ e^{ikx} \bsep  k = -N, -N+1, \dots, N \b\}
  \]
\end{definition}

\begin{definition}
  Let $f \in L^1(\TT)$, then its {\em Fourier coefficients} are given by,
  \begin{equation} \label{eq:trig:fourier coeffs}
    \hat{f}_k := \mint_{-\pi}^\pi f(x) e^{- i kx} \,dx
  \end{equation}
  The $N$-th partial sum, is a trigonometric polynomial, which we
  denote by
  \[
    \Pi_N f(x) := \sum_{n = -N}^N \hat{f}_k e^{i kx}.
  \]
\end{definition}

\subsection{Approximation by $L^2$-projection}
%
\label{sec:trig:L2}
%
We will initially study approximation of functions in the $L^2$-norm.
It can then be convenient to normalise the inner product, via
\[
  \< f, g \>_{L^2(\TT)} := \mint_{-\pi}^\pi f g^* \, dx.
\]
Equipped with this inner product, $L^2(\TT)$ is a Hilbert space.

\begin{theorem} \label{th:trig:plancherel}
  \begin{enumerate} \ilist
  \item Convergence of Fourier Series: $\{ e^{ikx} \sep k \in \Z \}$ is an orthonormal basis for $L^2(\TT)$.
  \item Plancherel Theorem: $\mathcal{F} : L^2(\TT; \C) \to \ell^2(\Z; \C)$ is an isomorphism;
    i.e., $f \in L^2(\TT)$ then $\hat{f} \in \ell^2(\Z)$ and
    \[
       \sum_{k \in \Z} \hat{f}_k \hat{g}_k^* = \mint_\TT f g^* \,dx.
    \]
    In particular, $\|f\|_{L^2} = \|\hat{f} \|_{\ell^2}$.
  \end{enumerate}
\end{theorem}
\begin{proof}
  This is left as an exercise. The key point is that
  \begin{equation}
    \mint_{-\pi}^\pi  e^{-ikx} e^{i\ell x} \,dx
    = \mint_{-\pi}^\pi e^{(\ell-i)x}
    = \cases{
      1, & \ell = k, \\
      0, & \text{otherwise.}
    }
  \end{equation}
\end{proof}

\begin{exercise}
  There is a general theorem that all (separable) Hilbert spaces are
  isometrically isomorphic to $\ell^2(\N)$ or equivalently to $\ell^2(\Z)$.
  Explain why the Plancherel theorem simply shows that the Fourier series map
  $f \mapsto \hat{f}$ is an the explicit construction of this isometry.
\end{exercise}

\begin{proposition} \label{th:trig:PiNf-orthproj}
  Let $f \in L^2(\TT)$, then
  \begin{equation} \label{eq:trip:PiNf-orthproj}
    \| \Pi_N f - f \|_{L^2}^2 = \sum_{|k| > N} |\hat{f}_k|^2.
  \end{equation}
  In particular, $\Pi_N f$ is the $L^2$-orthogonal
  projection of $f$ onto $\Trig_N$, or equivalently, the
  best approximation of $f$ from $\Trig_N$ w.r.t. $\|\cdot\|_{L^2}$.
\end{proposition}
\begin{proof}
  By definition,
  \begin{align*}
    f(x) - \Pi_N f(x) = \sum_{|k|>N} \hat{f}_k e^{ikx},
  \end{align*}
  and Plancherel's theorem then implies \eqref{eq:trip:PiNf-orthproj}.

  The fact that $\Pi_N f$ is the best approximation is a straightforward
  consequence: if $g \in \Trig_N$, then
  \begin{align*}
    \b\|f(x) - \Pi_N f(x) - g\b\|_{L^2}^2
    &= \sum_{|k| \leq N} |\hat{g}_k|^2 + \sum_{|k| > N} |\hat{f}_k|^2 \\
    &\geq \sum_{|k| > N} |\hat{f}_k|^2 \\
    &= \b\|f(x) - \Pi_N f(x) \b\|_{L^2}^2. \qedhere
  \end{align*}
\end{proof}

The main point of Lemma~\ref{th:trig:PiNf-orthproj} is that, exactly as in the
introductory example, we can characterise the error in terms of the
decay of the Fourier coefficients, so we will study this next.

\subsection{Decay of Fourier Coefficients}
%
\label{sec:trig:decay}
%
As we already saw in the introductory example, the ``smoother'' $f$ is, the
faster $\hat{f}_k$ decay. The following results are not difficult to generalise
in several ways; see remarks below, but in the spirit of valuing simplicity over
optimality, we will formulate them only for $C^p$ regularity.

\begin{theorem} \label{th:trig:decay}
  \begin{enumerate} \ilist
    \item Let $f \in C^{p-1}(\TT)$ and $f^{(p-1)}$ be absolutely continuous, then
    \[
        |\hat{f}_k| \leq \| f^{(p)} \|_{L^1(\TT)} |k|^{-p}.
    \]
    %
    \item {\it Paley--Wiener Theorem:} If $f \in A(\TT)$, then there exists
    $a > 0$ such that
    \[
        |\hat{f}_k| \lesssim e^{-a N}.
    \]
  \end{enumerate}
\end{theorem}
\begin{proof}[Proof of Theorem~\ref{th:trig:decay}(1)]
  Consider first the case $p = 1$, i.e., $f$ is absolutely continuous. This means that $f'$ exists almost everywhere and satisfies an integration by parts formula. We can utilize this as follows:
  \begin{align*}
    -i k \hat{f}_k &= \mint f(x) \big(- i k e^{-ikx}\big) \, dx \\
    &= \mint f(x) \frac{d}{dx} e^{ikx}\, dx \\
    &= - \mint f'(x) e^{ikx}\, dx.
  \end{align*}
  Taking the modulus on the left-hand side we obtain
  \[
    |k \hat{f}_k| \leq \mint |f'| \,dx = \|f'\|_{L^1(\TT)}.
  \]
  Since $f'$ is AC, the right-hand side is finite. This yields the claim.

  For $p > 1$, we can simply apply the integration by parts argument multiple times.
\end{proof}

We postpone the proof of the Paley--Wiener Theorem to
Theorem~\ref{th:trig:pw-trefversion}, but instead first discuss the consequences
of these results. First, we consider approximation in the max-norm even though this seems counter intuitive in the Hilbert space setting.

We can also prove uniform convergence results using the decay of the Fourier coefficients established above:

\begin{theorem} \label{th:trig:convergence_max}
  \begin{enumerate} \ilist
    \item Let $f \in W^{p,1}(\TT)$ then
    \[
        \|f - \Pi_N f \|_{\infty} \lesssim N^{1-p}
    \]
    \item Let $f \in C^\infty(\TT)$, then for each $p > 0$ there exists a
    constant $C_p$ such that
    \[
        \|f - \Pi_N f \|_{\infty} \leq C_p N^{-p}.
    \]
    \item If $f \in A(\TT)$, then there exists $a > 0$ such that
    \[
         \| f - \Pi_N f \|_{\infty} \lesssim e^{-a N}.
    \]
    \end{enumerate}
  \end{theorem}
\begin{proof}
  (1) This is a straightforward calculation:
  \begin{align*}
    \big|f(x) - \Pi_N f(x)\big|
    &=
    \bigg| \sum_{|k| > N} \hat{f}_k e^{i k x} \bigg|
    \\ &\leq
    \sum_{|k| > N} |\hat{f}_k|
    \\ &\lesssim
    \sum_{k = N+1}^\infty |k|^{-p} \lesssim N^{-p}.
  \end{align*}

  (2) is an immediate consequence; (3) is proven analogously.
\end{proof}

Returning to convergence in $L^2$, a direct naive calculation shows that, if $f \in W^{p,1}(\TT)$, then
\[
    \|f - \Pi_N f \|_{L^2} \lesssim N^{1/2-p}.
\]
Is this sharp? Depends really on whether $f$ is {\it exactly} in $W^{p,1}$ and no better. The tricky part is how one can really test this. In practice one actually rarely encounters functions of this kind (though in the theory of PDEs they are phenomenally important), and it is much more convenient and natural to consiter scales of continuous and H\"{o}lder continuous functions. In particular H\"{o}lder continuity connects very naturally to the kind of singularities one typically occurs in physical models. We will get to this soon.

For now, we will briefly look at two simple extensions that can be convenient sometimes and yield slightly sharper results. The proofs are left as exercises.

\begin{lemma} \label{th:trig:fCp-coeffL2}
  Let $f \in W^{p,2}(\TT)$, then $(\hat{f}_k |k|^p)_{k \in \Z} \in \ell^2(\Z)$.
\end{lemma}
% \begin{proof}
%   This is a relatively straightforward extension of the Proof of
%   Theorem~\ref{th:trig:decay}(1) and is left as an exercise.
% \end{proof}

\begin{theorem} \label{th:trig:convergence_L2}
  Let $f \in W^{p,2}(\TT)$ then
  \[
      \|f - \Pi_N f \|_{L^2} \lesssim N^{-p}
  \]
\end{theorem}
% \begin{proof}
%   We only prove (1); the results (2, 3) are left as an exercise.

%   From Lemma~\ref{th:trig:PiNf-orthproj} we have
%   \begin{align*}
%     \|f-\Pi_N f \|_{L^2}^2
%     &= \sum_{|k| > N} |\hat{f}_k|^2 \\
%     &= \sum_{|k| > N} |\hat{f}_k|^2 |k|^{2p} |k|^{-2p} \\
%     &\lesssim N^{-2p},
%   \end{align*}
%   where we used Lemma~\ref{th:trig:fCp-coeffL2} in the last step.
% \end{proof}

\subsubsection{Remarks}
\begin{enumerate}
  \item The algebraic convergence rates are rarely sharp, in the sense that taking an arbitrary function and showing that it is $W^{p,2}, W^{p,1}$ but no better in any way would be a rarity. The
    precise structure of $f^{(p)}$ is extremely relevant. One could go into fractional Sobolev spaces, to give a finer control, but we will not pursue this for now.

    % For example, one can
    % show that, if $f^{(p-1)}$ is absolutely continuous (or even just of bounded variatin)
    % then the decay rate $|\hat{f}_k| \leq \|f^{(p)}\|_{L^1} N^{-p}$ still holds.
    % In particular, if $f^{(p)} \in C(\TT)$ as we have assumed here, this gives
    % additional structure that we have not exploited.

  \item The important main message take away from these results is this: (1) $f \in C^p(\TT)$ regularity
  gives algebraic decay of $\hat{f}_k$; (2) $f \in C^\infty(\TT)$ gives
  super-algebraic decay; (3) $f \in A(\TT)$ gives exponential decay.

  % \item We can also derive uniform approximation error estimates which further
  % highlight that our results are not sharp, e.g.,  if $f \in C^p(\TT)$, then
  % \[
  %   |f(x) - \Pi_N f(x)| \leq \sum_{|k| > N} |\hat{f}_k|
  %       \lesssim \sum_{|k| > N} |k|^{-p}
  %       \lesssim N^{1-p}.
  % \]
  % In the next section we will show how to construct much better uniform
  % approximations with sharp rates. Using a similar trick as in the proof of
  % Theorem~\ref{th:trig:convergence_L2} we can improve this to $\|f-\Pi_N f
  % \|_\infty \lesssim N^{1/2-p}$. Getting a little deeper into harmonic analysis
  % we may even prove that $|\hat f_k| |k|^p \in \ell^p$ for all $p > 1$, which
  % indeed implies that $\|f - \Pi_N f \|_\infty \lesssim N^{\epsilon - p}$ for
  % all $\epsilon > 0$. This give us a hint that the best approximation error in
  % the max-norm is in fact $O(N^{-p})$ when $f \in C^p$. We will choose a very
  % different route in \S~\ref{sec:trig:jackson} to prove this result.

  \item The uniform convergence estimate for analytic functions arising from
  the Paley--Wiener theorem is however qualitatively sharp.

  \item The condition $f \in W^{p,1}$ is not sharp. One can weaken it to require only that $f^{(p-1)}$ has finite total variation. We will not look at this in generality, but consider a special case of this generalisation in Exercise~\ref{exr:trig:gibbs}.
\end{enumerate}

\subsection{The Paley--Wiener Theorem}
%
\label{sec:trip:pw}
%
If $f$ is analytic on an interval $[a, b]$, then standard theorems of complex
analysis imply the it can be extended to a analytic function in a
neighbourhood $U$ of $[a, b]$. In the case of periodic functions, such a
neighbourhood can be chosen to be a strip,
\[
  \Omega_\alpha := \{ z \in \C \sep |\Im z| < \alpha \},
\]
for some $\alpha > 0$. This is the starting point for a more refined
version of Theorem~\ref{th:trig:decay}(2).

\begin{theorem} \label{th:trig:pw-trefversion}
  Suppose that $f$ is analytic in $\Omega_\alpha$ with
  $\sup_{z \in \Omega_\alpha} |f(z)| = M_\alpha$, then
  \[
    |\hat{f}_k| \leq 2\pi M_\alpha e^{-\alpha|k|}.
  \]
\end{theorem}
\begin{proof}
  Assume $k > 0$; the case $k < 0$ is analogous.
  Recall that
  \[
    \hat{f}_k = \frac{1}{2\pi} \int_{-\pi}^\pi f(x) e^{-ikx} \,dx.
  \]
  We fix some $\beta < \alpha$ and define a complex contour
  \[
    \mathcal{C} := (-\pi, \pi] \cup (\pi, \pi+ \beta i]
        \cup (-\pi + \beta i, \pi + \beta i] \cup (-\pi, -\pi + \beta i]
      = \mathcal{C}_{1} \cup \mathcal{C}_{2}
        \cup \mathcal{C}_{3} \cup \mathcal{C}_{4},
  \]
  to be traversed counterclockwise. In particular $\frac{1}{2\pi i} \int_{\mathcal{C}_1} f(z) e^{ikz} \, dz = \hat{f}_k$, and periodicity of $f$ yields
  \[
      \sum_{j \in \{2, 4\}}  \int_{\mathcal{C}_j} f(z) e^{ikz} \, dz = 0.
  \]
  Combining these observations with Cauchy's theorem yields
  \begin{align*}
      0
      &= \frac{1}{2\pi} \oint_{\mathcal{C}} f(z) e^{ikz} \, dz \\
      &= \sum_{j = 1}^4\frac{1}{2\pi} \int_{\mathcal{C}_j} f(z) e^{ikz} \, dz  \\
      &= \hat{f}_k + \frac{1}{2\pi} \int_{-\pi}^\pi f(x + \beta i) e^{i (x+\beta i) k} \, dx.
  \end{align*}
  Since we assumed that $k > 0$ we have $|e^{i (x+\beta i)k}| = e^{-\beta k}$, hence
  rearranging the previous identity yields the estimate
  \begin{align*}
    |\hat{f}_k| &\leq \mint_{-\pi}^\pi |f(x+\beta i)| e^{-\beta k} \,dx
      \leq M_\beta e^{-\beta k}.
  \end{align*}
  Since the upper bound valid for all $\beta < \alpha$ it also holds for $\beta
  = \alpha$.
\end{proof}

The previous theorem clarifies that, to precisely understand the
best-approximation of an analytic function $f$ by  trigonometric polynomials
we {\em must} study $f$ not on $\TT$ but in the complex plane. While some
further generalisations are possible, we will restrict ourselves mostly
to the context of Theorem~\ref{th:trig:pw-trefversion} and thus look for the
largest $\alpha$ such that $f$ can be extended to a analytic function
on $\Omega_\alpha$.

Suppose we have found an $\alpha$ such that $f \in A(\Omega_\alpha)$. If $f$
blows up at some $x \pm i \alpha$ then we have found the maximal region of
analyticity. If $f$ is bounded in $\Omega_\alpha$ then it is analytic at every
point $z \in \partial \Omega_\alpha$ and hence we can extend $f$ to a
analytic function in a larger domain $\Omega_{\alpha'}$, $\alpha' > \alpha$.
Thus, to determine the maximal region of analyticity we must find the {\em
poles} of $f$. We obtain the following simple corollary of Theorem~\ref{th:trig:pw-trefversion}.

\begin{corollary} \label{th:trig:pw-sharp}
  Let $f \in A(\TT) \cap A(\Omega_\alpha)$ with $\alpha$ maximal, then
  for all $\epsilon > 0$ there exists $C_\epsilon > 0$ such that
  \[
    |\hat{f}_k| \leq C_\epsilon e^{- (\alpha-\epsilon) |k|}.
  \]
  Moreover, we have the approximation error estimate
  \[
    \| f - \Pi_N f \|_{L^\infty} \lesssim C_\epsilon' e^{-(\alpha-\epsilon) N}
    \qquad \forall \epsilon > 0.
  \]
\end{corollary}

\begin{example}[Smeared Zig-Zag]
  Consider a family of periodic functions inspired by our introductory example,
  \[
    f(x) = (1 + c^2 \sin^2 x)^{-1},
  \]
  where $c > 0$. Then the analytic extension is still given by $f(z) = (1 + c^2
  \sin^2 z)^{-1}$. To find the maximal strip of analyticity we need to compute
  the poles, i.e., the points $z \in C$ such that $\eps^2 + \sin^2 z = 0$, or
  equivalently $\sin z = \pm i \eps$, where $\eps = 1/c$.

  To that end, we first note that
  \[
    \sin z = \sin (x + i y) = \sin x \cosh y + i \b\{ \cos x \sinh y \b\}.
  \]
  Thus the poles are given by the solutions to
  \[
       \sin x \cosh y = 0, \qquad \qquad
       \cos x \sinh y = \pm \eps.
  \]
  Since $\cosh y \neq 0$, The first condition requires $\sin x = 0$, or,
  $x \in \pi \Z$, hence $\cos x = \pm 1$. The second condition therefore
  yields $\sinh y = \pm \eps$, or, equivalently,
  \[
      x \in \pi \Z, \qquad y = \pm \sinh^{-1} \eps.
  \]
  This characterises all the poles of $f(z)$, and in particular shows that
  the maximal strip of analyticity is
  \[
      \Omega_{\sinh^{-1} \eps}
  \]
  Our theory therefore predicts (ignoring the $\epsilon$-factors) that
  \[
      |\hat{f}_k| \lesssim e^{- \sinh^{-1} \eps |k|}
            \sim e^{- \eps |k|} = e^{-|k|/c} \qquad \text{for $\eps \sim 0$}
  \]
  as well as the approximation error estimate
  \[
      \| f - f_N \|_\infty \lesssim e^{- \sinh^{-1} \eps N} \sim e^{- \eps |k|}
      = e^{-|k|/c}
      \qquad \text{for $\eps \sim 0$.}
  \]
  After discussing trigonometric interpolation we will show numerical
  tests demonstrating that this is sharp.
\end{example}

Finally, it is also natural to ask about the case when $f$ is entire, i.e.,
$f \in A(\Omega_\alpha)$ for all $\alpha > 0$. In this case, we simply
obtain \Cref{th:trig:pw-sharp} with $\alpha = \infty$:

\begin{corollary} \label{th:trig:pw-entire}
  Suppose that $f \in A(\TT) \cap A(\C)$  (i.e., $f$ is entire), then for all
  $\alpha > 0$ there exists $C_\alpha > 0$ ($C_\alpha =
  \|f\|_{L^\infty(\Omega_\alpha)}$ such that
  \[
      |\hat{f}_k| \lesssim C_\alpha e^{-\alpha |k|}.
  \]
\end{corollary}

\subsection{Approximation by convolution: Jackson's Theorem}
%
\label{sec:trig:jackson}
%
The $L^2$-projection operator $\Pi_N$ can be written in terms of the {\em Dirichlet Kernel},
\[
    D_N(x) = \frac{\sin\b( (N+1/2) x \b)}{ \sin(x/2) }.
\]
That is,
\[
  \Pi_N f(x) = D_N \ast f(x) := \mint_{-\pi}^\pi D_N(x-t) f(t) \, dt.
\]
See Exercise~\ref{exr:trig:dirichlet} for the details. An analysis of the fine properties of the kernel $D_N$ leads to more precise convergence estimates, in particular in norms other than $L^2$.

Suppose, for example that we wanted to get an optimal estimate for $\| f - \Pi_N f \|_\infty$, then we can estimate (similar as we do for estimating the interpolation error!)
\[
  \| f - \Pi_N f \|_\infty
  \leq
  \| f - t_N \|_\infty + \| \Pi_N (t_N - f) \|_\infty.
\]
Moreover,
\[
  | \Pi_N g(x) | = \bigg| \mint D_N(x - t) g(t) \,dt \bigg|
    \leq \| D_N \|_{L^2(\TT)} \|g \|_\infty,
\]
and hence,
\[
  \| f - \Pi_N f \|_\infty \leq (1 + \|D_N\|_{L^1}) \| t_N - f \|_\infty,
\]
i.e. the $L^2$ projection $\Pi_N f$ is optimal up to a constant factor $C_N = 1 + \|D_N\|_{L^1}$. It turns out that $\|D_N\|_{L^1} \approx \log N$, which means we are really {\em very close} to optimal (again, see Exercise~\ref{exr:trig:dirichlet}). But it is still interesting to ask whether this log-factor can be removed!

In general, we can ask whether alternative kernels could be employed to construct approximations via convolutions,
\[
    (K_N \ast f)(x) := \mint_{-\pi}^\pi K_N(x-t) f(t) \, dt,
\]
and what advantages those different kernels might have.
If $K_N(t)$ is a trigonometric polynomial, then $K_N \ast f$ will also be a trigonometric polynomial:

\begin{lemma}
  If $K_N \in \Trig_N$, then $K_N \ast f \in \Trig_N$ for
  all $f \in L^1(\TT)$.
\end{lemma}
\begin{proof}
  \begin{align*}
    \mint_{-\pi}^\pi K_N(x-t) f(t) \,dt
    &=
    \sum_{k =  -N}^N \sum_{k' \in \Z}
        \hat{K}_{N,k} \hat{f}_{k'} \mint_{-\pi}^\pi e^{ik(x-t)} e^{ik't}\,dt
    \\ &=
    \sum_{k =  -N}^N \sum_{k' \in \Z}
       \hat{K}_{N,k} \hat{f}_{k'} e^{ikx} \delta_{kk'}
    \\ &=
    \sum_{k = -N}^N \hat{K}_{N,k} \hat{f}_{k} e^{ikx}. \qedhere
  \end{align*}
\end{proof}

There is considerable freedom in the choice of kernel; the only thing they all have in common is that they must approximate the Dirichlet-delta function in a suitable sense. Purely for the purpose of theoretical analysis, a great choice is the Jackson kernel,
\[
    J_M(x) := \gamma_M \left( \frac{\sin( Mx/2)}{\sin(x/2)} \right)^4,
    \qquad
    \mint_\TT J_M(x) = 1,
\]
where the second condition determines the normalisation constant $\gamma_M$.
Constructing approximates via the Jackson kernel leads to elegant and
sharp approximation error estimates in the max-norm in particular for H\"{o}lder continuous functions. Note already that we now have $\| J_M \|_{L^1} = 1$, i.e. we no longer have the growth of the operator norm as in the Dirichlet kernel!

We write $J_M$ instead of $J_N$ since the degree of $J_M$ is {\em not} equal to $M$:

\begin{lemma}
  $J_M \in \Trig_{2M-2}$.
\end{lemma}
\begin{proof}
  Let $z = e^{ix/2}$, then
  \[
      J_M(x)
      =
      \left((z^M - z^{-M}) / (z - z^{-1})\right)^4.
  \]
  Further, we have
  \begin{align*}
    % J_M(x)
    % &=
    \frac{z^M - z^{-M}}{z - z^{-1}}
    &= z^{M-1} + z^{M-3} z^{-1} + z^{M-3} z^{-2} + \dots
      + z z^{-M+2} + z^{-M+1} \\
    &= z^{M-1} + z^{M-3} + z^{M-5} + \dots + z^{-M+1}
    = \sum_{\alpha \in \mathcal{A}} z^\alpha,
  \end{align*}
  where $\mathcal{A} := \{-M+1, -M+3, -M+5, \dots, M-1\}$. Squaring yields
  \begin{align*}
     \left(\frac{z^M - z^{-M}}{z - z^{-1}} \right)^2
     &=
     \sum_{\alpha, \beta \in \mathcal{A}}
     z^\alpha z^{\beta} \\
     &= \sum_{\alpha, \beta \in \mathcal{A}}
     \frac{z^{\alpha +\beta} + z^{-\alpha-\beta}}{2} \\
     &= \sum_{\alpha, \beta \in \mathcal{A}} \cos\b( \smfrac{\alpha+\beta}{2} x \b).
   \end{align*}
   Since $\alpha+\beta$ is always even, it follows that
   $(\frac{z^M - z^{-M}}{z - z^{-1}} )^2 \in \Trig_{M-1}$ and in particular
   $J_M \in \Trig_{2M-2}$.
\end{proof}

\begin{lemma} \label{th:trig:gammaM_bound}
  There exist $C_1, C_2 > 0$, independent of $M$ such that
  \[
    C_1 M^{-3} \leq  \gamma_M \leq C_2 M^{-3}
  \]
\end{lemma}
\begin{proof}
  First note the geometrically evident fact that
  \[
    x/\pi \leq \sin(x/2) \leq x/2.
  \]

  To obtain a lower bound, we estimate
  \begin{align*}
    \pi/\gamma_m &= \int_0^\pi \left( \frac{\sin( Mx/2)}{\sin(x/2)} \right)^4 dx \\
    &= \int_0^{\pi/M} \left( \frac{\sin( Mx/2)}{\sin(x/2)} \right)^4 dx
        + \int_{\pi/M}^\pi \left( \frac{\sin( Mx/2)}{\sin(x/2)} \right)^4 dx  \\
        %
    &\lesssim
        \bg[ \int_0^{\pi/M} \left( \frac{Mx/2}{x/2} \right)^4 \, dx
        + \int_{\pi/M}^{\pi}
             \bg( \frac{1}{x} \bg)^4 \, dx \bg] \\
    &= c_1 M^3.
  \end{align*}
  Note in particular that this calculation shows that for the opposite bound
  we only need to consider the interval $(0, \pi/M)$. Thus, we calculate
  \begin{align*}
    \pi/\gamma_M
    % &= \int_0^\pi  \left( \frac{\sin( Mx/2)}{\sin(x/2)} \right)^4 \, dx  \\
    &\geq \int_0^{\pi/M} \left( \frac{\sin( Mx/2)}{\sin(x/2)} \right)^4 \, dx \\
    &\gtrsim \int_0^{\pi/M} \left( \frac{Mx/2}{x/2} \right)^4 \, dx \\
    &=c_2 M^3. \qedhere
  \end{align*}
\end{proof}

The next Lemma is key, and hidden in its proof is the reason that we used the fourth power to define the Jackson kernel. It quantifies the fact (already exploited above) that $J_M$ is concentrated near the origin.

\begin{lemma} \label{th:trig:jackson_moments}
  There exists a constant $C > 0$ such that
  \begin{align*}
    \mint J_M(x) \,dx &= 1, \qquad \text{and}
    \\
    \mint |x| J_M(x) \, dx &\leq C M^{-1}.
  \end{align*}
\end{lemma}
\begin{proof}
  The case $m = 0$ follows immediately  from the normalisation of the
  Jackson kernel, $\int_{-\pi}^\pi J_M(x)\,dx = 1$.

  The case $m = 1$ can be seen by a variation of the proof of
  Lemma~\ref{th:trig:gammaM_bound}:
  \begin{align*}
    \int_0^\pi x J_M(x) \, dx
    &=
    \int_0^{\pi/M} x J_M(x) \, dx
        + \int_{\pi/M}^{\pi}
             x J_M(x) \, dx \\
    &\lesssim
        \gamma_M \bg[ \int_0^{\pi/M} x M^4 \, dx
        + \int_{\pi/M}^{\pi}
             x \bg( \frac{1}{x} \bg)^4 \, dx \bg] \\
    &\lesssim
      M^{-3} \b[ M^{2} + M^{2} \b]
      \lesssim M^{-1}.
      \qedhere
  \end{align*}
\end{proof}

To state the Jackson theorems we first need to adapt the notion of modulus of continuity to the torus: Namely, we require that $\omega$ is a modulus of continuous for $f$ on all of $\mathbb{R}$. It is clear that all properties of the modulus of continuity survive, including the fact that any $f \in C(\TT)$ has such a m.o.c.

\begin{theorem}[Jackson's Theorem] \label{th:trig:jackson}
  \begin{enumerate}
  \item Let $f \in C(\TT)$ with modulus of continuity $\omega$, then
  \[
      \| f - J_M \ast f \|_\infty \lesssim \omega(M^{-1})
  \]
  In particular, if $f \in C^{0,\sigma}(\TT)$, then
  \[
      \|f - J_M \ast f \|_\infty \lesssim N^{-\sigma},
  \]
  and if $f \in C^1(\TT)$, then
  \begin{equation} \label{eq:trig:jackson:C1-version}
    \|f - J_M \ast f \|_\infty \lesssim M^{-1} \|f'\|_\infty.
  \end{equation}
  \item Let $f \in C^p(\TT)$ and $f^{(p)}$ have modulus of continuity $\omega$,
  then
  \[
      \inf_{t_N \in \Trig_N} \| f - t_N \|_\infty
        \lesssim N^{-p} \omega(N^{-1}).
  \]
  % \[
  %     \| f - J_N\ast f \|_\infty \lesssim N^{-p} \omega(N^{-1}).
  % \]
  \end{enumerate}
\end{theorem}
\begin{proof}[Proof of Theorem~\ref{th:trig:jackson}(1)]
  Recall that the polynomial degree of $J_M \ast f$ is $N = 2M-2$. For $N$ even we take $M = N/2+1$ while for $N$ odd we take $M = (N+1)/2+1$. Either way, $N \geq 2M-2$.

  \begin{align*}
    \b| J_M \ast f(x) - f(x) \b|
    &=
    \bg| \int_{-\pi}^\pi \B( f(x-t) - f(x) \B) J_M(t) \, dt \bg| \\
    &\leq
    \int_{-\pi}^\pi \b|f(x-t) - f(x) \b| J_M(t) \, dt.
  \end{align*}
  Next, we can use the modulus of continuity to estimate
  \[
    \b|f(x-t) - f(x) \b| \leq \sum_{k = 1}^K
      \b| f(x - kt/K) - f(x - (k-1)t/K) \b|
    \leq K \omega(t/K)
    % \leq K \omega(\pi/K).
  \]
  Choosing $K$ minimal such that $t/K \leq 1/N$ (i.e. $K = \lceil t N \rceil$) yields
  \[
    \b|f(x-t) - f(x) \b| \leq
      \cases{
        \omega(N^{-1}), & 0 \leq |t| \leq N^{-1}, \\
        2 t N \omega(N^{-1}), & |t| > N^{-1}.
      }
  \]
  Using Lemma~\ref{th:trig:jackson_moments} we conclude
  \begin{equation} \label{eq:trig:jackson1_proof_result}
    \b| J_N \ast f(x) - f(x) \b|
    \leq
      \omega(N^{-1}) \int_0^{1/N} J_N(t) \, dt
      + 2 N \omega(N^{-1}) \int_{1/N}^{\pi} t J_N(t)\, dt
    \lesssim \omega(N^{-1}).
  \end{equation}
  The stated results follow immediately from the fact that $\omega(N^{-1}) \leq \omega(M^{-1})$. For later reference though we will also need \eqref{eq:trig:jackson1_proof_result}.
\end{proof}

To prove Theorem~\ref{th:trig:jackson} (2), we need another auxiliary
results that is also of independent interest.

\begin{lemma} \label{th:trig:jackson-auxEN}
  Let $E_N(f) := \inf_{t_N \in \Trig_N} \|f - t_N \|_\infty$, then for
  $f \in C^1(\TT)$ we have
  \[
    E_N(f) \lesssim N^{-1} E_N(f').
  \]
\end{lemma}
\begin{proof}
  Let $q \in \Trig_N$ such that
  \[
    \|f' - q\|_\infty = E_N(f').
  \]
  (Because $\Trig_N$ is finite-dimensional, we know the best approximation
  error is attained.) Then we can write
  \[
    q(x) = \sum_{k = -N}^N \hat{q}_{k} e^{ikx}.
  \]
  We wish to write $q =  t_N'$, but this is in general false if $\hat{q}_0 \neq 0$. Instead, we split
  \[
    q(x) = \hat{q}_0 + r(x),
  \]
  then $\hat{r}_0 = 0$ and hence there exists $t \in \Trig_N$ such  that $t' =  r$. Moreover, we can estimate
  \[
    |\hat{q}_0| = \bg| \mint_{-\pi}^\pi q \,dx \bg|
      = \bg| \mint_{-\pi}^\pi (q-f') \,dx \bg|
      \leq E_N(f').
  \]
  Combining these manipulations we obtain
  \[
    \| f' - t'\|_\infty
    \leq
    \| f' - q \|_\infty + |\hat{q}_0|
    \lesssim 2 E_N(f').
  \]

  Finally, since $t \in \Trig_N$ we have $E_N(f) = E_N(f - t)$ and can therefore conclude, using Jackson's first theorem,
  \[
    E_N(f) = E_N(f - t)
    \lesssim N^{-1} \|f' - t'\|_\infty
    \lesssim N^{-1} E_N(f').
  \]
  where we also used that $r \mapsto \|f'-t'\|_\infty r$ is the modulus of continuity for $f - t$.
\end{proof}

\begin{proof}[Proof of Theorem~\ref{th:trig:jackson} (2)]
  According to Lemma~\ref{th:trig:jackson-auxEN},
  \[
    E_N(f) \lesssim N^{-1} E_N(f') \lesssim \dots \lesssim
    N^{-p} E_N(f^{(p)}),
  \]
  and according to Jackson's first theorem, for some $M \sim N$,
  \[
    E_N(f^{(p)}) \leq \| f^{(p)} - J_M \ast f^{(p)} \|_\infty
      \leq \omega(M^{-1}) \lesssim \omega(N^{-1}),
  \]
  that is, $E_N(f) \lesssim N^{-p} \omega(N^{-1})$.
  %
  % Note that this does not yet prove our statement. To prove
  % Theorem~\ref{th:trig:jackson} (2), let $t_N \in \Trig_N$ such that
  % $\|f - t_N \|_\infty \leq C N^{-p} \omega(N^{-1})$, then we have
  % \[
  %   \| f - J_M \ast f \|_\infty
  %     \leq \| f - t_N \|_\infty + \| J_M \ast (f - t_N) \|_\infty
  %         + \|t_N - J_M \ast t_N \|_\infty
  % \]
\end{proof}

\subsection{Interpolation}
%
\label{sec:trig:interp}
%
We have discussed two strategies to construct approximations of functions by
trigonometric polynomials: $L^2$-projection and convolution (e.g., with the
Jackson kernel). While both appear to be constructive, they both require additional
computational effort to evaluate the relevant integrals. Since this is normally
done via numerical quadrature, additional errors will be introduced that
need to be analysed separately. All this can be done, but it turns out that
a much more practical and performant approach that gives ``near-optimal''
approximants (most of the time) is nodal interpolation.

To specify a trigonometric polynomial $t \in \Trig_N$ we need to determine
$2N+1$ coefficients, which should be possible using $2N+1$ function values,
i.e., we may choose $2N+1$ nodes $x_0, \dots, x_{2N} \in (-\pi, \pi]$
and specify
\[
    t(x_j) = F_j,
\]
with $F_j$ some prescribed function values. If the $x_j$ are distinct, then it
is easy to prove (see below and Exercise~\ref{exr:poly:interpunique}) If $F_j =
f(x_j)$ for some $f \in C(\TT)$ the we call the resulting $t$ a {\em nodal
interpolant}.

An important question is how we can transform the nodal values into coefficients
for the trigonometric polynomial. Naively, this can be achieved by simply
solving a linear system for the coefficients at $O(N^3)$ cost:
Let $t(x) = \sum_{k = -N}^N \hat{F}_k e^{ik x}$, then
\begin{equation} \label{eq:trig:pre-dft}
  \sum_{k = -N}^N \hat{F}_k e^{i\pi x_j} = F_j.
\end{equation}

% It is straightforward to see (we will return to this in \S~\ref{sec:trig:fft}
% that the inversion formula is
% \begin{equation} \label{eq:trig:pre-idft}
%     \hat{F}_k = \frac{1}{2N} \sum_{j = -N+1}^N F_j e^{-i\pi k/N},
% \end{equation}
% that is, the linear system \eqref{eq:trig:pre-dft}  has an orthogonal (up to
% scaling) which reduces the solution of the linear system matrix reduces the
% solution of \eqref{eq:trig:pre-dft} to a matrix-vector multiplication
% \eqref{eq:trig:pre-idft} and hence $O(N^2)$ cost. But it turns out that there is
% even an $O(N \log N)$ algorithm - the Fast Fourier Transform. To present this
% important algorithm it is more convenient if we work with $2N$ interpolation
% nodes, instead of $2N+1$ nodes. This makes the theory of interpolation subtly
% different, since with $2N$ conditions we can no longer hope to determing $2N+1$
% coefficients.

The first question to ask then is how to choose the interpolation nodes. Because
on the torus no part of the domain is ``special'', it seems intuitive that
without any additional information about the target function equispaced nodes
must be ``generically optimal''. We will prove in the remainder of this section
that it is in fact optimal up to a logarithmic factor, but will also return to a
more careful discussion of different choices of interpolation nodes in
\S~\ref{sec:poly}. Moreover, equispaced nodes also lead to fast algorithms (FFT)
for solvig \eqref{eq:trig:pre-dft}, but this requires some specific choices and
some slightly annoying, but actually natural, modification of our approximation
space.

The most common implementation of trigonometric interpolation employs
\[
  x_j =  \frac{j \pi}{N},  \qquad j \in \Z.
\]
The $x_j$ are called {\em interpolation nodes}. They depend on $N$ of course, but we
supress this dependence for the sake of simplicity of notation. Unfortunately,
for a $2\pi$-periodic function, the nodes $x_j, x_{j+2N}$ are equivalent, i.e.,
there are only $2N$ independent nodes and hence only $2N$ interpolation conditions.
Thus, we cannot determine the $2N+1$ parameters of a $t_N \in \Trig_N$ but must
modify our approximation space.  To determine a trigonometric polynomial we may, for example,
drop the $e^{-iNx}$ basis function from $\Trig_N$, which leads to
interpolants of the form
\[
    t(x) = \sum_{k = -N+1}^N c_k e^{ik x}.
\]
But unless $c_N = 0$, this will mean that $t(x) \not\in \R$ even if all $f_j \in
\R$.

But why did we drop $e^{-iNx}$ from the basis and not $e^{i N x}$? It actually turns out that it doesn't matter since those two basis functions agree on the interpolation nodes $x_j = j \pi / N$:

\begin{lemma} \label{th:trig:baby-aliasing}
  Let $x_j = j \pi / N$, then $e^{iN x_j} = e^{-iNx_j}$ for all
  $j \in \Z$.
\end{lemma}
\begin{proof}
  \[
    e^{iNx_j} = e^{i \pi j} = (-1)^j = (-1)^{-j} = e^{-i\pi j} = e^{-iNx_j}.
    \qedhere
  \]
\end{proof}

A simple way therefore to proceed is to replace $e^{i N x}, e^{-i N x}$ with their mean, i.e. $\cos(Nx)$ which  leads to the following modified trigonometric polynomial space
\[
  \Trig_N' := {\rm span}\Big(\Trig_{N-1} \cup \{ \cos N x \} \Big)
    =  \bg\{ t(x) = \sum_{k = -N+1}^{N-1} c_k e^{ikx} + c_N \cos(Nx) \bg\}.
\]
Note, however, that for $x = x_j$ only we can equally write
\[
  t(x_j) = \sum_{k = -N+1}^{N-1} c_k e^{ikx_j} + c_N \cos(Nx_j)
     = \sum_{k = -N+1}^{N} c_k e^{ikx_j},
\]
which will be convenient in the following.

Finally, to prepare us for discussing the FFT in the next section, we will
fix the interpolation condition to be nodes $x_0, \dots, x_{2N-1}$. This may seem
counterintuitive given we used $(-\pi, \pi]$ as the domain until now, but it is
the most common convention and therefore we shall adopt it as well.

\begin{lemma}
  Let $F  = (F_j)_{j = 0}^{2N-1} \in \C^{2N}$, then there exists a unique
  $t \in \Trig_N'$ such that
  \[
    t(x_j) = F_j, \qquad j = 0, \dots, 2N-1.
  \]
\end{lemma}
\begin{proof}
  According to Lemma~\ref{th:trig:baby-aliasing} we need to solve
  \begin{align*}
      && \sum_{k = -N+1}^N c_k e^{i\pi k j/N} &= F_j \\
      \Leftrightarrow &&
      \sum_{k = -N+1}^N c_k \big(e^{i\pi j/N}\big)^k &= F_j \\
      \Leftrightarrow &&
      \sum_{k = -N+1}^N c_k z_j^k &= F_j, \\
      \Leftrightarrow &&
      \sum_{k = -N+1}^N c_k z_j^{k+N-1} &= F_j z_j^{N-1},
  \end{align*}
  where $z_j = e^{i\pi x_j}$ are distinct complex interpolation nodes. Existence
  and uniqueness of algebraic polynomial interpolation gives the stated result.
  (cf. Exercise~\ref{exr:poly:interpunique}).

  {\it REMARK: } the last line in the above chain was unnecessary, but we will
  revisit this later.
\end{proof}

\medskip

\begin{definition}
  Let $f \in C(\TT)$ then we define $I_N f \in \Trig_N'$ to be the unique nodal
  interpolant of $f$ at the nodes $x_j = \pi j / N, j \in \Z$, i.e., $I_N f(x_j)
  = f(x_j)$ for $j \in \Z$.
\end{definition}

\medskip

To understand the approximation error of the $I_N f$, let $f \in C(\TT)$, $t_N
\in \Trig_N'$ arbitrary, then
\begin{align*}
  \|f - I_N f \|_\infty &\leq \|f - t_N \|_\infty + \| t_N - I_N f\|_\infty \\
    & = \|f - t_N \|_\infty + \| I_N (t_N - f) \|_\infty \\
    & \leq (1 + \|I_N\|) \| t_N - f \|_\infty,
\end{align*}
where $\|I_N\|$ is the operator norm of $I_N$ associated with $\|\cdot\|$,
defined by
\[
    \| I_N \| = \sup_{\substack{f \in C(\TT) \\ \|f\|_\infty = 1}} \| I_N f \|_\infty.
\]
Taking the infimum over all $t_N \in \Trig_N$ we obtain that the
interpolation error deviates from the best approximation error by
factor determined by the operator norm of $I_N$, i.e.,
\[
    \|f - I_N f \| \leq (1 + \|I_N\|) \inf_{t_N \in \Trig_N'} \|f - t_N \|
    \leq (1 + \|I_N\|) \inf_{t_{N-1} \in \Trig_{N-1}} \|f - t_{N-1} \|,
\]
where the final inequality of course shows that the convergence rate does not
change asymptotivally from that in $\Trig_N$ except possibly for a constant
factor.

\begin{definition}
  The interpolation operator norm is also called {\em Lebesgue constant}, and
  typically denoted by $\Lambda_N = \| I_N \|$.
\end{definition}

\begin{remark}
  The above argument works in principle with {\em any} norm. But to obtain a
  finite bound that norm must be such that $C(\TT)$ is complete under it. If
  not, then $\|I_N\|$ becomes infinite. As an exercise, you may check that the
  $L^2$-operator norm, $\|I_N\|_{L(L^2)}$ is indeed infinite. This is not
  surprising since functions $f \in L^2$ do not even have well-defined point values.
\end{remark}

To estimate $\Lambda_N$ we wish to write $I_N f$ in terms of a {\em nodal
basis}, i.e.,
\[
  I_N f(x) = \sum_{j = -N+1}^N f(x_j) L_j(x),
\]
where $x_j = \pi j / N$, then we can simply estimate
\begin{align} \label{eq:trig:LamNbound}
  \Lambda_N \leq \sup_{x \in \TT}  \sum_{j = -N+1}^N |L_j(x)|.
\end{align}

An immediate observation is that, since the grid is translation invariant, the
nodal basis will be translation invariant as well, i.e., $L_j(x) = L_0(x -
x_j)$. This already gives us a hint what to look for.

\begin{lemma}
  The nodal basis for trigonometric interpolation (for an even number of
  grid points $2N$) is given by a modified Dirichlet kernel,
  \[
    % L_j(x) = \frac{\sin\b( N (x-x_j) \b)}{N \tan\b( \smfrac12 (x-x_j)\b)}
    %     = \frac{{\rm sinc}\b( N (x - x_j)\b)}{N {\rm sinc}\b( \smfrac12 (x-x_j)\b)}
    %         \cos\b( \smfrac12 (x-x_j) \b).
    L_j(x) = D_N'(x-x_j)
  \]
  where
  \[
    D_N'(x) = \frac{\sin(Nx)}{2N \tan(x/2)}.
  \]
\end{lemma}
\begin{proof}
  To see the identity for $D_N'$ simply use $\sin(\alpha+\beta) =
      \sin\alpha\cos\beta + \cos\alpha\sin\beta$. From the definition of $D_N'$
      it is straightforward to check that $L_j(x_i) = \delta_{ij}$. For the case
      $i = j$ this is a limit argument: (fill in the details!)
  \[
    \lim_{x \to x_j} L_j(x) = 1.
  \]
  Thus we ``only'' need to show that $L_j$ is indeed a trigonometric polynomial,
  or equivalently, $D_N \in \Trig_N$. This is achieved by an analogous argument
  as for the Jackson kernel.

  See also Exercise~\ref{exr:trig:dirichlet} for the Dirichlet kernel related to
  $\Trig_N$ and how it relates to $L^2$-projection.
\end{proof}

See \nbtrig for a numerical exploration of $\Lambda_N := \|I_N\|_{\rm op}$. The
numerical experiments shown there suggest that the following theorem holds.

\begin{theorem} \label{th:trig:lebesgue}
  The Lebesgue constant for trigonometric interpolation with respect to the
  $L^\infty$-norm is bounded by
  \[
    \|I_N\| \leq \smfrac{2}{\pi} \log (N+1) + 2.
  \]

  In particular, if $f \in C(\TT)$, then
  \begin{equation} \label{eq:trig:almost_best_approx_IN}
      \| f - I_N f \|_{L^\infty}
        \lesssim \log N \inf_{t_N \in \Trig_N'} \| f - t_N \|_{L^\infty}.
  \end{equation}
  If $f$ is real we can replace $\Trig_N'$ with $\Trig_N$ in \eqref{eq:trig:almost_best_approx_IN}.
\end{theorem}

\begin{remark}
  This bound is close to sharp. Erd\"{o}s (1961) proved  that
  \[
      \frac{2}{\pi} \log N - c_1 \leq \Lambda_N \leq \frac{2}{\pi} \log N + c_2.  \qedhere
  \]
\end{remark}

\begin{proof}
  Recall
\eqref{eq:trig:LamNbound}, then we need to bound
  \begin{align*}
    \Lambda_N
    &\leq
    \sum_{j =  -N+1}^N |D_N'(x - x_j)|.
  \end{align*}
  By translation invariance and reflection symmetry we only need to consider $x
  = -t$, $t \in (0, \frac{\pi}{2N})$ (the case $t = 0$ is trivial); in this
  case,
  \begin{align*}
    \Lambda_N
    &\leq
    \sum_{j =  -N+1}^N |D_N'(x - x_j)| \\
    &\leq
    \sum_{j =  0}^N |D_N'(x_j+t)|  + \sum_{j = -N+1}^{-1} \dots \\
    &\leq
    \frac{1}{2N} \Bg\{
        \frac{\sin(Nt)}{\tan(t/2)}
        + \sum_{j = 1}^N
        \bg|\frac{\sin\b(N(x_j+t)\b)}{\tan\b((x_j+t)/2\b)}\bg|
      \Bg\} + \dots \\
    &\leq
    \frac{1}{2N} \bg\{
        \frac{Nt}{t} + \sum_{j = 1}^N \frac{2}{ x_j+t }
    \bg\} + \dots \\
    &\leq
    1 + \frac{1}{\pi} \sum_{j = 1}^N \frac{1}{j} + \dots \\
    &\leq
    2\Big( 1 + \smfrac{1}{\pi} \log(N+1) \Big)
    \leq 2 + \smfrac{2}{\pi} \log(N+1).
  \end{align*}
  (Or, we could use $\sum_{j = 1}^N \frac{1}{j} \sim \log N + \gamma + O(1/N)$ to sharpen this a bit more.)

  The last statement is left as an exercise.
\end{proof}

\subsection{The Fast Fourier Transform}
%
\label{sec:trig:fft}
%
As a final topic on the theme of trigonometric polynomial approximation we will
study how to work efficiently with trigonometric interpolants. This is achieved
via the discrete Fourier transform and its fast implementation, the {\it Fast
Fourier Transform}, likely one of the most important and most widely used
numerical algorithms. We will assume from here on that the number of grid points
is even.

Given a function $f \in C(\TT)$ we can evaluate it at grid points $x_j$ which
leads to a grid function $F_j = (f(x_j))_{j=0}^{M-1}$. Given $M \in 2\N$ it is
common to define the DFT and FFT for the grid
%
\[
    x_j = \frac{2\pi j}{M} \qquad j = 0, \dots, M-1.
\]
%
The assumption that $M = 2N$ is even is consistent with
\S~\ref{sec:trig:interp}.
% In our notation up to now it would have been more
% natural to write $x_j = -\pi + \pi j/N$ instead, but since we are considering
% periodic functions we just need to shift them into a new domain $[0, 2\pi)$.
% Although we could initially avoid some inconveniences we want to eventually be
% able to use the FFT algorithms, so we may as well learn now how to convert
% between the two representations.

We then ask, what are the coefficients of the trigonometric polynomial $t_N \in
\Trig_{N}' = \Trig_{M/2}'$ such that
%
\[
  t_N(x_j) = F_j \qquad \text{for } j = 0, \dots, M-1,
\]
%
or, written as a linear system,
\begin{equation} \label{eq:trig:pre-dft-equi}
  \sum_{k = -N+1}^N \hat{F}_k e^{i \pi j k / N} = F_j, \qquad j = 0, \dots, 2N-1.
\end{equation}
The first important observation is that the system matrix is orthogonal up to a constant factor; that is, the explicit inversion formula is given by
\begin{equation} \label{eq:trig:pre-idft}
    \hat{F}_k = \frac{1}{2N} \sum_{j = -N+1}^N F_j e^{-i\pi k j /N},
\end{equation}
This is not at all surprising given that close analogy of the sums with the integrals in the definition of the Fourier coefficients.

\begin{exercise}
  Prove \eqref{eq:trig:pre-idft}. The key intermediate result you should extract is
  \begin{equation} \label{eq:trig:orth-dft}
    \sum_{j = 0}^{2N-1}
    e^{i \pi j k' / N} e^{- i \pi j k / N} = 2 N \delta_{kk'},
  \end{equation}
  which is precisely the orthogonality of the system matrix.
\end{exercise}

% that is, the linear system \eqref{eq:trig:pre-dft}  has an orthogonal (up to
% scaling) which reduces the solution of the linear system matrix reduces the
% solution of \eqref{eq:trig:pre-dft} to a matrix-vector multiplication
% \eqref{eq:trig:pre-idft} and hence $O(N^2)$ cost. But it turns out that there is
% even an $O(N \log N)$ algorithm - the Fast Fourier Transform. To present this
% important algorithm it is more convenient if we work with $2N$ interpolation
% nodes, instead of $2N+1$ nodes. This makes the theory of interpolation subtly
% different, since with $2N$ conditions we can no longer hope to determing $2N+1$
% coefficients.

We will call the mapping $F \mapsto \hat{F}$ the discrete Fourier transform (DFT) and its inverse the IDFT: for $F \in \C^{M}$, $k = 0, \dots, M-1$,
%
\begin{align}
  \label{eq:trig:dft}
  {\rm DFT}[F] := \hat{F}, \quad \text{where} \quad
  \hat{F}_k &= \frac{1}{M} \sum_{j = 0}^{M-1} F_j e^{-i x_j k} \\
  \notag
            &= \frac{1}{M} \sum_{j = 0}^{M-1} F_j e^{-i 2\pi j k / M}.
\end{align}
%
Note in particular that this is a trapezoidal rule approximation of
\eqref{eq:trig:fourier coeffs}.

\begin{remark} \label{rem:trig:k-grid}
  Since $x_j = 2 \pi j/ M$ it follows that
  \[
    e^{-i x_j (k \pm M)} = e^{-i x_j k}
  \]
  and hence the $k$-grid $\{0, \dots, M-1\}$ can alternatively be interpreted
  as, with $N = M/2$,
  \[
    \{ 0, \dots, N, -N+1, -N+2, \dots, -1 \}.
  \]
  This is the ordering normally adopted in implementations of the DFT and IDFT.
\end{remark}

\begin{proposition} \label{th:trig:dft}
  Let the ${\rm IDFT}$ be defined by
  \begin{equation} \label{eq:trig:idft}
    U = {\rm IDFT}[\hat{U}], \quad \text{where} \quad
    U_j := \sum_{k = 0}^{M-1} \hat{U}_k e^{i x_j k}
        = \sum_{k = 0}^{M-1} \hat{U}_k e^{i 2\pi j k/ M},
  \end{equation}
  then
  \[
    {\rm IDFT}\big[ {\rm DFT}[F] \big] = F \qquad \forall F \in \C^M.
  \]
  In particular, if $\hat{F} = {\rm DFT}[F]$, then the two trigonometric
  polynomials (cf. Remark~\ref{rem:trig:k-grid}) $t \in \Trig_N, t' \in
  \Trig_N'$
  \begin{align*}
    t(x) &= \sum_{k = 0}^{M-1} \hat{F}_k e^{i k x} \\
    t'(x) &= \sum_{k = 0}^{M/2-1} \hat{F}_k e^{i k x}
          + \hat{F}_{M/2} \cos(M/2 x) + \sum_{k = M/2+1}^{M-1} \hat{F}_k e^{i k x} \\
  \end{align*}
  interpolate $(x_j, F_j)_{j = 0}^{M-1}$, i.e.,
  \[
    t(x_j) = t'(x_j) = F_j \qquad \text{for } j = 0, \dots, M-1.
  \]
\end{proposition}
\begin{proof}
  Left as an exercise.
\end{proof}

Using expression \eqref{eq:trig:dft} the cost of computing ${\rm DFT}[F]$ is
$O(N^2)$. Indeed, this is the cost of a generic matrix-vector multiplication,
i.e., applying a linear operation in $\R^N \to \R^N$ that has no special
structure. Luckily the ${\rm DFT}$ has plenty of structure to exploit, which
finally brings us to the FFT algorithm (specifically the radix-2 variant of
Cooley--Tukey's algorithm, though the idea famously goes back to Gauss).

We begin by rewriting
\[
  \hat{F}_k = M^{-1} \sum_{j = 0}^{M-1} F_j \omega^{kj},
  \qquad \text{where} \quad \omega := e^{-i 2\pi/M}.
\]
Then,
\begin{align}
  \notag
  \hat{F}_k  &= \sum_{j = 0}^{M/2-1} F_{2j} \omega^{2kj}
      + \sum_{j = 0}^{M/2-1} F_{2j+1} \omega^{k(2j+1)} \\
  \notag
    &= \sum_{j = 0}^{M/2-1} F_{2j} \omega^{2kj}
        + \omega^k \sum_{j = 0}^{M/2-1} F_{2j+1} \omega^{2kj} \\
  \label{eq:trig:fft_split}
    &=: \hat{G}_k + \omega^k \hat{H}_k.
\end{align}
In particular, since $\omega^2 = e^{-i2\pi/(M/2)}$, we note that $\hat{G}_k$ is
the DFT of $(F_{2j})_{j=0}^{M/2-1}$, while $\hat{H}_k$ is the DFT of
$(F_{2j+1})_{j=0}^{M/2-1}$.

A final remark is that, {\it a priori} $\hat{G}_k$ and $\hat{H}_k$ will be given
only for $k = 0, \dots, M/2-1$, but the expressions are $M/2$-periodic and
\eqref{eq:trig:fft_split} allows us to recover $\hat{F}$ for all $k = 0, \dots,
M-1$. Specifically, we obtain the following identity:
%
\begin{equation} \label{eq:trig:fft_trick}
  \begin{split}
    \hat{F}_k &= \hat{G}_k + \omega^k \hat{H}_k, \qquad k = 0, \dots, M/2-1, \\
    \hat{F}_k &= \hat{G}_{k-M/2} - \omega^{k-M/2} \hat{H}_{k-M/2},
      \qquad k = M/2, \dots, M-1.
  \end{split}
\end{equation}
(We could also write $\omega^k$ instead of $\omega^{k-M/2}$; this is
equivalent.)
% The case $k = 0, \dots, N/2-1$ is already clear from
% \eqref{eq:trig:fft_split}. For $k > N/2-1$ it follows from the
% $N/2$-periodicity of $\hat{G}_k, \hat{H}_k$, i.e.,
% \[
%   \hat{G}_{k+N/2} = \hat{G}_k, \qquad \text{and} \qquad
%   \hat{H}_{k+N/2} = \hat{H}_k,
% \]
% as well as
% \[
%   \omega^{N/2} = e^{-i2\pi(N/2)/N} = e^{-i\pi} = -1.
% \]

Suppose now that $M/2$ is still divisible by 2, then the can split the
computation of $\hat{F}, \hat{G}$ again into four smaller DFTs. This process can
of course be iterated. If $M = 2^m$, then after $m \approx \log M$ iterations
iterations we compute $\approx M$ DFTs of length $O(1)$. Combining the small
DFTs into the larger DFTs requires $O(M)$ operations at each level. Since there
are $O(\log M)$ levels, this means that the cost of computing the original DFT
is $O(M \log M)$. Algorithms that use some variant of this strategy are called
{\em Fast Fourier Transform}s.

% \subsection{Examples}
% %
% We are now fully equipped to applying trigonometric polynomial approximation for
% numerical simulation. We will consider
% \begin{itemize}
%   \item a linear, homogeneous boundary value problem
%   \item a transport equation with variable coefficients
%   \item a filtering problem
% \end{itemize}
% These examples may be found in \nbtrig.

\subsection{Exercises}

\begin{exercise} \label{exr:trig:hilbert-onb}
  \begin{enumerate} \ilist
    \item Recall the definition of a complex Hilbert space and
        check that $( L^2(\TT), \< \cdot, \cdot \>_{L^2(\TT)} )$ is indeed
        a pre-Hilbert space, i.e. check all conditions except for completeness.
        (Completeness is a bit more involved, but it is not particularly
        difficult; feel free to look this up  in a suitable textbook.)

    \item Complete the proof of the Plancherel Theorem; i.e.
      Theorem~\ref{th:trig:plancherel}(ii).

    \item Using Jackson's theorem, prove also
    Theorem~\ref{th:trig:plancherel}(i).

    {\it Hint: use the fact that $\Pi_N$ is an orthogonal projector and
    in particular has operator norm 1.}
  \end{enumerate}
\end{exercise}

\begin{exercise} \label{exr:trig:convergence_L2}
  Complete the proof of Theorem~\ref{th:trig:convergence_L2}.
\end{exercise}

\begin{exercise} \label{exr:trig:functions}
  For the following functions $f$, categorize their regularity as closely as
  possible and estimate the rate of convergence of $\|f-\Pi_N f\|_{L^2}$.
  \begin{enumerate} \ilist
    \item  $f(x) = \sin(x)$
    \item  $f(x) = \sin(x/2)$
    \item $f(x) = |\sin(x)|$
    \item $f(x) = |\sin(x)|^3$
    \item $f(x) = (1 + c^2 \sin^2 x)^{-1}$
    \item $f(x) = \exp( - \sin(x))$
    \item $f(x) = \exp( - 1 / (1-x^2) ) \chi_{(-1,1)}(x)$, extended $2\pi$-periodically to $\R$.
    % x^2 < 2.49999^2 ? exp(3 - 3 / (1-(x/2.5)^2)) : 0.0
  \end{enumerate}
  Can you sharpen your estimates after working through
  Exercise~\ref{exr:trig:gibbs}?
\end{exercise}

\begin{exercise}[Gibbs Phenomenon] \label{exr:trig:gibbs}
  Consider the periodic, piecewise constant function
  \[
      f(x) = \cases{
        1, & x \in (0, \pi], \\
        -1, & x \in (-\pi, 0].
      }
  \]
  \begin{enumerate} \ilist
  \item Prove that, there exists no sequence of trigonometric polynomials
  $t_N \in \Trig_N$ such that $t_N \to f$ uniformly, but that
  \[
    \|\Pi_N f - f\|_{L^2} \to 0 \qquad \text{as } N \to \infty.
  \]

  \item Show that the Fourier series for $f$ is given by
  \[
    \Pi_N f(x) = \frac{4}{\pi} \sum_{\substack{j = 1 \\ j \text{ odd}}}^{N}
        \frac{\sin(jx)}{j}.
  \]

  \item Deduce that
  \[
      \| \Pi_N f - f \|_{L^2} \lesssim N^{-1/2}.
  \]

  \item {\bf Gibbs Phenomenon: } Prove that
  \[
    \lim_{N \to \infty} \Pi_N f\b(\smfrac{\pi}{N} \b) > 1
  \]
  You may use without proof that
  \[
      \int_0^\pi \frac{\sin(t)}{t} \,dt \approx
      \frac{\pi}{2} + \pi \cdot (0.089489\dots).
  \]

  {\it If you plot $\Pi_N f$ you will observe oscillations around the
  discontinuity. This ``picture'' is what is commonly known as the Gibbs
  phenomenon. It is a special case of {\bf ringing artefacts}, which are
  a common occurance when piecewise smooth data is approximated using
  global basis functions. This can be nicely visualised in image processing;
  see e.g. {\tt https://en.wikipedia.org/wiki/Ringing\_artifacts}.}

  \item {Piecewise smooth functions: } Make an educated guess what the
  rate of convergence is for $\|\Pi_N f - f \|_{L^2}$ when all derivatives up to $f^{(p-2)}$ are continuous, $f^{(p-2)}$ is Lipschitz (and in particular absolutely continuous) and $f^{(p-1)}$ is piecewise absolutely continuous with jump discontinuities at finite many points. This includes
  functions such as $|\sin(nx)|, |\sin(nx)|^q$ for $q$ odd.

  Adapt the proof of Theorem~\ref{th:trig:decay} to rigorously prove this.
  %
  \qedhere
  \end{enumerate}
\end{exercise}

\begin{exercise}[Dirichlet Kernel] \label{exr:trig:dirichlet}
  \begin{enumerate} \ilist
    \item Prove that
    \[
      D_N(x) = \frac{\sin\b((N+1/2) x\b)}{\sin(x/2)}
            = 1 + 2 \sum_{k = 1}^N \cos(k x)
            = \sum_{k = -N}^N e^{ikx}.
    \]
    \item Deduce that,
    \[
      (D_N \ast e^{in \bullet})(x) =
        \cases{
          e^{inx}, & -N \leq n \leq N, \\
          0, & \text{otherwise}
        }
    \]
    \item Deduce that, if $f \in L^1(\TT)$, then
    \[
        D_N \ast f = \Pi_N f.
    \]
    \item Show that $\|D_N\|_{L^1} \lesssim \log N$ and hence
    \[
        \| D_N \ast f \|_{L^\infty} \leq \|D_N \|_{L^1} \|f\|_\infty
          \lesssim \log N \|f\|_\infty.
    \]
    {\it HINT: to estimate $D_N$ use a similar splitting into sub-intervals
    as in the Jackson kernel estimates.}
    \item Deduce that
    \[
        \| f - \Pi_N f \|_{\infty}
        \lesssim  \log N \inf_{t_N \in \Trig_N} \| f - f_N \|_\infty,
    \]
    and in particular, if $f \in C^p(\TT)$ and $f^{(p)}$ has modulus of
    continuity $\omega$, then
    \[
        \| f - \Pi_N f \|_\infty \log N N^{-p} \omega(N^{-1}). \qedhere
    \]
    % Hint: use the fact that $\Pi_N t_N = t_N$ for all $t_N \in \Trig_N$.

  \end{enumerate}
\end{exercise}

\begin{exercise} \label{exr:trig:periodic extension}
  Let $f \in A(\TT)$. Prove that there exists $\alpha > 0$ such that $f$ has an
  analytic extension to $\Omega_\alpha$. Further, show that this extension
  (still called $f$) must be $2\pi$-periodic, i.e.,
  \[
      f(x + i y) = f(x + 2\pi + i y) \qquad \forall x+iy \in \Omega_\alpha. \qedhere
  \]
\end{exercise}

\begin{exercise}[The Exponentially Convergent Trapezoidal Rule]
  \label{exr:trig:trapezoidal rule}
  Let $f \in A(\TT)$, and consider the trapezoidal rule approximation
  of $I[f] := \mint_{-\pi}^\pi f\,dx$;
  \[
    Q_N[f] := \frac{1}{2N} \sum_{j = -N+1}^N f(x_j),
  \]
  where $x_j := j\pi/N$.
  %
  \begin{enumerate} \ilist
    \item Prove that,
    \[
        \frac{1}{2N} \sum_{j = -N+1}^N e^{ikx_j} =
          \cases{
              1, & k \in 2N \Z, \\
              0, & \text{otherwise.}
          }
    \]

    \item Suppose $f$ is analytic in $\Omega_\alpha$, where $\alpha > 0$ is
    maximal. Derive a sharp convergence rate for $|Q_N[f] - I[f]|$.
    {\it (You may of course revisit our sketches from the introductory lecture.)}

    \item {\it Poisson's example: } The perimeter of an ellipse with axis
    lengths $1/\pi, 0.6/\pi$ is given by the integral
    \[
        I = \frac{1}{2\pi} \int_{-\pi}^\pi \sqrt{1 - 0.36 \sin^2\theta}\,d\theta.
    \]
    {\it (You may justify this, but this is not required.)}
    \begin{itemize}
        %
      \item Compute the region of analyticity for $f(\theta) = \sqrt{1 - 0.36
      \sin^2\theta}$, hence prove a rate of convergence for $Q_N[f]$.
      {\it (For this problem, you also need to estimate the prefactor!)}
      %
      \item Only solve one of the following two problems:

      (OPTION 1) How many terms to you need to obtain 3, 5, 7 digits of accuracy?
      Using only a calculator, compute $I[f]$ to within 3 digits of accuracy.
      How many ``non-trivial'' function evaluations did you need?

      (OPTION 2) numerically demonstrate the convergence (use Julia, Matlab, Python or any language you wish.) \qedhere
    \end{itemize}
  \end{enumerate}
\end{exercise}

% \begin{exercise}[Spectral Differentiation]
% \end{exercise}

\begin{exercise}
  Prove Proposition~\ref{th:trig:dft}.
\end{exercise}

\begin{exercise}
  {\bf Radix-3 FFT: } Instead of $M$ even suppose that $M = 3 M'$ (you may
  actually still assume that $M$ is even for consistency with our treatment of
  trigonometric interpolation, but this is not really relevant here). Generalise
  the FFT to this case, i.e., derive the analogues of \eqref{eq:trig:fft_split}
  and \eqref{eq:trig:fft_trick}.
\end{exercise}


\lab{Application}{Depth and Breadth-First Searching with Kevin Bacon}{Depth and Breadth-First Searching with Kevin Bacon}
\label{lab:SixDegreesKevinBacon}

\objective{This section teaches about searching graphs and uses the parlor game ``the Six Degrees of Kevin Bacon'' as an application to graphs.}

\section*{Graphs}
\label{Graphs_section}
We commonly use graphs to represent the relationships between objects in a set. A graph is
represented by a set of nodes (objects) and a set of edges (relationships) where each edge
connects exactly two nodes. We would indicate an edge from node $x$ to node $y$ by $(x, y)$
and an edge from $y$ to $x$ by $(y, x)$. If \emph{all} of the edges within a graph are bidirectional
between their connecting nodes, the graph is said to be \emph{undirected}. If, however, the directions
of a graph's edges are specified, then the graph is said to be \emph{directed}. In this lab we will largely work
with undirected graphs.

There are two different data structures that can be used to represent graphs: adjacency matrices and adjacency lists.
Each data structure has its own advantages and disadvantages; the structure we use depends on the type of problem we are solving.

\begin{figure}[h]
\centering
\begin{tikzpicture}[auto,node distance=1.5cm,
 thick,main node/.style={circle,draw}]

  \node[main node] (1) {A};
  \node[main node] (2) [left of=1] {B};
  \node[main node] (3) [above of=1] {C};
  \node[main node] (4) [right of=1] {D};
  \node[main node] (5) [below of=1] {E};

  \path[every node/.style={font=\sffamily\small}]
     (1) edge node [] {} (4)
     	  edge node [] {} (3)
     	  edge node [] {} (5)
     (2) edge node [] {} (1)
     	  edge node []{}(3)
     (3) edge node []{}(4);
\end{tikzpicture}
\caption{Example Graph 1}
\label{Adjacency}
\end{figure}

\subsection*{Adjacency Matrices}
An adjacency matrix is two dimensional matrix that is used to represent a graph. To construct an adjacency matrix, we order
the nodes of the graph and allow each node to correspond to one column and one
row of the matrix. Therefore, if we have $n$ nodes, we use an $n \times n$ matrix to
represent the edges between the nodes. In an unweighted graph (a graph where all the edges have the same value), we indicate an
edge using either a $0$ or a $1$.
Consider Example Graph 1 in Figure \ref{Adjacency}. Because a bidirectional edge exists between A and D, for example, we put a $1$ in the $(3, 0)$ position and
in the $(0, 3)$ position. Since no edge exists between A and C, we put a $0$ in
the $(2, 0)$ position and $(0, 2)$ position. We can represent the adjacency matrix for this graph as follows:

\[
\bordermatrix{\hspace{.4cm}&A&B&C&D&E\cr
                A&0 & 1 & 0 & 1 & 1\cr
                B& 1 & 0 & 1 & 0 & 0\cr
                C& 0 & 1 & 0 & 1 & 0\cr
                D& 1 & 0 & 1 & 0 & 0\cr
                E& 1 & 0 & 0 & 0 & 0}\]
Note that because this graph is undirected, its adjacency matrix is symmetric.

\subsection*{Adjacency Lists}
An adjacency list links each node of the graph to a list of its corresponding neighbors, the nodes to which it is connected by an edge.
There are many ways to index the nodes so we can easily access their adjacent neighbors, but, in Python, one of the most straightforward ways is to use a simple dictionary.
We could represent Graph 1 using the following code:
\begin{lstlisting}
AList = {'A': ['B', 'D', 'E'], 'B': ['A', 'C'], 'C': ['B', 'D'], 'D': ['A', 'C'], 'E': ['A']}
# To obtain a list of the nodes adjacent to A, simply access the
# dictionary at the key 'A'.
print Alist['A']
# You should obtain the following output:
['B', 'D', 'E']
\end{lstlisting}
We can retrieve the neighbors of any node of the graph in constant time.
This makes algorithms that operate locally on the graph highly efficient.

\section*{Searching Graphs}
There are two common ways to search a graph, both of which present different advantages depending on the purpose of the search.
One method is called a depth-first search (DFS).  It is designed to search the deepest levels of a graph first.
The other method is called a breadth-first search (BFS).  A BFS searches a graph one level at a time until it finds a solution.

\begin{figure}[h]
\centering
\begin{tikzpicture}[auto,node distance=1.5cm,
 thick,main node/.style={circle,draw}]
\centering
  \node[main node](1) at (-1.25,1.25) {A};
  \node[main node](2) at (.75,1.5)  {B};
  \node[main node](3) at (0,0) {C};
  \node[main node](4) at (-1.6,-.75) {D};
  \node[main node](5) at (1.5,0)  {E};
  \node[main node](6) at (-.2,-1.5)  {F};

  \path[every node/.style={font=\sffamily\small}]
  	(2) edge node[]{}(1)
  	(4) edge node[]{}(1)
  	     edge node[]{}(3)
  	     edge node[]{}(6)
	(6) edge node[]{}(3)
	     edge node[]{}(5);
\end{tikzpicture}
\caption{Example Graph 2}
\label{bfs_dfs}
\end{figure}

Using Graph 2 in Figure \ref{bfs_dfs}, we will walk through two examples of a depth-first search.
In these examples, when a node has multiple branches originating from it,
we will visit the latest (greater) letter in the alphabet first.
The node at which the algorithm begins is called the ``root node''.
First, we will use D as our root node and E as our target node.
Starting with D, we look at D's greatest neighbor, F, then at F's greatest
neighbor, E. Since E is our target, the algorithm ends.

Second, we start with A and search for B.
We visit node A, then D, then F, and finally E.
At this point, we have gone to the deepest level possible without finding our target, B.
We must back up and try another branch of nodes;
the algorithm backtracks from E to F to try another route.
Here, it is very important that we mark those nodes we have already visited,
otherwise the algorithm would infinitely loop around the latter portion of
the graph without reaching our target, B. Since we have marked D as previously visited, we visit F's final neighbor, C.
We return to node F. Since we have already
searched all possible nodes adjacent to F, we backtrack, again, to D. Because we have
exhausted all sub-branches originating from D, we travel back to A and try
any unexplored branches there. Finally, we find B and the algorithm ends.

Let's try the same two searches using a breadth-first search. While exploring our branches this time, however,
we will visit the earliest (lesser) letter in the alphabet first. In this first search, we again use D as our root node and
E as our target node.
We start by searching for E among the neighbors of D: A, C, and F.
We have not found E, so we search among the neighbors of these adjacent nodes, starting with A.
B is the only neighbor of A that we have not already visited; we backtrack to our previous collection of neighbors: A, C, and F.
We have already visited both neighbors of C (namely D and F), so we move on to the adjacent nodes of F. Finally, we find E among the neighbors of F, and the algorithm ends.

In the second search, we start with A.
Since B is among the neighbors of A, we find it immediately.

Both DFS and BFS have advantages.
If we are able to choose a root node that is somewhat local to the target node, BFS is obviously the better choice. However,
DFS can be much more efficient when the solution is far from the target node.
Algorithm \ref{alg:BFSDFS} outlines the process by which we may implement the methods for a BFS and DFS, respectively.

\begin{algorithm}
\begin{algorithmic}[1]
\Procedure{BFS/DFS}{$G, root, destination$}
	\State $Q \gets \text{Deque (BFS) or list (DFS) with root node in it}$	\Comment{Initialization steps.}
	\State $marked \gets \text{Set with root node in it}$
	\State $visited \gets \text{empty list}$
	\While{$Q \text{ has elements}$}						\Comment{Go through the graph.}
		\State $t \gets Q\text{'s left (BFS) or right (DFS) element}$
		\State $\text{add }t \text{ to the visited list}$
		\If{$t==destination$}							\Comment{Find the destination node.}
			\State \pseudoli{return} $t,visited$

		\Else										\Comment{Visit $t$'s neighbors.}
			\For{$k \text{ in the adjacent nodes of } t$}
				\If{$k \text{ not in } marked$}
					\State $\text{add } k \text{ to } marked$
					\State $\text{add } k \text{ to } Q$
				\EndIf
			\EndFor
		\EndIf
	\EndWhile
\EndProcedure
\end{algorithmic}
\caption{Breadth-first and depth-first searches}
\label{alg:BFSDFS}
\end{algorithm}

\begin{problem}
Implement methods that will perform depth-first and breadth-first searches on a graph
(in this case, an adjacency list). Use a \li{set()} to store the visited nodes.
(Because Python implements sets as hash tables, they have very efficient membership
testing.)

\emph{Helpful Hint}: The implementations of a depth and a breadth first search
are almost exactly same, but they use a particular data structure differently.
Which data structure constitutes this important difference? How is it used differently?
\end{problem}

\section*{Bidirectional Searching an Unweighted Graph}

\begin{figure}[h]
\centering
\begin{tikzpicture}[
  level distance=1.5 cm,
  level 1/.style={sibling distance=5cm},
  level 2/.style={sibling distance=2.5cm},
  level 3/.style={sibling distance=1.25cm}, thick]

  \node[circle,draw] {A}
	child {node[circle,draw]{B}
		child{node[circle,draw]{D}
			child{node[circle,draw]{H}}
			child{node[circle,draw]{I}}
		}
		child{node[circle,draw]{E}
			child{node[circle,draw]{J}}
			child{node[circle,draw]{K}}
		}
	}
	child{node[circle,draw]{C}
		child{node[circle,draw]{F}
			child{node[circle,draw]{L}}
			child{node[circle,draw]{M}}
		}
		child{node[circle,draw]{G}
			child{node[circle,draw]{N}}
			child{node[circle,draw]{O}}
		}
	};
\end{tikzpicture}
\caption{Example Graph 3}
\label{Bidirectional}
\end{figure}

Let A be an unweighted graph. This means that none of the edges of A has any value, or ``weight'' attached to it. How can we find the shortest path between two nodes?
One way would be to simply use a breadth-first search, stopping when we reach the
target node. The downside to this approach is that the algorithm would visit (at least)
every node on every level prior to the target node before it could end. Observe Graph 3 in Figure \ref{Bidirectional} above.
Let us use a simple BFS to search for O, starting with A as our root node.
We begin by searching through all of the neighbors of A, all of the neighbors of B
and C, and so on until we finally locate O. Using this method, we are forced to check
a total of $15$ nodes before our algorithm ends.

A better way to find the shortest path is to use a breadth-first search from
both A \emph{and} O. Thus, we find the shortest path using a pair of breadth-first searches originating from the root node and the target node, respectively.
We advance each BFS one node at a time, alternating until they meet, and then
construct the shortest path of nodes between our root and target.
Using the BFS algorithm from above, we start with A and locate its
least neighbor, B. Since we have not yet found O (or a connection to O) we visit G,
the first neighbor of O. Continuing in this pattern, we return to the neighbors
of A to visit C. Again, we return to our second search to visit the least neighbor of
G, which is also C. We have found our connection, so the algorithm ends. Note that when we used a breadth-first
search with only one root node, we visited $15$ nodes, but when we used a bidirectional BFS, we only had to visit $5$ nodes to find our path.

The speedup from doing a breadth-first search from both sides is especially evident in larger graphs, where each level
contains more nodes as we search away from our root. Indeed, a bidirectional search is one of the best ways to find shortest
paths in unweighted, undirected graphs. For weighted graphs there are many algorithms
that allow us to find the shortest path. For further information research Dijkstra's Algorithm, Johnson's algorithm, the Bellman--Ford algorithm, the Floyd--Warshall algorithm, and A* search algorithm.

\section*{Six Degrees of Kevin Bacon}
\begin{figure}[h]
\includegraphics[scale = .4]{Kevin_Bacon.jpg}
\caption{Kevin Bacon.  Image source: Wikipedia.}
\end{figure}

The theory of ``the 6 Degrees of Separation'' suggests that each person in the world can be linked to any other person by 6 or less steps, or degrees, of acquaintanceship.
Similarly, the game ``the 6 Degrees of Kevin Bacon'' contends that every actor in the film industry can be linked to Kevin Bacon by 6 degrees or less. Kevin Bacon
is a prolific American actor whose film career spans over 30 years in a variety of genres. As such, he once reputably commented that he had either worked with everyone in
Hollywood or someone who has worked with them. The goal of the game, then, is to find the lowest Bacon number for each actor, a process demonstrated as follows:
\begin{enumerate}
\item Kevin Bacon has a Bacon number of $0$.
\item Actors that have been in a movie with Kevin Bacon have a Bacon number of $1$.
\item For all other actors $X$, if $n$ is the lowest Bacon number of any actor that $X$ has been in a film with, $X$ has a Bacon number of $n+1$.
\end{enumerate}

\begin{figure}[h]
\includegraphics[scale = .6]{Example}
\caption{Jeffrey Humphery was in \emph{End Game} with Cuba Gooding Jr., who was in \emph{A Few Good Men} with Kevin Bacon, so Jeffrey Humphrey has a Bacon Number of 2.  Image source: http://oracleofbacon.org/.}
\end{figure}

We can define a graph in the same way: where each actor is a node and there is an edge between any two nodes if their actors were in a movie together. From this structure we can find
the shortest path between any actor and Kevin Bacon by using any of the algorithms listed above. Of course, this game is not limited to Kevin Bacon; we could use any actor as our ``root actor.''
In fact, ``the Six Degrees of Separation'' can be applied to many different fields. Its most famous application is known as the Erdos numbers: how far away is a person
 from publishing a paper (rather than starring in a movie) with the prolific mathematician Paul Erdos?

\section*{NetworkX}
For this lab, we are going to use a network library called NetworkX. NetworkX is a useful Python package that allows us to create and manipulate large, complex networks.
When considering efficiency, however, it is important to note that because NetworkX uses Python objects to represent its graphs internally, graphs with many nodes will
use a large chunk of memory. (Other network libraries, such as igraph, are written in C++ and may therefore significantly reduce the overhead of storing such a graph.)
To make an undirected graph in NetworkX, we simply code the following:

\begin{lstlisting}
import networkx as nx
G = nx.Graph()
\end{lstlisting}

As an example, we will create a NetworkX graph that links the ingredients of
delicious americanized Italian foods together. We first want to add the ingredients as nodes
to our graph. To add nodes we can use either the \li{add_node(x)} method, where $x$
is the node we want to add, or the \li{add_nodes_from()} method, to
add multiple nodes at once.

\begin{lstlisting}
import networkx as nx
G = nx.Graph()

G.add_node('dough')
PizzaIngredients = ['dough', 'mozzarella cheese', 'tomato sauce', 'ham', 'pineapple']
G.add_nodes_from(PizzaIngredients)
\end{lstlisting}
Note that even though we added \li{'dough'} twice, the graph will only store this node once. NetworkX also comes with \li{has_node(x)} and \li{has_edge(x,y)} methods that will tell
us if a node $x$ or an edge $(x,y)$ has already been added.

Now we want to connect our pizza ingredients together. Adding an edge is simple; we just use the \li{add_edge(x,y)} method, where $x$ and $y$ are previously added nodes.
Again, we can also add multiple edges using the \li{add_edges_from()} method.
However, these edges must be provided as 2-tuples \li{(x,y)}, so we use the
itertools module to create a list of the edges we want to add. It might be wise
to review previous labs and the python documentation for this module as you implement
NetworkX graphs.

\begin{lstlisting}
from itertools import permutations
Pizza = permutations(PizzaIngredients, 2)
G.add_edges_from(Pizza)
\end{lstlisting}
If we add an edge between two non-existent nodes, the missing nodes will also be added to the graph. Let's add the ingredients for lasagna to the graph using only the \li{add_edges_from} method.
\begin{lstlisting}
LasagnaIngredients = ['lasagna noodles', 'sausage', 'mozzarella cheese', 'cottage cheese', 'tomato sauce', 'egg', 'parsley']
Lasagna = permutations(LasagnaIngredients, 2)
G.add_edges_from(Lasagna)
\end{lstlisting}
We can also remove nodes using the methods \li{remove_node(x)} and \li{remove_nodes_from()}.
Let's say, for example, that we are vegetarians and want to remove the
sausage and ham from our graph. Then, we simply run the following code:
\begin{lstlisting}
G.remove_nodes_from(['sausage', 'ham'])
\end{lstlisting}

Sometimes, with smaller graphs, it can be useful to visualize how nodes are linked together. However, the overhead associated with displaying larger graphs makes this unwise for extensive data sets.
For our example, though, it makes it easy to see the connections between our lasagna and pizza ingredients:

\begin{lstlisting}
# MatPlotLib is a standard Python plotting library; it allows you to plot
# and display your 2D graph.
import matplotlib.pyplot as plt
# Allows you to draw a simple diagram of the nodes and their connecting edges.
nx.draw(G)
# Displays your graph.
plt.show()
\end{lstlisting}

\begin{problem}
The data file, \texttt{movieData.txt}, contains the entire casts of movies made over the course of several years. It is a delimited file with each field delimited by the `/' character. Write a method that will implement the following:
\begin{itemize}
\item Open the file.
\item Generate a NetworkX graph, with the actors as nodes and with edges connecting all of the actors within the same movie to one another. Do not include movie titles as nodes.
\item Return your constructed NetworkX graph (note that, because of the large amount of data, you \textbf{should not} attempt to display this graph).
\end{itemize}
\emph{Helpful Hints:}
\begin{itemize}
\item For later solutions, it might be beneficial to also construct a map from the actors to the movies they appeared in.
\item For certain machine types, take care to open your file using universal newline support. See previous labs for further information.
\end{itemize}
\end{problem}

For a graph, G, the \li{shortest_path(G, x, y)} function in NetworkX outputs
the shortest path from $x$ to $y$ as a list. If more than one such path exists,
it will simply return the first of the shortest paths that it finds. In an
unweighted graph, this method finds the shortest path using a bidirectional search,
a process outlined earlier in this chapter. However, if no path exists between nodes
$x$ and $y$ then NetworkX will raise an exception. If we don't specify a
target, then the function will find the shortest paths between our root,
$x$, and every other node in the network.  It will return the results as a dictionary.

We can find the length of the shortest path using the \li{shortest_path_length(G, x, y)}
function. Again, the target node for this function is optional; if we only
specify the graph and the root node, \li{shortest_path_length()} will return a dictionary
with all \emph{connecting} nodes as keys. When we don't provide a target, if no path exists between the root
and another node then NetworkX will simply omit that node from the dictionary (rather than raise an exception).

\begin{problem}
Find the shortest path between Kevin Bacon and Liam Neeson. Then find the shortest path from Kevin Bacon to Imran Zahid. Write a function that will accept two actors and output
the path between them, along with the movies in between that connect each step of the path. Output the paths from Kevin Bacon to Liam Neeson and Imran Zahid along with the connecting movies.
\end{problem}

\begin{problem}
Find the average Bacon number for the dataset. Then find the number of actors
associated with each Bacon number and the number of actors that have no connection
 to Kevin Bacon at all (we would say these people have a Bacon number of infinity).
\end{problem}


\documentclass{article}

\usepackage{geometry}

\geometry{a5paper, margin=1in}
\setlength{\parskip}{\baselineskip}%
\setlength{\parindent}{0pt}%

\title{Statistics Notes}
\author{Zac Garby}
\begin{document}

\maketitle
\tableofcontents

\section{Probability}

For any events A and B:

$$ P(A') = 1 - P(A) $$
$$ P(A) = P(A \cap B) + P(A \cap B') $$
$$ P(A \cup B) = P(A) + P(B) - P(A \cap B) $$
$$ P(A' \cap B') = 1 - P(A \cup B) $$
$$ P(B|A) = \frac {P(B \cap A)} {P(A)} $$
$$ P(A \cap B) + P(B|A) P(A) $$

When A and B are mutually exclusive events:

$$ P(A \cap B) = 0 $$
$$ P(A \cup B) = P(A) + P(B) $$
$$ P(A_1 \cup A_2 \cup \dots \cup A_n) = P(A_1) + P(A_2) + \dots + P(A_n) $$

When A and B are independent:

$$ P(A \cap B) = P(A) P(B) $$

\break

\section{Statistical Distributions}

A random variable can be distributed according to a distribution function. The two
we need to know are the Binomial Distribution and the Normal Distribution.

\subsection{Binomial Distribution}

If a random variable $X$ is distributed according to a Binomial Distribution, you can
write:

$$ X \sim B(n, p) $$

Where n is the number of trials and p is the probability of success for each one.
A Binomial Distribution can only be used when all of the following conditions are true:

\begin{itemize}
	\item A fixed number of trials
	\item Each trial ends in success or failure
	\item Trials are independent
	\item Probability of success is constant
\end{itemize}

$E(X)$ denotes the expected value of the random variable.
$$ E(X) = np $$

$P(X = x)$ is the probability that the random variable $X$ is equal to $x$.
$$ P(X = x) = {n \choose x}p^x(1-p)^{n-x} $$

$P(X \leq x)$ is the probability that the random variable $X$ is less than or equal to $x$.
This can be calculated using an extension of the previous equation.
$$ P(X \leq x) = \sum_{i=0}^{\lfloor x \rfloor}{n \choose i}p^i(1-p)^{n-i} $$

\subsubsection{Normal Approximation}

A Binomial Distribution can be approximated as a Normal one if either $p$ is close to 0.5
or $n$ is large. A general rule is if $np$ and $n(1-p)$ are both greater than 5, a normal
approximation can be used.

Due to the nature of the two distributions, a continuity correction must be used. Thus, for
two random variables:

$$ X \sim B(n, p) $$
$$ Y \sim N(np, np(1-p)) $$

A probability expression in terms of $X$ can be transformed into one of $Y$:

$$ P(X \geq 5) \approx P(Y > 4.5) $$
$$ P(X \leq 10) \approx P(Y < 10.5) $$

\subsection{Normal Distribution}

If a random variable X is distributed according to a Normal Distribution, you can write:

$$ X \sim N(\mu, \sigma^2) $$

Where $\mu$ is the mean, i.e. the value around which the distribution is symmetrical, and
$\sigma^2$ is the variance. The square root of the variance, $\sigma$, known as the standard
deviation, is used more often.

A Normal Distribution is used for continuous variables which are symmetrical and follow a bell-curve shape.

$$ P(X = x) = 0 $$

The probability of $X$ being a particular value is so close to 0 that it actually is. This
is because $X$ is continuous, and therefore can take an infinite number of values. This also
means:

$$ P(X > x) = P(X \geq x) = 1 - P(X < x) $$

\subsubsection{The Normal Normal Distribution}

The random variable $Z$ is defined such that:

$$ Z \sim N(0, 1) $$

Other Normal Distributions can be converted to this distribution:

$$ X \sim N(\mu, \sigma^2) $$
$$ P(X < n) = P(Z < \frac{n-\mu}{\sigma}) $$

\subsubsection{The sample mean}

The sample mean of a normal distribution $X \sim N(\mu, \sigma^2)$, denoted $\overline X$, is also distributed normally:

$$ \overline X \sim N(\mu, \frac{\sigma^2}{n}) $$

Where $n$ is sample size.

\subsection{Hypothesis Testing}

A hypothesis test uses data from a sample to test whether or not a statement about a population
is likely to be true.

The general idea is that you're given two statements. The first, called the null hypothesis,
is always where $p$, or whatever variable you're testing, is equal to something. The other,
the alternate hypothesis, is $p$ being either greater than, less than, or not equal to the
value which the null hypothesis claims.

If the alternative hypothesis says $p \neq v$, the test is two tailed, which means that
at the end the significance level, $\alpha$, is halved.

\end{document}


\section{Division of any polyhedron into a union of simplexes}
As Adrian mentioned, the key is to divide a polyhedron into convex parts. Then one can further divide each convex body by a simple subdivision. So let us focus on the first step.

By the definition of polyhedron, there are finite numbers of $n-1$ dimensional faces. Each of these faces has the form
$$
a^i_1 x_1 + \ldots + a^i_n x_n +b^i +b^i= 0
$$
where i is the index of the face.  Each of the faces divide $R^n$ into two parts, i.e.,
$$
a^i_1 x_1 + \ldots + a^i_n x_n +b^i> 0 \mbox{ and } a^i_1 x_1 + \ldots + a^i_n x_n +b^i< 0.
$$
These planes divide $R^n$ into several parts, i.e., the intersection of
\begin{equation}\label{inequal}
  a^i_1 x_1 + \ldots + a^i_n x_ n +b^i<(>) 0
\end{equation}

Each part defined by these inequalities is convex (by definition). Then we need to check that the original polyhedron can be written as the union of some of these convex parts.

To see this, consider any point $x$ in the polyhedron. $x$ belongs to one of the convex bodies defined above, since \eqref{inequal} includes all the possible situations. This shows that the union of the above convex bodies contains the polyhedron.

Next, we claim that each convex body is either contained in the polyhedron, or has no intersection with the polyhedron (need a rigorous proof!).

This implies that we can represent the polyhedron as the union of some convex bodies.

\endinput
\subsection{Discussion with Lin}

Prof Ocneanu builds up a connection between polygon and binary tree.
\begin{itemize}
\item A hyperplane can cut a polygon into two polygons. The hyperplane can be regarded as a node, and the divided polygons can be seen as sub-binary trees.
\item Follow the steps above to get more sub-binary trees.
\item According to binary tree algorithm you will git a algorithm of cutting polygon.
\end{itemize}

Because of finiteness of polygon, the operator will be end. We can cut a polygon into simplices.

The other thing that is very interesting is that if you cut with a hyperplane, then the other polygons are only on one side of the hyperplane, which may not be connected, but increases the convexity, which guarantees convex.