\section{The Lebesgue Integral}
\setcounter{subsection}{1}

\subsection{The Lebesgue Integral of a Bounded Function}
  \paragraph{2.}
  \begin{proof}
    $\,$\par
    (a) By Problem 2.51, $h$ is upper semicontinuous as $f$ is bounded and by
    Problem 2.50, ${x:\, h(x)<\lambda}$ is open and hence measurable for every
    $\lambda\in\mathbb{R}$. Thus, $h$ is measurable.\par
    Let $\varphi(x)\ge f(x)$ be a step function and $x_0$ any point other
    than the endpoints of the intervals occurring in $\varphi$. Then there
    exists some $\delta>0$ such that for all $x\in(x_0-\delta,x_0+\delta)$,
    $\varphi(x_0) = \varphi(x) \ge f(x)$. Hence,
    \[
      h(x_0) = \inf_{\delta<0}\sup_{|x-x_0|<\delta}f(x) \le \varphi(x_0).
    \]
    Namely, $\varphi\ge h$ except at a finite number of points. Hence, $\int_a^b
    \varphi \ge \int_a^b h$ and therefore
    \[
      R\upint_a^b f = \inf_{\varphi\ge f}\int_a^b\varphi(x)\rd x \ge \int_a^b h.
    \]\par
    We can also derive from the previous discussion that there is a sequence of
    $\langle\varphi_n\rangle$ of step functions satisfying $\varphi \downarrow h$. By
    Proposition 6,
    \[
      \int_a^b h = \lim\int_a^b\varphi_n \ge R\upint_a^b f.
    \]
    Hence, $R\upint_a^b f = \int_a^b h$.\par
    (b) First suppose that $f$ is Riemann integrable and let $h$ and $g$ be the
    upper and lower envelope of $f$ respectively. By part (a), $f$ is Riemann
    integrable implies $\int_a^b(h-g) = 0$. Together with the fact that $h\ge
    g$, we conclude that $h=g$ a.e.. Therefore, by Problem 2.50, $f$ is
    continuous except on a set of measure zero.\par
    Note that the argument remains true if we reverse the order, verifying the
    converse part. Hence, the proposition holds.
  \end{proof}
% end

\subsection{The Integral of a Nonnegative Function}
  \paragraph{3.}
  \begin{proof}
    Suppose that $E_n=\{x:\, f(x)>1/n\}$. Then,
    \[
      0=\int f \ge \int_{E_n} f \ge \frac{mE_n}{n}
    \]
    implies $mE_n=0$. Hence, $m\{x:\, f(x)>0\}=m(\bigcup E_n)\le\sum mE_n=0$.
    Namely, $f=0$ a.e.
  \end{proof}

  \paragraph{5.}
  \begin{proof}
    For any fixed $x_0\in\mathbb{R}$, let $f_n(x) = f\cdot\chi_{(-\infty,x_0-
    1/n]}$, which is a increasing sequence of nonnegative measurable function
    whose limit is $f\cdot\chi_{-\infty,x_0}$. Then by Theorem 10,
    \[
      F(x_0)=\int_{-\infty}^{x_0} f = \int f\cdot\chi_{-\infty,x_0}
      = \lim\int f\cdot\chi_{(-\infty,x_0-1/n]} = \lim F(x_0-1/n).
    \]
    Meanwhile, since
    \[
      |F(x_0)-F(x_0+1/n)| = \left|\int_{x_0}^{x_0+1/n}f(x)\rd x\right|=
      \left|\int_{-1/n}^0 g(x)\rd x \right|,
    \]
    where $g(x)=f(x_0-x)$, arguing on $g$ in a similar manner yields $F(x_0)=
    \lim F(x_0+1/n)$. Thus, $F$ is continuous.
  \end{proof}

  \paragraph{6.}
  \begin{proof}
    By Theorem 9, $\int f \le \lowlim\int f_n$. Meanwhile, $f_n\le f$ implies
    $\int f_n\le \int f$ and therefore $\uplim \int f_n \le \int f$. Hence,
    $\int f =\lim\int f_n$.
  \end{proof}

  \paragraph{7.}
  \begin{solution}
    $\,$\par
    (a) Let $f_n(x)=n\cdot\chi_{[0,1/n]}$. $f_n$ converges to $f=0$ except on
    $x=0$. For each $n$, $\int f_n = 1$ but $\int f=0$. Hence, the inequality
    could be strict.\par
    (b) Let $f_n(x)=\chi_{[n,\infty)}$. Then $\langle f_n\rangle$ is a
    decreasing sequence which converges to $f=0$, the integral of which is $0$.
    However, for every $n$, $\int f_n = \infty$.
  \end{solution}

  \paragraph{8.}
  \begin{proof}
    Let $g_n = \inf\{f_n,f_{n+1},\dots\}$. Clear that
    \begin{equation}
      \label{eq:4.3.8}
      \int g_n \le \int f_n.
    \end{equation}
    Meanwhile $\langle g_n\rangle$ is a increasing sequence converging to
    $\lowlim f_n$. Hence, by the Monotone Convergence Theorem and
    \eqref{eq:4.3.8}
    \[
      \int \lowlim f_n = \int \lim_{n\to\infty}g_n =
      \lim_{n\to\infty}\int g_n \le \lowlim\int f_n.
    \]
  \end{proof}

  \paragraph{9.}
  \begin{proof}
    By Fatou's Lemma,
    \begin{equation}
      \label{eq:4.3.9}
      \int_E f \le \lowlim\int_E f_n.
    \end{equation}
    Similarly, $\int_{\bar{E}}f \le \lowlim\int_{\bar{E}} f_n$ and therefore
    \[
      \int_E f_n = \int f_n-\int_{\bar{E}} f_n\quad\Rightarrow\quad
      \uplim\int_E f_n \le \int f- \int_{\bar{E}} f = \int_{\bar{E}} f.
    \]
    \eqref{eq:4.3.9} and the inequality above together implies $\int_E f_n\to
    \int f$.
  \end{proof}
% end

\subsection{The General Lebesgue Integral}
  \paragraph{12.}
  \begin{proof}
    Note that $\langle g+f_n\rangle$ is a sequence of nonnegative measurable
    functions. Hence by Problem 8,
    \[
      \int_E\lowlim(g+f_n) \le \lowlim\int_E(g+f_n) \quad\Rightarrow\quad
      \int_E\lowlim f_n \le \lowlim\int_E f_n.
    \]
    The second inequality follows immediately from the definition of lower and
    upper limit. Replacing $g+f_n$ with $g-f_n$ and arguing in a similar manner
    gives the last inequality.
  \end{proof}

  \paragraph{13.}
  \begin{proof}
    $f_n\ge -h$ implies $f_n+h\ge 0$. Hence, $\int(f_n+h)$ always has a meaning.
    And since $g$ is integrable, $\int f_n = \int(f_n+h)-\int h$ also has a
    meaning. Similarly, $\int f$ has a meaning. Meanwhile,
    \[
      \int f = \int(f+h) - \int h \le \lowlim\int(f_n+h) - \int h
      = \lowlim\int f_n.
    \]
  \end{proof}

  \paragraph{15.}
  \begin{proof}
    $\,$\par
    (a) By Problem 4, for every $\vep>0$, there exists some simple functions
    $\varphi_1\le f^+$ and $\varphi_2\le f^-$ such that
    \[
      \int_E f^+ - \int_E\varphi_1 < \vep \quad\text{and}\quad
      \int_E f^- - \int_E\varphi_2 < \vep.
    \]
    Let $\varphi=\varphi_1-\varphi_2$, which is also a simple function.
    Meanwhile,
    \[
      \int_E|f-\varphi| \le \int_E(f^+-\varphi_1) + \int_E(f^--\varphi_2) < 2\vep.
    \]
  \end{proof}

  \paragraph{16.}
  \begin{proof}
    For every integrable $f$, by Problem 15, there exists some step function $\psi
    =\sum_{k=1}^N c_k\chi_{E_k}$ such that $\int|f-\psi|<\vep$. Note that
    \begin{equation}
      \label{eq:4.4.16}
      \lim_{n\to\infty}\int_{-\infty}^\infty \psi(x)\cos nx\rd x =
      \lim_{n\to\infty}\sum_{k=1}^N c_k\int_{E_k}\cos nx\rd x = 0.
    \end{equation}
    Hence,
    \begin{align*}
      \left|\int_{-\infty}^\infty f(x)\cos nx\rd x \right|
      &= \left|\int_{-\infty}^\infty (f(x)-\psi(x)+\psi(x))\cos nx\rd x\right|\\
      &\le \int_{-\infty}^\infty |f(x)-\psi(x)||\cos nx|\rd x +
           \left|\int_{-\infty}^\infty \psi(x)\cos nx\rd x\right| \\
      &\le \vep + \left|\int_{-\infty}^\infty \psi(x)\cos nx\rd x\right| \\
      &\to 0 \text{ as }n\to\infty.
    \end{align*}
  \end{proof}

  \paragraph{18.}
  \begin{proof}
    Let $\langle t_n\rangle$ be any sequence with $t_n\ne 0$ and tending to $0$.
    Then $\langle f(x,t_n)\rangle$ is sequence of functions satisfying the
    hypotheses of Lebesgue Convergence Theorem. Meanwhile, $f(x,t_n)\to f$ as
    $n\to\infty$. Hence,
    \[
      \lim_{n\to\infty}\int f(x,t_n)\rd x = \int f(x)\rd x.
    \]
    Since the choice of $\langle t_n\rangle$ is arbitrary, by Problem 2.49f,
    \[
      \lim_{t\to 0}\int f(x,t)\rd x = \int f(x)\rd x.
    \]
    If $f$ is continuous in $t$ for each $x$, then $\lim_{\Delta t\to 0}f(x,t+
    \Delta t)=f(x,t)$ holds for every $t$. Therefore, replacing $t$ with
    $\Delta t$ in the previous result yields
    \[
      \lim_{\Delta t\to 0}\int f(x,t+\Delta t)\rd x = \int f(x,t)\rd x.
    \]
    Namely, $h(t)$ is continuous.
  \end{proof}

% end


\chapter{Annontated Examples}
\section{Preliminaries}

This chapter demonstrates the core functionality of the code. In Section \ref{sec:mansol}, we present steps necessary for running a manufactured solution problem.

In this section, we will go through several example problems for running \pkg{dgswem-v2}. In order to provide succint instructions to the reader, we introduce the following environment variables. We will assume that the following environment variables have been defined in the users environment:
\begin{lstlisting}{language=bash}
export DGSWEMV2_REPO=<path/to/dgswemv2_repo>
export DGSWEMV2_BUILD=<path/to/dgswemv2_build>
export DGSWEMV2_EXAMPLES=${DGSWEMV_REPO}/examples
\end{lstlisting}

Additionally, to keep the compile time of the application short, the cmake configuration does not automatically build the examples. To build the following examples, you will need to rerun your \pkg{cmake} build command with the additional option \lstinline{-DBUILD_EXAMPLES=On}.

\section{Manufactured Solution}
\label{sec:mansol}
The method of manufactured solutions presents an easy means of determining whether the solution is converging at the rates stipulated by the theoretical error estimates. In addition, since we obtain exact error estimates, the method of manufactured solutions may also be used to determine, whether or not errors have been introduced between parallel and serial implementations.

To avoid the stability-related difficulties associated with greatly varying mesh refinements, we evaluate the manufactured solution from~\cite{Wiraset2014}. For the manufactured solution, we set the solution to be equal to
\begin{align}
\zeta(x,y,t) &= 2 \zeta_0 \frac{ \cos \left( \omega(x - x_1) \right) \cos \left( \omega(y - y_1)\right) \cos(\omega t)}{
\cos \left( \omega(x_2 - x_1)\left) \cos \right( \omega(y_2 - y_1)\right)} \nonumber \\
q_x(x,y,t) &= \zeta_0 \frac{ \sin \left(\omega(x - x_1)\right) \cos\left(\omega(y - y_1)\right) \sin(\omega t)}{\cos\left( \omega ( x_2 - x_1)\right) \cos\left(\omega ( y_2 - y_1)\right)} \nonumber \\
q_y(x,y,t) &= \zeta_0 \frac{ \cos\left(\omega (x - x_1)\right) \sin\left( \omega(y -y_1)\right) \sin( \omega t)}{\cos \left( \omega ( x_ 2 - x_1)\right) \cos\left( \omega(y_2 - y_1)\right)} \nonumber
\end{align}
where $x_1 = 40,000\,\mathrm{m},\, x_2= 83,200\,\mathrm{m},\, y_1 = 10,000\,\mathrm{m},\,y_2 = 53,200\,\mathrm{m},\,\zeta_0 = 0.25\,\mathrm{m},\, \omega = 2 \pi/43,200\, \mathrm{rad}/\mathrm{s}$. Additionally, $g=9.81\,\mathrm{m}/\mathrm{s}^2$, and the bathymetry is constant with depth $H_0=2\,\mathrm{m}$. The source term is then obtained by applying the left hand side of the shallow water equations to the target solution.

The manufactured solutions can be run via the three following targets, which can be made as follows:
\begin{lstlisting}[language=bash]
cd ${DGSWEMV2_BUILD}
make MANUFACTURED_SOLUTION_SERIAL
make MANUFACTURED_SOLUTION_HPX
make MANUFACTURED_SOLUTION_OMPI
\end{lstlisting}
Note that for the HPX and the OMPI manufactured targets, the project must have the \lstinline{-DUSE_HPX} and \lstinline{-DUSE_OMPI} options set to \lstinline{On}, respectively.

