\section{Scattering theory}

Since in a real metal there would be many scatters that can be behave as obstacles for electron that have free wave functions. Therefore we need to calculate them to analyse the real behaviour of the electrons.

\noindent
Then the wave function of the electron in a real matel $\Psi(\vb{r},t)$ should satisfy the following time-dependet Schrodinger equation
\begin{equation} \label{2.1}
  i \hbar \pdv{\Psi(\vb{r},t)}{t} = [H_e(t) + U(\vb{r})] \Psi(\vb{r},t)
\end{equation}
where $U(\vb{r})$ is the total scattering potential. We have represented the all scatters using this potential. Since the solutions \eqref{1.52} are create a complete orthonormal basis we can represent this wave function using those as follows
\begin{equation} \label{2.2}
  \Psi(\vb{r},t) = \sum_j a_j(t) \ket{\psi_j(t)}
\end{equation}
where the difference inidces j corresponding to the different sets of all quantum numbers $p_x$ and $n$
\begin{equation} \label{2.3}
  j \rightarrow (m,n) \quad \text{where} \quad
  m,n =0,1,2,...
\end{equation}
with $m$ is defined for quantized momentum in $x$ direction
\begin{equation} \label{2.4}
  p_x = m \frac{2\pi \hbar}{L_x}
\end{equation}

