\documentclass{memoir}
\usepackage{linalg}

% \begin{figure}[ht]
%     \centering
%     \incfig{riemmans-theorem}
%     \caption{Riemmans theorem}
%     \label{fig:riemmans-theorem}
% \end{figure}

\begin{document}
% \section{}
\chapter{Special Vector Spaces}
\label{cha:products_and_quotients_of_vector_spaces}

\section{Products and Direct Sums}
\begin{defn}[Product of Vector Spaces]
	Suppose $V_1,\ldots,V_m$ are vector spaces over $F$.
	\begin{itemize}
		\item The \textbf{product} $V_1\times \ldots \times V_m$ is defined by
			\begin{align*}
				V_1 \times \ldots \times V_m = \left\{(v_1,\ldots,v_m) \mid v_1 \in V_1,\ldots,v_m \in V_m \right\} .
			\end{align*}
	\item Addition on $V_1\times \ldots \times V_m$ is defined by
		\begin{align*}
			(u_1,\ldots,u_m) + (v_1,\ldots,v_m) = (u_1+v_1,\ldots,u_m+v_m).
		\end{align*}
	\item Scalar multiplication on $V_1\times \ldots \times V_m$ is defined by
		\begin{align*}
			 \lambda(v_1,\ldots,v_m) = (\lambda v_1,\ldots, \lambda v_m).
		\end{align*}
	\end{itemize}
\end{defn}
\begin{lemma}
	Suppose $V_1,\ldots,V_m$ are vector spaces over $F$. Then $V_1\times \ldots \times V_m$ is a vector space over $F$.
\end{lemma}
\begin{lemma}[Dimension of a product is sum of dimensions]
	Suppose $V_1,\ldots,V_m$ are finite-dimensional vector spaces. Then $V_1 \times \ldots \times V_m$ is finite-dimensional and
	\begin{align*}
		\textrm{dim}(V_1\times \ldots\times V_m) = \textrm{dim}V_1 + \ldots + \textrm{dim}V_m .
	\end{align*}
\end{lemma}
\begin{lemma}[Products and direct sums]
	Suppose that $U_1,\ldots,U_m$ are subspaces of $V$. Define a linear map $\Gamma: U_1\times \ldots\times U_m \to U_1+\ldots+U_m$ by
	\begin{align*}
		\Gamma(u_1,\ldots,u_m) = u_1+\ldots+u_m.
	\end{align*}
	Then $U_1 + \ldots + U_m$ is a direct sum if and only if $\Gamma$ is injective (and thus invertible).
\end{lemma}
\begin{lemma}
	Suppose $V$ is finite-dimensional and $U_1,\ldots,U_m$ are subspaces of $V$. Then $U_1+\ldots+U_m$ is a direct sum if and only if
	\begin{align*}
		\textrm{dim}(U_1+\ldots+U_m) = \textrm{dim}U_1 + \ldots + \textrm{dim}U_m.
	\end{align*}
\end{lemma}
\section{Quotients of Vector Spaces}
\label{sec:quotients_of_vector_spaces}
\begin{defn}[v + U]
	Suppose $v \in V$ and $U$ is a subspace of $V$. Then $v+U$ is the subset of $V$ defined by
	\begin{align*}
		v + U = \left\{v+u \mid u \in U \right\} .
	\end{align*}
\end{defn}
\begin{defn}[affine subset, parallel]
	An \textbf{affine subset} of $V$ is a subset of $V$ of the form $v+U$ for some $v \in V$ and some subspace $U$ of $V$.\\

	For $v \in V$ and $U$ a subspace of $V$, the affine subset $v+U$ is said to be \textbf{parallel} to $U$.
\end{defn}
\begin{defn}[Quotient space]
	Suppose $U$ is a subspace of $V$. Then the \textbf{quotient space} $V / U$ is the set of all affine subsets of $V$ parallel to $U$. In other words,
	\begin{align*}
		V / U = \left\{v + U \mid v \in V \right\}.
	\end{align*}
\end{defn}
\begin{lemma}
	Suppose $U$ is a subspace of $V$ and $v,w \in V$. Then the following are equivalent:
	\begin{itemize}
		\item $v - w \in U$
		\item $v + U = w + U$
		\item $(v+U) \cap (w+U) \neq \emptyset$
	\end{itemize}
\end{lemma}
\begin{defn}[addition and scalar multiplication on $V / U$]
	Suppose $U$ is a subspace of $V$. Then \textbf{addition} and \textbf{scalar multiplication} are defined on $V / U$ by
	\begin{align*}
		(v+U) + (w + U) = (v + w) + U\\
		\lambda(v+U) = (\lambda v) + U
	\end{align*}
	for $v,w \in V$ and $\lambda in F$.
\end{defn}
\begin{thm}
	Suppose $U$ is a subspace of $V$. Then $V / U$ is a vector space.
\end{thm}
\begin{defn}[quotient map, $\pi$ ]
	Suppose $U$ is a subspace of $V$. The \textbf{quotient map} $\pi$ is the linear map $\pi:V\to V / U$ defined by
	\begin{align*}
		\pi(v) = v + U
	\end{align*}
	for $v \in V$.
\end{defn}
\begin{lemma}
	Suppose $V $ is finite-dimensional and $U$ is a subspace of $V$. Then
	\begin{align*}
		\textrm{dim}V / U = \textrm{dim}V - \textrm{dim}U.
	\end{align*}
\end{lemma}
Each linear map $T$ on $V$ induces a linear map $\hat{T} on V / ( \textrm{null}T)$, which we define:
\begin{defn}
	Suppose $T \in \mathcal{L}(V,W)$. Define $\hat{T}:V / ( \textrm{null}T) \to W$ by
	\begin{align*}
		\hat{T}(v+ \textrm{null}T) = Tv.
	\end{align*}
\end{defn}
\begin{lemma}[Null space and range of $\hat{T}$]
	Suppose $T \in  \mathcal{L}(V,W)$. Then
	\begin{itemize}
		\item $\hat{T}$ is a linear map from $V / ( \textrm{null}T)$ to $W$
			\item $\hat{T}$ is injective
			\item \textrm{range} $\hat{T}$ = \textrm{range} $T$
			\item $V / ( \textrm{null} T)$ is isomorphic to $ \textrm{range} T$.
	\end{itemize}
\end{lemma}
\section{Dual Space and Dual Map}
\label{sec:dual_space_and_dual_map}
\begin{defn}[Linear functional]
	A \textbf{linear functional} on $V$ is a linear map from $V$ to $F$. In other words, a linear functional is an element of $\mathcal{L}(V,F)$.
\end{defn}
\begin{defn}[Dual Space]
	The \textbf{dual space} of $V$, denoted $V'$, is the vector space of all linear functionals on $V $. In other words, $V' = \mathcal{L}(V,F)$.
\end{defn}
\begin{cor}
	Suppose $V $ is finite-dimensional. Then $V'$ is also finite-dimensional and $ \textrm{dim}V' = \textrm{dim}V$
\end{cor}
\begin{defn}[Dual Basis]
	If $v_1,\ldots,v_n$ is a basis of $V$, then the \textbf{dual basis} of $v_1,\ldots,v_n$ is the list $\varphi_1,\ldots,\varphi_n$ of elements of $V'$, where each $\varphi_j$ is the linear functional on $V$ such that
	\begin{align*}
		\varphi_j (v_j) = \begin{cases}
			1 & \text{if } k = j\\
			0 & \text{if } k \neq j
		\end{cases}
	\end{align*}
\end{defn}
\begin{lemma}
	Suppose $V$ is finite-dimensional. Then the dual basis of a basis of $V$ is a basis of $V'$.
\end{lemma}

\begin{defn}[Dual Map]
	If $T \in \mathcal{L}(V,W)$, then the \textbf{dual map} of $T$ is the linear map $T' \in \mathcal{L}(W',V')$ defined by $T'(\varphi) = \varphi \circ T$ for $\varphi \in W'$.
\end{defn}
\begin{lemma}[Algebraic properties of dual maps]
	\begin{itemize}
		\item $(S+T)' = S' + T'$ for all $S,T \in \mathcal{L}(V,W)$.
		\item $(\lambda T)' = \lambda T'$ for all $\lambda \in F$, and all $T \in \mathcal{L}(V,W)$.
		\item $(ST)' = T'S'$ for all $T \in \mathcal{L}(U,V)$ and all $S \in \mathcal{L}(V,W)$.
	\end{itemize}
\end{lemma}

\begin{defn}[Transpose]
The \textbf{transpose} of a matrix $A$, denoted $A^{T}$, is the matrix obtained from $A$ by interchanging the rows and columns. More specifically, if $A$ is an $m$-by-$n$ matrix, then $A^{T}$ is the $n$-by-$m$ matrix whose entries are given by
\begin{align*}
	(A^{T})_{k,j} = A_{j,k}
\end{align*}
\end{defn}
\begin{cor}
	If $A$ is $m\times n$, and $C$ is $n\times p$, then $(AC)^{T} = C^{T}A^{T}$
