\chapter[Finite differences in 1D]{Finite differences for \\
stationary problems in 1D}
\label{chap: finite diff 1d}
We begin our study of numerical methods for partial differential equations
(PDEs) by treating the one-dimensional (1D) case, which means we deal only with
an \emph{ordinary} differential equation (ODE).  Moreover, in this chapter we
limit our attention to \emph{two-point boundary-value problems} for
\emph{second-order} ODEs.  In fact, we will focus mostly on the very simple
model problem on the interval~$[0,L]$,
\begin{equation}\label{eq: model 1d}
-u''=f(x)\quad\text{for $0<x<L$,}
	\quad\text{with $u(0)=\gamma_0$ and $u(L)=\gamma_L$,}
\end{equation}
in which the \emph{source term}~$f(x)$ and \emph{boundary data} $\gamma_0$,
$\gamma_L$ are given, and we seek the \emph{unknown solution}~$u(x)$.  The
simplest physical interpretation of~\eqref{eq: model 1d} is as a steady-state
heat equation, so that $u(x)$ is the temperature at~$x$ and $f(x)$ gives the
density of heat sources.

For a constant source term~$f(x)=c$, the solution is given by
\begin{equation}\label{eq: u const f}
u(x)=\frac{1}{L}\bigl((L-x)\gamma_0+x\gamma_L\bigr)+\frac{c}{2}\,x(L-x)
	\quad\text{for $0\le x\le L$;}
\end{equation}
see Exercise~\ref{ex: u const f} and Figure~\ref{fig: bvp 1d f const}. For a
general~$f$, Exercise~\ref{ex: variation of params} shows that
\begin{equation}\label{eq: model 1d exact soln}
u(x)=\frac{L-x}{L}\biggl(\gamma_0+\int_0^x yf(y)\,dy\biggr)
	+\frac{x}{L}\biggl(\gamma_L+\int_x^L(L-y)f(y)\,dy\biggr)
	\quad\text{for $0\le x\le L$.}
\end{equation}
Our aim in this chapter is to study finite difference methods for computing
a numerical approximation to the values of~$u$ at a set of grid points in the
interval~$[0,L]$.

\begin{figure}
\caption{Solutions of the simple two-point boundary-value
problem~\eqref{eq: model 1d} with $L=1$ and right-hand side~$f(x)=c$, for
various choices of the constant~$c$.}\label{fig: bvp 1d f const}
\begin{center}
\includegraphics[scale=0.75]{../src/chap1/bvp1d_const_rhs.pdf}
\end{center}
\end{figure}

\section{Second-order central difference}
To derive a finite difference approximation to the second derivative~$u''(x)$,
we will use Taylor's theorem in the following form.

\begin{theorem}\label{thm: Taylor remainder}
Let $h>0$. If $f$ is $C^{k+1}$ on the closed interval~$[x,x+h]$ then
\begin{equation}\label{eq: Taylor 1}
f(x+h)=\sum_{j=0}^k\frac{1}{j!}\,f^{(j)}(x)\,h^j+(R_kf)(x,h),
\end{equation}
where the remainder term is given by
\begin{equation}\label{eq: Taylor 2}
(R_kf)(x,h)=\frac{1}{k!}\int_x^{x+h}(x+h-y)^kf^{(k+1)}(y)\,dy
\end{equation}
and satisfies
\begin{equation}\label{eq: Taylor 3}
|(R_kf)(x,h)|\le\frac{h^{k+1}}{(k+1)!}\,\max_{x\le y\le x+h}|f^{(k+1)}(y)|.
\end{equation}
Similarly, if $f$ is $C^{k+1}$ on~$[x-h,x]$, then
\[
f(x-h)=\sum_{j=0}^k\frac{(-1)^j}{j!}\,f^{(j)}(x)\,h^j+(R_kf)(x,-h)
\]
where
\[
(R_kf)(x,-h)=\frac{(-1)^{k+1}}{k!}\int_{x-h}^x(y+h-x)^kf^{(k+1)}(y)\,dy
\]
and
\[
|(R_kf)(x,-h)|\le\frac{h^{k+1}}{(k+1)!}\,\max_{x-h\le y\le x}|f^{(k+1)}(y)|.
\]
\end{theorem}
\begin{proof}
We use induction on~$k$.  If $k=0$, then the formulae
\eqref{eq: Taylor 1}~and \eqref{eq: Taylor 2} reduce to
\[
f(x+h)=f(x)+(R_0f)(x,h)\quad\text{where}\quad
(R_0f)(x,h)=\int_x^{x+h}f'(y)\,dy,
\]
which follows from the fundamental theorem of calculus.  Let $k\ge0$ and make
the induction hypothesis that \eqref{eq: Taylor 1} holds with $R_kf$ given
by~\eqref{eq: Taylor 2}.  Integrating by parts, we have
\begin{align*}
(R_kf)(x,h)&=\biggl[
	-\frac{(x+h-y)^{k+1}}{(k+1)!}\,f^{(k+1)}(y)\biggr]_{y=x}^{x+h}
	+\int_x^{x+h}\frac{(x+h-y)^{k+1}}{(k+1)!}\,f^{(k+2)}(y)\,dy\\
	&=\frac{h^{k+1}}{(k+1)!}\,f^{(k+1)}(x)+(R_{k+1}f)(x,y),
\end{align*}
implying that \eqref{eq: Taylor 1}~and \eqref{eq: Taylor 2} hold with~$k$
replaced by~$k+1$, as required.  The estimate~\eqref{eq: Taylor 3} is an
immediate consequence of the inequality
\begin{align*}
|(R_kf)(x,y)|&\le\int_x^{x+h}\frac{(x+h-y)^k}{k!}\,|f^{(k+1)}(y)|\,dy\\
	&\le\biggl(\max_{x\le y\le x+h}|f^{(k+1)}(y)|\biggr)
	\int_x^{x+h}\frac{(x+h-y)^k}{k!}\,dy.
\end{align*}
The expansion for~$f(x-y)$ and the bound for~$(R_kf)(x,-h)|$ follow in a
similar fashion.
\end{proof}

A straight forward manipulation of such Taylor expansions shows that
\[
f''(x)=\frac{f(x+h)-2f(x)+f(x-h)}{h^2}+O(h^2)\quad\text{as $h\to0$;}
\]
more precisely, the following result holds.

\begin{theorem}\label{thm: 2nd central diff}
If $f$ is $C^4$ on the closed interval~$[x-h,x+h]$, then
\[
\biggl|f''(x)-\frac{f(x+h)-2f(x)+f(x-h)}{h^2}\biggr|\le\frac{h^2}{12}
	\max_{x-h\le y\le x+h}|f^{(4)}(y)|.
\]
\end{theorem}
\begin{proof}
Since
\[
f(x+h)=f(x)+f'(x)h+\tfrac12f''(x)h^2+\tfrac{1}{3!}f'''(x)h^3+(R_3f)(x,h)
\]
and
\[
f(x-h)=f(x)-f'(x)h+\tfrac12f''(x)h^2-\tfrac{1}{3!}f'''(x)h^3+(R_3f)(x,-h)
\]
we see that
\[
f(x+h)+f(x-h)=2f(x)+f''(x)h^2+(R_3f)(x,h)+(R_3f)(x,-h).
\]
Thus,
\begin{equation}\label{eq: second diff remainder}
\frac{f(x+h)-2f(x)+f(x-h)}{h^2}-f''(x)=\frac{(R_3f)(x,h)+(R_3f)(x,-h)}{h^2}
\end{equation}
and since
\begin{align*}
|(R_3f)(x,h)+(R_3f)(x,-h)|&\le
\frac{h^4}{4!}\max_{x\le y\le x+h}|f^{(4)}(y)|
+\frac{h^4}{4!}\max_{x-h\le y\le x}|f^{(4)}(y)|\\
	&\le\frac{2h^4}{4!}\,\max_{x-h\le y\le x+h}|f^{(4)}(y)|
\end{align*}
the result follows.
\end{proof}

To set up a finite difference scheme for~\eqref{eq: model 1d}, we choose a
positive integer~$P$ and define a uniform grid on~$[0,L]$,
\[
x_p=p\,\Delta x\quad\text{for $0\le p\le P$,}
	\quad\text{where $\Delta x=\frac{L}{P}$.}
\]
In this way, we divide $[0,L]$ into $P$~subintervals, namely $[x_{p-1},x_p]$
for~$1\le p\le P$, each of length~$\Delta x$.  The finite difference solution
consists of $P+1$ numbers $U_0$, $U_1$, \dots, $U_P$ that approximate $u(x)$ at
the $P+1$ grid points, that is,
\[
U_p\approx u(x_p)\quad\text{for $0\le p\le P$.}
\]
Noting that $x_{p\pm1}=x_p\pm\Delta x$, we have
\begin{equation}\label{eq: u'' approx}
\frac{u(x_{p+1})-2u(x_p)+u(x_{p-1})}{\Delta x^2}
	=\frac{u(x_p+\Delta x)-2u(x_p)+u(x_p-\Delta x)}{\Delta x^2}
	\approx u''(x_p)
\end{equation}
which suggests the following discrete approximation to the ODE $-u''=f(x)$,
\begin{equation}\label{eq: model 1d discrete}
-\frac{U_{p+1}-2U_p+U_{p-1}}{\Delta x^2}=f(x_p)\quad\text{for $1\le p\le P-1$.}
\end{equation}
We can satisfy the boundary conditions $u(x_0)=u(0)=\gamma_0$ and
$u(x_P)=u(L)=\gamma_L$ exactly, by putting
\[
U_0=\gamma_0\quad\text{and}\quad U_P=\gamma_L.
\]
When~$p=1$ in~\eqref{eq: model 1d discrete} we move $U_0=\gamma_0$ to the
right-hand side,
\[
\frac{2U_1-U_2}{\Delta x^2}=f(x_1)+\frac{\gamma_0}{\Delta x^2},
\]
and similarly when $p=P-1$ we move $U_P=\gamma_L$ to the right-hand side,
\[
\frac{-U_{P-2}+2U_{P-1}}{\Delta x^2}=f(x_{P-1})+\frac{\gamma_L}{\Delta x^2}.
\]

\begin{figure}
\caption{Comparison of exact solution and its finite difference approximation
from Example~\ref{example: bvp1d example}.}\label{fig: bvp1d example}
\begin{center}
\includegraphics[scale=0.75]{../src/chap1/bvp1d_example.pdf}
\end{center}
\end{figure}

\begin{example}\label{example: bvp1d example}
If $P=6$ then the finite difference scheme results in a $5\times5$
linear system
\begin{equation}\label{eq: model 1d linear system}
\frac{1}{\Delta x^2}\begin{bmatrix}
 2&-1&  & &\\
-1& 2&-1& &\\
  &-1& 2&-1&\\
  &  &-1& 2&-1\\
  &  &  &-1& 2
\end{bmatrix}
\begin{bmatrix}U_1\\ U_2\\ U_3\\ U_4\\ U_5\end{bmatrix}
=\begin{bmatrix}f_1\\ f_2\\ f_3\\ f_4\\ f_5 \end{bmatrix}
+\frac{1}{\Delta x^2}
\begin{bmatrix}\gamma_0\\ 0\\ 0 \\ 0\\ \gamma_L \end{bmatrix},
\end{equation}
where, for brevity, we have written $f_p=f(x_p)$.  Also, the zero entries in
the coefficient matrix are left blank.  The problem
\[
-u''=5e^{-x}\quad\text{for $0\le x\le2$,}
    \quad\text{with $u(0)=-1$ and $u(2)=5/2$,}
\]
has the solution
\[
u(x)=A+Bx+5e^{-x}\quad
\text{where $A=\gamma_0+5$ and $B=(\gamma_L-A-5e^{-L})$.}
\]
Figure~\ref{fig: bvp1d example} shows that $U_p$ does in fact provide a
reasonable approximation to~$u(x_p)$ in this case using only $P=6$~subintervals.
\end{example}

Several questions arise naturally with regard to the above numerical method.
\begin{enumerate}
\item Does the linear system arising from the finite difference approximation
always have a unique solution?
\item If so, what is an efficient way to compute the $U_p$?
\item How accurate is the approximation~$U_p\approx u(x_p)$?
\item Does the error $U_p-u(x_p)$ tend to zero if~$\Delta x\to0$, and if so how
rapidly?
\end{enumerate}
The following sections will address these concerns.

\section{Symmetric tridiagonal linear systems}
\label{sec: sym tridiagonal}
How can we solve a symmetric, tridiagonal linear system such as the one
\eqref{eq: model 1d linear system} arising from the finite difference
scheme~\eqref{eq: model 1d discrete}?  To discuss this problem, consider a
$5\times5$ matrix of the form
\begin{equation}\label{eq: A symm tridiagonal}
\boldsymbol{A}=\begin{bmatrix}
\alpha_1& \beta_1&        &        &\\
 \beta_1&\alpha_1& \beta_2&        &\\
        & \beta_2&\alpha_3&\beta_3 &\\
        &        & \beta_3&\alpha_4&\beta_4\\
        &        &        & \beta_4&\alpha_5
\end{bmatrix}.
\end{equation}
A standard algorithm involves computing $5\times5$~matrices
$\boldsymbol{L}$~and $\boldsymbol{D}$ of the form
\[
\boldsymbol{L}=\begin{bmatrix}
     1&      &      &      &\\
\ell_1&     1&      &      &\\
      &\ell_2&     1&      &\\
      &      &\ell_3&     1&\\
      &      &      &\ell_4&1
  \end{bmatrix}
\quad\text{and}\quad
\boldsymbol{D}=\begin{bmatrix}
d_1&   &   &   &\\
   &d_2&   &   &\\
   &   &d_3&   &\\
   &   &   &d_4&\\
   &   &   &   &d_5
  \end{bmatrix}
\]
having the property that
\begin{equation}\label{eq: L D LT}
\boldsymbol{A}=\boldsymbol{L}\boldsymbol{D}\boldsymbol{L}^\top.
\end{equation}
Given a right-hand side vector~$\boldsymbol{b}$, we can solve the linear system
$\boldsymbol{A}\boldsymbol{x}=\boldsymbol{b}$ by solving in sequence the three
linear systems
\begin{equation}\label{eq: LDLT systems}
\boldsymbol{L}\boldsymbol{z}=\boldsymbol{b},\qquad
\boldsymbol{D}\boldsymbol{y}=\boldsymbol{z},\qquad
\boldsymbol{L}^\top\boldsymbol{x}=\boldsymbol{y},
\end{equation}
because it will then follow that
\[
\boldsymbol{A}\boldsymbol{x}
    =\boldsymbol{L}\boldsymbol{D}\boldsymbol{L}^T\boldsymbol{x}
    =\boldsymbol{L}\boldsymbol{D}\boldsymbol{y}
    =\boldsymbol{L}\boldsymbol{z}=\boldsymbol{b}.
\]
Since $\boldsymbol{L}$ is \emph{lower triangular} and $\boldsymbol{D}$ is
\emph{diagonal}, we can easily compute first $\boldsymbol{z}$, then
$\boldsymbol{y}$ and finally $\boldsymbol{x}$.  To see how, we write out the
equations in the $5\times5$~case:
\begin{align*}
      z_1    &=b_1,& d_1y_1&=z_1,& x_1+\ell_1 x_2&=y_1,\\