\noindent
Now we can use the conventional pertubation theory to calculate scattering process of electron at a state $\ket{\psi_j}$ to a state $\ket{\psi_j'}$. For that assume an electron be in the $j$ state at the time $t=0$ and corresponding $a_j'(0) = \delta_{j,j'}$.

\noindent
First subtitute a general electron state $\Psi(\vb{r},t)$ at time $t$ as the incoming electron to the Schrodinger equation given in Eq. \eqref{2.1}
\begin{equation} \label{2.5}
  i \hbar \pdv{t} \sum_j a_j(t) \ket{\psi_j(t)}= [H_e(t) + U(\vb{r})] \sum_j a_j(t) \ket{\psi_j(t)}
\end{equation}
\begin{equation} \label{2.6}
  i \hbar\sum_j   \dot{a_j}(t) \ket{\psi_j(t)} + a_j(t)\pdv{t}\ket{\psi_j(t)}= [H_e(t) + U(\vb{r})] \sum_j a_j(t) \ket{\psi_j(t)}
\end{equation}
since all the ${\ket{\psi(t)}}$ staistfy the Schrodinger equation \eqref{1.18}
\begin{equation} \label{2.7}
  i \hbar\sum_j   \dot{a_j}(t) \ket{\psi_j(t)} = \sum_j U(\vb{r}) a_j(t) \ket{\psi_j(t)}.
\end{equation}
Then take inner product with state with the state $\ket{\psi_{j'}(t)}$
\begin{equation} \label{2.8}
  i \hbar\sum_j   \dot{a_j}(t) \braket{\psi_{j'}(t)}{\psi_j(t)} = \sum_j
  a_j(t) \bra{\psi_{j'}(t)} U(\vb{r}) \ket{\psi_j(t)}
\end{equation}
But using the \textit{Born approximation} we can assume that this incoming wave have the initial state of the electron at $t=0$ and therefore this equation will modified to
\begin{equation} \label{2.9}
  i \hbar\sum_j   \dot{a_j}(t) \braket{\psi_{j'}(t)}{\psi_j(t)} =
  \bra{\psi_{j'}(t)} U(\vb{r}) \ket{\psi_j(t)}
\end{equation}
due to orthonormality this becomes
\begin{equation} \label{2.10}
  i \hbar \dot{a_{j'}}(t) =
  \bra{\psi_{j'}(t)} U(\vb{r}) \ket{\psi_j(t)}
\end{equation}
and finally this leads to first order pertubation theory for Sscattering as follows
\begin{equation} \label{2.11}
   \dot{a_{j'}}(t) =
  -\frac{i}{\hbar}\bra{\psi_{j'}(t)} U(\vb{r}) \ket{\psi_j(t)}
\end{equation}
where
\begin{equation} \label{2.12}
   a_{j'}(t) =
  -\frac{i}{\hbar}
  \int_0^t dt' \int_S d\vb{r} \;
  \psi_{j'}^{*} (\vb{r},t') U(\vb{r}) {\psi_j(\vb{r},t')}
\end{equation}
where the integration should be performed over the 2DEG area $S=L_xL_y$. Then we can calculate this using the eqution we derived in \eqref{1.52} as follows
\begin{equation} \label{2.13}
  \begin{aligned}
    a_{j'}(t) & =
   -\frac{i}{\hbar}
   \int_0^t dt' \int_S d\vb{r} \;
   \bigg[
   \frac{1}{\sqrt{L_x}} \chi_{n'}^*\big(y - {y'}_0 -\zeta(t)\big) \\
   & \times
     \exp(
    \frac{i}{\hbar}\bigg[E_{n'}t' - m'\frac{2\pi \hbar x}{L_x} -
   \frac{eE(y-{y'}_0)}{\omega}\cos(\omega t')-
   m_e\dot{\zeta}(t)\big[y - {y'}_0 -\zeta(t')\big]
    - \int_0^{t'}dt'L(\zeta,\dot{\zeta},t")\bigg]) \\
    & \times
    U(\vb{r}) \\
    & \times
    \frac{1}{\sqrt{L_x}} \chi_{n}\big(y - y_0 -\zeta(t')\big) \\
    & \times
      \exp(
     \frac{i}{\hbar}\bigg[ - E_{n}t' + m\frac{2\pi \hbar x}{L_x} -
    \frac{eE(y-y_0)}{\omega}\cos(\omega t') -
    m_e\dot{\zeta}(t')\big[y - y_0 -\zeta(t')\big]
     - \int_0^{t'}d\tilde{t}L(\zeta,\dot{\zeta},\tilde{t})\bigg])
    \bigg]
  \end{aligned}
\end{equation}
then this will be simplified to
\begin{equation} \label{2.14}
  \begin{aligned}
    a_{j'}(t) & =
   -\frac{i}{\hbar}
   \int_0^t dt' \int_S d\vb{r} \;
   \bigg[
   \frac{1}{\sqrt{L_x}} \chi_{n'}^*\big(y - {y'}_0 -\zeta(t')\big)
   U(\vb{r})
   \frac{1}{\sqrt{L_x}} \chi_{n}\big(y - y_0 -\zeta(t')\big)  \\
   & \times
     \exp(
    \frac{i}{\hbar}\bigg[ E_{n'}t' - m'\frac{2\pi \hbar x}{L_x} -
   \frac{eE(y-{y'}_0)}{\omega}\cos(\omega t') -
   m_e\dot{\zeta}(t')\big[y - {y'}_0 -\zeta(t')\big]
    - \int_0^{t'}d\tilde{t}L(\zeta,\dot{\zeta},\tilde{t})\bigg]) \\
    & \times
      \exp(
     \frac{i}{\hbar}\bigg[ - E_{n}t' + m\frac{2\pi \hbar x}{L_x} +
    \frac{eE(y-y_0)}{\omega}\cos(\omega t') +
    m_e\dot{\zeta}(t')\big[y - y_0 -\zeta(t')\big]
     + \int_0^{t'}d\tilde{t}L(\zeta,\dot{\zeta},\tilde{t})\bigg])
    \bigg]
  \end{aligned}
\end{equation}
\begin{equation} \label{2.15}
  \begin{aligned}
    a_{j'}(t) & =
   -\frac{i}{\hbar}
   \int_0^t dt' \int_S d\vb{r} \;
   \bigg[
   \frac{1}{\sqrt{L_x}} \chi_{n'}^*\big(y - {y'}_0 -\zeta(t')\big)
   U(\vb{r})
   \frac{1}{\sqrt{L_x}} \chi_{n}\big(y - y_0 -\zeta(t')\big)
   \exp(\frac{2\pi i(m-m') \hbar x}{L_x})
   \\
   & \times
     \exp(
    \frac{i}{\hbar}\bigg[ E_{n'}t' +
   \frac{eE{y'}_0}{\omega}\cos(\omega t') +
   m_e\dot{\zeta}(t'){y'}_0
    \bigg])
      \exp(
     \frac{i}{\hbar}\bigg[ - E_{n}t' -
    \frac{eEy_0}{\omega}\cos(\omega t') -
    m_e\dot{\zeta}(t)y_0
    \bigg])
    \bigg].
  \end{aligned}
\end{equation}
The time dependence of the $chi_n(y)$ can neglect since it is integrate over all the values of the $y$ and we can write this as
\begin{equation} \label{2.16}
  \begin{aligned}
    a_{j'}(t) & =
   -\frac{i}{\hbar}
   \int_S d\vb{r} \;
   \frac{1}{\sqrt{L_x}} \chi_{n'}^*\big(y - {y'}_0 -\zeta(t')\big)
   U(\vb{r})
   \frac{1}{\sqrt{L_x}} \chi_{n}\big(y - y_0 -\zeta(t')\big)
   \exp(\frac{2\pi i(m-m') \hbar x}{L_x})
   \\ & \times
   \int_0^t dt' \;
   \bigg[
     \exp(
    \frac{i}{\hbar}\bigg[ (E_{n'} -E_{n}) t' +
   \frac{eE({y'}_0 - y_0)\omega_0^2}{\omega(\omega_0^2-\omega^2)}\cos(\omega t')
    \bigg])
    \bigg].
  \end{aligned}
\end{equation}
Using Jacobi-Anger expansion
\begin{equation} \label{2.17}
  e^{iz\cos(\theta)} = \sum_{l=-\infty}^{\infty} i^l J_j(z)e^{in\theta}
\end{equation}
above eqution can be modified as
\begin{equation} \label{2.18}
  \begin{aligned}
    a_{j'}(t)  =
   -\frac{i}{\hbar}
   U_{j'j}
   \int_0^t dt' \;
   \bigg[
   \sum_{l=-\infty}^{\infty} i^l J_l\bigg[\frac{eE({y'}_0 - y_0)\omega_0^2}{\hbar\omega(\omega_0^2-\omega^2)}\bigg]
     \exp(
    \frac{i}{\hbar} (E_{n'} -E_{n} + l\hbar\omega) t')
    \bigg]
  \end{aligned}
\end{equation}
where
\begin{equation} \label{2.19}
  U{j'j} \equiv \mel{\Phi_{j'}(\vb{r})}{U(\vb{r})}{\Phi_j(\vb{r})}
\end{equation}
with bare electron eigen states (without dressing field)
\begin{equation} \label{2.20}
  \Phi_{j}(\vb{r}) = \frac{1}{\sqrt{L_x}}\exp(\frac{2\pi im \hbar x}{L_x}) \chi_{n}(y).
\end{equation}
Considering time evalution from negative values we can write the same expression as follows
\begin{equation} \label{2.21}
  \begin{aligned}
    a_{j'}(t)  =
   -\frac{i}{\hbar}
   U_{j'j}
   \int_{-t/2}^{t/2} dt' \;
   \bigg[
   \sum_{l=-\infty}^{\infty} i^l J_l\bigg[\frac{eE({y'}_0 - y_0)\omega_0^2}{\hbar\omega(\omega_0^2-\omega^2)}\bigg]
     \exp(
    \frac{i}{\hbar} (E_{n'} -E_{n} + l\hbar\omega) t')
    \bigg].
  \end{aligned}
\end{equation}
To calculate scattering probability we can use this scattering amplitude's squre value
\begin{equation} \label{2.22}
  \begin{aligned}
    |a_{j'}(t)|^2  =
   \frac{|U_{j'j}|^2}{\hbar^2} &
   \int_{-t/2}^{t/2} dt' \;
   \bigg[
   \sum_{l=-\infty}^{\infty} {-i}^l J_l\bigg[\frac{eE({y'}_0 - y_0)\omega_0^2}{\hbar\omega(\omega_0^2-\omega^2)}\bigg]
     \exp(
    \frac{-i}{\hbar} (E_{n'} -E_{n} + l\hbar\omega) t')
    \bigg] \\
    & \times
    \int_{-t/2}^{t/2} dt^{''} \;
    \bigg[
    \sum_{k=-\infty}^{\infty} i^k J_k\bigg[\frac{eE({y'}_0 - y_0)\omega_0^2}{\hbar\omega(\omega_0^2-\omega^2)}\bigg]
      \exp(
     \frac{i}{\hbar} (E_{n'} -E_{n} + k\hbar\omega) t^{''})
     \bigg]
  \end{aligned}
\end{equation}
Considering long time $t\rightarrow \infty$ we can make the integral into a delta function as follows
\begin{equation} \label{2.23}
  \begin{aligned}
    |a_{j'}(t)|^2  =
   4\pi^2|U_{j'j}|^2 &
   \bigg[
   \sum_{l=-\infty}^{\infty} {-i}^l J_l\bigg[\frac{eE({y'}_0 - y_0)\omega_0^2}{\hbar\omega(\omega_0^2-\omega^2)}\bigg]
     \delta(-E_{n'} +E_{n} - l\hbar\omega)
     \bigg] \\
    & \times
    \bigg[
    \sum_{k=-\infty}^{\infty} i^k J_k\bigg[\frac{eE({y'}_0 - y_0)\omega_0^2}{\hbar\omega(\omega_0^2-\omega^2)}\bigg]
      \delta(E_{n'} - E_{n} + k\hbar\omega)
     \bigg]
  \end{aligned}
\end{equation}
and this implies $l=k$ and this leads to
\begin{equation} \label{2.24}
  \begin{aligned}
    |a_{j'}(t)|^2  =
   4\pi^2|U_{j'j}|^2
   \bigg[
   \sum_{l=-\infty}^{\infty} J_l^2\bigg[\frac{eE({y'}_0 - y_0)\omega_0^2}{\hbar\omega(\omega_0^2-\omega^2)}\bigg]
     \delta^2(E_{n'} - E_{n} + l\hbar\omega).
  \end{aligned}
\end{equation}
Then using the famous the square $\delta$ function transormation method
\begin{equation} \label{2.25}
  \begin{aligned}
     \delta^2(\epsilon ) = \delta(\epsilon )\delta^2(0)
     \lim_{t\rightarrow\infty} \int_{-t/2}^{t/2} e^{i0\times t' /\hbar} dt' =
     \frac{\delta(\epsilon) t}{2\pi \hbar}
  \end{aligned}
\end{equation}
we can calculatre the probability of electron scattering between states $j$ and $j'$ per unit time as
\begin{equation} \label{2.26}
    \mathcal{W}_{j'j} \equiv \dv{|a_{j'}(t)|^2}{t} =
    |U_{j'j}|^2
     \sum_{l=-\infty}^{\infty} J_l^2\bigg[\frac{eE({y'}_0 - y_0)\omega_0^2}{\hbar\omega(\omega_0^2-\omega^2)}\bigg]
    \times
    \frac{2\pi}{\hbar} \delta(E_{n'} - E_{n} + l\hbar\omega)
\end{equation}

\noindent
To avoid thee energy echange betwen a high-frequency field and electrons, the field should be purely dressing. We can achieve that by using the field with off-resonant and high frequency. Therefore, the only effect of the dressing field on 2DEG is the renormalization of the probability of elastic electron scattering within the same Landau level $(E_{n'}=E_n)$, which described by the term with $l=0$ the Eq. \eqref{2.26} leads to
\begin{equation} \label{2.27}
    \mathcal{W}_{j'j} = J_0^2\bigg[\frac{eE({y'}_0 - y_0)\omega_0^2}{\hbar\omega(\omega_0^2-\omega^2)}\bigg]
    \mathcal{W}_{j'j}^{(0)}
\end{equation}
where
\begin{equation} \label{2.28}
    \mathcal{W}_{j'j}^{(0)} = \frac{2\pi}{\hbar} |U_{j'j}|^2
    \delta(E_{n'} - E_{n})
\end{equation}
is the probability of scattering of a \textit{bare electron}. It is important to notice that the Bessel function factor depend on both the dressing field and stationaty magnetic field. This factor is responcible for all the effcts discussed in thisarticle.

\noindent
One can define the lifetime of the dressed electron at the Landau level $\tau$ is renormalized by the Bessel function as below
\begin{equation} \label{2.29}
    \frac{1}{\tau} \equiv \sum_{j'} \mathcal{W}_{j'j} =
    \sum_{j'}
    J_0^2\bigg[\frac{eE({y'}_0 - y_0)\omega_0^2}{\hbar\omega(\omega_0^2-\omega^2)}\bigg]
    \mathcal{W}_{j'j}^{(0)}
\end{equation}
where we have consider all posibilities that electron can jump to the state $j'$. Then rewrite the delat function as follows
\begin{equation} \label{2.30}
    \delta(\epsilon) =
    \frac{1}{\pi} \lim_{\Gamma \rightarrow 0 } \frac{\Gamma}{\Gamma^2 + \epsilon^2}
\end{equation}
where in this study we can assume that the paramater $\Gamma \equiv \hbar/\tau$ as scattering induced broading of the Landau level. But for the elestic scatteing within the same Landau level, we can write the $\delta$ function as
\begin{equation} \label{2.31}
    \delta(E_{n'} - E_{n}) \approx
    \frac{1}{\pi \Gamma}.
\end{equation}
Therefore Eq. \eqref{2.29} will change to
\begin{equation} \label{2.32}
    \frac{1}{\tau} =
    \sum_{j'}
    J_0^2\bigg[\frac{eE({y'}_0 - y_0)\omega_0^2}{\hbar\omega(\omega_0^2-\omega^2)}\bigg]
    \times
    \frac{2\pi}{\hbar} |U_{j'j}|^2 \times \frac{1}{\pi \Gamma}
\end{equation}
\begin{equation} \label{2.33}
    \frac{1}{\tau} =
    \sum_{j'}
    J_0^2\bigg[\frac{eE({y'}_0 - y_0)\omega_0^2}{\hbar\omega(\omega_0^2-\omega^2)}\bigg]
    \times
    \frac{2}{\hbar} |U_{j'j}|^2 \times \frac{\tau}{\hbar}
\end{equation}
and finally this can be modified to
\begin{equation} \label{2.34}
    \frac{1}{\tau} =
    \bigg[
    \frac{2}{\hbar^2}
    \sum_{j'}
    J_0^2\bigg[\frac{eE({y'}_0 - y_0)\omega_0^2}{\hbar\omega(\omega_0^2-\omega^2)}\bigg]
    |U_{j'j}|^2
    \bigg]^{1/2}
\end{equation}
where the summation is performed over electron states $j'$ within the same Landau level.

\noindent
Now lets specify more on the scattering potential where we can model them as randomly distributed delta fucntions as follows
\begin{equation} \label{2.35}
    U(\vb{r}) \equiv \sum_{i=1}^{N_s} U_0 \delta(\vb{r} - \vb{r}_i)
\end{equation}
where $N_s$ is the total number of scatters in the considering matel. Now we can calculate $|U_{j'j}|^2$ as follows
\begin{equation} \label{2.36}
  \begin{aligned}
    |U_{j'j}|^2 = &
    \sum_{i =1}^{N_s}
    \frac{1}{L_x^2}
    \int \int  dx_1 dy_1
      \exp(\frac{-{p'}_x x_1}{\hbar})
      \chi_n^* (y_1 - {y'}_0)
      U_0 \delta(x_1-x_i)\delta(y_1-y_i)
      \exp(\frac{{p}_x x_1}{\hbar})
      \chi_n (y_1 - {y}_0) \\
    & \times
    \int \int  dx_2 dy_2
      \exp(\frac{{p'}_x x_2}{\hbar})
      \chi_n (y_2 - {y'}_0)
      U_0 \delta(x_2 - x_i)\delta(y_2 - y_i)
      \exp(\frac{-{p}_x x_2}{\hbar})
      \chi_n^* (y_2 - {y}_0)
  \end{aligned}
\end{equation}
and considering only non-zero values for $x_1$ and $x_2$ integrals we can re-write this as
\begin{equation} \label{2.37}
  \begin{aligned}
    |U_{j'j}|^2 = &
    \sum_{i =1}^{N_s}
    \frac{U_0^2}{L_x^2}
    \int dy_1
      \exp(\frac{-{p'}_x x_i}{\hbar})
      \chi_n^* (y_1 - {y'}_0)
      \delta(y_1-y_i)
      \exp(\frac{{p}_x x_i}{\hbar})
      \chi_n (y_1 - {y}_0) \\
    & \times
    \int dy_2
      \exp(\frac{{p'}_x x_i}{\hbar})
      \chi_n (y_2 - {y'}_0)
      \delta(y_2 - y_i)
      \exp(\frac{-{p}_x x_i}{\hbar})
      \chi_n^* (y_2 - {y}_0)
  \end{aligned}
\end{equation}
and this will be simplified to
\begin{equation} \label{2.38}
  \begin{aligned}
    |U_{j'j}|^2 = &
    \sum_{i =1}^{N_s}
    \frac{U_0^2}{L_x^2}
    \int dy_1
      \chi_n^* (y_1 - {y'}_0)
      \delta(y_1-y_i)
      \chi_n (y_1 - {y}_0) \\
    & \times
    \int dy_2
      \chi_n (y_2 - {y'}_0)
      \delta(y_2 - y_i)
      \chi_n^* (y_2 - {y}_0).
  \end{aligned}
\end{equation}
Again considering only non-zero values for $y_1$ and $y_2$ integrals we can re-write this as
\begin{equation} \label{2.39}
  \begin{aligned}
    |U_{j'j}|^2 = &
    \sum_{i =1}^{N_s}
    \frac{U_0^2}{L_x^2}
      \chi_n^* (y_i - {y'}_0)
      \chi_n (y_i - {y}_0)
      \chi_n (y_i - {y'}_0)
      \chi_n^* (y_i - {y}_0).
  \end{aligned}
\end{equation}
\begin{equation} \label{2.40}
  \begin{aligned}
    |U_{j'j}|^2 =
    \frac{U_0^2}{L_x^2}
    \sum_{i =1}^{N_s}
      \chi_n^2 (y_i - {y'}_0)
      \chi_n^2 (y_i - {y}_0).
  \end{aligned}
\end{equation}
Now subtituting this derivation into the Eq. \eqref{2.34} we will get
\begin{equation} \label{2.41}
    \frac{1}{\tau} =
    \bigg[
    \frac{2U_0^2}{\hbar^2 L_x^2}
    \sum_{{y'}_0}
    J_0^2\bigg[\frac{eE({y'}_0 - y_0)\omega_0^2}{\hbar\omega(\omega_0^2-\omega^2)}\bigg]
    \sum_{i =1}^{N_s}
      \chi_n^2 (y_i - {y'}_0)
      \chi_n^2 (y_i - {y}_0)
    \bigg]^{1/2}
\end{equation}
where $j'$ reduced to ${p'}_x$ (since $n'=n$)and we can represent it by ${y'}_0$. Then this will modified to
\begin{equation} \label{2.42}
    \frac{1}{\tau} =
    \bigg[
    \frac{2U_0^2}{\hbar^2 L_x^2}
    \sum_{{y'}_0}
    \sum_{i =1}^{N_s}
    J_0^2\bigg[\frac{eE({y'}_0 - y_0)\omega_0^2}{\hbar\omega(\omega_0^2-\omega^2)}\bigg]
      \chi_n^2 (y_i - {y'}_0)
      \chi_n^2 (y_i - {y}_0)
    \bigg]^{1/2}.
\end{equation}
Now considering large size of sample and a macroscopically large $N_s$ scatters we can promate the summation to integrations as follows
\begin{equation} \label{2.43}
    \frac{1}{\tau} =
    \bigg[
    \frac{2U_0^2}{\hbar^2 L_x^2}
    \frac{eB L_x}{2\pi\hbar}\int d{y'}_0
    \frac{N_s} {L_x}\int dy_i
    J_0^2\bigg[\frac{eE({y'}_0 - y_0)\omega_0^2}{\hbar\omega(\omega_0^2-\omega^2)}\bigg]
      \chi_n^2 (y_i - {y'}_0)
      \chi_n^2 (y_i - {y}_0)
    \bigg]^{1/2}.
\end{equation}
Assuming $L_x = L_y$ we can define the area of the 2D material as
\begin{equation} \label{2.44}
    S \equiv L_xL_x = L_xL_y
\end{equation}
and then we can re-write the above as
\begin{equation} \label{2.45}
    \frac{1}{\tau} =
    \bigg[
    \frac{eBN_sU_0^2}{\pi\hbar^3 S}
    \int d{y'}_0
    \int dy_i
    J_0^2\bigg[\frac{eE({y'}_0 - y_0)\omega_0^2}{\hbar\omega(\omega_0^2-\omega^2)}\bigg]
      \chi_n^2 (y_i - {y'}_0)
      \chi_n^2 (y_i - {y}_0)
    \bigg]^{1/2}.
\end{equation}
Define the \textit{density of scatters} per unit area of 2DEG
\begin{equation} \label{2.46}
    n_s \equiv \frac{N_s}{S}
\end{equation}
and the \textit{magnetic length} as
\begin{equation} \label{2.47}
    l_0 \equiv \sqrt{\frac{\hbar}{eB}}.
\end{equation}
Now our Eq. \eqref{2.45} leads to
\begin{equation} \label{2.48}
    \frac{1}{\tau} =
    \sqrt{
    \frac{n_sU_0^2}{\pi l_0^2 \hbar^2}}
    \bigg[
    \int d{y'}_0
    \int dy_i
    J_0^2\bigg[\frac{eE({y'}_0 - y_0)\omega_0^2}{\hbar\omega(\omega_0^2-\omega^2)}\bigg]
      \chi_n^2 (y_i - {y'}_0)
      \chi_n^2 (y_i - {y}_0)
    \bigg]^{1/2}
\end{equation}
and now define new dummy variables as follows (since $y_0$ is a paramter)
\begin{equation} \label{2.49}
    ({y'}_0 - y_0) \rightarrow  y  \quad \text{and} \quad
    ({y}_i- {y'}_0) \rightarrow y'
\end{equation}
and finally we will get the eqution for the dressed electron lifetime at the $n$th Landau level as
\begin{equation} \label{2.50}
    \frac{1}{\tau} =
    \sqrt{
    \frac{n_sU_0^2}{\pi l_0^2 \hbar^2}}
    \bigg[
    \int \int  dy dy'\;
    J_0^2\bigg[\frac{eEy\omega_0^2}{\hbar\omega(\omega_0^2-\omega^2)}\bigg]
      \chi_n^2 (y')
      \chi_n^2 (y+y')
    \bigg]^{1/2}
\end{equation}
\hfill$\blacksquare$


\chapter{Power and Sample Size \label{chapter:powersamplesize}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Statistical Power}

\textbf{Power} is the probability that a hypothesis test will reject the null hypothesis if the alternative hypothesis is, in fact, true. The graph below shows two distributions: the sampling distribution under the null (mean $\mu_0$) and the sampling distribution under the alternative hypothesis (mean $\mu_1$). Note that these sampling distributions are for the \emph{test statistic}; it could be the sample mean, sample proportion, sample correlation coefficient, sample difference in means, etc.

\begin{center}
%\includegraphics[width=0.65\textwidth]{img/statistical-power-chart.png}
\end{center}

\begin{mdframed}
\textbf{Question 2.8:} There are three ways to increase the power of a hypothesis test. (Hint: Have a look at the parameters in the formula for the $T$-test statistic, above.) Try to list them here:
\begin{enumerate}
\item ~
\item ~
\item ~
\end{enumerate}
\vspace{5mm}
\end{mdframed}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Sample Size Calculations}

For many statistical hypothesis tests, we can reverse the calculation and ask: how many samples do we need at a certain effect size to reject the null at a given significance level? Even in cases where you can't invert the test statistic itself, you can often use \textbf{bootstrapping}, \textbf{permutations}, etc. to simulate the appearance of the data under the null.

This graph shows the number of samples required to detect the effect size shown in the Appalachian town example in the slides ($\overline{x} = 125.45$, $\mu_0 = 139.75$) at varying significance levels: $\alpha = 0.1$ (orange line), $\alpha = 0.05$ (blue dotted line), and $\alpha = 0.01$ (turquoise dashed line).

\begin{center}
\includegraphics[width=0.5\textwidth]{img/hyp-z-test-power-curve-1.png}
\end{center}

Now let's vary the effect size. The graph below shows three lines, representing the sample sizes needed to reject the null at $\alpha = 0.1$ (solid), $0.05$ (dotted), and $0.01$ (dashed) significance levels, using a two-sided test.

\begin{center}
\includegraphics[width=0.5\textwidth]{img/hyp-z-test-power-curve-2.png}
\end{center}


%!TEX root = ../Thesis.tex
\chapter{Conclusions\label{ch:Conclusions}}
In this dissertation I have developed the first Monte Carlo implementation of Arnoldi's method for neutron transport.  This implementation uses explicitly restarted Arnoldi's method to estimate multiple eigenvalues of the transport-fission operator of the Boltzmann transport equation \eqref{eq:BoltzmannEquation}.

Using Arnoldi's method for estimating eigenvalues is a new technique in the Monte Carlo particle transport field; traditionally, the power method has been used.  Arnoldi's method has been used in the numerical analysis community for many years to estimate multiple eigenvalues and eigenvectors of a linear operator, but this is the first time it has been used with a Monte Carlo application of the linear operator.

I have demonstrated the ability to use Arnoldi's method to estimate up to three eigenvalues of the transport-fission operator for a variety of homogeneous and heterogeneous one-dimensional problems.  The eigenvalue estimates have been compared to and agree with published results and independent deterministic calculations within statistical uncertainty.  The eigenvectors have also been estimated and compared with deterministic calculations; again the results from Arnoldi's method are in harmony with the deterministic calculations.  In some situations, Arnoldi's estimated eigenvectors are improvements over the power method estimated eigenvectors.

Arnoldi's method can be used to calculate more than three eigenmodes.  Calculating additional higher-order eigenmodes will require additional iterations in a restart, and it may be necessary to track more particles in an iteration or use a finer spatial discretization.

Arnoldi's method requires the fission source to be discretized.  The simplest way to discretize the fission source is to use a constant in space or first-order accurate spatial approximation of the fission source which can, unfortunately, cause an error in the eigenvalue estimate if too few spatial bins are used to discretize the source.  I have implemented a second order accurate approximation to the fission source and have used it to reduce the error in the eigenvalue calculation by an order of magnitude for the same number of particles tracked.  The eigenvector estimates from the second order accurate approximation are a great improvement over the first order accurate approximation.  Rather than a jagged, step-wise approximation to the eigenvector, the second-order accurate approximation is a smooth and nearly continuous function across bins.  In addition, the figure of merit for the second-order accurate approximation is 2--3 times larger than the first order accurate approximation.

I have investigated relaxing the precision to which the transport-fission operator is applied at every Arnoldi iteration.  Studies have shown that relaxing Arnoldi's method has no effect on the convergence to the correct eigenvalues.  Relaxing Arnoldi's method in a Monte Carlo particle transport application simply involves tracking fewer particles in an iteration than was initially specified; tracking fewer particles is less computationally expensive and reduces the overall time for the simulation.

Relaxing Arnoldi's method for Monte Carlo criticality applications can save on computation time.  Relaxing too much, however,  can cause the eigenvalue estimates to be incorrect.  In addition, relaxing Arnoldi's method only a little can cause the figure of merit to be smaller than for a non-relaxed Arnoldi's method.  Relaxing Arnoldi's method turns out to be not a sufficient improvement to make it worthwhile to use in practice.

Two important topics currently being investigated in the Monte Carlo particle transport community are the underestimation of the variance of the mean eigenvalue estimate and the convergence of the fission source.  The power method \emph{under}estimates the variance because it ignores the correlation between power method iterations.  My implementation of Arnoldi's method also ignores the inter-iteration correlation, but the reported variance appears to be more conservative.  In one of the problems discussed in this dissertation, I have shown that Arnoldi's method \emph{over}estimates the uncertainty in the eigenvalue estimate by approximately 10\%.

The power method can take a long time to converge the fission source---especially for problems with a large dominance ratio.  In Monte Carlo criticality calculations, both the eigenvalue estimate and the fission source must be converged before tallying begins.  If convergence is slow, more iterations must be discarded and computation wasted.  I have shown that Arnoldi's method is superior to the power method in converging both the eigenvalue estimate and the fission source.  Arnoldi's method appears to converge both the eigenvalue estimate and the fission source immediately, while the power method can require several hundreds of iterations.

\section{Future Work \label{sec:FutureWork}}
Work on Monte Carlo Arnoldi's method for criticality calculations is far from complete.  This dissertation represents the first work performed in this field.  Some of the many topics that still need to be explored are described next.

\subsection{Implicit Restarts} \label{sec:IRAM}
One of the most intriguing ways that restarted Arnoldi's method could be improved is by implementing \emph{implicit} restarts.  Implicit restarts were developed by \citet{Sorensen:1992Impli-0} as a way to restart Arnoldi's method with an improved starting vector, and at the same time reduce the computational expense of the algorithm and increase the stability of maintaining orthogonality between Arnoldi vectors.

Implicitly restarted Arnoldi's method (IRAM), as it is called, performs iterations of the shifted \QR algorithm on the upper Hessenberg matrix, $H_m$, in the Arnoldi factorization in exchange of Arnoldi iterations.  It can be shown that performing these shifted \QR iterations allows Arnoldi's method to jump into the middle of the next restart, skipping several iterations.  IRAM is mathematically equivalent to picking a vector for the beginning of a new restart as a linear combination of the eigenvectors associated with the desired region of the spectrum of \A{} as we did in explicitly restarted Arnoldi's method.  The full derivation and proof of implicitly restarted Arnoldi's method is given in Appendix \ref{ch:IRAM}.

In Arnoldi's method, applying the linear operator \A{} is the most computationally expensive part, using greater than 80\% of the computer cycles in a given iteration.  Since the number of iterations in an Arnoldi restart is small, performing \QR iterations on $H_m$ will be inexpensive.  Trading computationally expensive applications of \A{} for inexpensive \QR iterations on $H_m$ should significantly reduce the computational expense of Arnoldi's method.

\subsection{Calculating Eigenvalue Estimates at Every Iteration}
In \Fref{ch:ArnoldiMethod} it was suggested that an Arnoldi restart could be treated similarly to the power method; that is, at the end of an Arnoldi restart an estimate for the eigenvalues are calculated and stored.  In the power method, an eigenvalue estimate is calculated at every iteration; since multiple iterations make up one Arnoldi restart, the power method has many more eigenvalue estimates than does Arnoldi's method for the same number of iterations and number of particles tracked.

The variance of the mean of the eigenvalue estimates goes as one over the square-root of the number of eigenvalue estimates.  Thus, the power method has an advantage over Arnoldi's method in that it has more eigenvalue estimates---the variance for the mean eigenvalue from the power method will almost certainly be smaller.

There is no reason why an eigenvalue estimate could not be calculated at every Arnoldi iteration instead of just at the end of a restart.  Of course, after the first iteration we could only estimate the fundamental eigenvalue; we would have to wait for additional iterations to estimate higher order eigenvalues.  It would be slightly more computationally expensive, but the decrease in the variance may be worth the extra expense.  In all of the calculations in which Arnoldi's method was directly compared to the power method, the figures of merit from the power method calculations were larger than the figures of merit for Arnoldi's method even though both methods took approximately the same amount of computational time.  The figure of merit is larger for the power method because the variance is smaller.  Calculating an eigenvalue estimate at every iteration would increase the number of estimates in Arnoldi's method, but is more computationally expensive.

A preliminary test has been performed to see how estimating the eigenvalue at each iteration might work.  I have repeated the 20 mfp simulation in \Fref{ch:ArnoldiMethod} from Arnoldi's method, but instead of estimating three eigenvalues only the fundamental eigenvalue is estimated and only two iterations are done per restart.  In each iteration 1E5 particles are tracked.  The number of restarts are 125 inactive and 500 active.  The total number of particles tracked and the total number of iterations is the same for this simulation and the power method and Arnoldi's method from \Fref{ch:ArnoldiMethod}.

The results of this simulation are given in \Fref{tab:N1Arnoldi}.  The results for the new simulation are given first and denoted with a star.  The eigenvalue estimate is within statistical uncertainty of the reference value of $\lambda_0 = 4.82780$.  We can see that the standard deviation of this simulation is identical to the power method simulation from \Fref{ch:ArnoldiMethod}, but that the figure of merit is larger.
\begin{table}[h] \centering
    \begin{tabular}{rccc}
        \toprule
        & $\lambda_0$ & $\sigma$ & FOM \\
        \midrule
        Arnoldi* & 4.82806 & 6.3\e{-4} & 6.6\e{3} \\
        Power    & 4.82734 & 6.3\e{-4} & 5.4\e{3} \\
        Arnoldi  &  4.8290 & 1.5\e{-3} & 1.1\e{3} \\
        \bottomrule
    \end{tabular}
    \caption{Eigenvalue estimate and figure of merit for Arnoldi's method (Arnoldi*) with just 2 iterations per restart and only saving the fundamental eigenmode.  Also included are results from \Fref{tab:BasicResults} for comparison.  (Reference $\lambda_0 = 4.82780$.)}
    \label{tab:N1Arnoldi}
\end{table}

\Fref{fig:N1ArnoldiValues} shows the eigenvalue estimate convergence as well as the Shannon Entropy.  In this particular simulation, Arnoldi's method does not converge the eigenvalue estimate or the Shannon entropy immediately as we have seen previously, but it still only requires a few restarts to converge.  \Fref{fig:N1ArnoldiVectors} shows the estimated fundamental eigenvector along with the reference solution.  Again we see that Arnoldi's method can accurately estimate the fundamental eigenvector.
\begin{figure} \centering
    \subfloat[Eigenvalue estimate and Shannon Entropy]{\label{fig:N1ArnoldiValues}\input{Conclusions/Data/N1ArnoldiValues}}

    \subfloat[Eigenvector]{\label{fig:N1ArnoldiVectors}\input{Conclusions/Data/N1ArnoldiVectors}}
    \caption{Preliminary calculation of eigenvalue estimates, eigenvector, and Shanon entropy from an Arnoldi's method with just 2 iterations per restart and only saving the fundamental eigenmode.  The black line is the reference solution.}
\end{figure}

These preliminary results show two important things.  First, if we are interested in just one eigenmode we should use a smaller Krylov subspace (fewer iterations per restart) and have more active restarts; this reduces the variance and increases the figure of merit. Second, we see that computing an eigenvalue estimate more frequently can reduce the variance for the mean eigenvalue.

\subsection{Condensing Arnoldi's Method}
One suggestion that has been made\footnote{very determinedly, in fact} for Arnoldi's method is to eliminate restarts and just do a few highly accurate iterations.  We have seen that Arnoldi's method does not need many inactive restarts to converge the fission source.  The only remaining reason to use many restarts is to obtain an estimation of the statistical uncertainty of the eigenvalue estimate.  However we also know that the estimate of the uncertainty is wrong.

It has been proposed that rather than performing many Arnoldi restarts, it may be beneficial to track many more particles during just one or two Arnoldi restarts with sufficient Krylov subspace size.  The application of \A{} would be performed much more accurately and the eigenvalue estimates would be much better.

With only one or two Arnoldi restarts the statistical uncertainty could not be calculated as described in this dissertation; the variance of just two estimates isn't helpful.  In principle the statistical uncertainty could be calculated by propagating the statistical error through the iterations of Arnoldi's method.  This would be a profound change in how statistical uncertainties are calculated in Monte Carlo eigenvalue computations, no one has done this before.

A preliminary simulation has been done using this idea.  We return to the 20 mfp slab geometry introduced in \Fref{ch:ArnoldiMethod}.  We will use the same total number of particles, but will put all of them into one inactive restart and one active restart; each restart has 10 iterations with 6.25\e{6} particles tracked in each iteration.

The eigenvalues from this simulation are given in \Fref{tab:CondensedArnoldi} and the estimated eigenvectors in \Fref{fig:CondensedArnoldiVectors}; the ``Condensed'' Arnoldi is Arnoldi's method with just two restarts, but many particles tracked in each iteration.  We see that the eigenvalue estimates are not exactly the same as the reference solution but that the eigenvector is an excellent estimate as with the regular Arnoldi's method.  However we do not have a way yet of estimating the statistical uncertainty of these values so we do not know if the eigenvalue estimates are within statistical uncertainty of the reference solution.
\begin{table}[h] \centering
    \begin{tabular}{rccc}
        \toprule
         & $\lambda_0$ & $\lambda_1$ & $\lambda_2$ \\
         \midrule
        Condensed Arnoldi & 4.8309 & 4.3824 & 3.8131 \\
        Reference & 4.8278 & 4.3831 & 3.8174 \\
        \bottomrule
\end{tabular}
    \caption{Eigenvalue estimates for 20 mfp thick slab geometry from a condensed Arnoldi's method and Reference eigenvalues from \cite{Garis:1991One-s-0}, and \cite{Dahl:1979Eigen-0}.}
    \label{tab:CondensedArnoldi}
\end{table}

\begin{figure} \centering
    \input{Conclusions/Data/CondensedArnoldiVectors}
    \caption{Preliminary calculation of eigenvectors from condensed Arnoldi.}
    \label{fig:CondensedArnoldiVectors}
\end{figure}

\subsection{Multi-dimensional and Real-world Problems}
All of the simulations demonstrated in this dissertation have been one-dimensional.  Restricting a prolem to one-dimension is sufficient for a proof-of-concept but it certainly does not represent a real-world problem.  As of yet, there have been no attempts at using Arnoldi's method in a three-dimensional, production code such as MCNP; any estimates of how Arnoldi's method may operate in three-dimensions is just speculation.

Most likely the biggest issue when moving to three-dimensions is the length of the Arnoldi vectors.  It is expected that the number of discretization bins necessary for three dimensions would be at least $N^3$ where $N$ is the number of bins for a one-dimensional problem.  Increasing the size of the Arnoldi vectors by this magnitude would increase the computational expense of orthogonalizing the Arnoldi vectors as well as the expense of sampling and scoring in and from a three-dimensional source.

The size of the Krylov subspace (number of iterations in a restart) necessary for a three-dimensional problem would seem to be greater than for a one-dimensional problem.  In this dissertation, the more difficult problems (i.e. problems with larger dominance ratios) required additional iterations for an accurate eigenvalue estimate.  A three-dimensional problem would almost certainly require more iterations than a one-dimensional problem and would therefore require more time.

Arnoldi's method, while having been used extensively in the numerical analysis community has yet to be incorporated in Monte Carlo particle transport algorithms.  This dissertation represents the first work in this area.  Arnoldi's method has many promising qualities; further investigation will determine the ability of Arnoldi's method to be used in production Monte Carlo codes.


\section{Summary and Background}
\label{sec:summary}

  A brief summary of \citetitle{qe2paper} follows. Supporting information and
  commentary on the methods presented is included as applicable.

  \subsection{Multigroup Neutron Diffusion and \glsentrylong{nem}}

    % write the equation
    In its conventional form, the multigroup neutron diffusion equation for a
    problem domain $\vr \in \Omega$ can be written
    \begin{equation}
      \label{eq:multigroup_diffusion}
      \grad \cdot \current_g(\vr) + \Sigma_{r,g}(\vr) \phi_g(\vr)=
        \frac{\chi_g(\vr)}{\lambda}
        \sum_{g'=1}^{G} \nu\Sigma_{f,g'}(\vr) \phi_{g'}(\vr) +
        \sum_{\substack{g'=1 \\ g' \ne g}}^{G}
        \Sigma_{s,g' \rightarrow g}(\vr) \phi_{g'}(\vr)
    \end{equation}
    where
    \begin{conditions} % custom environment designed for this purpose
      \vr & spatial position vector, \\
      \current_g(\vr) & net neutron current for energy group $g$
        \units{$\frac{1}{\text{cm}^2 \; \text{s}}$}, \\
      \phi_g(\vr) & fundamental eigenvector,
        scalar neutron flux for energy group $g$
        \units{$\frac{1}{\text{cm}^2 \; \text{s}}$}, \\
      \Sigma_{r,g}(\vr) & macroscopic removal cross section for energy group $g$
        \units{$\frac{1}{\text{cm}}$}, \\
      \chi_g(\vr) & fission spectrum for energy group $g$,\\
      \lambda & fundamental eigenvalue, effective neutron multiplication factor, \\
      \nu \Sigma_{f,g}(\vr) & number of fission neutrons times macroscopic fission
        cross section in energy group $g$ \units{$\frac{1}{\text{cm}}$}, \\
      \Sigma_{s,g' \rightarrow g} (\vr) & macroscopic scatter cross section from
        energy group $g'$ to energy group $g$ \units{$\frac{1}{\text{cm}}$}, \\
      G & total number of energy groups.
    \end{conditions}

    The three-dimensional domain, $\Omega$, can then be discretized into a
    structured grid of nodes. Next, the \gls{nem} is introduced. Fundamentally,
    the \gls{nem} projects the transverse integrated flux onto a set of basis
    functions. The transverse integrated multigroup neutron equation is given as
    \begin{equation}
      \label{eq:transverse_multigroup_diffusion}
      \frac{d \current_{g,u} (u)}{d u} + \overline{\Sigma_{r,g}}
        \phi_{g,u}(u) = Q_{g,u}(u) - L_{g,u}(u)
    \end{equation}
    where $u = x,\ y,\ z$ is a coordinate direction \cite{qe2paper}. The terms
    $Q_{g,u}(u)$ and $L_{g,u}(u)$ represent the transverse integrated neutron
    source and transverse leakage respectively. Note that in
    \eref{eq:transverse_multigroup_diffusion}, the node indices
    (i.e.~$i,j,k$) have been omitted and $\overline{\Sigma_{r,g}}$ represents
    the average value of $\Sigma_{r,g}(\vr)$ in the node. The transverse
    integrated current $\current_{g,u}$ is then approximated
    \begin{equation}
      \label{eq:current_approximation}
      \current_{g,u}(u) = - \overline{D_g} \, \frac{d \phi_{g,u}(u)}{du}
    \end{equation}
    where $\overline{D_g}$ is the average value of the diffusion coefficient in
    the node.

    \gls{nem} basis functions are typically polynomials and
    \citeauthor{qe2paper} select the Legendre polynomials. In their work, the
    scalar flux is projected onto quartic polynomials and the transverse
    integrated neutron source and transverse leakage terms are projected onto
    quadratic polynomials. The selection of quartic and quadratic polynomials
    respectively is consistent as the second derivative of the scalar flux is
    related to the source and leakage terms \cite{gehinThesis}. This projection
    can be written
    \begin{align}
      \label{eq:flux_expansion}
      \phi_{g,u}(u) &= \sum_{n=0}^{N_{\phi} = 4} a_{g,u,n} \, f_{u,n}(u), \\
      \label{eq:source_expansion}
      Q_{g,u}(u)    &= \sum_{n=0}^{N_Q = 2}      q_{g,u,n} \, f_{u,n}(u), \\
      \label{eq:leakage_expansion}
      L_{g,u}(u)    &= \sum_{n=0}^{N_L = 2}      l_{g,u,n} \, f_{u,n}(u),
    \end{align}
    where $a_{g,u,n}$, $q_{g,u,n}$, and $l_{g,u,n}$ are the expansion
    coefficients of $\phi_{g,u}(u)$, $Q_{g,u}(u)$, and $L_{g,u}(u)$ respectively
    and $f_{u,n}(u)$ is the $n^{th}$ Legendre polynomial \cite{qe2paper}. To
    solve for the coefficients of the expansions in \eref{eq:flux_expansion},
    \eref{eq:source_expansion}, and \eref{eq:leakage_expansion}, five equations
    are required. The selected equations are:
    \begin{enumerate}
      \item flux continuity condition,
      \item current continuity condition,
      \item nodal neutron balance ($f_0$ weighted residual),
      \item $f_1$ weighted residual, and
      \item $f_2$ weighted residual.
    \end{enumerate}
    Special attention is paid to boundary conditions and these conditions
    replace the flux and current continuity equations on the problem boundary.

  \subsection{Local Elimination}
    \label{sec:local_elimination}

    Recall the quantity of interest for this calculation is the fundamental
    eigenmode composed of eigenvalue, $\lambda$, and eigenvector,
    $\phi_g(\vr)$. The coefficients in \eref{eq:flux_expansion},
    \eref{eq:source_expansion}, and \eref{eq:leakage_expansion} are not
    desired directly, but must be solved in order to obtain the quantity of
    interest. It has been previously demonstrated that all coefficients need
    not be solved simultaneously which allows for the reduction of the
    dimensionality of the problem. The odd and even coefficients can be solved
    separately as a result of the five solution equations selected
    \cite{gehinThesis}.

    \citeauthor{qe2paper} then show that the number of coefficients can be
    further reduced as the odd coefficients can be expressed in terms of
    each other with similar results for the even coefficients. That is,
    instead of solving for $a_{g,u,1}$ and $a_{g,u,3}$, a new coefficient is
    introduced: $a_{g,u,1-3}$. Similarly, $a_{g,u,2-4}$ becomes a solution
    variable. This is an important and novel step of the reviewed work as the
    solution of \gls{nem} coefficients represents the bulk of the
    computational time and the number of coefficients required for solving the
    \gls{nem} equations has been reduced \cite{qe2paper}.

    Finally, the \gls{nem} equations are solved to obtain currents, scalar
    fluxes, $a_{g,u,1-3}$, and $a_{g,u,2-4}$. The resulting solution vector,
    $\vPhi_g$, can be written
    \begin{equation}
      \label{eq:solution_vector}
      \vPhi_g =
      \begin{pmatrix}
        \current_{g,x,+} \\
        \current_{g,y,+} \\
        \vspace{8pt}
        \current_{g,z,+} \\
        \overline{\phi_g} \\
        a_{g,x,1-3} \\
        a_{g,y,1-3} \\
        a_{g,z,1-3} \\
        a_{g,x,2-4} \\
        a_{g,y,2-4} \\
        a_{g,z,2-4}
      \end{pmatrix}
    \end{equation}
    and the total number of solving variables is $10 \times N \times G$ where
    $N$ is the total number of nodes and $G$ is the total number of energy
    groups. Note that the \gls{nem} coefficients and neutron current must be
    solved in each spatial direction. Typically, solving for the \gls{nem}
    coefficients themselves is not preferred as the values themselves are not
    quantities of interest.  The preferred \gls{cmfd} implementation computes
    a modified diffusion coefficient, $\dtilde$, and will be discussed in
    \sref{sec:cmfd_formulation}.

  \subsection{\glsentrylong{jfnk} Theory and Inexact Newton Condition}
    \label{sec:jfnk_theory}

    The \gls{jfnk} method begins with Newton's method. Newton's method provides
    a framework to find the root of a residual function as $\residual(\vx^*) =
    \vzero$ where $\vx^*$ is a root of the residual function. It is first
    necessary to construct a residual function based on the \gls{nem} equations.
    \citeauthor{qe2paper} propose a residual function that contains the residual
    of  the \gls{nem} solution, $\vPhi_g$, and the absolute change in the
    fundamental eigenvalue, $\lambda$, during the Newton iteration. This differs
    from the residual function proposed by \citeauthor{gill_azmy} but the new
    form may be preferable as it directly contains information about the
    fundamental eigenvalue, $\lambda$.

    The $m^{th}$ Newton step can be described
    \begin{equation}
      \label{eq:newton_step}
      \jacobian (\vx^m) \cdot \step^m = - \residual(\vx^m)
    \end{equation}
    where $\jacobian(\vx^m)$ is the Jacobian of $\residual(\vx^m)$ and
    $\step^m$ is the Newton step. Then, the Newton step proceeds as
    \begin{equation}
      \vx^{m+1} = \vx^{m} + \step^{m}.
    \end{equation}

    For the choice of residual function made by \citeauthor{qe2paper}, an
    analytic Jacobian is not simple to compute. Therefore, a finite difference
    approximation is used to estimate the Jacobian. In fact, the Jacobian itself
    is not needed as the Newton step in \eref{eq:newton_step} will be solved
    with a Krylov method. All that is needed is the finite difference
    Jacobian-vector product which is equivalent to a directional derivative. The
    directional derivative can be written
    \begin{equation}
      \label{eq:dirder}
      \jacobian(\vx^m) \cdot \vv \approx \frac{\residual(\vx^m + \epsilon \vv) -
        \residual(\vx^m)}{\epsilon}
    \end{equation}
    where $\vv$ is the direction and $\epsilon$ is the finite difference step
    size. Typically, $\epsilon$ is on the order of the square-root of machine
    precision, $\sqrt{\epsilon_{mach}} \approx 10^{-8}$
    \cite{qe2paper,gill_azmy,textbookkelley}. Since the Jacobian has been
    replaced by a directional derivative, the method is termed Jacobian-Free.

    As previously mentioned, the Newton step in \eref{eq:newton_step} is solved
    using a Krylov method as
    \begin{equation}
      \label{eq:inexact_newton_condition}
      \| \residual(\vx^m) + \jacobian(\vx^m) \cdot \step^m \| \le
        \eta_m \| \residual(\vx^m) \|
    \end{equation}
    where the tolerance $\eta_m$ is referred to as the forcing term.
    \citeauthor{qe2paper} investigated solving
    \eref{eq:inexact_newton_condition} using \gls{gmres} and \gls{bicgstab}
    Krylov methods and determined that \gls{bicgstab} performed preferably.
    \eref{eq:inexact_newton_condition} is termed the inexact Newton condition.
    It is known that the choice of forcing term, $\eta_m$, can significantly
    effect the convergence rate of the \gls{jfnk} method \cite{textbookkelley}.
    Essentially, it is undesirable to ``over-solve''
    \eref{eq:inexact_newton_condition}, especially during the initial Newton
    iterations.

    The Eisenstat-Walker forcing term is a standard choice for $\eta_m$ and has
    been shown to work well \cite{qe2paper,gill_azmy}. In both the work by
    \citeauthor{qe2paper} and the work by \citeauthor{gill_azmy}, the authors
    perform a study to determine a preferable choice of forcing term. The
    authors all determine that the Eisenstat-Walker forcing term is ideal.
    However, $\eta_m = 10^{-1}$ provides equally well and sometimes better
    results \cite{qe2paper,gill_azmy,jfnk_wielandt,ma784notes}. These results
    indicate that both the Eisenstat-Walker forcing term and $\eta_m = 10^{-1}$
    perform similarly and such a study is unnecessary in the future.

  \subsection{Choice of Physics-Based Preconditioner}

    The inexact Newton condition (discussed in
    \sref{sec:jfnk_theory}) is solved iteratively with a Krylov solver.
    The wise choice of preconditioner can significantly reduce the number of
    Krylov iterations required to solve a linear system. It can be shown that
    if one preconditions a matrix with its inverse, the Krylov method will
    converge in one iteration \cite{textbookkelley}. While computing the
    matrix inverse would defeat the purpose of the Krylov method, this result
    indicates that the preconditioning operator is selected to approximate the
    matrix inverse.

    \citeauthor{qe2paper} investigate only left-preconditioning. The
    left-preconditioned form of the inexact Newton condition can be written
    \begin{equation}
      \label{eq:left_precondition}
      \| \mm_L^{-1} \residual(\vx^m) + \mm_L^{-1} \left( \jacobian(\vx^m)
        \cdot \step^m \right) \| \le \eta_m \| \mm_L^{-1} \residual(\vx^m) \|
    \end{equation}
    where $\mm_L$ is the left-preconditioning operator. Note that in
    \eref{eq:left_precondition}, the residual that is reduced during a Krylov
    iteration is not directly related to the residual $\residual(\vx^m)$.
    Typically, this is not a problem. Right-preconditioning can also be
    considered but is not investigated in the proposed implementation.
    \citeauthor{gill_azmy} investigate both left- and right-preconditioning.

    An extensive review of the choice of preconditioner in \gls{jfnk}
    solutions to the multigroup neutron diffusion equation has been presented
    elsewhere \cite{gill_azmy}. It is expected that the results of the
    preconditioner study for the multigroup neutron diffusion equation should
    be applicable to the \gls{nem} equations. Preconditioners investigated
    include the diffusion operator, incomplete Cholesky factorization, the
    diffusion-fission operator, and preconditioning with a few \glspl{pi}. The
    results of the preconditioner study indicates that preconditioning with
    approximately five \glspl{pi} provided the fastest convergence of the
    \gls{jfnk} iterations. The incomplete Cholesky preconditioner provided
    similar convergence rate but requires additional and expensive matrix
    calculations. A \gls{pi} preconditioner was also investigated in work by
    \citeauthor{jfnk_wielandt} and similar performance was reported
    \cite{jfnk_wielandt}.

    The use of a \gls{pi} preconditioner was not investigated by
    \citeauthor{qe2paper}. Instead, the authors designed a new preconditioner
    based on the data available during the \gls{nem} iterations. Special
    attention is also paid to ensure the preconditioner can be solved using
    the \gls{tdma} and \gls{adi} methods. These methods are among the fastest
    numerical matrix solution techniques available. The authors incorporate
    information in the preconditioner from the \gls{nem} equations,
    group-to-group scattering, and transverse leakage contributions. The
    construction of the preconditioner appears to be computationally
    expensive. Unfortunately, \citeauthor{qe2paper} do not compare their
    preconditioner to any other preconditioner or even a case without a
    preconditioner. Therefore, it is impossible to determine the relative
    computational efficiency of this preconditioning method.

  \subsection{Convergence Rate Consideration}

    The $q$-quadratic convergence rate of the \gls{jfnk} method is what makes it
    desirable to implement. Therefore, it is important to compare the
    convergence behavior of the \gls{jfnk} method to the traditional \gls{pi}
    method.

    \subsubsection{Convergence of \glsentrylong{pi} Method}
      \label{sec:dominance_ratio}

      The convergence behavior of the \gls{pi} method is known and studied well
      \cite{nakamura,gehinThesis,my_ms_thesis}. The convergence rate of the
      \gls{pi} method is determined by the first and second largest eigenvalues
      of the equations of interest (either the multigroup neutron diffusion
      equation or \gls{nem} equations). It can be shown that each iteration of
      the \gls{pi} method reduces the error by a multiple termed the dominance
      ratio, $d$, defined as
      \begin{equation}
        \label{eq:dominance_ratio}
        d = \frac{\lambda_1}{\lambda_0}
      \end{equation}
      where $\lambda_0 = \lambda$ is the fundamental eigenvalue and $\lambda_1$
      is the next largest eigenvalue \cite{my_ms_thesis}. The \gls{pi} method
      converges linearly and the dominance ratio is the leading coefficient in
      the convergence behavior. For the multigroup neutron diffusion and
      \gls{nem} equations, $d$ is guaranteed to be less than one and smaller
      values of $d$ result in faster convergence \cite{nakamura}. For typical
      reactor simulations, $d > 0.9$ and often $d > 0.95$ implying convergence
      is slow, often requiring hundreds of iterations.

      The typical approach to address the challenge of large dominance ratios is
      the \gls{ws}~\cite{gehinThesis}. Briefly, the \gls{ws} subtracts a portion
      of the fission source from both sides of the equation. It can be shown
      that the dominance ratio is decreased \textit{significantly} such that $d
      \approx 0.5$ may be expected. It is common for the \gls{ws} to speedup
      the solution \gls{pi} method by a factor of three or more. This will be
      discussed more in \sref{sec:wielandt_shift}.

    \subsubsection{Convergence of Newton Method}

      When the iterate in the \gls{jfnk} method, $\vx^m$, is far from the
      solution, $\vx^*$, the method converges $q$-superlinearly. As the
      iteration approaches the solution, it achieves $q$-quadratic convergence
      \cite{textbookkelley}.

      It has been demonstrated that the dominance ratio of the multigroup
      neutron diffusion equation has little effect on the convergence rate when
      the equations are solved using the \gls{jfnk} method \cite{gill_azmy}.
      This is expected as the convergence rate of the \gls{jfnk} method is not
      related to the eigenvalues of the operator but is related to the
      properties of the Jacobian including its Lipschitz continuity
      \cite{textbookkelley}. These results indicate that the \gls{ws} would have
      little to no effect on the \gls{jfnk} method. However,
      \citeauthor{qe2paper} indicate in their concluding remarks that they
      expect the \gls{ws} to ``improve the efficiency further'' and this is
      inaccurate~\cite{qe2paper}.


\chapter{A quick theoretical background}
\label{chap:theoreticalbackground}

In this chapter we introduce briefly some essential terminology for supervised learning. Clearly, a complete mathematical treatment of the field is beyond the scope of this manual, thus we go only briefly over most of the introduced ideas. We refer to any reference textbook for an in-depth treatment.\cite{alpaydin2004introduction,Friedman2009,Mohri2012}

\section{The supervised learning problem}

The starting point in supervised learning is a set $S$, called a \textit{dataset}, composed of $N$ pairs of the form:

\begin{equation}
S = \left\{ x_i, y_i \right\}_{i=1}^N
\label{eq:dataset}
\end{equation}

\noindent where $x \in \mathcal{X}$ and $y \in \mathcal{Y}$. $\mathcal{X}$ and $\mathcal{Y}$ are called the \textit{input} and \textit{output} spaces, respectively.  We call a particular choice of $\mathcal{Y}$ a \textit{task}, partly for historical reasons with respect to previous versions of the toolbox. We list the input and output types currently supported by Lynx in Table \ref{tab:basicinputs} and Table \ref{tab:basictasks}, respectively. Although we have currently focused on real-valued input patterns, nothing prevents the toolbox from being extended to more complex situations, such as graphs, sequences, and so on.

We suppose that points in the two spaces are linked by an unknown probabilistic relationship, of which we only know the $N$ samples in $S$. Each sample is also called an \textit{example}, while each element $x \in \mathcal{X}$ is sometimes referred to as a \textit{pattern}.

Informally, the goal of supervised learning is finding a suitable function $f: \mathcal{X} \rightarrow \mathcal{Y}$ such that, given any new sampling $(x, y)$ of the unknown process, minimizes the expected error between $f(x)$ and $y$. Formally, a \textit{learning algorithm} $\mathcal{A}$ can be defined as a mapping between the space $\mathcal{S}$ of all possible datasets and a given \textit{hypothesis} of the unknown relation:

\begin{equation}
\mathcal{A}(S) : \mathcal{S} \rightarrow \mathcal{H}
\label{eq:learningalgorithm}
\end{equation}

\noindent where $\mathcal{H}$ is called the \textit{hypothesis space}, or \textit{model space}. The notion of error is instead formalized by defining a \textit{loss} function of the form:

\begin{equation}
L(S, f) :  \mathcal{S} \times \mathcal{H} \rightarrow \R
\label{eq:lossfunction}
\end{equation}

\noindent An example of Eq. (\ref{eq:lossfunction}) is the \textit{Mean-Squared Error} (MSE) given by:

\[
L(S, f) = \sum_{(x,y) \in S}(f(x) - y)^2
\]

\noindent In this framework, different learning algorithms differentiate themselves on the choice of the model space, or in the way in which a single hypothesis is extracted from $\mathcal{H}$ given a dataset. It is also possible to compute the performance of an algorithm in term of more complex measures, such as confusion matrices, ROC curves, correlation coefficients, and so on. In this case, we will talk more generally about \textit{performance measures}.
%
\ra{1.2}
\begin{center}
\begin{table}[t]
{\centering\hfill{}
	\begin{tabular}{p{0.25\columnwidth}p{0.6\columnwidth}}
		\toprule
		Name & Description \\
		\midrule
		Real-valued vector & $\vect{x} \in \R^d$, each element is a \textit{feature} of the pattern  \\
		Kernel vector &  $\vect{x} \in \R^n$, corresponds to a vector of similarities with respect to $n$ other patterns \\
		Time-series & $\vect{x} \in \R^n$, each element is the sample of a univariate time-series \\
		\bottomrule
	\end{tabular}}
	\hfill{}
	\caption{Summary of inputs defined in the toolbox.}
	\label{tab:basicinputs}
\end{table}
\end{center}

\ra{1.2}
\begin{center}
\begin{table}[t]
	{\centering\hfill{}
		\begin{tabular}{p{0.35\columnwidth}p{0.5\columnwidth}}
			\toprule
			Name & Description \\
			\midrule
			Regression & $y \in \R$  \\
			Binary classification & $y \in \left\{-1,+1\right\}$  \\
			Multiclass classification & $y \in \left\{1,\dots,M\right\}$, where $M$ is the number of classes  \\
			Multilabel classification & $\vect{y} \in \left\{-1,+1\right\}^L$, where $L$ is the number of binary labels associated to every pattern \\
			\bottomrule
		\end{tabular}}
		\hfill{}
		\caption{Summary of outputs (tasks) defined in the toolbox.}
		\label{tab:basictasks}
	\end{table}
\end{center}
	%
\vspace{-5em}
An extension of this basic framework that we have implemented inside Lynx is the \textit{semi-supervised} learning (SSL) framework.\cite{chapelle2006semi} In SSL, we suppose having available an additional unlabeled dataset of $M$ input patterns $U = \left\{ x_i \right\}_{i=1}^M$. An SSL algorithm uses this additional information trying to improve its own accuracy. Formally, if we denote by $\mathcal{U}$ the space of all possible unlabeled datasets, an SSL algorithm $\mathcal{A}_{\mbox{\scriptsize SSL}}$ is a mapping of the form:

\begin{equation}
\mathcal{A}_{\mbox{\scriptsize SSL}}(S,U): \mathcal{S} \times \mathcal{U} \rightarrow \mathcal{H}
\label{eq:ssl}
\end{equation}

\noindent where the difference with Eq. \eqref{eq:learningalgorithm} is the presence of the additional input $U$.

\section{Some additional terminology}

We call an \textit{experiment} the test of a particular learning algorithm on a dataset. For a realistic workflow, refer to Fig. \ref{fig:workflow}. The dataset, before being used, is processed by one (or more) \textit{preprocessors}. A typical example of preprocessor is the \textit{Principal Component Analysis} (PCA). The dataset is not used all for training, but it is partitioned in a \textit{training set} and a \textit{testing set}. The way in which data is subdivided is called a \textit{partitioning strategy}. A partitioning strategy can also define multiple splits for a single dataset (e.g., k-fold cross-validation procedures): in this case, each split is called a \textit{fold}. Finally, in Fig. \ref{fig:workflow} the learning algorithm is encapsulated inside a generic ``\textit{wrapper}'' object, that we define to be any method performing some additional optimization over the training procedure or the resulting model. This rather general definition includes techniques for optimizing the parameters of the algorithm, selecting a suitable subset of features, or training an ensemble of classifiers \cite{alpaydin2004introduction}.

\begin{figure}[t]
\centering
\includegraphics[scale=0.6]{./images/Disegno3}
\caption{Workflow of a single experiment}
\label{fig:workflow}
\end{figure}


\section{Elementary number theory}

One of the things type theory is great for, is for the formalization of mathematics in a computer proof assistant. Those are programs that can compile any type theoretical construction to check that this construction indeed has the type it was claimed it has.

At this point in our development of type theory there are two areas of mathematics that would be natural to try to do in type theory: discrete mathematics and elementary number theory. Indeed, how does one define in type theory the greatest common divisor of two natural numbers, or how does one show that there are infinitely many primes? How does one even formalize that every non-empty subset of the natural numbers has a least element?

To answer these questions we will run into questions of decidability. How do we write a term that decides wheter a number is prime or not? Or indeed, is it even true that every non-empty subset of the natural numbers has a least element? What about the subset of $\N$ that contains $1$, and it contains $0$ if and only if Goldbach's conjecture holds? Finding the least element of this subset is equivalent to settling the conjecture!

Therefore, we will prove the well-foundedness of the natural numbers for decidable subsets of $\N$. In fact, we will show it for decidable families, because sometimes we don't know in advance whether a family of types is in fact a subtype. A consequence of involving decidability in the well-foundedness of the natural numbers is that for many properties one has to prove that they are decidable. Luckily this is the case: many of the familiar properties that one encounters in number theory are indeed decidable.

\subsection{Decidability}

A common way of reasoning in mathematics is via a proof by contradiction: ``in order to show that $P$ holds we show that it cannot be the case that $P$ doesn't hold". There are no inference rules in type theory that allow us to obtain a term of type $P$ from a term of type $\neg\neg P$. However, for some propositions $P$ one can construct a function $\neg\neg P \to P$. The \emph{decidable propositions} from a class of such propositions $P$ for which we can show $\neg\neg P \to P$.

The following definition of decidability is made for general types, even though we will mostly be interested in the decidabilyt of proposition. The reason will become aparent in a moment, when we show that types with decidable equality are sets. This useful theorem would become trivial if we restricted the notion of decidability to propositions.

\begin{defn}
  A type $A$ is said to be decidable if it comes equipped with a term of type
  \begin{equation*}
    \isdecidable(A)\defeq A+\neg A.
  \end{equation*}
  Decidable propositions are called \define{classical}. We will write
  \begin{equation*}
    \classicalprop_\UU \defeq \sm{P:\prop_\UU}\isdecidable(P)
  \end{equation*}
  for the type of all classical propositions (with respect to a universe $\UU$).
\end{defn}

\begin{eg}\label{eg:classical-prop}
  The types $\unit$ and $\emptyt$ are decidable. Indeed, we have
  \begin{align*}
    \decunit & \defeq \inl(\ttt) & & :\isdecidable(\unit) \\
    \decemptyt & \defeq \inr(\idfunc) & & : \isdecidable(\emptyt).\qedhere
  \end{align*}
  Any type $A$ equipped with a point $a:A$ is decidable.
\end{eg}

Since $P$ and $\neg P$ are mutually exclusive cases, it follows that $\isdecidable(P)$ is a proposition. Therefore we see that the type of decidable propositions in a universe $\UU$ form a subtype of the type of all propositions in $\UU$.

\begin{lem}\label{lem:isprop-isdecidable}
  For any proposition $P$, the type $\isdecidable(P)$ is a proposition.
\end{lem}

\begin{proof}
  By \cref{lem:isprop_eq} it suffices to show that
  \begin{equation*}
    \prd{t,t':\isdecidable(P)}t=t.
  \end{equation*}
  We proceed by case analysis on $t$ and $t'$. We have four cases to consider:
  \begin{align*}
    \inl(p) & =\inl(p') & \inr(f) & =\inl(p') \\
    \inl(p) & =\inr(f') & \inr(f) & =\inr(f').
  \end{align*}
  We construct these four identifications as follows:
  \begin{enumerate}
  \item First, we want to show that $\inl(p)=\inl(p')$ for any $p,p':P$. We obtain this identification from the fact that $p=p'$, which we have because $P$ is assumed to be a proposition.
  \item Next, we want to show that $\inl(p)=\inr(f')$ for any $p:P$ and $f':\neg P$. Since we have contradictory assumptions, we obtain $f'(p):\emptyt$. We now obtain the desired identification by applying the function $\emptyt \to (\inl(p)=\inr(f')$.
  \item The construction of an identification $\inr(f)=\inl(p')$ for $f:\neg P$ and $p':P$ is similar. We have $f(p'):\emptyt$, which gives the desired identification via the function $\emptyt\to (\inr(f)=\inl(p'))$.
  \item Finally, we want to show that $\inr(f)=\inr(f')$ for $f,f':\neg P$. The type $\neg P$ is a proposition, so we have an identification $f=f'$ from which we obtain $\inr(f)=\inr(f')$.\qedhere
  \end{enumerate}
\end{proof}

We have seen in \cref{thm:propositional-extensionality} that the univalence axiom implies propositional extensionality. Recall that propositional extensionality is the property that the map
\begin{equation*}
  (P=Q)\to (P\leftrightarrow Q)
\end{equation*}
is an equivalence. We will use this fact here to conclude that $\classicalprop_\UU$ is equivalent to $\bool$.

\begin{prp}
  The type of classical propositions in any universe $\UU$ is equivalent to $\bool$.%
  \index{classical-Prop_U@{$\classicalprop_\UU$}!classical-Prop_U bool@{$\classicalprop_\UU\eqvsym\bool$}}
\end{prp}

\begin{proof}
  Since the empty type and the unit type are decidable propositions, we have a map $\varphi:\bool\to\classicalprop_\UU$ defined by
  \begin{align*}
    \varphi(\btrue) & \defeq (\unit,\decunit) \\
    \varphi(\bfalse) & \defeq (\emptyt,\decemptyt).
  \end{align*}
  Next, we define a map $\psi:\classicalprop_\UU\to\bool$. Let $P$ be a proposition that comes equipped with a term $t:P+\neg P$. To define a boolean, we proceed by case analysis on $t$. The map $\psi$ is thus defined by
  \begin{align*}
    \psi(P,\inl(p)) & \defeq \btrue \\
    \psi(P,\inr(f)) & \defeq \bfalse.
  \end{align*}
  To see that $\psi$ is an inverse of $\varphi$, note that
  \begin{align*}
    \varphi(\psi(P,\inl(p))) & \jdeq (\unit,\decunit) & \psi(\varphi(\btrue)) & \jdeq \btrue \\
    \varphi(\psi(P,\inr(f))) & \jdeq (\emptyt,\decemptyt) & \psi(\varphi(\bfalse)) & \jdeq \bfalse.
  \end{align*}
  It is therefore immediate that $\psi$ is a retract of $\varphi$. However, in order to show that $\psi$ is a section of $\varphi$ we still need to show that
  \begin{align*}
    (\unit,\decunit) & = (P,\inl(p)) \\
    (\emptyt,\decemptyt) & = (P,\inr(f)).
  \end{align*}
  Since $\isdecidable(P)$ is shown to be a proposition in \cref{lem:isprop-isdecidable}, it suffices to show that
  \begin{align*}
    \unit & = P & & \text{if we have }p:P \\
    \emptyt & = P & & \text{if we have }f:\neg P.
  \end{align*}
  In both cases we proceed by propositional extensionality. Therefore we obtain the desired identifications by observing that
  \begin{align*}
    \unit & \leftrightarrow P & & \text{if we have }p:P \\
    \emptyt & \leftrightarrow P & & \text{if we have }f:\neg P.\qedhere
  \end{align*}
\end{proof}

We will now study the concept of decidable equality.

\begin{defn}
  We say that a type $A$ has \define{decidable equality} if the identity type $x=y$ is decidable for every $x,y:A$. Types with decidable equality are also called \define{discrete}.
\end{defn}

\begin{lem}
  For each $m,n:\N$, the types $\EqN(m,n)$, $m\leq n$ and $m<n$ are decidable.
\end{lem}

\begin{proof}
  The proofs in each of the three cases is similar, so we only show that $\EqN(m,n)$ is decidable for each $m,n:\N$. This is done by induction on $m$ and $n$. Note that the types
  \begin{align*}
    \EqN(\zeroN,\zeroN) & \jdeq \unit \\
    \EqN(\zeroN,\succN(n)) & \jdeq \emptyt \\
    \EqN(\succN(m),\zeroN) & \jdeq \emptyt
  \end{align*}
  are all decidable. Moreover, the type $\EqN(\succN(m),\succN(n))\jdeq \EqN(m,n)$ is decidable by the inductive hypothesis.
\end{proof}

\begin{cor}
  Equality on the natural numbers is decidable.
\end{cor}

\begin{proof}
  Recall from the proof of \cref{thm:eq_nat} that the canonical map
  \begin{equation*}
    (m=n)\simeq \EqN(m,n)
  \end{equation*}
  is an equivalence. Thus we obtain that $(m=n)$ is decidable from the fact that $\EqN(m,n)$ is decidable.
\end{proof}

\begin{comment}
\begin{lem}
  Suppose that $A$ and $B$ are types with decidable equality. Then the coproduct $A+B$ also has decidable equality.
\end{lem}

\begin{proof}
  Our goal is to construct a dependent function
  \begin{equation*}
    d_{A+B} : \prd{z,z':A+B}\isdecidable(z=z').
  \end{equation*}
  This function is constructed by coproduct induction on both $z$ and $z'$, so we have four cases to consider. Recall from \cref{thm:id-coprod-compute} that we have equivalences
  \begin{align*}
    (\inl(x)=\inl(x')) & \simeq (x=x') \\
    (\inl(x)=\inr(y')) & \simeq \emptyt \\
    (\inr(y)=\inl(x')) & \simeq \emptyt \\
    (\inr(y)=\inr(y')) & \simeq (y=y').
  \end{align*}
  Therefore the type $z=z'$ is equivalent to a decidable type in each of the four cases.
\end{proof}

\begin{cor}
  The type $\Z$ has decidable equality.
\end{cor}

\begin{cor}
  For any $n:\N$ the type $\Fin(n)$ has decidable equality.
\end{cor}
\end{comment}

We have already shown in \cref{thm:eq_nat} that the type of natural numbers is a set. In fact, any type with decidable equality is a set. This fact is known as Hedberg's theorem.

\begin{thm}[Hedberg]
  Any type with decidable equality is a set.
\end{thm}

\begin{proof}
  Let $A$ be a type, and let
  \begin{equation*}
    d:\prd{x,y:A}(x=y)+\neg(x=y).
  \end{equation*}
  Recall from \cref{ex:dne-dec} that $(A+\neg A)\to (\neg\neg A\to A)$ for any type $A$, so we obtain that
  \begin{equation*}
    \prd{x,y:A}\neg\neg(x=y)\to (x=y).
  \end{equation*}
  Now observe that $\neg\neg(x=y)$ is a proposition for each $x,y:A$, and that the relation $x,y\mapsto\neg\neg(x=y)$ is reflexive. Therefore we are in position to apply \cref{lem:prop_to_id} and we conclude that $A$ is a set.
\end{proof}

\subsection{The well-ordering principle for decidable families over \texorpdfstring{$\N$}{ℕ}}

\begin{defn}
  A family $P$ over a type $A$ is said to be decidable if $P(x)$ is decidable for every $x:A$. A \define{decidable subset} of a type $A$ is a map
  \begin{equation*}
    P:A\to\classicalprop.
  \end{equation*}
\end{defn}

\begin{defn}
  Let $P$ be a decidable family over $\N$, and let $n:\N$ be a natural number equipped with $p:P(n)$. We say that $n$ is a \define{minimal $P$-element} if it comes equipped with a term of type
  \begin{equation*}
    \isminimal_P(n,p)\defeq \Big(\prd{m:\N}P(m)\to (n\leq m)\Big)
  \end{equation*}
\end{defn}

Note that the type $\isminimal_P(n,p)$ doesn't depend on $p$. However, it doesn't make much sense that $n$ is a minimal element of $P$ unless we already know that $n$ is in $P$. Indeed, if we would omit the hypothesis that $n$ is in $P$, it would be more accurate to say that $n$ is a \emph{lower bound} of $P$. The following theorem is the well-ordering principle of $\N$.

\begin{thm}
  Let $P$ be a decidable family over $\N$. Then there is a function
  \begin{equation*}
    \Big(\sm{n:\N}P(n)\Big)\to\Big(\sm{m:\N}{p:P(m)}\isminimal_P(m,p)\Big).
  \end{equation*}
\end{thm}

\begin{proof}
  Consider a universe $\UU$ that contains $P$. We show by induction on $n:\N$ that there is a function
  \begin{equation*}
    Q(n)\to \Big(\sm{m:\N}{p:Q(m)}\isminimal_Q(m,p)\Big)
  \end{equation*}
  for every decidable family $Q:\N\to\UU$. Note that we performed a swap in the order of quantification, using the universe that contains $P$. This slightly strengthens the inductive hypothesis, which we will be able to exploit.

  The base case is trivial, since $\zeroN$ is the least natural number. For the inductive step, suppose that $Q(\succN(n))$ holds. Note that $Q(\zeroN)$ is assumed to be decidable, so we proceed by case analysis on $Q(\zeroN)+\neg Q(\zeroN)$. Given $q:Q(\zeroN)$, it follows immediately that $\zeroN$ must be minimal. In the case where $\neg Q(\zeroN)$, we consider the decidable subset $Q'$ of $\N$ given by
  \begin{equation*}
    Q'(n)\defeq Q(\succN(n)).
  \end{equation*}
  Since we have $q:Q'(n)$, we obtain a minimal element in $Q'$ by the inductive hypothesis. Of course, by the assumption that $Q(\zeroN)$ doesn't hold, the minimal element of $Q'$ is also the minimal element of $Q$.
\end{proof}

\subsection{The strong induction principle of \texorpdfstring{$\N$}{N}}

\begin{thm}
  For any type family $P$ over $\N$ there an operation
  \begin{equation*}
    \strongindN : P(\zeroN)\to\Big(\prd{n:\N}\Big(\prd{m:\N}(m\leq n)\to P(m)\Big)\to P(n+1)\Big)\to \Big(\prd{n:\N}P(n)\Big).
  \end{equation*}
  Moreover, the operation $\strongindN$ comes equipped with identifications
  \begin{align*}
    \strongindN(p_0,p_S,\zeroN) & = p_0 \\
    \strongindN(p_0,p_S,n+1) & = p_S(n,(\lam{m}\lam{p}\strongindN(p_0,p_S,m))),
  \end{align*}
  for any $p_0:P(\zeroN)$ and $p_S:\prd{n:\N}\Big(\prd{m:\N}(m\leq n)\to P(m)\Big)\to P(n+1)$.
\end{thm}

\begin{proof}
  Consider
  \begin{align*}
    p_0 & : P(\zeroN) \\
    p_S & : \prd{n:\N}\Big(\prd{m:\N}(m\leq n)\to P(m)\Big)\to P(n+1)
  \end{align*}

  First, we claim that there is a function
  \begin{equation*}
    \tilde{p}_0 : \prd{m:\N}(m\leq\zeroN)\to P(m)
  \end{equation*}
  that comes equipped with an identification
  \begin{equation*}
    \tilde{p}_0(\zeroN,p)=p_0
  \end{equation*}
  for any $p:\zeroN\leq\zeroN$. The fact that we have such a dependent function $\tilde{p}_0$ follows immediately by induction on $m$ and $p:m\leq \zeroN$.

  Next, we claim that there is a function
  \begin{equation*}
    \tilde{p}_S : \prd{n:\N}\Big(\prd{m:\N}(m\leq n) \to P(m)\Big)\to \Big(\prd{m:\N}(m\leq n+1)\to P(m)\Big)
  \end{equation*}
  equipped with a homotopy
  \begin{equation*}
    \prd{m:\N}\prd{q:m\leq n}{p:m\leq n+1} \tilde{p}_S(n,H,m,p) = H(m,q)
  \end{equation*}
  and an identification
  \begin{equation*}
    \tilde{p}_S(n,H,n+1,p)=p_S(n,H)
  \end{equation*}
  for every $p:n+1\leq n+1$.

  Using $\tilde{p}_0$ and $\tilde{p}_S$, we obtain by induction on $n$ a function
  \begin{equation*}
    \tilde{s}:\prd{n:\N}\prd{m:\N} (m\leq n)\to P(m)
  \end{equation*}
  satisfying the computation rules
  \begin{align*}
    \tilde{s}(\zeroN) & \jdeq \tilde{p}_0 \\
    \tilde{s}(n+1) & \jdeq \tilde{p}_S(n,\tilde{s}(n)).
  \end{align*}
  Now we define
  \begin{equation*}
    \strongindN(p_0,p_S,n) \defeq \tilde{s}(n,n,\reflleqN(n)),
  \end{equation*}
  where $\reflleqN(n):n\leq n$ is the proof of reflexivity of $\leq$.

  It remains to show that $\strongindN$ satisfies the identifications claimed in the statement of the theorem. The identification that computes $\strongindN$ at $\zeroN$ is easy to obtain:
  \begin{align*}
    \strongindN(p_0,p_S,\zeroN) & \jdeq \tilde{s}(\zeroN,\zeroN,\reflleqN(\zeroN)) \\
                                & \jdeq \tilde{p}_{0}(\zeroN,\reflleqN) \\
                                & = p_0.
  \end{align*}
  To construct the identification that computes $\strongindN$ at a successor, we start with a similar computation:
  \begin{align*}
    \strongindN(p_0,p_S,n+1) & \jdeq \tilde{s}(n+1,n+1,\reflleqN(n+1)) \\
                                   & \jdeq \tilde{p}_S(n,\tilde{s}(n),n+1,\reflleqN(n+1)) \\
    & = p_S(n,\tilde{s}(n))
  \end{align*}
  Thus we see that, in order to show that
  \begin{equation*}
    p_S(n,\tilde{s}(n))=p_S(n,(\lam{m}\lam{p}\tilde{s}(m,m,\reflleqN(m)))),
  \end{equation*}
  we need to prove that
  \begin{equation*}
    \tilde{s}(n)=\lam{m}\lam{p}\tilde{s}(m,m,\reflleqN(m)).
  \end{equation*}
  Here we apply function extensionality, so it suffices to show that
  \begin{equation*}
    \tilde{s}(n,m,p)=\tilde{s}(m,m,\reflleqN(m))
  \end{equation*}
  for every $m:\N$ and $p:m\leq n$. We proceed by induction on $n:\N$. The base case is trivial. For the inductive step, we note that
  \begin{align*}
    \tilde{s}(n+1,m,p)=\tilde{p}_S(n,\tilde{s}(n),m,p)=\begin{cases}\tilde{s}(n,m,p) & \text{if }m\leq n \\
    p_S(n,\tilde{s}(n)) & \text{if }m=n+1.\end{cases}
  \end{align*}
  Therefore it follows by the inductive hypothesis that
  \begin{equation*}
    \tilde{s}(n+1,m,p)=\tilde{s}(m,m,\reflleqN(m))
  \end{equation*}
  if $m\leq n$ holds. In the remaining case, where $m=n+1$, note that we have
  \begin{align*}
    \tilde{s}(\succN,\succN,\reflleqN(\succN)) & = \tilde{p}(n,\tilde{s}(n),n+1,\reflleqN(n+1)) \\
    & = p_S(n,\tilde{s}(n)).
  \end{align*}
  Therefore we see that we also have an identification
  \begin{equation*}
    \tilde{s}(n+1,m,p)=\tilde{s}(m,m,\reflleqN(m))
  \end{equation*}
  when $m=n+1$. This completes the proof of the strong induction principle for $\N$.
\end{proof}

\subsection{Defining the greatest common divisor}

\begin{lem}
  For any $d,n:\N$, the type $d\mid n$ is decidable.
\end{lem}

\begin{proof}
  We give the proof by case analysis on $(d=\zeroN)+(d\neq\zeroN)$. If $d=\zeroN$, then $d\mid n$ holds if and only if $\zeroN=n$, which is decidable.

  If $d\neq\zeroN$, then it follows that $n\leq nd$. Therefore we obtain by the well-ordering principle of the natural numbers a minimal $m:\N$ that satisfies the decidable property $n\leq md$. Now we observe that $d\mid n$ holds if and only if $n=md$, which is decidable.
\end{proof}

\begin{defn}
  A type family $P$ over $\N$ is said to be \define{bounded from above} by $m$ for some natural number $m$, if it comes equipped with a term of type
  \begin{equation*}
    \isbounded_m(P) \defeq \prd{n:\N}P(n)\to (n\leq m).
  \end{equation*}
\end{defn}

\begin{defn}
  Let $P$ be a type family over $\N$, and consider $p:P(n)$. We say that $n$ is the maximal $P$-number if it comes equipped with a term of type
  \begin{equation*}
    \ismaximal_P(n,p) \defeq \prd{m:\N} P(m)\to m\leq n.
  \end{equation*}
\end{defn}

In the following lemma we show that if a decidable family $P$ is bounded from above and inhabited, then it possesses a maximal element.

\begin{lem}\label{lem:maximal}
  Consider a decidable type family $P$ over $\N$ which is bounded from above by $m$. Then there is a function
  \begin{equation*}
    \maximum_P:\Big(\sm{n:\N}P(n)\Big)\to\Big(\sm{n:\N}{p:P(n)}\ismaximal_P(n,p)\Big).
  \end{equation*}
\end{lem}

\begin{proof}
  We define the asserted function by induction on $m$. In the base case, if we have $p:P(n)$, then it follows from $n\leq 0$ that $n=0$. It follows by the boundedness of $P$ that $(n,p)$ is maximal.

  In the inductive step we proceed by case analysis on $P(\succN(m))$. This is allowed because $P$ is decidable. If we have $q:P(\succN(m))$, then it follows by the boundedness of $P$ that $(\succN(m),q)$ is maximal. If $\neg P(\succN(m))$, then it follows that $P$ is bounded by $m$, which allows us to proceed by recursion.
\end{proof}

\begin{defn}
  For any two natural numbers $m,n$ we define the \define{greatest common divisor} $\gcd(m,n)$, which satisfies the following two properties:
  \begin{enumerate}
  \item We have both $\gcd(m,n)\mid m$ and $\gcd(m,n)\mid n$.
  \item For any $d:\N$ we have $d\mid \gcd(m,n)$ if and only if both $d\mid m$ and $d\mid n$ hold.
  \end{enumerate}
\end{defn}

\begin{proof}[Construction]
  Consider the type family $P(d)\defeq (d\mid m)\times (d\mid n)$. Then $P$ is bounded from above by $m$. Moreover, $P(1)$ holds since $1\mid n$ for any natural number $n$. Furthermore, the divisibility relation is decidable, so it follows that $P$ is a family of decidable types. Now the greatest common divisor is defined as the maximal $P$-element, which is obtained by \cref{lem:maximal}
\end{proof}

\subsection{The Euclidean algorithm}

It was immediate from our definition of the greatest common divisor of $a$ and $b$ that it indeed divides both $a$ and $b$, and that it is the greatest such number. However, as a program that is supposed to \emph{compute} the greatest common divisor of $a$ and $b$ it performs rather poorly: it checks for every $n$ from $1$ until either $a$ or $b$ whether it is a divisor of both $a$ and $b$, and only then it gives as output the largest common divisor that it has found. In this section we give a new definition of an operation
\begin{equation*}
  \gcdeuclid:\N \to (\N \to \N)
\end{equation*}
following Euclid's algorithm, with the opposite qualities: it will compute rather quicky a value for $\gcdeuclid(a,b)$, but it will be left as something to show that this value is indeed the greatest common divisor of $a$ and $b$.

\begin{defn}
  We define a binary operation
  \begin{equation*}
    \gcdeuclid:\N \to (\N\to\N).
  \end{equation*}
\end{defn}

\begin{proof}
  We will define the operation $\gcdeuclid$ with the \emph{strong} induction principle for $\N$, which was given as \cref{ex:strong-induction}. Thus it suffices to construct a function $\N\to\N$, which will provide the values for $\gcdeuclid(\zeroN)$, and a function
  \begin{equation*}
    h_a:\Big(\prd{x:\N}(x\leq a) \to \N\to\N\Big)\to (\N\to\N),
  \end{equation*}
  for every $a:\N$, which will provide the values for $\gcdeuclid(a+1)$.

  In the base case, we simply define
  \begin{equation*}
    \gcdeuclid(\zeroN)\defeq\idfunc.
  \end{equation*}
  For the inductive step, consider a family of maps $F_x:\N\to\N$ indexed by $x\leq a$. We think of $F_x(b)$ as the value for $\gcdeuclid(x,b)$, so our assumption of having such a family of maps $F_x$ is really the assumption that $\gcdeuclid(x,b)$ is defined for every $x\leq a$. Our goal is to construct a map
  \begin{equation*}
    \gcdeuclid(a+1):\N\to\N
  \end{equation*}
  We proceed by strong induction on $b:\N$. In the base case, we define
  \begin{equation*}
    \gcdeuclid(a+1,\zeroN)\defeq a+1.
  \end{equation*}
  For the inductive step, assume that we have a number $G_y:\N$ for every $y\leq b$. Observe that $(b\leq a)+(a<b)$ holds for any $b:B$, see \cref{ex:order_N}. Thus we can proceed by case analysis to define
  \begin{equation*}
    h_a(F,b+1)\defeq
    \begin{cases}
      F_{(a+1)-(b+1)}(b+1) & \text{if }b\leq a\\
      G_{(b+1)-(a+1)} & \text{if }a<b.
    \end{cases}
  \end{equation*}
  This completes the inductive step, and hence we obtain a binary operation
  $\gcdeuclid$ that satisfies
  \begin{align*}
    \gcdeuclid(\zeroN,b) & \jdeq b \\
    \gcdeuclid(a+1,\zeroN) & \jdeq a+1 \\
    \gcdeuclid(a+1,b+1) & \jdeq \gcdeuclid((a+1)-(b+1),b+1) & & \text{if }b\leq a.\\
    \gcdeuclid(succN(a),b+1) & \jdeq \gcdeuclid(a+1,(b+1)-(a+1)) & & \text{if }a<b.\qedhere
  \end{align*}
\end{proof}

\begin{prp}
  For each $a,b:\N$, the number $\gcdeuclid(a,b)$ is the greatest common divisor of $a$ and $b$.
\end{prp}

\subsection{The trial division primality test}

\begin{thm}
  For any $n:\N$, the proposition $\isprime(n)$ is decidable.
\end{thm}

It is important to note that, even when we prove that a type such as $\isprime(n)$ is decidable, it is only after we \emph{evaluate} the proof term that we know whether the type under consideration has a term or not. In other words, for any given $n$ we don't know right away whether it is prime or not. Evaluating whether $n$ is prime can be computationally costly, so it may be desirable in any specific situation to give a separate mathematical \emph{argument} that decides whether or not the number is prime.

\subsection{Prime decomposition}

We will show now that any natural number $n>0$ can be written as a product of primes
\begin{equation*}
  n=p_1^{k_1}\cdots p_{m}^{k_m}
\end{equation*}
This prime decomposition is unique if we require that the primes $p_i<p_{i+1}$ for each $0<i<m$. In order to establish these facts in type theory, we first have to define finite products.

\subsection{The infinitude of primes}

\begin{thm}
  There are infinitely many primes.
\end{thm}

\begin{proof}
  We will show that for every $n:\N$ there is a prime number that is larger than $n$. In other words, we will construct a term of type
  \begin{equation*}
    \prd{n:\N}\sm{p:\N}\isprime(p)\times (n\leq p).
  \end{equation*}
  Note that the number $n!+1$ is relatively prime to any number $m\leq n$. Therefore the primes in its prime factorization must all be larger than $n$. Thus, the function that assigns to $n$ the least prime factor of $n!+1$ shows that for any $n:\N$ there is a prime number $p$ that is larger than $n$.
\end{proof}

\begin{cor}
  There is a function
  \begin{equation*}
    \primetype : \N \to \sm{p:\N}\isprime(p)
  \end{equation*}
  that sends $n$ to the $n$-th prime. This function is strictly monotone, so it is an embedding.
\end{cor}

\begin{exercises}
  \exercise Show that for any $f:\Fin(m)\to\Fin(n)$ and any $i:\Fin(n)$, the type $\fib{f}{i}$ is decidable.
  \exercise Consider a decidable type $P(i)$ indexed by $i:\Fin(n)$.
  \begin{subexenum}
  \item Show that the type
    \begin{equation*}
      \prd{i:\Fin(n)}P(i)
    \end{equation*}
    is decidable.
  \item Show that the type
    \begin{equation*}
      \sm{i:\Fin(n)}P(i)
    \end{equation*}
    is decidable.
  \end{subexenum}
  \exercise
  \begin{subexenum}
  \item Show that $\N$ and $\bool$ have decidable equality. Hint: to show that $\N$ has decidable equality, show first that the successor function is injective.
  \item Show that if $A$ and $B$ have decidable equality, then so do $A+B$ and $A\times B$. Conclude that $\Z$ has decidable equality.
  \item Show that if $A$ is a retract of a type $B$ with decidable equality, then $A$ also has decidable equality.
  \end{subexenum}
  \exercise Define the prime-counting function $\pi:\N\to\N$.
  \exercise (The Cantor-Schr\"oder-Bernstein theorem) Let $X$ and $Y$ be two sets with decidable equality, and consider two maps $f:X\to Y$ and $g:Y\to X$, both of which we assume to be injective. Construct an equivalence $X\simeq Y$.
  \exercise For any $k:\Z$, define a function $i\mapsto i+k \mod n$ of type $\Fin(n)\to\Fin(n)$. Show that this function is an equivalence.
  \exercise For any $k:\Z$, define a function $i\mapsto i\cdot k \mod n$ of type $\Fin(n)\to\Fin(n)$. Show that this function is an equivalence if and only if $\gcd(n,k)=1$.
  \exercise Show that
  \begin{equation*}
    \sum_{i=0}^n \binom{n-i}{i}=F_{n+1}
  \end{equation*}
  \exercise Show that if $2^n-1$ is prime, then $n$ is prime.
  \exercise Prove Fermat's little theorem.
  \exercise Extend the definition of the greatest common divisor to all integers.
  \exercise Show that
  \begin{equation*}
    (\Fin(m)\simeq\Fin(n))\leftrightarrow(m=n).
  \end{equation*}
  \exercise Show that $\N$ satisfies \define{ordinal induction}, i.e., construct for any type family $P$ over $\N$ a function of type
  \begin{equation*}
    \ordindN : \Big(\prd{k:\N} \Big(\prd{m:\N} (m< k) \to P(m)\Big)\to P(k)\Big) \to \prd{n:\N}P(n).
  \end{equation*}
  Moreover, prove that
  \begin{equation*}
    \ordindN(h,n)=h(n,\lam{m}\lam{p}\ordindN(h,m))
  \end{equation*}
  for any $n:\N$ and any $h:\prd{k:\N}\Big(\prd{m:\N}(m<k)\to P(m)\Big)\to P(k)$.
  \exercise
  \begin{subexenum}
  \item Show that if $A$ and $B$ have decidable equality, then so do the types $A+B$ and $A\times B$.
  \item Show that $\Z$ and $\Fin(n)$ have decidable equality, for every $n:\N$.
  \end{subexenum}
  \exercise Let $P:\N\to\classicalprop$ be a decidable subset of $\N$.
  \begin{subexenum}
  \item Show that $\sm{m:\N}{p:P(m)}\isminimal_P(m,p)$ is a proposition.
  \item Show that the map
    \begin{equation*}
      \Big(\sm{n:\N}P(n)\Big)\to\Big(\sm{m:\N}{p:P(m)}\isminimal_P(m,p)\Big)
    \end{equation*}
    is a propositional truncation.
  \end{subexenum}
  \exercise Suppose that $A:I\to \UU$ is a type family over a set $I$ with decidable equality. Show that
  \begin{equation*}
    \Big(\prd{i:I}\iscontr(A_i)\Big)\leftrightarrow \iscontr\Big(\prd{i:I}A_i\Big).
  \end{equation*}
\end{exercises}