The workflow for building the manufactured solution consists of three parts: (1) generating a mesh, (2) if necessary, partitioning the mesh, (3) running the simulation, and (4) interpreting the output.

\subsection{Generating the mesh}
For the generation of meshes, we use a \lstinline{yaml}-formatted input file. We have included an input file in
\begin{lstlisting}
${DGSWEMV2_EXAMPLES}/manufactured_solution/input_files/mesh_generator_input.yml
\end{lstlisting}
We have provided comments for the individual variables in the yaml files. One key aspect for improving the accuracy of the solution is the mesh resolution. To modulate these, \lstinline{num_x_subdivisions} and \lstinline{num_y_subdivisions} allows one to refine or coarsen the mesh appropriately. However, for now we leave them at their defaults. To generate the mesh, run the following commands
\begin{lstlisting}[language=bash]
cd ${DGSWEMV2_EXAMPLES}/manufactured_solution/input_files/
${DGSWEMV2_BUILD}/mesh_generators/quad_mesh_generator \
    mesh_generator_input.yml
\end{lstlisting}
This will generate a \lstinline{quad_mesh.14} file in the current directory. This is an ADCIRC-formatted meshfile.

\subsection{Partitioning the mesh (optional)}
Note that this section is only necessary if you are attempting to run the simulation in a distributed manner, i.e. using the OMPI or HPX executable. If you are only trying to run the simulation in serial, skip to the next subsection.

In order to run the simulation in parallel, the mesh must be broken into smaller pieces, which can then be assigned to individual processors. Since there exist stark contrasts in the send latencies between two processors on the same node versus processors that might be connected via an interconnect, we require the user to specify the number of localities (i.e. private memory address spaces) in addition to the number of partitions to allow for the mesh partitioner to optimize these interconnect effects.

The partitioner executable can be found in
\begin{lstlisting}[language=bash]
${DGSWEMV2_BUILD}/partitioner/partitioner
\end{lstlisting}
and running the executable without any arguments will provide usage information. In particular, the variables mean the following:
\begin{itemize}
\item \lstinline{<input filename>}: The name of the input file used to run the execution.
\item \lstinline{<number of partitions>}: The number of partitions the mesh is to be partitioned into.
\item \lstinline{<number of nodes>}: The number of hardware localities the simulation is to be run on.
\item \lstinline{<ranks per locality>}: The number of ranks per locality.
\item \lstinline{<rank balanced>}: Whether or not the constraints should be balanced across the individual submeshes. Note that the entry options are \lstinline{true} or \lstinline{false}. Recommended for OpenMP/MPI.
\end{itemize}
Our two parallelization strategies rely on different parallel execution models. Thus the inputs for either version need to be slightly modified. In the following two subsections we outline, the differences in general, but also provide concrete numbers to allow the user to proceed.
\subsubsection{Partitioning for HPX}
The HPX execution model varies from that of traditional parallelization strategies in that the number of partitions does not correspond to any hardware concept. For example, traditional MPI implementations assigned one rank per core, and insofar the number of partitions equalled the number of cores. For HPX, the number of partitions is roughly proportional to the number of tasks executed on that locality. \emph{Oversubscribing} meshes --- assigning more meshes to the locality than there are cores --- can lead to desirable behavior in the form of hiding of send latencies. However, the user needs to be careful to avoid exposing too fine grain parallelism in the form of too many meshes, because task overhead will dominate the execution time and lead performance degradation.

To continue the example for an HPX parallelization, we recommend running:
\begin{lstlisting}[language=bash]
cd ${DGSWEMV2_EXAMPLES}/manufactured_solution/input_files
${DGSWEMV2_BUILD}/partitioner/partitioner\
    dgswemv2_input.15 4 1
\end{lstlisting}
This should generate, 4 \lstinline{meta}-formatted mesh files. The \lstinline{meta}-format isn't an official mesh format, but rather a simple method of representing the mesh internally for the \pkg{dgswem-v2} application, and 4 \lstinline{dbmd}-formatted files, which encapsulate the distributed metadata information required to ensure that submeshes appropriately communication with one another. In addition, we will have generated an updated input file specifically for running parallel meshes. For this example, it's
\lstinline{dgswemv2_input_parallelized.15}.

\subsubsection{Partitioning for OpenMP/MPI}
Partitioning for the MPI+OpenMP implementation is slightly, different. Ratio of number of partitions to number of threads should correspond to the number of threads available on each node. However, it's also possible to run a flat MPI implementation by modifying the \lstinline{<ranks per locality>} option. This option will correspond to the number MPI ranks on each node.

Additionally, since submeshes are mapped statically to threads, we recommend setting the \lstinline{<rank balanced>} option to \lstinline{true}. For applications with varying load across the elements, e.g. in the case of wetting and drying, we would like to balance load and memory constraints across the submeshes. Ultimately, the performance will be constrained along the critical path, for statically mapped parallelizations optimal performance is achieved when each submesh roughly has the same amount of work. Note that this is not necessary for the HPX parallelization, which implements aggressive on-node work stealing.

To generate a mesh partitioning for this parallelization, we run
\begin{lstlisting}[language=bash]
cd ${DGSWEMV2_EXAMPLES}/manufactured_solution/input_files
${DGSWEMV2_BUILD}/partitioner/partitioner\
   dgswemv2_input.15 2 1 2 true
\end{lstlisting}
This configuration will result in a flat MPI run. With 2 MPI ranks. Note that similar to the HPX run, we generate both \lstinline{meta} mesh files and \lstinline{dbmd} connectivity information and an updated \lstinline{dgswemv2_input_paralllelized.15} input file.
\subsection{Running the simulation}
For each of the three execution modes --- serial, with HPX, and with MPI+OpenMP --- we have a separate executable.
To execute the serial implementation, run
\begin{lstlisting}[language=bash]
cd ${DGSWEMV2_EXAMPLES}/manufactured_solution/input_files
mkdir -p output
${DGSWEMV2_BUILD}/examples/MANUFACTURED_SOLUTION_SERIAL\
    dgswemv2_input.15
\end{lstlisting}
For the MPI and HPX versions, we assume that the parallel launcher, would be accessible through some \lstinline{<mpirun>} command, e.g. on a slurm based system this might \lstinline{srun}. The MPI-version can then be executed via
\begin{lstlisting}[language=bash]
cd ${DGSWEMV2_EXAMPLES}/manufactured_solution/input_files
mkdir -p ouptut
<mpirun> ${DGSWEMV2_BUILD}/examples/MANUFACTURED_SOLUTION_OMPI\
    dgswemv2_input.15
\end{lstlisting}
For the HPX version, the execution command depends on the set-up of the system. If there is no distributed aspect to your computing environment, you do not need a distributed launcher like \lstinline{srun}. Thus, for the reader running the simulation on their laptop, i.e. with one locality, the HPX example can be run as
\begin{lstlisting}[language=bash]
cd ${DGSWEMV2_EXAMPLES}/manufactured_solution/input_files
mkdir -p output
${DGSWEMV2_BUILD}/examples/MANUFACTURED_SOLUTION_HPX\
    dgswemv2_input.15
\end{lstlisting}

\subsection{Intepreting the Output}
\pkg{dgswem-v2} writes output in several forms. Firstly, in the \lstinline{output} folder that was created in the \texttt{\$\{DGSWEMV2\}\_EXAMPLES/manufactured\_solution} directory. You will find a log file name \lstinline{log} and \texttt{vtk}-formatted output. The output can be visualized using a package like \pkg{paraview}. A sample image of what the output should look like is shown in Figure \ref{fig:mansol}.
\begin{figure}
\centering
\includegraphics[width = 0.8 \textwidth]{{images/man_sol}.png}
\caption{Sample output of the manufactured solution output.}
\label{fig:mansol}
\end{figure}
\section{1D Inlet}
\label{sec:inlet}


\subsection{First-order models}\label{subsec:first_order_models}

Much of \fullref{subsec:first_order_logic} is dedicated to semantic equivalences between logical formulas, which are formulated and proved using \hyperref[def:first_order_structure]{structures}. This section is dedicated to the study of structures themselves and relations between them. While model theory is a wide topic, for the purposes of this document we are only interested in the following questions:

\begin{itemize}
  \item Which subsets of a structure form a \hyperref[def:first_order_substructure]{substructure}?

  This is answered by \fullref{def:first_order_substructure} and by \fullref{def:first_order_generated_substructure}. Vacuously, if the language contains no functional symbols, by \fullref{thm:def:first_order_substructure/no_functions} every subset of a structure is a substructure. Such is the case with \hyperref[def:set]{sets} themselves, with \hyperref[def:partially_ordered_set]{partially ordered sets} or with \hyperref[def:metric_space]{metric} and \hyperref[def:topological_space]{topological spaces}.

  \Fullref{thm:substructures_form_complete_lattice} shows that the set of all substructures of a structure is worth studying in itself.

  \item Given a model of some set \( \Gamma \) of formulas, which substructures and \hyperref[def:first_order_homomorphism]{homomorphic} images of the model are again models of \( \Gamma \)?

  This is answered by \fullref{thm:positive_formulas_preserved_under_homomorphism}, \fullref{thm:arbitrary_formulas_preserved_under_isomorphisms} and \fullref{thm:functions_over_model_form_model}.
\end{itemize}