\ell_1z_1+z_2&=b_2,& d_2y_2&=z_2,& x_2+\ell_2 x_3&=y_2,\\
\ell_2z_2+z_3&=b_3,& d_3y_3&=z_3,& x_3+\ell_3 x_4&=y_3,\\
\ell_3z_3+z_4&=b_4,& d_4y_4&=z_4,& x_4+\ell_4 x_5&=y_4,\\
\ell_4z_4+z_5&=b_5,& d_5y_5&=z_5,& x_5           &=y_5.
\end{align*}
Hence, the steps of the computation are as follows:
\begin{align*}
z_1&=b_1,           & y_1&=z_1/d_1,& x_5&=y_5,\\
z_2&=b_2-\ell_1 z_1,& y_2&=z_2/d_2,& x_4&=y_4-\ell_4x_5,\\
z_3&=b_3-\ell_2 z_2,& y_3&=z_3/d_3,& x_3&=y_3-\ell_3x_4,\\
z_4&=b_4-\ell_3 z_3,& y_4&=z_4/d_4,& x_2&=y_2-\ell_2x_3,\\
z_5&=b_5-\ell_4 z_4,& y_5&=z_5/d_5,& x_1&=y_1-\ell_1x_2.\\
\end{align*}
Matrix multiplication gives
\[
\boldsymbol{L}\boldsymbol{D}\boldsymbol{L}^\top=\begin{bmatrix}
      d_1&      \ell_1d_1&               &               &         \\
\ell_1d_1&d_2+\ell_1^2d_1&      \ell_2d_2&               &         \\
         &      \ell_2d_2&d_3+\ell_2^2d_2&      \ell_3d_3&         \\
         &               &      \ell_3d_3&d_4+\ell_3^2d_3&\ell_4d_4\\
         &               &               &      \ell_4d_4&d_5+\ell_4^2d_4
\end{bmatrix},
\]
so the factorization~\eqref{eq: L D LT} requires that
\begin{align*}
     \alpha_1&=d_1,      &        &                 &&&&&&\\
      \beta_1&=\ell_1d_1,&\alpha_2&=d_2+\ell_1^2d_1,&&&&&&\\
    &&\beta_2&=\ell_2d_2,&\alpha_3&=d_3+\ell_2^2d_2,&&&&\\
  &&&&\beta_3&=\ell_3d_3,&\alpha_4&=d_4+\ell_3^2d_3,&&\\
&&&&&&\beta_4&=\ell_4d_4,&\alpha_5&=d_5+\ell_4^2d_4.
\end{align*}
Thus, the steps in computing the entries of $\boldsymbol{L}$~and
$\boldsymbol{D}$ are as follows:
\begin{align*}
         d_1&=\alpha_1,   &   &                      &&&&&&\\
      \ell_1&=\beta_1/d_1,&d_2&=\alpha_2-\ell_1^2d_1,&&&&&&\\
    &&\ell_2&=\beta_2/d_2,&d_3&=\alpha_3-\ell_2^2d_2,&&&&\\
  &&&&\ell_3&=\beta_3/d_3,&d_4&=\alpha_4-\ell_3^2d_3,&&\\
&&&&&&\ell_4&=\beta_4/d_4,&d_5&=\alpha_5-\ell_4^2d_4.
\end{align*}
In the general $n\times n$~case,
Algorithm~\ref{alg: LDLT} computes the factorization~\eqref{eq: L D LT}
and Algorithm~\ref{alg: solve symmetric tridiagonal}
uses this factorization to solve the linear
system~$\boldsymbol{A}\boldsymbol{x}=\boldsymbol{b}$.

The derivations above show that if the factorization \eqref{alg: LDLT} exists,
then it is unique, that is, $\boldsymbol{L}$ and $\boldsymbol{D}$ are uniquely
determined by~$\boldsymbol{A}$.  Unfortunately,
Algorithm~\ref{alg: LDLT} can break down because one of the $d_j$ is zero, as
the following example shows.

\begin{example}
The $3\times3$ matrix
\[
\boldsymbol{A}=\begin{bmatrix}1&1&0\\1&1&2\\ 0&2&0 \end{bmatrix}
\]
is non-singular with inverse
\[
\boldsymbol{A}^{-1}=\begin{bmatrix}1&0&-1/2\\ 0&0&1/2\\ -1/2&1/2&0\end{bmatrix},
\]
but Algorithm~\ref{alg: LDLT} produces
\[
d_1=1,\quad \ell_1=1,\quad d_2=0,
\]
and then breaks down because $\ell_2=2/0$.
\end{example}

However, the following result can be shown.

\begin{theorem}
If the $n\times n$, symmetric, tridiagonal matrix $\boldsymbol{A}$ is
positive-definite, then the factorization~\eqref{eq: L D LT} exists and $d_j>0$
for $1\le j\le n$.
\end{theorem}

\begin{algorithm}
\caption{Compute the factorization \eqref{eq: L D LT} for a symmetric,
tridiagonal matrix $\boldsymbol{A}$.}
\label{alg: LDLT}
\begin{algorithmic}
\Require{$\boldsymbol{\alpha}=[\alpha_1,\alpha_2,\ldots,\alpha_n]$ is the main
diagonal of $\boldsymbol{A}$.}
\Require{$\boldsymbol{\beta}=[\beta_1,\beta_2,\ldots,\beta_{n-1}]$ is the
off-diagonal of $\boldsymbol{A}$.}
\Statex
\Function{Factorize}{$\boldsymbol{\alpha}, \boldsymbol{\beta}$}
\State Allocate storage for $\boldsymbol{d}=[d_1,d_2,\ldots, d_n]$ and
$\boldsymbol{\ell}=[\ell_1,\ell_2,\ldots,\ell_{n-1}]$
\State $d_1=\alpha_1$
\For{$j=1:n-1$}
\State $\ell_j=\beta_j/d_j$
\State $d_{j+1}=\alpha_{j+1}-\ell_j^2d_j$
\EndFor
\State\Return{$\boldsymbol{d}$, $\boldsymbol{\ell}$}
\EndFunction
\end{algorithmic}
\end{algorithm}

\begin{algorithm}
\caption{Solve a symmetric, tridiagonal linear system
$\boldsymbol{A}\boldsymbol{x}=\boldsymbol{b}$ given the
factorization~\eqref{eq: L D LT}.}
\label{alg: solve symmetric tridiagonal}
\begin{algorithmic}
\Require{$\boldsymbol{b}=[b_1,b_2,\ldots,b_n]$ the right-hand side vector.}
\Require{$\boldsymbol{d}=[d_1, d_2, \ldots, d_n]$ the main diagonal of
$\boldsymbol{D}$.}
\Require{$\boldsymbol{\ell}=[\ell_1,\ell_2,\ldots,\ell_{n-1}]$ the first
sub-diagonal of~$\boldsymbol{L}$.}
\Statex
\Function{Solve}{$\boldsymbol{b},\boldsymbol{d},\boldsymbol{\ell}$}
\State Allocate storage for $\boldsymbol{x}=[x_1,x_2,\ldots,x_n]$,
$\boldsymbol{y}=[y_1,y_2,\ldots,y_n]$ and $\boldsymbol{z}=[z_1,z_2,\ldots,z_n]$.
\State $z_1=b_1$
\For{$j=1:n-1$}
\State $z_{j+1}=b_{j+1}-\ell_jz_j$
\EndFor
\For{$j=1:n$}
\State $y_j=z_j/d_j$
\EndFor
\State $x_n=y_n$
\For{$j=n-1:-1:1$}
\State $x_j=y_j-\ell_jx_{j+1}$
\EndFor
\State\Return{$\boldsymbol{x}$}
\EndFunction
\end{algorithmic}
\end{algorithm}

Once $d_j$ has been computed, the value of $\alpha_j$ is never used in
subsequent steps of Algorithm~\ref{alg: LDLT}. Similarly, once $\ell_j$ has been
computed, the value of~$\beta_j$ is never used subsequently.  It is therefore
possible to economize on storage by computing the factorization
\emph{in place}, that is, overwriting $\alpha_j$ with~$d_j$,
and likewise overwriting $\beta_j$ with~$\ell_j$, as shown in
Algorithm~\ref{alg: LDLT in place}.  The solution of the triangular and
diagonal linear systems can also be performed in place, as shown in
Algorithm~\ref{alg: solve symmetric tridiagonal in place}.  Here, we have
followed the convention from Julia that an exclamation mark~! is appended to
the name of any function that modifies at least one of its arguments.  Also,
the symbol~$\gets$ in this context means ``is overwritten with''.

\begin{algorithm}
\caption{Compute the factorization \eqref{eq: L D LT} in place.}
\label{alg: LDLT in place}
\begin{algorithmic}
\Require{$\boldsymbol{d}=[\alpha_1,\alpha_2,\ldots,\alpha_n]$ holds the main
diagonal of $\boldsymbol{A}$.}
\Require{$\boldsymbol{\ell}=[\beta_1,\beta_2,\ldots,\beta_{n-1}]$ holds the
off-diagonal of $\boldsymbol{A}$.}
\Statex
\Function{Factorize!}{$\boldsymbol{d}$, $\boldsymbol{\ell}$}
\For{$j=1:n-1$}
\State $\ell_j\gets\ell_j/d_j$
\State $d_{j+1}\gets d_{j+1}-\ell_j^2d_j$
\EndFor
\EndFunction
\end{algorithmic}
\end{algorithm}

\begin{algorithm}
\caption{Solve a symmetric, tridiagonal linear system
$\boldsymbol{A}\boldsymbol{x}=\boldsymbol{b}$ in place.}
\label{alg: solve symmetric tridiagonal in place}
\begin{algorithmic}
\Require{$\boldsymbol{x}=[b_1,b_2,\ldots,b_n]$ holds the right-hand side
vector.}
\Require{$\boldsymbol{d}=[d_1, d_2, \ldots, d_n]$ holds the main diagonal of
$\boldsymbol{D}$.}
\Require{$\boldsymbol{\ell}=[\ell_1,\ell_2,\ldots,\ell_{n-1}]$ holds the first
sub-diagonal of~$\boldsymbol{L}$.}
\Statex
\Function{Solve!}{$\boldsymbol{x},\boldsymbol{d},\boldsymbol{\ell}$}
\For{$j=1:n-1$}
    \State $x_{j+1}\gets x_{j+1}-\ell_jx_j$
\EndFor
\For{$j=1:n$}
    \State $x_j\gets x_j/d_j$
\EndFor
\For{$j=n-1:-1:1$}
    \State $x_j\gets x_j-\ell_jx_{j+1}$
\EndFor
\EndFunction
\end{algorithmic}
\end{algorithm}

Counting the numbers of arithmetic operations in Algorithms
\ref{alg: LDLT}~ and \ref{alg: solve symmetric tridiagonal} yields
\cref{tab: LDLT flops}.  If we use the in-place versions,
Algorithms \ref{alg: LDLT in place}~and
\ref{alg: solve symmetric tridiagonal in place}, then we have to store only
$3n-1$ floating-point numbers.  Thus, the overall computational cost is
$O(n)$~operations and $O(n)$~storage, implying that the computational cost of
solving the finite difference equations~\eqref{eq: model 1d discrete} is
$O(P)$~operations and $O(P)$~storage.  We therefore expect that the runtime and
memory requirements of a program to compute~$\boldsymbol{U}$ will scale
linearly with~$P$ for our 1D boundary-value problem.

\begin{table}
\caption{Operation counts for solving a symmetric positive-definite,
tridiagonal linear system.}\label{tab: LDLT flops}
\begin{center}
\begin{tabular}{c|c|c}
&Algorithm~\ref{alg: LDLT}
&Algorithm~\ref{alg: solve symmetric
tridiagonal}\\
\hline
additions/subtractions&   $n-1$&$2(n-1)$\\
       multiplications&$2(n-1)$&$2(n-1)$\\
             divisions&   $n-1$&$n$
\end{tabular}
\end{center}
\end{table}

\section{General two-point boundary-value problem}
\label{sec: gen two-point bvp}

Now consider a general second-order linear differential operator,
\[
\mathcal{L}u=-a(x)u''+b(x)u'+c(x)u,
\]
where, for simplicity, we will assume that the coefficients $a$, $b$ and
$c$ are continuous on~$[0,L]$, and that the \emph{leading coefficient}~$a$ is
\emph{strictly positive} on~$[0,L]$, so there is a constant~$a_{\min}$ such that
\begin{equation}\label{eq: ellipticity 1d}
a(x)\ge a_{\min}>0\quad\text{for $0\le x\le L$.}
\end{equation}
The general \emph{two-point boundary-value problem} is to find $u=u(x)$
satisfying
\begin{equation}\label{eq: two-point bvp}
\begin{aligned}
\mathcal{L}u&=f(x)&&\text{for $0<x<L$,}\\
\alpha_0u'+\beta_0u&=\gamma_0&&\text{at $x=0$,}\\
\alpha_Lu'+\beta_Lu&=\gamma_L&&\text{at $x=L$,}
\end{aligned}
\end{equation}
where, for the boundary conditions to make sense, we assume that at least one
of $\alpha_0$~and $\beta_0$ is not zero, and likewise at least one of
$\alpha_L$~and $\beta_L$ is not zero.  For simplicity, we will also assume that
the function~$f$ is continuous on~$[0,L]$.  The simple
problem~\eqref{eq: model 1d} is just the special case
\[
a(x)=1,\quad b(x)=0,\quad
c(x)=0,\quad\alpha_0=0,\quad\beta_0=1,\quad\alpha_L=0,\quad\beta_L=1.
\]

To construct a finite difference scheme for~\eqref{eq: two-point bvp}, we need
to approximate the first derivative~$u'$.  Once again, we apply Taylor
expansions, this time showing that
\[
\frac{f(x+h)-f(x-h)}{2h}=f'(x)+O(h^2)\quad\text{as $h\to0$.}
\]
More precisely, the following holds.