\end{cor}
These properties extend to their matrices.
\begin{thm}[The matrix of T' is the transpose of the matrix of T]
	Suppose $T \in \mathcal{L}(V,W)$. Then $\mathcal{M}(T') = (\mathcal{M}(T))^{T}$
\end{thm}
\end{document}


\documentclass[letterpaper, 10pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath,amsthm,amssymb,scrextend}
\usepackage{fancyhdr}
\pagestyle{fancy}
\usepackage{silence}
\WarningFilter{latex}{You have requested package}
\input{ltx/pkg/preamble}

\begin{document}

\lhead{MAT224 Linear Algebra II}
\chead{Triangular and Nilpotent Matrices}
\rhead{Week 10}

\title{Linear Algebra II \\ \Large{MAT224}}
\author{Lennart Döppenschmitt}
% \maketitle
% \tableofcontents

\section*{Triangular Form}%
\textbf{Textbook:} Section 6.1

\pr
A diagonalizable matrix is in its \emph{normal form} when it is diagonal. And every
diagonalizable matrix can be brought to a diagonal form with a change of basis to
a \emph{canonical basis}.
\lb
Some matrices are not diagonalizable, what are their canonical forms and canonical bases?
The remaining sections 6.1-6.4 answer this quetsion step by step.

\lb
For this entire lecture, let $V$ be a finite dimensional vector space over a field $F$.

\lb
\textbf{Definition}
\lb
A matrix $A ∈ \mat{n}{F}$ is called \emph{upper triangular} if all entries below the diagonal
are 0. For example,
\[
    \begin{pmatrix}
        \ast & \ast & \ast \\
        0 & \ast & \ast \\
        0 & 0 & \ast \\
    \end{pmatrix}
\]

\lb
\textbf{Definition (6.1.2)}
\lb
Let $\map{V}[T]{V}$ be a linear transformation. A subspace $W \subseteq V$ is called
\emph{invariant} or \emph{stable} under $T$ if $T(W) \subseteq W$.

\lb
\textbf{Examples}
\begin{enumerate}
    \item Let $\map{F^3}[P_{xy}]{F^3}$ be the projection on the xy-plane. Then the xy-plane is
        an invariant subspace for it.
    \item $ \cb{0} $ and $V$ are always invariant subspaces for any transformation
        $T ∈ \mathcal{L}(V)$.
\end{enumerate}

\vspace{40pt}
\lb
\textbf{Discussion}
\lb
Let $\map{V}[T]{V}$ be a linear transformation such that in a basis
$β = \cb{\vec v_1, \vec v_2, \vec v_3}$
\[ [T]_β^β = \begin{pmatrix}  2 & 1 & -1 \\  & 3 & 1 \\  &  & 3 \end{pmatrix} \]

\begin{enumerate}
    \item Is $\spn{\vec v_1}$ invariant?
    \item Can you find three subspaces $W_1 \subset W_2 \subset W_3 $ which are all invariant?
\end{enumerate}

\newpage
\lb
\textbf{Proposition (6.1.4)}
\lb
A linear transformation $\map{V}[T]{V}$ is upper triangular in a basis $β$ if and only if for
each $i$, $1 \leq i \leq \dim(V)$
\[ W_i = \spn{\vec v_1, \ldots, \vec v_i} \]
is invariant.
\begin{proof}
\end{proof}
\vspace{200pt}

\lb
\textbf{Definition (6.1.5)}
\lb
A linear transformation $\map{V}[T]{V}$ is called triangulizable if there exists a basis $β$
such that $[T]_β^β$ is upper trianglar.

\lb
We skip a technical result from proposition 6.1.6 in the book and directly state
\lb
\textbf{Theorem (6.1.8)}
\lb
A linear transformation $\map{V}[T]{V}$ is triangulizable if and only if
the characterstic polynomial $c_T(λ)$ has $dim(V)$ roots (counted with multiplicity).

\vspace{100pt}

\lb
\textbf{Discussion}
\lb
Let $A ∈ \mat{n}{\bb C}$. Why is there always an upper trianglar matrix $B ∈ \mat{n}{\bb C}$
which is similar to $A$?

\newpage
\lb
\textbf{Notation}
\lb
If $p(x) = a_n x^k + \dots + a_0$ is a polynomial in $\pol{k}{F}$ and $A ∈ \mat{n}{F}$, we
define
\[ p(A) = a_n A^k + \dots + a_0 I_n \]

\lb
\textbf{Discussion}
\begin{enumerate}
    \item Suppose $A, B ∈ \mat{n}{F}$ are similar matrices such that
        $A = Q B \iv Q$ for an invertible matrix $Q ∈ \mat{n}{F}$.
        Show that
        \[ p(A) = Q p(B) \iv Q \]
    \item Suppose that
        \[ A = \begin{pmatrix}
            λ_1 & \ast & \cdots& \ast \\
                & λ_2 & & \vdots \\
                & & \ddots &  \ast \\
                & & & λ_n
        \end{pmatrix}
    \]
    is an upper triangular matrix. Compute $c_A(λ)$ and $c_A(A)$.
\end{enumerate}

\vspace{200pt}
\lb
\textbf{Theorem (6.1.12)}
\lb
Let $\map{V}[T]{V}$ be a linear transformation and assume that
$c_A(λ)$ has $dim{V}$ roots in $F$. Then $c_T(T) = 0$.
\begin{proof}
    \emph{( Sketch of proof )}
\end{proof}

\newpage
\section*{Nilpotent Normal Forms}%
\textbf{Textbook:} Section 6.2

\lb
The next kind of matrices we want to bring into a normal form are nilpotent matrices. Remember
that for the reminder of these notes $V$ is a finite dimensional vector space over a field $F$.

\lb
\textbf{Definition}
\lb
A linear transformation $\map{V}[N]{V}$ is called
\emph{nilpotent} if $N^n = 0$ for some $n \geq 1$.
The least $n$ such that $N^n = 0$ is called the \emph{index} of the nilpotent transformation.

\lb
\textbf{Discussion}
\begin{enumerate}
    \item Suppose $\map{V}[N]{V}$ is nilpotent, does $N$ always have an eigenvector?
    \item Suppose $\map{V}[N]{V}$ is nilpotent and $λ$ is an eigenvalue of $N$. What can $λ$ be?
    \item Suppose $\map{V}[T]{V}$ has only one distinct eigenvalue $λ=0$ of multiplicity
        $ m_λ = \dim(V)$. Is $T$ nilpotent?
\end{enumerate}

\vspace{100pt}
\lb
\textbf{Observation}
\lb
For a nilpotent transformation $\map{V}[N]{V}$ and $\vec v ∈ V$ nonzero
\[ N^k (\vec v) = 0\]
for $1 \leq k \leq n$, but not necessarily $n = k$!

\vspace{140pt}
\lb
\textbf{Deifnition (6.2.1)}
\lb
Let $\map{V}[N]{V}$ be a nilpotent transformation on $V$ and $\vec v ∈ V$
nonzero with $k$ as above.

\begin{enumerate}
    \item The set $α = \cb{N^{k-1}(\vec v), N^{k-2}(\vec v), \ldots, \vec v}$ is called the
        \emph{cycle} generated by $\vec v$.
        \pr $\vec v$ is called the \emph{inital vector} of this cycle.
    \item The subspace generated by this cycle $C(\vec v) = \spn{α}$ is called the
        \emph{cyclic subspace} generated by $\vec v$.
    \item We call $k$ the \emph{length} of the cycle.
\end{enumerate}

\newpage
\lb
\textbf{Discussion}
\begin{enumerate}
    \item Is $ \frac{d^2}{dx^2} $ nilpotent on $\pol{n}{F}$? What is the index?
    \item Can you find a polynomial $p ∈ \pol{n}{F}$ that generates a cycle of length 3?
\end{enumerate}

\lb
Look at Example (6.2.2) in the book for another example.

\vspace{200pt}
\lb
\textbf{Proposition (6.2.3)}
\lb
Let $\map{V}[N]{V}$ be a nilpotent transformation on $V$ and $\vec v ∈ V$.
\begin{enumerate}
    \item If $\vec v$ generates a cycle of length $k$, then $N^{k-1}(\vec v)$ is
        an eigenvector of $N$ with eigenvalue $λ=0$.
    \item $C(\vec v)$ is an invariant subspace for $N$.
    \item The cycle $α$ generated by $\vec v$ is independent and hence a basis for $C(\vec v)$.
\end{enumerate}
\begin{proof}
\end{proof}

\newpage
\lb
\textbf{Notation (Cycle tableau)}
\lb
Let $\map{V}[N]{V}$ be a nilpotent transformation on $V$.
\begin{enumerate}
    \item Write for a cycle $α$ of length $k$ a row of $k$
        boxes to represent every element in $α$.
        \vspace{100pt}
    \item If we consider $r$ cycles $α_1, \ldots, α_r$ generated by $\vec v_1, \ldots, \vec v_r$
        each of length $k_1, \ldots, k_r$ write $r$ rows of boxes sorted by length.
        \vspace{100pt}
        \lb
        We call this the \emph{cycle tableau} of the cycles $α_1, \ldots α_r$.
\end{enumerate}

\newpage
\lb
\textbf{Discussion}
\lb
Consider the following cycle tableau of the cycles $α_1, α_2, α_3$ of a nilpotent transformation
$\map{V}[N]{V}$.
\vspace{100pt}
\lb
Which of the boxes necessarily correspond to elements in
\begin{enumerate}
    \item ker($N$)
    \item im($N$)
    \item im($N^2$)
    \item ker($N) \cap \tx{im}(N^3)$
\end{enumerate}

\newpage
\lb
\textbf{Discussion}
\lb
Let $\map{V}[N]{V}$ be a nilpotent transformation on a vector space $V$ of dimension $6$.
\begin{enumerate}
    \item If $\vec v$ generates a cycle $α$ of length $6$,
        \begin{enumerate}
            \item is $α$ a basis for $V$?
            \item What is $[N]_α^α$?
        \end{enumerate}
    \item If $α_1, α_2, α_3$ are cycles of lengths $1, 2$ and $3$ such that
        $β = α_1 \cup α_2 \cup α_3$ is linearly independent,
        \begin{enumerate}
            \item is $β$ a basis for $V$?
            \item What is $[N]_β^β$?
        \end{enumerate}

    \item
        Does the matrix $[N]_β^β$ depend on the particular elements in $β$?
\end{enumerate}

\newpage
\lb
\textbf{Notation}
\lb
\begin{enumerate}
    \item \emph{nilpotent Jordan block} of size $k \times k$
    \vspace{150pt}
    \item direct sum of matrices
    \vspace{150pt}
    \item \emph{nilpotent Jordan matrix}
\end{enumerate}

\vspace{150pt}
\lb
\textbf{Goal:} For a nilpotent transformation, we want to find a basis consisting of cycles.
\lb
\q{How many cycles do we need? Which cycles can we use? How do we find these cycles?}

\newpage
\lb
\textbf{Proposition (6.2.4)}
\lb
Let $α_1, \ldots, α_r$ be cycles of lengths $k_i$ respectively, generated by $\vec v_i$.
If the set of eigenvectors
\[ \cb{N^{k_1 -1} (\vec v_1) , \ldots, N^{k_r -1} (\vec v_r)} \]
is linearly independent, then the union
\[ α_1 \cup \cdots \cup α_r \]
is linearly independent.
\begin{proof}
\end{proof}

\vspace{300pt}
\lb
\textbf{Definition (6.2.5 \& 6.2.7)}
\lb
Let $\map{V}[N]{V}$ be a nilpotent transformation.
\begin{enumerate}
    \item Cycles $α_1, \ldots, α_r$ such that $ α_1 \cup \cdots \cup α_r $ is linearly independent are called \emph{non-overlapping} cycles.
    \item A basis of $V$ consisting of non-overlapping cycles for $N$ is called
        a \emph{canonical basis} for $N$.
\end{enumerate}

\newpage
\lb
\textbf{Theorem (6.2.8)}
\lb
Every nilpotent transformation $\map{V}[N]{V}$, has a canonical basis on $V$.

\vspace{40pt}
\lb
\textbf{Lemma (6.2.9)}
\lb
The cycle tableau of a canonical basis for $N$ has
\[ \dim(\ker(N^j)) - \dim(\ker(N^{j-1})) \]
boxes in column $j$.

\vspace{40pt}
\lb
\textbf{Discussion (6.2.10)}
\lb
Let $\map{V}[N]{V}$ be a nilpotent transformation such that
\begin{itemize}
    \item $\dim(\ker(N)) = 3$
    \item $\dim(\ker(N^2)) = 5$
    \item $\dim(\ker(N^3)) = 7$
    \item $\dim(\ker(N^4)) = 8$
\end{itemize}
What shape does the cycle tableau of a canonical basis for $N$ must have?
What is the canonical form of the transformation $N$ is such a basis?

\vspace{200pt}
\lb
\textbf{Corollary (6.2.11)}
\lb
The canonical form of a nilpotent transformation is unique up to reordering
the nilpotent Jordan blocks. ( By convention we sort them by size. )

\newpage
\lb
\textbf{Discussion}
\lb
Find the nilpotent Jordan form and a canonical basis $α$ of $T(p) = \frac{d^2}{dx^2} p$
on $\pol{3}{\bb C}$.

\end{document}


\documentclass{article}
\usepackage{tocloft}
\include{common_symbols_and_format}
\renewcommand{\cfttoctitlefont}{\Large\bfseries}
\begin{document}
\logo
\rulename{Test Rule}
\ruledescription{This rule uses differentials between four moving averages to determine trade positioning. The parameters accepted are the integer length of each short average (2 parameters - one for price, one for research), the additional number of days for the longer averages (2 parameters - also, one for price, one for research) and 4 coefficients for each average's weighting contribution. The total sum is divided by the current price to calculate a position size.}
\ruleparameters
{Short price average length}{2}{Number of days in the short price
average.}{$\averagelengthshort^{\price}$}
{Long price average length}{5}{Number of additional days in the longer price average (added to the number in the short price average).}{$\averagelengthlong^{\price}$}
{Short research average length}{2}{Number of days in the short research average.}{$\averagelengthshort^{\research}$}
{Long research average length}{5}{Number of additional days in the longer research average (added to the number in the short price average).}{$\averagelengthlong^{\research}$}
{Amplitude of short price average}{1.0}{Weighting coefficient for the short term average of price.}{$\amplitudecoefficientone^{\price}$}
{Amplitude of long price average}{1.0}{ Weighting coefficient for the long term average of price.}{$\amplitudecoefficienttwo^{\price}$}
{Amplitude of short research average}{1.0}{Weighting coefficient for the short term average of research.}{$\amplitudecoefficientone^{\research}$}
{Amplitude of long research average}{1.0}{Weighting coefficient for the long term average of research.}{$\amplitudecoefficienttwo^{\research}$}
\stoptable

\section{Equation}

\begin{equation}
\bigcontribution(\currenttime, \averagelength, \amplitudecoefficient, \genericfunction) = \frac{\amplitudecoefficient}{\averagelength} \sum_{\dummyiterator=0}^{\averagelength-1} \genericfunction(\currenttime - \dummyiterator)\\
\label{eq1}
\end{equation}

\begin{equation}
\position(\currenttime) = \frac{\bigcontribution(\currenttime, \averagelengthshort^{\price}, \amplitudecoefficientone^{\price}, \price)+\bigcontribution(\currenttime, (\averagelengthshort^{\price} + \averagelengthlong^{\price}), \amplitudecoefficienttwo^{\price},\price)+\bigcontribution(\currenttime, \averagelengthshort^{\research}, \amplitudecoefficientone^{\research}, \research)+\bigcontribution(\currenttime, (\averagelengthshort^{\research} + \averagelengthlong^{\research}), \amplitudecoefficienttwo^{\research}, \research)}{\price(\currenttime)} \\
\label{eq2}
\end{equation}

\hspace{200mm}

\noindent where $\position_\currenttime$ is the portfolio allocation at time $\currenttime$, $\price = \price(\currenttime)$ is the value of the price series and $\research = \research(\currenttime)$ is the value of the research series.

\hspace{200mm}
\hspace{200mm}

\keyterms
\furtherlinks

\end{document}


\documentclass[paper.tex]{subfiles}

\usepackage{tikz}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{tabularx}
\usepackage{multicol}
\usepackage{algpseudocode}
\usepackage{algorithm}

% Add vertical spacing to tables
\renewcommand{\arraystretch}{1.4}

% Begin Document
\begin{document}

\section{Background}

The Minimum Dominating Set of a Graph is defined as the set of vertices for which each vertex or one of its neighbors is in the set.
The size of the minimum set will be unique, however there may be multiple sets of that size that are a minimal covering.
Consider the following graph:

\vspace{5mm}
\begin{center}
\begin{tikzpicture}[node distance={15mm}, thick, main/.style = {draw, circle}]
    \node[main] (1) {$x_1$};
    \node[main] (2) [right of = 1] {$x_2$};
    \node[main] (3) [right of = 2] {$x_3$};
    \node[main] (4) [right of = 3] {$x_4$};
    \node[main] (5) [below right of = 1] {$x_5$};
    \node[main] (6) [below right of = 2] {$x_6$};
    \node[main] (7) [below right of = 3] {$x_7$};
    \draw (1) -- (5);
    \draw (1) -- (2);
    \draw (2) -- (3);
    \draw (2) -- (5);
    \draw (2) -- (6);
    \draw (3) -- (6);
    \draw (3) -- (7);
    \draw (4) -- (7);
\end{tikzpicture}
\end{center}

The vertex $x_2$ covers vertices $\{x_1,x_3,x_5,x_6\}$, leaving nodes $x_4$ and $x_7$.
Either vertex can dominate the other and complete a covering of the graph.
Thus, this graph has two minimum dominating sets, $\{x_2,x_4\}$ and $\{x_2,x_7\}$.

This problem is computationally difficult, and scales exponentially with the size of the graph.
The next two sections cover the Brute Force approach and the Approximation approach, respectively.

\end{document}


In the previous Chapter, we demonstrated the use of the adjoint method to compute the gradient of a device that is parameterized by optical phase shifters.  We showed that this formalism allows one to train a common class of optical neural network based on MZI meshes.  In this Chapter, we will continue to explore the use of the adjoint method in training of optical machine learning hardware, but will take a more general approach.  Specifically, we will discuss the use of an optical system (or a wave system, more generally), as an analog computing platform for processing time series signals.  As we will show, this capability is made possible by the use of the adjoint method, which allows one to come up with a structure to solve a particular machine learning task through inverse design and optimization \cite{hughes_wave_2019}

% In this framework, rather than using reflections or feedback circuits, the recurrence relationship occurs naturally in the time dynamics of the physics itself and the memory and information processing is provided by the waves as they propagate through space.

% To train the analog RNN, we show that standard gradient-based optimization techniques, as used in the field of machine learning, can tailor the physical characteristics of the system for a given task.
% This allows an inhomogeneous distribution of materials to learn complex features in time-series data with strong temporal dynamics.
% As a demonstration, we inject raw audio signals of spoken vowels into a numerical model of the wave equation and train the physical domain between the emitter and several receiving probes to classify the vowels.
% Our results demonstrate that such an approach can achieve high classification accuracy and is capable of generalizing to unseen vowel samples, with performance comparable to that of a conventional RNN.

\section{Wave Physics as a Recurrent Neural Network}

Analog computing platforms, which use the natural evolution of a continuous physical system to perform calculations, are emerging as important direction for the implementation of machine learning models \cite{shen2017deep, biamonte_quantum_2017, laporte2018numerical, lin2018all, Khoram_2019}.
Such a scheme has a number of potential benefits. Namely, they are able to \textit{passively} process signals and information in their native domain, without analog-to-digital conversion, which should result in a significant gain in speed and a reduction in power consumption.

Here, we identify a mapping between the dynamics of wave-based physical phenomena, such as acoustics and optics, and the computation in a recurrent neural network (RNN).
RNNs are one of the most important machine learning models and have been widely used to perform tasks such as natural language processing \cite{yao2013recurrent} and time-series prediction \cite{husken_recurrent_2003, dorffner_neural_1996, connor_recurrent_1994}.
Here we show that wave-based physical systems can be trained to operate as an RNN, and as a result can \textit{passively} process signals and information in their native domain, without analog-to-digital conversion, which should result in a significant gain in speed and a reduction in power consumption.

An RNN converts a sequence of inputs into a sequence of outputs by applying the same basic operation to each member of the input sequence in a step-by-step fashion (Fig \ref{fig:RNN}A).
Memory of previous time steps is encoded into the RNN's \textit{hidden state}, which is updated at each step.
The information in the hidden state allows for the RNN to learn temporal structure and long-range dependencies in data \cite{elman1990finding, jordan1997serial}.
At a given time step, $t$, the RNN operates on the current input vector in the sequence, $\bvec{x}_t$, and the hidden state vector from the previous step, $\bvec{h}_{t-1}$, to produce an output vector, $\bvec{y}_t$, as well as an updated hidden state, $\bvec{h}_t$.
%
While many variations of RNNs exist, a common implementation \cite{goodfellow2016deep} is described by the following update equations
%
\begin{align}
    \bvec{h}_t &= \sigh \left(\Wh \cdot \bvec{h}_{t-1} + \Wx \cdot \bvec{x}_t \right)
    \label{eq:RNN1} \\
    \bvec{y}_t &= \sigy \left(\Wy \cdot \bvec{h}_t\right),
    \label{eq:RNN2}
\end{align}
%
which are diagrammed in Fig. \ref{fig:RNN}B.
As in the standard feedforward neural network case, the dense matrices defined by $\Wh$, $\Wx$, and $\Wy$ are optimized during training while $\sigh{\left(\cdot \right)}$ and $\sigy{\left(\cdot \right)}$ are fixed nonlinear activation functions.
The operation defined by Eq. \ref{eq:RNN1} and Eq. \ref{eq:RNN2}, when applied to each element of an input sequence, can be described by the directed graph shown in Fig. \ref{fig:RNN}C.

\begin{figure*}
  \centering
  \includegraphics[width=\textwidth]{figures/insitu_RNN}
  \caption{
  \textbf{Conceptual comparison of a standard recurrent neural network and a wave-based physcal system.}
  (\textbf{A})
  Diagram of a recurrent neural network (RNN) cell operating on a discrete input sequence and producing a discrete output sequence.
  (\textbf{B})
  Internal components of the RNN cell, consisting of trainable dense matrices $\Wh$, $\Wx$, and $\Wy$.
  Activation functions for the hidden state and output are represented by $\sigh$ and $\sigy$, respectively.
  (\textbf{C})
  Diagram of the directed graph of the RNN cell.
  (\textbf{D})
  Diagram of a recurrent representation of a continuous physical system operating on a continuous input sequence and producing a continuous output sequence.
  (\textbf{E})
  Internal components of the recurrence relation for the wave equation when discretized using finite differences.
  (\textbf{F})
  Diagram of the directed graph of discrete time steps of the continuous physical system.}
  \label{fig:RNN}
\end{figure*}

We now discuss the connection between the dynamics in the RNN as described by Eqs. \ref{eq:RNN1} and \ref{eq:RNN2}, and the dynamics of a wave-based physical system.
We explore the dynamics of a scalar field distribution, $u = u{\left(x,y,z,t\right)}$, as governed by the damped wave equation \cite{elmore_physics_2012}
%
\begin{equation}
    \frac{\partial^2{u}}{{\partial{t}}^2} +  2 b \cdot \frac{\partial{u}}{{\partial{t}}} = c^2 \cdot \nabla^2 u + f,
    \label{eq:wave_eq_damping}
\end{equation}
where $\nabla^2 = \frac{\partial^2}{{\partial{x}}^2} + \frac{\partial^2}{{\partial{y}}^2} + \frac{\partial^2}{{\partial{z}}^2}$ is the Laplacian operator.
$c = c{\left(x,y,z\right)}$ is the spatial distribution of the wave speed and $f = f{\left(x,y,z,t\right)}$ is a source term.
$b = c{\left(x,y,z\right)}$ is the dampening coefficient which is spatially varying but frequency-independent.
For a time step indexed by $t$, Eq. \ref{eq:wave_eq_damping} is discretized using \textit{centered} finite differences in time to give
\begin{align}
    \frac{u_{t+1} - 2u_t + u_{t-1}}{\Delta{t}^2} + 2b \frac{u_{t+1}-u_{t-1}}{2\Delta{t}} &= c^2 \nabla^2 u_t + f_t,
    \label{eq:wave_eq_damping_discretized}
\end{align}
where the subscript in $(\cdot)_t$ is used to indicate the value of a scalar field at time step $t$.

From Eq. \ref{eq:wave_eq_damping_discretized}, we may form a recurrence relation in terms of $u_{t+1}$, which leads to the following update equation
% \begin{widetext}
\begin{align}
    \left( \frac{1}{\Delta{t}^2} + \frac{b}{\Delta{t}}\right) u_{t+1} - \frac{2}{\Delta{t}^2} u_t  + \left( \frac{1}{\Delta{t}^2} - \frac{b}{\Delta{t}} \right) u_{t-1} = c^2 \cdot \nabla^2 u_t + f_t \nonumber \\
    \left( \frac{1}{\Delta{t}^2} + \frac{b}{\Delta{t}}\right) u_{t+1} = \frac{2}{\Delta{t}^2} u_t  - \left( \frac{1}{\Delta{t}^2} - \frac{b}{\Delta{t}} \right) u_{t-1} + c^2 \cdot\nabla^2 u_t + f_t \nonumber \\
    u_{t+1} = \left( \frac{1}{\Delta{t}^2} + \frac{b}{\Delta{t}}\right)^{-1} \left[ \frac{2}{\Delta{t}^2} u_t - \left( \frac{1}{\Delta{t}^2} - \frac{b}{\Delta{t}} \right) u_{t-1} + c^2\cdot \nabla^2 u_t + f_t \right].
    \label{eq:tmp1}
\end{align}
% \end{widetext}
Equation \ref{eq:tmp1} therefore represents the discretized update equation for the scalar wave equation with damping. In matrix form, we may express Eq. (\ref{eq:tmp1}) as
\begin{equation}
    \begin{bmatrix}
    u_{t+1} \\ u_t
    \end{bmatrix}
    =
    \begin{bmatrix}
    \frac{2 + \Delta t^2\cdot c^2 \cdot \nabla^2}{1 + \Delta t\cdot b}
    & \frac{-1 - \Delta t\cdot b}{1 + \Delta t\cdot b}  \\
    1 & 0
    \end{bmatrix}
    \cdot
    \begin{bmatrix}
    u_{t} \\ u_{t-1}
    \end{bmatrix}
    +
    \Delta{t}^2 \cdot \begin{bmatrix}
     f_{t} \\ 0
    \end{bmatrix}.
    \label{eq:update_damped}
\end{equation}

Nonlinearity may be introduced into the system by assuming an intensity-dependent wave speed of the form $c{(x,y)} = c_{\text{linear}} + c_{\text{nonlinear}} \cdot \vert u_t{(x,y)} \vert^2$, where $c_{\text{nonlinear}}$ is exhibited in regions containing nonlinear materials.
In practice, this class of nonlinearity is encountered in a variety of wave physics, such as shallow water waves \cite{ursell_long-wave_1953} or nonlinear optics via the Kerr effect \cite{Boyd__2008}.

To make the connection to the standard RNN more apparent, we define the wave system's \textit{hidden state} as the concatenation of the field distributions at the current and immediately preceding time steps, $\bvec{h}_t \equiv [\bvec{u}_t,~\bvec{u}_{t-1}]^T$, where $\bvec{u}_t$ and $\bvec{u}_{t-1}$ are vectors representing the \textit{flattened} fields, $u_t$ and $u_{t-1}$, discretized in the spatial domain.  With this, we may express the update of the wave equation from Eq. (\ref{eq:update_damped}) as
%
\begin{align}
\bvec{h}_t &= \bmat{A}{\left(\bvec{h}_{t-1}\right)} \cdot \bvec{h}_{t-1} + \Pin \cdot \bvec{x}_t \label{eq:sup_ScalarRNN1}\\
\bvec{y}_t &= \left\vert \Pout \cdot \bvec{h}_t \right\vert^2, \label{eq:sup_ScalarRNN2}
\end{align}
where we have defined $\bmat{A}$ as the matrix appearing in Eq. (\ref{eq:update_damped}) and its  dependence on $\bvec{h}_{t-1}$ is caused by the inclusion of the nonlinear wave speed as described above.

Like the standard RNN, the connections between the hidden state and the input and output of the wave equation are also defined by linear operators, given by $\Pin$ and $\Pout$.
These matrices define injection and measuring points within the spatial domain.
Unlike the standard RNN, where the input and output matrices are dense, the input and output matrices of the wave equation are usually sparse and, moreover, unchanged by the training process.
The nonlinear relationship between the hidden state, $\bvec{h}_t$, and the output, $\bvec{y}_t$, of the wave equation is typical in wave physics since the output usually corresponds to a measurement of the wave intensity.

To more explicitly define the input and output of this RNN, we introduce the linear operators, $\Min$ and $\Mout$, each column of which define the respective spatial distributions of the injection and measurement points in this flattened basis.
With this, we can write the injection of the input vector ($\bvec{x}_t$) as a matrix-vector multiplication
%
\begin{equation}
    \Delta t^2 \bvec{f}_t \equiv \Min \cdot \bvec{x}_t.
\end{equation}

Similarly, as the output of the RNN at each time step is given by an intensity measurement of the scalar fields, we may express this in terms of the flattened scalar field as
\begin{equation}
    \bvec{y}_t = |\Mout{}^T \cdot \bvec{u}_t|^2.
\end{equation}

As the wave equation \textit{hidden state}, $\bvec{h}_t$ is defined as the concatenation of $\bvec{u}_t$ and $\bvec{u}_{t-1}$, we define the following matrices for convenience, as they only act on the $\bvec{u}_t$ portion of $\bvec{h}_t$
\begin{align}
    \Pin &\equiv \begin{bmatrix} \Min \\ \bmat{0} \end{bmatrix} \\
    \Pout &\equiv [\Mout{}^T,~\bmat{0}],
\end{align}
where $\bmat{0}$ is a matrix of all zeros.
These matrices are used in the injection and measurement stages of the scalar wave update equations of the main text and thus serve a similar role to the $\Wx$ and $\Wy$ matrices of the traditional RNN in Eqs. (\ref{eq:RNN1}) and (\ref{eq:RNN2}).  However, unlike $\Wx$ and $\Wy$, these matrices are fixed and not trainable parameters.

When training the system, we take the wave speed distribution, $c{\left(x,y,z\right)}$ as design parameters, which are optimized for a given machine learning task, physically corresponding to a patterning of materials within the domain.
Thus, when modeled numerically in discrete time (Fig. \ref{fig:RNN}E), the wave equation defines an operation which maps into that of an RNN (Fig. \ref{fig:RNN}B).
Similarly to the RNN, the full time dynamics of the wave equation may be represented as a directed graph (Fig. \ref{fig:RNN}F).

\begin{figure*}
  \centering
  \includegraphics{figures/insitu_RNN_train}
  \caption{
  \textbf{Schematic of the vowel recognition system and the training procedure.}
  (\textbf{A})
  Raw audio waveforms of spoken vowel samples from three classes.
  (\textbf{B})
  Layout of the vowel recognition system. Vowel samples are independently injected at the source, located at the left of the domain, and propagate through the center region, indicated in green, where a material distribution is optimized during training. The dark gray region represents an absorbing boundary layer.
  (\textbf{C})
  For classification, the time-integrated power at each probe is measured and normalized to be interpreted as a probability distribution over the vowel classes.
  (\textbf{D})
  Using automatic differentiation, the gradient of the loss function with respect to the density of material in the green region is computed. The material density is updated iteratively, using gradient-based stochastic optimization techniques, until convergence.
  }
  \label{fig:train}
\end{figure*}

\section{Vowel Recognition Demonstration}

We now demonstrate how the dynamics of the wave equation can be trained to classify vowels through the construction of an inhomogeneous material distribution.
For this task, we utilize a dataset consisting of 930 raw audio recordings of 10 vowel classes from 45 different male speakers and 48 different female speakers \cite{hillenbrand_acoustic_1995}.
For our learning task, we select a subset of 279 recordings corresponding to three vowel classes, represented by the vowel sounds \textit{ae}, \textit{ei}, and \textit{iy}, as contained in the words h\textit{a}d, h\textit{aye}d, and h\textit{ee}d, respectively (Fig. \ref{fig:train}A).
Before injection into the system, each vowel waveform is downsampled from its original recording with a 16 kHz sampling rate to a sampling rate of 10 kHz.

Our goal is to design a system such that when we inject the raw audio corresponding to each vowel (Fig. \ref{fig:train}A), the power is successfully directed to one of three measuring points.
The physical layout of the vowel recognition system consists of a two-dimensional domain in the $x$-$y$ plane, infinitely extended along the $z$-direction (Fig. \ref{fig:train}B).
The audio waveform of each vowel, represented by $\bvec{x}^{(i)}$, is injected by a source at a single grid cell on the left side of the domain, emitting waveforms which propagate through a central region with a trainable distribution of the wave speed, indicated by the light green region in Fig. \ref{fig:train}B.
An absorbing region is introduced to approximate an open boundary condition \cite{oskooi_failure_2008}, corresponding to the grey region in Fig. \ref{fig:train}B.
This region is defined by a dampening coefficient, $b{(x,y)}$, which has a cubic dependence on the distance from the interior boundary of the layer.
The precense of absorption prevents energy from building up inside the computational domain.
Three probe points are defined on the right hand side of this region, each assigned to one of the three vowel classes.
To determine the system's output, $\bvec{y}^{(i)}$, the time-integrated power at each probe is measured (Fig. \ref{fig:train}C).
After the simulation evolves for the full duration of the vowel recording, this integral gives a non-negative vector of length 3, which is then normalized by its sum and interpreted as the system's predicted probability distribution over the vowel classes.

For the purposes of our numerical demonstration, we consider binarized systems consisting of two materials: a background material with a normalized wave speed $c_0 = 1.0$, and a second material with $c_1 = 0.5$.
We assume that the second material has a nonlinear parameter, $c_{\text{nonlinear}} = -30$, while the background material has a linear response.
In practice, the wave speeds would be modified to correspond to different materials being used.
For example, in an acoustic setting the material distribution could consist of air, where the sound speed is 331 m/s, and porous silicone rubber, where the sound speed is 150 m/s \cite{ba_soft_2017}.
The initial distribution of the wave speed consists of a uniform region of material with a speed which is midway between those of the two materials (Fig. \ref{fig:train}D).

To create realistic devices with sufficiently large minimum feature sizes and binarized materials, we utilized spatial filtering and thresholding schemes.  A material density parameter $\rho(x,y)$ is defined for the structure, which may vary from 0 to 1.  Then, we apply a low pass spatial filter to $\rho(x,y)$, which gives a filtered density, labeled $\tilde{\rho}(x,y)$
%
% \begin{equation}
% \tilde{\rho}(x,y) =
% \begin{bmatrix}
% 0.000 & 0.125 & 0.000 \\
% 0.125 & 0.500 & 0.125 \\
% 0.000 & 0.125 & 0.000
% \end{bmatrix}
% *
% \rho(x,y),
% \end{equation}
\begin{equation}
\tilde{\rho}(x,y) =
\begin{bmatrix}
0 & 1/8 & 0 \\
1/8 & 1/2 & 1/8 \\
0 & 1/8 & 0
\end{bmatrix}
*
\rho(x,y),
\end{equation}
where $*$ is a spatial convolution operation.
For binarization of the resulting structure, a projection scheme is used to recreate the final wave speed from the filtered density.  We define $\bar{\rho}(x,y)$ as the projected density, which is created from $\tilde{\rho}(x,y)$ as
\begin{equation}
\bar{\rho}_i = \frac{\tanh{\left( \beta \eta \right)} + \tanh{\left( \beta \left[ \tilde{\rho}_i - \eta \right] \right)}}{\tanh{\left( \beta \eta  \right)} + \tanh{\left( \beta \left[ 1 - \eta \right] \right)}}.
\end{equation}
Here, $\eta$ is a parameter between 0 and 1 that controls the mid-point of the projection, typically 0.5, and $\beta$ controls the strength of the projection, typically around 100.  The wave speed can be determined from $\bar{\rho}$ as
\begin{equation}
c(x,y) = (c_1(x,y) - c_0(x,y))\bar{\rho} + c_0(x,y),
\end{equation}
where $c_0$ and $c_1$ are the background and optimized material wave speed, respectively.

%

To train the system, we perform backpropagation through the model of the wave equation to compute the gradient of the cross entropy loss function of the measured outputs with respect to the density of material in each pixel of the trainable region.
As we discussed to in the previous Chapter, this process is mathematically equivalent performing the adjoint method.
Then, we use this gradient information to update the material density until convergence on a final structure (Fig. \ref{fig:train}D).

Numerical modeling and simulation of the wave equation physics was performed using a custom software package written in Python \cite{wavetorch}.
The software package was developed on top of the popular machine learning library, \texttt{pytorch}, to compute the gradients of the loss function with respect to the material distribution via reverse-mode automatic differentiation.
Using of a machine learning platform to perform the numerical simulation greatly reduces opportunities for errors in the analytic derivation or numerical implementation of the gradient.

All windowed samples from the training set are run through the simulation in batches of 9 and the categorical cross entropy loss between the output probe probability distribution and the correct one-hot vector for each vowel sample is computed.
To encourage the optimizer to produce a binarized distribution of the wave speed with relatively large feature sizes, the optimizer minimizes this loss function with respect to a material density distribution, $\rho{\left(x,y\right)}$ within a central region of the simulation domain, indicated by the green region in Fig. \ref{fig:train}B.
We use the Adam algorithm \cite{kingma_adam_2014} with learning rate 0.0004 to perform the optimization in batches of 9.
Fig. \ref{fig:train}D illustrates the optimization process over several epochs, during which, the wave velocity distribution converges to a final structure.
At the end of each epoch, the classification accuracy is computed over both the testing and training set.
Unlike the training set, the full length of each vowel sample from the testing set is used.

The mean energy spectrum of the three vowel classes after downsampling to 10 kHz is shown in Fig. \ref{fig:s_spectrum}.
We observe that the majority of the energy for all vowel classes is below 1 kHz and that there is strong overlap between the mean peak energy of the \textit{ei} and \textit{iy} vowel classes.
Moreover, the mean peak energy of the \textit{ae} vowel class is very close to the peak energy of the other two vowels.
Therefore, the vowel recognition task learned by the system in the main text is non-trivial.

\begin{figure*}[t]
  \centering
  \includegraphics{figures/insitu_spectrum}
  \caption{Mean energy spectrum for the \textit{ae}, \textit{ei}, and \textit{iy} vowel classes.}
  \label{fig:s_spectrum}
\end{figure*}

To validate the performance of our system, the full dataset of (3 classes) $\times$ (45 males + 48 females) = 272 vowel samples is divided into 5 groups of approximately equal size.
Cross validated training is performed with 4 out of the 5 sample groups forming a training set and 1 out of the 5 sample groups forming a testing set.
Independent training runs are performed with each of the 5 groups serving as the testing set, with results being averaged over all training runs.
During each epoch, every sample vowel sequence from the training set is windowed to a length of 1000, taken from the center of the sequence, but the testing is performed on the full sequence.

% 4. What are the limitations?
% One major limitation of our approach is that it relies on numerical simulation for training, which may be an issue when the simulation size becomes too complex to model feasibly on a digital computer.
% Furthermore, the presence of chaos or high sensitivity to inputs and initial conditions in the system may introduce significant differences between the numerical simulation and the physical realization, meaning that the digitally trained device may not perform as expected when fabricated and introduced to physical signals.
% However, this issue may be mitigated by utilizing the previously mentioned techniques from the field of inverse design, such as fabrication or sensitivity constraints \cite{wang2011robust, piggott2017fabrication}.

% \section{Binarization and Filtering of Wave Speed \label{appx:fab}}

The confusion matrices over the training and testing sets for the starting structure are shown in Fig. \ref{fig:results}A and Fig. \ref{fig:results}B, averaged over five cross-validated training runs.
Here, the confusion matrix defines the percentage of correctly predicted vowels along its diagonal entries and the percentage of incorrectly predicted vowels for each class in its off-diagonal entries.
Clearly the starting structure can not perform the recognition task.
Fig. \ref{fig:results}C and Fig. \ref{fig:results}D show the final confusion matrices after optimization for the testing and training sets, averaged over five cross validated training runs.
The trained confusion matrices are diagonally dominant, indicating that the structure can indeed perform vowel recognition.

Fig. \ref{fig:results}E and Fig. \ref{fig:results}F show the cross entropy loss value and the prediction accuracy, respectively, as a function of the training epoch over the testing and training datasets, where the solid line indicates the mean and the shaded region corresponds to the standard deviation over the cross-validated training runs.
Interestingly, we observe that the first epoch results in the largest reduction of the loss function and the largest gain in prediction accuracy.
From Fig. \ref{fig:results}F we see that the system obtains a mean accuracy of 92.6\% $\pm$ 1.1\% over the training dataset and a mean accuracy of 86.3\% $\pm$ 4.3\% over the testing dataset.
From Fig. \ref{fig:results}C and Fig. \ref{fig:results}D we observe that the system attains near perfect prediction performance on the \textit{ae} vowel and is able to differentiate the \textit{iy} vowel from the \textit{ei} vowel, but with less accuracy, especially in unseen samples from the testing dataset.
Fig. \ref{fig:results}G, Fig. \ref{fig:results}H, and Fig. \ref{fig:results}I show the distribution of the integrated field intensity, $\sum_t{\left\vert u_t{\left(x,y\right)} \right\vert^2}$, when a representative sample from each vowel class is injected into the trained structure.
We thus provide visual confirmation that the optimization procedure produces a structure which routes the majority of the signal energy to the correct probe.
As a performance benchmark, a conventional RNN was trained on the same task, achieving comparable classification accuracy to that of the wave equation. However, a larger number of free parameters was required.
Additionally, we observed that a comparable classification accuracy was obtained when training a linear wave equation.

\begin{figure*}
  \centering
  \includegraphics[width=\textwidth]{figures/insitu_RNN_results}
  \caption{\textbf{Vowel recognition system training results.}
  Confusion matrix over the training and testing datasets for the initial structure (\textbf{A}),(\textbf{B}) and final structure (\textbf{C}),(\textbf{D}), indicating the percentage or correct (diagonal) and incorrect (off-diagonal).
  Cross validated training results showing the mean (sold line) and standard deviation (shaded region) of the (\textbf{E}) cross entropy loss and (\textbf{F}) prediction accuracy over 20 training epochs and 5 folds of the dataset, which consists of a total of 272 total vowel samples of male and female speakers.
  (\textbf{G})-(\textbf{I}) The time-integrated intensity distribution for a randomly selected input (\textbf{G}) \textit{ae} vowel, (\textbf{H}) \textit{ei} vowel, and (\textbf{I}) \textit{iy} vowel.}
  \label{fig:results}
\end{figure*}

We compare the wave equation results to a conventional RNN model as defined in Eq. (\ref{eq:RNN1}) and Eq. (\ref{eq:RNN2}).
The number of trainable parameters in the model is determined by the hidden state size $N_h$, as the model is given by three matrices $\Wx, \Wh$, and $\Wy$ of size $[N_h, 1]$, $[N_h, N_h]$ and $[3, N_h]$, respectively.
We tried $N_h = $70, for which the total number of RNN free parameters is 5250, and $N_h=$100, with 10500 free parameters.
The RNN was implemented and trained using \texttt{pytorch}.
In Table \ref{tab:final_table} we show the results of a standard RNN on the vowel recognition task and compare them to the scalar wave.
We find that the conventional RNN achieves a performance comparable to the wave equation.
However, this performance is highly dependent on the number of trainable parameters.
For a similar number of trainable parameters, the conventional RNN achieves about 6\% lower classification accuracy.
However, when the number of free parameters is increased to about 2 times that of the scalar wave, the accuracy is higher by about 3 \%.
We note that it is possible that more advanced recurrent models like long short-term memory (LSTM) \cite{hochreiter1997long} or gated recurrent unit (GRU) \cite{chung2014empirical} could have a better performance with a smaller number of parameters, but exploring this is outside the scope of this study.

\begin{table*}[t]
    \centering
    \setlength{\tabcolsep}{6pt}
    \renewcommand{\arraystretch}{1.25}
    \begin{tabular}{llccccc}
        \hline\hline
        \textbf{Model} & \textbf{Nonlinearity} & \textbf{\# parameters} & \multicolumn{2}{c}{\textbf{Accuracy}} \\
        & & & Training & Testing \\
        \hline
        \textbf{Wave Equation} & linear wave speed & 4200 & 93.1\% & 86.6\% \\
          & nonlinear wave speed & 4200 & 92.6\% & 86.3\% \\
%       \hline
        \hline
        \textbf{Conventional RNN} & linear & 5250 & 78.8\% & 79.4\% \\
        & leaky ReLU & 5250 & 82.6\% & 80.2\% \\
%       \hline
        & linear & 10500  & 88.9\% & 88.2\% \\
        & leaky ReLU     & 10500 & 89.4\% & 89.4\% \\
        \hline\hline
    \end{tabular}
    \caption{Comparison of scalar wave model and conventional RNN on vowel recognition task.}
    \label{tab:final_table}
\end{table*}

\section{Comparison of Analog and Digital Implementations}

The conventional RNN and the one implemented by a scalar wave equation have many qualitative differences.
We discuss some of those below.
First, in the RNN case, the trainable parameters are given by the elements of the weight matrices.
In the wave equation case, we choose to use the wave velocity, $c(x,y,z)$, as trainable parameters, because a specific distribution of $c$ can be physically implemented after the training process.
In acoustic or optical systems, this can be practically realized using technologies such as 3D printing or nanolithography.
Furthermore, whereas the RNN free parameters define a matrix which is multiplied by the input, output, and hidden state vectors, in the wave equation case, the free parameters are multiplied element-wise with the hidden state, which limits the influence of each individual parameter over the full dynamics.

% \subsection{Length and Time Scale}

For a given amount of expressive power, the size of the hidden state in the wave equation must arguably be much larger than that in the RNN case.  This is because the amount of information that can be encoded in the spatial distribution of $u_t$ is constrained by the diffraction limit for wave systems.  It follows that a single RNN hidden state element may be analogous to several grid cells in the scalar wave equation. Furthermore, the wave update matrix $A$ is sparse and only contains non-zeros on diagonal elements (self coupling) and those corresponding to neighbor-to-neighbor coupling between spatial grid cells.  Because of this, information in a given cell of $u_t$ will take several time steps to reach other cells, as determined by the wave velocity and the distance between them.  The presence of this form of causality practically means that one must wait longer for a full `mixing' of information between cells in the domain, suggesting that in the our numerical simulations, a larger number of time steps may be needed as compared to the typical RNN.

% \subsection{Nonlinearities}

The form of nonlinearity used in the wave equation is different from that in the typical RNN, which involves the application of the nonlinear function, $\sigh(\cdot)$, as in Eq. (\ref{eq:RNN1}).  In the wave equation, nonlinearity is provided by making the wave velocity, $c$, or damping dependent, $b$, to be dependent on the instantaneous wave intensity $|u_t|^2$.  For example $c = c(|u_t|^2)$, or $b = b(|u_t|^2)$.  In optics, these nonlinearities may be implemented using $\chi^{(3)}$ materials or saturable absorption, respectively.  With this addition, the update matrix of Eq. (\ref{eq:sup_ScalarRNN1}), $\bmat{A} = \bmat{A}(\bvec{h}_{t-1})$, becomes a function of the solution at that time step, making the dynamics nonlinear.  Nonlinearity is introduced into the  output of the wave system ($\bvec{y}_t$) by measuring the intensity of the wave field, which involves a squaring operation.  One may also consider directly discretizing a nonlinear wave equation using the same technique.

The wave-based RNN presented here has a number of favorable qualities that make it a good candidate for processing sequential data.
Unlike the standard RNN, the update of the wave equation from one time step to the next enforces a nearest-neighbor coupling between elements of the hidden state through the Laplacian operator, which is represented by a sparse matrix in Fig. \ref{fig:RNN}E.
This is a direct consequence of the fact that the wave equation is a hyperbolic partial differential equation in which information propagates with a finite velocity.
Thus, the size of the analog RNN's hidden state, and therefore its memory capacity, is directly determined by the size of the propagation medium.
Additionally, unlike the conventional RNN, the wave equation enforces an energy conservation constraint, preventing unbounded growth of the norm of the hidden state and the output signal.
In contrast, the unconstrained dense matrices defining the update relationship of the standard RNN lead to vanishing and exploding gradients, which pose a major challenge for training traditional RNNs \cite{jing2017tunable}.

In this chapter, we have shown that the dynamics of the wave equation are conceptually equivalent to those of a recurrent neural network.
This conceptual connection opens up the opportunity for a new class of analog hardware platform, in which evolving time dynamics play a significant role in both the physics and the dataset.
% Our results also demonstrate that inverse design techniques are effective at training physical systems on machine learning tasks.
% In particular, we demonstrated that, on a vowel classification problem, train and test accuracies of \result{93}\% and \result{86}\% can be attained, which are comparable to those from a standard RNN with a comparable number of free parameters.
% More broadly, our results point toward several exciting directions in analog computing for machine learning.
While we have focused on a specific example of the scalar wave equation, our results apply broadly to other wave-like physics.
Such an approach of using physics to perform computation \cite{silva_performing_2014, Hermans2015, guo_photonic_2018, lin2018all, kwon_nonlocal_2018, estakhri_inverse-designed_2019} may inspire a new platform for analog machine learning devices, with the potential to perform computation far more naturally and efficiently than their digital counterparts.
The generality of the approach further suggests that many physical systems may be attractive candidates for performing RNN-like computation on naturally occurring sequences, such as optical, acoustic, or seismic signals.


% !TeX root = ../main.tex
\section{Narrowing}

In this section, we discuss the use of narrowing techniques on solving equations over an equational theory $E$. By solving an equation we mean finding a substitution $\sigma$ such that $s \sigma \eqTh{E} t\sigma$. These solutions can be found by unification \textit{modulo} $E$ (if such a unification algorithm exists for this theory), but here we are interested in applications of rewriting theory to solving such equations.

We start the presentation by the so-called syntactic unification, i.e. $E$-unification with empty $E$. The transformation rules given below is due to Martelli and Montanari \cite{DBLP:books/el/RV01/BaaderS01}, the idea is to transform sets of equations to other sets of equations until a termination state is reached; that is, a solution state or a failure one.

\begin{definition}
	Let $\signature$ be a signature. An equational goal is a finite set of $\signature$-equations.
\end{definition}

\begin{table}[ht]
	\caption{Martelli-Montanari rules}
	\label{table:unify_inf_rules}
	{\small
		\hrule
		\vspace{10pt}

		\textbf{(1) Trivial}
        $$\{ \ueq{x}{x} \} \cup G \implies G $$

		\textbf{(2) Decompose}
		$$\{\ueq{f(s_1, \cdots, s_n)}{f(t_1, \dots, t_n)}\} \cup G \implies \{\ueq{s_1}{t_1}, \dots , \ueq{s_n}{t_n} \} \cup G$$

		\textbf{(3) Symbol Clash}
		$$\{ \ueq{f(s_1, \dots, s_n)}{g(t_1, \dots, t_n)} \} \cup G \implies \bot \; \text{if} \; f \neq g $$

		\textbf{(4) Orient}
		$$\{ \ueq{t}{x} \} \cup G \implies \{ \ueq{x}{t} \} \cup G \; \text{if} \; t \notin V$$

		\textbf{(5) Occurs Check}
		$$\{ \ueq{x}{t} \} \cup G \implies \bot \text{ if } x \in \vars{t} \text{ and } x \neq t$$

		\textbf{(6) Variable Elimination}
		$$\{ \ueq{x}{t} \} \cup G \implies G\{x \mapsto t\} \text{ if } x \notin \vars{t}$$
		\hrule
	}
\end{table}

The application of the above rules non-deterministically transforms goals into goals:
$$G_0 \transRel \cdots \transRel G_n$$
Each application of a rule will then called a \textit{elementary derivation step}. As for the case of (Variable Elimination), we may get some substitution on the way, we make them explicit by writing:
$$G_0 \transRel G_1 \transRel_{\sigma_1} G_2 \transRel_{\sigma_2} \cdots \transRel_{\sigma_i} \cdots \transRel_{\sigma_{n-1}} G_n$$
The computed solution of the derivation chain is then the composition of such substitutions in their order of appearance.

\begin{definition}
	A \textit{successful} derivation chain is a finite sequence of equational goals $G_0,G_1,\dots,G_n$ such that the last goal is empty. We also say a derivation chain has \textit{failed} if it last goal is the fail symbol $\bot$.
\end{definition}

\begin{example}\label{example:unif-examples}
	\begin{enumerate}
		\item We want to determine an mgu of the terms $f(g(x),h(x,u))$ and $f(z,h(f(y,y),z))$. That is, solving the equational goal:
		      \begin{align*}
			      \{ f(g(x),h(x,u)) & = f(z,h(f(y,y),z)) \}                                           \\
			                        & \overset{(2)}{\transRel} \{g(x) = z, h(x,u) = h(f(y,y),z)\}     \\
			                        & \overset{(4)}{\transRel} \{z = g(x), h(x,u) = h(f(y,y),z)\}     \\
			                        & \overset{(6)}{\transRel}_{[z/g(x)]} \{h(x,u) = h(f(y,y),g(x))\} \\
			                        & \overset{(2)}{\transRel} \{x = f(y,y), u = g(x) \}              \\
			                        & \overset{(6)}{\transRel}_{[x/f(y,y)]} \{ u = g(f(y,y)) \}       \\
			                        & \overset{(6)}{\transRel}_{[u/f(y,y)]} \emptyset
		      \end{align*}
		      We get as the computed solution to the problem the composition $[z/g(x)][x/f(y,y)][u/g(f(y,y))]$.

		\item A failing unification derivation:
		      \begin{align*}
			      \{ h(x,y,x) & = h(y,g(x),x)\}                                     \\
			                  & \overset{(2)}{\transRel} \{x = y, x = g(x), x = x\} \\
			                  & \overset{(5)}{\transRel} \bot
		      \end{align*}
	\end{enumerate}
\end{example}

It can be proved (see \cite{DBLP:books/el/RV01/BaaderS01}) that this set of rules derive a correct and terminating procedure for syntactic unification.

\begin{definition}\label{definition:narrow}
	Say a term $s$ \textit{narrows} to a term $t$ if there exists a non-variable position $p \in \pos{s}$, a variant $l \rightarrow r$ of a rewrite rule in $\trs$, and a substitution $\sigma$ satisfying two conditions:
	\begin{enumerate}
		\item $\sigma$ is a \textit{mgu} of $\restr{s}{p}$ and $l$,
		\item $t = \left( s[r]_{p} \right)\sigma$.
	\end{enumerate}
\end{definition}

The relation $\narrow$ is called \textit{narrowing relation}. We write $s \narrow_{\sigma}^{*} t$ if there exists a narrowing derivation
$$s = t_1 \narrow_{\sigma_1} t_2 \narrow_{\sigma_2} t_3 \narrow \cdots \narrow_{\sigma_{n-1}}t_n = t$$
and $\sigma$ is given by $\sigma := \sigma_1\sigma_2 \cdots \sigma_{n-1}$, the computed solution of the above narrowing derivation.

\begin{remark}
	Renaming of rewrite rules will be mandatory to ensure completeness of the narrowing approach. We always use a simple renaming such that $\vars{l} \cap \vars{s} = \emptyset$. This also extend to chains of narrowing derivations.
\end{remark}

In a rewrite step $s \contr_[p, l \contr r, \sigma] t$ we always assume that the applied rewrite rule has no variables in common with $s$ and $\sigma$ is restricted to variables occurring in $l$. Consequently, $\sigma$ is a most general unifier of $\restr{s}{p}$ and $l$, and $t = s[r\sigma]_p = (s[r]_p)\sigma$. Hence rewriting can be viewed as a special case of narrowing.

Narrowing was first introduced in the context of $E$-unification. Fay \cite{fay1978first} and Hullot \cite{hullot:cfunif} shows that narrowing is a complete method for solving equations in the theory defined by a confluent and terminating term rewriting system. In fact, narrowing derive from paramodulation (a first attempt for solving equations in arbitrary equational theories), but with the requirement that the theory can be represented as a convergent rewrite system. This approach also shows an important application for the Knuth-Bendix completion procedure: it prepares the way for solving equations over $E$, by delivering a complete TRS for $E$ (if possible).

For this set of technology we give the name \textit{narrowing}, we now present the first application (of solving equations modulo $E$) in the framework of transformation rules on sets of equational goals.

Consider an equational theory $E$ specified by a convergent rewrite system $\trs$, which is called the equational specification of $E$. The rules below extend the Martelli-Montanari unification on syntax terms to solve equations modulo $E$.
\begin{table}[ht]
	\caption{Narrowing rules}
	\label{table:narrowing_inf_rules}
	{\small
		\hrule
		\vspace{10pt}

		\textbf{(7) Left Narrowing} if $s \narrow_{\sigma} s'$
		$$\{ s = t \} \cup G \implies_{\sigma} \{ s' = t\sigma \} \cup G\sigma $$

		\textbf{(8) Right Narrowing} if $t \narrow_{\sigma} t'$
		$$\{ s = t \} \cup G \implies_{\sigma} \{ s\sigma = t' \} \cup G\sigma $$
		\hrule
	}
\end{table}

\begin{definition}
    A \textit{narrowing derivation} is a finite set of equational goals such that each step is obtained by using the rules (1)-(8). It is a successful narrowing derivation if it last goal is empty.
\end{definition}

\begin{example}
	Let $\trs = \{ g(a) \rightarrow a \}$ and consider the failing unification attempt of Example \ref{example:unif-examples}-(2). Observe that $h(y,g(x),x) \narrow_{[x / a]} h(y,a,a)$. Thus by the above rules:
	\begin{align*}
		\{ h(x,y,x) = h(y,g(x),x) \} & \overset{(8)}{\implies}_{[x/A]} \{ h(a,y,a) = h(y,a,a) \} \\
		                             & \overset{(2)}{\implies} \{ a = y, y = a, a = a \}         \\
		                             & \overset{(4,1)}{\implies} \{ y = a\}                      \\
		                             & \overset{(8)}{\implies}_{[x/A]} \{  a = a \}              \\
		                             & \overset{(1)}{\implies} \emptyset
	\end{align*}
	The equational problem is now a successful narrowing derivation with computed answer substitution $\sigma = [x / a, y / a]$.
\end{example}

In order to solve an equation $\ueq{s}{t}$ in an equational theory, corresponding to such a TRS, one can construct all possible narrowing derivations starting from the given equational goal until an equation $\ueq{s'}{t'}$ is obtained such that $s'$ and $t'$ are indeed syntactically unifiable. Note that, if this equational goal has a solution one always get a last equation of the form $\ueq{s}{s}$. We now investigate the semantic of solving equations using narrowing techniques.

If $(\signature, E)$ is an equational theory, write $[s = t]_E$ for the set of all solutions to the equation $s = t$ modulo $E$, i.e $\{\gamma \mid E \vdash s\gamma = t \gamma \}$. Moreover, if $X$ is some set of substitutions, let $\sigma X$ be the set $\{ \sigma \mid \gamma \in X \}$.

By definition, solutions of the equation $s \eqUnif{E}{?}t$ are indeed closed under any ``variable assignment'', that is, they are closed under applications of substitutions. As explained in \cite{10.1007/BFb0032044} -- Section 2.4; for every substitution $\sigma$ we have $[s = t]_E \supseteq \{ \sigma \tau \mid E \vdash s\sigma \tau = t \sigma \tau  \} = \sigma [s\sigma = t \sigma]_E$, there is in principle the possibility of a step wise determination of $[s = t]_E$ by means of ``specialisations'' of the solution set. It consists of two kind of steps. The first one is as just described guess a component $\sigma$ of the solution set and narrows $[s = t ]_E$ to $\sigma[s = t]_E$. The other one is: apply one equation of $E$ in one of the sides of the equation $s = t$ in hand. It is clear that this two types of steps do not change the solution set of the equation $s \eqUnif{E}{?} t$. In this way, by iterating steps of the first and second type, we can finally get an equation that is syntactically unifiable, as described above.

The narrowing relation was defined on terms rather equational goals. They act upon goals by the means of the above transformation rules.

We now turn our attention on completeness of narrowing. The propositions below will be useful to prove the various versions of ``Lifting Lemmas'' of this study.

\begin{proposition}\label{proposition:varst}
	If $t$ is a term and $\gamma$ a substitution then $\vars{t\gamma} = (\vars{t} \setminus \dom{\gamma}) \cup \vran{\restr{\gamma}{\vars{t}}}$.
\end{proposition}

\begin{proposition}\label{proposition:change-of-domain}
	Suppose we have substitutions $\gamma, \theta, \theta'$ and sets $A,B$ of variables such that $(B \setminus \dom{\gamma}) \cup \vran{\gamma} \subseteq A$. If $\theta =^{A} \theta'$ then $\gamma\theta =^{B} \gamma\theta'$.
\end{proposition}

\begin{proposition}\label{proposition:change-of-domain-normalized}
	Let $\trs$ be a TRS and suppose we have sets $A,B$ of variables and substitutions $\gamma, \theta, \theta'$ such that the following conditions are satisfied:
	\begin{enumerate}
		\item $\restr{\theta}{A}$ is $\trs$-normalised,
		\item $\theta =^A \gamma \theta'$,
		\item $B \subseteq (A \setminus \dom{\gamma}) \cup \vran{\restr{\gamma}{A}}$
	\end{enumerate}
	Then $\restr{\theta'}{B}$ is also normalised.
	\begin{proof}
		Let $x \in B$. We have to show that $x\theta'$ is an $\trs$-normal form. If $x \in A \setminus \dom{\gamma}$ then $x\theta' = x(\gamma\theta') = x\theta$ which is an $\trs$-normal form by the first condition. If $x \in \vran{\restr{\gamma}{A}}$ then there exists a variable $y \in A$ such that $x \in \vars{y\gamma}$. Also, by condition (2), we have $x\theta' \leq (y\gamma)\theta' = y\theta$. By condition (1) $y\theta$ is an $\trs$-normal form and hence its subterm $x\theta'$ is also an $\trs$-normal form.
	\end{proof}
\end{proposition}

\begin{lemma}[Lifting Lemma]\label{lemma:lifting-lemma-ordinary-narrowing}
	Let $\trs$ be a TRS. Suppose we have terms $s$ and $t$, a normalised substitution $\theta$ and a finite set of variables $V$ such that $\vars{s} \cup \dom{\theta} \subseteq V$ and $t = s\theta$. If $t \stc t'$ then there exist a term $s'$ and substitutions $\theta', \gamma$ such that:
	\begin{enumerate}
		\item $s \narrow^{*}_{\gamma} s'$,
		\item $t' = s'\theta'$,
		\item $\theta =^V \gamma\theta'$,
		\item $\theta'$ is $\trs$-normalised.
	\end{enumerate}
	Furthermore, we may assume that the narrowing derivation $s \narrow^{*}_{\sigma} s'$ and the rewrite sequence $t \stc t'$ employ the same rules at the same positions.

	\begin{proof}
		The proof is by induction on the length of the reduction sequence from $t$ to $t'$. If $t = t'$, a reduction of length zero, then the result clearly follows. Suppose $t \contr t_1 \stc t'$ is a reduction sequence of length $n+1$.

		\begin{figure}[h!]
			\begin{displaymath}
				\xymatrix{
                    s \ar@{~>}[r]_>>{\gamma_1} & s_1 \ar@{}[dr]|{I.H} \ar@{~>>}[r]_>>{\gamma'}^>>{*} & s' \\
                    t = s\theta \ar@{-->}[u] \ar@{->>}[r]_>>{\trs} & \ar@{-->}[u] t_1 \ar@{->>}[r]_>>{\trs}^>>{*} & t' = s'\theta' \ar@{-->}[u]
				}
			\end{displaymath}
			\caption{Lifting Lemma}
			\label{figure:lifting-lemma}
        \end{figure}

		Let $t$ contract to $t_1$ using a position $p \in \pos{t}$ and a variant $l \rightarrow r$ of a rule from $\trs$ such that $\vars{l} \cap V = \emptyset$. We use the fact that $t = s\theta$ to write $\restr{(s\theta)}{p} = l \tau$ for some substitution $\tau$ with $\dom{\tau} \subseteq \vars{l}$. Since $\theta$ is normalised we have $p$ is a non-variable position and hence $\restr{(s\theta)}{p} = (\restr{s}{p})\theta$. Let $\gamma = \tau \cup \theta$ so $(\restr{s}{p})\gamma = (\restr{s}{p})\theta = l \tau$ then $\restr{s}{p}$ and $l$ are unifiable. Consider $\gamma_1$ as an idempotent mgu of $\restr{s}{p}$ and $l$. By Lemma \ref{lemma:unifiers-preserve-variables} $\dom{\gamma_1} \cup \vran{\gamma_1} = \vars{\restr{s}{p}} \cup \vars{l}$. Let $s_1 = (s[r]_p)\gamma_1$. By definition \ref{definition:narrow},
		\begin{equation}\label{lifting-lemma:eqn:s-narrows-s_1}
			s \narrow_{\gamma_1} s_1
		\end{equation}

		Since $\gamma_1 \leq \gamma$, there exists a substitution $\rho$ such that $\gamma = \gamma_1 \rho$. Let $V_1 = (V \setminus \dom{\gamma_1}) \cup \vran{\gamma_1}$ and define $\theta_1 = \restr{\rho}{V_1}$. Clearly $\dom{\theta_1} \subseteq V_1$. Also,
		\begin{align*}
			\vars{s_1} & = \vars{s[r]_p \gamma_1}                                                       \\
			           & \subseteq \vars{s[l]_p \gamma_1}                                               \\
			           & = \vars{s \gamma_1} \text{, by Proposition \ref{proposition:change-of-domain}} \\
			           & \subseteq V_1.
		\end{align*}
		Therefore, $\vars{s_1} \cup \dom{\theta_1} \subseteq V_1$.

		Using the equality $\theta_1 =^{V_1} \rho$ we obtain
		\begin{align*}
			s_1 \theta_1 = s_1\rho & = ( (s[r]_p)\gamma_1)\rho \\
			                       & = (s[r]_p)\gamma          \\
			                       & = (s\gamma)[r\gamma]_p
		\end{align*}
		and since $V \cap \dom{\tau} = \emptyset$ (remember that $\vars{l} \cap V = \emptyset$ and $\dom{\tau} \subseteq \vars{l})$, one have $\gamma =^{V} \theta_1$. Likewise $\gamma =^{\vars{r}} \tau$. Hence the term $(s\gamma)[r\gamma]_p$ is equal to $(s\theta)[r\theta]_p = t_1$. Thus
		\begin{equation}\label{lifting-lemma:eqn:t_1-instance-s_1}
			t_1 = s_1\theta_1
		\end{equation}
		Next we show that $\gamma_1\theta_1 =^V \theta$. Proposition \ref{proposition:change-of-domain} yields $\gamma_1\theta_1 =^V \gamma_1\rho$. Since $\gamma =^V \theta$ and using the equality $\gamma = \gamma_1\rho$ we have
		\begin{align}
			\gamma_1 \theta_1 & =^V \gamma_1 \rho \nonumber                            \\
			                  & =^V \gamma \nonumber                                   \\
			                  & =^V \label{lifting-lemma:gamma1-instance-theta} \theta
		\end{align}

		Finally we show that $\theta_1$ is normalised. Since $\dom{\theta_1} \subseteq V_1$ if suffices to show that $\restr{\theta_1}{V_1}$ is normalised. Let $B = (V \setminus \dom{\gamma_1}) \cup \vars{\restr{\gamma_1}{V}}$. Proposition \ref{proposition:change-of-domain-normalized} (with $A = V$) yields the normalisation of $\restr{\theta_1}{B}$. Remember that $\vran{\gamma_1} \subseteq \vars{\restr{s}{p}} \cup \vars{l}$. Let $x \in \vran{\gamma_1}$. Idempotence of $\gamma_1$ yields $x \notin \dom{\gamma_1}$. If $x \in \vars{\restr{s}{p}} \subseteq V$ then $x \in V \setminus \dom{\gamma_1}$. If $x \in \vars{l}$ then $x \in \vars{l\gamma_1} = \vars{(\restr{s}{p})\gamma_1}$ and thus $x \in \vran{\restr{\gamma_1}{V}}$. So $\vran{\gamma_1} \subseteq B$, and hence $B = V_1$. So $\theta_1$ is normalised.

		The induction hypothesis give us a term $s'$ and substitutions $\theta'$, $\gamma'$ such that
		\begin{align}
			 & s_1 \narrow_{\gamma'}^{*} s' \label{lifting-lemma:narrow:s_1-narrows-to-s-prime}       \\
			 & t' = s'\theta'                                                                         \\
			 & \theta_1 =^{V_1} \gamma'\theta' \label{lifting-lemma:eqn:theta_1-instance-theta-prime} \\
			 & \theta' \text{ is normalised} \label{lifting-lemma:eqn:theta-prime-is-normalized}
		\end{align}
		Moreover, we can assume that $s_1 \narrow_{\gamma'}^{*} s'$ and $t_1 \stc t'$ using the same rules at the same positions. Let $\gamma = \gamma_1 \gamma'$. By joining (\ref{lifting-lemma:eqn:s-narrows-s_1}) and (\ref{lifting-lemma:narrow:s_1-narrows-to-s-prime}) we get $s \narrow_{\gamma}^{*} s'$. By construction this narrowing derivation employs the same positions as the rewrite sequence $t \stc t'$. It remains to show that $\gamma \theta' =^V \theta$. Proposition \ref{proposition:change-of-domain} applied to (\ref{lifting-lemma:eqn:theta_1-instance-theta-prime}) give $\gamma_1 \gamma'\theta' =^V \gamma_1\theta_1$ and hence
		\begin{align*}
			\gamma \theta' & =^V \gamma_1\theta_1                                                       \\
			               & = \theta, \text{ by equation (\ref{lifting-lemma:gamma1-instance-theta}).}
		\end{align*}
	\end{proof}
\end{lemma}

Now we have the following theorem which express the completeness of narrowing on solving equations modulo $E$.
\begin{theorem}[Narrowing Completeness]\label{theorem:narrowing-completeness}
    Let $\trs$ be a complete TRS for the equational theory $E$. Let, moreover, $s, t$ be terms and $\sigma \in [s = t]_E$. Then there is a successful derivation starting with $P_0 = \{ s = t \}$, using the rules (1)-(8), such that the computed solution $\tau$ is a solution for $s = t$ with $\tau \leq \sigma$.

    \begin{proof}
        Let $\trsNF{\sigma}$ be the $\trs$-normal form of $\sigma$. Notice that $\sigma =_{E} \trsNF{\sigma}$, hence $s \trsNF{\sigma} =_{E} t \trsNF{\sigma}$. Confluence of $\trs$ yields the existence of a common reduct $r$, that is, $s\trsNF{\sigma} \stc r \stcR t \trsNF{\sigma} $. Now using the Lifting Lemma with $V_1 = \vars{s} \cup \vars{t}$ we get a term $s'$ and substitutions $\rho, \tau$ such that
        \begin{enumerate}
            \item $s \narrow_\tau^* s'$
            \item $r = s' \rho$,
            \item $\tau \rho =^{V_1} \trsNF{\sigma}$
        \end{enumerate}
        Hence we have a derivation $\{s = t \} \implies \cdots \implies \{s' = t\tau\}$ consisting of left narrowing steps with computed answer substitution $\tau$.

        In a similar way, using (3) above, it follows that $t \tau \rho = t \trsNF{\sigma} \sigma \stc r$. We again apply the Lifting Lemma (with $V_2 = \vars{s',s,t}$) which give us a term $t'$ and substitutions $\gamma, \eta$ such that
        \begin{enumerate}
            \setcounter{enumi}{3}
            \item $t \tau \narrow_{\eta}^{*} t'$,
            \item $r = t' \gamma$,
            \item $\eta \gamma =^{V_2} \rho$.
        \end{enumerate}
        Again we can prolong the above narrowing derivation with a sequence of right narrowing steps
        $$\{s' = t\} \implies \cdots \{s'\gamma = t'\}$$
        and obtain as compute answer $\tau \eta$.

        Using (6), (5) and (2) above one get
        $$s'\eta\gamma = s'\rho = r = t'\gamma$$
        So $s'\eta$ and $t'$ are unifiable. The Martelli-Montanari rules yields an mgu $\gamma'$ of $s'\eta$ and $t'$. So one have a successful derivation with computed solution $\tau \eta \gamma'$. This is indeed a solution: since $s \narrow_{\tau}^* s'$ and $t\tau \narrow_{\eta}^{*} t'$, we have $s\tau =_E s'$ and
        $$t\tau \eta \gamma' =_E s'\eta\gamma' = t'\gamma' =_E t\tau\eta\gamma'.$$
        It remains to show that $\tau\eta \gamma' \iqoless[E][V_1]$. Since $\gamma'$ is most general, there exists a $\rho'$ such that
        $$\tau \eta \gamma' \rho' = \tau \eta \gamma =_E \tau \rho =_E \trsNF{\sigma} =_E \sigma.$$
        Hence, $\tau \eta \gamma' \iqoless[E][V_1] \sigma$. As required.
    \end{proof}
    From the above proof one can see that we can indeed drop the strong normalisation condition if only normalised substitution is considered. Strong normalisation of $\trs$ is only used in the normalisation of $\sigma$ to $\trsNF{\sigma}$, hence we can strengthen this result by dropping the strong normalisation requirement and restricting ourselves to normalise substitutions. Since in a weakly normalising TRS every substitution is normalise, we obtain the following result.
    \begin{corollary}
        Narrowing is complete for weakly normalising and confluent Term Rewriting Systems.
    \end{corollary}
\end{theorem}

Now with the completeness result in hand, we can think of matters of efficiency. The first problem one will face when using narrowing techniques is its large search space. The search for possible derivations can even be an infinite set (see Example below). In order to reduce the search space of narrowing, Hullot \cite{hullot:cfunif} introduced the concept of basic narrowing. He showed that basic narrowing is complete for convergent TRSs. Motivated by the example below we study, in the next section, a restricted form of narrowing called basic narrowing.

\begin{example}\label{example:app-concat-standard-narrowing}
	Consider the TRS
	\[
		\trs =
		\begin{cases}
			\rho_1: \appFunc{\nilList}{x} \contr x \\
			\rho_2: \appFunc{x \cdot y}{z} \contr x \cdot \appFunc{y}{z}
		\end{cases}
    \]
    which represents the application function over $\cdot$ (conc function on lists). By a simple analysis we can see that $\trs$ is convergent.

    \begin{landscape}
        \thispagestyle{empty}
        \hrule
        \begin{figure}[h!]
            \begin{displaymath}
                \xymatrix{
                     & \{ \appFunc{\appFunc{x}{y}}{z} = \nilList \} \ar@{=>}[dl]_>>{\sigma_1}_a \ar@{=>}[dr]^>>{\gamma_1}^b & & \\
                    \{ \appFunc{x_1}{z} = \nilList \} \ar@{=>}[d]_>>{\sigma_2}  & & \{ \appFunc{w_1 \cdot \appFunc{y_1}{u_1}}{z} = \nilList \} \ar@{=>}[dl]_>>{\gamma_2}_c \ar@{=>}[dr]^>>{\gamma_3}^d & \\
                    \{ x_2 = \nilList \} \ar@{=>}[d]_>>{\sigma_3} & \{ w_2 \cdot \appFunc{\appFunc{y_1}{u_1}}{u_2} = \nilList \} \ar@{=>}[d] & & \{ \appFunc{w_1 \cdot x_5}{z} = \nilList \} \ar@{=>}[d]_>>{\gamma_4}^e \\
                    \{ \nilList = \nilList \} \ar@{=>}[d] & \vdots & & \{ w_1 \cdot \appFunc{x_5}{z} \} \ar@{=>}[d] \\
                    \emptyset & & &  \vdots \\
                }
            \end{displaymath}
            \caption{Derivation tree for the objective $\{ \appFunc{\appFunc{x}{y}}{z} = \nilList \}$}
            \label{figure:example:app:derivation-tree}
        \end{figure}
        \hrule
        \begin{table}[h!]
            \caption{Narrowing steps}
            \label{table:nice_table}
            \centering
            \begin{tabular}{l  c  c}
            \cline{1-3}
                step & rewrite rule & narrowing substitution \\
            \hline
            a & $\appFunc{\nilList}{x_1} \contr x_1$ & $[x / \nilList, y / x_1]$ \\

            b & $\appFunc{w_1 \cdot v_1}{u_1} \contr w_1 \cdot \appFunc{v_1}{u_1}$ & $[x / w_1 \cdot v_1, y / u_1 ]$ \\

            c & $\appFunc{w_2 \cdot v_2}{u_2} \contr w_2 \cdot \appFunc{v_2}{u_2}$ & $[w_1 / w_2, v_2 / \appFunc{v_1}{u_1}, z / u_2]$ \\

            d & $\appFunc{\nilList}{x_5} \contr x_5$ & $[v_1 / \nilList, u_1 / x_5]$ \\

            e & $\appFunc{w_3 \cdot v_3}{u_3} \contr w_3 \cdot \appFunc{v_3}{u_3}$ & $[w_3 / w_1, v_3 / x_5, u_3 / z]$ \\
            \hline
            \end{tabular}
        \end{table}
    \end{landscape}
\end{example}


\documentclass[11pt, oneside]{article}
\usepackage[a4paper,bindingoffset=0.2in,%
            left=1in,right=1in,top=1in,bottom=1in,%
            footskip=.25in]{geometry}
\usepackage{graphicx}
\usepackage{enumerate}
\usepackage{url}
\usepackage{hyperref}
\usepackage{pbox}
\usepackage{CJKutf8}
\usepackage{mathtools}
\usepackage{microtype}
\usepackage[htt]{hyphenat}
\renewcommand{\vec}[1]{\mathbf{#1}}

\begin{document}
\title{Assignment 1: Naive Bayes (II)}
\author{Xiyou Zhou, 13307130189 \\ Computer Science and Technology}
\maketitle
\section{Content}
\subsection{Modeling using Naive Bayes}
Here we are given such a problem: There is a email dataset split into training set of size $N$ and test set of size $M$. In training set, We have $D$ features represented as $\vec{X} \in R^D$ and the label $Y \in \{0, 1\}$ regarding whether this email is spam. In test set, we only have the same number of features for each instance and we would like to figure out the correct label for each instance.

Therefore, we can represent the probability of label $Y$ given the features $\vec{X}$ as $P(Y|\vec{X})$. Following the Bayes Rule:
$$
	P(Y|\vec{X}) = \frac{P(\vec{X}|Y)P(Y)}{P(\vec{X})} \propto P(\vec{X}|Y)P(Y) = P(Y)\prod_i P(X_i|Y)
$$
While we are given $X_i$ as continuous value and we can also preprocess the values as binary value. Consider word "purchase", the existence of such word in spam is very common and the existence rather than frequency of such word matters more in classification. That's why we can use binarized feature input can be used. Actually, our later experiments show Beta-Bernoulli Naive Bayes Classifier using binarized input performs well.

Here we only need to estimate $P(X_i|Y)$ given the training data. Consider if we use binarized features, the features can be each considered as results of several coin toss, where 1 regards the head and 0 regards the tail and estimate the probability $\theta$ of tossing head. The likelihood is $P(D|\theta) = \theta^{N_1} + \theta^{N_0}$. Here we can use Bernoulli distribution's conjugate prior distribution Beta Distribution as the prior since they have the same form. Thus, the prior is:
$$P(\theta) = Beta(\theta | \alpha, \beta) = \frac{\theta^{\alpha_1-1}+(1-\theta)^{\alpha_0-1}}{Beta(\alpha, \beta)}$$
Thus, the posterior is:
$$P(\theta|D) \propto \theta^{\alpha+N_1-1}+(1-\theta)^{\beta+N_0-1}$$
Therefore, the MAP of $\theta$ is $\frac{\alpha+N_1}{\alpha+\beta+N_1+N_0}$. We can estimate $P(X_i|Y)$ using the MAP.

When features given as continuous data, we can fit the distribution of such feature $X_i$ given label $Y$ to a Gaussian distribution and simply use the fitted mean and variance to predicate the probability density at given test data feature.

For model selection, here we use Beta-Bernoulli model according to their conjugation and the fact that binarized data make sense. Also, we select Gaussian Naive-Bayes model since we would like to predicate the probability density given feature input.

Here the assumption of i.i.d features results in we can derive $P(\vec{X}|Y)$ from $\prod_i P(X_i|Y)$. If not i.i.d we would have to figure out the dependence situation inside features and recalculate $P(\vec{X}|Y)$. In practice, we would assume i.i.d for Naive Bayes models and the result is not bad even if there are dependence in features.

\subsection{Preprocessing}

The binarization processing make it possible to use Beta-Bernoulli model to predicate the label since it turns continuous value into binary integer value. Also, according to our observation, the existence of words sometimes overweights their frequency. Therefore, binarization can be used for such model to reach great performance.

The log-transformation put extreme large and extreme small value into a reasonably common interval. Therefore later calculation won't be affected by extreme value.

The z-normalization-transformation turns the mean to 0 and the variance to 1. Therefore, the data is also put into a reasonably common interval for the whole dataset level.

\section{Beta-Bernoulli Naive Bayes}
Here we can observe the error rate change with $\alpha$ increases. The red line indicates training error and the green line indicates test error. Clearly, the error level increases as $\alpha$ increases for both training data and test data. When $\alpha = 1$, the training error is $11.25611\%$, test error is $11.00260\%$. When $\alpha = 10$, the training error is $11.97390\%$, test error is $11.19792\%$. When $\alpha = 100$, the training error is $13.99674\%$, test error is $13.28125\%$. To my surprise, the training error rate is higher than the test error rate, I guess it's because of the the data in two sets might be very uniform in feature distribution and the training set is bigger and contains more special data.

Also, when $\alpha$ increases the posterior is more affected by Beta Prior Distribution and thus makes worse performance since the prior is even for both sides.

\begin{figure}[h]
\centering
\includegraphics{foo.pdf}
\caption{Plot of Error Rate Change with $\alpha$}
\end{figure}

\section{Gaussian Naive Bayes}

It turns out $17.61827\%$ training error and $18.68490\%$ test error for non-preprocessing, $17.61827\%$ training error and $19.01042\%$ test error for z-normalization preprocessing, $16.34584\%$ training error and $18.09896\%$ test error for log preprocessing. Using sklean GaussianNB achieved similar results. I also tested with Random Forest Classifier and reached $6.18490\%$ test set error rate.

\begin{thebibliography}{9}
\bibitem{UBCMLNote8}
UBC Machine Learning CS540 Lecture Note 8,
\\\texttt{https://www.cs.ubc.ca/\~{}murphyk/Teaching/CS540-Fall08/L8.pdf}

\bibitem{lucius-yu}
Beta Distribution, Dirichlet Distribution and Bayesian Parameter Estimation,
\\\texttt{https://lucius-yu.github.io/docs/probability/BaysianEstimation/}

\end{thebibliography}

\end{document}


%% ---------------------------------------------------
%% Notice that we use the "report" class instead of "article"
%% ---------------------------------------------------
\documentclass{report}

\title{Project 5}
\author{\textbf{Jinhao Wei}}
\date{\textbf{19 February 2019}}

%% ---------------------------------------------------
%% CSBDformat specifies the format of our reports
%% ---------------------------------------------------
\usepackage{634format}

%% ---------------------------------------------------
%% enumerate
%% ---------------------------------------------------
\usepackage{enumerate}

%% ---------------------------------------------------
%% listings is used for including our source code in reports
%% ---------------------------------------------------
\usepackage{listings}
\usepackage{textcomp}

%% ---------------------------------------------------
%% Packages for math environments
%% ---------------------------------------------------
\usepackage{amsmath}

%% ---------------------------------------------------
%% Packages for URLs and hotlinks in the table of contents
%% and symbolic cross references using \ref
%% ---------------------------------------------------
\usepackage{hyperref}

%% ---------------------------------------------------
%% Packages for using HOL-generated macros and displays
%% ---------------------------------------------------
\usepackage{holtex}
\usepackage{holtexbasic}
\input{commands}

\begin{document}

\input{../HOL/HOLReports/HOLAll}

%% --------------------------------------------------- the listings
%% parameter "language" is set to "ML"
%% ---------------------------------------------------
\lstset{language=ML}

\maketitle{}

\begin{abstract}
  This report is a summary on my solutions of problem \emph{8.4.1}, \emph{8.4.2} and\emph{8.4.3}. In these problems, we used \emph{HOL} to prove theorems and used \emph{EmitTex} to do pretty prints. Our works are contained in two folders respectively named \emph{LaTeX} and \emph{HOL}. In folder \emph{LaTeX}, we provided files that are used to produce this project report. In folder \emph{HOL}, we put all the proofs into one file named \emph{AllScript.sml} and used some other helper files to generate HOL reports named \emph{AllReport.pdf}.
\end{abstract}

\begin{acknowledgments}
  This project follows the format and structure of \emph{sampleTheory} provided by Professor Shiu-Kai Chin. Specifically, this report is basically a modification on the file \emph{exampleEmitTeXReport.tex}, I merely saturated it up with my works.
\end{acknowledgments}

\tableofcontents{}

\chapter{Executive Summary}
\label{cha:executive-summary}

\textbf{All requirements for this project are satisfied}.  In
particular, we proved all the theorems in this project, pretty printed the HOL theories,
and made use of the \emph{EmitTeX} structure to typeset HOL theorems
in this report.

The following theorems are proved and their corresponding \LaTeX{}
macros used in this report.
\begin{quote}
  \HOLAllTheorems
\end{quote}
However, due to my incompentence on pretty print techniques, the theorems' output order is not consistent with the theorems' proof order in the report.

\chapter{Proof of problem1Thm}
\label{cha:proof-prob1theorem}

\section{Problem Statement}
\label{sec:problem-statement}
Our task is to prove the theorem \HOLAllTheoremsproblemOneThm

\section{HOL Code Proving problem1Thm}
\label{sec:hol-code-proving}

\begin{lstlisting}[frame=trBL]
val problem1Thm =
let
  val th1 = ASSUME``p:bool``
  val th2 = ASSUME``(p:bool)==>(q:bool)``
  val th3 = ASSUME``(q:bool) ==> (r:bool)``

  val th4 = MP th2 th1
  val th5 = IMP_TRANS th2 th3

  val th6 = MP th5 th1

  val th7 = DISCH(last(hyp(th6))) th6
  val th8 = DISCH(last(hyp(th7))) th7
in
  DISCH(last(hyp(th8))) th8
end;

\end{lstlisting}

\section{Session Transcript}
\label{sec:session-transcript}

\setcounter{sessioncount}{0}
\begin{session}
  \begin{scriptsize}
\begin{verbatim}

> > > > # # # # # # # # # ** types trace now on
> # # # # # # # # # ** Unicode trace now off
> val problem1Thm =
let
  val th1 = ASSUME``p:bool``
  val th2 = ASSUME``(p:bool)==>(q:bool)``
  val th3 = ASSUME``(q:bool) ==> (r:bool)``

  val th4 = MP th2 th1
  val th5 = IMP_TRANS th2 th3

  val th6 = MP th5 th1

  val th7 = DISCH(last(hyp(th6))) th6
  val th8 = DISCH(last(hyp(th7))) th7
in
  DISCH(last(hyp(th8))) th8
end;
# # # # # # # # # # # # # # # # val problem1Thm =
   |- (p :bool) ==> (p ==> (q :bool)) ==> (q ==> (r :bool)) ==> r:
   thm
>
\end{verbatim}
  \end{scriptsize}
\end{session}

\chapter{Proof of conjSymThm}
\label{cha:proof-prob1theorem}

\section{Problem Statement}
\label{sec:problem-statement}
Our task is to prove the theorem \HOLAllTheoremsconjSymThm using \texttt{IMP_ANTISYM_RULE}.

\section{HOL Code Proving conjSymThm}
\label{sec:hol-code-proving}

\begin{lstlisting}[frame=trBL]
val conjSymThm =
let
  val tmp1Thm =
  	  let
		val thm1 = ASSUME``p/\q``
   		val thm2 = CONJUNCT1 thm1
   		val thm3 = CONJUNCT2 thm1
   		val thm4 = CONJ thm3 thm2
  	  in
		DISCH ``p/\q`` thm4
   	  end;
  val tmp2Thm =
      	  let
		val thm1 = ASSUME``q/\p``
  		val thm2 = CONJUNCT1 thm1
  		val thm3 = CONJUNCT2 thm1
  		val thm4 = CONJ thm3 thm2
	  in
		DISCH ``q/\p`` thm4
          end;
in
  IMP_ANTISYM_RULE tmp1Thm tmp2Thm
end;
\end{lstlisting}

\section{Session Transcript}
\label{sec:session-transcript}

\setcounter{sessioncount}{0}
\begin{session}
  \begin{scriptsize}
\begin{verbatim}

> val conjSymThm =
let
  val tmp1Thm =
  	  let
		val thm1 = ASSUME``p/\q``
   		val thm2 = CONJUNCT1 thm1
   		val thm3 = CONJUNCT2 thm1
   		val thm4 = CONJ thm3 thm2
  	  in
		DISCH ``p/\q`` thm4
   	  end;
  val tmp2Thm =
      	  let
		val thm1 = ASSUME``q/\p``
  		val thm2 = CONJUNCT1 thm1
  		val thm3 = CONJUNCT2 thm1
  		val thm4 = CONJ thm3 thm2
	  in
		DISCH ``q/\p`` thm4
          end;
in
  IMP_ANTISYM_RULE tmp1Thm tmp2Thm
end;
# # # # # # # # # # # # # # # # # # # # # # val conjSymThm =
   |- (p :bool) /\ (q :bool) <=> q /\ p:
   thm
\end{verbatim}
  \end{scriptsize}
\end{session}

\chapter{Proof of conjSymThmAll}
\label{cha:proof-demotheorem}

\section{Problem Statement}
\label{sec:problem-statement-1}

Our task is to prove the following theorem using \texttt{GENL}.

\HOLAllTheoremsconjSymThmAll

\section{HOL Code Proving conjSymThmAll}
\label{sec:hol-code-proving-1}

\begin{lstlisting}[frame=trBL]
val conjSymThmAll = GENL [``p:bool``, ``q:bool``] conjSymThm;
\end{lstlisting}

\section{Session Transcript}
\label{sec:session-transcript-1}

\setcounter{sessioncount}{0}
\begin{session}
  \begin{scriptsize}
\begin{verbatim}

> val conjSymThmAll = GENL [``p:bool``, ``q:bool``] conjSymThm;
val conjSymThmAll =
   |- !(p :bool) (q :bool). p /\ q <=> q /\ p:
   thm
\end{verbatim}
  \end{scriptsize}
\end{session}

%% ------------------------------------------
%% Change to letters for appendix
%% ------------------------------------------

%% ------------------------------------------
%% this restarts the section numbering
%% ------------------------------------------
\appendix{}

%% ------------------------------------------
% label using capital letters
%% ------------------------------------------
\renewcommand{\thechapter}{\Alph{chapter}}

\chapter{Source Code for example1Script}
\label{cha:source-code-sample}

The following code is from \emph{AllScript.sml}, which is located
in a different subdirectory than this file.
\lstinputlisting{../HOL/AllScript.sml}

\end{document}