\begin{definition}\label{def:first_order_substructure}
  Let \( \mscrX = (X, I) \) be a structure for the language \( \mscrL \) and let \( Y \subseteq X \). We say that \( \mscrY = (Y, I) \) is a \term{substructure} of \( \mscrX \) if it satisfies any of the following equivalent conditions:

  \begin{thmenum}
    \thmitem{def:first_order_substructure/deductive} If \( Y \) is closed under function application, that is, for any functional symbol \( f \) in \( \mscrL \) with arity \( n \), we have \( I(f)(Y^n) \subseteq Y \).

    \thmitem{def:first_order_substructure/inductive} The universe \( Y \) is a \hyperref[def:fixed_point]{fixed point} of the operator
    \begin{equation}\label{eq:def:first_order_substructure/inductive/operator}
      \begin{aligned}
        &T: \pow(X) \to \pow(X) \\
        &T(A) \coloneqq A \cup \set*{ x \in X \given \qexists{f \in \boldop{Fun}} \qexists{x_1, \ldots, x_{\#f} \in A} f\Bracks{x_1, \ldots, x_{\#f}} = x },
      \end{aligned}
    \end{equation}
    which enlarges \( A \) with the union of all image of \( A \) under functions of the language \( \mscrL \).

    Note that the formula inside \eqref{eq:def:first_order_substructure/inductive/operator} is in the metalanguage despite using syntax similar to first-order logic formulas.
  \end{thmenum}
\end{definition}
\begin{proof}
  By definition of \( T \), \( Y \) if a fixed point if and only if
  \begin{equation*}
    \set*{ x \in X \given \qexists{f \in \boldop{Fun}} \qexists{x_1, \ldots, x_{\#f} \in A} f\Bracks{x_1, \ldots, x_{\#f}} = x } \subseteq Y.
  \end{equation*}

  This condition is clearly satisfied if \( B \) satisfies \fullref{def:first_order_substructure/deductive}.

  If, instead \( Y \) is a fixed point of \( T \), for the \( n \)-ary functional symbol \( f \in \boldop{Fun} \) and for any tuple \( x_1, \ldots, x_n \), the value \( I(f)(x_1, \ldots, x_n) \) belongs to \( Y \). Therefore, \fullref{def:first_order_substructure/deductive} is satisfied.
\end{proof}

\begin{remark}\label{rem:topological_first_order_structures}\mimprovised
  Let \( \mscrX = (X, I) \) be a structure over some language \( \mscrL \) without predicate symbols.

  If, for every functional symbol \( f \), the interpretation \( I(f) \) is a \hyperref[def:global_continuity]{continuous function}, we call \( \mscrX \) a \term{topological structure}.

  For every algebraic structure defined in \fullref{sec:group_theory} and \fullref{sec:ring_theory}, there exists a topological equivalent. We discuss \hyperref[def:topological_group]{topological groups} and \hyperref[def:topological_vector_space]{topological vector spaces} through the document, especially in \fullref{sec:functional_analysis}.

  Naturally, every substructure of a topological structure is again a topological structure.
\end{remark}

\begin{example}\label{ex:def:first_order_substructure/vector_space}
  The classic definition for a subset \( U \) of a \hyperref[def:vector_space]{vector space} \( \mscrV \) being a vector subspace is that \( U \) is closed under \hyperref[rem:linear_combinations]{linear combinations}. Linear combinations are simply finite \hyperref[def:multi_valued_function/superposition]{superpositions} of addition and scalar multiplication in \( \mscrV \). So this condition ensures that \( U \) is closed under application of the functional symbols corresponding to addition and scalar multiplication.

  See \fullref{thm:span_via_linear_combinations} for a further discussion.
\end{example}

\begin{proposition}\label{thm:def:first_order_substructure}
  Fix a language \( \mscrL \). The \hyperref[def:first_order_substructure]{first order substructures} of a structure \( \mscrX = (X, I) \) have the following basic properties:
  \begin{thmenum}
    \thmitem{thm:def:first_order_substructure/no_functions} If \( \mscrL \) has no functional symbols, then \( (Y, I) \) is a substructure where \( Y \) is any subset of \( X \).

    \thmitem{thm:def:first_order_substructure/intersection} Let \( \seq{ (Y_k, I) }_{k \in \mscrK} \) be a family of substructures of \( \mscrX \). Then their \term{intersection structure} \( \parens*{\bigcap_{k \in \mscrK} Y_k, I} \) is again a substructure of \( \mscrX \).
  \end{thmenum}
\end{proposition}
\begin{proof}
  \SubProofOf{thm:def:first_order_substructure/no_functions} Both conditions in \fullref{def:first_order_substructure} are vacuously satisfied if there are no functional symbols in \( \mscrL \).

  \SubProofOf{thm:def:first_order_substructure/intersection} For any functional symbol \( f \) in \( \mscrL \) with arity \( n \), we have
  \begin{equation*}
    I(f)\parens*{\parens*{\bigcap_{\smash{k \in \mscrK}} Y_k}^n}
    \reloset {\ref{thm:def:function_image/intersection}} \subseteq
    \bigcap_{k \in \mscrK} I(f)(Y_k^n) \subseteq \bigcap_{k \in \mscrK} \mscrY_k.
  \end{equation*}

  Therefore, \( \parens*{\bigcap_{k \in \mscrK} Y_k, I} \) is indeed a substructure of \( \mscrX \).
\end{proof}

\begin{definition}\label{def:first_order_generated_substructure}
  Let \( \mscrX = (X, I) \) be a structure over \( \mscrL \) and let \( A \subseteq X \) be any set. The set \( A \) is said to \term[bg=поражда,ru=порождает]{generate} the substructure \( \mscrY = (Y, I) \) if it satisfies any of the equivalent statements:
  \begin{thmenum}
    \thmitem{def:first_order_generated_substructure/smallest} Out of all substructures of \( \mscrX \) whose domain contains \( A \), the domain \( \mscrY \) is the smallest with respect to \hyperref[def:subset]{set inclusion}.

    \thmitem{def:first_order_generated_substructure/intersection} \( \mscrY \) is the intersection structure of all substructures of \( \mscrX \) that contain \( A \).
  \end{thmenum}
\end{definition}
\begin{proof}
  Let \( \seq{ (Y_k, I) }_{k \in \mscrK} \) be the family of all substructures of \( \mscrX \) whose domains contain \( A \). Fix one of these substructures, say \( (Y_{k_0}, I) \).

  We have the obvious inclusion
  \begin{equation*}
    \bigcap_{k \in \mscrK} Y_k \subseteq Y_{k_0}.
  \end{equation*}

  The reverse inclusion holds if and only if \( Y_{k_0} \) is contained in each one of domains \( Y_k \) for \( k \in \mscrK \). In other words, \( Y_{k_0} \) is the smallest of the domains \( \seq{ Y_k }_{k \in \mscrK} \) with respect to set inclusion if and only if \( Y_{k_0} \) equals their intersection.
\end{proof}

\begin{example}\label{ex:def:first_order_generated_substructure}
  Common examples of generated substructures are the \hyperref[def:semimodule/submodel]{linear span} discussed in \fullref{thm:span_via_linear_combinations} and the \hyperref[def:generated_ring_ideal]{generated ring ideals}.
\end{example}

\begin{proposition}\label{thm:first_order_generated_substructures_exist}
  Let \( \mscrX = (X, I) \) be a structure for the language \( \mscrL \).

  Every subset of \( X \) has a \hyperref[def:first_order_generated_substructure]{generated substructure}.
\end{proposition}
\begin{proof}
  Given a set \( Y \subseteq X \), we apply \fullref{thm:knaster_tarski_theorem} to the \hyperref[thm:boolean_algebra_of_subsets]{Boolean algebra of all subsets} \( \pow(X) \) with the operator
  \begin{equation*}
    \begin{aligned}
      &R: \pow(X) \to \pow(X) \\
      &R(A) \coloneqq Y \cup T(A),
    \end{aligned}
  \end{equation*}
  where \( T \) is defined in \eqref{eq:def:first_order_substructure/inductive/operator}.

  We thus obtain the smallest fixed point \( Z \) of \( T \), which contains \( Y \) and satisfies \fullref{def:first_order_substructure/inductive}. The structure \( \mscrZ = (Z, I) \) is thus a substructure of \( \mscrX \).

  Furthermore, \( \mscrZ \) is unique because its domain is the smallest set invariant under \( R \).
\end{proof}

\begin{proposition}\label{thm:substructures_form_complete_lattice}
  Fix a structure \( \mscrX = (X, I) \) for the language \( \mscrL \).

  The set of all substructures of a \( \mscrX \) forms a complete lattice with respect to set inclusion of domains. It is isomorphic to a complete \hyperref[def:semilattice/submodel]{sublattice} of the Boolean algebra \( \pow(X) \) described in \fullref{thm:boolean_algebra_of_subsets}.

  Explicitly:
  \begin{thmenum}
    \thmitem{thm:substructures_form_complete_lattice/join} The \hyperref[def:semilattice/join]{join} of the family of substructures with domains \( \seq{ (Y_k, I) }_{k \in \mscrK} \) is the \hyperref[def:first_order_generated_substructure]{generated substructure} of the set \( \bigcup_{k \in \mscrK} Y_k \).

    \thmitem{thm:substructures_form_complete_lattice/top} The \hyperref[def:partially_ordered_set_extremal_points/top_and_bottom]{top element} is the substructure \( \mscrX \) itself. Any substructures that are different from \( \mscrX \) are called \term{proper}.

    \thmitem{thm:substructures_form_complete_lattice/meet} The \hyperref[def:semilattice/meet]{meet} of the family of substructures \( \seq{ (Y_k, I) }_{k \in \mscrK} \) is simply the \hyperref[thm:def:first_order_substructure/intersection]{intersection structure} of the family.

    \thmitem{thm:substructures_form_complete_lattice/bottom} The \hyperref[def:partially_ordered_set_extremal_points/top_and_bottom]{bottom element} of this lattice is the intersection of all substructures. This is called the \term{trivial substructure}. As a matter of fact, because the trivial substructures for any two structures are isomorphic, we refer to them collectively as the \term{trivial structure} because trivial substructures are isomorphic.

    As discussed in \fullref{rem:empty_models}, the empty set is not allowed to be the domain of a structure by definition. Nevertheless, for the sake of having a bottom element we allow structures with empty domains in this lattice.

    By \fullref{thm:order_category_isomorphism_properties/universal}, the trivial structures are the \hyperref[def:universal_objects/initial]{initial objects} of all \hyperref[def:category_of_small_first_order_models]{categories of models}.

    The trivial substructure usually consists only of the constants of \( \mscrL \) --- for example, the \hyperref[def:group/trivial]{trivial group} \( \set{ e } \) or the \hyperref[def:semilattice/trivial]{trivial bounded lattice} \( \set{ \top, \bot } \).
  \end{thmenum}
\end{proposition}
\begin{proof}
  \SubProofOf{thm:substructures_form_complete_lattice/join} Let \( (Y, I) \) be the generated substructure of the set \( A \coloneqq \bigcup_{k \in \mscrK} \mscrY_k \). From \fullref{def:first_order_generated_substructure/smallest} it follows that out of the domains of all substructures of \( \mscrX \), \( Y \) is the smallest that contains \( A \) and hence the smallest that contains \( Y_k \) for all \( k \in \mscrK \). Therefore, it is indeed the supremum of the family \( \seq{ (Y_k, I) }_{k \in \mscrK} \) with respect to set inclusion of domains.

  \SubProofOf{thm:substructures_form_complete_lattice/top} Since \( \mscrX \) is a substructure of itself, it is not only the supremum of the entire lattice, but actually the maximum.

  \SubProofOf{thm:substructures_form_complete_lattice/meet} The domain of the intersection structure of the family \( \seq{ (Y_k, I) }_{k \in \mscrK} \) of substructures of \( \mscrX \) is obviously the infimum of the family since its domain are the infimum in the \hyperref[thm:boolean_algebra_of_subsets]{Boolean algebra of subsets} of \( X \).

  \SubProofOf{thm:substructures_form_complete_lattice/bottom} It trivially follows from \fullref{thm:substructures_form_complete_lattice/meet} that the bottom element is the intersection of all substructures of \( \mscrX \).
\end{proof}

\begin{definition}\label{def:first_order_homomorphism}
  Let \( \mscrX = (X, I) \) and \( \mscrY = (Y, I) \) be structures over a common language. We say that the \hyperref[def:function]{function} \( h: X \to Y \) is a \term{homomorphism} between \( \mscrX \) and \( \mscrY \) if it preserves all functions and relations. Explicitly:
  \begin{thmenum}
    \thmitem{def:first_order_homomorphism/functions} For any functional symbol \( f \in \boldop{Fun} \) of arity \( n \) and any tuple \( x_1, \ldots, x_n \in X \) we have
    \begin{equation*}
      h\parens[\Big]{ I_X(f)(x_1, \ldots, x_n) } = I_Y(f) \parens[\Big]{ h(x_1), \ldots, h(x_n) }
    \end{equation*}

    \thmitem{def:first_order_homomorphism/predicates} For any predicate symbol \( p \in \boldop{Pred} \) of arity \( n \) and any \( x_1, \ldots, x_n \in X \),
    \begin{equation*}
      I_X(p) (x_1, \ldots, x_n) = T \T{implies} I_Y(p) \parens[\Big]{ h(x_1), \ldots, h(x_n) } = T.
    \end{equation*}
  \end{thmenum}
\end{definition}

\begin{remark}\label{rem:first_order_strong_homomorphism}
  Homomorphisms as they are defined in \fullref{def:first_order_homomorphism} are sometimes called \term{weak homomorphisms}. Under weak homomorphisms, it is possible that \( I_X(p) (x_1, \ldots, x_n) = F \) and yet \( I_Y(p) \parens[\Big]{ h(x_1), \ldots, h(x_n) } = T \).

  Logicians sometimes define \term{strong homomorphisms} where they replace \fullref{def:first_order_homomorphism/predicates} with the stronger condition
  \begin{equation*}
    I_X(p) (x_1, \ldots, x_n) = I_Y(p) \parens[\Big]{ h(x_1), \ldots, h(x_n) }.
  \end{equation*}

  This condition seems much more natural at first, but it is less useful in practice. For example, \hyperref[def:partially_ordered_set/homomorphism]{monotone maps} and \hyperref[eq:def:category_of_small_hypergraphs/homomorphism]{graph homomorphisms} are both weak homomorphisms and these are the most used definitions of homomorphisms in languages with predicate symbols. For this reason, we mostly avoid studying strong homomorphisms.

  They are useful for certain propositions like \fullref{thm:totally_ordered_strong_homomorphism}, however.
\end{remark}

\begin{proposition}\label{thm:def:first_order_homomorphism}
  \hyperref[def:first_order_homomorphism]{First-order structure homomorphisms} have the following basic properties:
  \begin{thmenum}
    \thmitem{thm:def:first_order_homomorphism/submodel} If \( \mscrX = (X, I) \) is a structure and \( \mscrY = (Y, I) \) is a \hyperref[def:first_order_substructure]{substructure} of \( \mscrX \), then the \term{canonical embedding} function
    \begin{equation}\label{thm:def:first_order_homomorphism/submodel/canonical_embedding}
      \begin{aligned}
        &\iota: Y \to X \\
        &\iota(y) \coloneqq y
      \end{aligned}
    \end{equation}
    is indeed a \hyperref[def:first_order_homomorphism_invertibility/surjective]{homomorphism} (and thus an embedding in the sense of \fullref{def:first_order_homomorphism_invertibility}).

    \thmitem{thm:def:first_order_homomorphism/preserves_substructures} If \( \mscrX = (X, I_X) \) and \( \mscrY = (Y, I_Y) \) are structures and \( h: X \to Y \) is a (weak) homomorphism, then the \hyperref[def:multi_valued_function/image]{image} \( h(\mscrX) \coloneqq (h(X), I_Y) \) is a substructure of \( \mscrY \).

    \thmitem{thm:def:first_order_homomorphism/composition} The \hyperref[def:multi_valued_function/composition]{composition} of two homomorphisms is again a homomorphism.

    \thmitem{thm:def:first_order_homomorphism/term_valuation} Fix a homomorphism \( h: X \to Y \) and a term \( \tau \). For any variable assignments \( v_X \) and \( v_Y \) such that \( v_Y(\xi) = h(v_X(\xi)) \) for all \( \xi \in \boldop{Var}(\tau) \), we have
    \begin{equation*}
      h(\tau\Bracks{v_X}) = \tau\Bracks{v_Y}.
    \end{equation*}
  \end{thmenum}
\end{proposition}
\begin{proof}
  \SubProofOf{thm:def:first_order_homomorphism/submodel} The interpretation in the substructure \( \mscrY \) is the \hyperref[def:multi_valued_function/restriction]{restriction} \( I\restr_Y \) of \( I \) to \( \mscrY \) which simply restricts the domain of any predicate and function is indeed an interpretation in \( \mscrY \). Thus, \( (Y, I\restr_Y) \) is a structure.

  Conditions \fullref{def:first_order_homomorphism/functions} and \fullref{def:first_order_homomorphism/predicates} are both satisfied since the interpretation of any function and predicate is restricted to \( \mscrY \). Thus, \( \iota \) is a homomorphism.

  \SubProofOf{thm:def:first_order_homomorphism/preserves_substructures} To prove that \( (f(X), I_Y) \) is a substructure of \( \mscrY \), we will show that \fullref{def:first_order_substructure/deductive} holds.

  Indeed, due to \fullref{def:first_order_homomorphism/functions}, for any \( n \)-ary functional symbol and any tuple \( {x_1, \ldots, x_n \in X} \), we have that
  \begin{equation*}
    I_Y(f) \parens[\Big]{ h(x_1), \ldots, f(x_n) }
    \reloset {\ref{def:first_order_homomorphism/functions}} =
    h\parens[\Big]{ I_X(f)(x_1, \ldots, x_n) }
    \reloset {\ref{def:first_order_substructure/deductive}} \in
    h(X).
  \end{equation*}

  \SubProofOf{thm:def:first_order_homomorphism/composition} Let \( h: \mscrX \mapsto \mscrY \) and \( l: \mscrY \mapsto \mscrZ \) both be homomorphisms.

  \begin{itemize}
    \item \Fullref{def:first_order_homomorphism/functions} is satisfied because for any \( n \)-ary functional symbol \( f \) and any tuple \( x_1, \ldots, x_n \in X \),
    \begin{align*}
      &\phantom{{}={}}
      (l \bincirc h) \parens[\Big]{ I_X(f)(x_1, \ldots, x_n) }
      \reloset {\ref{def:first_order_homomorphism/functions}} = \\ &=
      l\parens[\Big]{ I_Y(f) \parens[\Big]{ h(x_1), \ldots, h(x_n) } }
      \reloset {\ref{def:first_order_homomorphism/functions}} = \\ &=
      I_{\mscrZ}(f) \parens[\Big]{ (l \bincirc h)(x_1), \ldots, (l \bincirc h)(x_n) }.
    \end{align*}

    \item \Fullref{def:first_order_homomorphism/predicates} is satisfied because for any \( n \)-ary predicate symbol \( p \) and any tuple \( x_1, \ldots, x_n \in X \),
    \begin{align*}
      &\phantom{{}={}}
      I_X(p) (x_1, \ldots, x_n)
      \reloset {\ref{def:first_order_homomorphism/predicates}} = \\ &=
      I_Y(p) \parens[\Big]{ h(x_1), \ldots, h(x_n) }
      \reloset {\ref{def:first_order_homomorphism/predicates}} = \\ &=
      I_{\mscrZ}(p) \parens[\Big]{ (l \bincirc h)(x_1), \ldots, (l \bincirc h)(x_n) }.
    \end{align*}
  \end{itemize}

  \SubProofOf{thm:def:first_order_homomorphism/term_valuation} We use induction on the structure of \( \tau \). If \( \tau \) is a variable, the statement is obvious from the compatibility condition for \( v_X \) and \( v_Y \). If \( \tau = f(\kappa_1, \ldots, \kappa_m) \), then
  \begin{align*}
    \tau\Bracks{v_Y}
    &=
    I(f) \parens[\Big]{ \kappa_1\Bracks{v_Y}, \ldots, \kappa_m\Bracks{v_Y} }
    = \\ &=
    I(f) \parens[\Big]{ h(\kappa_1\Bracks{v_X}), \ldots, h(\kappa_m\Bracks{v_X}) }
    \reloset {\ref{def:first_order_homomorphism/functions}} = \\ &=
    h\parens[\Big]{ I(f) \parens[\Big]{ \kappa_1\Bracks{v_X}, \ldots, \kappa_m\Bracks{v_X} } }
    = \\ &=
    h(\tau\Bracks{v_X}).
  \end{align*}
\end{proof}

\begin{definition}\label{def:first_order_homomorphism_invertibility}
  In connection with \fullref{def:function_invertibility} and \fullref{def:morphism_invertibility}, and more importantly \fullref{thm:first_order_categorical_invertibility}, we introduce the following terminology for homomorphisms:
  \begin{thmenum}
    \thmitem{def:first_order_homomorphism_invertibility/embedding} An \term{embedding} is an \hyperref[def:function_invertibility/injective]{injective} homomorphism.

    \thmitem{def:first_order_homomorphism_invertibility/surjective} The dual \hyperref[def:function_invertibility/surjective]{surjective} homomorphisms do not have an established name. The term \term{projection} is often used, but projections are conventionally idempotent.

    \thmitem{def:first_order_homomorphism_invertibility/isomorphism} An \term{isomorphism} is a \hyperref[def:function_invertibility/bijective]{bijective} homomorphism whose inverse is also a homomorphism. Equivalently, it is a bijective strong homomorphism.

    The peculiarity here is that the inverse in the sense of \fullref{def:morphism_invertibility/isomorphism} may not be a homomorphism in general. See \fullref{ex:bijective_order_homomorphism_not_isomorphism}. For examples where this condition can be relaxed, see \fullref{thm:automorphism_without_predicate_symbols} and \fullref{thm:totally_ordered_strict_isomorphisms}.

    \thmitem{def:first_order_homomorphism_invertibility/endomorphism} An \term{endomorphism} is a homomorphism that is also an \hyperref[def:multi_valued_function/endofunction]{endofunction}.

    \thmitem{def:first_order_homomorphism_invertibility/automorphism} A homomorphism that is both an endomorphism and an isomorphism is called an \term{automorphism}.
  \end{thmenum}
\end{definition}

\begin{example}\label{ex:bijective_order_homomorphism_not_isomorphism}
  Consider the \hyperref[def:set_of_integers]{set of integers \( \BbbZ \)} endowed with two different \hyperref[def:partially_ordered_set]{partial orders}:
  \begin{itemize}
    \item The standard total order \( \leq \) where \( n \leq m \) if there exists a nonnegative integer \( k \) such that \( n + k = m \).
    \item The equality \( = \) relation, which is simply the \hyperref[def:binary_relation/diagonal]{diagonal relation} \( \increment \).
  \end{itemize}

  The identity \( \id(x) = x \) is an \hyperref[def:partially_ordered_set/homomorphism]{order homomorphisms} from \( (\BbbZ, =) \) to \( (\BbbZ, \leq) \). Indeed, for any integers \( n \) and \( m \), \( n = m \) implies \( n \leq m \).

  Furthermore, the identity function is bijective. The inverse of \( \id \), which is again \( \id \), is not however a homomorphism from \( (\BbbZ, \leq) \) to \( (\BbbZ, =) \) since, for example, \( 1 \leq 2 \), but \( 1 \neq 2 \).

  Hence, \( \id: (\BbbZ, =) \to (\BbbZ, \leq) \) is a bijective homomorphism, but not an isomorphism.
\end{example}

\begin{proposition}\label{thm:automorphism_without_predicate_symbols}
  Let \( \mscrL \) be a language without predicate symbols. Then any bijective homomorphism between structures for \( \mscrL \) is an \hyperref[def:first_order_homomorphism_invertibility/isomorphism]{isomorphism}.

  This applies to arbitrary languages if we restrict ourselves to \hyperref[rem:first_order_strong_homomorphism]{strong homomorphisms}.
\end{proposition}
\begin{proof}
  If \( \mscrL \) has no predicate symbols, then \fullref{def:first_order_homomorphism/predicates} is vacuously satisfied for the inverse of any bijective homomorphism. It is just as well satisfied if we restrict ourselves to strong homomorphisms.
\end{proof}

\begin{definition}\label{def:positive_formula}
  We say that a \hyperref[def:propositional_syntax/formula]{propositional formula} \( \varphi \) is \term{positive} if it contains only \hyperref[def:cnf_and_dnf]{positive literals} connected using \hyperref[def:propositional_language/connectives/conjunction]{conjunction \( \wedge \)} and \hyperref[def:propositional_language/connectives/disjunction]{disjunction \( \vee \)}. We can also add propositional constants, however that would be redundant due to \fullref{thm:binary_lattice_operations/identity}.

  The point of positive formulas is to avoid \hyperref[def:propositional_language/negation]{negation \( \neg \)}. This definition is not equivalent to \hyperref[def:positive_implicational_deductive_system]{positive implicational formulas} where \( \rightarrow \) is the only connective. We avoid adding \( \rightarrow \) because that would allow us, assuming classical logic, to derive negation using \fullref{thm:boolean_equivalences/negation_bottom}.

  Positive formulas are used in \fullref{thm:positive_formulas_preserved_under_homomorphism}, which fails to hold for some non-positive formulas --- see \fullref{ex:monoid_cancellation_not_preserved_by_homomorphism}.

  When dealing with first-order logic, we simply use \hyperref[thm:first_order_substitution_equivalence/propositional]{substitution} to replace propositional variables with atomic formulas. This way we obtain positive first-order formulas with \hyperref[thm:implicit_universal_quantification]{implicit universal quantification}. Of course, we can always add explicit universal quantifiers, but we avoid existential quantifiers because of \fullref{thm:first_order_quantifiers_are_dual}.
\end{definition}

\begin{proposition}\label{thm:positive_formulas_preserved_under_homomorphism}
  Let \( \mscrX = (X, I) \) and \( \mscrY = (Y, I) \) be structures over a common language \( \mscrL \) and let \( h: X \to Y \) be a \hyperref[def:first_order_homomorphism]{homomorphism} between them. Take \( \Gamma \) to be a nonempty set of \hyperref[def:positive_formula]{positive formulas}.

  Then \( h \) preserves models of \( \Gamma \). That is, if \( \mscrX \vDash \Gamma \) then \( (h(X), I_Y) \vDash \Gamma \).
\end{proposition}
\begin{proof}
  Let \( v \) a variable assignment in \( \mscrY \). Let \( v_X: \boldop{Var} \to X \) be an assignment such that for any variable \( \xi \) we have
  \begin{equation*}
    v_X(\xi) \in h^{-1}(v_Y(\xi)).
  \end{equation*}

  At least one such assignment exists due to \fullref{def:zfc/choice}. If \( h \) is injective, this assignment is unique.

  We will show that
  \begin{equation}\label{thm:positive_formulas_preserved_under_homomorphism/ind_hyp_x}
    \mscrX \vDash_{v_X} \varphi
  \end{equation}
  implies
  \begin{equation}\label{thm:positive_formulas_preserved_under_homomorphism/ind_hyp_y}
    (h(X), I_Y) \vDash_{v_Y} \varphi.
  \end{equation}

  We assume \eqref{thm:positive_formulas_preserved_under_homomorphism/ind_hyp_x} for \( \varphi \) and we use induction on the structure of \( \varphi \) to prove \eqref{thm:positive_formulas_preserved_under_homomorphism/ind_hyp_y}, starting with different \hyperref[def:first_order_syntax/atomic_formula]{atomic formulas}:
  \begin{itemize}
    \item The constant \( \top \) is vacuously preserved by homomorphisms because it does not depend on the interpretation or variable assignment.

    \item Suppose that \( \varphi = (\tau_1 \doteq \tau_2) \). We have \( \tau_1\Bracks{v_X} = \tau_2\Bracks{v_X} \) and hence \( h(\tau_1\Bracks{v_X}) = h(\tau_2\Bracks{v_X}) \) and
    \begin{equation*}
      \tau_1\Bracks{v_Y}
      \reloset {\ref{thm:def:first_order_homomorphism/term_valuation}} =
      h(\tau_1\Bracks{v_X})
      =
      h(\tau_2\Bracks{v_X})
      \reloset {\ref{thm:def:first_order_homomorphism/term_valuation}} =
      \tau_2\Bracks{v_Y}.
    \end{equation*}

    \item Suppose that \( \varphi \) is the predicate formula \( p(\tau_1, \ldots, \tau_n) \). By assumption for every variable assignment in \( \mscrX \) and, in particular, for any \( v_X \),
    \begin{equation*}
      \mscrX \vDash_{v_X} p(\tau_1, \ldots, \tau_n),
    \end{equation*}
    then
    \begin{equation}\label{eq:thm:positive_formulas_preserved_under_homomorphism/predicates/x}
      I_X(p) \parens[\Big]{ \tau_1\Bracks{v_X}, \ldots, \tau_n\Bracks{v_X} } = T.
    \end{equation}

    By definition of homomorphism, we have
    \begin{equation}\label{eq:thm:positive_formulas_preserved_under_homomorphism/predicates/y}
      I_Y(p) \parens[\Big]{ h(\tau_1\Bracks{v_X}), \ldots, h(\tau_n\Bracks{v_X}) } = T.
    \end{equation}

    Now
    \begin{equation*}
      (h(X), I_Y) \vDash_{v_Y} p(\tau_1, \ldots, \tau_n),
    \end{equation*}
    follows from \fullref{thm:def:first_order_homomorphism/term_valuation}.

    If \( h \) is a \hyperref[rem:first_order_strong_homomorphism]{strong homomorphism}, then the converse also holds, i.e. \eqref{eq:thm:positive_formulas_preserved_under_homomorphism/predicates/x} follows from \eqref{eq:thm:positive_formulas_preserved_under_homomorphism/predicates/y}. See \fullref{thm:arbitrary_formulas_preserved_under_isomorphisms} for an application of this converse.

    \item Suppose that \( \varphi = \psi_1 \wedge \psi_2 \) and that the inductive hypothesis holds for \( \psi_1 \) and \( \psi_2 \).

    Since \( \varphi\Bracks{v_X} = T \) by assumption, by definition of valuation of conjunction we have
    \begin{equation*}
      \psi_1\Bracks{v_X}
      =
      \psi_2\Bracks{v_X}
      =
      T.
    \end{equation*}

    This allows us to apply the inductive hypothesis to obtain
    \begin{equation*}
      \psi_1\Bracks{v_Y}
      =
      \psi_2\Bracks{v_Y}
      =
      T.
    \end{equation*}
    and conclude that
    \begin{equation*}
      \varphi\Bracks{v_Y}
      =
      \psi_1\Bracks{v_Y} \wedge \psi_2\Bracks{v_Y}
      =
      T \wedge T
      =
      T.
    \end{equation*}

    \item Suppose that \( \varphi = \psi_1 \vee \psi_2 \) and that the inductive hypothesis holds for \( \psi_1 \) and \( \psi_2 \).

    Since the formula \( \varphi \) is valid in \( \mscrX \), at least one of \( \psi_1 \) or \( \psi_2 \) is valid under \( v_X \). For different \( v_X \) the valuation pair \( (\psi_1\Bracks{v_X}, \psi_2\Bracks{v_X}) \) may be different, but will always have at least one \( T \) value.

    The inductive hypothesis holds for both \( \psi_1 \) and \( \psi_2 \) and therefore \( (\psi_1\Bracks{v_Y}, \psi_2\Bracks{v_Y}) \) also contains at least one \( T \) value.

    This allows us to conclude that
    \begin{equation*}
      \varphi\Bracks{v_Y}
      =
      \psi_1\Bracks{v_Y} \vee \psi_2\Bracks{v_Y}
      =
      T.
    \end{equation*}

    \item To see how this proof fails for conditionals, consider \( \varphi = (\psi_1 \rightarrow \psi_2) \). Then \( \varphi\Bracks{v_X} = T \) implies either \( \psi_1\Bracks{v_X} = F \) or \( \psi_1\Bracks{v_X} = \psi_2\Bracks{v_X} = T \).

    If \( \psi_1\Bracks{v_X} = \psi_2\Bracks{v_X} = F \), we have \( \varphi\Bracks{v_X} = T \), but we cannot conclude that \( \varphi\Bracks{v_Y} = T \) because that would require the \hyperref[def:material_implication/inverse]{inverse} of the inductive hypothesis to hold for \( \psi_1 \) and \( \psi_2 \).

    See \fullref{ex:monoid_cancellation_not_preserved_by_homomorphism} for an example where a conditional is not preserved by a homomorphism.
  \end{itemize}

  Since \( v \) was chosen arbitrarily, we conclude that
  \begin{equation*}
    (h(X), I_Y) \vDash \varphi.
  \end{equation*}
\end{proof}

\begin{corollary}\label{thm:substructure_is_model}
  If \( \Gamma \) is a set of positive formulas, any \hyperref[def:first_order_substructure]{substructure} of a model of \( \Gamma \) is again a model of \( \Gamma \).
\end{corollary}
\begin{proof}
  Follows from \fullref{thm:def:first_order_homomorphism/submodel} and \fullref{thm:positive_formulas_preserved_under_homomorphism}.
\end{proof}

\begin{proposition}\label{thm:arbitrary_formulas_preserved_under_isomorphisms}\mcite[thm. 25.9]{OpenLogicFull}
  If \( \mscrX = (X, I_X) \) is a model of \( \Gamma \) and if \( h: X \to Y \) is a embedding from \( \mscrX \) to \( \mscrY = (Y, I_Y) \), then \( (h(X), I_Y) \) is also a model of \( \Gamma \).

  We say that embeddings preserve arbitrary formulas.

  If \( (h(X), I_Y) \) is a model of \( \Gamma \), then \( \mscrX \) is also a model if \( h \) is a \hyperref[rem:first_order_strong_homomorphism]{strong homomorphism}.

  We say that strong embeddings reflect arbitrary formulas.
\end{proposition}
\begin{proof}
  The proof simply extends the induction in the proof of \fullref{thm:arbitrary_formulas_preserved_under_isomorphisms} to
  \begin{equation*}
    \varphi\Bracks{v_Y} = \varphi\Bracks{v_Y}
  \end{equation*}
  which allows us to use the usual induction on the negation and all connectives and quantifiers.

  The result regarding strong homomorphisms is shown in the note about \eqref{eq:thm:positive_formulas_preserved_under_homomorphism/predicates/x} following from \eqref{eq:thm:positive_formulas_preserved_under_homomorphism/predicates/y} under strong homomorphisms.
\end{proof}

\begin{proposition}\label{thm:functions_over_model_form_model}
  Let \( \Gamma \) to be a nonempty set of \hyperref[def:positive_formula]{positive formulas}. Let \( \mscrX \) be a model of \( \Gamma \) and let \( A \) be any nonempty set. Consider the set \( Y \coloneqq \fun(A, \mscrX) \) of \hyperref[def:function]{all set functions} from \( A \) to \( X \).

  Define the function \( \iota: X \mapsto Y \) by sending each \( x \in X \) to the corresponding constant function in \( Y \).

  Define the interpretation \( I_Y \) as follows:
  \begin{itemize}
    \item For each \( n \)-ary functional symbol \( f \) in \( \mscrL \), define the interpretation of the functions \( k_1, \ldots, k_n \) componentwise as
    \begin{equation*}
      \begin{aligned}
        &I_Y(f): Y^n \to Y \\
        &I_Y(f) \parens[\Big]{ k_1, \ldots, k_n } \coloneqq \parens[\Big]{ s \mapsto I(f) \parens[\Big]{ k_1(s), \ldots, k_n(s) } }.
      \end{aligned}
    \end{equation*}

    \item For each \( n \)-ary predicate symbol \( p \) in \( \mscrL \), define \( I_Y(p) \subseteq Y^n \) via
    \begin{equation*}
      \begin{aligned}
        &I_Y(p): Y^n \to \set{ T, F } \\
        &I_Y(p) \parens[\Big]{ k_1, \ldots, k_n } \coloneqq \bigwedge \set[\Big]{ I(p) \parens[\Big]{ k_1(s), \ldots, k_n(s) } \given* s \in S }.
      \end{aligned}
    \end{equation*}

    That is, \( I_Y(p) (k_1, \ldots, k_n) = T \) if and only if \( I(p) (k_1(s), \ldots, k_n(s)) = T \) simultaneously for all \( s \in S \).
  \end{itemize}

  Then the structure \( \mscrY = (Y, I_Y) \) is also a model of \( \Gamma \) and \( \iota: \mscrX \to \mscrY \) is an embedding.
\end{proposition}
\begin{proof}
  It is obvious that \( \mscrY \) is a structure and that \( \iota \) is an embedding. We will prove using induction on the structure of a formula \( \varphi \) that \( \mscrX \vDash \varphi \) implies \( \mscrY \vDash \varphi \).

  Let \( v_Y \) be a variable assignment in \( \mscrY \).

  Suppose that \( \mscrX \vDash \varphi \). We use induction on the structure of \( \varphi \) to show that \( \varphi\Bracks{v_Y} = T \).
  \begin{itemize}
    \item If \( \varphi \) is a propositional constant, its value does not depend on \( v_Y \) and thus \( \varphi\Bracks{v_Y} = \varphi\Bracks{v_X} \).

    \item If \( \varphi = (\tau_1 \doteq \tau_2) \), then \( \tau_1\Bracks{v_X} = \tau_2\Bracks{v_X} \) for all assignments \( v_X \) in \( \mscrX \), hence for any \( s \in S \) we have \( \parens[\Big]{\tau_1\Bracks{v_Y}}(s) = \parens[\Big]{\tau_2\Bracks{v_Y}}(s) \) since both sides of the equality here are elements of \( \mscrX \).

    \item Analogously, if \( \varphi = p(\tau_1, \ldots, \tau_n) \), then
    \begin{equation*}
      I_Y(p) \parens[\Big]{ k_1, \ldots, k_n }
      =
      \bigwedge \set[\Big]{ I(p) \parens[\Big]{ k_1(s), \ldots, k_n(s) } \given* s \in S }
      =
      \bigwedge \set{ T \given s \in S }
      =
      T.
    \end{equation*}

    \item Analogous to the proof of \fullref{thm:positive_formulas_preserved_under_homomorphism}, conjunction and disjunction formulas that are valid in \( \mscrX \) are valid in \( \mscrY \) while conditional formulas may fail to be valid. See \fullref{ex:thm:functions_over_model_of_positive_formulas_form_model} for examples where this proposition fails.
  \end{itemize}
\end{proof}

\begin{example}\label{ex:thm:functions_over_model_of_positive_formulas_form_model}
  While the statement of \fullref{thm:functions_over_model_form_model} may be a little cryptic, a few examples show that it is actually obvious.
  \begin{itemize}
    \item \hyperref[def:boolean_function]{Boolean functions} have their values in the Boolean algebra \( \set{ T, F } \). Let \( S \) be the set of all tuples of values in \( \set{ T, F }^n \) for arbitrary \( n \). That is,
    \begin{equation*}
      S \coloneqq \bigcup_{n \geq 1} \set{ T, F }^n.
    \end{equation*}

    Then from \fullref{thm:functions_over_model_form_model} it follows that the set \( B = \fun(S, \set{ T, F }) \) of all Boolean functions of arbitrary arities is again a Boolean algebra. See \fullref{thm:lindenmaum_tarski_algebra_of_full_propositional_logic/bijection} for further discussion.

    \item If \( R \) is a \hyperref[def:ring]{ring} and \( A \) is any set, then \( \fun(A, R) \) is again a ring with componentwise operations --- see \fullref{thm:functions_over_algebra}.

    This is useful in functional analysis where we study real-valued and complex-valued functions over arbitrary sets.

    \item If \( \BbbK \) is a field, then in general \( \fun(A, \BbbK) \) is not a field. The simplest example are the real-valued real functions --- \( \sin(x) \) has no multiplicative inverse since \( \sfrac 1 {\sin(x)} \) is not defined for \( x = 2k\pi, k = 1, 2, \ldots \). We can form a \hyperref[def:ring_localization]{field of fractions}, but in general fields of fractions over function rings no longer correspond to functions --- they are purely algebraic constructions, just like \hyperref[def:formal_power_series]{formal power series}.

    This happens because the definition of a field and, more generally has a non-positive axiom --- it requires every nonzero element to have a multiplicative inverse, which can be described formally as
    \begin{equation*}
      \qforall \xi \parens[\Big]{ (\xi \doteq 0) \vee \qexists \eta (\xi \cdot \eta \doteq 1) }.
    \end{equation*}

    As discussed in \fullref{def:positive_formula}, a formula with an existential quantifier may fail to be positive.
  \end{itemize}
\end{example}

\begin{definition}\label{def:first_order_definability}
  Fix a \hyperref[def:first_order_syntax]{first-order language} \( \mscrL \) and a \hyperref[def:first_order_structure]{structure} \( \mscrX = (X, I) \) for \( \mscrL \).

  To every formula \hyperref[def:first_order_syntax]{formula} \( \varphi[\xi_1, \ldots, \xi_n] \) there corresponds a set \( A \subseteq X^n \) such that
  \begin{equation*}
    (x_1, \ldots, x_n) \in A \T{if and only if} \varphi\Bracks{x_1, \ldots, x_n} = T.
  \end{equation*}

  We say that \( \varphi \) \term{defines} \( A \). An arbitrary set \( A \subseteq X^n \) is \term{definable} if there exists a formula \( \varphi \) that defines \( A \).

  See \fullref{thm:cumulative_hierarchy_model_of_zfc} for how this concept deeply relates to set theory.
\end{definition}

\begin{proposition}\label{thm:automorphism_preserves_validity}
  Let \( \mscrX = (X, I) \) be a structure over some language \( \mscrL \). Then for every formula \( \varphi[\xi_1, \ldots, \xi_n] \) in \( \mscrL \) and any \hyperref[def:first_order_homomorphism_invertibility/automorphism]{automorphism} \( h: X \to X \) we have
  \begin{equation*}
    \varphi\Bracks{x_1, \ldots, x_n} = \varphi\Bracks{h(x_1), \ldots, h(x_n)}.
  \end{equation*}
\end{proposition}
\begin{proof}
  Let \( x_1, \ldots, x_n \) be points in \( X \) such that
  \begin{equation*}
    \varphi\Bracks{x_1, \ldots, x_n} = T.
  \end{equation*}

  Since \( h \) is a homomorphism, it follows that
  \begin{equation*}
    \varphi\Bracks{h(x_1), \ldots, h(x_n)} = \varphi\Bracks{x_1, \ldots, x_n} = T.
  \end{equation*}

  Conversely, suppose that \( x_1, \ldots, x_n \) are points in \( X \) such that
  \begin{equation*}
    \varphi\Bracks{h(x_1), \ldots, h(x_n)} = T.
  \end{equation*}

  Then since \( h^{-1} \) is a homomorphism, we have
  \begin{equation*}
    \varphi\Bracks{h^{-1}(h(x_1)), \ldots, h^{-1}(h(x_n))}
    =
    \varphi\Bracks{x_1, \ldots, x_n}
    =
    T.
  \end{equation*}

  Therefore,
  \begin{equation*}
    \varphi\Bracks{h(x_1), \ldots, h(x_n)} = T \T{if and only if} \varphi\Bracks{x_1, \ldots, x_n} = T,
  \end{equation*}
  which is equivalent to the statement of the proposition.
\end{proof}

\begin{corollary}\label{thm:automorphism_of_definable_set}
  Let \( \mscrX = (X, I) \) be a structure over some language \( \mscrL \).

  \begin{thmenum}
    \thmitem{thm:automorphism_of_definable_set/direct} If the set \( A \subseteq X \) is \hyperref[def:first_order_definability]{definable} and if \( h: X \to X \) is an automorphism, then \( h(A) = A \).
    \thmitem{thm:automorphism_of_definable_set/contrapositive} If for some automorphism \( h: X \to X \) we have \( h(A) \neq A \), then the set \( A \subseteq X \) is not definable.
  \end{thmenum}
\end{corollary}
\begin{proof}
  \SubProofOf{thm:automorphism_of_definable_set/direct} If \( A \) is definable via \( \varphi[\xi] \), then by \fullref{thm:automorphism_preserves_validity} for any automorphism \( h: X \to X \) we have \( \varphi\Bracks{x} = \varphi\Bracks{h(x)} \). Thus, the set \( h(A) \) is just as well-definable via \( \varphi \), which implies \( A = h(A) \).

  \SubProofOf{thm:automorphism_of_definable_set/contrapositive} This is the contrapositive of \fullref{thm:automorphism_of_definable_set/direct}.
\end{proof}


\section{{\tt DChvMtx}: Double Precision Chevron Matrix Object}
\par
Let $A$ be an $n \times n$ matrix to be factored.
We are going to allow both row and column permutations as the
factorization proceeds, and our pivot blocks may be larger
than $1 \times 1$ pivots.
After the first step we have
$$
A = A_0
=
P_0
\left \lbrack \begin{array}{cc}
I   & 0 \\
{\hat L}_0 & I
\end{array} \right \rbrack
\left \lbrack \begin{array}{cc}
I   & 0 \\
0   & {\hat A}_1
\end{array} \right \rbrack
\left \lbrack \begin{array}{cc}
{\hat D}_0 & {\hat U}_0 \\
 0  & I
\end{array} \right \rbrack
Q_0
= P_0 \ L_0 \ A_1 \ U_0 \ Q_0
$$
where $P_0$ is the row permutation matrix,
${\hat D}_0$ is the pivot block,
$\displaystyle L_0 =
\left \lbrack \begin{array}{cc}
I   & 0 \\
{\hat L}_0 & I
\end{array} \right \rbrack
$,
$\displaystyle A_1 =
\left \lbrack \begin{array}{cc}
I   & 0 \\
0   & {\hat A}_1
\end{array} \right \rbrack
$,
$\displaystyle U_0 =
\left \lbrack \begin{array}{cc}
{\hat D}_0 & {\hat U}_0 \\
 0  & I
\end{array} \right \rbrack
$ and
$Q_0$ is the column permutation matrix.
Now $A_1$ can be factored in the same manner,
$$
A_1
=
P_1
\left \lbrack \begin{array}{cc}
I   & 0 \\
{\hat L}_1 & I
\end{array} \right \rbrack
\left \lbrack \begin{array}{cc}
I   & 0 \\
0   & {\hat A}_2
\end{array} \right \rbrack
\left \lbrack \begin{array}{cc}
{\hat D}_1 & {\hat U}_1 \\
 0  & I
\end{array} \right \rbrack
Q_1
= P_1 \ L_1 \ A_2 \ U_1 \ Q_1
$$
so we can expand $A_0$ to find
$A = P_0 \ L_0 \ P_1 \ L_1 \ A_2 \ U_1 \ Q_1 \ U_0 \ Q_0$.
Eventually the factorization proceeds to completion and we have
$$
A = (P_0 \ L_0 \ P_1 \ L_1 \ \cdots \ P_k \ L_k)
    (U_K \ Q_k \ \cdots \ U_1 \ Q_1 \ U_0\ Q_0)
$$
Note, $L_0, L_1, \ldots, L_k$ are all lower triangular,
for it is the row permutation matrices that are responsible.
Note that ${\tilde L}_i = P_i L_i$ is not a lower triangular
matrix, but one can solve a linear system involving ${\tilde L}_i$
without any difficulty.
A similar statement holds for ${\tilde U}_i = U_i Q_i$,
except we have the presence of a block pivot ${\hat D}_i$.
\par
The key to understanding how we factor and solve linear systems
using the {\tt DChvMtx} object is that the permutation matrices
$P_i$ and $Q_i$ are held implicitly in
${\tilde L}_i$
and
${\tilde U}_i$,
and we solve any linear system with
${\tilde L}_i$
and
${\tilde U}_i$ correctly,
even though they are not triangular.
We can write $A$ as
$$
A =
{\tilde L}_0 \
{\tilde L}_1 \
\cdots \
{\tilde L}_k \
{\tilde U}_k \
\cdots \
{\tilde U}_1 \
{\tilde U}_0
$$
and thus $A y = b$ by solving a sequence of linear systems
${\tilde L}_0 \ x^0 = b$,
${\tilde L}_1 \ x^1 = x^0$,
$\cdots$,
${\tilde L}_k \ x^k = x^{k-1}$,
${\tilde U}_k \ y^k = x^k$,
${\tilde U}_{k-1} \ y^{k-1} = y^k$,
$\cdots$,
${\tilde U}_1 \ y^1 = y^2$,
${\tilde U}_0 \ y = y^1$.
% Here is a crucial point:
% solving a linear system with ${\tilde L}_i$ requires
% information about the row permutation at the $i$-th step,
% but does not require any knowledge about the column permutation
% at that or any other step.
% A similar point holds for systems with ${\tilde U}_i$.
\par
Let us clarify the process with an example.
At the first step we permute a $3 \times 3$ matrix so that the
$a_{1,2}$ element is the pivot.
\begin{eqnarray*}
A & = &
\left \lbrack \begin{array}{ccc}
a_{0,0} & a_{0,1} & a_{0,2} \\
a_{1,0} & a_{1,1} & a_{1,2} \\
a_{2,0} & a_{2,1} & a_{2,2}
\end{array} \right \rbrack
 =
\left \lbrack \begin{array}{ccc}
0 & 1 & 0 \\
1 & 0 & 0 \\
0 & 0 & 1
\end{array} \right \rbrack
\left \lbrack \begin{array}{ccc}
a_{1,2} & a_{1,0} & a_{1,1} \\
a_{0,2} & a_{0,0} & a_{0,1} \\
a_{2,2} & a_{2,0} & a_{2,1}
\end{array} \right \rbrack
\left \lbrack \begin{array}{ccc}
0 & 0 & 1 \\
1 & 0 & 0 \\
0 & 1 & 0
\end{array} \right \rbrack \\
& = &
\left \lbrack \begin{array}{ccc}
0 & 1 & 0 \\
1 & 0 & 0 \\
0 & 0 & 1
\end{array} \right \rbrack
\left \lbrack \begin{array}{ccc}
1 & 0 & 0 \\
l_{0,2} & 1 & 0 \\
l_{2,2} & 0 & 1
\end{array} \right \rbrack
\left \lbrack \begin{array}{ccc}
1 & 0 & 0 \\
0 & {\hat a}_{0,0} & {\hat a}_{0,1} \\
0 & {\hat a}_{2,0} & {\hat a}_{2,1}
\end{array} \right \rbrack
\left \lbrack \begin{array}{ccc}
d_{1,2} & u_{1,0} & u_{1,1} \\
0 & 1 & 0 \\
0 & 0 & 1
\end{array} \right \rbrack
\left \lbrack \begin{array}{ccc}
0 & 0 & 1 \\
1 & 0 & 0 \\
0 & 1 & 0
\end{array} \right \rbrack \\
& = &
{\tilde L}_0 \ A_1 \ {\tilde U}_0
=
\left \lbrack \begin{array}{ccc}
l_{0,2} & 1 & 0 \\
1 & 0 & 0 \\
l_{2,2} & 0 & 1
\end{array} \right \rbrack
\left \lbrack \begin{array}{ccc}
1 & 0 & 0 \\
0 & {\hat a}_{0,0} & {\hat a}_{0,1} \\
0 & {\hat a}_{2,0} & {\hat a}_{2,1}
\end{array} \right \rbrack
\left \lbrack \begin{array}{ccc}
u_{1,0} & u_{1,1} & d_{1,2} \\
1 & 0 & 0 \\
0 & 1 & 0
\end{array} \right \rbrack
\end{eqnarray*}
We then do the same for $A_1$, using ${\hat a}_{2,1}$ as the pivot.
\begin{eqnarray*}
A_1 & = &
\left \lbrack \begin{array}{ccc}
1 & 0 & 0 \\
0 & {\hat a}_{0,0} & {\hat a}_{0,1} \\
0 & {\hat a}_{2,0} & {\hat a}_{2,1}
\end{array} \right \rbrack
 =
\left \lbrack \begin{array}{ccc}
1 & 0 & 0 \\
0 & 0 & 1 \\
0 & 1 & 0
\end{array} \right \rbrack
\left \lbrack \begin{array}{ccc}
1 & 0 & 0 \\
0 & {\hat a}_{2,1} & {\hat a}_{2,0} \\
0 & {\hat a}_{0,1} & {\hat a}_{0,0}
\end{array} \right \rbrack
\left \lbrack \begin{array}{ccc}
1 & 0 & 0 \\
0 & 0 & 1 \\
0 & 1 & 0
\end{array} \right \rbrack \\
& = &
\left \lbrack \begin{array}{ccc}
1 & 0 & 0 \\
0 & 0 & 1 \\
0 & 1 & 0
\end{array} \right \rbrack
\left \lbrack \begin{array}{ccc}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & l_{0,1} & 1
\end{array} \right \rbrack
\left \lbrack \begin{array}{ccc}
1 & 0 & 0 \\
0 & {\hat a}_{2,1} & {\hat a}_{2,0} \\
0 & {\hat a}_{0,1} & {\hat a}_{0,0}
\end{array} \right \rbrack
\left \lbrack \begin{array}{ccc}
1 & 0 & 0 \\
0 & d_{2,1} & u_{2,0} \\
0 & 0 & 1
\end{array} \right \rbrack
\left \lbrack \begin{array}{ccc}
1 & 0 & 0 \\
0 & 0 & 1 \\
0 & 1 & 0
\end{array} \right \rbrack \\
& = &
{\tilde L}_1 \ A_2 \ {\tilde U}_1
=
\left \lbrack \begin{array}{ccc}
1 & 0 & 0 \\
0 & l_{0,1} & 1 \\
0 & 1 & 0
\end{array} \right \rbrack
\left \lbrack \begin{array}{ccc}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & {\tilde a}_{0,0}
\end{array} \right \rbrack
\left \lbrack \begin{array}{ccc}
1 & 0 & 0 \\
0 & u_{2,0} & d_{2,1} \\
0 & 1 & 0
\end{array} \right \rbrack
\end{eqnarray*}
Finally, we have the factorization of $A_2$.
$$
A_2
=
P_2 \ L_2 \ U_2 \ Q_2
=
\left \lbrack \begin{array}{ccc}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1
\end{array} \right \rbrack
\left \lbrack \begin{array}{ccc}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1
\end{array} \right \rbrack
\left \lbrack \begin{array}{ccc}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & d_{0,0}
\end{array} \right \rbrack
\left \lbrack \begin{array}{ccc}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1
\end{array} \right \rbrack
$$
Written on one line we have the factorization of $A$ given below
where ${\tilde L}_2 = I$ is omitted.
\begin{eqnarray*}
A
& = &
{\tilde L}_0 \
{\tilde L}_1 \
{\tilde U}_2 \
{\tilde U}_1 \
{\tilde U}_0 \\
& = &
\left \lbrack \begin{array}{ccc}
l_{0,2} & 1 & 0 \\
1 & 0 & 0 \\
l_{2,2} & 0 & 1
\end{array} \right \rbrack
\left \lbrack \begin{array}{ccc}
1 & 0 & 0 \\
0 & l_{0,1} & 1 \\
0 & 1 & 0
\end{array} \right \rbrack
\left \lbrack \begin{array}{ccc}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & d_{0,0}
\end{array} \right \rbrack
\left \lbrack \begin{array}{ccc}
1 & 0 & 0 \\
0 & u_{2,0} & d_{2,1} \\
0 & 1 & 0
\end{array} \right \rbrack
\left \lbrack \begin{array}{ccc}
u_{1,0} & u_{1,1} & d_{1,2} \\
1 & 0 & 0 \\
0 & 1 & 0
\end{array} \right \rbrack
\end{eqnarray*}
At best this equation looks awkward, but take heart, it really
doesn't describe how we store the entries or use them to solve a
linear system.
The entries can be logically grouped in terms of chevrons.
$$
A^0 =
\left \lbrack \begin{array}{ccc}
d_{1,2} & u_{1,0} & u_{1,1} \\
l_{0,2} & 0 & 0 \\
l_{2,2} & 0 & 0
\end{array} \right \rbrack,
\qquad
A^1 =
\left \lbrack \begin{array}{ccc}
0 & 0 & 0 \\
0 & d_{2,1} & u_{2,0} \\
0 & l_{0,1} & 0
\end{array} \right \rbrack,
\qquad
A^2 =
\left \lbrack \begin{array}{ccc}
0 & 0 & 0 \\
0 & 0 & 0 \\
0 & 0 & d_{0,0}
\end{array} \right \rbrack,
\qquad
$$
For each chevron we store
the indices of the rows and columns that are nonzero,
and the entries in the diagonal, lower triangular and
upper triangular block.
Note, the row indices and the column indices need not be the same.
\par
Let us show how we solve a linear system.
To solve $A x = b$ we first solve $L y = b$ and then solve $U x = y$
where $L = {\tilde L}_0 {\tilde L}_1 {\tilde L}_2$
and $U = {\tilde U}_2 {\tilde U}_1 {\tilde U}_0$.
Note that there are no permutation matrices anywhere.
\begin{itemize}
\item
To solve with ${\tilde L}_0$ we set $y_1 = b_1$,
$b_0 := b_0 - l_{0,2} y_1$ and
$b_2 := b_2 - l_{2,2} y_1$.
\item
To solve with ${\tilde L}_1$ we set $y_2 = b_2$ and
$b_0 := b_0 - l_{0,1} y_2$.
\item
To solve with ${\tilde L}_2$ we set $y_0 = b_0$.
\item
To solve with ${\tilde U}_2$ we set $x_0 = y_0/d_{0,0}$.
\item
To solve with ${\tilde U}_1$ we set $x_1 = (y_2 - u_{2,0}x_0)/d_{2,1}$.
\item
To solve with ${\tilde U}_0$
we set $x_2 = (y_1 - u_{1,0}x_0 - u_{1,1} x_1)/d_{1,2}$.
\end{itemize}

In general, let us write the $k$-th chevron as
$$
A^2 =
\left \lbrack \begin{array}{cc}
D_{\alpha_k,\beta_k} & U_{\alpha_k,\delta_k} \\
L_{\gamma_k,\beta_k} & 0
\end{array} \right \rbrack
$$
where $\alpha_k$ and $\gamma_k$ form the row index set
and $\beta_k$ and $\delta_k$ form the column index set.
Since the pivots partition the rows and columns, we have
$$
\gamma_k \cap \bigcup_{i < k} \alpha_i = \emptyset
\quad \mbox{and} \quad
\delta_k \cap \bigcup_{i < k} \beta_i = \emptyset.
$$
The forward solve and backward solve are executed by
\begin{center}
\begin{minipage}{3 in}
\begin{tabbing}
XXX\=XXX\=XXX\=\kill
for $k = 1, \ldots, \mbox{\# of pivots}$ \\
\> $y_{\alpha_k} = b_{\alpha_k}$ \\
\> $b_{\gamma_k} := b_{\gamma_k}
                 - L_{\gamma_k, \beta_k} y_{\alpha_k}$ \\
end for \\
for $k = \mbox{\# of pivots}, \ldots, 1$ \\
\> $y_{\alpha_k} = b_{\alpha_k}$ \\
\> $x_{\beta_k} := D_{\alpha_k, \beta_k}^{-1}
(y_{\alpha_k} - U_{\alpha_k, \delta_k} x_{\delta_k})$
end for
\end{tabbing}
\end{minipage}
\end{center}


\subsubsection{Small Open Economy Solution Details}

Consider the household's normalized problem in the SOE model, given in \eqref{eq:scaledv}.  Substituting the latter two constraints into the maximand, this problem has one first order condition (with respect to $\cLev_{t,i}$), which is sufficient to characterize the solution:
\begin{equation}\label{eq:SOEFOC}
\cLev_{t,i}^{-\CRRA} - \underbrace{\Rfree \PLives \beta \Ex_t \big[  (\PtyGro_{t+1} \pmb{\psi}_{t+1,i})^{-\CRRA}\vFunc^\mLev\big(\Rfree/(\PtyGro_{t+1} \pmb{\psi}_{t+1,i}) \aLev_{t,i} + \Wage \pmb{\theta}_{t+1,i},\PtyGro_{t+1}\big) \big]}_{\equiv\,\mathfrak{v}^\aLev(\aLev_{t,i},\PtyGro_t)} = 0
\end{equation}
\begin{equation*}
\Longrightarrow \cLev_{t,i} = \mathfrak{v}^\aLev(\aLev_{t,i},\PtyGro_t)^{-1/\CRRA}.
\end{equation*}

We use the endogenous grid method to solve the model by iterating
on the first order condition.  Eliding some uninteresting complications, our procedure is straightforward:
\begin{enumerate}
\item Construct discrete approximations to the lognormal distributions of $\theta$, $\Theta$, $\psi$, and $\Psi$,
adjusting for the point mass at 0 for $\theta$ with probability $\wp$.  We use equiprobable $N_\psi = N_\theta = 7$
point approximations for the (lognormal portion of) the idiosyncratic shocks and $N_\Psi = N_\Theta = 5$
point approximations for the aggregate shocks.

\item Choose an exogenous grid of end-of-period normalized assets-above-natural-borrowing-constraint $\mathbb{A} = \{\blacktriangle \aLev_j\}_{j=1}^{N_a}$, spanning
the range values that an agent might reasonably encounter in a simulated lifetime.  We use a triple-exponential grid spanning $\blacktriangle \aLev \in [10^{-5},40]$
with $N_a$ = 48 gridpoints.  The natural borrowing constraint is zero because of the possibility of $\theta=0$, so
 assets-above-natural-borrowing-constraint is simply assets $\aLev$.

\item Initialize the guess of the consumption function to $\cFunc(\mLev,\cdot) = \mLev$, the solution for an agent who has no future.

\item Define the marginal value function $\vFunc^\mLev(\cdot)$ as $\uFunc'(\cFunc(\cdot))$, as determined by the
standard envelope condition.

\item Use the discrete approximations to the shock processes and the Markov transition matrix $\Xi$ to
compute $\mathfrak{v}^\aLev(\aLev_j,\PtyGro_k)$ for all $(\aLev_j,\PtyGro_k) \in \mathbb{A} \times \{\PtyGro\}$.

\item Use \eqref{eq:SOEFOC} to find the level of consumption that would make ending the period
with $\aLev_j$ in assets optimal (when aggregate growth is $\PtyGro_k$): $\cLev_{j,k} = \mathfrak{v}^\aLev(\aLev_j,\PtyGro_k)^{-1/\CRRA}$.

\item Calculate beginning of period market resources $\mLev_{j,k} = \aLev_{j,k}+ \cLev_{j,k}$ for all $j,k$.

\item For each $k$, construct $\cFunc(\mLev,\PtyGro_k)$ by linearly interpolating $\cLev_{j,k}$ over $\mLev_{j,k}$, with
an additional point at $(\mLev=0,\cLev=0)$.

\item Calculate the supnorm distance between the newly constructed $\cFunc$ and the previous guess,
evaluated at the $N_\aLev \times ||\{\PtyGro\} ||$ gridpoints.  If the distance is less than $\epsilon = 10^{-6}$, STOP; else go to step 4.
\end{enumerate}

The numerically computed consumption function can then be used to simulate a population of households,
as described in Appendix~\ref{appendix:Simulation}.

\subsubsection{Dynamic Stochastic General Equilibrium Solution Details} \label{app:Sol_DSGE}

Consider the household's normalized problem in the HA-DSGE model,
given in \eqref{eq:DSGEproblemNorm}. Recalling that we are taking the
aggregate saving rule $\aleph$ as given, optimal consumption is
characterized by the solution to the first-order condition:
\begin{equation}\label{eq:DSGEFOC}
\cLev_{t,i}^{-\CRRA} - \underbrace{\beta \Ex \big[ \Rprod_{t+1} (\PtyGro_{t+1} \pmb{\psi}_{t+1,i})^{-\CRRA}\vFunc\big(\Rprod_t \aLev_{t,i}/(\PLives \PtyGro_{t+1}\pmb{\psi}_{t+1,i}) + \pmb{\theta}_{t+1,i} \Wage_{t+1},\MLev_{t+1},\PtyGro_{t+1}\big) \big]}_{\equiv\, \mathfrak{v}^\aLev(\aLev_{t,i},\MLev_t,\PtyGro_t)} = 0
\end{equation}
\begin{equation*}
\Longrightarrow \cLev_{t,i} = \mathfrak{v}^\aLev(\aLev_{t,i},\MLev_t,\PtyGro_t)^{-1/\CRRA}.
\end{equation*}

Solving the HA-DSGE model requires a nested loop procedure in the style of \cite{ksHetero},
as the equilibrium of the model is a fixed point in the space of household beliefs about the
aggregate saving rule. For the outer loop, searching for the equilibrium $\aleph$, we use the following procedure:
\begin{enumerate}
\item Construct a grid of (normalized) aggregate market resources $\mathbb{M} = \{\MLev_j \}_{j=1}^{N_\MLev}$.
We use a $N_M = 19$ point grid based on the steady state of the perfect foresight DSGE model, spanning the range of 10 percent to 500 percent of this value.

\item For each $\PtyGro_k \in \{\PtyGro\}$, initialize the aggregate saving rule to arbitrary values.
We use $\aggrSavingRuleCoeff_{k,0} = 0$ and $\aggrSavingRuleCoeff_{k,1} = 1$; there exist more efficient initial guesses.

\item In the inner loop, solve the household's optimization problem for the current guess of $\aleph$,
using the procedure described below.

\item Simulate many households for many periods, using the procedure described in
Appendix~\ref{appendix:Simulation}, yielding a long \textit{history} of aggregate
market resources, productivity growth, and assets $\mathfrak{H} = \left\{(\MLev_t,\PtyGro_t,\ALev_t)\right\}_{t=0}^T$.

\item For each $k$, define $\mathfrak{H}_k \equiv \left\{ \mathfrak{H} | \PtyGro_t = \PtyGro_k \right\}$.
Regress $\ALev_t$ on $\MLev_t$ on the set $\mathfrak{H}_k$, yielding coefficients
that provide updated values of $\aggrSavingRuleCoeff_{k,0}$ and $\aggrSavingRuleCoeff_{k,1}$ for $\aleph$.

\item Calculate the supnorm distance between the new and previous values of aggregate
saving rule coefficients $\aggrSavingRuleCoeff$.  If it is less than $\grave{\epsilon} = 10^{-4}$, STOP;
else go to step 3.
\end{enumerate}

The inner solution loop (step 3) proceeds very similarly to the SOE solution method above,
with differences in the following steps:
\begin{enumerate}
\setcounter{enumi}{1}

\item The set $\mathbb{A}$ spans $[10^{-5},120]$ because of the higher $\beta$ in the HA-DSGE model.

\setcounter{enumi}{4}

\item End-of-period marginal value of assets is calculated as $\mathfrak{v}^\aLev(\aLev_j,\MLev_k,\PtyGro_\ell)$
for all $(\aLev_j,\MLev_k,\PtyGro_\ell) \in \mathbb{A} \times \mathbb{M} \times \{\PtyGro\}$.

\item Use \eqref{eq:DSGEFOC} to calculate $\cLev_{j,k,\ell} = \mathfrak{v}^\aLev(\aLev_j,\MLev_k,\PtyGro_\ell)^{-1/\CRRA}$.

\setcounter{enumi}{7}

\item For each $\ell$, construct $\cFunc(\mLev,\MLev,\PtyGro_\ell)$ by linearly interpolating $\cLev_{j,k,\ell}$ over $\mLev_{j,k,\ell}$
for each $k$, then interpolating the linear interpolations over $\mathbb{M}$.
\end{enumerate}


\documentclass[]{article}
\usepackage{graphicx}
\newtheorem{Def}{Definition}

%opening
\title{MTH 343 Numerical Analysis Lecture 3: Errors, Solutions of Equations in one Variable}
\author{Sheikh Abdul Raheem Ali}
\date{January 31, 2019}

\begin{document}

\maketitle

The floating point form is obtained by terminating the mantissa using the following two ways:

\begin{enumerate}
	\item Chopping
	\item Rounding
\end{enumerate}

\begin{Def}[Absolute error]
$	|true \ value - approximate \ value| = |p - p^*| $
\end{Def}

\begin{Def}[Relative error]
$ |\frac{true \ value \ - \ approximate \ value}{true \ value}| =  |\frac{p \ - \ p^*}{p}| $
\end{Def}

\[  (approximate \ value ) \ p_1 = 1.2, \ (true \ value) \ p_1^* = 1.2 \]
\[  p_2 = 1000, \ p_2^* = 1000.2 \]

\begin{eqnarray}
absolute \ error &=&  |1 - 1.2| = 0.2 \nonumber \\
&=& |1000 - 1000.2| = 0.2 \nonumber
\end{eqnarray}

\begin{eqnarray}
	rel. \ error &=& |\frac{1 - 1.2}{1}| = 0.2 \nonumber \\
	&=&  |\frac{1000 - 1000.2}{1000}| = 0.0002 \nonumber
\end{eqnarray}

\[ find \ f(x) = x^3 = 61x^3 + 3.2x + 1.5 \ at \ f(x = 4.71) \]

\begin{tabular}{l c c c c c}
	& $ x $ & $ x^2 $ & $ x^3 $ & $ 61x^3 $ & $ 3.2x $ \\ \hline
	Exact &4.7&22.1841&104.487111&135.32301&15.072\\
	3-digit chopping &4.7&22.1&104.0&134&15.0 \\
	3-digit rounding &4.7&22.2&104&135&15.1 \\

\end{tabular}

Exact: -14.263899, Chopping: -13.5. Rounding: -13.4 \\
Rel. error: Chopping: 0.0045, Rounding: 0.0025

\section*{Solutions of Equations of one variable}

\begin{eqnarray}
	0 &=& x^3 - 16x \nonumber \\
	&=& x(x^2 - 16) \nonumber\\
	&=& x(x-4)(x+4) \nonumber\\
	 x = 0,4,-4 \nonumber
\end{eqnarray}

\begin{eqnarray}
	0 &=&  x^2 - 4x + 3x - 12 \nonumber \\
	&=& x(x-4) + 3(x-4) \nonumber \\
	&=& (x-4)(x-3) \nonumber \\
	x = 4,3 \nonumber
\end{eqnarray}

\section*{Next time: Bisection Method (f(x) = 0)}

Find the values of $ x $ for which $ f(x) = 0, $ that is find the points of intersection of $ f(x) $ with the x-axis.

\begin{figure}
	\centering
	\includegraphics[width=\linewidth]{graph.jpeg}
	\caption{Visual demonstration of the bisection algorithm. Credits to Luna Hatahet. }
	\label{fig:graph}
\end{figure}

\end{document}


\section{Starting is Slow}

Like any language, starting to learn a language is slow and in most cases, troublesome. However, a programming language is a bit easier to learn, but harder to master. The reason for this is that one must first know simple English language, basic maths and advanced logic thinking.

To wrap it around quickly, the English language is good to know as most syntax relies on it. The basic maths is good to know for simple algebraic solutions. One could of course need more advanced maths, but this course does not aim such a thing.

For the advanced logic, one need to know this to be able to tell the computer exactly what it should do. It’s extremely rare that a computer is disobedient. If one always assume it will do as told, a program could easily be structured in a logical way to make advanced instructions to the computer.
% Disobedient: Read: Not working correctly

\subsection{Variables}

First thing to bring up is variables. Think these as labelled containers that will hold any datum one want to store within it. Most commonly this includes, but are not restricted to: \firstfound{numbers}, \firstfound{strings} and \firstfound{objects}.
% Datum: Singular to data

\important{Numbers} could either be integer or decimal numbers. These are usually represented with the ordinary base 10 system. One could of course make use of hexadecimal, octagonal and even binary to represent a number. These are left as an exercise to the reader.

\important{Strings} are used to store text. It could contain anything that can be stored as a character. This includes new line, space, even backspace.

\important{Objects} will be mentioned more further down.

To store a value to a variable, one could write like so.

\begin{algorithm}[H]
	\begin{algorithmic}[1]
		\State $age \gets 18$
	\end{algorithmic}
\end{algorithm}

This will store the number 18 into the variable \code{age}. This makes it possible to temporarily store data within the program so the user experience is different depending on the variables content.

This can also be used with strings.

\begin{algorithm}[H]
	\begin{algorithmic}[1]
		\State $name \gets "Jones"$
	\end{algorithmic}
\end{algorithm}

This will store the text \code{Jones} within the variable \code{name}. Take a note in the quotation marks, as those are used to define a string. Anything within those quotation marks will be stored within the variable.

\subsection{Operators}

Naturally, one could not make use of the variables unless one got some sort of modifier. In comes the operators. Most of them are maths operators, but there is some that is specific to the computer. This section will only go through the maths ones, which is also called the \important{arithmetic} operators.

First is the \firstfound{assignment} (\code{=}) operator. It was used briefly in previous section to assign a value to a variable. It is visualized with an equal sign. As long as a variable is on the left side, any value on the right will be assigned to it.

\begin{algorithm}[H]
	\begin{algorithmic}[1]
		\State $x \gets 4$
	\end{algorithmic}
\end{algorithm}

Moreover are \important{addition} (\code{+}), \important{subtraction} (\code{-}), \important{multiplication} (\code{*}) and \important{division} (\code{/}).

\begin{algorithm}[H]
	\begin{algorithmic}[1]
		\State $x \gets 4 + 2$
		\State $y \gets 4 - 2$
		\State $z \gets 4 * 2$
		\State $w \gets 4 / 2$
	\end{algorithmic}
\end{algorithm}

This will result in each variable of \code{x}, \code{y}, \code{z} and \code{w} to contain 6, 2, 8 and 2 respectively. There is also this operator called \important{modulus} (\code{\%}) that is used to get the remainder from a division. How it is used is left as an exercise to the reader.

Along with the arithmetic operators, there is also the \important{relational} operators. These are used to compare values. These are \important{equal} (\code{==}), \important{less than} (\code{<}), \important{greater than} (\code{>}), \important{less equal} (\code{<=}), \important{greater equal} (\code{>=}) and \important{not equal} (\code{\!\=}). Even if they sound somewhat the same, they actually are used in different manners, depending on what one is aiming for.

\begin{algorithm}[H]
	\begin{algorithmic}[1]
		\State $1 = 1$
		\State $1 < 1$
		\State $1 > 1$
		\State $1 \leq 1$
		\State $1 \geq 1$
		\State $1 \neq 1$
	\end{algorithmic}
\end{algorithm}

Each one of these will give a \firstfound{boolean} value: \code{true} or \code{false}. From the top: \code{true}, \code{false}, \code{false}, \code{true}, \code{true}, \code{false}. To explain this, the first one is 1 equals 1, which they are the same. Then is two false, because 1 is neither bigger or smaller than 1. The two following is true even if it checks for values bigger or smaller, as it also checks if they are the same as well, which they are. The last one is checking if they are not equal, which they are not.
Of course one could put in variables in there, to see if its value relation to an another value.

\begin{algorithm}[H]
	\begin{algorithmic}[1]
		\State $x \gets 1$
		\State $y \gets 2$
		\State $y \neq x$
	\end{algorithmic}
\end{algorithm}

This will make the third statement to be true, as the value of \code{y} (2) is not equal the value of \code{x} (1).

\subsection{Conditions}

While setting variables and do operations on them, one cannot do anything unless there is some way to change the program flow. In comes conditions.

\begin{algorithm}[H]
	\begin{algorithmic}[1]
		\State $x \gets 1$
		\If{$x > 0$}
			\State $x \gets 0$
		\EndIf
	\end{algorithmic}
\end{algorithm}

This will first prepare the variable \code{x}. Then it will check with if its value is bigger than 0. As this is the case, the \important{\code{if}}-condition will then check if the value is \code{true}, and if so it will execute the next statements. The \important{\code{then}} is only used to mark the start of a code block. The \important{\code{end}} is closing the previously mentioned code block. A code block is a bunch of statements grouped together.

It is worth noting that the third statement is \important{indented}. This is only to visually tell everyone that reads this that this belongs to the statement preceding it. That is, it makes it a lot easier to understand the code-flow. In some languages, indentation is a requirement.

Along with the if-statement, there is two more normal conditions that one should use. They are \important{\code{else}} and \important{\code{else if}}. The \code{else} is only executed if the preceding conditions were \code{false}. Of course the \code{else if} looks like it is \code{else} and \code{if} concatenated together, but in most languages they are actually split apart.

\begin{algorithm}[H]
	\begin{algorithmic}[1]
		\State $x \gets 3$
		\State $y \gets 4$
		\State $z \gets 3$
		\If{$x = y$}
			\State $y \gets z$
		\ElsIf{$x = z$}
			\State $z \gets y$
		\Else
			\State $x \gets y$
		\EndIf
	\end{algorithmic}
\end{algorithm}

We got three variables with values. Two of them are checked if they are equal. If they are, then it will run the next statement. In this case, it is not and will skip the next code block and go for the next condition. Here it is also is false, so it will proceed to the last code block and execute it.

\subsection{Loops}

After getting to know how to change the flow of the program by jumping to statements that should be executed, one could easily make a program like this.

\begin{algorithm}[H]
	\begin{algorithmic}[1]
		\State $n \gets 1$
		\State $x \gets 1$
		\State \textcolor{highlightred}{$x \gets x * n$}
		\State \textcolor{highlightred}{$n \gets n + 1$}
		\State $x \gets x * n$
		\State $n \gets n + 1$
		\State $x \gets x * n$
		\State $n \gets n + 1$
		\State $x \gets x * n$
	\end{algorithmic}
\end{algorithm}

However, this is duplicated code, highlighted in red. There is a way to make this way shorter and easier to manage. In comes \important{loops}.

\begin{algorithm}[H]
	\begin{algorithmic}[1]
		\State $n \gets 1$
		\State $x \gets 1$
		\While{$n \leq 4$}
			\State $x \gets x * n$
			\State $n \gets n + 1$
		\EndWhile
	\end{algorithmic}
\end{algorithm}

The \important{\code{while}}-statement will repeat its code block until its condition is false. Its condition is checked first, and then each time it repeats its code block. This generates the same result as the previous example.

There is also a loop that make the logic of the example even easier to understand. The \important{\code{for}}-loop is one of the most used loops, as it will easily handle your \important{iterator}. An iterator is exactly what the variable \code{n} is doing in the examples. It will increase by a certain amount of number and then be used to run a code block a certain amount of times or get data from a list or variables.

\begin{algorithm}[H]
	\begin{algorithmic}[1]
		\State $x \gets 1$
		\For{$n \gets 1$ to $5$}
			\State $x \gets x * n$
		\EndFor
	\end{algorithmic}
\end{algorithm}

For variable \code{n}, set the value of 1 and iterate it one step until value is equal or higher than 5. There is several variants of this, but this document will only use this version as it is most used that way.

\subsection{Functions}
% Functions: Write more about the difference of parameters and arguments

Functions are code blocks that one could send in certain variables and execute and then return from where it was called from. The biggest advantages of them is to move out algorithms into them and to reduce duplicate code and reuse them.

\begin{algorithm}[H]
	\begin{algorithmic}[1]
		\Function{add}{$x$, $y$}
			\State $z \gets x + y$
			\State \Return $z$
		\EndFunction
	\end{algorithmic}
\end{algorithm}

This function can then be called upon.

\begin{algorithm}[H]
	\begin{algorithmic}[1]
		\State $x \gets 5$
		\State $z \gets 4$
		\State $y \gets$ \Call{add}{$x$, $z$}
	\end{algorithmic}
\end{algorithm}

Basically, \code{x} and \code{z} is set to a value each. Their value is then sent into the function \code{add}, and now the program flow is moved into there. From there is creates two other variables, and then do some addition and send it into a third variable. Its value is then returned from the function. The program flow will then be moved back to where it was called and the returned value will be assigned to \code{y}.

Keep in mind here that the variables within add does not affect the variables outside of it. These are called \important{local variables}. The name of the parameters could be anything, as they are created like normal variables and assigned the values that are sent into the function, in order.

\begin{algorithm}[H]
	\begin{algorithmic}[1]
		\Function{sum}{$a$}
			\If{$a < 1$}
				\State \Return $a$
			\EndIf
			\State \Return \Call{sum}{$a - 1$}$ + a$
		\EndFunction
	\end{algorithmic}
\end{algorithm}

This function will be called with a number contained within \code{a}. It will then check if it is less than 1. If not, it will continue and call itself. This will continue until \code{a} is less than one, which it then will return normally. All accumulates of \code{a} from each call to sum will then be added together, resulting into \code{a} sum of 0 up to the value of \code{a} in the first call to sum.

\code{sum} is called a \important{recursive} function. This applies whenever a function calls itself directly or indirectly. Indirectly is done when a function first calls another function that in turn calls the first function.

As previously mentioned, each call to the function will create a new set of local variables that does not affect the previous result. This means that the variable \code{a} will be created anew with a new value for that function call.

To make this a bit simpler to understand, the previous section example will be used with recursion instead of a \code{for}-loop.

\begin{algorithm}[H]
	\begin{algorithmic}[1]
		\Function{factorial}{$n$}
			\If{$n \leq 1$}
				\State \Return $n$
			\EndIf
			\State \Return \Call{factorial}{$n - 1$}$ * n$
		\EndFunction
	\end{algorithmic}
\end{algorithm}

This will return the same result as the previous example.

\begin{algorithm}[H]
	\begin{algorithmic}[1]
		\State $x \gets$ \Call{factorial}{$4$}
	\end{algorithmic}
\end{algorithm}

Worth noting is that even if recursive functions are really helpful, it is really easy to make it go indefinitely, if not careful. Most languages does not like to call too many functions, and will crash if going too deep.

\subsection{Objects}

Sometimes there is a mess keeping track on variables. It even gets worse when one needs to, for instance, handle several persons with their respective age and name. Here is where \important{objects} are helpful.
Objects are like variables, a container. But instead of values, they contain variables. One could then easily move around groups of data about certain types.

\begin{algorithm}[H]
	\begin{algorithmic}[1]
		\State $person \gets$ new Person
		\State $person.age \gets 45$
		\State $person.name \gets "James"$
	\end{algorithmic}
\end{algorithm}

The first statement will create a \important{\code{new}} object called Person and store it within the variable \code{person}. Then it will use the \important{dot}(\code{.})-operator to access two of its variables, \code{age} and \code{name}, and then assign them values.

Worth noting is that both the \code{new}-operator and dot-operator is not used in some languages, but are used here to explain how objects are used more easily. For example, some languages uses functions to both create objects and access their variables.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PROBLEM 3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section*{Problem 3}

Consider a 1000 MWE reactor with a 33\% efficiency conversion from MWT to MWE.
What is the minimum volume of UO$_2$, enriched to 3 (atom) \% $^{235}$U that could theoretically supply the yearly energy production of this reactor.
Treat energy contributions as coming only from the fission of $^{235}$U.
These fission events release about 200 MeV with 95\% of that energy staying in the reactor.


    \filetitle{invgamma}{Create function proportional to log of inv-gamma distribution}{logdist/invgamma}

	\paragraph{Syntax}\label{syntax}

\begin{verbatim}
F = logdist.invgamma(MEAN,STD)
\end{verbatim}

\paragraph{Input arguments}\label{input-arguments}

\begin{itemize}
\item
  \texttt{MEAN} {[} numeric {]} - Mean of the inv-gamma distribution.
\item
  \texttt{STD} {[} numeric {]} - Std dev of the inv-gamma distribution.
\end{itemize}

\paragraph{Output arguments}\label{output-arguments}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \texttt{F} {[} function\_handle {]} - Function handle returning a
  value proportional to the log of the inv-gamma density.
\end{itemize}

\paragraph{Description}\label{description}

See \href{logdist/Contents}{help on the logdisk package} for details on
using the function handle \texttt{F}.

\paragraph{Example}\label{example}


\subsection{The Riemann sphere (elliptic)}