\begin{theorem}\label{thm: first central diff}
If $f$ is $C^3$ on the closed interval~$[x-h,x+h]$, then
\[
\biggl|f'(x)-\frac{f(x+h)-f(x-h)}{2h}\biggr|
	\le\frac{h^2}{6}\,\max_{x-h\le y\le x+h}|f'''(y)|.
\]
\end{theorem}
\begin{proof}
Since
\[
f(x+h)=f(x)+f'(x)h+\tfrac12f''(x)h^2+(R_2f)(x,h)
\]
and
\[
f(x-h)=f(x)-f'(x)h+\tfrac12f''(x)h^2+(R_2f)(x,-h),
\]
we have
\[
f(x+h)-f(x-h)=2f'(x)h+(R_2f)(x,h)-(R_2f)(x,-h).
\]
Thus,
\[
\frac{f(x+h)-f(x-h)}{2h}-f'(x)=\frac{(R_2f)(x,h)-(R_2f)(x,-h)}{2h},
\]
and since
\begin{align*}
\bigl|(R_2f)(x,h)-(R_2f)(x,-h)\bigr|
	&\le\frac{h^3}{3!}\max_{x\le y\le x+h}|f'''(y)|
 	   +\frac{h^3}{3!}\max_{x-h\le y\le x}|f'''(y)|\\
	&\le\frac{2h^3}{3!}\max_{x-h\le y\le x+h}|f'''(y)|,
\end{align*}
the result follows at once.
\end{proof}

The approximations \eqref{eq: u'' approx}~and
\begin{equation}\label{eq: u' approx}
\frac{u(x_{p+1})-u(x_{p-1})}{2\,\Delta x}
	=\frac{u(x_p+\Delta x)-u(x_p-\Delta x)}{2\,\Delta x}
	\approx u'(x_p)
\end{equation}
suggest the following discrete approximation to~$(\mathcal{L}u)(x_p)$,
\begin{equation}\label{eq: Lu Delta x}
(\mathcal{L}_{\Delta x}U)_p=
-a_p\,\frac{U_{p+1}-2U_p+U_{p-1}}{\Delta x^2}
	+b_p\,\frac{U_{p+1}-U_{p-1}}{2\,\Delta x}+c_pU_p,
\end{equation}
where we have used the abbreviations $a_p=a(x_p)$, $b_p=b(x_p)$~and
$c_p=c(x_p)$.  The central difference approximation to~$u'$ can also be used
in the boundary conditions by allowing ``ghost'' grid points $x_{-1}=-\Delta
x$~and $x_{P+1}=L+\Delta x$ lying just outside the interval~$[0,L]$, so that
\begin{equation}\label{eq: bc ghost points}
u'(0)\approx\frac{U_1-U_{-1}}{2\,\Delta x}
\quad\text{and}\quad
u'(L)\approx\frac{U_{P+1}-U_{P-1}}{2\,\Delta x}.
\end{equation}
Four cases can occur.
\begin{enumerate}
\item If $\alpha_0=0$ and $\alpha_L=0$, then we require
\[
(\mathcal{L}_{\Delta x}U)_p=f_p\quad\text{for $1\le p\le P-1$,}\quad
\text{with $\beta_0U_0=\gamma_0$ and $\beta_LU_P=\gamma_L$.}
\]
Eliminating $U_0=\gamma_0/\beta_0$~and $U_P=\gamma_L/\beta_L$ leads to a
$(P-1)\times(P-1)$ linear system for $U_1$, $U_2$, \dots, $U_{P-1}$.
\item If $\alpha_0\ne0$ and $\alpha_L=0$, then we require
\[
(\mathcal{L}_{\Delta x}U)_p=f_p\quad\text{for $0\le p\le P-1$,}\quad
\text{with $\alpha_0\,\frac{U_1-U_{-1}}{2\,\Delta x}+\beta_0U_0=\gamma_0$
and $\beta_LU_P=\gamma_L$.}
\]
Eliminating $U_{-1}=U_1+2\,\Delta x\,(\beta_0U_0-\gamma_0)/\alpha_0$ and
$U_P=\gamma_L/\beta_L$ leads to a $P\times P$ linear system for $U_0$,
$U_1$, \dots, $U_{P-1}$.
\item If $\alpha_0=0$ and $\alpha_L\ne0$, then we require
\[
(\mathcal{L}_{\Delta x}U)_p=f_p\quad\text{for $1\le p\le P$,}\quad
\text{with $\beta_0U_0=\gamma_0$ and
$\alpha_L\,\frac{U_{P+1}-U_{P-1}}{2\,\Delta x}+\beta_LU_P=\gamma_L$.}
\]
Eliminating $U_0=\gamma_0/\beta_0$ and
$U_{P+1}=U_{P-1}+2\,\Delta x(\gamma_L-\beta_LU_P)/\alpha_L$ leads to a
$P\times P$ linear system for $U_1$, $U_2$, \dots, $U_P$.
\item If $\alpha_0\ne0$ and $\alpha_L\ne0$, then we require
\[
(\mathcal{L}_{\Delta x}U)_p=f_p\quad\text{for $0\le p\le P$,}
\]
with
\[
\alpha_0\,\frac{U_1-U_{-1}}{2\,\Delta x}+\beta_0U_0=\gamma_0
\quad\text{and}\quad
\alpha_L\,\frac{U_{P+1}-U_{P-1}}{2\,\Delta x}+\beta_LU_P=\gamma_L.
\]
Eliminating $U_{-1}$~and $U_{P+1}$ leads to a $(P+1)\times(P+1)$ linear system
for $U_0$, $U_1$, \dots, $U_P$.
\end{enumerate}

In case~1, the first equation is
\[
-a_1\,\frac{U_2-2U_1+U_0}{\Delta x^2}+b_1\,\frac{U_2-U_0}{2\,\Delta x}
	+c_1U_1=f_1
\]
so $U_0=\gamma_0/\beta_0$ gives
\[
a_1\,\frac{2U_1-U_2}{\Delta x^2}+b_1\,\frac{U_2}{2\,\Delta x}+c_1U_1
	=f_1+\biggl(\frac{a_1}{\Delta x^2}+\frac{b_1}{2\,\Delta x}\biggr)
	\frac{\gamma_0}{\beta_0}.
\]
Similarly, the last equation is
\[
-a_{P-1}\,\frac{U_P-2U_{P-1}+U_{P-2}}{\Delta x^2}
	+b_{P-1}\,\frac{U_P-U_{P-2}}{2\,\Delta x}+c_{P-1}U_{P-1}=f_{P-1},
\]
so $U_P=\gamma_L/\beta_L$ gives
\[
a_{P-1}\,\frac{-U_{P-2}+2U_{P-1}}{\Delta x^2}
	-b_{P-1}\,\frac{U_{P-2}}{2\,\Delta x}+c_{P-1}U_{P-1}
	=f_{P-1}+\biggl(\frac{a_{P-1}}{\Delta x^2}-\frac{b_{P-1}}{2\,\Delta x}
	\biggr)\,\frac{\gamma_L}{\beta_L}.
\]
For example, if $P=6$ we obtain a $5\times5$ linear system
\begin{equation}\label{eq: finite diff matrix 1d}
\boldsymbol{A}\boldsymbol{U}+\boldsymbol{B}\boldsymbol{U}
	+\boldsymbol{C}\boldsymbol{U}=\boldsymbol{f}+\boldsymbol{g},
\end{equation}
where
\begin{gather*}
\boldsymbol{A}=\frac{1}{\Delta x^2}\begin{bmatrix}
2a_1&-a_1&    &    &    \\
-a_2&2a_2&-a_2&    &    \\
    &-a_3&2a_3&-a_3&    \\
    &    &-a_4&2a_4&-a_4\\
    &    &    &-a_5&2a_5\\
               \end{bmatrix},\qquad
\boldsymbol{B}=\frac{1}{2\,\Delta x}\begin{bmatrix}
   0& b_1&    &    &    \\
-b_2&   0& b_2&    &    \\
    &-b_3&   0& b_3&    \\
    &    &-b_4&   0& b_4\\
    &    &    &-b_5&   0\end{bmatrix},\\
\boldsymbol{C}=\begin{bmatrix}
c_1&   &   &   &   \\
   &c_2&   &   &   \\
   &   &c_3&   &   \\
   &   &   &c_4&   \\
   &   &   &   &c_5
\end{bmatrix},\quad
\boldsymbol{U}=\begin{bmatrix} U_1\\ U_2\\ U_3\\ U_4\\ U_5\end{bmatrix},\quad
\boldsymbol{f}=\begin{bmatrix} f_1\\ f_2\\ f_3\\ f_4\\ f_5\end{bmatrix},\quad
\boldsymbol{g}=\begin{bmatrix} g_1\\ 0\\ 0\\ 0\\ g_5 \end{bmatrix},
\end{gather*}
where
\[
g_1=\biggl(\frac{a_1}{\Delta x^2}+\frac{b_1}{2\,\Delta x}\biggr)
	\frac{\gamma_0}{\beta_0}
\quad\text{and}\quad
g_5=\biggl(\frac{a_{P-1}}{\Delta x^2}-\frac{b_{P-1}}{2\,\Delta x}
	\biggr)\,\frac{\gamma_L}{\beta_L}.
\]

In case~4, the first equation is
\[
-a_0\,\frac{U_1-2U_0+U_{-1}}{\Delta x^2}+b_0\,\frac{U_1-U_{-1}}{2\,\Delta x}
    +c_0U_0=f_0
\]
so $U_{-1}=U_1+2\,\Delta x(\beta_0U_0-\gamma_0)/\alpha_0$ gives
\[
-2a_0\,\frac{U_1-U_0}{\Delta x^2}+c_0U_0
    -\frac{\beta_0(2a_0+b_0\,\Delta x)}{\alpha_0\,\Delta x}\,U_0
    =f_0
    -\frac{2a_0+b_0\,\Delta x}{\alpha_0\,\Delta x}\,\gamma_0.
\]
Similarly, the last equation is
\[
-a_P\,\frac{U_{P+1}-2U_P+U_{P-1}}{\Delta x^2}
    +b_P\,\frac{U_{P+1}-U_{P-1}}{2\,\Delta x}+c_PU_P=f_P,
\]
so $U_{P+1}=U_{P-1}+2\,\Delta x(\gamma_L-\beta_LU_P)/\alpha_L$ gives
\[
-2a_P\,\frac{-U_P+U_{P-1}}{\Delta x^2}+c_PU_P
    +\frac{\beta_L(2a_P-b_P\,\Delta x)}{\alpha_L\,\Delta x}\,U_P
    =f_P+\frac{2a_P-b_P\,\Delta x}{\alpha_L\,\Delta x}\,\gamma_L.
\]
For example, if $P=4$ then we obtain a $5\times5$ linear
system of the form~\eqref{eq: finite diff matrix 1d} but now
\begin{gather*}
\boldsymbol{A}=\frac{1}{\Delta x^2}\begin{bmatrix}
2a_0(1-\beta_0\,\Delta x/\alpha_0)&-2a_0&    &    &    \\
-a_1&2a_1&-a_1&    &    \\
    &-a_2&2a_2&-a_2&    \\
    &    &-a_3&2a_3&-a_3\\
    &    &    &-2a_4&2a_4(1+\beta_L\,\Delta x/\alpha_L)\\
               \end{bmatrix},\\
\boldsymbol{B}=\frac{1}{2\,\Delta x}\begin{bmatrix}
-2b_0\beta_0\,\Delta x/\alpha_0&    &    &    &    \\
-b_1&   0& b_1&    &    \\
    &-b_2&   0& b_2&    \\
    &    &-b_3&   0& b_3\\
    &    &    &    &-2b_P\beta_L\,\Delta x/\alpha_L\end{bmatrix},\\
\boldsymbol{C}=\begin{bmatrix}
c_0&   &   &   &   \\
   &c_1&   &   &   \\
   &   &c_2&   &   \\
   &   &   &c_3&   \\
   &   &   &   &c_4
\end{bmatrix},\quad
\boldsymbol{U}=\begin{bmatrix} U_0\\ U_1\\ U_2\\ U_3\\ U_4\end{bmatrix},\quad
\boldsymbol{f}=\begin{bmatrix} f_0\\ f_1\\ f_2\\ f_3\\ f_4\end{bmatrix},\quad
\boldsymbol{g}=\begin{bmatrix} g_0\\ 0\\ 0\\ 0\\ g_4 \end{bmatrix},
\end{gather*}
where
\[
g_0=-\frac{2a_0+b_0\,\Delta x}{\alpha_0\,\Delta x}\,\gamma_0
\quad\text{and}\quad
g_4=\frac{2a_4-b_4\,\Delta x}{\alpha_L\,\Delta x}\,\gamma_L.
\]

The procedure in cases 2~and 3 should now be clear.

\section{Maximum principle}
It will be convenient to use the standard notation
\[
\|f\|_\infty=\max_{0\le x\le L}|f(x)|
\]
for the \emph{maximum norm} of any (continuous) function~$f$ over
the interval~$[0,L]$, and to let
\[
u^+(x)=\max\{u(x),0\}
\quad\text{and}\quad
u^-(x)=\min\{u(x),0\}.
\]
We begin by proving a \emph{maximum principle} that
follows from our \emph{ellipticity} assumption~\eqref{eq: ellipticity 1d} and
simple calculus.

\begin{lemma}\label{lem: Lu<0}
Assume that $u$ is $C^2$ on the open interval~$(0,L)$.
If $c\ge0$ and $\mathcal{L}u<0$ on~$(0,L)$, then $u$ cannot attain a
non-negative local maximum in~$(0,L)$.
\end{lemma}
\begin{proof}
Suppose for a contradiction that there exist $x_0\in(0,L)$~and $\delta>0$ such
that
\[
u(x_0)\ge0\quad\text{and}\quad
\text{$u(x)\le u(x_0)$ for~$x\in(x_0-\delta,x_0+\delta)\subseteq(0,L)$.}
\]
Since $u$ has an interior local maximum at~$x_0$, it follows that
$u'(x_0)=0$~and $u''(x_0)\le0$ so
\[
(\mathcal{L}u)(x_0)=-a(x_0)u''(x_0)+c(x_0)u(x_0)\ge 0,
\]
contradicting our assumption that $\mathcal{L}u<0$ on~$(0,L)$.
\end{proof}

\begin{theorem}\label{thm: max principle 1d}
Assume that $u$ is continuous on the closed interval~$[0,L]$ and is $C^2$ on
the open interval~$(0,L)$. If $c\ge0$ and $\mathcal{L}u\le0$ on~$(0,L)$, then
\[
u(x)\le\max\{u^+(0),u^+(L)\}\quad\text{for $0<x<L$.}
\]
\end{theorem}
\begin{proof}
Let $\epsilon>0$~and $\mu>0$, and put $w(x)=u(x)+\epsilon e^{\mu x}$.  Since
\[
(\mathcal{L}w)(x)
	=(\mathcal{L}u)(x)+\epsilon\bigl[-a(x)\mu^2+b(x)\mu+c(x)\bigr]e^{\mu x}
\le-\epsilon\bigl[a_{\min}\mu^2-\mu\|b\|_\infty-\|c\|_\infty\bigr]e^{\mu x}
\]
by choosing $\mu$ sufficiently large we can ensure that $\mathcal{L}w<0$
on~$(0,L)$.  By Lemma~\ref{lem: Lu<0}, the function~$w$ cannot attain a
non-negative local maximum in~$(0,L)$, implying that
\[
u(x)\le w(x)\le w^+(x)\le\max\{w^+(0),w^+(L)\}
	\le\max\{u^+(0)+\epsilon,u^+(L)+\epsilon e^{\mu L}\}
\]
for~$0<x<L$.  Since this inequality holds for any~$\epsilon>0$, the result
follows.
\end{proof}

\begin{theorem}\label{thm: max min 1d}
Assume that $u$ is continuous on the closed interval~$[0,L]$ and is $C^2$ on
the open interval~$(0,L)$. If $c\ge0$ and $\mathcal{L}u=f$ on~$(0,L)$, then
\[
\max_{[0,L]}u\le\max\{u^+(0),u^+(L)\}+\cosh(\mu L/2)\max_{[0,L]}f^+
\]
and
\[
\min_{[0,L]}u\ge\min\{u^-(0),u^-(L)\}+\cosh(\mu L/2)\min_{[0,L]}f^-,
\]
where $\mu=\max\bigl\{1,(1+\|b\|_\infty)/a_{\min}\bigr\}$.
\end{theorem}
\begin{proof}
Let
\[
w(x)=\max\{u^+(0),u^+(L)\}+v(x)\,\max_{[0,L]}f^+
\quad\text{where}\quad
v(x)=\cosh(\mu L/2)-\cosh\mu(x-L/2),
\]
and observe that $v\ge0$ on~$[0,L]$ with
\begin{align*}
(\mathcal{L}v)(x)&=a(x)\mu^2\cosh\mu(x-L/2)
	-b(x)\mu\sinh\mu(x-L/2)+c(x)v(x)\\
	&\ge\bigl(a_{\min}\mu^2-\|b\|_\infty\mu\bigr)\cosh\mu(x-L/2)
	\ge(a_{\min}\mu-\|b\|_\infty)\mu\ge1
\end{align*}
for $0<x<L$, so
\[
\mathcal{L}(u-w)=f-c(x)\max\{u^+(0),u^+(L)\}-(\mathcal{L}v)\max_{[0,L]}f^+
    \le f-\max_{[0,L]}f^+\le0\quad\text{on $(0,L)$.}
\]
By Theorem~\ref{thm: max principle 1d},
\[
u(x)-w(x)\le\max\{(u-w)^+(0),(u-w)^+(L)\}\quad\text{for $0<x<L$,}
\]
and since $v(0)=0=v(L)$ we see that
\[
(u-w)(0)=u(0)-\max\{u^+(0),u^+(L)\}\le0
\]
and
\[
(u-w)(L)=u(L)-\max\{u^+(0),u^+(L)\}\le0.
\]
Thus, $u-w\le0$ on~$(0,L)$, and therefore
\[
\max_{[0,L]}u\le\max_{[0,L]}w(x)=w(L/2)=\max\{u^+(0),u^+(L)\}
	+v(L/2)\max_{[0,L]}f^+,
\]
proving the first inequality.
The second follows because $\mathcal{L}(-u)=-f$, $(-u)^+=-u^-$~and
$(-f)^+=-f^-$.
\end{proof}

Now consider the two-point boundary-value problem~\eqref{eq: two-point bvp} in
the case when $\alpha_0=0=\alpha_L$ and $\beta_0=1=\beta_L$:
\begin{equation}\label{eq: Lu=f Dirichlet}
\mathcal{L}u=f\quad\text{on $(0,L)$,}
	\quad\text{with $u(0)=\gamma_0$ and $u(L)=\gamma_L$.}
\end{equation}
As an immediate consequence of Theorem~\ref{thm: max min 1d} we have the
following \emph{a priori} estimate, which serves to bound the solution~$u$ in
terms of the data $f$, $\gamma_0$~and $\gamma_L$.

\begin{theorem}\label{thm: Lu=f apriori infty}
If $c\ge0$ on~$(0,L)$, then any solution~$u$ of~\eqref{eq: Lu=f Dirichlet}
satisfies
\[
\|u\|_\infty\le\max\{|\gamma_0|,|\gamma_L|\}+\cosh(\mu L/2)\|f\|_\infty.
\]
\end{theorem}

Using Theorem~\ref{thm: Lu=f apriori infty} it can be shown that
the two-point boundary-value problem~\eqref{eq: Lu=f Dirichlet} is
\emph{well-posed}, that is,
\begin{enumerate}
\item a unique solution exists for each choice of the data $f$, $\gamma_0$~and
$\gamma_L$, and
\item small changes in the data lead to only small changes in the solution.
\end{enumerate}
To explain the second part, suppose that we perturb the data to $\tilde f$,
$\tilde\gamma_0$ and $\tilde\gamma_L$, and let $\tilde u$ denote the solution
of the resulting perturbed problem,
\[
\mathcal{L}\tilde u=\tilde f\quad\text{on $(0,L)$,}
    \quad\text{with $\tilde u(0)=\tilde\gamma_0$
and $\tilde u(L)=\tilde\gamma_L$.}
\]
If we denote the changes in the solution and the data by
\[
\delta u=\tilde u-u,\quad\delta f=\tilde f-f,\quad
\delta\gamma_0=\tilde\gamma_0-\gamma_0,\quad
\delta\gamma_L=\tilde\gamma_L-\gamma_L,
\]
then we need to show that $\delta u$ is small whenever $\delta f$,
$\delta\gamma_0$~and $\delta\gamma_L$ are all small.

\begin{theorem}
If $c\ge0$ on~$(0,L)$, then \eqref{eq: Lu=f Dirichlet} has a unique
solution~$u$.  This solution is continuous on~$[0,L]$ and is $C^2$ on~$(0,L)$.
Moreover, when the problem is perturbed as described above,
\[
\|\delta u\|_\infty\le\max\{|\delta\gamma_0|,|\delta\gamma_L|\}
    +\cosh(\mu L/2)\|\delta f\|_\infty.
\]
\end{theorem}
\begin{proof}
We will not prove the hard part, namely existence.  Since the $\mathcal{L}$ is
linear, the proof of uniqueness follows easily from
Theorem~\ref{thm: Lu=f apriori infty}.  In fact, suppose that $u_1$~and $u_2$
are solutions, that is,
\[
\mathcal{L}u_1=f=\mathcal{L}u_2\quad\text{on $(0,L)$,}
    \quad\text{with $u_1(0)=\gamma_0=u_2(0)$ and $u_1(L)=\gamma_L=u_2(L)$.}
\]
The difference $v=u_1-u_2$ satisfies
\[
\mathcal{L}v=\mathcal{L}(u_1-u_2)=\mathcal{L}u_1-\mathcal{L}u_2=f-f=0
    \quad\text{on $(0,L)$},
\]
with $v(0)=u_1(0)-u_2(0)=\gamma_0-\gamma_0=0$ and
$v(L)=u_1(L)-u_2(L)=\gamma_L-\gamma_L=0$, so $\|v\|_\infty\le0$ by
Theorem~\ref{thm: Lu=f apriori infty}, which means that $u_1=u_2$ on~$[0,L]$.

Similarly, since $\mathcal{L}$ is linear, we find that
\[
\mathcal{L}\delta u=\delta f\quad\text{on $(0,L)$,}
    \quad\text{with $\delta u(0)=\delta\gamma_0$
and $\delta u(L)=\delta\gamma_L$,}
\]
and Theorem~\ref{thm: Lu=f apriori infty} implies the desired estimate
for~$\|\delta u\|_\infty$.
\end{proof}

\section{Discrete maximum principle}

We will now establish a maximum principle for our finite difference
approximation to the two-point boundary-value problem~\eqref{eq: two-point bvp}
in the case
\begin{equation}\label{eq: discrete max assumptions}
b(x)=0,\qquad\alpha_0=0,\quad\beta_0=1,\quad\alpha_L=0,\quad\beta_L=1.
\end{equation}
Thus,
\begin{equation}\label{eq: simple Lu=f}
(\mathcal{L}u)(x)=-a(x)u''(x)+c(x)u(x)=f(x)\quad\text{for~$0<x<L$,}\quad
\text{with $u(0)=\gamma_0$~and $u(L)=\gamma_L$.}
\end{equation}
Likewise,
\[
(\mathcal{L}_{\Delta x}U)_p=-a_p\,\frac{U_{p+1}-2U_p+U_{p-1}}{\Delta x^2}
	+c_pU_p\quad\text{for $1\le p\le P-1$,}
\]
and our finite difference method is (case~1 in
section~\ref{sec: gen two-point bvp})
\begin{equation}\label{eq: finite diff Dirichlet 1d}
(\mathcal{L}_{\Delta x}U)_p=f_p\quad\text{for $1\le p\le P-1$,}\quad
	\text{with $U_0=\gamma_0$ and $U_P=\gamma_L$.}
\end{equation}
The following discrete analogue of Theorem~\ref{lem: Lu<0} holds.

\begin{lemma}\label{lem: discrete LU<0}
Assume \eqref{eq: discrete max assumptions}.
If $c_p\ge0$ and $(\mathcal{L}_{\Delta x}U)_p<0$ for~$1\le p\le P-1$, then
there is no $p^*$ in the range $1\le p^*\le P-1$ for which
\[
U_{p^*}\ge 0,\qquad U_{p^*-1}\le U_{p^*}\qquad\text{and}\qquad
U_{p^*+1}\le U_{p^*}.
\]
\end{lemma}
\begin{proof}
If such a $p^*$ exists, then $U_{p^*+1}+U_{p^*-1}\le 2U_{p^*}$ so
\[
(\mathcal{L}_{\Delta x}U)_{p^*}
	=a_{p^*}\,\frac{-U_{p^*+1}+2U_{p^*}-U_{p^*-1}}{\Delta x^2}
	+c_{p^*}U_{p^*}\ge 0,
\]
contradicting the second hypothesis of the lemma.
\end{proof}

A discrete analogue of Theorem~\ref{thm: max principle 1d} then follows.

\begin{theorem}\label{thm: discrete max principle}
Assume \eqref{eq: discrete max assumptions}.
If $c_p\ge0$ and $(\mathcal{L}_{\Delta x}U)_p\le0$ for $1\le p\le P-1$, then
\[
U_p\le\max\{U_0^+,U_P^+\}\quad\text{for $1\le p\le P-1$.}
\]
\end{theorem}
\begin{proof}
Let $\epsilon>0$~and $\mu>0$, and put $W_p=U_p+\epsilon e^{\mu x_p}$.
We see from~\eqref{eq: second diff remainder} that the
function~$g(x)=e^{\mu x}$ satisfies
\[
\frac{g(x_{p+1})-2g(x_p)+g(x_{p-1})}{\Delta x^2}-g''(x_p)
	=\frac{1}{\Delta x^2}\bigl[(R_3g)(x_p,\Delta x)+(R_3g)(x_p,-\Delta x)\bigr],
\]
where
\begin{align*}
(R_3g)(x_p,\Delta x)
	&=\frac{1}{4!}\int_{x_p}^{x_{p+1}}(x_{p+1}-y)^3g^{(4)}(y)\,dy,\\
(R_3g)(x_p,-\Delta x)
	&=\frac{(-1)^4}{4!}\int_{x_{p-1}}^{x_p}(y-x_{p-1})^3g^{(4)}(y)\,dy.
\end{align*}
Since $g^{(4)}(y)=\mu^4e^{\mu y}\ge0$ for all~$y$, it follows that
$(R_3g)(x_p,\pm\Delta x)\ge0$ and thus
\[
\frac{g(x_{p+1})-2g(x_p)+g(x_{p-1})}{\Delta x^2}\ge g''(x_p)=\mu^2e^{\mu x_p}.
\]
Hence,
\begin{align*}
(\mathcal{L}_{\Delta x}W)_p&=(\mathcal{L}_{\Delta x}U)_p
	+\epsilon\biggl(-a_p\,\frac{g(x_{p+1})-2g(x_p)+g(x_{p-1})}{\Delta x^2}
	+c_pg(x_p)\biggr)\\
	&\le (\mathcal{L}_{\Delta x}U)_p
		+\epsilon\bigl(-a_p\mu^2e^{\mu x_p}+c_pe^{\mu x_p}\bigr)
	\le0-\epsilon(a_{\min}\mu^2-\|c\|_\infty)e^{\mu x_p},
\end{align*}
and by choosing $\mu^2>\|c\|_\infty/a_{\min}$ we can ensure that
$(\mathcal{L}_{\Delta x}U)_p<0$ for~$1\le p\le P-1$.  By
Lemma~\ref{lem: discrete LU<0}, we conclude that
\[
U_p\le W_p\le W_p^+\le\max\{W_0^+,W_P^+\}
	=\max\{U_0^++\epsilon,U_P^++\epsilon e^{\mu L}\}
\]
for $1\le p\le P-1$.  Since this inequality holds for any $\epsilon>0$, the
result follows.
\end{proof}

Next is a discrete version of Theorem~\ref{thm: max min 1d}.

\begin{theorem}
Assume \eqref{eq: discrete max assumptions}.
If $c_p\ge0$ and $(\mathcal{L}_{\Delta x}U)_p=f_p$ for $1\le p\le L$, then
\[
\max_{0\le p\le P}U_p\le\max\{U^+_0,U^+_P\}+\frac{L^2}{8a_{\min}}
	\max_{1\le q\le P-1}f_q^+
\]
and
\[
\min_{0\le p\le P}U_p\ge\min\{U^+_0,U^+_P\}+\frac{L^2}{8a_{\min}}
	\max_{1\le q\le P-1}f_q^-
\]
\end{theorem}
\begin{proof}
Let
\[
W_p=\max\{U^+_0,U^+_P\}+V_p\max_{1\le q\le P-1}f_q^+
\quad\text{where}\quad
V_p=\frac{x_p(L-x_p)}{2a_{\min}}.
\]
Since the second-order central difference formula is exact for a quadratic
polynomial,
\[
\frac{V_{p+1}-2V_p+V_{p-1}}{\Delta x^2}=\frac{-1}{a_{\min}}
\]
and thus, noting that $V_p\ge0$, we conclude that
\[
(\mathcal{L}_{\Delta x}V)_p=\frac{a_p}{a_{\min}}+c_pV_p\ge1
	\quad\text{for $1\le p\le P-1$.}
\]
It follows that
\[
(\mathcal{L}_{\Delta x}W)_p=\max\{U^+_0,U^+_P\}(\mathcal{L}_{\Delta x}1)_p
	+\Bigl(\max_{1\le q\le P-1}f_q^+\Bigr)(\mathcal{L}_{\Delta x}V)_p
	\ge\max_{1\le q\le P-1}f_q^+
\]
and so
\[
\bigl(\mathcal{L}_{\Delta x}(U-W)\bigr)_p=f_p-(\mathcal{L}_{\Delta x}W)_p
	\le f_p-\max_{1\le q\le P-1}f_q^+\le0\quad\text{for $1\le p\le P-1$,}
\]
with
\[
(U-W)_0=U_0-\max{U_0^+,U_P^+}\le0
\quad\text{and}\quad
(U-W)_P=U_P-\max{U_0^+,U_P^+}\le0.
\]
By Theorem~\ref{thm: discrete max principle},
\[
U_p-W_p\le\max\{(U-W)_0^+,(U-W)_P^+\}\le0\quad\text{for $1\le p\le P-1$,}
\]
and the first inequality follows because
\[
U_p\le W_p\le\max\{U^+_0,U^+_P\}
	+\biggl(\max_{0\le x\le L}\frac{x(L-x)}{2a_{\min}}\biggr)
	\biggl(\max_{1\le q\le P-1}f_q^+\biggr).
\]
The second follows because $\bigl(\mathcal{L}_{\Delta x}(-U)\bigr)_p=-f_p$,
$(-U)_p^+=-U_p^-$~and $(-f)_p^+=-f_p^-$.
\end{proof}

Our final result for this section is a discrete version of
Theorem~\ref{thm: Lu=f apriori infty}.

\begin{theorem}\label{thm: discrete apriori 1D}
Assume \eqref{eq: discrete max assumptions}. If $c\ge0$ on~$(0,L)$, then the
finite difference equations~\eqref{eq: finite diff Dirichlet 1d} have a unique
solution~$U_p$, and
\[
\max_{0\le p\le P}|U_p|\le\max\{|\gamma_0|,|\gamma_L|\}
    +\frac{L^2}{8a_{\min}}\max_{1\le q\le P-1}|f_q|.
\]
\end{theorem}
\begin{proof}
The \emph{a priori} estimate for~$U_p$ follows at once from
Theorem~\ref{thm: max min 1d}.  To prove existence and uniqueness, we write the
finite difference equations in matrix form, as
in~\eqref{eq: finite diff matrix 1d} except that
now $\boldsymbol{B}=\boldsymbol{0}$ so
\[
(\boldsymbol{A}+\boldsymbol{C})\boldsymbol{U}=\boldsymbol{f}+\boldsymbol{g}.
\]
The \emph{a priori} estimate shows that if $f_p=0$ for~$1\le p\le P-1$ and
$\gamma_0=0=\gamma_L$, then $U_p=0$ for~$0\le p\le P$.  In other words if
$\boldsymbol{f}=\boldsymbol{0}=\boldsymbol{g}$, then
$\boldsymbol{U}=\boldsymbol{0}$.  Thus, the homogeneous linear system admits
only the trivial solution, which implies that the
matrix~$\boldsymbol{A}+\boldsymbol{C}$ is non-singular and so the linear system
is uniquely solvable for any $f_p$, $\gamma_0$~and $\gamma_L$.
\end{proof}

\section{An error bound}

The finite difference method defined in section~\ref{sec: gen two-point bvp}
is said to be \emph{stable} if a unique solution~$U_p$ exists for any choice of
the data $f_p$, $\gamma_0$~and $\gamma_L$, and if there is a constant~$C$ ---
independent of $f_p$, $\gamma_0$, $\gamma_L$~and $\Delta x$ --- such that
\[
\max_{0\le p\le P}|U_p|\le C\Bigl(|\gamma_0|+|\gamma_L|
    +\max_{1\le p\le P-1} |f_p|\Bigr).
\]
For example, by Theorem~\ref{thm: discrete apriori 1D}, the
conditions~\eqref{eq: discrete max assumptions} are sufficient to ensure
stability.

Suppose $\alpha_0=0=\alpha_L$ (that is, case~1). We define the
\emph{local truncation error}~$\tau_p$ by
\[
\tau_p=f_p-(\mathcal{L}_{\Delta x}u)_p
    =(\mathcal{L}u)_p-(\mathcal{L}_{\Delta x}u)_p,
\]
and note that
\begin{multline*}
\tau_p=-a_p\biggl(
    u''(x_p)-\frac{u(x_p+\Delta x)-2u(x_p)+u(x_p-\Delta x)}{\Delta x^2}\biggr)\\
+b_p\biggl(u'(x_p)-\frac{u(x_p+\Delta x)-u(x_p-\Delta x)}{2\,\Delta x}\biggr)
\end{multline*}
so, by Theorems \ref{thm: 2nd central diff}~and \ref{thm: first central diff},
\begin{equation}\label{eq: tau_p bound}
|\tau_p|\le\biggl(\frac{|a_p|}{12}\,\|u^{(4)}\|_\infty
    +\frac{|b_p|}{6}\,\|u^{(3)}\|_\infty\biggr)\,\Delta x^2.
\end{equation}
Since $(\mathcal{L}_{\Delta x}U)_p=f_p=(\mathcal{L}u)_p$ and since the
finite difference operator~$\mathcal{L}_{\Delta x}$ is linear, it follows that
the \emph{solution error},
\[
E_p=U_p-u(x_p)
\]
satisfies
\[
(\mathcal{L}_{\Delta x}E)_p=(\mathcal{L}_{\Delta x}U)_p
    -(\mathcal{L}_{\Delta x}u)_p=\tau_p\quad\text{for $1\le p\le P-1$.}
\]
Moreover, since $U_0=\gamma_0=u(x_0)$~and $U_P=\gamma_L=u(x_P)$ we have
$E_0=0=E_P$.  Therefore, the stability property applies with $U_p$, $f_p$,
$\gamma_0$~and $\gamma_L$ replaced by $E_p$, $\tau_p$, $0$~and $0$,
respectively, and so
\[
\max_{0\le p\le P}|E_p|\le C\max_{1\le p\le P-1}|\tau_p|.
\]
Combining this estimate with~\eqref{eq: tau_p bound} we obtain the error bound
\[
|U_p-u(x_p)|\le C \biggl(\frac{\|a\|_\infty}{12}\,\|u^{(4)}\|_\infty
    +\frac{\|b\|_\infty}{6}\,\|u^{(3)}\|_\infty\biggr)\,\Delta x^2
    \quad\text{for $0\le p\le P$.}
\]
In other words,
\[
U_p=u(x_p)+O(\Delta x^2)\quad\text{as $\Delta x\to0$, for $0\le p\le P$,}
\]
showing that the finite difference method is \emph{second-order accurate}.

It turns out that for many numerical methods, there is an exponent~$r>0$ and a
continuous function~$v(x)$ such that
\begin{equation}\label{eq: Ep asymp}
E_p=U_p-u(x_p)=v(x_p)\,\Delta x^r+O(\Delta x^{r+\epsilon})
\end{equation}
for some~$\epsilon>0$.  Therefore, if we let
\[
\mathcal{E}(\Delta x)=\max_{0\le p\le P}|E_p|
\]
then
\[
\mathcal{E}(\Delta x)\approx\Bigl(\max_{0\le x\le L}|v(x)|\Bigr)\,\Delta x^2.
\]
It follows that we can estimate the value of~$r$ by computing the ratio
\[
\frac{\mathcal{E}(2\,\Delta x)}{\mathcal{E}(\Delta x)}\approx 2^r
\]
and taking logarithms (to base~2):
\[
r\approx\log_2\frac{\mathcal{E}(2\,\Delta x)}{\mathcal{E}(\Delta x)}.
\]
More precisely, the logarithm on the right will converge to~$r$
as~$\Delta x\to0$.

\begin{table}
\caption{Convergence behaviour of the finite difference solution from
\cref{example: bvp1d conv}}\label{table: bvp1d conv}
\begin{center}
\renewcommand{\arraystretch}{1.2}
\begin{tabular}{r|cc}
\multicolumn{1}{c|}{$P$}&max error&rate\\
\hline
   8&   5.28e-03&\\
  16&   1.33e-03&   1.986\\
  32&   3.34e-04&   1.998\\
  64&   8.35e-05&   1.999\\
 128&   2.09e-05&   2.000
\end{tabular}
\end{center}
\end{table}

\begin{example}\label{example: bvp1d conv}
Let $e_P=\max_{0\le p\le P}|U_p-u(x_p)|$ denote the maximum error in the finite
difference solution for a grid with $P$~subintervals.  The argument above shows
that if the error behaves as in~\eqref{eq: Ep asymp} then
\begin{equation}\label{eq: eP asymp}
r\approx\log_2\frac{E_{P/2}}{E_P}.
\end{equation}
\Cref{table: bvp1d conv} shows the values of the maximum error~$e_P$ and
the estimated convergence rates according to~\eqref{eq: eP asymp} as we
repeatedly double~$P$, using the problem from \cref{example: bvp1d example}.
These results confirm that the finite difference scheme is second-order
accurate, that is, $r=2$.
\end{example}

\begin{Exercises}

\exercise\label{ex: u const f}
Verify that \eqref{eq: u const f} satisfies \eqref{eq: model 1d} if $f(x)=c$.

\exercise\label{ex: variation of params}
By following the steps below, use variation of parameters to  verify that
\eqref{eq: model 1d exact soln} solves \eqref{eq: model 1d} for a
general~$f(x)$.  Let
\[
u_1(x)=L-x\quad\text{and}\quad u_2(x)=x
\]
and write
\[
u(x)=v_1(x)u_1(x)+v_2(x)u_2(x).
\]
\begin{description}
\item{(i)} Verify that $u_1$~and $u_2$ are solutions of the homogeneous
equation~$u''=0$, and find their Wronskian
\[
W=\begin{vmatrix}u_1&u_2\\ u_1'&u_2' \end{vmatrix}.
\]
\item{(ii)} Hence, noting that $u''=-f(x)$, determine
\[
v_1'(x)=-\frac{u_2(x)[-f(x)]}{W(x)}=\frac{u_2(x)f(x)}{W(x)}
\quad\text{and}\quad
v_2'(x)=\frac{u_1(x)[-f(x)]}{W(x)}=-\frac{u_1(x)f(x)}{W(x)}.
\]
\item{(iii)}
Find $v_1$~and $v_2$, using the boundary conditions $u(0)=\gamma_0$~and
$u(L)=\gamma_L$ to determine the constants of integration.
\item{(iv)} Deduce from \eqref{eq: model 1d exact soln} that if $f(x)\ge0$
for~$0\le x\le L$, then the graph of~$u(x)$ is always above the straight line
joining $(0,\gamma_0)$~and $(L,\gamma_L)$.  What if $f(x)\le0$
for~$0\le x\le L$?
\end{description}
\begin{ans}
(i) $W(x)=L$\quad (ii) $v_1'=\dfrac{xf(x)}{L}$,
$v_2'=\dfrac{(L-x)f(x)}{L}$\quad (iii) We have
\[
v_1(x)=A+\int_0^x\frac{yf(y)}{L}\,dy
\quad\text{and}\quad
v_2(x)=B+\int_x^L\frac{(L-y)f(y)}{L}\,dy,
\]
and the boundary conditions imply that $A=\gamma_0$ and $B=\gamma_L$.
\end{ans}

\exercise
Use Taylor expansion to find the coefficient~$c$ such that, as~$x\to0$,
\begin{description}
\item{(i)}
$\dfrac{x}{1-x^2}-\sin x=cx^3+O(x^5)$.
\item{(ii)}
$\log\cos x=cx^2+O(x^4)$.
\item{(iii)}
$\dfrac{x-\sinh x}{x^3}=c+O(x^2)$.
\end{description}
\begin{ans}
(i) $7/6$\quad (ii) $-1/2$\quad (iii) $-1/6$
\end{ans}

\exercise
Use Taylor expansion to find the coefficient~$c$ such that
\begin{description}
\item{(i)} $\dfrac{3u(x)-4u(x-h)+u(x-2h)}{2h}
=u'(x)+cu'''(x)\,h^2+O(h^3)$.
\item{(ii)} $\dfrac{-u(x+2h)+4u(x+h)-3u(x)}{2h}
=u'(x)+cu'''(x)\,h^2+O(h^3)$.
\end{description}
\begin{ans}
(i) $c=-1/3$ \quad(ii) $c=-1/3$
\end{ans}

\begin{table}
\caption{Errors in the approximations of $f'(a)$ from
Exercise~\ref{ex: complex deriv}.}
\label{tab: complex deriv}
\begin{center}
\renewcommand{\arraystretch}{1.2}
\ttfamily
\begin{tabular}{lrr}
\multicolumn{1}{c}{$h$}&
\multicolumn{1}{c}{$\epsilon_1(h)$}&
\multicolumn{1}{c}{$\epsilon_2(h)$}\\
\hline
$10^{-2}$ &-9.00e-06&9.01e-06\\
$10^{-4}$ &-9.02e-10&9.01e-10\\
$10^{-6}$ & 8.52e-11&9.02e-14\\
$10^{-8}$ &-4.02e-09&0.00e+00\\
$10^{-10}$& 1.22e-06&0.00e+00
\end{tabular}
\end{center}
\end{table}

\exercise\label{ex: complex deriv}
Let $f$ be complex analytic in a neighbourhood of a point~$a$ on the real axis,
and assume that $f(x)$ is real when $x$ is real.
\begin{description}
\item{(i)} Show that $\Im f(a+ih)/h=f'(a)+O(h^2)$ if $h\to0$ and $h$ is real.
\item{(ii)} \cref{tab: complex deriv} shows the values of
\[
\epsilon_1(h)=\frac{f(a+h)-f(a-h)}{2h}-f'(a)
\quad\text{and}\quad
\epsilon_2(h)=\frac{\Im f(a+ih)}{h}-f'(a),
\]
when $f(x)=x^2+\sin x$ and $a=1$, for different choices of~$h$, with all
computations performed in standard 64~bit floating-point arithmetic.  Explain
the different behavior of $\epsilon_1(h)$ and $\epsilon_2(h)$.
\end{description}

\exercise
Consider the boundary value problem
\[
\begin{aligned}
-u''+2u'-u&=1&&\text{for $0<x<2$,}\\
u&=1&&\text{at $x=0$,}\\
u&=-1&&\text{at $x=2$.}
\end{aligned}
\]
\begin{description}
\item{(i)}
Set up a finite difference approximation as explained in lectures
with $P=4$ (so $\Delta x=1/2$).
\item{(ii)}
Eliminate the variables $U_0$ and $U_4$ to obtain a $3\times3$ linear
system.
\item{(iii)}
Solve the linear system to find $U_1$, $U_2$~and $U_3$.  (You can use
for favourite software.)
\item{(iv)}
Find the exact solution~$u(x)$, and hence compute the errors~$E_p=U_p-u(x_p)$
in your numerical solution.
\end{description}
\begin{ans}
(i) $-6U_{p-1}+7U_p-2U_{p-1}=1$ for $1\le p\le3$\quad
(ii) Since $U_0=1$ and $U_4=-1$,
\[
\begin{bmatrix}7&-2& 0\\ -6& 7&-2\\  0&-6& 7\end{bmatrix}
\begin{bmatrix}U_1\\ U_2\\ U_3\end{bmatrix}
=\begin{bmatrix}7\\ 1\\ -1\end{bmatrix}
\]
(iii) $U_1=269/175=1.5371$, $U_2=47/25=1.88$,
$U_3=257/175=1.4686$\\
(iv) $u=(2-x)e^x-1$ so the errors $E_p=U_p-u(x_p)$ are
$E_1=0.06406$, $E_2=-0.16172$, $E_3=0.22773$.
\end{ans}

\exercise
Consider the two-point boundary-value problem
\[
-u''=f(x)\quad\text{for $0<x<L$,}
	\quad\text{with $-u'(0)=\gamma_0$ and $u'(L)=\gamma_L$.}
\]
\begin{description}
\item{(i)} Show that if $u(x)$ is a solution then so is $u(x)+C$ for any
constant~$C$.
\item{(ii)} Show that if $u(x)$ exists, then
$\gamma_0+\gamma_L+\int_0^Lf(x)\,dx=0$.
\item{(iii)} Use central difference approximations of the form
\eqref{eq: u'' approx}~and \eqref{eq: u' approx} to derive a $(P+1)\times(P+1)$
linear system
$\boldsymbol{A}\boldsymbol{U}=\boldsymbol{f}+\boldsymbol{g}$ that yields
$U_p\approx u(x_p)$ for~$0\le p\le P$.  When $P=5$ you should obtain
\[
\frac{1}{\Delta x^2}\begin{bmatrix}
 1&-1&  &  &  &\\
-1& 2&-1&  &  &\\
  &-1& 2&-1&  &\\
  &  &-1& 2&-1&\\
  &  &  &-1& 2&-1\\
  &  &  &  &-1& 1
\end{bmatrix}
\begin{bmatrix}U_0\\ U_1\\ U_2\\ U_3\\ U_4\\ U_5\end{bmatrix}
=\begin{bmatrix}\tfrac12f_0\\ f_1\\ f_2\\ f_3\\ f_4\\ \tfrac12 f_5
\end{bmatrix}
+\frac{1}{\Delta x}
\begin{bmatrix}\gamma_0\\ 0\\ 0\\ 0\\ 0\\ \gamma_L\end{bmatrix}.
\]
Hint: applying central differences at $x_0$~and $x_P$ requires the use of
``ghost'' grid points $x_{-1}$~and $x_{P+1}$ lying outside the interval~$[0,L]$.
However, the boundary conditions allow you to eliminate $U_{-1}$~and $U_{P+1}$.
\item{(iv)} Show that if $\boldsymbol{U}=[U_p]$ is a solution, then so is
$[U_p+C]$.
\item{(v)} Show that if a solution~$\boldsymbol{U}$ exists, then
\[
\gamma_0+\gamma_L+\bigl(\tfrac12f_0+f_1+f_2+\cdots+f_{P-1}+\tfrac12f_P\bigr)
\Delta x=0.
\]
\item{(vi)} If we interpret the ODE as a steady-state heat equation, what is
the physical meaning of the condition in~(ii), and of its discrete analogue
in~(v)?
\end{description}

\exercise
How can we use the $LDL^\top$ factorization of an $n\times n$, symmetric
tridiagonal matrix~$\boldsymbol{A}$ to compute its determinant?
\begin{ans}
$\det\boldsymbol{A}=d_1d_2\cdots d_n$
\end{ans}

\exercise
Find the $LDL^\top$ factorization of the matrix
\[
\boldsymbol{A}=\begin{bmatrix}1&-1&0&0\\ -1&4&6&0\\ 0&6&14&6\\ 0&0&6&22
\end{bmatrix}.
\]
\begin{ans}
$\boldsymbol{L}=\begin{bmatrix}1&0&0&0\\ -1&1&0&0\\ 0&2&1&0\\ 0&0&3&1
\end{bmatrix}$,
$\boldsymbol{D}=\begin{bmatrix}1&&&\\ &3&&\\ &&2&&\\ &&&4\end{bmatrix}$
\end{ans}

\exercise
If $\boldsymbol{A}$ is symmetric tridiagonal matrix, then instead of the
factorization~\eqref{eq: L D LT} we can seek a tridiagonal, upper triangular
matrix~$\boldsymbol{R}$ such that
$\boldsymbol{A}=\boldsymbol{R}^\top\boldsymbol{R}$.  We call $\boldsymbol{R}$
the \emph{Cholesky factor} of~$\boldsymbol{A}$.
\begin{description}
\item{(i)} Show that if a matrix~$\boldsymbol{A}$ has a Cholesky factorization,
then $\boldsymbol{A}$ is necessarily symmetric and positive semidefinite.
\item{(ii)} Show that if the Cholesky factor is non-singular, then
$\boldsymbol{A}$ must be \emph{strictly} positive-definite.
\item{(iii)} Consider the $5\times5$ case and denote the entries
of~$\boldsymbol{A}$ as in~\eqref{eq: A symm tridiagonal}.  Determine a sequence
of formulae to compute the entries of
\[
\boldsymbol{R}=\begin{bmatrix}
d_1&u_1&   &   &\\
   &d_2&u_2&   &\\
   &   &d_3&u_3&\\
   &   &   &d_4&u_4\\
   &   &   &   &u_5\end{bmatrix}.
\]
\item{(iv)} Hence formulate an algorithm to compute~$\boldsymbol{R}$ in the
general, $n\times n$~case.
\item{(v)} Formulate an in-place version of the algorithm in part~(iv).
\item{(vi)} How is the Cholesky factorization used to solve a linear
system~$\boldsymbol{A}\boldsymbol{x}=\boldsymbol{b}$?
\item{(vii)} Show that the algorithm is stable (provided $\boldsymbol{A}$ is
positive-definite) in the sense that all entries of~$\boldsymbol{R}$ can be
bounded by entries of~$\boldsymbol{A}$.
\end{description}
\begin{ans}
(i)
\[
\begin{aligned}
d_1&=\sqrt{\alpha_1},&u_1&=\beta_1/d_1,\\
d_2&=\sqrt{\alpha_2-u_1^2},&u_2&=\beta_2/d_2\\
d_3&=\sqrt{\alpha_3-u_2^2},&u_3&=\beta_3/d_3\\
d_4&=\sqrt{\alpha_4-u_3^2},&u_4&=\beta_3/d_3\\
d_5&=\sqrt{\alpha_5-u_4^2}.
\end{aligned}
\]
\end{ans}

\exercise
Consider the ODE in \emph{divergence form}:
\begin{equation}\label{eq: ODE divergence form}
-\frac{d}{dx}\biggl(a(x)\,\frac{du}{dx}\biggr)=f(x)\quad\text{for $0<x<L$.}
\end{equation}
\begin{description}
\item{(i)}
Find $w(x)$ such that
\begin{multline*}
\frac{1}{\Delta x}\biggl(
 a(x+\tfrac12\Delta x)\,\frac{u(x+\Delta x)-u(x)}{\Delta x}
-a(x-\tfrac12\Delta x)\,\frac{u(x)-u(x-\Delta x)}{\Delta x}\biggr)\\
    =\frac{d}{dx}\biggl(a(x)\,\frac{du}{dx}\biggr)+w(x)\Delta x^2+O(\Delta x^4)
    \quad\text{as $\Delta x\to0$.}
\end{multline*}
Hint: for $h=\tfrac12\Delta x$, let
\[
v(x)=a(x)\,\frac{u(x+h)-u(x-h)}{2h}
\]
and use the result
\[
\frac{v(x+h)-v(x-h)}{2h}=v'(x)+\frac{1}{6}\,v'''(x)h^2+O(h^4)
    \quad\text{as $h\to0$.}
\]
\item{(ii)} Hence devise a finite difference scheme with $\Delta x=L/P$ to
solve \eqref{eq: ODE divergence form} subject to the Dirichlet boundary
conditions $u(0)=\gamma_0$~and $u(L)=\gamma_L$.
\item{(iii)} Write out the linear system in matrix form when~$P=5$.
\end{description}
\begin{ans}
(i) $w(x)=\bigl[(au')'''(x)+(au''')'(x)\bigr]/24$\quad (ii) Putting
$a_{p\pm1/2}=a(x_p\pm\tfrac12\Delta x)$ we have
\[
-\frac{1}{\Delta x}\biggl(a_{p+1/2}\,\frac{U_{p+1}-U_p}{\Delta x}
    -a_{p-1/2}\,\frac{U_p-U_{p-1}}{\Delta x}\biggr)=f_p
    \quad\text{for $1\le p\le P-1$,}
\]
with $U_0=\gamma_0$~and $U_P=\gamma_L$.\quad (iii)
\begin{multline*}
\frac{1}{\Delta x^2}\begin{bmatrix}
            (a_{1/2}+a_{3/2})&-a_{3/2}&&\\
   -a_{3/2}&(a_{3/2}+a_{5/2})&-a_{5/2}&\\
  &-a_{5/2}&(a_{5/2}+a_{7/2})&-a_{7/2}\\
 &&-a_{7/2}&(a_{7/2}+a_{9/2})
\end{bmatrix}
\begin{bmatrix}U_1\\ U_2\\ U_3\\ U_4\end{bmatrix}\\
=\begin{bmatrix}f_1\\ f_2\\ f_3\\ f_4 \end{bmatrix}+\frac{1}{\Delta x^2}
\begin{bmatrix}a_{1/2}\gamma_0\\ \\ \\ a_{9/2}\gamma_L \end{bmatrix}.
\end{multline*}
\end{ans}

\end{Exercises}


\section{Verifying a random number generator}
\Label{sec:randomnumber}

We describe in this section \specref{randomnumber} which implements
a simple random-number generator.
As in the case of \specref{shuffle} itself, we do not formulate precise
properties of randomness and only require its result to
be in the specified range \inl{[0..n-1]}.
Again, the \inl{assigns} clause to the array \inl{state} models the
dependency on an additional state.

Note that in the following listing, we also provide the rather simple
specification of the function \randominit that is called to initialize the
state of the random generator.

\input{Listings/random_number.h.tex}

The implementations of \randomnumber and \randominit are shown in the following listing.
Internally, we rely on a custom implementation of the POSIX.1
random number generator \inl{lrand48()}\footnote{
  See \url{http://pubs.opengroup.org/onlinepubs/9699919799/functions/lrand48.html}
}
This random number generator is a linear congruence
generator with a 48~bit state and the iteration procedure
\begin{equation}
\label{eq:random}
x_{n+1}=ax_n+c\bmod 2^{48}
\end{equation}
%
where $a=25214903917$ and $c=11$ are relatively prime integers.

As a part of the iteration procedure in Equation~\eqref{eq:random}
an unsigned overflow may occur.
This does not affect the result as we are only interested in its lowest 48 bits.
However, as one of the options we use, \inl{-warn-unsigned-overflow},
causes \wpframac  assert the absence of unsigned overflow this algorithm does not verify under
the same options used for the other algorithms.
%
As an exception, we have therefore decided to disable \inl{-warn-unsigned-overflow}
for this function as the unsigned overflow is both benign and
well-defined (cf.\ \cite[\S 6.2.5, 9]{isoc}).

\input{Listings/random_number.c.tex}

Note that we use the custom acsl lemma \logicref{RandomNumberModulo}
from the following listing to support the verification of some assertions.

\input{Listings/C_Bit.acsl.tex}

\clearpage


\chapter{Kutzelnigg-Mukherjee tensor notation}

\minitoc

\begin{ntt}\label{ntt:kutzelnigg-mukherjee-notation}
\thmtitle{Kutzelnigg-Mukherjee tensor notation}
In \textit{Kutzelnigg-Mukherjee (KM) tensor notation}, creation operators are denoted using superscripts, $a^p\equiv a_p\dg$.
Consequently, the one-particle and one-hole density matrices are written as
$
  \g^p_q
\equiv
  \ip{\F|a^pa_q|\F}
$
and
$
  \h_p^q
\equiv
  \ip{\F|a_p a^q|\F}
$,
respectively.
The one- and two-electron integrals are also written with upper and lower indices: lower indices denote spin-orbitals in the bra and upper ones refer to spin-orbitals in the ket.
\begin{align}
  h_p^q
\equiv
  \ip{\y_p|\op{h}|\y_q}
&&
  g_{pq}^{rs}
\equiv
  \ip{pq|rs}
&&
  \ol{g}_{pq}^{rs}
\equiv
  \ip{pq||rs}
\end{align}
Vacuum-normal-ordered excitations are given the compact notation
$
  a^{p_1\cd p_m}_{q_1\cd q_m}
\equiv
  a^{p_1}\cd a^{p_m}a_{q_m}\cd a_{q_1}
$
and $\F$-normal-ordered excitations are written as
$
  \tl{a}^{p_1\cd p_m}_{q_1\cd q_m}
\equiv
  \gno{a^{p_1}\cd a^{p_m}a_{q_m}\cd a_{q_1}}
$.
Using upper and lower indices enables one to employ the \textit{Einstein summation convention}, in which any index that appears twice in a product, once as a lower index and once as an upper one, is implicitly summed over.
As an example, consider the electronic Hamiltonian as expressed in KM notation.
\begin{align}
  H
=
  h_p^q
  a^p_q
+
  \tfr{1}{4}
  \ol{g}_{pq}^{rs}
  a^{pq}_{rs}
=
  E_{\text{ref}}
+
  H_{\text{c}}
&&
  H_{\text{c}}
=
  f_p^q
  \tl{a}^p_q
+
  \tfr{1}{4}
  \ol{g}_{pq}^{rs}
  \tl{a}^{pq}_{rs}
&&
  E_{\text{ref}}
\equiv
  h_p^q
  \g^p_q
+
  \tfr{1}{2}
  \ol{g}_{pr}^{qs}
  \g^p_q
  \g^r_s
&&
  f_p^q
\equiv
  h_p^q
+
  \ol{g}_{pr}^{qs}
  \g_s^r
\end{align}
Here, $E_{\text{ref}}$ is the Hartree-Fock reference energy, $f_p^q$ denotes a matrix element of the Fock operator, and $H_{\text{c}}$ denotes the correlation component of the Hamiltonian.
More generally, if $\op{v}$ is an \textit{$m$-electron operator}, i.e.\ an operator that acts on $m$ electronic coordinates, its second quantized form is expressed in KM notation as
\begin{align}\label{eq:interaction-tensor}
  \left.
  \op{v}\,
  \right|_{\mc{F}(\mc{H})}
=&\
  \tfr{1}{m!}
  v_{p_1\cd p_m}^{q_1\cd q_m}
  a^{p_1\cd p_m}_{q_1\cd q_m}
&
  v_{p_1\cd p_m}^{q_1\cd q_m}
\equiv
  \int
  d(1\cd m)\,
  \y_{p_1}^*(1)\cd \y_{p_m}^*(m)
  \op{v}(1,\ld,m)
  \y_{q_1}(1)\cd \y_{q_m}(m)
&
\\
\intertext{
where $v_{p_1\cd p_m}^{q_1\cd q_m}$ is the \textit{interaction tensor} of $\op{v}$.
Equivalently, $\op{v}$ can also be expressed as
}
\label{eq:antisymmetrized-interaction-tensor}
  \left.
  \op{v}
  \right|_{\mc{F}(\mc{H})}
=&\
  (\tfr{1}{m!})^2\,
  \ol{v}_{p_1\cd p_m}^{q_1\cd q_m}
  a^{p_1\cd p_m}_{q_1\cd q_m}
&
  \ol{v}_{p_1\cd p_m}^{q_1\cd q_m}
\equiv
  \sum_\pi^{\mr{S}_m}
  \e_\pi
  v_{p_1\hphantom{_{\pi()}}\cd p_m}^{q_{\pi(1)}\cd q_{\pi(m)}}
\hspace{3cm}&
\end{align}
where $\ol{v}_{p_1\cd p_m}^{q_1\cd q_m}$ is an \textit{antisymmetrized interaction tensor}.
Ordinary interaction tensors are symmetric under simultaneous permutation of upper and lower indices, which is equivalent to changing integration variables in equation~\ref{eq:interaction-tensor}.
Antisymmetrized interaction tensors allow for independent permutations of upper and lower indices, with a sign factor corresponding to the parity of the permutation.
The same permutational symmetries are shared by the excitation operators
\begin{align}\label{eq:excitation-permutational-symmetries-1}
  \tl{a}^{p_1\cd p_m}_{q_1\cd q_m}
=
  \e_{\pi}
  \tl{a}^{p_{\pi(1)}\cd p_{\pi(m)}}_{q_1\hphantom{_{\pi()}}\cd q_m}
=
  \e_{\pi}
  \tl{a}^{p_1\hphantom{_{\pi()}}\cd p_m}_{q_{\pi(1)}\cd q_{\pi(m)}}
=
  \tl{a}^{p_{\pi(1)}\cd p_{\pi(m)}}_{q_{\pi(1)}\cd q_{\pi(m)}}
&&
  \text{for all $\pi\in\mr{S}_m$}
\end{align}
since creation operators anticommute with each other, as do annihilation operators.
Note also the following rearrangements
\begin{align}\label{eq:excitation-permutational-symmetries-2}
  \tl{a}^{p_1\cd p_m}_{q_1\cd q_m}
=
  \gno{a^{p_1}_{q_1}\cd a^{p_m}_{q_m}}
&&
  \gno{a^{p_1\cd p_m}_{q_1\cd q_m}a^{r_1\cd r_n}_{s_1\cd s_n}}
=
  \gno{a^{p_1}_{q_1}\cd a^{p_m}_{q_m}a^{r_1}_{s_1}\cd a^{r_n}_{s_n}}
=
  \tl{a}^{p_1\cd p_mr_1\cd r_n}_{q_1\cd q_ms_1\cd s_n}
\end{align}
which follow from the fact that the normal-ordering mapping is antisymmetric with respect to its operator string.
\end{ntt}

\begin{ntt}\label{ntt:dot-notation}
\thmtitle{Dot notation for contractions}
To make the notation more flexible, we here augment the traditional KM notation with the following definitions of \emph{particle $\ptcl$ contractions} and \emph{hole $\hole$ contractions}.%\footnote{The dot notation is borrowed from physics: \url{https://en.wikipedia.org/wiki/Wick's_theorem#Definition_of_contraction}}
\begin{align}
&&
  a_{p^\ptcl}a^{q^\ptcl}
\equiv
  \ctr{}{a}{_p}{a} a_pa^q
&&
  a^{q^\ptcl}a_{p^\ptcl}
\equiv
-
  \ctr{}{a}{_p}{a} a_pa^q
&&
  a^{q^\hole}a_{p^\hole}
\equiv
  \ctr{}{a}{^q}{a} a^qa_p
&&
  a_{p^\hole}a^{q^\hole}
\equiv
-
  \ctr{}{a}{^q}{a} a^qa_p
\end{align}
Note that $a^{p^\ptcl}_{q^\ptcl}=-\h^p_q$ and $a^{p^\hole}_{q^\hole}=\g_q^p$.
In $\vac$-normal ordering, the hole contractions vanish and the particle contractions become Kronecker deltas.
For multiply contracted strings, different contractions will be distinguished with repeated dots, $a_{p^{\ptcl\ptcl}}a^{p^{\ptcl\ptcl}}$, or dots with numbers, $a_{p^{\ptcl2}}a^{p^{\ptcl2}}$.
This notation allows for normal ordered strings with contractions to keep all of the permutational symmetries shown in equations~\ref{eq:excitation-permutational-symmetries-1} and~\ref{eq:excitation-permutational-symmetries-2}.
\end{ntt}

\begin{ex}\label{ex:km-notation-wick-expansions}
Using notations~\ref{ntt:kutzelnigg-mukherjee-notation} and~\ref{ntt:dot-notation}, the Wick expansions for vacuum-normal single and double excitations in terms of $\F$-normal ones look as follows.
The signs are determined by pairing up contracted indices using \cref{eq:excitation-permutational-symmetries-1,eq:excitation-permutational-symmetries-2}.
\begin{align*}
  a^p_q
=&\
  \tl{a}^p_q
+
  \tl{a}^{p^\hole}_{q^\hole}
=
  \tl{a}^p_q
+
  \g^p_q
\\
  a^{pq}_{rs}
=&\
  \tl{a}^{pq}_{rs}
+
  \tl{a}^{p^\hole q}_{r^\hole s}
+
  \tl{a}^{p^\hole q}_{r\ s^\hole}
+
  \tl{a}^{p\ q^\hole}_{r^\hole s}
+
  \tl{a}^{p q^\hole}_{r s^\hole}
+
  \tl{a}^{p^\hole q^{\hole\hole}}_{r^\hole s^{\hole\hole}}
+
  \tl{a}^{p^{\hole\hphantom{\hole}} q^{\hole\hole}}_{r^{\hole\hole} s^\hole}
\\=&\
  \tl{a}^{pq}_{rs}
+
  \g^p_r
  \tl{a}^{q}_{s}
-
  \g^p_s
  \tl{a}^{q}_{r}
-
  \g^q_r
  \tl{a}^{p}_{s}
+
  \g^q_s
  \tl{a}^{p}_{r}
+
  \g^p_r
  \g^q_s
-
  \g^p_s
  \g^q_r
\end{align*}
\end{ex}

\newpage
\begin{dfn}\label{dfn:riffle-shuffles}
\thmtitle{Riffle shuffle permutations}
Let $\mr{S}_R$ be the \textit{symmetric group on $R\equiv(r_1,\ld,r_k)$}, comprising all $k!$ permutations of this tuple.
For each integer composition\footnote{See \url{https://en.wikipedia.org/wiki/Composition_(combinatorics)}} $(k_1,\ld,k_m)$ of $k$, the \textit{$(k_1,\ld,k_m)$-shuffles~of~$R$} partition the tuple into blocks $R_1\cup\cd\cup R_m$ of the form $R_i\equiv(r_{h_i+1},\ld,r_{h_i+k_i})$ with $h_i\equiv\sum_{j=1}^{i-1}k_j$ and interleave them in all possible ways.
The cardinality of this subset $\mr{S}_R^{(k_1,\ld,k_m)}\subseteq \mr{S}_R$ is given by the following multinomial coefficient
\begin{align}
  |\mr{S}_R^{(k_1,\ld,k_m)}|
\equiv
  \fr{k!}{k_1!\cd k_m!}
\end{align}
since for each shuffle there are $k_1!\cd k_m!$ permutations in $\mr{S}_R$ derived by permuting the elements within each block.
These are termed \textit{riffle shuffle permutations},\footnote{See \url{https://en.wikipedia.org/wiki/Riffle_shuffle_permutation}} by analogy with the process of cutting and interleaving a deck of cards.
\end{dfn}

\begin{dfn}
\thmtitle{Index antisymmetrizers}
Let $\op{P}_{(r_1/\cd/r_k)}$ be the \textit{full antisymmetrizer} for the indices in $R$.\footnote{Note that we are borrowing some definitions from \cref{dfn:riffle-shuffles}, such as $R$ and $R_i$.}\,\footnote{For example, we can express the position-space Slater determinant $\F_{(p_1\cd p_n)}(1,\ld,n)$ as $\fr{1}{\sqrt{n!}}\op{P}_{(p_1/\cd/p_n)}\y_{p_1}(1)\cd \y_{p_n}(n)$.}
More generally, let $\op{P}_{(R_1/\cd/R_m)}$ be a \textit{reduced antisymmetrizer}, which antisymmetrizes a term in $R$ that it is already antisymmetric in each block, $R_i$.
Reduced antisymmetrization is achieved by summing over riffle shuffles.
\begin{align}
  \op{P}_{(R_1/\cd/R_m)}
\equiv
  \sum_\pi^{\mr{S}_R^{(k_1,\ld,k_m)}}
  \e_\pi
  \op{\pi}
&&
  \op{\pi}\,
  t_{r_1\cd r_k}
\equiv
  t_{\pi(r_1)\cd \pi(r_k)}
\end{align}
To antisymmetrize multiple disjoint sets of indices, we use the compact notation
$
  \op{P}^{(X_1|\cd|X_l)}_{(Y_1|\cd|Y_m)}
\equiv
  \op{P}^{(X_1)}\cd \op{P}^{(X_l)}
  \op{P}_{(Y_1)}\cd \op{P}_{(Y_m)}
$,
where each $X_i$ or $Y_i$ stands for a reduced antisymmetrizer argument $R_1/\cd/R_m$ for a different set of indices.
\end{dfn}

\begin{ex}
Permutation operators $\op{\pi}$ can be written as products of transpositions $(pq)t_{pq}\equiv t_{qp}$.
In this notation,
\begin{align*}
  \op{P}_{(p/q)}
=
  1
-
  (pq)
&&
  \op{P}_{(p/q/r)}
=
  1
-
  (pq)
-
  (pr)
-
  (qr)
+
  (pq)(qr)
+
  (pr)(qr)
&&
  \op{P}_{(p/qr)}
=
  1
-
  (pq)
+
  (pr)(qr)
\end{align*}
are the unique antisymmetrizers for two and three indices.
Note that $\op{P}_{(p/qr)}\op{P}_{(q/r)}=\op{P}_{(p/q/r)}$, which is an immediate consequence of the definition of the reduced antisymmetrizer.
\end{ex}

\begin{ex}
Using index antisymmetrizers, the second Wick expansion in \cref{ex:km-notation-wick-expansions} can be expressed even more compactly.
\begin{align*}
  a^{pq}_{rs}
=&\
  \tl{a}^{pq}_{rs}
+
  \op{P}^{(p/q)}_{(r/s)}
  \tl{a}^{p^\hole q}_{r^\hole s}
+
  \op{P}_{(r/s)}
  \tl{a}^{p^\hole q^{\hole\hole}}_{r^\hole s^{\hole\hole}}
=
  \tl{a}^{pq}_{rs}
+
  \op{P}^{(p/q)}_{(r/s)}
  \g^p_r
  \tl{a}^{q}_{s}
+
  \op{P}_{(r/s)}
  \g^p_r
  \g^q_s
\end{align*}
Note that permuting $p$ and $q$ in the second term, for example, is equivalent to ``moving the contraction dot'' from one to the other, since the excitation operators are antisymmetric in their upper and lower indices:
$
  (pq)
  \tl{a}^{p^\hole q}_{r^\hole s}
=
-
  \tl{a}^{q^\hole p}_{r^\hole s}
=
  \tl{a}^{p\,\, q^\hole}_{r^\hole s}
$.
In general, this allows us to reduce Wick expansions to a sum over unique contraction ``patterns'', with the remaining contractions generated from these by index antisymmetrizers.
The next two examples show how this works in practice.
\end{ex}

\begin{ex}
Reduced antisymmetrizers come into play in the Wick expansion of a triple excitation operator.
\begin{align}
  a^{pqr}_{stu}
=
  \tl{a}^{pqr}_{stu}
+
  \op{P}^{(p/qr)}_{(s/tu)}
  \tl{a}^{p^\hole qr}_{s^\hole tu}
+
  \op{P}^{(p/q/r)}_{(st/u)}
  \tl{a}^{p^\hole q^{\hole\hole} r}_{s^\hole t^{\hole\hole} u}
+
  \op{P}^{(p/q/r)}
  \tl{a}^{p^\hole q^{\hole\hole} r^{\hole\hole\hole}}
        _{s^\hole t^{\hole\hole} u^{\hole\hole\hole}}
\end{align}
The permutation factors are chosen as follows.
In the second term, permutations of $qr$ an $tu$ are omitted because they do not produce unique contractions.
In the third term, we can include permutations of either $pq$ or $st$ but not both.
Otherwise, we would double-count terms like
$
  \tl{a}^{p^\hole q^{\hole\hole} r}_{s^\hole t^{\hole\hole} u}
$
by also including
$
  \tl{a}^{p^{\hole\hole} q^\hole r}_{s^{\hole\hole} t^\hole u}
$,
which represents the same contraction.
The same reasoning applies to the last term, where we can either antisymmetrize $pqr$ or $stu$.
\end{ex}

\begin{ex}
\thmtitle{Derivation of CIS equations in KM notation}
A programmable expression for the CI singles Hamiltonian matrix elements, $\ip{\F_i^a|H_c|\F_j^b}$,  can be derived in KM notation as follows.
\begin{align*}
&
  \ip{\F|\tl{a}_a^i\tl{a}_q^p\tl{a}_j^b|\F}
=
  \gno{
    a_{a^\ptcl}^{i^\hole}
    a_{q^{\ptcl\ptcl}}^{p^\ptcl}
    a_{j^\hole}^{b^{\ptcl\ptcl}}
  }
+
  \gno{
    a_{a^\ptcl}^{i^{\hole}}
    a_{q^{\hole}}^{p^{\hole\hole}}
    a_{j^{\hole\hole}}^{b^{\ptcl}}
  }
=
  \g^i_j\h_a^p\h_q^b
-
  \g_q^i\g_j^p\h_a^b
\\
&
  \ip{\F|\tl{a}_a^i\tl{a}^{pq}_{rs}\tl{a}_j^b|\F}
=
  \op{P}^{(p/q)}_{(r/s)}
  \gno{
    a^{i^\hole}_{a^\ptcl}
    a^{p^\ptcl q^{\hole\hole}}_{r^\hole s^{\ptcl\ptcl}}
    a^{b^{\ptcl\ptcl}}_{j^{\hole\hole}}
  }
=
  \op{P}^{(p/q)}_{(r/s)}
  \g^i_r\h^p_a\g^q_j\h^b_s
\\
&
\implies
  \ip{\F_i^a|H_{\text{c}}|\F_j^b}
=
  f_p^q\pr{
    \g^i_j\h_a^p\h_q^b
  -
    \g_q^i\g_j^p\h_a^b
  }
+
  \tfr{1}{4}
  \ol{g}_{pq}^{rs}\pr{
    \op{P}^{(p/q)}_{(r/s)}
    \g^i_r\h^p_a\g^q_j\h^b_s
  }
=
  f_a^b\g_j^i
-
  f_j^i\h_a^b
+
  \ol{g}^{ib}_{aj}
\end{align*}
which simplifies to
$\ip{\F_i^a|H_{\text{c}}|\F_j^b}=f_a^b\delta_j^i-f_j^i\delta_a^b+\ol{g}^{ib}_{aj}$.
Here, we have used the fact that $\tl{a}_i^a=a_i^a$ and $\tl{a}_a^i=a_a^i$.
\end{ex}

\begin{ex}
\begin{samepage}
\thmtitle{Derivation of CID equations in KM notation}
Projecting the CI doubles Schr\"odinger equation, $H_{\text{c}}\Y=E_{\text{c}}\Y$ where $\Y=(1+\tfr{1}{4}c_{cd}^{kl}\tl{a}_{kl}^{cd})\F$, by $\F$ and $\F_{ij}^{ab}$
gives a system of linear equations
\begin{align*}
  E_{\text{c}}
=&\
  \ip{\F|H_{\text{c}}(1+\tfr{1}{4}c_{cd}^{kl}\tl{a}_{kl}^{cd})|\F}
&&
  \implies
&
  E_{\text{c}}
=&\
  \tfrac{1}{4}
  \ip{\F|H_c|\F_{kl}^{cd}}c_{cd}^{kl}
\\
  E_{\text{c}}c_{ab}^{ij}
=&\
  \ip{\F_{ij}^{ab}|H_{\text{c}}(1+\tfr{1}{4}c_{cd}^{kl}\tl{a}_{kl}^{cd})|\F}
&&
  \implies
&
  E_{\text{c}}c_{ab}^{ij}
=&\
  \ip{\F_{ij}^{ab}|H_{\text{c}}|\F}
+
  \tfrac{1}{4}
  \ip{\F_{ij}^{ab}|H_{\text{c}}|\F_{kl}^{cd}}c_{cd}^{kl}
\end{align*}
which can be simplified into programmable expressions as follows.\\[5pt]
$
\begin{array}{rl}
  \ip{\F|\tl{a}^{pq}_{rs}\tl{a}_{kl}^{cd}|\F}
=&
  \op{P}^{(p/q)}_{(r/s)}
  \g_k^p\g_l^q\h_r^c\h_s^d
\\
  \ip{\F|\tl{a}_{ab}^{ij}\tl{a}_q^p\tl{a}_{kl}^{cd}|\F}
=&
  \op{P}^{(c/d)}
        _{(a/b|k/l)}
  \gno{
    a_{a^{\ptcl1}b^{\ptcl3}}
     ^{i^{\hole1}j^{\hole2}}
    a_{q^{\ptcl2}}
     ^{p^{\ptcl1}}
    a_{k^{\hole1}l^{\hole2}}
     ^{c^{\ptcl2}d^{\ptcl3}}
  }
+
  \op{P}^{(i/j|c/d)}
        _{(k/l)}
  \gno{
    a_{a^{\ptcl1}b^{\ptcl2}}
     ^{i^{\hole1}j^{\hole3}}
    a_{q^{\hole1}}
     ^{p^{\hole2}}
    a_{k^{\hole2}l^{\hole3}}
     ^{c^{\ptcl1}d^{\ptcl2}}
  }
\\=&
  \op{P}^{(c/d)}
        _{(a/b|k/l)}
  \h_a^p\h_q^c\h_b^d\g_k^i\g_l^j
-
  \op{P}^{(i/j|c/d)}
        _{(k/l)}
  \g_q^i\g_k^p\g_l^j\h_a^c\h_b^d
\\
  \ip{\F|\tl{a}_{ab}^{ij}\tl{a}_{rs}^{pq}\tl{a}_{kl}^{cd}|\F}
=&
  \op{P}^{(c/d)}
        _{(a/b|k/l)}
  \gno{
    a_{a^{\ptcl1}b^{\ptcl2}}
     ^{i^{\hole1}j^{\hole2}}
    a_{r^{\ptcl3}s^{\ptcl4}}
     ^{p^{\ptcl1}q^{\ptcl2}}
    a_{k^{\hole1}l^{\hole2}}
     ^{c^{\ptcl3}d^{\ptcl4}}
  }
+
  \op{P}^{(i/j|c/d)}
        _{(k/l)}
  \gno{
    a_{a^{\ptcl1}b^{\ptcl2}}
     ^{i^{\hole1}j^{\hole2}}
    a_{r^{\hole1}s^{\hole2}}
     ^{p^{\hole3}q^{\hole4}}
    a_{k^{\hole3}l^{\hole4}}
     ^{c^{\ptcl1}d^{\ptcl2}}
  }
+
  \op{P}^{(p/q|i/j|c/d)}
        _{(r/s|k/l|a/b)}
  \gno{
    a_{a^{\ptcl1}b^{\ptcl3}}
     ^{i^{\hole1}j^{\hole3}}
    a_{r^{\hole1}s^{\ptcl2}}
     ^{p^{\hole2}q^{\ptcl1}}
    a_{k^{\hole2}l^{\hole3}}
     ^{c^{\ptcl2}d^{\ptcl3}}
  }
\\=&
  \op{P}^{(c/d)}
        _{(a/b|k/l)}
  \h_a^p\h_b^q\h_r^c\h_s^d
  \g_k^i\g_l^j
+
  \op{P}^{(i/j|c/d)}
        _{(k/l)}
  \g_r^i\g_s^j\g_k^p\g_l^q
  \h_a^c\h_b^d
-
  \op{P}^{(p/q|i/j|c/d)}
        _{(r/s|k/l|a/b)}
  \g_r^i\g_k^p\g_l^j
  \h_a^q\h_s^c\h_b^d
\end{array}$
\begin{align*}
&
  \implies
&&
  E_{\text{c}}
=
  \tfr{1}{4}
  \ol{g}_{kl}^{cd}c_{cd}^{kl}
&&
  E_{\text{c}}c_{ab}^{ij}
=
  \ol{g}_{ab}^{ij}
+
  \op{P}_{(a/b)}
  f_a^c
  c_{cb}^{ij}
-
  \op{P}^{(i/j)}
  f_k^i
  c_{ab}^{kj}
+
  \tfrac{1}{2}
  \ol{g}_{ab}^{cd}
  c_{cd}^{ij}
+
  \tfrac{1}{2}
  \ol{g}_{kl}^{ij}
  c_{ab}^{kl}
+
  \op{P}^{(i/j)}
        _{(a/b)}
  \ol{g}_{ak}^{ic}
  c_{bc}^{jk}
\end{align*}
\end{samepage}
\end{ex}


\chapter{Electricity \& Magnetism}

\section{Maxwell's Equations}
\begin{align}
	\nabla\cdot\mathbf{E}&=4\pi\rho \label{eq:gauss}\\[0.5em]
	\nabla\cdot\mathbf{B}&=0 \label{eq:divB0}\\[0.5em]
	\nabla\times\mathbf{E}&=-\frac{1}{c}\frac{\partial\mathbf{B}}{\partial t} \label{eq:faraday}\\[0.5em]
	\nabla\times\mathbf{B}&=\frac{4\pi}{c}\mathbf{j} + \frac{1}{c}\frac{\partial\mathbf{E}}{\partial t} \label{eq:ampere_maxwell}
\end{align}

\section{Basic Properties of Maxwell's Equations}
\begin{itemize}
	\item Eq. \ref{eq:gauss} relates the flux of the electric field through any ``Gaussian surface'' to the total enclosed charge. By counting the field lines through the surface, we may have an idea of the total enclosed charge, or the charge distribution.
	\item Eq. \ref{eq:divB0} says that there are no magnetic monopoles. Unlike electric fields, there are no ``magnetic point charges'' to generate magnetic fields. Rather, a magnetic field must be generated by the \textit{flow} of charge, i.e. a current. This fields are of course also generated by magnetic dipoles which may be viewed as miniature current loops. More specifically, this statement says that any magnetic field line that leaves a given volume must return to that given volume such that the net flux is zero.
	\item Eq. \ref{eq:faraday} says that a time varying magnetic field can produce an electric field. This is a statement of electromagnetic induction.
	\item Eq. \ref{eq:ampere_maxwell} says that a magnetic field can be generated both by a current and by a time-varying electric field.
	\item Because $\nabla\cdot\mathbf{B}=0$, we may express $\mathbf{B}$ as the curl of some vector field $\mathbf{A}$ such that $\nabla\times\mathbf{A}=\mathbf{B}$, where $\mathbf{A}$ is referred to as the magnetic vector potential. Using Eq. \ref{eq:faraday}, we find that $\nabla\times\left(\mathbf{E} + \frac{1}{c}\frac{\partial}{\partial t}\mathbf{A}\right) = 0$. Thus, we may express the term in the parentheses as $-\nabla\phi$, the gradient of a vector potential. This leads to the familiar formulation.
	\begin{equation}
		\mathbf{E} = -\nabla\phi - \frac{1}{c}\frac{\partial }{\partial t}\mathbf{A}.
	\end{equation}
\end{itemize}
%
\section{Electrostatics}
\section{Magnetostatics}
\section{Boundary Value Problems}
\section{Waves}


\subsection{2016 Free-Response Questions}
Questions 1 and 2 part of the same section area are allotted 30 minutes from completion with the aid of a graphing calculator.
Questions 3 through 6 are part of the same section and area allotted 1 hour from completion without the aid of a graphing calculator.

\begin{table}[H]
	\begin{center}
		\begin{tabular}{|c||c|c|c|c|c|}
			\hline
			$t$ (hours) & 0 & 1 & 3 & 6 & 8 \\
			\hline
			$R(t)$ (liters / hour) & 1340 & 1190 & 950 & 740 & 700 \\
			\hline
		\end{tabular}
	\end{center}
\end{table}

\begin{enumerate}
	\item Water is pumped into a tank at a rate modeled by $W(t)=2000e^{-t^2/20}$ liters per hour for $0 \leq t \leq 8$, where $t$ is measured in hours.
	Water is removed from the tank at a rate modeled by $R(t)$ liters per hour, where $R$ is differentiable and decreasing on $0 \leq t \leq 8$.
	Selected values of $R(t)$ are shown in the table above.
	At time $t=0$, there are 50000 liters of water in the tank.
	\begin{enumerate}
		\item Estimate $R^\prime(2)$.
		Show the work that leads to your answer.
		Indicate units of measure.
		\item Use a left Riemann sum with the four subintervals indicated by the table to estimate the total amount of water removed from the tank during the 8 hours.
		Is this an over estimate or underestimate of the total amount of water removed?
		Give a reason for your answer.
		\item Use your answer from part (b) to find an estimate for the total amount of water in the tank, to the nearest liters, at the end of the 8 hours.
		\item For $0 \leq t \leq 8$, is there a time $t$ when the rate at which the water is pumped into the tank is the same rate as the rate at which water is removed from the tank?
		Explain why or why not.
	\end{enumerate}

	\begin{figure}[H]
		\label{2016_2}
		\centering
		\includegraphics{./additional_materials/2016_2.png}
		\caption{\hyperref{https://secure-media.collegeboard.org/digitalServices/pdf/ap/ap16\_frq\_calculus\_bc.pdf}{}{}{AP Calculus BC 2016 Exam Free-Response Question 2}}
	\end{figure}

	\item At time $t$, the position of a particle moving in the $xy$-plane is given by the parametric functions $(x(t),y(t))$, where $\dd{x}{t} = t^2 + \sin{(3t^2)}$.
		The graph of $y$ consisting of three line segments, is shown in the figure above.
		At $t=0$, the particle is at position $(5,1)$.
		\begin{enumerate}
			\item Find the position of the particle at $t=3$.
			\item Find the slope of the line tangent to the part of the particle at $t=3$.
			\item Find the speed of the particle at $t=3$.
			\item Find the total distance traveled by the particle from $t=0$ to $t=2$.
		\end{enumerate}

	\begin{figure}[H]
		\label{2016_3}
		\centering
		\includegraphics{./additional_materials/2016_3.png}
		\caption{\hyperref{https://secure-media.collegeboard.org/digitalServices/pdf/ap/ap16\_frq\_calculus\_bc.pdf}{}{}{AP Calculus BC 2016 Exam Free-Response Question 3, Graph of $f$}}
	\end{figure}

	\item The figure above shows the graph of the piecewise-linear function $f$.
		For $-4 \leq x \leq 12$, the function $g$ is defined by $g(x)=\int_{2}^{x}{f(t)\d{t}}$.
		\begin{enumerate}
			\item Does $g$ have a relative minimum, a relative maximum, or neither at $x=10$?
				Justify your answer.
			\item Does the graph of $g$ have a point of inflection at $x=4$?
				Justify your answer.
			\item Find the absolute minimum value and the absolute maximum value of $g$ on the interval $-4 \leq x \leq 12$.
				Justify your answers.
			\item For $-4 \leq x \leq 12$, find all intervals for which $g(x) \leq 0$.
		\end{enumerate}

	\item Consider the differential equation $\dd{y}{x} = x^2 - \frac{1}{2}y$.
		\begin{enumerate}
			\item Find $\dd{^2y}{x^2}$ in terms of $x$ and $y$.
			\item Let $y=f(x)$ be the particular solution to the given differential equation whose graph passes through the point $(-2,8)$.
				Does the graph of $f$ have a relative minimum, a relative maximum, or neither at the point $(-2,8)$?
				Justify your answer.
			\item Let $y=g(x)$ be the particular solution to the given differential equation with $g(-1)=2$.
				Find $\lim_{x\to -1}{\left(\frac{g(x)-2}{3(x+1)^2}\right)}$.
				Show the work that leads to your answer.
			\item Let $y=h(x)$ be the particular solution to the given differential equation with $h(0)=2$.
				Use Euler's method, starting at $x=0$ with two steps of equal size, to approximate $h(1)$.
		\end{enumerate}

	\begin{figure}[H]
		\label{2016_5}
		\centering
		\includegraphics{./additional_materials/2016_5.png}
		\caption{\hyperref{https://secure-media.collegeboard.org/digitalServices/pdf/ap/ap16\_frq\_calculus\_bc.pdf}{}{}{AP Calculus BC 2016 Exam Free-Response Question 5}}
	\end{figure}

	\item The inside of a funnel of height 10 inches has circular cross sections, as shown in the figure above.
		At height $h$, the radius of the funnel is given by $r = \frac{1}{20}\left(3+h^2\right)$, where $0 \leq h \leq 10$.
		The units of $r$ and $h$ are inches.
		\begin{enumerate}
			\item Find the average value of the radius of the funnel.
			\item Find the volume of the funnel.
			\item The funnel contains liquid that is draining from the bottom.
				At the instant when the height of the liquid is $h=3$ inches, the radius of the surface of the liquid is decreasing at a rate of $\frac{1}{5}$ inch per second.
				At this instant, what is the rate of change of the height of the liquid with respect to time?
		\end{enumerate}

	\item The function $f$ has a Taylor series about $x=1$ that converges to $f(x)$ for all $x$ in the interval of convergence.
		It is known that $f(1)=1$, $f^\prime(1)=-\frac{1}{2}$, and the $n$th derivative of $f$ at $x=1$ is given by $f^{(n)}(1) = (-1)^{n}\frac{(n-1)!}{2^n}$ for $n \geq 2$.
		\begin{enumerate}
			\item Write the first four nonzero terms and the general term of the Taylor series for $f$ about $x=1$.
			\item The Taylor series for $f$ about $x=1$ has a radius of convergence of 2.
				Find the interval of convergence.
				Show the work that leads to your answer.
			\item The Taylor series for $f$ about $x=1$ can be used to represent $f(1.2)$ as an alternating series.
				Use the first three non-zero terms of the alternating series to approximate $f(1.2)$.
			\item Show that the approximation found in part (c) is within 0.001 of the exact value of $f(1.2)$.
		\end{enumerate}
\end{enumerate}


% !TeX root = ./main.tex
% chktex-file 46
% !TeX spellcheck = en-GB
% !TeX encoding = utf8

% - re-model current strategies: doing nothing, social distancing, isolation.

In the following we will present two extensions to the above methods.
First we demonstrate a baysian approach to estimate the populations' health state.
Second we show how the above methods could be utilized to derive disease containment policies.

\subsection{Baysian way of estimating the Probabilities}

A major drawback of our analysis is that we only take fixed disease states of individuals into account, i.e either susceptible, infected or recovered. However, in real live, due to the lack of data, these states are mostly unknown and have to be estimated by probability distributions. If we model these probabilities based on detailed assumptions, we can use Bayes' posterior probability to update these distributions based on the individual interaction of people. Thus, we define $X$ as the random variable that represents the state of a single individual. It can be assumed, that $X$ is well approximated by the multinomial distribution ($n\gg1$):
\begin{equation}
    X = \left(X_{S}, X_{I}, X_{R}\right) \sim \text{Mn}(n;p_S^{(t)},p_I^{(t)},p_R^{(t)})
\end{equation}
where $n$ is the population size, and $p_S^{(t)}$, $p_I^{(t)}$, and $p_R^{(t)}$ are the probability for an individual to be either in susceptible, an infected or recovered state. These probabilities change over time as the disease progresses, while

\begin{equation}
    p_S^{(t)}+p_I^{(t)}+p_R^{(t)}=1.
\end{equation}

As we can only estimate the probabilities $p_S^{(t)}$, $p_I^{(t)}$, and $p_R^{(t)}$ from samples of our population with a certain error, we assume that the probabilities are realisations of a prior distribution, in this case the Dirichlet distribution:

\begin{equation}
    \left(p_{S,i}^{(t)}, p_{I,i}^{(t)}, p_{R,i}^{(t)}\right) \sim \operatorname{Dir}\left(\alpha_{S,i}^{(t)}, \alpha_{I,i}^{(t)}, \alpha_{R,i}^{(t)}\right).
\end{equation}

The pseudo-counts $\alpha_{1,i}^{(0)}$, $\alpha_{2,i}^{(0)}$, and $\alpha_{3,i}^{(0)}$ can be estimated from $p_S^{(0)}$, $p_I^{(0)}$, and $p_R^{(0)}$ and are equal for each individual $i$ at $t=0$. Because we have no additional prior information, we also choose $\sum_{i\in\{S,I,R\}}\alpha_i = 1$.

If we now assume that two individuals with unknown state meet at the same location, an interaction occurs and we cannot neglect, that at least one of the individuals may carry the disease and may potentially infect the other. Therefore, we assume, that each individual is present in an infectious superstate $\braket{x}$ where
\begin{equation}
    \braket{x} = \left(\braket{x_{S,i}}, \braket{x_{I,i}}, \braket{x_{R,i}}\right) = \left(p_S^{(t)},p_I^{(t)},p_R^{(t)}\right)
\end{equation}
If the state of an individual in this group known, due to a previous test for example, the state collapses into the right state (e.g $(0,1,0)$ for an infected individual).

Based on this either known or super positional state, we can define the graph based Bayesian update rule of the pseudo-counts, where the infection rate $\beta$ is also taken into account, as followed~\cite{rice2006mathematical}:

\begin{align}
    \alpha_{S,i}^{(t+1)\prime} &= \alpha_{1,i}^{(t)\prime}\\
    \alpha_{I,i}^{(t+1)\prime} &= \alpha_{2,i}^{(t)\prime} + \beta \cdot
    \sum_{j\in A^{(t)}_{v_i}} A^{(t)}_{i,j} x_{I,j}\\
    \alpha_{R,i}^{(t+1)\prime} &= \alpha_{3,i}^{(t)\prime}\\
    \alpha_{0,i}^{(t+1)\prime} &= \alpha_{S,i}^{(t+1)\prime}+\alpha_{I,i}^{(t+1)\prime}+\alpha_{R,i}^{(t+1)\prime}
\end{align}

Important to notice is that the adjacent $A$ has to be normalized to $\operatorname{max}(A)=1$, where $A_{v_i,v_j}=1$ means a direct contact between individual $v_i$ and individual $v_j$.
In this step, the update is only done for the $\alpha_I$, as only the possible infection of an individual can change the infection state of another individual.

However, so far this model still does not take the potential recovery into account. We can add this by looking at the estimated Values of $p_{S,i}^{(t+1)}$, $p_{I,i}^{(t+1)}$, and $p_{R,i}^{(t+1)}$
\begin{alignat}{2}
    \operatorname{E}[p_{S,i}^{(t+1)}] &= \frac{\alpha_{S,i}^{(t+1)\prime}}{\alpha_{0,i}^{(t+1)\prime}} & &= \frac{\alpha_{S,i}^{(t+1)}}{\alpha_{0,i}^{(t+1)\prime}} \\
    \operatorname{E}[p_{I,i}^{(t+1)}]&= \frac{\alpha_{I,i}^{(t+1)\prime}}{\alpha_{0,i}^{(t+1)\prime}}-\gamma & &=\frac{\alpha_{I,i}^{(t+1)}}{\alpha_{0,i}^{(t+1)}} \\
    \operatorname{E}[p_{R,i}^{(t+1)}]&= \frac{\alpha_{R,i}^{(t+1)\prime}}{\alpha_{0,i}^{(t+1)\prime}}+\gamma & &= \frac{\alpha_{R,i}^{(t+1)}}{\alpha_{0,i}^{(t+1)}}
\end{alignat}

Based on this equation system, we can calculate $\alpha_{k,i}^{(t+1)}$ for $k\in{S,I,R}$. Needless to say, that if the expectation value of $p_{I,i}^{(t+1)}$ or $p_{R,i}^{(t+1)}$ return a value smaller than 0 or larger than 1, we set $\gamma=0$ and therefore $\alpha_{k,i}^{(t+1)\prime}=\alpha_{k,i}^{(t+1)}$. Something similar may be found in~\cite{stojanovic2019bayesian}.

We see here, that the probability for an individual to be in the susceptible state shrinks over time, the more contacts with people have been observed. Thus, if we set this individual into quarantine, the probabilities shift towards either the susceptible state or the recovered state. This is an early indicator, why it is necessary to put the whole population under quarantine in the case of a pandemic.

We can now find an optimal test setup by applying tests to those individuals, who's unknown infection state introduce the most uncertainty to our estimation. Furthermore, we could even set individuals with a high probability to have the disease under quarantine, without testing them (which should be an urgent object of an ethical discussion).

\subsection{Policy Design}
In the previous sections we have shown how predict the health state of all individuals in a location tracked population.
The next step is to use this information to compute optimal policies.
These policies guide a governments' and societies' response to the spread of COVID-19 and modify how the disease is able to spread in a population.

An optimal policy always keeps the infection counts below the medical systems' capacity and the total number of infections as small as possible.
% not much real data on this.
Mathematically speaking we want to minimize the sum of all future infections
\begin{equation}\label{eq:number-of-infected}
	\min \sum_{\forall t} N^{(t)}_i
\end{equation}
whilst
\begin{equation}\label{eq:cap-constraint}
	N^{(l)}_i \leq N_{limit}, \forall t
\end{equation}

A policy may consist out of two different kinds of actions, non pharmaceutical interventions (NPI) and test prioritization (TP).

\subsubsection{Non Pharmaceutical Interventions (NPI)}
All non pharmaceutical interventions can be understood as some kind of edge removal in our graph-based approach:
\begin{itemize}
	\item Isolation of an infected individual removes all of its edges with very high probability.
	\item Quarantine of a contact person removes all of its edges with high probability.
	\item Social distancing removes some edges of many individuals.
	\item Cancellation of large events remove many edges of many individuals.
\end{itemize}

Formally speaking, the square matrix $C \in \mathbb{B}$ with dimensions $N \times N$ models desirable edge cancellations.
This is the policy, which will be optimized.
To avoid the trivial solution of $C=0$, the cancellation of all edges, we also want to minimize the the number of cancellations
\begin{equation}\label{eq:cancellations}
	\min_{C} -\sum_i \sum_j C_{ij}
\end{equation}

Note, that this matrix does not have to know the edges of a future time step, it only expresses which edges must not exist.
It is multiplied element-wise onto the adjacency matrix $A$ to obtain the adjacency matrix with applied cancellations $\bar{A} = A \odot C$.

% Future Work: This would be more powerful, if there would be some different kinds of edges (social, work, education, large events, etc.)\\
% Future Work: This only takes the current time step into account but it would be desirable to look even further into the future.

To obtain an optimal cancellation policy one thus must jointly minimize \cref{eq:number-of-infected,eq:cancellations} whilst fulfilling \cref{eq:cap-constraint}.

\subsubsection{Test Prioritization (TP)}
When tests are limited, we argue that they should be used to discover as much as possible about the health state of the overall population.
This in turn allows non pharmaceutical interventions such as school cancellations to become more efficient.
Currently there are only rough medical-based guidelines who should be tested and who should not.

Lets assume there are $t_{\text{max}}$ tests per time step.
A test reveals the true health state of an individual (ignoring false negatives and false positives)
\begin{equation}
h_{{v}_i}^{(t)} \xrightarrow{\text{test}} h_{{v}_i}^{(t+1)} \in \{\vec{e}_0, \vec{e}_1, \vec{e}_2 \}
\end{equation}

The test assignment $T$ with dimension $N$ is a binary variable describing which individuals should be tested.


\chapter{Speech Recognition Features}

\section{Mel Spectral Features}
\label{sec:mel-spectral-features}

Mel spectral features are generally formed using a filterbank
whose center frequencies increase exponential and whose
bandwidths are increasing along the frequency axis.  They are
centered such that they are linear with the mel scale given
by the equation
\begin{equation}
mel(f) = 2595 \log_{10}\left(1 + \frac{f}{700}\right)
\end{equation}
which is given in \cite{wiki:mel_scale}.