% !TEX root = apxthy.tex

\section{Algebraic Polynomials}
%
\label{sec:poly}
%
Our second major topic concerns approximation of functions defined on an
interval $f : [-1, 1] \to \R$, without loss of generality. But contrary to
\S~\ref{sec:trig} we no longer assume periodicity. Instead we will approximate
$f$ by algebraic polynomials,
\[
      f(x) \approx p_N \in \Poly_N
\]
where $\Poly_N$ denotes the space of degree $N$ polynomials,
\[
   \Poly_N := \bg\{ \sum_{n = 0}^N c_n x^n \bsep c_n \in \R \bg\}.
\]
Note in particular that in the terms of "simplicity" these are indeed the
simplest functions to evaluate numerically in that they only require addition
and multiplication operations.

In terms of a basic convergence result we have the following initial
proposition, which we will not prove now, but it will follow from our
later work.

\begin{proposition}[Weierstrass Approximation Theorem] \label{th:poly:Weierstrass}
   $\bigcup_{N \in \N} \Poly_N$ is dense in $C([-1,1])$ and by extension also
   in $L^p(-1,1)$ for all $p \in [1, \infty)$.
\end{proposition}

\bigskip

Indeed, as we have argued before, convergence in itself of {\em some} sequence
of approximations  is rarely useful, but we require (i) rates and (ii) explicit
constructions. Much of this chapter is therefore devoted to interpolation.

It is a standard fact (and easy to prove) that for any $N+1$ distinct points
$x_0, \dots, x_N \in \R$ and values $f_0, \dots, f_N$ there exists exactly one
polynomial $p_N \in \Poly_N$ interpolating those values, i.e.,
\[
   p_N(x_j) = f_j, \qquad j = 0, \dots, N.
\]
(Indeed, the same is even true for $x_j \in \C$.) These equations form
a linear system for the coefficients $c_n$, which can be solved to obtain
the interpolation polynomial, which in turn can be easily readily
numerically.

A key question is how to choose the interpolation points $x_j$? It may seem
intuitive to take equispaced nodes, $x_j = -1 + 2j/N$.  We start this section by
exploring precisely this approach to approximate some smooth functions on
$[-1,1]$; see \nbpoly for some motivating examples. In this Julia notebook we
clearly observe that this yields a divergent sequence of polynomials, but by
exploring also other kinds of fits we also see that this does not preclude the
possibility of computing a (very) good approximation. We therefore focus
initially by deriving a ``good'' set of interpolation nodes. The same idea will
also naturally lead to the Chebyshev polynomials.

\subsection{Chebyshev Points, Chebyshev Polynomials and Chebyshev Series}
%
We can motivate the idea of the Chebyshev points by mapping the polynomial
approximation problem to the trigonometric approximation problem:

Let $f\in C([-1,1])$, then let $g \in C(\TT)$ be defined by
\[
   g(\theta) = f(\cos\theta).
\]
Note that $g$ ``traverses'' $f$ twice!

We will later see that $g$ inherits the regularity of $f$ even across domain
boundaries; for now let us understand the consequence of this observation. We
know from \S~\ref{sec:trig} that equispaced interpolation of $g$ yields an
excellent trigonometric interpolant, i.e., we choose $\theta_j = -\pi + 2\pi
j/N$ and we choose coefficients $\hat{g}_k$ such that
\[
   t_N(\theta_j) = \sum_{-N}^N \hat{g}_k e^{ik \theta_j} = g(\theta_j)
\]
%
We may ask to interpolate $f$ at the analogous points, $x_j = \cos(\theta_j)$
but since $g$ contains ``two copies'' we only take half of the nodes.
This gives the Chebyshev nodes
%
\begin{equation} \label{eq:poly:chebnodes}
   x_j := \cos\b( \pi j / N \b) \qquad j = 0, \dots, N.
\end{equation}

We can readily test our hypothesis that these yield much better approximations;
see again \nbpoly. Thus, for future reference we define the Chebyshev
interpolant $I_N f$ to be the unique function $I_ f \in \Poly_N$ such that
\[
   I_N f(x_j) = f(x_j) \qquad \for j = 0, \dots, N,
\]
where $x_j$ are the Chebyshev nodes \eqref{eq:poly:chebnodes}.

Next, we ask what the analogue of the Fourier series is. We write
\[
   g(\theta) = \sum_{k \in \Z} \hat{f}_k e^{ik\theta},
\]
then using that $g$ is real and $g(-\theta)=g(\theta)$,
\[
   g(\theta) = \hat{g}_0 + 2 \sum_{k = 1}^N \hat{g}_k \cos(k\theta)
\]
It is therefore natural to define the {\em Chebyshev polynomials}
%
\begin{equation} \label{eq:poly:defn_Tk}
   T_k(\cos\theta) = \cos(k\theta), \qquad k \in \N := \{0,1,2,\dots\}.
\end{equation}
%
A wide-ranging consequence of this definition is that
\[
      |T_k(x)| \leq 1 \qquad \forall k.
\]

\begin{lemma} \label{th:poly:chebpolys}
   The functions $T_k : [-1,1] \to \R$ are indeed polynomials and
   satisfy the recursion
   \begin{equation} \label{eq:poly:chebrecursion}
      T_{k+1}(x) = 2 x T_k(x) - T_{k-1}(x),
   \end{equation}
   with initial conditions $T_0(x) = 1, T_1(x) = x$.
\end{lemma}
\begin{proof}
   The identities $T_0(x) = 1, T_1(x) = x$ follow immediately from
   \eqref{eq:poly:defn_Tk}. If we can prove the recursion, then
   the fact that $T_k$ are polynomials follows as well.

   To that end, we introduce another representation,
   \[
      T_k\B( \smfrac{z + z^{-1}}{2} \B)
      = T_k(\Re z)
      = \Re z^k = \frac{z^k + z^{-k}}{2},
   \]
   where $|z| = 1$. Then,
   \begin{align*}
      & \hspace{-1cm} T_{k+1}(\Re z) - 2 \Re z T_k(\Re z) + T_{k-1}(\Re z) \\
      &= \smfrac12 \B(
         z^{k+1} + z^{-k-1}  - (z+z^{-1}) (z^k+z^{-k})
         + z^{k-1} + z^{-k+1} \B) \\
      &= \smfrac12 \B( z^{k+1} + z^{-k-1}
               - z^{k+1} - z^{k-1} - z^{1-k} - z^{-1-k}
               + z^{k-1} + z^{-k+1} \B) \\
      &=0. \qedhere
   \end{align*}
\end{proof}

For future reference we define the Joukowsky map
\[
   \phi(z) = \frac{z+z^{-1}}{2}.
\]
and note that it is analytic in $\C \setminus \{0\}$.

We now know that $T_k(x)$ are indeed polynomials of degree $k$ and in light of
the foregoing motivating discussion, we have the following result.

\begin{lemma}
   Let $f \in C([-1,1])$ is uniformly continuous, then there exists {\em
   Chebyshev coefficients} $\tilde{f}_k \in \R$ such that the {\em Chebyshev
   series}
   \begin{equation} \label{eq:poly:chebseries}
      f(x) = \sum_{k = 0}^\infty \tilde{f}_k T_k(x)
   \end{equation}
   is absolutely and uniformly convergent.

   The Chebyshev coefficients are given by the following equivalent formulas,
   \begin{align*}
      \tilde{f}_k
      &=  \frac{2}{\pi} \int_{-1}^1 \frac{f(x) T_k(x)}{\sqrt{1-x^2}} \,dx \\
      &=  \frac{1}{2\pi i} \oint_{\SS} \,\,\b(z^{-1+k} + z^{-1-k}\b) f(\phi(z))
                  \, dz \\
      &= \frac{1}{\pi i} \oint_{\SS} \,\, z^{-1+k} f(\phi(z)) \, dz \\
      &= \frac{1}{\pi i} \oint_{\SS} \,\, z^{-1-k} f(\phi(z)) \, dz.
   \end{align*}
   For $k = 0$ a factor $1/2$ must be applied.
\end{lemma}
\begin{proof}
   If $f \in C([-1,1])$ with modulus of continuous $\omega$, then $g \in C(\TT)$
   also has a modulus of continuity and hence the Fourier series converges
   uniformly and equivalently, the Chebyshev series does as well.

   The expressions for $\tilde{f}_k$ are simply transplanting the fourier
   coefficients $\hat{g}_k$ to Chebyshev coefficients $\tilde{f}_k$.
\end{proof}

In analogy with the truncation of the Fourier series $\Pi_N g$ (which
is the $L^2(\TT)$-projection or best-approximation we define
the Chebyshev projection
\[
   \PCheb_N f(x) := \sum_{k = 0}^N \tilde{f}_k T_k(x).
\]

\subsection{Convergence rates}
%
\label{sec:poly:rates}
%
As we have learned in \S~\ref{sec:trig}, the real power of polynomials is in
the approximation of analytic functions, hence we begin again with this
setting.

Intuitively, the idea is that analyticity of $f$ on $[-1,1]$ translates into
analyticity of the corresponding periodic function $g(\theta) = f(\cos\theta)$.
Exponential decay of the Fourier coefficients $\hat{g}_k$ then translates into
exponential decay of the Chebyshev coefficients  $\tilde{f}_k$. But we can prove
this exponential decay directly with a relatively straightforward variation of
the argument we used in \S~\ref{sec:trip:pw}, which is interesting to see the
analogies.

We begin by defining
\[
   F(z) := f(\Re z) = f\b( \smfrac12(z+z^{-1}) \b)
         = f(\phi(z)) \qquad \for z \in \SS := \{|z|=1\}.
\]
%
where $\phi(z) = \smfrac12 (z+z^{-1})$ is also called Joukowsky map. $\phi$ is
clearly analytic in $\C \setminus \{0\}$. Thus, if $f$ is analytic on $[-1,1]$
then $F$ must be analytic on $\SS$. Next, we note that analyticity of $g(\theta)$
on the strip $\Omega_\alpha$ is equivalent to analyticity of $F$ on the annulus
%
\[
   \SS_\rho := \{ z \in \C \sep \rho^{-1} \leq |z| \leq \rho \},
\]
%
with $\rho = 1+\alpha$. Let the corresponding {\em Bernstein ellipse} be the
pre-image of $\SS_\rho$ under the Joukowsky map,
%
\[
   E_\rho := \phi(\SS_\rho),
\]
%
then analyticity of $f$ in $E_\rho$ implies analyticity of $F$ in $\SS_\rho$.

Finally, we recall from the derivation of the Chebyshev polynomials $T_k(x)$
that they can also be written as
\[
   \smfrac12 (z^k + z^{-k}) = T_k(\phi(z)).
\]

After these preparations, we can prove the following result.

\begin{theorem}[Decay of Chebyshev coefficients]
   Let $\rho > 1$ and $f \in A(E_\rho)$ with  $M := \|f\|_{L^\infty(E_\rho)} <
   \infty$, then the Chebyshev coefficients of $f$ satisfy
   \[
      |\tilde{f}_k| \leq 2 M \rho^{-k}, \qquad k \geq 1.
   \]
\end{theorem}
\begin{proof}
   We start with
   \[
      \tilde{f}_k = \frac{1}{\pi i} \oint_{\SS} z^{-1-k} F(z) \, dz
   \]
   Since $F$ is analytic on $\SS_\rho$ (and hence in the neighbourhood of $\SS_\rho$)
   we can expand the contour to {\it (Exercise: explain why this can be done using
   Cauchy's integral formula and a suitable sketch!)}
   \[
      \tilde{f}_k = \frac{1}{\pi i} \oint_{|z|=\rho} z^{-1-k} F(z) \, dz
   \]
   and hence we immediately obtain
   \[
      |\tilde{f}_k| \leq \frac{2\pi \rho \rho^{-1-k} M}{\pi} = 2 M \rho^{-k}.
      \qedhere
   \]
\end{proof}

Decay of Chebyshev coefficients gives the following approximation error
estimates.

\begin{theorem}[Chebyshev Projection and Interpolation Error]
   %
   \label{th:poly:err_analytic}
   %
   Let $\rho > 1$ and $f \in A(E_\rho)$ with $M := \|f\|_{L^\infty(E_\rho)} <
   \infty$, then
   \begin{align}
      \label{eq:poly:projerror}
      \| f - \PCheb_N f \|_{L^\infty(-1,1)} &\leq \frac{2M \rho^{-N}}{\rho-1}, \\
      \label{eq:poly:interperror}
      \| f - I_N f \|_{L^\infty(-1,1)} &\leq C M \log N \rho^{-N},
   \end{align}
   where $C$ is a generic constant.
\end{theorem}
\begin{proof}
   For the proof of \eqref{eq:poly:projerror} we use the fact that
   $\|T_k\|_\infty \leq 1$ to estimate
   \begin{align*}
      \| f - \PCheb_N f \|_\infty
      &\leq
      \sum_{k = N+1}^\infty |\tilde{f}_k|  \\
      &\leq
      2M \sum_{k = N+1}^\infty \rho^{-k} \\
      &=
      \frac{2M \rho^{-N}}{\rho-1}.
   \end{align*}
   The estimate \eqref{eq:poly:interperror} follows from the bound on the
   Lebesgue constant
   \[
      \| I_N \|_{L(L^\infty)} \leq C \log N,
   \]
   which follows from the analogous bound for trigonometric interpolation
   given in Theorem~\ref{th:trig:lebesgue}.

   (For a sharp bound, it is in fact known that $\Lambda_N \leq \frac{2}{\pi}
   \log(N+1) + 1$.)
\end{proof}

\begin{remark}
   One can in fact prove that
   \[
      \| f - I_N f \|_{L^\infty(-1,1)} \leq \frac{4M \rho^{-N}}{\rho-1},
   \]
   using an aliasing argument; see \cite[Thm. 8.2]{Trefethen2013-rg},
   somewhat similar to the argument we used for our convergence estimate of
   the trapezoidal rule in Exercise~\ref{exr:trig:trapezoidal rule}.
\end{remark}

\begin{example}[Fermi-Dirac Function]
   %
   \label{exa:poly:fermi-dirac}
   %
   Consider the Fermi-Dirac function
   \begin{equation}
     f_\beta(x) = \frac{1}{1 + e^{\beta x}},
   \end{equation}
   where $\beta > 0$.

   {\it REMARK: The Fermi--Dirac function describes the distribution of particles
   over energy states in systems consisting of many identical particles that obey
   the Pauli exclusion principle, e.g., electrons. A broad range of important
   algorithms in computational physics are fundamentally about approximating the
   Fermi--Dirac function. The parameters $\beta$ is inverse proportional to
   temperature (that is, Fermi-temperature).}

   Extending $f_\beta$ to the complex plane simply involves replacing $x$ with
   $z$, i.e.,
   \[
     f_\beta(z) = \frac{1}{1 + e^{\beta z}},
   \]
   which is well-defined {\em except at the poles}
   \[
       z_j = \pm i  \frac{\pi}{\beta}.
   \]

   In Exercise~\ref{exr:poly:ellipse} we show that the semi-minor axis of the
   Bernstein ellipse $E_\rho$ is $\frac12 (\rho-\rho^{-1})$, hence the largest
   $\rho$ for which ${\rm int}E_\rho$ does not intersect any singularity is
   given by
   \[ \frac12 (\rho-\rho^{-1}) = \frac{\pi}{\beta}x. \]
   Solving this quadratic equation for $\rho$ yields one positive root
   \[ \rho = \smfrac{\pi}{\beta}+\sqrt{1 + \smfrac{\pi^2}{\beta^2}}
   \]
   Of particular interest is the low temperature regime $\beta \to \infty$
   (recall that $\beta \propto$ inverse temperature), for which we obtain
   \[
      \rho \sim 1 + \smfrac{\pi}{\beta}.
   \]

   In this regime we therefore expect an approximation rate close to
   \[
      \| f_\beta - I_N f_\beta \|_{\infty}
      \lesssim \beta \b(1 + \smfrac{\pi}{\beta}\b)^{-N}
      \sim \beta \exp\b( - \pi \beta^{-1} N).
   \]
   (Why is this not a rigorous and in fact likely false bound? You can get
   a rigorous reformulation from the foregoing theorems.)
 \end{example}

For convergence rates for $C^{j,\sigma}([-1,1])$ and similar functions, we
want to adapt the Jackson theorems. We could again "transplant" the argument
from the Fourier to the Chebyshev setting, but it will be more convenient
this time to simply apply the Fourier results directly. The details
are carried out in Exercise~\ref{exr:poly:convergence}. We obtain
the following result.

\begin{theorem}[Jackson's Theorem(s)]
   \label{th:poly:jackson}
   %
   Let $f \in C^{(j)}([-1,1])$, $j \geq 0$, where $f^{(j)}$ has modulus of
   continuity $\omega$, then
   \begin{equation}
      \label{eq:poly:jackson1}
      \inf_{p_N \in \Poly_N} \| f - p_N \|_{L^\infty} \leq
      C N^{-j} \omega\b(N^{-1}\b).
   \end{equation}
\end{theorem}
\begin{proof}
   See Exercise~\ref{exr:poly:convergence}.
\end{proof}

We cannot yet test these predictions numerically, since we don't yet have
a numerically stable way to evaluate the Chebyshev interpolants (or projections).
We will remedy this in the next two sections.

\subsection{Chebyshev transform}
%
We have seen in \nbpoly that naive evaluation of the Chebyshev interpolant leads
to highly unstable numerical results. The emphasis here is on the term
``naive''. Indeed, there exist at least two natural and numerically stable way
to evaluate the Chebyshev interpolant.

The first approach we consider is the Discrete Chebyshev transform (DCT), an
immediate analogy of the Discrete Fourier transform (DFT). As in the Fourier case,
once we have transformed the polynomial to the Chebyshev basis, we can
evaluate it in $O(N)$ operations. But in the Chebyshev case, this is even more
efficient due to the recursion formula \eqref{eq:poly:chebrecursion}. Moreover,
the polynomial derivatives are straightforward to compute in this case as well.

Let $F = (F_j) \in \R^{N+1}$ (we immagine that $F_j = f(x_j)$ are nodal values
of some $f \in C([-1,1])$ at the Chebyshev nodes), then there exists a unique
polynomial $p_N \in \Poly_N$ such that $p_N(x_j) = F_j$. We write $p_N(x) =
\sum_{k = 0}^N \tilde{F}_k T_k(x)$, then
\begin{equation}
   \label{eq:poly:chebtransform}
   \tilde{F} := {\rm DCT}[F] := \b( \tilde{F}_k \b)_{k = 0}^N.
\end{equation}
Since polynomial interpolation is linear and unique the operator is
an invertible linear mapping, with inverse (obviously) given by
\begin{equation}
   \b({\rm IDCT}[\tilde{F}]\b)_j = \sum_{k = 0}^N \tilde{F}_k T_k(x_j).
\end{equation}

\begin{lemma} \label{th:poly:dct_explicit}
   Let $\tilde{F} = {\rm DCT}[F]$, then
   \[
      \tilde{F}_k = \frac{p_k}{N}\bg\{
            \smfrac12 \b( (-1)^k F_0 + F_N \b)
            + \sum_{k = 1}^{N-1} F_k T_k(x_j)
         \bg\}.
   \]
\end{lemma}

We won't prove \Cref{th:poly:dct_explicit} since we won't need this expression.
It is only stated here for the sake of completeness. The interested reader
will be able to check it by a direct computation; it is also implicitly
contained in the following discussion.

{\it A priori} the cost of evaluating the DCT and IDCT is $O(N^2)$, but the
connection between the Fourier and Chebyshev settings gives us an $O(N\log N)$
algorithm which we now derive. Let $F = {\rm IDCT}[\tilde{F}]$, then writing
\[
   T_k(x_j) = T_k(\cos(j\pi/N)) = \cos(kj\pi/N)
\]
we obtain
\begin{align}
   \label{eq:poly:costtransform}
   F_j
   &=
   \sum_{k = 0}^N \tilde{F}_k \cos(kj\pi/N)
   \\ \notag &=
   \sum_{k = 0}^N \tilde{F}_k \smfrac12 \b( e^{i2\pi kj/ (2N)} + e^{-i2\pi kj/(2N)}),
\end{align}
which looks {\em almost} like a IDFT on the grid $\{-N, \dots, N\}$. We
can rewrite this a little more,
\begin{align*}
   F_j
   &=
   \tilde{F}_0 + \sum_{k = 1}^{N-1} \b[\smfrac12 \tilde{F}_k\b] e^{i2\pi kj/ (2N)}
   + \tilde{F}_N \smfrac12 \b( e^{i2\pi N j/ (2N)} + e^{-i2\pi N j/ (2N)} \b)
   \\  & \qquad
   + \sum_{k = -N+1}^{-1} \b[\smfrac12 \tilde{F}_{-k}\b] e^{i2\pi kj/ (2N)}
   \\ &=
   \tilde{F}_0 + \sum_{k = 1}^{N-1} \b[\smfrac12 \tilde{F}_k\b] e^{i2\pi kj/ (2N)}
   + \tilde{F}_N e^{i2\pi N j/ (2N)}
   + \sum_{k = N+1}^{2N-1} \b[\smfrac12 \tilde{F}_{2N-k}\b] e^{i2\pi kj/ (2N)}
   \\ &=:
   \sum_{k = 0}^{2N-1} \hat{G}_k e^{i2\pi kj/ (2N)},
\end{align*}
where we have defined
\[
   \hat{G}_k := \cases{
      \tilde{F}_k, & k = 0, \\
      \smfrac12 \tilde{F}_k, & k = 1, \dots, N-1, \\
      \tilde{F}_k, & k = N, \\
      \smfrac12 \tilde{F}_{2N-k}, & k = N+1, \dots, 2N-1.
   }
\]
Let $\hat{G}[\tilde{F}]$ be defined by this expression, then we have shown
that
\[
   F_j = \b({\rm IDCT}[\tilde{F}]\b)_j =
   \b( {\rm IDFT}[\hat{G}[\tilde{F}]] \b)_j, \qquad j = 0, \dots, N.
\]
After determining $F_j$ for $j = N+1, \dots, 2N-1$ we can then evaluate the
DCT via the DFT.  From the expression \eqref{eq:poly:costtransform} we
immediately see that
\begin{align*}
   F_{j}
   &=
   \sum_{k = 0}^N \tilde{F}_k \cos(kj\pi/N - 2\pi k)
   \\ &=
   \sum_{k = 0}^N \tilde{F}_k \cos(k2\pi(j-2N)/2N)
   \\ &=
   \sum_{k = 0}^N \tilde{F}_k \cos(k2\pi(2N-j)/2N)
   \\ &=
   F_{2N-j}
\end{align*}
That is, if we define
\[
   G_j := \cases{
      F_j, & j = 0, \dots, N, \\
      F_{2N-j}, & j = N+1, \dots, 2N-1
   }
\]
then we obtain
\[
   {\rm DFT}[G] = \hat{G},
\]
from which we can readily obtain $\tilde{F}$.

In {\tt Julia} code an $O(N\log N)$ scaling Chebyshev transform might
look as follows:

\begin{verbatim}
   "fast Chebyshev transform"
   function fct(F)
      N = length(F)-1
      G = [ F; F[N:-1:2] ]
      Ghat = real.(fft(F))
      return [Ghat[1]; 2 * Ghat[2:N]; Ghat[N+1]]
   end

   "fast inverse Chebyshev transform"
   function ifct(Ftil)
      N = length(Ftil)-1
      Ghat = [Ftil[1]; 0.5 * Ftil[2:N]; Ftil[N+1]; 0.5*Ftil[N:-1:2]]
      G = real.(ifft(Ghat))
      return G[1:N+1]
   end
\end{verbatim}

\begin{remark}
   The expression \eqref{eq:poly:costtransform} is in fact another kind of
   well-known transform, the {\em Discrete Cosine Transform} (one of several
   variants). A practical implementation of the fast Chebyshev transform
   should therefore use an efficient implementation of the fast cosine transform
   rather than the FFT.
   For the sake  of simplicity (to avoid studying yet another transformation)
   we did not study this transform in detail, but there is plenty of literature
   and software available on this topic.
\end{remark}

\subsection{Barycentric interpolation formula}
%
\label{sec:poly:bary}
%
The second method we discuss is the {\em barycentric interpolation formula}.
After precomputing some ``weights'' it gives another $O(N)$ method to evaluate
the Chebyshev interpolant (or indeed {\em any} polynomial interpolant) in a
numerically stable manner. This method entirely avoids the transformation to the
Chebyshev basis. (This section is taken almost verbatim from
\cite{Trefethen2013-rg}; see also \cite[Ch. 5]{Trefethen2013-rg} for a more
detailed, incl historical, discussion).

We begin with the usual Lagrange formula for the nodal interpolant.
Let $p(x_j) = f_j, j = 0, \dots, N$ where $p \in \Poly_N$, then
\[
   p(x) = \sum_{j = 0}^N f_j \ell_j(x),
   \qquad \text{where} \quad
   \ell_j(x) = \frac{ \prod_{n \neq j} (x - x_n)}{\prod_{n \neq j} (x_j-x_n)}.
\]
This formula has the downside that it costs $O(N^2)$ to evaluate $p$ at a
single point $x$.

But we observe that $\ell_j(x)$ have a lot of terms in common. This can be
exploited by defining the {\em node polynomial}
\[
   \ell(x) := \prod_{n = 0}^N (x-x_n),
\]
then we obtain
\begin{equation} \label{eq:poly:bary_weights}
   \ell_j(x) = \ell(x) \frac{\lambda_j}{x - x_j}
   \qquad \text{where}  \qquad
   \lambda_j = \frac{1}{\prod_{n \neq j} (x_j - x_n)}.
\end{equation}
The ``weights'' $\lambda_j$ still cost $O(N^2)$, but they are independent of $x$
and can therefore be precomputed (Moreover, for various important sets of nodes
there exist fast algorithms. For Chebyshev nodes there is an explicit
experession; see below.). Since the common factor $\ell(x)$ does not depend on
$j$ we can now evaluate all $\ell_j(x), j = 0, \dots, N$ at $O(N)$ cost and thus
obtain the {\em first form of the barycentric interpolation formula},
\begin{equation} \label{eq:poly:bary1}
   p(x) = \ell(x) \sum_{j = 0}^n \frac{\lambda_j}{x - x_j} f_j.
\end{equation}
Once the weights $\lambda_j$ have been precomputed, the cost of evaluating
$p(x)$ becomes $O(N)$. However, \eqref{eq:poly:bary1} has a different
shortcoming: in floating point arithmetic it is prone to overflow or underflow.
Specifically, suppose that $x = -1$ and we compute $\ell(x)$ with $x_j$
ordered decreasingly as defined in \eqref{eq:poly:chebnodes}, then after
approximately the first $M \approx N/4$ terms we have evaluated
\[
   \bg|\prod_{n = 0}^M (x - x_j) \bg|
   \geq \b( \smfrac34 \b)^{M+1}
\]
which quickly becomes very large. The issue is also reflected in the
coefficients $\lambda_j$, which for Chebyshev points are $O(2^N)$ (cf.
Exercise~\ref{exr:poly:bary}). In practise, one typically gets overflow
beyond 100 or so grid points.

This can be avoided with the second form of the barycentric formula: observing
that $\sum_{j = 0}^N \ell_j \equiv 1$ we obtain
\[
   1 = \ell(x) \sum_{j = 0}^N \frac{\lambda_j}{x-x_j},
\]
and hence arrive at the second form of the barycentric interpolation formula:

\begin{theorem}[Barycentric interpolation formula]
   \label{th:poly:bary}
   Let $p \in \Poly_N$, with $p(x_j) = f_j$ at $N+1$ distincts points
   $\{x_j\}$ then
   \[
      p(x) = \frac{
         \sum_{j = 0}^N \frac{\lambda_j f_j}{x-x_j}
      }{
         \sum_{j = 0}^N \frac{\lambda_j}{x-x_j}
      },
      \qquad \text{where} \qquad
      \lambda_j = \frac{1}{ \prod_{n \neq j} (x_j-x_n)},
   \]
   with the special case $p(x_j) = f_j$.
\end{theorem}

\begin{theorem}[Barycentric interpolation formula in Chebyshev Points]
   \label{th:poly:barycheb}
   Let $\{x_j\}$ be the Chebyshev points \eqref{eq:poly:chebnodes}, then
   the barycentric weights $\lambda_j$ from \Cref{th:poly:bary}
   may be chosen as
   \[
       \lambda_j = \cases{
          (-1)^j, & j = 1, \dots, N-1, \\
          \frac12 (-1)^j, & j = 0, N. }
   \]
\end{theorem}
\begin{proof}
   See Exercise~\ref{exr:poly:bary}.
\end{proof}

\subsubsection{Numerical stability of barycentric interpolation}
%
\label{sec:poly:barystab}
%
While the DFT is matrix multiplication with an othogonal matrix, and the FFT
an algorithm that even reduced the number of operations it is natural to
expect that these algorithms are numerically stable. By contrast, this is
not at all obvious {\it a priori} for the barycentric formula. We will therefore
spend a little time discussing this.
%
To simplify this discussion we will only analyse the numerical stability
of the {\em first} barycentric formula \eqref{eq:poly:bary1}. Understanding
stability of the second barycentric formula is slightly more involved;
see \cite{Higham2004-fn} for the details.

We have to begin by explaining the standard model of floating point arithmetic.
Let $\otimes \in \{ +, -, *,  / \}$ be one of the standard four floating point
operations, then applying the operation $a \otimes b$  to two floating point
numbers will give an error, which we express as
\[
   {\rm fl}\b( a \otimes b \b) = (a\otimes b)(1+\delta),
\]
where $|\delta| \leq \eps$ and $\eps$ denotes machine precision (typically
$10^{-6}$). That is, floating point arithmetic controls the {\em relative
error}. For more on this topic, in particular additional subtleties that we are
sweeping under the carpet here, see \cite{Higham2002-nk}.

For example, consider the evaluation of an inner product of two vectors
${\bf a}, {\bf b} \in \R^2$,
\begin{align*}
   \fl({\bf a} \cdot {\bf b})
   &= \fl\b( \fl(a_1 b_1) + \fl(a_2b_2)\b) \\
   &= \fl\b( a_1 b_1 (1+\delta_1) + a_2b_2 (1+\delta_2)) \\
   &= \b( a_1 b_1 (1+\delta_1) + a_2b_2 (1+\delta_2))(1+\delta_3) \\
   &= a_1 b_1 (1+\delta_1)(1+\delta_3)
      + a_2b_2 (1+\delta_2)(1+\delta_3).
\end{align*}
Upon setting
\[
   \tilde{a_1} = a_1 (1+\delta_1), \quad
   \tilde{b_1} = b_1 (1+\delta_3), \quad
   \tilde{a_2} = a_2 (1+\delta_2), \quad
   \tilde{b_2} = b_2 (1+\delta_3),
\]
we obtain
\[
   \fl({\bf a} \cdot {\bf b}) = \tilde{\bf a} \cdot \tilde{\bf b},
\]
where $\|{\bf a} - \tilde{\bf a}\| = O(\eps)$ and $\|{\bf b} - \tilde{\bf b}\| =
O(\eps)$. This is called {\em backward stability}: the numerically evaluated
quantity is the exact quantity for an exact computation with perturbed data.

% As a second example we can consider
% \begin{align*}
%    \fl\bg( \frac{f(x+h) - f(x)}{h} \bg)
%    &= \frac{(f(x+h)(1+\delta_1) - f(x))(1+\delta_2)}{h} (1+\delta_3)  \\
%    &= \frac{f(x+h) - f(x)}{h}(1+\delta_3) +
% \end{align*}

We can now turn to the first barycentric formula. First we consider the
evalutation of a weight $\ell(x)$,
\begin{align}
   \fl\B( \prod_{n=0}^N (x-x_n) \B)
   &=
   \fl\bg( \fl\bg( \prod_{n=0}^{N-1} (x-x_n)\bg) * \fl(x-x_N) \bg)  \\
   &=
   \fl\bg( \prod_{n=0}^{N-1} (x-x_n)\bg) * (x-x_N) (1+\delta_{1}) (1+\delta_{2}),
\end{align}
and by induction
\begin{align}
   \fl\bg( \prod_{n=0}^N (x-x_n) \bg) =
   \ell(x) \, \prod_{m = 1}^{2N+1} (1+\delta_m).
\end{align}
The argument for $\lambda_j$ is of course analogous, hence we obtain
with a little extra work:

\begin{proposition} \label{th:poly:barystab}
   Let
   \[
      \tilde{p}_N(x) := \fl\bg( \ell(x) \sum_{j = 0}^N \frac{\lambda_j}{x - x_j} \bg)
   \]
   be the numerically evaluated polynomial in the standard model of
   floating point arithmetic, then
   \[
      \tilde{p}_N(x) =
      \ell(x) \sum_{j = 0}^N \frac{\lambda_j f_j}{x - x_j}
      \prod_{m = 1}^{5N+5} (1+\delta_{jm}).
   \]
\end{proposition}
\begin{proof}
   This is a straightforward continuation of the calculations above.
\end{proof}

The key point of \Cref{th:poly:barystab} is that this is a {\em backward
stability} result, i.e., let $\tilde{f}_j = f_j\prod_{m = 1}^{5N+5}
(1+\delta_{jm})$, then $\hat{p}_N$ is interpolates the values $\tilde{f}_j$.
In particular, the error in the floating point polynomial $\hat{p}_N(x)$
is no larger than if we had small errors in the nodal values $f_j$, which
we will normally have anyhow.

Finally, for the second barycentric formula, the numerical stability result is
weaker, but one can still show that for interpolation nodes with moderate
Lebesgue constant, and reasonable functions $f$ that we are interpolating, the
numerical stability is of no concern; see \cite{Higham2004-fn} for more details.

\subsection{Applications}

The following applications of the theory in this chapter will be
covered in \nbpoly.

\begin{itemize}
   \item Evaluating special functions
   \item Approximating a Matrix function
   \item Spectral methods for BVPs; see also \cite[Sec. 21]{Trefethen2013-rg}
\end{itemize}

\noindent Further applications that could be explored in self-directed reading:
\begin{itemize}
   \item Chebyshev filtering
   \item Conjugate gradients and other Krylov methods
   \item Quadrature: \cite{Trefethen2013-rg},
   \item Richardson extrapolation: \cite{Trefethen2013-rg}, p. 258
   \item \dots
\end{itemize}

\subsection{Exercises}
%
% IDEAS FOR COMPUTATIONAL EXERCISES:
% - test instability of Vandermonde interpolation
% - super-exponential convergence for entire functions ...
%   e^x should be easy to analyse too! Also e^{-x^2}!
%   http://www.chebfun.org/examples/approx/EntireBound.html

% \begin{exercise}
%    \label{exr:poly:vandermonde}
%    The seemingly ``canonical'' approach to constructing a polynomial interpolant
%    in the monomial basis is via the linear system
%    \[
%       c_0 + c_1 x_j + c_2 x_j^2 + \cdots + c_N x_j^N = f_j.
%    \]
%    The system matrix $V = (x_j^n)_{j,n=0}^N$ is called a {\em Vandermonde matrix}.

%    1. Suppose we take $x_j$ to be equispaced points on the complex unit circle,
%    i.e., $x_j = e^{i2\pi j/N}$, $j = 0, \dots, N-1$ (i.e., $N \to N-1$!, then
%    show that $V$ is the discrete Fourier transform operator, in particular it
%    is unitary (up to rescaling) and thus has condition number $\kappa(V) = 1$.

%    2.

%    Show that

%    \alert{condition number of the Vandermonde matrix}.
% \end{exercise}

\begin{exercise}[Interpolation: Existence and Uniqueness]
   \label{exr:poly:interpunique}
   Prove that for any collection of nodes $z_0, \dots, z_N \subset \C$ with $x_i
   \neq z_j$  for $i \neq j$, and nodal values $f_j$, there exists a unique
   interpolant $p \in \Poly_N$ such that $p(z_j) = f_j$.
\end{exercise}

\begin{exercise}[Runge Phenomenon]
   \label{exr:poly:Runge Phenomenon}
   %
   For a partial explanation of the Runge phenomenon (cf \nbpoly) consider
   the following steps:
   \begin{enumerate} \ilist
      \item Suppose $f \in C^{N+1}([-1,1])$. Prove that there exists
      $\xi \in (-1,1)$ such that
      \[
         f(x) - I_N f(x) =  \frac{f^{(N+1)}(\xi)}{(N+1)!}
            \ell_N(x),
      \]
      where $\ell_N(x)$ is the node polynomial for the interpolation points.

      {\it Hint: Let $e(t) = f(t)-I_N f(t)$ and show that $y(t) = e(t) - e(x)
      \ell(t) / \ell(x)$ has $N+2$ roots. What does this imply about the roots
      of $y^{(N+1)}$?}

      \item Prove that for equispaces nodes, $\|\ell_N\|_\infty \geq \frac14
      (N/2)^{-N-1} (N-1)!$.

      \item For $f(x) = 1 / (1+25 x^2)$ (The Witch of Agnesi), prove that $\|
       f^{(N+1)} \|_\infty \| \ell_N \|_\infty  / (N+1)! \to \infty$ as $N \to
       \infty$. {\it [HINT: $(1+c^2x^2)^{-1} = (1+cix)^{-1}+(1-cix)^{-1}$]}

       {\it (Note this does not prove divergence but proved a strong
       hint why divergence occurs.)}
   \end{enumerate}
\end{exercise}

\begin{exercise}[Clenshaw's Algorithm]
   %
   \label{exr:poly:clenshaw}
   %
   Let $p \in \Poly_N$, $N \geq 1$, be given in the Chebyshev basis, with
   coefficients $(\tilde{f}_k)_{k = 0}^N$ and let $x \in [-1,1]$. Show that
   $p(x)$ can be evaluated by Clenshaw's algorithm:
   \begin{align*}
      & u_{N+1} = 0, \qquad u_N = \tilde{f}_N; \\
      & u_n = 2 x u_{n+1} - u_{n+2} + \tilde{f}_n, n = N-1, N-2, \dots, 0; \\
      & p(x) = \smfrac12 \b( \tilde{f}_0 + u_0 - u_2).
   \end{align*}
   What is the purpose of the Clenshaw algorithm, i.e., the potential
   advantage over simply summing over the Chebyshev basis?
\end{exercise}

\begin{exercise}[Orthogonality of $T_k$]
   Consider the weighted space
   \begin{align*}
      L^2_C &:= \b\{ f : (-1,1) \to \R, \text{ measurable, }
            \|f\|_{L^2_C} < \infty \b\}, \qquad
            \text{where} \\
      \|f\|_{L^2_C}^2 &:= \int_{-1}^1 \frac{|f|^2}{\sqrt{1 - x^2}} \,dx.
   \end{align*}
   Prove that $L^2_C$ is a Hilbert space and show that the Chebyshev
   polynomials are (up to scaling) and orthonormal basis of this space.

   Thus, conclude that the Chebyshev projection $\tilde\Pi_N$ is in fact that
   best-approximation with respect to the $\|\cdot\|_{L^2_C}$-norm.
\end{exercise}

\begin{exercise}[Bernstein Ellipse]
   \label{exr:poly:ellipse}
   %
   Prove that the Bernstein Ellipse $E_\rho$, $\rho > 1$ is indeed an ellipse
   with centre $z = 0$, foci $\pm 1$, semi-major axis $\frac12 (\rho+\rho^{-1})$
   and semi-minor axis $\frac12 (\rho-\rho^{-1})$.
\end{exercise}

\begin{exercise}[Convergence Bounds]
   \label{exr:poly:convergence}
   \begin{enumerate} \ilist
   \item Complete the proof of \eqref{eq:poly:interperror} by proving
      \[
         \| I_N \|_{L(L^\infty)} \leq C \log N,
      \]
      where $I_N$ is the Chebyshev nodal interpolation operator.

   \item In preparation for the proofs of the best approximation error estimates
      for differentiable (non-analytic) functions, prove that, if $f \in
      C([-1,1])$ with modulus of continuity $\omega$, then $g \in C(\TT)$ and it
      has the same modulus of continuity.

   \item Prove Theorem~\ref{th:poly:jackson}, case $j = 0$.

   \item Let $E_N(f) := \inf_{p \in \Poly_N} \|f - p\|_\infty$. Prove that
      \[
         E_N(f)  \leq C N^{-1} E_{N-1}(f'),
      \]
      where $C$ is independent of $N$ and try to quantify $C$.

   \item Complete the proof of Theorem~\ref{th:poly:jackson} (general $j$).
   Indeed, you should obtain a more precise formula.
   \end{enumerate}
\end{exercise}

\begin{exercise}
   \label{exr:poly:examplefunctions}
   %
   \begin{enumerate} \ilist
   \item For the following functions give bounds on the rate of polynomial best
   approximation in the max-norm, as sharp as you can manage:
   \begin{enumerate} \ilist
      \item $f(x) = |\sin(5 x)|$
      \item $f(x) = \sqrt{|x|}$
      \item $f(x) = x (1 + 1000 (x - 1/2)^2)^{-1/2}$
      \item $f(x) = e^{- \cos(3x)}$
      \item $f(x) = x^{100}$
      \item $f(x) = e^{-x^2}$
      \item $f(x) = {\rm sign}(x)$
   \end{enumerate}
   \item and for the following two functions also in the $L^2$-norm:
   \begin{itemize}
      \item $f(x) = {\rm sign}(x)$
      \item $f(x) = \sqrt{|x|}$ \qedhere
   \end{itemize}
   \end{enumerate}
\end{exercise}

\begin{exercise}[Barycentric Chebyshev Interpolation]
   %
   \label{exr:poly:bary}
   %
   Let $x_j$ be the Chebyshev points on $[-1,1]$.
   \begin{enumerate} \ilist
   \item In general (not only for Chebyshev points), demonstrate that the
   barycentric weights satisfy $\lambda_j = 1 / \ell'(x_j)$.
   \item Prove that the node polynomial satisfies
   \[
      \ell(x) = 2^{-N} \b(T_{N+1}(x) - T_{N-1}(x)\b)
   \]
   \item Show that
   \[
      T_{N+1}'(x_j) - T_{N-1}'(x_j) =
      \cases{
         2N(-1)^j, & 1 \leq j \leq N-1, \\
         4N(-1)^j, & j = 0, N.
      }
   \]
   \item Deduce that, if $\lambda_j$ is given by \eqref{eq:poly:bary_weights}, then
   \[
      \lambda_j = \frac{2^{N-1}}{N} (-1)^j, \qquad j = 1, \dots, N-1,
   \]
   and suitably adjusted for $j = 0, N$. Explain why we can rescale the weights
   $\lambda_j$ without changing the validity of the barycentric formula, and
   hence complete the proof of Theorem~\ref{th:poly:barycheb}.
   \end{enumerate}

   {\it WARNING: it turns out, this exercise needs more material than I
   realised, namely aliasing of Chebyshev coefficients. It is still very
   interesting so I will leave it here for now. An interested reader should
   follow to \cite[Sec. 5]{Trefethen2013-rg} to derive this formula.}
\end{exercise}

{\it NOTE: The last exercise is a bit tedious; it needs to be redesigned
a bit. Maybe best leave it for now.}

\begin{exercise}[Coordinate Tranformations]
   \label{exr:poly:coordinates}
   %
   The purpose of this exercise is to investigate how the choice of
   coordinate systems can expand the range of approximable functions, as
   well as have an affect on the rate of convergence.

   The basic idea is to considier functions $F : [a, b] \to \R$ and via a
   coordinate transformation $f(x) = F(\xi(x))$ transform them to functions $f :
   [-1,1] \to \R$. This can can multiple consequences, including: (1) we can
   represent functions on an arbitrary interval (inclusinf $\R$); (2) we can
   transform functions in such a way to increase the region of analyticity and
   thus accelerate convergence.
   %
   \begin{enumerate} \ilist
      \item Consider the Morse potential $F(y) = e^{-2\alpha y} - 2 e^{-\alpha
      y}$, then $F(y) = f(e^{-\alpha y})$ where $f(x) = x^2 - 2x$ is a quadratic
      polynomial. Suppose though that this ``optimal'' coordinate transform $x =
      e^{-y}$ is not known.

      Instead, consider the Morse coordinate transformation $\xi^{-1}(y) = 2
      e^{-y} - 1$ and the transformed function $f(x) = F(\xi(x))$.

      coordinate transformation $x = 2/(1+y) - 1 =
      \xi^{-1}(y)$, that is, $\xi^{-1}(0) = 1, \xi^{-1}(\infty) = -1$, and
      let $f(x) = F(\xi(x))$.
      \begin{enumerate} \alist
         \item Establish an upper bound (as sharp as you can manage) for
          approximation by Chebyshev projection and interpolation of $f$ on
          $[-1,1]$ in the max-norm.
         \item Convert this bound to an approximation result for $F(y)$ on $[0,
          \infty)$.
         \item Can you give a simpler / more direct characterisation of the
         effective approximation space for functions on $[0, \infty)$ that you
         used here?
      \end{enumerate}

      \item Now consider the function $F(y) = (\eps^2 + y^2)^{-1/2}$ on
      $[-1, 1]$. Recall the rate of convergence of Chebyshev projection and
      Chebyshev interpolation.

      Now consider a coordinate transformation
      \[
         \xi^{-1}(y) = \frac{\arctan(x/\eta)}{\arctan(1/\eta)},
      \]
      and explicitly compute its inverse. Show that $\xi, \xi^{-1} : [-1,1] \to
      [-1,1]$ are bijective.

      \begin{enumerate} \alist
         \item For any $\eta > 0$ establish an upper bound (as sharp as you can
         manage) for approximation of $f(x) = F(\xi(x))$ by Chebyshev projection
         and Chebyshev interpolation.

         \item Discuss which choices of $\eta$ appear to be particularly good.
         Visualise the effect of $\xi$ on the function $f$ as well as on
         the singularities in the complex plane.
      \end{enumerate}
   \end{enumerate}
\end{exercise}


\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{parskip}
\usepackage{svg}
\usepackage[utf8]{inputenc}
\usepackage{helvet}
\renewcommand{\familydefault}{\sfdefault}
\usepackage{geometry}
\usepackage[document]{ragged2e}
\geometry{letterpaper, portrait, top=1in, bottom=1in, left=1.5in, right=1.5in}
\usepackage{comment}

\title{MATH 153}

\begin{document}
\section{Sequences}

A sequence is a list of numbers written in a defined order

\begin{gather*}
    a_1, a_2, a_3, \cdots
\end{gather*}

Sequences can also be denoted
\begin{gather*}
    \{a_n\} \\
    \{a_n\}^\infty_{n=1}
\end{gather*}

Converging sequences settle on a certain number while diverging sequences does not settle on a number.

Recursive sequence: A sequence that depends on previous values in the sequence.

\section{Limit of a sequence}

L'Hospital's rule: For differentiable functions $f$ and $g$, if $\lim_{x \to c} f(x) = \lim_{x \to c} g(x) = 0$ or $\pm \infty$, and $g'(x) \neq 0$ for all $x$, and $\lim_{x \to c} \frac{f'(x)}{g'(x)}$ exists, then

\begin{gather*}
    \lim_{x \to c} \frac{f(x)}{g(x)} = \lim_{x \to c} \frac{f'(x)}{g'(x)}
\end{gather*}

Limit laws:

Let $\{a_n\}$ and $\{b_n\}$ be convergent sequences and $c$ be a constant.

\begin{gather*}
\lim_{n \to \infty} (a_n + b_n) = \lim_{n \to \infty} a_n + \lim_{n \to \infty} b_n \\
\lim_{n \to \infty} (a_n - b_n) = \lim_{n \to \infty} a_n - \lim_{n \to \infty} b_n \\
\lim_{n \to \infty} (a_n b_n) = \lim_{n \to \infty} a_n \cdot \lim_{n \to \infty} b_n \\
\lim_{n \to \infty} ca_n = c \lim_{n \to \infty} a_n \\
\lim_{n \to \infty} \frac{a_n}{b_n} = \frac{\lim_{n \to \infty} a_n}{\lim_{n \to \infty} b_n} \text{ if } \lim_{n \to \infty} b_n \neq 0 \\
\lim_{n \to \infty} a^p_n = \left( \lim_{n \to \infty} a_n \right)^p \text{ if } p > 0, a_n > 0
\end{gather*}

If $\lim_{n \to \infty} |a_n| = 0$, $\lim_{n \to \infty} a_n = 0$.

If $\lim_{n \to \infty} a_n = L$ and function $f$ is continuous at $L$, $\lim_{n \to \infty} f(a_n) = f(L)$.

The sequence $\{r^n\}$ is convergent if $-1 < r \leq 1$ and divergent for all other values of $r$.

\begin{gather*}
    \lim_{n \to \infty} r^n = \begin{cases}
        0 \text{ if } -1 < r < 1 \\
        1 \text{ if } r = 1
    \end{cases}
\end{gather*}

Squeeze theorem: If $a_n \leq b_n \leq c_n$ and $\lim_{n \to \infty} a_n = \lim_{n \to \infty} c_n = L$, $\lim_{n \to \infty} b_n = L$.

Monotonic sequences: A sequence ${a_n}$ is increasing if $a_n < a_{n+1}$ for all $n \geq 1$ and is decreasing if $a_n > a_{n+1}$ for all $n \geq 1$. Monotonic sequences are either increasing or decreasing.

Bounded sequences: A sequence ${a_n}$ is bounded above if there is a number $M$ such that $a \leq M$ for all $n \geq 1$. ${a_n}$ is bounded below if there is a number $m$ such that $m \leq a_n$ for all $n \geq 1$. If ${a_n}$ is bounded above and below, then ${a_n}$ is a bounded sequence.

If a sequence is bounded and monotonic, that sequence is convergent.

\section{Ways to determine the limit}

\begin{itemize}
    \item Draw graph or construct table
    \item Break it up into smaller limits using the limit laws
    \item L'Hospital's Rule
    \item Determine how each term will change as $n$ approaches infinity
    \item Use the squeeze theorem if it converges between two functions with the same limits
    \item Substitute a variable if a common pattern appears more than once. Remember to solve its limit to replace the limit variable.
    \item If it is raised to the $n$th power, calculate the natural log of both sides.
\end{itemize}

\section{Series}

Series: The sum of all terms of a sequence.

Infinite sum:
\begin{gather*}
    \sum_{n=1}^{\infty} a_n = S
\end{gather*}

Partial sum:
\begin{gather*}
    \sum_{K=1}^{n} a_K = S_n
\end{gather*}

Partial sums can be used to determine convergence of an infinite sum.

\begin{gather*}
    \sum_{n=1}^{\infty} a_n = \lim_{n \to \infty} \sum_{K=1}^{n} a_K
\end{gather*}

If $\{S_n\}$ converges, then $\lim_{n \to \infty} S_n = S$.

\section{Geometric series}

\begin{gather*}
    \sum_{n=1}^{\infty} ar^{n-1} = a + ar + a^2 + a^3 + \cdots + ar^{n-1}
\end{gather*}

\begin{itemize}
    \item If $r \leq -1$ or $r \geq 1$, the series will diverge  \\
    \item If $-1 < r < 1$, the series will converge and the limits is \\
    \begin{gather*}
        S = \frac{a}{1-r}
    \end{gather*}
\end{itemize}

Theorems for convergent series
\begin{align*}
    \sum_{n=1}^{\infty} ca_n &= c \sum_{n=1}^{\infty} a_n \\
    \sum_{n=1}^{\infty} (a_n + b_n) &= \sum_{n=1}^{\infty} a_n + \sum_{n=1}^{\infty} b_n \\
    \sum_{n=1}^{\infty} (a_n - b_n) &= \sum_{n=1}^{\infty} a_n - \sum_{n=1}^{\infty} b_n
\end{align*}

Telescoping series: Each pair of consecutive terms have parts that cancel out, which makes it easy to find the closed form.

Test for divergence: If $\lim_{n \to \infty} a_n$ does not exist or if $\lim_{n \to \infty} a_n \neq 0$, then the series $\sum_{n=1}^{\infty} a_n$ is divergent.

\section{Integral test}

Integral Test: Let $f$ be a continuous, positive, decreasing function on $[1, \infty)$ and let $a_n = f(n)$. Then the series $\sum_{n=1}^\infty a_n$ is convergent if and only if the improper integral $\int_1^\infty f(x) dx$ is convergent.

P-series test: The $p$-series $\sum_{n=1}^\infty \frac{1}{n^p}$ is convergent if $p > 1$ and divergent if $p \leq 1$.

\section{Comparison test}

Comparison test: Let $\sum a_n$ and $\sum b_n$ be series with positive terms.
\begin{itemize}
    \item If $\sum b_n$ is convergent and $a_n \leq b_n$ for all $n$, then $\sum a_n$ is convergent.
    \item If $\sum b_n$ is divergent and $a_n \geq b_n$ for all $n$, then $\sum a_n$ is divergent.
\end{itemize}

\section{Limit comparison test}

Limit comparison test: Let $\sum a_n$ and $\sum b_n$ be series with positive terms. If
\begin{gather*}
    \lim_{n \to \infty} \frac{a_n}{b_n} = c
\end{gather*}

where $c$ is a finite number and $c > 0$, then either both series converge or both diverge.

\section{Alternating series}

Alternating series: A series whose terms are alternately positive and negative.
\begin{gather*}
    \sum_{n = 0}^\infty (-1)^{n-1} b_n = a_0 - a_1 + a_2 - a_3 + \cdots
\end{gather*}

Alternating series test: Given an alternating series $\sum (-1)^{n-1} b_n$, if $b_n$ is decreasing and $\lim_{n \to \infty} b_n = 0$, then $\sum (-1)^{n-1} b_n$ is convergent.

Alternating series estimation theorem: If $S = \sum (-1)^{n-1}b_n$ where $b_n > 0$ is the sum of a converging alternating series, then
\begin{gather*}
    |R_n| = |S - S_n| \leq b_{n + 1}
\end{gather*}

\section{Ratio test}

Absolute convergence: Given a series $\sum a_n$, if $\sum |a_n|$ converges, then $\sum a_n$ is absolutely convergent.

Conditional convergence: A series $\sum a_n$ that is convergent but not absolutely convergent.

Ratio test: Given the series $\sum_{n=1}^\infty a_n$,
\begin{gather*}
    \text{if } \lim_{n \to \infty} \left| \frac{a_{n+1}}{a_n} \right|
    \begin{cases}
        < 1 \text{, then the series is absolutely convergent} \\
        > 1 \text{ or } \infty \text{, then the series is divergent} \\
        = 1 \text{, the Ratio Test is inconclusive}
    \end{cases}
\end{gather*}

\section{Root test}

Root test: Given the series $\sum_{n=1}^\infty a_n$,
\begin{gather*}
    \text{if } \lim_{n \to \infty} |a_n|^{\frac{1}{n}}
    \begin{cases}
        < 1 \text{, then the series is absolutely convergent} \\
        > 1 \text{ or } \infty \text{, then the series is divergent} \\
        = 1 \text{, the Ratio Test is inconclusive}
    \end{cases}
\end{gather*}

\section{Tests for convergence and divergence}

\begin{itemize}
    \item Geometric series
    \item Telescoping series
    \item Test for divergence
    \item Integral test
    \item P-series test
    \item Comparison test
    \item Limit comparison test
    \item Alternating series test
    \item Ratio test
    \item Root test
\end{itemize}

\section{Power series}

The power series:
\begin{gather*}
    \sum_{n=0}^\infty c_n (x - a)^n
\end{gather*}

This power series is centered around $a$.

For a given power series $\sum_{n=0}^\infty c_n (x - a)^n$, there are only three possibilities:
\begin{itemize}
    \item The series converges only when $x=a$
    \item The series converges for all x
    \item There is a positive number $R$ such that the series converges if $|x - a| < R$ and diverges if $|x - a| > R$
\end{itemize}

The interval of convergence is the interval for $x$ such that the series converges.

The radius of convergence is half the distance of the interval of convergence.

Sometimes, such as for the ratio test and root test, the endpoints must be checked for convergence.

\section{Representations of functions as power series}

\begin{gather*}
    \frac{1}{1-x} = \sum_{n=0}^\infty x^n \\
    \text{where } |x| < 1
\end{gather*}

Term-by-term differentiation and integration:

If the power series $\sum_{n=0}^\infty c_n (x - a)^n$ has radius of convergence $R > 0$, then the function $f$ defined by

\begin{gather*}
    f(x) = c_0 + c_1(x+a) + c_2(x-a)^2 + \cdots = \sum_{n=0}^\infty c_n (x - a)^n
\end{gather*}

is differentiable on the interval $(a - R, a + R)$ and

\begin{gather*}
    f'(x) = c_1 + 2 c_2 (x - a) + 3 c_3 (x - a)^2 + \cdots = \sum_{n=1}^\infty n c_n (x - a)^{n-1} \\
    \int f(x) dx = C + c_0 (x - a) + c_1 \frac{(x-a)^2}{2} + c_2 \frac{(x-a)^3}{3} + \cdots = C + \sum_{n=0}^\infty c_n \frac{(x - a)^{n+1}}{n + 1}
\end{gather*}

The radii of convergence of both equations are $R$.

\section{Taylor and Maclaurin Series}

If the power series representation at $a$ is
\begin{gather*}
    f(x) = \sum_{n=0}^\infty c_n (x - a)^n
\end{gather*}

then its Taylor series is

\begin{gather*}
    f(x) = \sum_{n=0}^\infty \frac{f^{n}(a)}{n!} (x - a)^n
\end{gather*}

Maclaurin series: The Taylor series but with $a = 0$

Important Maclaurin series
\begin{align*}
    \frac{1}{1-x} &= \sum_{n=0}^\infty x^n, R = 1 \\
    e^x &= \sum_{n=0}^\infty \frac{x^n}{n!}, R = \infty \\
    \sin(x) &= \sum_{n=0}^\infty (-1)^n \frac{x^{2n+1}}{(2n+1)!}, R = \infty \\
    \cos(x) &= \sum_{n=0}^\infty (-1)^n \frac{x^{2n}}{(2n)!}, R = \infty \\
    \tan^{-1}(x) &= \sum_{n=0}^\infty (-1)^n \frac{x^{2n+1}}{2n+1}, R = 1 \\
    \ln(1 + x) &= \sum_{n=1}^\infty (-1)^{n-1} \frac{x^n}{n}, R = 1 \\
    (1 + x)^k &= 1 + kx + \frac{k(k-1)}{2!}x^2 + \frac{k(k-1)(k-2)}{3!}x^3 + \cdots = \sum_{n=0}^\infty {k \choose n} x^n, R = 1
\end{align*}

\section{Parametric equations}
Unlike rectangular equations, parametric equations have a third variable $t$.
It has an initial point and terminal point for the minimum and maximum $t$ values respectively.

First derivative (Slope):
\begin{gather*}
    \frac{dy}{dx} = \frac{\frac{dy}{dt}}{\frac{dx}{dt}} \\
    \text{if }\frac{dx}{dt} \neq 0
\end{gather*}

Second derivative (Concavity):
\begin{gather*}
    \frac{d^2 y}{dx^2} = \frac{\frac{d}{dt}\left( \frac{dy}{dx} \right)}{\frac{dx}{dt}}
\end{gather*}

If a curve is defined by $x = f(t), y = g(t), \alpha \leq t \leq \beta$, the area under the curve is
\begin{gather*}
    \int_\alpha^\beta g(t) f'(t) dt
\end{gather*}

The arc length is
\begin{gather*}
    \int_\alpha^\beta \sqrt{\left(\frac{dx}{dt}\right)^2 + \left(\frac{dy}{dt}\right)^2} dt
\end{gather*}

\section{Polar coordinates}

Converting from polar coordinates to Cartesian coordinates
\begin{gather*}
    x = r \cos(\theta) \\
    y = r \sin(\theta)
\end{gather*}

Converting from Cartesian to polar coordinates
\begin{gather*}
    r^2 = x^2 + y^2 \\
    \tan(\theta) = \frac{y}{x}
\end{gather*}

Distance formula
\begin{gather*}
    d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}
\end{gather*}

\begin{itemize}
    \item If $f(\theta) = f(-\theta)$, the curve is symmetric about the polar axis. \\
    \item If $f(\theta) = f(\theta + \pi)$, the curve is symmetric about the pole. \\
    \item If $f(\theta) = f(\pi - \theta)$, the curve is symmetric about $\theta = \pi / 2$.
\end{itemize}

Area of $R$:

\begin{gather*}
    \int_a^b \frac{1}{2}(f(\theta))^2 d\theta
\end{gather*}

Trig identities

\begin{align*}
    \sin^2(\theta) &= \frac{1-\cos(2\theta)}{2} \\
    \cos^2(\theta) &= \frac{1+\cos(2\theta)}{2}
\end{align*}

\section{Vectors}

A vector:
\begin{itemize}
    \item Has a magnitude and direction.
    \item Has an initial point and terminal point
    \item A vector from $A$ to $B$ is written $\overrightarrow{AB}$
\end{itemize}

Scalar multiplication of vectors: If $c$ is a scalar and $v$ is a vector, the scalar multiple $cv$ is the vector whose length is $|c|$ times the length of $v$ and whose direction is the same as $v$ if $c > 0$ and is opposite to $v$ if $c<0$. If $c=0$, then $cv=0$.

Vector addition: The sum of vectors $u$ and $v$, $u+v$, is a vector from the initial point of $u$ to the terminal point of $v$. $u + v = v + u$.

Position vector: A vector that represents a coordinate relative to an origin.

Given the points $A(x_1, y_1, z_1)$ and $B(x_2, y_2, z_2)$, the vector $\overrightarrow{AB} = \left\langle x_2 - x_1, y_2 - y_1, z_2 - z_1 \right\rangle$

Unit vector: Vectors that have a magnitude of $1$.

\begin{align*}
    \hat{i} &= \text{unit vector in $x$ direction} \\
    \hat{j} &= \text{unit vector in $y$ direction} \\
    \hat{k} &= \text{unit vector in $z$ direction}
\end{align*}

Magnitude of a vector:
\begin{gather*}
    |a| = \sqrt{a_1^2 + a_2^2}
\end{gather*}

\begin{gather*}
    \hat{u} = \frac{\overrightarrow{a}}{|\overrightarrow{a}|}
\end{gather*}

Equation of a sphere:
\begin{gather*}
    (x-h)^2 + (y-k)^2 + (z-l)^2 = r^2
\end{gather*}

\end{document}


% Prelim, Appendix A
% by Rachel Slaybaugh
\separatorpage{}
\chapter{Diffusion Equation}
\label{sec:AppendixA}
The diffusion approximation is a widely used simplification that reduces the computational complexity of the transport equation. The approximation is that the angular dependence of the flux is unimportant, so the direction component of the transport equation can be discarded. Physically this means that neutrons move against their concentration gradient like just heat diffuses through a conductor. The information in this section is derived from Duderstadt and Hamilton's \emph{Nuclear Reactor Analysis} \cite{Duderstadt1976} and neglects fission for simplicity.

The first step in applying this approximation is to integrate the angular dependence out of Equation \eqref{eq:neutron transport}, resulting in the neutron continuity equation:
%
\begin{equation}
  \nabla \cdot J(\vec{r},E) + \Macro(\vec{r},E)\phi(\vec{r},E) = \int \:dE' \:\Macro_{s}(\vec{r}, E' \to E)\phi(\vec{r},E') + Q_{ex}(\vec{r},E) \:,
  \label{eq:continuity}
\end{equation}
%
where the following definitions have been used:\\
\indent $J(\vec{r},E) = \int d\vOmega \:\vOmega \psi(\vec{r}, \vOmega, E)$ is the neutron current \\
\indent  $\phi(\vec{r},E) = \int d\vOmega \:\psi(\vec{r}, \vOmega, E)$ is the scalar flux, and \\
\indent $Q_{ex} (\vec{r},E)= \int d\vOmega \:q_{ex}(\vec{r}, \vOmega, E)$ is the external source.

Unfortunately, this simplifying approximation added another unknown, $J$, which leaves one equation with two unknowns. In an attempt to eliminate one of these unknowns, Equation \eqref{eq:continuity} is multiplied by $\hat{\Omega}$ and integrated over angle again to obtain the first angular moment:
%
\begin{align}
  \nabla \cdot \int  d\vOmega \:\vOmega \vOmega \psi(\vec{r}, \vOmega, E) &+ \Macro(\vec{r},E) J(\vec{r},E)= \nonumber \\
  &\int dE' \:\Macro_{s1}(\vec{r}, E' \to E)J(\vec{r},E') + \int d\vOmega \int d\vOmega \:\vOmega q_{ex} \:,
  \label{eq:current1}
\end{align}
%
where $\Macro_{s1}  = \int d\vOmega \:\vOmega \Macro_{s}$. The first angular moment form of the equation cannot be solved either because the streaming (first) term is still unknown.

To make Equation \eqref{eq:current1} solvable, the original assumption is modified to assert that the angular flux is weakly, in fact linearly, dependent on angle rather than independent of angle. To implement this assumption the angular flux is expanded in angle and only the first two terms are retained:
%
\begin{equation}
  \psi(\vec{r}, \vOmega, E) \cong \frac{1}{4 \pi} \phi(\vec{r}, E) + \frac{3}{4 \pi}J(\vec{r}, E) \cdot \vOmega \:.
  \label{eq:angExpand}
\end{equation}
The truncated angular flux is then inserted into the streaming term in Equation \eqref{eq:current1}, giving
%
\begin{equation}
  \nabla \cdot \int d \vOmega \:\vOmega \vOmega \psi(\vec{r}, \vOmega, E)  \cong \frac{1}{3} \nabla \phi(\vec{r}, E) \:.
  \label{eq:firstTerm}
\end{equation}

Next, the scattering source term is simplified in angle and energy. To address the angular dependence define $\bar{\mu}_{0}$ as the average cosine of the scattering angle, which, temporarily suppressing energy, gives $\Macro_{s1} = \bar{\mu}_{0}\Macro_{s}$. For elastic scattering from stationary nuclei when s-wave scattering is present in the center of mass frame, $\bar{\mu_{0}} = \frac{2}{3A}$ where $A$ is atomic mass number. A common procedure to simplify the energy dependence is to neglect the anisotropic contribution to energy transfer in a scattering collision. Mathematically this means $\Macro_{s1}(E' \to E) = \Macro_{s1}(E) \delta(E' = E)$, giving $\int dE' \:\Macro_{s1}(\vec{r}, E' \to E)J(\vec{r},E') = \bar{\mu_{0}}\Macro_{s}(\vec{r},E)J(\vec{r},E)$. Finally, it is assumed that the external source is isotropic, $\int  d\vOmega \int d\vOmega \:\vOmega q_{ex} = 0$.

If these approximations are all included and Equation \eqref{eq:current1} is solved for $J$, the result is Fick's Law:
%
\begin{equation}
  J(\vec{r},E) \cong -\frac{1}{3(\Macro(\vec{r},E) - \bar{\mu_{0}}\Macro_{s}(\vec{r},E))} \nabla \phi (\vec{r},E) = -D(\vec{r},E) \nabla \phi (\vec{r},E) \:.
\end{equation}
%
Fick's Law can be introduced back into Equation \eqref{eq:continuity} to obtain the diffusion equation:
%
\begin{equation}
  -\nabla \cdot D(\vec{r},E) \nabla \phi(\vec{r},E) + \Macro(\vec{r},E)\phi(\vec{r},E) = \int dE' \:\Macro_{s}(\vec{r}, E' \to E)\phi(\vec{r},E') + q_{ex}(\vec{r},E) \:.
  \label{eq:diffusion}
\end{equation}

This equation now includes several assumptions which are valid when the solution is not near a void, boundary, source, or strong absorber. While these requirements can be quite restrictive, the diffusion equation has been used frequently for analysis of nuclear systems throughout the history of the nuclear industry.

Some terms from this section that are used when describing DSA in Chapter \ref{sec:Chp4} are:
\begin{align}
  \Macro_{tr} &= \Macro(\vec{r},E) - \bar{\mu_{0}}\Macro_{s}(\vec{r},E) \qquad \text{is the transport cross section, and}\\
  \ve{C} &= -\nabla \cdot \frac{1}{3\Macro_{tr}}\nabla+ \Macro(\vec{r},E) \qquad \text{is the diffusion operator.}
\end{align}


\section{Policy Gradient Algorithms}

\begin{frame}{Policy Gradient Algorithms}

	\begin{block}{Key Idea}
	\begin{enumerate}
		\item<+-> The optimal policy $\pi_*$ is approximated with a parametric policy $\pi_{\theta^*}$
		\item<+-> The parameters $\theta^*$ of the policy solve the optimization problem
		\begin{equation*}
			\max_{\theta \in \Theta} J(\theta) = V_{\pi_\theta}(s)
		\end{equation*}
		\item<+-> Which is solved numerically via gradient descent
		\begin{equation*}
		\theta_{k+1} = \theta_k + \alpha_k
       		\tikz[baseline]{
           		\node[anchor=base] (gradient) {$\nabla_\theta J\left(\theta_k\right)$};
       	   }
		\end{equation*}
	\end{enumerate}
	\end{block}

	\onslide<+-|handout:0>{
		\begin{tikzpicture}[overlay]
				\node[draw=SteelBlue, circle, line width=3pt, minimum size=1.8cm] at (8.67, 1.65) (g) {};
				\node[SteelBlue] at (11,-0.2) (q) {\Huge \textbf{?}};
		        \draw [->, line width=3pt, SteelBlue] (q) to [bend left=35] (g);
		\end{tikzpicture}
	}
\end{frame}

\begin{frame}{The Keystone of Policy Gradient Algorithms}

	\onslide<1->
	\begin{alertblock}{Policy Gradient Theorem}
		\begin{equation*}
			\nabla_\theta J(\theta) =
			\E[\substack{S \sim d^\theta\\A \sim \pi_\theta}]{\nabla_\theta\log
			\pi_\theta(S,A) Q_{\theta}(S, A)}
		\end{equation*}
	\end{alertblock}

	\onslide<2->{
	For an episodic environment, the policy gradient can be approximated via Monte-Carlo
	\begin{equation*}
		\nabla_\theta J(\theta_k) \approx \frac{1}{M} \sum_{m=0}^M
		 \sum_{u=0}^{T^{(m)}-1} \nabla_\theta\log \pi_{\theta_k} \left(s_u^{(m)}, a_u^{(m)}\right) \sum_{v \geq u}^{T^{(m)}-1} \gamma^{v-u} r_{v+1}^{(m)}
	\end{equation*}
	}

	\onslide<3->{
		However, this estimate is characterized by a large variance. Possible improvements:
		\begin{enumerate}
			\item Optimal baseline
			\item Actor-critic methods
			\item Natural gradient
		\end{enumerate}
	}
\end{frame}

\begin{frame}{Policy Gradient with Parameter-Based Exploration (PGPE)}
	\begin{block}{Key Idea}
		\begin{enumerate}
			\item<+-> Actions are selected using a deterministic parametric controller $F_\theta$
			\item<+-> The controller parameters are drawn from a probability distribution $p_\xi$
			\item<+-> The search for an optimum is performed in the space of the hyperparameters $\xi$
		\end{enumerate}
	\end{block}

	\onslide<+->{
		More formally, the update scheme becomes
			\begin{equation*}
				\xi_{k+1} = \xi_k + \alpha_k \nabla_\xi J(\xi_k)
			\end{equation*}
		where the policy gradient is given by
		\begin{alertblock}{Parameter-Based Policy Gradient Theorem}
			\begin{equation*}
				\nabla_\xi J(\xi) = \E[\substack{S \sim d^\xi\\\theta \sim p_\xi}]{\nabla_\xi \log p_\xi(\theta) Q_{\xi}\left(S, F_\theta(S)\right)}
			\end{equation*}
		\end{alertblock}
	}
\end{frame}


This chapter gives routines and tools for computing and using scalar and vector wave functions in free-space spherical coordinates. The wave functions represent the frequency-domain multipoles of the field, which together give the multipole expansion of a field. While the wave functions themselves are not always used, the coefficients of the expansions are quite useful. The expansion coefficients can be quickly manipulated by rotation and translation addition theorems to transform the fields in different reference frames (see Chapters \ref{chap:rotation} and \ref{chap:translation}). This chapter provides codes for spherical wave functions and serves as a quick reference for certain properties and derivations.

\section{Indexing}

Spherical harmonics and spherical wave functions are defined by the degree and order of the underlying associated Legendre polynomials, $(l,m)$. It is convenient to store and access harmonics using a linear index. For vector wave functions, where the harmonics range from $l = 1,...,L$ and $m = \pm l$, the linear index for harmonic $(l,m)$ is given by $n = l^2 + l + m$. When the set of harmonics includes all $m$ at each $l$, the total number of harmonics is $N = L^2 + 2L$. The following sequence converts a linear index $n$ to $(l,m)$ for vector wave functions:
\begin{eqnarray}
l &=& \lfloor \sqrt{n} \rfloor \\
m &=& n - l^2 - l
\end{eqnarray}

Table \ref{linearindexvec} illustrates the mapping between linear index and harmonic for vector waves. A similar table appears in \cite{tsang2000scattering}.  Scalar wave functions include degree $l=0$, which is the monopole. The linear index is then given by $n = l^2 + l + m + 1$.  When the set of harmonics includes all $m$ at each $l$, the total number of harmonics is $N = L^2 + 2L + 1$.  The following sequence converts a linear index $n$ to $(l,m)$ for scalar waves:
\begin{eqnarray}
l &=& \lfloor \sqrt{n-1} \rfloor \\
m &=& n - l^2 - l -1
\end{eqnarray}

Table \ref{linearindexmono} illustrates the mapping for scalar wave functions including the monopole.

The function \texttt{lm2ind} returns the linear index for pair $(l,m)$, while \texttt{ind2lm} outputs the $(l,m)$ pair given the index.   Use the string switch \texttt{'mono'} to include the monopole for scalar waves. \texttt{lmtable} produces a table of $(l,m)$ pairs. These routines are useful for general purpose manipulation of spherical harmonic indexing or when developing routines. However, the error checking slows them down and should be replaced by inline expressions once routines are debugged.

\begin{table}[H]
\parbox{.45\linewidth}{
\caption{Vector Wave Function Harmonic Indexing}
\centering
\begin{tabular}{|c|c|c|}
\hline
Linear index & $l$ & $m$ \\
\hline
1 &      1  &  -1 \\
2 &      1  &   0\\
3 &      1  &   1\\
\hline
4 &      2  &  -2\\
5 &      2   & -1\\
6 &      2   &  0\\
7 &      2   &  1\\
8 &      2   &  2\\
\hline
9 &      3   & -3\\
10 &      3  &  -2\\
11 &      3  &  -1\\
12 &      3  &   0\\
13 &      3   &  1\\
14 &      3   &  2\\
15 &      3  &   3 \\
\hline
\end{tabular}
\label{linearindexvec}
}
\hfill
\parbox{.45\linewidth}{
\centering
\caption{Scalar Wave Function Harmonic Indexing}
\centering
\begin{tabular}{|c|c|c|}
\hline
Linear index & $l$ & $m$ \\
\hline
1 & 	0 & 0 \\
\hline
2 &      1  &  -1 \\
3 &      1  &   0\\
4 &      1  &   1\\
\hline
5 &      2  &  -2\\
6 &      2   & -1\\
7 &      2   &  0\\
8 &      2   &  1\\
9 &      2   &  2\\
\hline
10 &      3   & -3\\
11 &      3  &  -2\\
12 &      3  &  -1\\
13 &      3  &   0\\
14 &      3   &  1\\
15 &      3   &  2\\
16 &      3  &   3 \\
\hline
\end{tabular}
%\caption{Scalar Wave Function Harmonic Indexing}
\label{linearindexmono}
}
\end{table}

%
%\begin{table}[htp]
%\caption{Vector Wave Function Harmonic Indexing}
%\begin{center}
%\begin{tabular}{|c|c|c|}
%\hline
%Linear index & $l$ & $m$ \\
%\hline
%1 &      1  &  -1 \\
%2 &      1  &   0\\
%3 &      1  &   1\\
%\hline
%4 &      2  &  -2\\
%5 &      2   & -1\\
%6 &      2   &  0\\
%7 &      2   &  1\\
%8 &      2   &  2\\
%\hline
%9 &      3   & -3\\
%10 &      3  &  -2\\
%11 &      3  &  -1\\
%12 &      3  &   0\\
%13 &      3   &  1\\
%14 &      3   &  2\\
%15 &      3  &   3 \\
%\hline
%\end{tabular}
%\end{center}
%\label{linearindexvec}
%\end{table}

%
%\begin{table}[htbp]
%\caption{Scalar Wave Function Harmonic Indexing}
%\begin{center}
%\begin{tabular}{|c|c|c|}
%\hline
%Linear index & $l$ & $m$ \\
%\hline
%1 & 	0 & 0 \\
%\hline
%2 &      1  &  -1 \\
%3 &      1  &   0\\
%4 &      1  &   1\\
%\hline
%5 &      2  &  -2\\
%6 &      2   & -1\\
%7 &      2   &  0\\
%8 &      2   &  1\\
%9 &      2   &  2\\
%\hline
%10 &      3   & -3\\
%11 &      3  &  -2\\
%12 &      3  &  -1\\
%13 &      3  &   0\\
%14 &      3   &  1\\
%15 &      3   &  2\\
%16 &      3  &   3 \\
%\hline
%\end{tabular}
%\end{center}
%\label{linearindexmono}
%\end{table}

{\footnotesize
\VerbatimInput{\code/Wavefunctions/lm2ind.m}
}
{\footnotesize
\VerbatimInput{\code/Wavefunctions/ind2lm.m}
}

{\footnotesize
\VerbatimInput{\code/Wavefunctions/lmtable.m}
}

\clearpage
\section{Spherical Harmonics}

\subsection{$Y_{lm}(\theta,\phi)$}
The fully normalized spherical harmonics are given by
\begin{equation}
Y_{lm}(\theta,\phi) = \sqrt{\dfrac{2l+1}{4\pi}\dfrac{(l-m)!}{(l+m)!}}P_l^m(\cos\theta)e^{im\phi}
\end{equation}

\noindent where $P_l^m(\cos\theta)$ are the associated Legendre polynomials. The Condon-Shortley phase, $(-1)^m$, is included in the definition of $P_l^m(\cos\theta)$. The spherical harmonics can be written in terms of the fully normalized associated Legendre polynomials, $\widetilde P_l^m(\cos\theta)$, as
\begin{equation}
Y_{lm}(\theta,\phi) = \dfrac{1}{\sqrt{2\pi}} \widetilde P_l^m(\cos\theta) e^{im\phi}
\end{equation}

Any scalar spherical function can be expanded as a sum of spherical harmonics
\eq{f(\theta,\phi) = \sum_{l=0}^{\infty} \sum_{m=-l}^{l} a_{lm} Y_{lm}(\theta,\phi)}

Being fully normalized, the orthogonality relation for the spherical harmonics is simply
\begin{equation}
\int_0^{2\pi} \int_0^{\pi}Y_{lm}(\theta,\phi) Y^*_{l'm'}(\theta,\phi) \sin\theta d\theta d\phi = \delta_{ll'}\delta_{mm'}
\end{equation}

\noindent where one can show with \eqref{eq2} that
\begin{equation}
Y^*_{lm}(\theta,\phi) = (-1)^mY_{l,-m}(\theta,\phi)
\label{eq1}
\end{equation}

It is useful to note that
\eq{\int_0^{2\pi} \int_0^{\pi}Y_{lm}(\theta,\phi) \sin\theta d\theta d\phi =  \sqrt{4 \pi} \delta_{l0}\delta_{m0} }

and
\eq{Y_{l,0}(0,0) = \sqrt{\dfrac{2l+1}{4\pi}} \label{ylzerozz} }

Fully normalized spherical harmonics are given by the routine \texttt{sphericalY}.  The routine returns the values at points $(\theta,\phi)$ up to degree $L$ for all $\pm m$, linearly indexed.  All the harmonics are returned because it is fastest to compute Legendre polynomials recursively and because field expansions often need all the harmonics. The output is a 2D array with first dimension with the points $(\theta,\phi)$ and second dimension with harmonics size $L^2 + 2L$. This is to facilitate matrix-vector multiplication of the harmonics with a column vector of expansion coefficients. After such multiplication, the result has to be reshaped to the size of the input arrays. Use the string switch \texttt{'mono'} to include the monopole term, in which case the second dimension will be size $L^2 + 2L + 1$.  A naive computation of $Y_{lm}$ would compute and divide the factorials directly.  This is only accurate to about $L=21$.  It is best to use the fully normalized Legendre polynomials, then only a factor of $1/\sqrt{2\pi}$ is needed to complete the normalization.  Finally, this routine does not take advantage of the separability of $\theta$ and $\phi$ in the computation if, for example, the arrays contain redundant points on evenly spaced grids over the sphere.

{\footnotesize
\VerbatimInput{\code/Wavefunctions/sphericalY.m}
}

\subsection{Angular Momentum Operators}

The angular momentum operators, $\mathcal{L}$, for spherical harmonics are quite useful and will be used later to express vector spherical harmonics in Cartesian components. Several properties are repeated here from \cite{angularmomop}, which can also be found in textbooks (the operators are usually denoted with $L$, rather than $\mathcal{L}$).
\eq{\mathcal{L}^2 Y_{lm} = l(l+1)Y_{lm}}
\eq{\mathcal{L}^2 = \mathcal{L}_x^2 + \mathcal{L}_y^2 + \mathcal{L}_z^2}

\noindent where
\ea{\bb{\mathcal{L}} &=& -i \bb{r} \times \nabla \\
\ &=& \mathcal{L}_x \hat{x} + \mathcal{L}_y \hat{y} + \mathcal{L}_z \hat{z}}

\noindent where $\mathcal{L}_x$, $\mathcal{L}_y$, and $\mathcal{L}_z$ are differential operators that act on Cartesian vector fields.  The components are combined to create raising and lowering operators:
\ea{\mathcal{L}_{+} &=& \mathcal{L}_x + i \mathcal{L}_y = e^{i\phi}\left(\dd{}{\theta} + i \cot\theta \dd{}{\phi}\right) \\
\mathcal{L}_{-} &=& \mathcal{L}_x - i \mathcal{L}_y = e^{-i\phi}\left(-\dd{}{\theta} + i \cot\theta \dd{}{\phi}\right) \\
\mathcal{L}_{z}  &=& -i\dd{}{\phi}
}

Adding and subtracting the raising and lower operators give
\ea{\mathcal{L}_x &=& \dfrac{1}{2}\left(\mathcal{L}_{+} + \mathcal{L}_{-}\right) \\
\mathcal{L}_y &=& \dfrac{1}{2 i}  \left(\mathcal{L}_{+} - \mathcal{L}_{-}\right) }

The operators have the property that
\ea{\mathcal{L}_{+} Y_{lm} &=& \sqrt{(l-m)(l+m+1)} Y_{l,m+1} \\
\mathcal{L}_{-} Y_{lm} &=& \sqrt{(l+m)(l-m+1)}  Y_{l,m-1} \\
\mathcal{L}_{z} Y_{lm} &=& m Y_{l,m} }

\section{Scalar Spherical Wave Functions}

The spherical scalar wave functions are solutions to the Helmholtz wave equation in free space in spherical coordinates. They are a product of spherical Bessel functions and spherical harmonics.  The form of the Bessel function determines whether the wave is incoming or outgoing, specifically whether the field is regular at the origin or radiating, though they are all standing waves:
\begin{eqnarray}
\textrm{Incoming regular wave:} \quad \quad\textit{Rg} \psi_{lm}(k,\bb{r}) &=& j_l(kr) Y_{lm}(\theta,\phi) \\
\textrm{Outgoing radiating wave:} \quad  \quad \quad \psi_{lm}(k,\bb{r}) &=& h_l^{(1)}(kr) Y_{lm}(\theta,\phi)
\end{eqnarray}

\noindent where $j_l(kr)$ and $h_l^{(1)}(kr)$ are spherical Bessel and Hankel functions, respectively, $k$ is the background wavenumber, and \textit{Rg} means being regular (non-singular) at the origin which uses $j_l(kr)$. The wave functions form a complete set, so any scalar field can be expanded as an infinite sum:
\begin{eqnarray}
\textrm{Incoming regular field:} \quad \quad \phi(\bb{r}) &=& \sum_{l=0}^{\infty} \sum_{m=-l}^l a_{lm} \textit{Rg} \psi_{lm}(k,\bb{r})  \\
\textrm{Outgoing radiating field:} \quad \quad \phi(\bb{r}) &=& \sum_{l=0}^{\infty} \sum_{m=-l}^l b_{lm} \psi_{lm}(k,\bb{r})
\end{eqnarray}

The far-field scalar wave functions for radiating waves are found by taking the large argument limit of the Hankel function
\eq{\lim_{kr \rightarrow \infty}  \psi_{lm}(k,\bb{r}) = \dfrac{e^{ikr}}{kr} i^{-l-1} Y_{lm}(\theta,\phi)  }

The addition theorem for the scalar Green's function is given in terms of regular and radiating waves as, \cite{chew1995waves},
\eq{g(\bb{r},\bb{r}') = i k\sum_{l=0}^{\infty} \sum_{m=-l}^l \textit{Rg} \psi_{lm}(k,\bb{r}_{<}) \psi_{lm}(k,\bb{r}_{>}) \label{scalargreens}}

\noindent where $\bb{r}_{<}$ is for the smaller of $\vert \bb{r} \vert$ and $\vert \bb{r}'\vert $ and $\bb{r}_{>}$ is for the greater of $\vert \bb{r}\vert$ and $\vert \bb{r}'\vert$.

The routine \texttt{psilm} returns $\psi_{lm}(k,\br)$ at points $(r,\theta,\phi)$ up to maximum degree $L$, for all $\pm m$, linearly indexed.  $k$ is real or complex.  Like \texttt{sphericalY}, the first dimension has evaluation points and second dimension is size $L^2 + 2L+1$.  Use the string switch \texttt{'rg'} for $\textit{Rg} \psi_{lm}(k,\bb{r})$. Evaluation of the sum over harmonics for all points can be accomplished with matrix-vector multiplication then reshaping the result to match the dimensions of the input coordinates.

{\footnotesize
\VerbatimInput{\code/Wavefunctions/psilm.m}
}

\section{Scalar Plane Wave Expansion}

The expansion of scalar plane waves in terms of spherical waves at the origin is, \cite{tsang2000scattering},
\eq{e^{i \bb{k} \cdot \bb{r}} = 4\pi \sum_{l = 0}^{\infty} \sum_{m = -l}^l i^l  j_l(kr) Y_{lm}(\theta,\phi) Y_{lm}^*(\theta_k,\phi_k) \label{scalarplanewaveexp} }

\noindent where the spherical harmonics are fully-normalized with Condon-Shortley phase and
\ea{\bb{r} &=& x \hat{x} + y \hat{y} + z \hat{z} \\
\bb{k} &=& k \hat{k} \\
\hat{k} &=& \sin\theta_k \cos\phi_k \hat{x} +  \sin\theta_k\sin\phi_k \hat{y} + \cos\theta_k \hat{z} }

The plane wave expansion coefficients are found by separating the regular scalar wave functions from \eqref{scalarplanewaveexp} and collecting the remaining terms as expansion coefficients as
\ea{e^{i \bb{k} \cdot \bb{r}} &=& \sum_{l = 0}^{\infty} \sum_{m = -l}^l a_{lm} \textit{Rg} \psi_{lm}(k,\bb{r}) \\
a_{lm} &=&  4\pi i^l Y_{lm}^*(\theta_k,\phi_k)}

The routine \texttt{scalarPlaneWaveCoef} routine takes as input the maximum harmonic degree $L$, and the spherical coordinates of the plane wave propagation direction, $(\theta_k, \phi_k)$, and returns the $a_{lm}$ scalar plane wave expansion coefficients for $l = 0,...,L$, for all $\pm m$, linearly indexed.

{\footnotesize
\VerbatimInput{\code/Wavefunctions/scalarPlaneWaveCoef.m}
}

\clearpage

\section{Vector Spherical Harmonics}
\label{sec:vecsphharm}
Vector spherical harmonics capture the angular variation of a vector field in spherical coordinates and are used for far-field radiation patterns. Their close cousin is the radially independent vector wave function usually notated $\bb{X}_{lm}$, \cite{jackson1999classical}. The three fully normalized vector spherical harmonics, which form an orthonormal basis, are given by, \cite{chew1995waves, tsang2000scattering},
\begin{eqnarray}
\bb{P}_{lm}(\theta,\phi) &=& \hat{r}Y_{lm}(\theta,\phi) \\
\  & \ & \nonumber \\
\bb{B}_{lm}(\theta,\phi) &=&\dfrac{1}{\sqrt{ l(l+1)}} r\nabla Y_{lm}(\theta,\phi)  \\
\ &=& N_{lm} \left[\hat\theta \dfrac{d}{d\theta} P_l^m(\cos\theta) + \hat\phi \dfrac{im}{\sin\theta}P_l^m(\cos\theta) \right] e^{im\phi}   \\
\ &=& \dfrac{1}{\sqrt{2\pi l(l+1)}} \left[\hat\theta \dfrac{d}{d\theta} \widetilde{P}_l^m(\cos\theta) + \hat\phi \dfrac{im}{\sin\theta}\widetilde{P}_l^m(\cos\theta) \right] e^{im\phi}  \\
\bb{C}_{lm}(\theta,\phi) &=& \dfrac{1}{\sqrt{ l(l+1)}} \nabla \times \left[ \br Y_{lm}(\theta,\phi) \right] \\
\ &=& N_{lm} \left[\hat\theta \dfrac{im}{\sin\theta}P_l^m(\cos\theta) - \hat\phi \dfrac{d}{d\theta} P_l^m(\cos\theta)  \right] e^{im\phi}  \\
\ &=& \dfrac{1}{\sqrt{2\pi l(l+1)}}  \left[\hat\theta \dfrac{im}{\sin\theta}\widetilde{P}_l^m(\cos\theta) - \hat\phi \dfrac{d}{d\theta} \widetilde{P}_l^m(\cos\theta)  \right] e^{im\phi}
\end{eqnarray}

\noindent where
\begin{equation}
N_{lm} = \dfrac{1}{\sqrt{l(l+1)}}\sqrt{\dfrac{2l+1}{4\pi}\dfrac{(l-m)!}{(l+m)!}}
\end{equation}

The Condon-Shortley phase, $(-1)^m$, is included in the definition of the associated Legendre polynomials. The following relation exists between $\bb{B}_{lm}$ and $\bb{C}_{lm}$:
\begin{eqnarray}
\bb{B}_{lm}(\theta,\phi) &=& \hat{r} \times \bb{C}_{lm}(\theta,\phi) \\
\bb{C}_{lm}(\theta,\phi) &=& -\hat{r} \times \bb{B}_{lm}(\theta,\phi)
\end{eqnarray}

In other words, $\bb{B}_{lm}\cdot \hat{\theta} = -\bb{C}_{lm} \cdot\hat{\phi} $ and  $\bb{B}_{lm}\cdot \hat{\phi} = -\bb{C}_{lm} \cdot\hat{\theta} $.  The fully normalized vector spherical harmonics have orthonormality
\begin{equation}
\int_0^{2\pi} \int_0^{\pi}
\left\{
\begin{array}{c}
\bb{P}_{lm}(\theta,\phi) \cdot \bb{P}^*_{lm}(\theta,\phi) \\
\bb{B}_{lm}(\theta,\phi) \cdot \bb{B}^*_{lm}(\theta,\phi) \\
\bb{C}_{lm}(\theta,\phi) \cdot \bb{C}^*_{lm}(\theta,\phi)
\end{array}
\right\}
\sin\theta d\theta d\phi = \delta_{ll'}\delta_{mm'}
\end{equation}

and
\eq{\int_0^{2\pi} \int_0^{\pi}\bb{B}_{lm}(\theta,\phi) \cdot \bb{C}^*_{lm}(\theta,\phi) \sin\theta d\theta d\phi= 0}

Any spherical vector field (i.e., polarized spherical far-field pattern) can be expanded as
\begin{equation}
\bb{F}(\theta,\phi) = \sum_{l=1}^{\infty}\sum_{m=-l}^l b_{lm} \bb{B}_{lm}(\theta,\phi) + c_{lm}\bb{C}_{lm}(\theta,\phi) \label{FinBC}
\end{equation}

The vector spherical harmonics can be expressed in Cartesian components using the angular momentum operators.  $\bb{C}_{lm}$ written with the angular momentum operator is
\eq{\bb{C}_{lm} = \dfrac{\nabla \times\left[ \br Y_{lm}\right]}{\sqrt{l(l+1)}}  = \dfrac{-\br \times \nabla Y_{lm}}{\sqrt{l(l+1)}}  = \dfrac{-i \bb{\mathcal{L}} Y_{lm}}{\sqrt{l(l+1)}} }

which leads to
\ea{\bb{C}_{lm} &=&  \dfrac{-i}{\sqrt{l(l+1)}} \left[\dfrac{1}{2}\left(\hat{x} - i \hat{y}\right)e_{lm}Y_{l,m+1} + \dfrac{1}{2}\left(\hat{x} + i \hat{y}\right) f_{lm}Y_{l,m-1} + mY_{l,m} \hat{z}\right] \nonumber \label{Clmxyz} \\    }
%\ea{\bb{C}_{lm} &=& \dfrac{-i}{\sqrt{l(l+1)}} \left[\dfrac{1}{2}\left(e_{lm}Y_{l,m+1} + f_{lm}Y_{l,m-1}\right)\hat{x} + \right. \nonumber \\
%\ & \ & \left. \dfrac{1}{2i}\left(e_{lm}Y_{l,m+1} - f_{lm}Y_{l,m-1}\right)\hat{y} + mY_{l,m} \hat{z}\right] \label{Clmxyz} \\
%\ & =& \dfrac{-i}{\sqrt{l(l+1)}} \left[\dfrac{1}{2}\left(\hat{x} - i \hat{y}\right)e_{lm}Y_{l,m+1} + \dfrac{1}{2}\left(\hat{x} + i \hat{y}\right) f_{lm}Y_{l,m-1} + mY_{l,m} \hat{z}\right] }

\noindent where
\ea{e_{lm} &=& \sqrt{(l-m)(l+m+1)} \\
f_{lm} &=& \sqrt{(l+m)(l-m+1)}  }

For the special case $(\theta,\phi) = (0,0)$, only the $m = \pm 1$ harmonics survive, which leads to
\ea{\bb{C}_{l,\pm1}(0,0) &=& \dfrac{-i}{2}\left(\hat{x} \mp i \hat{y}\right)\sqrt{\dfrac{2l+1}{4\pi}} \label{clmzplus} \\
\bb{B}_{l,\pm1}(0,0)  &=& \dfrac{-i}{2}\left(\hat{y} \pm i \hat{x}\right)\sqrt{\dfrac{2l+1}{4\pi}} \label{blmzplus}}

The second special case $(\theta,\phi) = (\pi,0)$ has again only $m = \pm 1$ harmonics.  Using $Y_{lm}(\pi,0) = (-1)^l Y_{lm}(0,0)$ we get
\ea{\bb{C}_{l,\pm1}(\pi,0) &=& (-1)^l \bb{C}_{l,\pm1}(0,0) \label{clmzminus} \\
\bb{B}_{l,\pm1}(\pi,0) &=& (-1)^{l+1} \bb{B}_{l,\pm1}(0,0) \label{blmzminus}}

The routine \texttt{BC} returns the components of $\bb{B}_{lm}$ and $\bb{C}_{lm}$, for all harmonics up to degree $L$, for all $\pm m$, linearly indexed along the second dimension with spherical points along the first dimension. There is duplication in returning each vector component, but none in the computation.  Use the string switch \texttt{'norm'} for fully normalized ($1/\sqrt{l(l+1)}$).

The helper function \texttt{BCmult} takes the outputs from \texttt{BC} and expansion coefficients $b_{lm}$ and $c_{lm}$ and returns the field components of \eqref{FinBC}, $F_{\theta}$, $F_{\phi}$. These arrays are columnized and require reshaping to make them the same size as the \texttt{BC} input $(\theta,\phi)$ arrays.

{\footnotesize
\VerbatimInput{\code/Wavefunctions/BC.m}
}

{\footnotesize
\VerbatimInput{\code/Wavefunctions/BCmult.m}
}

\clearpage
\newpage

\section{Vector Spherical Wave Functions}
\label{sec:vecsphwave}
The vector spherical wave functions are solutions to the free-space vector wave equation in spherical coordinates for electric and magnetic fields, \cite{chew1995waves, tsang2000scattering}.  Analogous to scalar wave functions, they are composed of products of spherical Bessel functions and vector spherical harmonics and come in regular and radiating forms.
\begin{eqnarray}
\textit{Rg}\M{k,\br} &=& \dfrac{1}{\sqrt{l(l+1)}}\nabla \times \left[ \bb{r} \textit{Rg}\psi_{lm}(k,\br)\right]  \\
\ &= & j_l(kr) \bb{C}_{lm}(\theta,\phi) \\
\ & \ & \  \nonumber  \\
\M{k,\br} &=& \dfrac{1}{\sqrt{l(l+1)}}\nabla \times \left[ \bb{r} \psi_{lm}(k,\br)\right]  \\
\ &= & h_l^{(1)}(kr) \bb{C}_{lm}(\theta,\phi) \\
\ & \ & \  \nonumber \\
\textit{Rg}\N{k,\br} &=&  \dfrac{1}{k} \nabla \times \textit{Rg}\M{k,\br}  \\
\ &=&  \sqrt{l(l+1)}\dfrac{j_l(kr)}{kr} \bb{P}_{lm} (\theta,\phi) + \dfrac{\left[kr j_l(kr)\right]'}{kr} \bb{B}_{lm}(\theta,\phi) \\
\ & \ & \  \nonumber \\
\N{k,\br} &=&  \dfrac{1}{k} \nabla \times \M{k,\br}  \\
\ &=&  \sqrt{l(l+1)}\dfrac{h_l^{(1)}(kr)}{kr} \bb{P}_{lm} (\theta,\phi) + \dfrac{\left[kr h_l^{(1)}(kr)\right]'}{kr} \bb{B}_{lm}(\theta,\phi)
\end{eqnarray}

Note that $\M{k,\br}$ contains only $\hat\theta$ and $\hat\phi$ components, while $\N{k,\br}$ contains the same plus an $\hat r$ component.  $\N{k,\br}$ holds the near-field radial part of a field, which disappears in the far-field.  Here, $\bb{P}_{lm}$, $\bb{B}_{lm}$ and $\bb{C}_{lm}$ are fully normalized. If partially normalized wave functions are desired, then both sides of the equations are multiplied by $\sqrt{l(l+1)}$.

%If $\bb{B}_{lm}$ and $\bb{C}_{lm}$ are to be partially normalized, then an extra factor of  is applied to $\bb{P}_{lm}$.

The two wave functions are related by
\ea{\M{k,\br} &=& \dfrac{1}{k} \curl \N{k,\br} \label{vswfcurlM}\\
\N{k,\br} &=& \dfrac{1}{k} \curl \M{k,\br} \label{vswfcurlN}}

An electric or magnetic field can be written as a sum of vector wave functions. This is the multipole expansion of the vector field.  $ \M{k,\br}$ and \N{k,\br} are linearly independent, so any field is a linear combination of both
\begin{equation}
\bb{E}(\br) = \sum_{l=1}^{\infty}\sum_{m=-l}^l a_{lm} \M{k,\br} + b_{lm} \N{k,\br}
\end{equation}

\begin{equation}
\bb{H}(\br) = \dfrac{k}{i\omega\mu}\sum_{l=1}^{\infty}\sum_{m=-l}^l a_{lm} \N{k,\br} + b_{lm} \M{k,\br} \label{Halmblm}
\end{equation}

\noindent where $a_{lm}$ and $b_{lm}$ are complex expansion coefficients. These equations are for radiating fields. Expressions for incoming fields will use the $\textit{Rg}$ counterparts.

The addition theorem for the dyadic Green's function can be written as an outer product of normalized vector wave functions, \cite{chew1995waves},
\eq{\G{} = ik\sum_{l=1}^{\infty}\sum_{m=-l}^l \textit{Rg}\M{k,\br}\Mhat{k,\br'} + \textit{Rg}\N{k,\br}\Nhat{k,\br'} \label{dyadicgreensaddition}}

\noindent where $\hat{}$ means conjugation of the angular function only. This equation applies under the condition $\vert \br \vert < \vert \br' \vert$. When $\vert \br \vert > \vert \br' \vert$ the equation changes so that the \textit{Rg} is instead applied to $\Mhat{k,\br}$ and $\Nhat{k,\br}$. The far-field vector wave functions are found by taking the large argument limit of the Bessel functions in $\M{k,\br}$ and $\N{k,\br}$
\begin{eqnarray}
\lim_{kr \rightarrow \infty} \M{k,\br} &=& \dfrac{e^{ikr}}{kr} i^{-l-1} \bb{C}_{lm}(\theta,\phi) \label{farfieldM} \\
\lim_{kr \rightarrow \infty} \N{k,\br} &=& \dfrac{e^{ikr}}{kr} i^{-l} \bb{B}_{lm}(\theta,\phi) \label{farfieldN}
\end{eqnarray}

Vector wave functions are orthogonal over the unit sphere as
\begin{eqnarray}
\int \bb{M}_{lm}(k,\br) \cdot \hat{\bb{M}}_{l'm'}(k,\br) d\Omega &=& \left(h_l^{(1)}(kr)\right)^2 \delta_{ll'}\delta_{mm'}\label{MMint} \\
\int \bb{N}_{lm}(k,\br) \cdot \hat{\bb{N}}_{l'm'}(k,\br) d\Omega &=& \dfrac{1}{(kr)^2}\left[l(l+1)\left(h_l^{(1)}(kr)\right)^2 + \left(\left[kr h_l^{(1)}(kr)\right]'\right)^2\right] \delta_{ll'}\delta_{mm'} \\
\int \bb{M}_{lm}(k,\br) \cdot \hat{\bb{N}}_{l'm'}(k,\br) d\Omega &=& 0
\end{eqnarray}
%
%\begin{equation}
%\int \bb{M}_{lm}(k,\br) \cdot \hat{\bb{M}}_{l'm'}(k,\br) d\Omega = \left(h_l^{(1)}(kr)\right)^2 \delta_{ll'}\delta_{mm'}
%\end{equation}
%
%\begin{equation}
%\int \bb{N}_{lm}(k,\br) \cdot \hat{\bb{N}}_{l'm'}(k,\br) d\Omega= \dfrac{1}{(kr)^2}\left[\left(h_l^{(1)}(kr)\right)^2 + \left(\left[kr h_l^{(1)}(kr)\right]'\right)^2\right] \delta_{ll'}\delta_{mm'}
%\end{equation}
%
%\begin{equation}
%\int \bb{M}_{lm}(k,\br) \cdot \hat{\bb{N}}_{l'm'}(k,\br) d\Omega = 0
%\end{equation}

The cross products dotted with the radial vector and integrated over the sphere are
\begin{eqnarray}
\int \left(\bb{M}_{lm}(k,\br) \times \hat{\bb{M}}_{l'm'}(k,\br)\right)\cdot \hat{\br}  d\Omega &=& 0 \label{MNrorth1} \\
\int \left(\bb{N}_{lm}(k,\br) \times \hat{\bb{N}}_{l'm'}(k,\br) \right)\cdot \hat{\br} d\Omega &=& 0 \\
\int \left( \bb{M}_{lm}(k,\br) \times \hat{\bb{N}}_{l'm'}(k,\br)\right)\cdot \hat{\br} d\Omega &=& h_l^{(1)}(kr) \dfrac{\left[kr h_l^{(1)}\right]'}{kr} \delta_{ll'}\delta_{mm'}  \label{MNrorth3}
\end{eqnarray}

%\begin{equation}
%\int \bb{M}_{lm}(k,\br) \times \hat{\bb{N}}_{l'm'}(k,\br) d\Omega= h_l^{(1)}(kr)\left( \dfrac{h_l^{(1)}}{kr} + \dfrac{\left[kr h_l^{(1)}\right]'}{kr}  \right) \delta_{ll'}\delta_{mm'}
%\end{equation}
%
%\begin{eqnarray}
%\int \bb{M}_{lm}(k,\br) \times \hat{\bb{M}}_{l'm'}(k,\br) d\Omega= 0 \\
%\int \bb{N}_{lm}(k,\br) \times \hat{\bb{N}}_{l'm'}(k,\br) d\Omega= 0
%\end{eqnarray}

These work for any combination of regular or radiating wave functions, simply substitute the correct Bessel functions.

The routine \texttt{MN} returns the components of $\M{k,\br}$ and $\N{k,\br}$, for all harmonics up to degree $L$, for all $\pm m$, linearly indexed. This routine calls \texttt{BC}. Like \texttt{BC}, the outputs are 2D arrays with $L^2 + 2L$ harmonics along the second dimension and the evaluation points at $(r,\theta,\phi)$ along the first.  Use the string switch \texttt{'rg'} for regular waves, \texttt{'hat'} for angular conjugation, and \texttt{'norm'} to use fully normalized vector spherical harmonics. It defaults to partial normalization. For regular waves, the spherical Bessel function routines from Chapter \ref{sphericalbess} take care of $kr$ near the origin.  Use the following input constructions for \texttt{MN}:

\renewcommand{\arraystretch}{1.5}
\begin{table}[H]
\caption{\texttt{MN} Input Options}
\begin{center}
\begin{tabular}{|l|l|}
\hline
$\M{k,\br}$, $\N{k,\br}$ & \texttt{MN(L,k,r,theta,phi)} \\
\hline
$\textit{Rg}\M{k,\br}$, $\textit{Rg}\N{k,\br}$ & \texttt{MN(L,k,r,theta,phi,'rg')} \\
\hline
$\Mhat{k,\br}$, $\Nhat{k,\br}$ & \texttt{MN(L,k,r,theta,phi,[],'hat') } \\
\hline
$\textit{Rg}\Mhat{k,\br}$, \textit{Rg}$\Nhat{k,\br}$ & \texttt{MN(L,k,r,theta,phi,'rg','hat')}  \\
\hline
Fully normalized $\bb{B}_{lm}$ and $\bb{C}_{lm}$& \texttt{MN(L,k,r,theta,phi,...,...,'norm')} \\
\hline
\end{tabular}
\end{center}
\label{default}
\end{table}

The helper function \texttt{MNmult} takes the outputs from \texttt{MN} and expansion coefficients $a_{lm}$ and $b_{lm}$ and returns the electric field components $E_r, E_{\theta}, E_{\phi}$. These arrays are columnized and require reshaping to make them the same size as the \texttt{MN} input $(r, \theta,\phi)$ arrays. Magnetic field components are obtained by swapping $a_{lm}$ and $b_{lm}$ and multiplying by $k/i\omega\mu$ as in \eqref{Halmblm}.

{\footnotesize
\VerbatimInput{\code/Wavefunctions/MN.m}
}

{\footnotesize
\VerbatimInput{\code/Wavefunctions/MNmult.m}
}

\section{Vector Plane Wave Expansion}

Here we derive the vector wave function expansion coefficients for vector plane waves with arbitrary propagation direction. These are needed when combining plane wave excitations and vector wave functions, for instance, in T-matrix scattering problems.

\subsection{Plane Wave Representation}

Let the electric and magnetic fields of the vector plane wave be written
\ea{\bb{E}(\br) &=& \bb{E} e^{i \bb{k} \cdot \bb{r}} \label{Eplane} \\
\bb{H}(\br) &=& \bb{H}  e^{i \bb{k} \cdot \bb{r}} }

%\ea{\bb{E}(\br) &=& \left(E_v \hat{v} + E_h \hat{h}\right) e^{i \bb{k} \cdot \bb{r}} \\
%\bb{H}(\br) &=& \dfrac{1}{\eta} \hat{k} \times \left(E_v \hat{v} + E_h \hat{h}\right) e^{i \bb{k} \cdot \bb{r}} }

\noindent where
\ea{\bb{r} &=& x \hat{x} + y \hat{y} + z \hat{z} \\
\bb{k} &=& k \hat{k} \\
\hat{k} &=& \sin\theta_k \cos\phi_k \hat{x} +  \sin\theta_k\sin\phi_k \hat{y} + \cos\theta_k \hat{z} }

and
\ea{\bb{E} &=& E_x \hat{x} + E_y \hat{y} + E_z \hat{z} \label{Explane}  \\
\bb{H} &=& \dfrac{1}{\eta} \hat{k} \times \bb{E} \label{Hcross} \\
\ & =& H_x \hat{x} + H_y \hat{y} + H_z \hat{z}   }

\noindent where $\eta$ is the characteristic impedance of free-space.

\subsection{Vector Plane Wave Coefficients}

To solve for the expansion coefficients about the origin, write the electric and magnetic fields as an expansion of regular waves at the origin
\ea{
\bb{E}(\br) &=& \sum_{l=1}^{\infty}\sum_{m=-l}^l a_{lm} \textit{Rg}\M{k,\br} + b_{lm} \textit{Rg}\N{k,\br} \\
\bb{H}(\br) &=& \dfrac{k}{i\omega\mu}\sum_{l=1}^{\infty}\sum_{m=-l}^l a_{lm} \textit{Rg}\N{k,\br} + b_{lm} \textit{Rg}\M{k,\br}
}

Multiplying both fields by $\textit{Rg}\Mhat{k,\br}$, integrating over the sphere, applying orthogonality for fully normalized wave functions, \eqref{MMint}, and canceling one factor of the Bessel functions, we get
%\ea{a_{lm}\left(j_l(kr)\right)^2 &=& \int \textit{Rg}\Mhat{k,\br} \cdot \bb{E}(\br) d\Omega  \\
%b_{lm}\left(j_l(kr)\right)^2 \dfrac{k}{i\omega\mu} &=& \int \textit{Rg}\Mhat{k,\br} \cdot \bb{H}(\br) d\Omega
%}
%
%or
\ea{a_{lm}j_l(kr) &=& \int \bb{C}^*_{lm}(\theta,\phi) \cdot \bb{E}(\br) d\Omega  \label{cstarte} \\
b_{lm}j_l(kr)\dfrac{k}{i\omega\mu} &=& \int \bb{C}^*_{lm}(\theta,\phi) \cdot \bb{H}(\br) d\Omega \label{cstarth}
}

Substituting \eqref{Eplane} into \eqref{cstarte}, then substituting the scalar plane wave expansion, \eqref{scalarplanewaveexp}, which cancels the remaining Bessel function, and exchanging the sum and integration
\eq{a_{lm} = 4\pi \bb{E}  \cdot \sum_{l' = 0}^{\infty} \sum_{m' = -l'}^{l'} i^l   \int \bb{C}^*_{lm}(\theta,\phi) Y_{l'm'}(\theta,\phi) Y_{l'm'}^*(\theta_k,\phi_k) d\Omega  }
%\eq{a_{lm}j_l(kr) = \int \bb{C}^*_{lm}(\theta,\phi) \cdot \bb{E}e^{i \bb{k} \cdot \bb{r}} d\Omega }

%Substituting  \eqref{Eplane}  into \eqref{Eplane}

%Substituting \eqref{cstarte}

Using \eqref{Clmxyz} it can be shown that the integral and sum reduces for both coefficients to
\ea{a_{lm} &=& 4\pi i^l  \bb{C}^*_{lm}(\theta_k,\phi_k)  \cdot  \bb{E} \label{almplaneE}\\
b_{lm} &=& 4\pi i^{l+1}  \bb{C}^*_{lm}(\theta_k,\phi_k) \cdot  (\eta  \bb{H}) \label{blmH}\\
\ &=& -4\pi i^{l+1}  \bb{B}^*_{lm}(\theta_k,\phi_k) \cdot  \bb{E} \label{blmplaneE}}

\noindent where the last equation comes from taking $\hat{k} = \hat{r}$ in \eqref{Hcross}. Taking the dot product between \eqref{Clmxyz} and \eqref{Explane} the coefficients are written out as
\ea{a_{lm} &=& \dfrac{4\pi i^{l+1}}{\sqrt{l(l+1)}} \left[ \dfrac{1}{2}\left(E_x + i E_y\right)\sqrt{(l-m)(l+m+1)} Y^*_{l,m+1}(\theta_k,\phi_k) \right. \nonumber \\
\ & \ &  \left. + \dfrac{1}{2}\left(E_x - i E_y\right)\sqrt{(l+m)(l-m+1)}Y^*_{l,m-1}(\theta_k,\phi_k)  + E_z m Y^*_{lm}(\theta_k,\phi_k)  \right] \\
b_{lm} &=& i a_{lm}(\bb{E} \rightarrow \eta \bb{H} =  \hat{k} \times \bb{E}) }

The plane wave expansion coefficients have left and right circularly polarized amplitudes embedded in them.

The routine \texttt{vectorPlaneWaveCoef} routine takes as input the maximum harmonic degree $L$, the electric field components of the plane wave, $E_x$, $E_y$, and $E_z$, and the spherical coordinates of the plane wave propagation direction, $(\theta_k, \phi_k)$, and returns the $a_{lm}$ and $b_{lm}$ vector plane wave expansion coefficients for $l = 1,...,L$, for all $\pm m$, linearly indexed.

{\footnotesize
\VerbatimInput{\code/Wavefunctions/vectorPlaneWaveCoef.m}
}

%\ea{b_{lm} &=& 4\pi i^{l+1}  \bb{C}^*_{lm}(\theta_k,\phi_k)  \cdot  \bb{H} \label{blmH} \\
%\ & =& -4\pi i^{l+1}  \bb{B}^*_{lm}(\theta_k,\phi_k) \cdot  \bb{E}}

%\ea{b_{lm} &=& 4\pi i^{l+1}  \bb{C}^*_{lm}(\theta_k,\phi_k)  \cdot \left(\hat{r}\times \bb{E} \right) \\
%\ &=& -4\pi i^{l+1}  \left(\bb{r}\times\bb{C}^*_{lm}(\theta_k,\phi_k) \right) \cdot  \bb{E} \\
%\ &=& -4\pi i^{l+1}  \bb{B}^*_{lm}(\theta_k,\phi_k) \cdot  \bb{E} }

\subsection{$\hat{z}$-Propagating Plane Wave}

Here we derive the special case of a vector plane wave propagating in the $\hat{z}$ direction, $\theta_k = \phi_k = 0$, and $E_z = 0$.  This is useful for radar backscatter computations. Only the $m=\pm 1$ harmonics are needed, and the expansion coefficients become
\ea{a_{l,\pm1} &=& 2\pi i^{l+1} \left(E_x \mp i E_y\right)Y_{l,\pm1}(0,0)  \\
b_{l,\pm1} &=& -2\pi i^{l} \eta \left(H_x \mp i H_y\right)Y_{l,\pm1}(0,0) }

%Using the fact that
%\ea{
%Y_{l,0}(0,0) = \sqrt{\dfrac{2l+1}{4\pi}}
%}

Using \eqref{ylzerozz} and the fact that $H_x = -E_y/\eta$ and $H_y = E_x/\eta$ for a $\hat{z}$-propagating plane wave, one can show
\ea{a_{l,\pm1} &=& \sqrt{\pi (2l +1)} i^{l+1} \left(E_x \mp i E_y\right) \\
b_{l,\pm1} &=& \pm a_{l,\pm1} }

The routine \texttt{vectorPlaneWaveCoefZ} works like \texttt{vectorPlaneWaveCoef}. It takes as input the maximum harmonic degree $L$, and the electric field components of the $\hat{z}$-propagating plane wave, $E_x$, $E_y$, and returns the $a_{lm}$ and $b_{lm}$ vector plane wave expansion coefficients for $l = 1,...,L$, for all $\pm m$, linearly indexed.

{\footnotesize
\VerbatimInput{\code/Wavefunctions/vectorPlaneWaveCoefZ.m}
}

%\ea{a_{l,\pm1} &=& \sqrt{\pi (2l +1)} i^{l+1} \left(E_x \mp i E_y\right) \\
%b_{l,\pm1} &=& -\sqrt{\pi (2l +1)} i^{l} \left(-E_y \mp i E_x\right) }
%
%\ea{a_{l,\pm1} &=& \sqrt{\pi (2l +1)} i^{l+1} \left(E_x \mp i E_y\right) \\
%b_{l,\pm1} &=& -\sqrt{\pi (2l +1)} i^{l+1} \left(iE_y \mp  E_x\right) }
%
%\ea{a_{l,\pm1} &=& \sqrt{\pi (2l +1)} i^{l+1} \left(E_x \mp i E_y\right) \\
%b_{l,\pm1} &=& \sqrt{\pi (2l +1)} i^{l+1} \left(\pm  E_x -iE_y \right) }
%
%\ea{a_{l,\pm1} &=& \sqrt{\pi (2l +1)} i^{l+1} \left(E_x \mp i E_y\right) \\
%b_{l,\pm1} &=& \pm  \sqrt{\pi (2l +1)} i^{l+1} \left(E_x \mp iE_y \right) }

\section{Band-limited Fields and Required Number of Spherical Harmonics}

The number of spherical harmonics, or spherical spectral components, that are needed to fully capture the information contained in a far-field radiation pattern depends only on the size of the object and the background wavenumber. This is true regardless of the make up of the object and applies to both scalar and vector fields. For instance, the radiation pattern could be the scattered field from a natural object or the field radiated by an antenna array.

It is commonly stated that $O(kd)$ spherical harmonics are required to represent a spherical field pattern, where $k$ is the background wavenumber and $d$ is the diameter of the smallest sphere that encloses the object. $O(kd)$ actually gives the maximum degree harmonic $L$, where all $m$ need to be included in the expansion. Therefore, a total of $N = L^2 + 2L + 1$ harmonics are needed in the case of scalar waves. This is also equal to the number of angular points needed to Nyquist-sample the spatial pattern of a field over the sphere. This criteria has been studied in detail, \cite{yaghjian1996sampling}, and a more precise value for the maximum degree harmonic, written in terms of the radius of the smallest enclosing sphere, $a$, is
\eq{L \approx \left\lceil 1.1 ka \left( 1 + \dfrac{1}{ka}\right) \right\rceil \label{LLmax} }

This is a remarkable fact about fields. No matter the make up of a scattering object or antenna, so long as it is contained within a radius $a$ relative to the origin, the angular variation of the far-field can only oscillate as fast as the maximum harmonic $(l,m) = (L,\pm L)$.  This is what is meant by the phrase band-limited field, because the spherical harmonic spectrum of any radiated field is finite.

A quick derivation and visual of this idea follows. Start with the volume integral equation for the scalar scattered field
\eq{\phi_{sca}(\br) = \int g(\br,\br') s(\br') dV' \label{view}}

\noindent where $s(\br)$ is a volume source. Substitute the addition theorem for the scalar Green's function, \eqref{scalargreens}, so that \eqref{view} is expanded
\ea{\phi_{sca}(\br) &=& \sum_{lm} a_{lm} \psi_{lm}(\br)  \\
a_{lm} &=&  i k \int \textit{Rg}\psi_{lm}(\br') s(\br')  dV' \label{almpoint}}

Choose a point source in spherical coordinates that has unit amplitude at radius $a$ along the $z$ axis:
\eq{s(\br) = \dfrac{1}{r^2\sin\theta}\delta(r-a)\delta(\theta)\delta(\phi)}

Evaluating \eqref{almpoint} with this source over spherical coordinates gives the expansion coefficients
\ea{a_{lm} &=&  i k  j_l(ka) Y_{lm}(0,0) \\
\ & = &  i k \sqrt{\dfrac{2l+1}{4\pi}} j_l(ka)  }

The power in each harmonic is given by the magnitude squared of the coefficients
\ea{\vert a_{lm}\vert^2  &=&  k^2  \dfrac{(2l+1)}{4\pi} \vert j_l(ka) \vert^2  }

Finally, we can normalize this to capture only the dependence on $l$
\ea{  \vert \widetilde{a}_{lm}\vert^2  &=& (2l+1) \vert j_l(ka) \vert^2  }

Figure \ref{sphharmcont} left shows the magnitude of the normalized coefficients as a function of $ka$ and $l$. There is very quickly little harmonic content in the expansion coefficients above the line $l = ka$.  Figure \ref{sphharmcont} right shows the normalized harmonics for $ka = 25$ as well as the cutoff predicted by \eqref{LLmax}.

\begin{figure}[H]
   \centering
      \begin{tabular}{cc}
     \subfigure{\includegraphics[width=3in]{WaveFunctions/Figures/sphharmcont}}
     \subfigure{\includegraphics[width=3in]{WaveFunctions/Figures/sphharmcontex}}
  \end{tabular}
  \label{sphharmcont}
\caption{Spherical harmonic content of a scalar point source at $z = a$. Left: coefficients vs $ka$ with the line $l = ka$ plotted. Right: example of $ka = 25$ with \eqref{LLmax} plotted.}
\end{figure}