\documentclass[12pt,a4]{article}

\usepackage{xcolor}

\newcommand{\handoutdate}{Friday, 2020-06-05}
\newcommand{\firstduedate}{Thursday, 2020-06-12}
\newcommand{\finalduedate}{Thursday, 2020-06-19}

\input{preamble}

\newcommand{\rank}{\textnormal{rank}}
\newcommand{\y}{\mathbf{y}}
\renewcommand{\c}{\mathbf{c}}
\newcommand{\x}{\mathbf{x}}
\newcommand{\z}{\mathbf{z}}
\renewcommand{\u}{\mathbf{u}}
\newcommand{\V}{\mathbf{v}}

\renewcommand{\a}{\mathbf{a}}

\renewcommand{\b}{\mathbf{b}}
\newcommand{\zero}{\mathbf{0}}
\newcommand{\rpn}{\mathbb{R}_{\geq 0}}
\newcommand{\sol}{\textup{\textrm{sol}}}
\newcommand{\opt}{\textup{\textrm{opt}}}
\setcounter{section}{6}

\section{Farkas Lemma and LP Duality}

\subsection{Different Versions of Farkas Lemma}

In the following, let $A \in \R^{m \times n}$ and $\b \in \R^m$, and let
$\x = (x_1,\dots,x_n)^T$ be a column vector of $n$ variables and
$\y = (y_1, \dots,y_m)$ be a row vector of $m$ variables.

\begin{exercise}
 Show that the three versions of Farkas Lemma presented in class are all equivalent (I actually did not present
 the third version in class):
 \begin{align}
   ( \neg \exists \x : \, A \x \leq \b ) \, & \Longleftrightarrow
    ( \exists \y \geq \zero : \, \y^T A = \zero, \,     \y^T \b < 0 ) \ . \\
      ( \neg \exists \x \geq \zero : \, A \x \leq \b ) \, & \Longleftrightarrow
    ( \exists \y \geq \zero : \, \y^T A \geq \zero, \,  \y^T \b < 0 ) \ . \\
   ( \neg \exists \x \geq \zero : \, A \x = \b ) \, & \Longleftrightarrow
    ( \exists \y \begingroup \color{white} \geq \zero \endgroup : \, \y^T A \geq \zero, \,  \y^T \b < 0 ) \ .
 \end{align}
  Note that the direction ``$\Longleftarrow$'' is easy in each case.
  We will show the ``$\Longrightarrow$'' of (1) in class using a technique called {\em Fourier-Motzkin Elimination}.
  This exercise is actually not that hard. The hardest part is keeping track of what you
  want to prove and what you can assume.
\end{exercise}
\begin{proof}
		First we prove (1) and (2) are equivalent. First consider (1) $\implies$ (2). If $\neg \exists \x \ge  \zero : A\x \le \b$,
		notice that $\x \ge \zero $ iff $\left( -I_{n} \right) \x \le \zero$ , we can construct a new maxtrix $A'$
		\[
		A' = \begin{pmatrix} -I \\ A \end{pmatrix} , \b' = \begin{pmatrix} \zero\\ \b \end{pmatrix}
		.\]
		such that our assumption turns into
		\[
		\neg \exists \x : A' \x \le \b'
		.\]
		And by (1), if follows that $\exists \y ' \ge \zero$ s.t. $(\y ')^{\top} A' = 0$ and  $(\y ') ^{\top} \b ' < 0$.
		For convenience, we wirte $\y' = \left( z_1,z_2\ldots,z_n, \y \right) $ while $z_{i} \ge 0$, therefore
		\[
		-z_{i} + \y ^{\top} A_{i} = 0 \implies \y^{\top} A_{i} = z_{i} \ge  0
		.\]
		Which leads that $\y^{\top} A \ge 0$.
		Similarly $(\y')^{\top} \b'= \y ^{\top} \b  < 0 $, thus (2) is true.

		Next we prove (2) $\implies$ (3). If $\neg \x \ge 0, A\x = \b$, which is $A \x \le \b$ plus $\left( -A \right) \x \le -\b$,
		similarly we construct
		\[
		A' = \begin{pmatrix} A \\ -A \end{pmatrix}, \b' = \begin{pmatrix} \b \\ -\b \end{pmatrix}
		.\]
		By (2), $\exists \y' = \left( \y_1, \y_2 \right) $ , such that
		\[
		\y_1 A - \y_2 A \ge 0, \y_1\b - \y_2 \b < 0
		.\]
		Choose $\y = \y_1 - \y_2$ we are done.

		Now we prove (2) $\implies$ (1).

		We know that $\neg \x \ge 0, A\left( \x \right) \le \b$.
		And it's also obvious that $\neg \exists \x \ge 0, A \left( -\x \right) = \left( -A \right) \x\le \b$,
		we can build a new matrix $A' = \left( A, -A \right) $, hence
		\[
				\neg \x' = \begin{pmatrix} \x_1\\ \x_2 \end{pmatrix}  \ge 0, \left( A, -A \right) \begin{pmatrix} \x_1 \\ \x_2 \end{pmatrix} \le 2\b
		.\]
		By (2) we have
		\[
				\exists \y, \y^{\top} A \ge 0, \y ^{\top} \left( -A \right) \ge 0, \y^{\top} \b < 0 \implies \y^{\top} A = 0
		.\]
		Thus (2) $\implies$ (1) is true.

		Finally we prove (3) $\implies$ (2), if $\neg \exists \x \ge 0: A\x \le \b$, similarly construct
		\[
				A' = \left( A, I \right)
		.\]
		We know $\neg \exists \x \ge 0, A' \x' = \b$.
		The result follows directly from (3).
\end{proof}

\subsection{A Linear Program for, well, for what?}

Let $G = (V,E)$ be a directed graph, $s,t \in V$,  and $c: E \rightarrow \mathbf{R}^+$ be a cost
function. We want to find an $s-t$-flow $f$ of value $1$. Every edge $e$ generates cost $f(e) \cdot c(e)$, and we want to minimize the overall cost. There are no capacity constraints.
We can easily write this as a linear program MCF (Minimum Cost Flow):
\begin{align*}
  \textrm{MCF}(G,s,t,c): \qquad
  \begin{array}{ll}
    \textnormal{minimize} \quad & \multicolumn{1}{l}{\sum_{e \in E} c(e) f(e)} \\
    \\
    \textnormal{subject to} \quad & \sum_{v \in V} f(v,t)  = 1 \\
					        & \sum_{u \in V} f(u,v) - \sum_{w \in V} f(v,w)  = 0  \quad \forall\ v \in V  \setminus \{s,t\}\\
					        \\
     & f(e)  \geq 0 \ \forall \ e \in E
  \end{array}
\end{align*}
Note that we have $m$ variables, one variable $f(e)$ for each edge $e$.
The first constraint says that the value of the flow should be 1. The other constraints say that
the inflow at $v$ should equal the outflow.

\begin{exercise}
   Let $d$ be the shortest path distance from $s$ to $t$ in the directed graph $G$, where distance
   means sum of the $c(e)$ along the path. Show that $\opt(MCF) = d$.
   \textbf{Hint.} Make sure you show both $\leq$ and $\geq$.
\end{exercise}

\begin{proof}
To prove $opt(MCF) \le d$, we just need to prove that the shortest path is a solution to $MCF$.
    We set $f(e) = 1$ along all edges in the shortest path, since there is only one path with flow $1$,
    The constraints are obviously satisfied. So it is a solution of $MCF$, and its value is $1 * d = d$,
    so $opt(MCF) \le d$

    To prove $opt(MCF) \ge d$, we need to prove that all solutions of $MCF$ is not better than $d$.
    We try to improve the value of all possible solutions to $d$.

    Suppose we have a solution with $x$ different $s-t$ path. Define $b(path)$ be the smallest flow in all
    edges of $path$, $d(path)$ be the length of $path$. Let $sp$ be the shortest $s-t$ path. We do as follows, choose any path $p$ besides $sp$,
    put $b(p)$ units of flow on $p$ to $sp$. Repeat it until there is only $1$ unit flowing through $sp$.

    We need to show in each turn, $val(MCF)$ is not worse than previous and no constraints are broke. We fist prove
    $val(MCF)$ is not worse. In each turn, $val(MCF)' = val(MCF) + d * b(p) - d(p) * b(p)$, $d \le d(p)$, so $val(MCF)' \le val(MCF)$.
    As for the constraints, inflow of $t$ remains to be 1 since we just move $b(p)$ units between two different paths.
    Flow constraints remains since we modify the flow in one path, which means we move inflow and outflow of a single
    vertex at the same time. Since we only have $x$ different $s-t$ path, and the flow value on each path is finite,
    the process terminates. So $val(MCF) \ge d$

    In all $val(MCF) = d$
\end{proof}

\begin{exercise}
    Write down the dual of MCF. This will be a maximization problem. Don't use any matrix notation.
\end{exercise}

\begin{proof}
We introduce a dual coefficient $g_v, v \in V$. The dual program is:
    \begin{itemize}
        \item Maximize $g_t$, subject to:
        \item $g_v - g_u \le c(u, v), \forall (u,v) \in E$
        \item $g_v \in \mathbb{R}, v \in V$
    \end{itemize}
\end{proof}

\begin{exercise}
   Interpret the dual. Show that it is the LP formulation of a ``natural'' maximization problem on $G$.
\end{exercise}

\begin{proof}
If we set $g_s = 0$, then the $g_v$ can be thought as the cost of some $s-t$-path.
    Since each edge $(u,v)$ must satisfy $g_v - g_u \le c(u,v))$, we can not just choose the
    maximal $s-t$-path as solution. Under this constraint, we can see that the solution
    must at first be a \textbf{safe} path, so the program is actually the shortest path problem.
\end{proof}

\begin{exercise}
  Describe an optimal solution of the dual program.
\end{exercise}

\begin{proof}
    The optimal solution is the shortest $s-t$-path $sp$, and for each vertex along the path, we must
    set $g_v = g_u$ for $(u,v), u \in sp, v \notin sp$ accordingly to $g_v-g_u$ constraints.
\end{proof}

\end{document}


\documentclass[]{article}\usepackage[]{graphicx}\usepackage[]{color}
%% maxwidth is the original width if it is less than linewidth
%% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%
\let\hlipl\hlkwb

\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
 \def\at@end@of@kframe{}%
 \ifinner\ifhmode%
  \def\at@end@of@kframe{\end{minipage}}%
  \begin{minipage}{\columnwidth}%
 \fi\fi%
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
 \colorbox{shadecolor}{##1}\hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage}}%
 {\par\unskip\endMakeFramed%
 \at@end@of@kframe}
\makeatother

\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX

\usepackage{alltt}

%\usepackage[numbers]{natbib}
\usepackage[]{natbib}
%\bibliographystyle{plainnat}
%\bibliographystyle{unsrt}
\bibliographystyle{apa}
%opening
\title{Project 1}
\author{Sahir}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}

\maketitle
\begin{abstract}
this is an abstract
\end{abstract}

\section{Linear Regression fit}

This is a demo for including R code in an knitr document. This model was given by~\citep{breiman1996bagging}.
And also by~\citep{yang2017insurance}. He can also be referred to as~\citep{breiman1999prediction}.

This is a demo for including R code in an knitr document. This model was given by~\cite{breiman1996bagging}.
And also by~\cite{yang2017insurance}. He can also be referred to as~\cite{breiman1999prediction}.

\begin{equation}
y = \beta_0 + \beta_1 * X_1 + \epsilon
\end{equation}

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{verbatim}
##
## Call:
## lm(formula = mpg ~ ., data = mtcars)
##
## Residuals:
##     Min      1Q  Median      3Q     Max
## -3.4506 -1.6044 -0.1196  1.2193  4.6271
##
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept) 12.30337   18.71788   0.657   0.5181
## cyl         -0.11144    1.04502  -0.107   0.9161
## disp         0.01334    0.01786   0.747   0.4635
## hp          -0.02148    0.02177  -0.987   0.3350
## drat         0.78711    1.63537   0.481   0.6353
## wt          -3.71530    1.89441  -1.961   0.0633 .
## qsec         0.82104    0.73084   1.123   0.2739
## vs           0.31776    2.10451   0.151   0.8814
## am           2.52023    2.05665   1.225   0.2340
## gear         0.65541    1.49326   0.439   0.6652
## carb        -0.19942    0.82875  -0.241   0.8122
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.65 on 21 degrees of freedom
## Multiple R-squared:  0.869,	Adjusted R-squared:  0.8066
## F-statistic: 13.93 on 10 and 21 DF,  p-value: 3.793e-07
\end{verbatim}
\end{kframe}
\end{knitrout}

\newpage

\begin{kframe}
\begin{alltt}
\hlstd{texreg}\hlopt{::}\hlkwd{texreg}\hlstd{(fit)}
\end{alltt}
\end{kframe}
\begin{table}
\begin{center}
\begin{tabular}{l c }
\hline
 & Model 1 \\
\hline
(Intercept) & $12.30$   \\
            & $(18.72)$ \\
cyl         & $-0.11$   \\
            & $(1.05)$  \\
disp        & $0.01$    \\
            & $(0.02)$  \\
hp          & $-0.02$   \\
            & $(0.02)$  \\
drat        & $0.79$    \\
            & $(1.64)$  \\
wt          & $-3.72$   \\
            & $(1.89)$  \\
qsec        & $0.82$    \\
            & $(0.73)$  \\
vs          & $0.32$    \\
            & $(2.10)$  \\
am          & $2.52$    \\
            & $(2.06)$  \\
gear        & $0.66$    \\
            & $(1.49)$  \\
carb        & $-0.20$   \\
            & $(0.83)$  \\
\hline
R$^2$       & 0.87      \\
Adj. R$^2$  & 0.81      \\
Num. obs.   & 32        \\
RMSE        & 2.65      \\
\hline
\multicolumn{2}{l}{\scriptsize{$^{***}p<0.001$, $^{**}p<0.01$, $^*p<0.05$}}
\end{tabular}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}

\bibliography{bibliography}

\end{document}


\documentclass[12pt]{article}
\usepackage{fullpage}

\pagestyle{empty}

\newcommand{\ie}{{\it i.e.}}%
\newcommand{\reals}{{\mbox{\bf R}}}%
\newcommand{\Span}{\mathop{\bf span}}%
\newcommand{\diag}{\mathop{\bf diag}}%

\title{Subspaces that Minimize the Condition Number of a Matrix}
\author{Siddharth Joshi \and Stephen Boyd\thanks{The authors are with the department of
Electrical Engineering at Stanford University. Email addresses: Siddharth
Joshi: \texttt{sidj@stanford.edu}, Stephen Boyd:
\texttt{boyd@stanford.edu}.}}
\date{}

\begin{document}
\hyphenation{non-singular}
\maketitle
\thispagestyle{empty}

\begin{abstract}
We define the condition number of a nonsingular matrix on a
subspace, and consider the problem of finding
a subspace of given dimension that minimizes the condition
number of a given matrix.
We give a general solution to this problem, and show in particular
that when the given dimension is less than half the dimension
of the matrix, a subspace can be found on which the condition
number of the matrix is one.
\end{abstract}

\section{The problem}

Suppose $A\in \reals^{n \times n}$ and
$\mathcal V \subseteq \reals^n$ is a subspace with $\dim \mathcal
V = k \geq 1$.
We define the \emph{maximum gain} (\emph{minimum gain})
of $A$ on $\mathcal V$, as
\[
G_\mathrm{max} =
\sup_{x \in \mathcal V,~x \neq 0} \frac{\|Ax\|}{\|x\|}, \qquad
G_\mathrm{min} =
\inf_{x \in \mathcal V,~x \neq 0} \frac{\|Ax\|}{\|x\|},
\]
respectively, where $\|~\|$ denotes the Euclidean norm.
When $A$ is nonsingular, we define its \emph{condition number
on the subspace $\mathcal V$} as
\[
\kappa_{\mathcal V}(A) = G_\mathrm{max}/G_\mathrm{min}.
\]
The condition number of $A$ on any
one-dimensional subspace is $1$, and its condition number
on $\mathcal V=\reals^n$ is the (usual) condition number of $A$,
which we denote $\kappa(A)$.
The condition number on any subspace is between $1$ and $\kappa(A)$.
If $\kappa_{\mathcal V}(A)=1$, we say that $A$ is isotropic
on $\mathcal V$,
since its gain $\|Ax\|/\|x\|$ is the same for any nonzero vector
$x \in \mathcal V$.

In this note we address the following problem:
Given a nonsingular matrix $A \in \reals^{n \times n}$, and
$k \in \{1,\ldots, n\}$, find a subspace $\mathcal V \subseteq \reals^n$
of dimension $k$ which minimizes $\kappa_{\mathcal V}(A)$.
The number $\kappa_{\mathcal V}(A)$ is a measure of the
anisotropy of the linear function induced by $A$, restricted
to the subspace $\mathcal V$, so
our problem is to find a subspace of dimension $k$
on which $A$ is maximally isotropic.

We will show that the minimum possible condition number of $A$,
on a subspace of dimension $k$, is given by
\begin{equation}
\label{sol}
\inf_{\mathcal V\;:\;\mathrm{dim} \mathcal V=k}
\kappa_{\mathcal V} (A) =
\max \left(\frac{\sigma_{n-k+1}}{\sigma_k}, 1 \right)  =
\left\{ \begin{array}{ll}
1 & \quad  k \leq \lceil n/2 \rceil,\\
\sigma_{n-k+1} / \sigma_k & \quad  k > \lceil n/2 \rceil,
\end{array}\right.
\end{equation}
where $\sigma_1 \geq \cdots \geq \sigma_n >0$
are the singular values of $A$.  (The infimum is over all
subspaces of $\reals^n$ of dimension $k$.)
This means, in particular, that for $k \leq \lceil n/2 \rceil$,
we can find a subspace of dimension $k$ on which $A$ is isotropic.

There are many classical results that identify a subspace of a
given dimension that minimizes or maximizes some quantity that
depends on the subspace and matrix.
For example, the Courant-Fischer theorem tells us that the
minimum value of $G_\mathrm{max}$, over all subspaces of
dimension $k$, is $\sigma_{n-k+1}$, and the
maximum value of $G_\mathrm{min}$, over all subspaces of
dimension $k$, is $\sigma_{k}$. For these and similar results,
see, e.g., \cite[\S 4.2]{HoJ:85} or \cite{Ber:05}.
Also, the idea of condition number of a matrix restricted to a
particular subspace can be seen in \cite{ChanF:88}.

We can give a geometric application (or interpretation) of
our problem.
We are given an ellipsoid $\mathcal E = \{ z \;|\; \|Az \|\leq 1\}$
in $\reals^n$, where $A \in \reals^{n \times n}$ is nonsingular.
Our goal is to find a $k$ dimensional subspace $\mathcal V$
so that the ellipsoid $\mathcal V \cap \mathcal E$ is as spherical
as possible, \ie, has minimum eccentricity.
(The eccentricity of $\mathcal V \cap \mathcal E$ is defined as
the ratio of its maximum semi-axis length to its
minimum semi-axis length, which is exactly $\kappa_{\mathcal V}(A)$.)
The solution is to choose $\mathcal V$ that minimizes the condition
number of $A$ on $\mathcal V$.
Our result~(\ref{sol}) can be interpreted in this geometric setting.
For example, if $k< \lceil n/2 \rceil$, we can always find a subspace
of dimension $k$ for which $\mathcal V \cap \mathcal E$ is perfectly
spherical, \ie, a ball.
As a very simple special case, we see that for any ellipsoid in
$\reals^3$, there is a plane that intersects it in a ball.
Our general result~(\ref{sol}) can be considered a generalization of
this simple fact.

\section{The solution}

Suppose $Q$ and $Z$ are $n \times n$ orthogonal matrices,
\ie, $Q^TQ=Z^TZ=I$.  Then we have
\[
\kappa_{\mathcal V} (QAZ) = \kappa_{\mathcal W} (A),
\]
where $\mathcal W = Z \mathcal V = \{ Zv \;|\; v \in \mathcal V \}$.
It follows that
\[
\inf_{\mathcal V\;:\; \mathrm{dim} \mathcal V =k}
\kappa_{\mathcal V} (A) =
\inf_{\mathcal V\;:\;\mathrm{dim} \mathcal V=k}
\kappa_{\mathcal V} (QAZ),
\]
since the first orthogonal matrix $Q$ has no effect,
and the second
orthogonal matrix $Z$ simply changes the parametrization of
subspaces of dimension $k$.

Now let $A=U \Sigma V^T$ be a
singular value decomposition of $A$, \ie,
$U$ and $V$ are orthogonal, and
$\Sigma = \diag (\sigma_1, \ldots, \sigma_n)$.
Our observation above, with $Q=U^T$, $Z=V$, shows that
\[
\inf_{\mathcal V\;:\;\mathrm{dim} \mathcal V =k}
\kappa_{\mathcal V} (A) =
\inf_{\mathcal V\;:\;\mathrm{dim} \mathcal V =k}
\kappa_{\mathcal V} (\Sigma).
\]
So we can just as well solve the problem for the
diagonal matrix $\Sigma$.
(To reconstruct a subspace of dimension $k$ on which $A$
has least condition number, we find a subspace of dimension
$k$ for which $\Sigma$ has least condition number, and multiply
it by $V$.)

Now our problem is to find a subspace $\mathcal{V}$ of
dimension $k$ which minimizes $\kappa_\mathcal{V}(\Sigma)$.
We will show that
\begin{equation}
\label{sol2}
\inf_{\mathcal V\;:\; \mathrm{dim} \mathcal V =k}
\kappa_{\mathcal V} (\Sigma) =
\max \left(\frac{\sigma_{n-k+1}}{\sigma_k}, 1 \right)
= \left\{ \begin{array}{ll}
1 & \quad  k \leq \lceil n/2 \rceil,\\
\sigma_{n-k+1} / \sigma_k & \quad  k > \lceil n/2 \rceil,
\end{array}\right.
\end{equation}

Let $\{e_1, \ldots, e_n\}$ be the standard basis for $\reals^n$,
\ie, for $i=1,\ldots, n$, $e_{ij} = 0$ if $i \neq j$ and $e_{ij}
= 1$ otherwise.

We first give a simple result.
Suppose $i<j$, and let $\sigma$ satisfy
$\sigma_i \geq \sigma \geq \sigma_j$.
Then there is a unit vector $z \in \Span \{e_i,e_j\}$ for
which $\|\Sigma z\|=\sigma$.
This can be seen several ways. For example, we can rotate
a unit vector $z$ from $e_i$ towards $e_j$.  The norm $\|
\Sigma z\|$ varies continuously from $\sigma_i$ to $\sigma_j$,
and therefore has the value $\sigma$ at some rotation angle.
We can easily construct such a $z$.
If $\sigma_i = \sigma_j$, we can take $z = e_i$ or $z = e_j$.
If $\sigma_i > \sigma_j$, we can take
\[
z =  \frac{ (\sigma^2 - \sigma_j^2 )^{1/2} e_i +
(\sigma_i^2 - \sigma^2 )^{1/2} e_j}
{ ( \sigma_i^2 - \sigma_j^2 )^{1/2} }.
\]
It is easily verified that $\|z\| = 1$ and $\| \Sigma z\| =
\sigma$.

\subsection{Case 1: $k \leq \lceil n/2 \rceil$}

To establish~(\ref{sol2}), we will construct a subspace $\mathcal
V^*$ of dimension $k$, with $\kappa_{\mathcal V^*}(\Sigma)=1$.
We will construct an orthonormal basis
$\left\lbrace z_0, z_1, \ldots ,z_{k-1} \right\rbrace $
for $\mathcal V^*$.
We start with $z_0=e_{\lceil n/2 \rceil }$.
Note that $\| \Sigma z_0 \| =\sigma_{\lceil n/2 \rceil }$.

Next, we choose a unit vector
$z_1 \in \Span \{ e_{\left\lceil n/2 \right\rceil -1}, e_{\lceil
(n+1)/2\rceil +1} \}$ that satisfies
$\|\Sigma z_1 \| = \sigma_{\lceil n/2 \rceil }$.
We can do this using our simple result above,
noting that
\[
\sigma_{\left\lceil n/2 \right\rceil-1}\geq
\sigma_{\lceil n/2 \rceil }
\geq \sigma_{\left\lceil (n+1)/2 \right\rceil+1}.
\]
We note that $z_1 \perp z_0$
and $\Sigma z_1 \perp \Sigma z_0$.

We continue the construction, taking $z_2$ as any unit vector
\[
z_2 \in \Span \{ e_{\left\lceil n/2 \right\rceil -2},e_{\lceil
(n+1)/2\rceil +2}\}
\]
that satisfies $\| \Sigma z_2\|=\sigma_{\lceil n/2 \rceil }$.
This continues, until we have unit vectors $z_0, \ldots, z_{k-1}$.
These vectors are mutually orthogonal, since each one is in the span
of two standard basis vectors, and these pairs of standard basis
vectors are disjoint. Since $\Sigma$ is a diagonal matrix,
the vectors $\Sigma z_0, \ldots, \Sigma z_{k-1}$ are
mutually orthogonal.

We now show that $\kappa_{\mathcal V^*}(\Sigma) = 1$.
\iffalse
Let $b$ be any nonzero vector in
$\mathcal V^*$, say,
$b = \beta_0 z_0 + \cdots + \beta_{k-1} z_{k-1}$.
The gain of $A$ in the direction $b$ is
\begin{eqnarray*}
\frac{\|Ab\|}{\|b\|} &=&
\left( \frac{\beta_0^2  \|A z_0\|^2 + \cdots + \beta_{k-1}^2 \|A
z_{k-1} \|^2}
{\beta_0^2 \|z_0\|^2 + \cdots + \beta_{k-1}^2  \|z_{k-1}\|^2}
\right)^{1/2} \\
&=&
\left( \frac{\beta_0^2 \sigma_{\lceil n/2 \rceil }^2 + \cdots +
\beta_{k-1}^2 \sigma_{\lceil n/2 \rceil }^2}
{\beta_0^2+ \cdots + \beta_{k-1}^2 }\right) ^{1/2}\\
&=& \sigma_{\lceil n/2 \rceil }.
\end{eqnarray*}
\fi
For any nonzero vector $b \in \mathcal V^*$, the gain of $\Sigma$
in the direction of $b$,  ${\|\Sigma b\| / \|b\|} =
\sigma_{\lceil n/2 \rceil }$, because the gain of $\Sigma$ in
the direction of any unit vector in the orthonormal basis
$\{z_0, \ldots, z_{k-1} \}$  of $\mathcal V^*$ is $\sigma_{\lceil
n/2 \rceil}$.
Thus $G_\mathrm{max} =G_\mathrm{min} = \sigma_{\lceil
n/2\rceil}$, and therefore ${\kappa_{\mathcal V^*}}(\Sigma) = 1$.

\subsection{Case II: $k > \lceil n/2 \rceil$}
To establish~(\ref{sol2}), we first construct a subspace
$\mathcal V^*$ of dimension $k$, with
$\kappa_{\mathcal V^*}(\Sigma)$ $=\sigma_{n-k+1} / \sigma_k$, and
then show that for any subspace $\mathcal V$ of dimension $k$,
$\kappa_{\mathcal V}(\Sigma) \geq \kappa_{\mathcal V^*}(\Sigma)$.

We will construct an orthonormal basis for $\mathcal V^*$.
We start with the $2k-n$ vectors
$\{ e_{n-k+1}, e_{n-k}, \ldots, e_{k-1}, e_{k} \}$.
We will choose $n-k$ unit vectors, $z_1, \ldots, z_{n-k}$,
such that
\[
\{ z_1, \ldots, z_{n-k}, e_{n-k+1},  \ldots, e_{k-1},e_{k} \}
\]
forms an orthonormal basis for $\mathcal V^*$.
The $n-k$ unit vectors $z_1, \ldots, z_{n-k}$ will be chosen in
$\Span \{ e_{1}, \ldots, e_{n-k},
e_{k+1}, \ldots, e_{n} \}$, and will therefore
be orthogonal to $\{e_{n-k+1},  \ldots, e_{k} \}$.

Choose a unit vector $z_1 \in \Span\{e_1, e_n\}$, satisfying
$\|\Sigma z_1\| = \sigma_{k}$. We can do this using
the simple result given earlier, since
$\sigma_{1} \geq \sigma_{k} \geq \sigma_{n}$.
We note that $z_1 \perp e_j$,
and $\Sigma z_1 \perp  \Sigma e_j$,  $j=n-k+1, \ldots, k$.

We continue the construction, choosing a unit vector $z_2 \in
\Span\{e_2, e_{n-1}\}$,
satisfying $\| \Sigma z_2\| =\sigma_{k}$.
This continues, until we have chosen a unit vector
$z_{n-k}$ in $\Span\{e_{n-k}, e_{k+1}\}$, satisfying
$\|\Sigma z_{n-k}\| =\sigma_{k}$.

The vectors $z_1, \ldots, z_{n-k}$ are mutually orthogonal, since each
one is in the span of two standard basis vectors, and these pairs
of standard basis vectors are disjoint.
Also $z_i \perp e_j$ for $i = 1,\ldots, n-k$ and
$j = n-k+1, \ldots, k$, since each vector $z_i$ is in the
span of two standard basis vectors which are not in the set
$\{e_{n-k+1}, \ldots, e_{k} \}$.
Thus $\{ z_1, \ldots, z_{n-k}$, $e_{n-k+1}, e_{n-k}, \ldots,
e_{k-1},e_{k} \}$ forms an orthonormal basis for $\mathcal V^*$.
Similarly, since $\Sigma$ is a diagonal matrix, the
vectors $\Sigma z_1, \ldots, \Sigma z_{n-k}, \Sigma e_{n-k+1},
\ldots, \Sigma e_{k}$ are mutually orthogonal.

We now show $\kappa_{\mathcal V^*}(\Sigma) =\sigma_{n-k+1} /
\sigma_k$.
Let $b$ any nonzero vector in $\mathcal V^*$, say,
\[
b = \beta_1 z_1 +
\cdots + \beta_{n-k} z_{n-k} + \beta_{n-k+1} e_{n-k+1} + \cdots +
\beta_{k} e_{k}.
\]
The gain of $\Sigma$ in the direction $b$ is
\begin{eqnarray*}
\frac{\|\Sigma b\|}{\|b\|}&=&
\left( \frac
{\sum_{i=1}^{n-k}  \beta_i^2 \|\Sigma z_i\|^2 +
\sum_{j=n-k+1}^{k} \beta_j^2 \|\Sigma e_j\|^2}
{\sum_{i=1}^{n-k} \beta_i^2 \|z_i\|^2 +
\sum_{j=n-k+1}^{k} \beta_j^2\|e_j\|^2}
\right) ^{1/2}\\
&=&
\left( \frac
{\sum_{i=1}^{n-k}  \beta_i^2 \sigma_k^2 +
\sum_{j=n-k+1}^{k} \beta_j^2 \sigma_j^2}
{\sum_{i=1}^{n} \beta_i^2}
\right) ^{1/2},
\end{eqnarray*}
and therefore
$\sigma_{n-k+1} \geq \|\Sigma b\|/\|b\| \geq \sigma_{k}$.
For $b = e_{n-k+1}$, $\|\Sigma b\| / \|b\| = \sigma_{n-k+1} $,
so $G_\mathrm{max} =  \sigma_{n-k+1}$;
for $b = e_{k}$, we have $\| \Sigma b\| / \|b\| = \sigma_{k} $,
so $G_\mathrm{min} = \sigma_{k}$.
It follows that $\kappa_{\mathcal V^*}(\Sigma)
= \sigma_{n-k+1} / \sigma_k$.

Now we will show that for any subspace $\mathcal V$ of dimension
$k$, $\kappa_{\mathcal V} (\Sigma)\geq \sigma_{n-k+1} /
\sigma_k$.
By the Courant-Fischer theorem, for any subspace
$\mathcal{V}$ of dimension $k$, $G_\mathrm{max} \geq
\sigma_{n-k+1}$ and $G_\mathrm{min} \leq \sigma_{k}$.
It follows that
$\kappa_{\mathcal V}(\Sigma) =  G_\mathrm{max}/{G_\mathrm{min}}
\geq \sigma_{n-k+1}/\sigma_k$.
This establishes~(\ref{sol2}), and therefore~(\ref{sol}).

\bibliographystyle{plain}
%\bibliography{min_cond_sub}
\begin{thebibliography}{1}

\bibitem{Ber:05}
D.~S. Bernstein.
\newblock {\em Matrix Mathematics: {T}heory, facts, and formulas,
with
  application to linear systems theory}.
\newblock Princeton University Press, 2005.

\bibitem{ChanF:88}
T.~F. Chan and D.~E. Foulser.
\newblock Effectively well-conditioned linear systems.
\newblock {\em SIAM Journal on Scientific and Statistical
Computing},
  9(6):963--968, November 1988.

\bibitem{HoJ:85}
R.~A. Horn and C.~A. Johnson.
\newblock {\em Matrix Analysis}.
\newblock Cambridge University Press, 1985.

\end{thebibliography}

\end{document}


\section{Topos} \label{topos}
In this subsection we will examine the categorical interpretation of finite sets.
In particular, we will prove that discrete Kuratowski finite types form a
\(\Pi\)-pretopos.
A lot of the work for this proof has been done already: we have already proven
that discrete Kuratowski finiteness is equivalent to cardinal finiteness
(\Cref{cardinal-kuratowski}), meaning that we can work with the latter
definition which is much simpler to prove things about.

There are two reasons we're interested in the categorical and topos-theoretic
interpretation of finite sets: first, it's an important theoretical grounding
for finite sets, which allows us to understand them in the context of other
set-like constructions.
Secondly, and more practically, the language of a topos is (or in our case the
\(\Pi\)-pretopos) is a common standard framework for doing mathematics
generally.
This makes it a good basis for an API for building QuickCheck-like generators,
for example.
\subsection{Categories in HoTT}
At first glance, HoTT seems like a perfect setting for category theory: the
univalence axiom identifies isomorphisms with equality, a useful tool for
category theory missing from MLTT.
While this initial impression is broadly true, the construction of categories in
HoTT is unfortunately quite complex and involved.

Much of this subsection is simply a summary of parts of \citet[section
9]{hottbook}.
The formal proofs we provide are part translation of those proofs in that
section, part from \citep{iversenFredefoxCat2018}
\citep{huProofrelevantCategoryTheory2020}, and part our own.

First, we need to think about the type of objects and arrows.
We cannot, unfortunately, leave them unrestricted: because of the potential for
higher homotopy in HoTT types, we have
to restrict the type of arrows to just the sets.
This notion: that of a category with all the usual laws such that arrows are a
set, is called a \emph{precategory}.
\begin{agdalisting}
  \ExecuteMetaDataInline[agda/Categories.tex]{precategory}
\end{agdalisting}
We will use long arrows to refer to morphisms within a category:
\begin{agdalisting*}
  \ExecuteMetaDataInline[agda/Categories.tex]{morph-arrow}
\end{agdalisting*}

From here, we can define a notion of isomorphisms.
\begin{agdalisting}
  \ExecuteMetaDataInline[agda/Categories.tex]{isomorphism}
\end{agdalisting}
It's a condition on this type which separates the precategories from the
categories: if it satisfies a form of univalence, it the precategory is a full
category.
\begin{agdalisting}
  \ExecuteMetaDataInline[agda/Categories.tex]{cat-univalence}
\end{agdalisting}
\subsection{The Category of Sets}
Next we'll look at how to construct the category of sets (in the HoTT sense).
Much of this work comes directly from \citet{rijkeSetsHomotopyType2015} and
\citet[section 10]{hottbook} (the latter of which is in fact an updated and
slightly less detailed version of the former).
In particular, our treatment (and definition) of categories and topoi comes
directly from those works.
We have provided in the formalisation a proof that sets in CuTT form a
\(\Pi\)-pretopos: this proof (in HoTT) is in fact the main result of
\citet{rijkeSetsHomotopyType2015}; our contribution is simply the formalisation.

The objects are represented by a \(\Sigma\):
\begin{agdalisting*}
  \ExecuteMetaDataInline[agda/Snippets/Category.tex]{hset}
\end{agdalisting*}
This will be quite similar to our objects for finite sets.

Since sets in HoTT don't form a topos, there are quite a few smaller lemmas we
need to prove to get as close as we can (a \(\Pi W\)-pretopos): we won't include
them here, other than the closure proofs in the following subsection.
\subsection{Closure}
The two most involved proofs for showing that discrete Kuratowski sets form a
\(\Pi\)-pretopos are those proofs that show closure under \(\Pi\) and
\(\Sigma\).
We will describe them here.

In \cite[Theorem 4.21]{fruminFiniteSetsHomotopy2018}, Kuratowski finite types
are proven to be closed under surjections, products, and sums.
Here we prove closure under products and sums, but also functions, \(\Sigma\),
and \(\Pi\) (and furthermore our closure proofs are given on all of the
finiteness predicates that they apply to).
\paragraph{Closure of the Ordered Predicates}
First, we will show that split enumerability (and, by extension, manifest
enumerability) are closed under \(\Pi\) and \(\Sigma\).
This is the first stepping stone on our way to prove that cardinal finiteness is
closed under the same.

Practically speaking, these proofs also open up a wide number of other closure
proofs to us.
By proving that dependent products and sums are finite, we get the non-dependent
cases for free.

\begin{lemma} \label{split-enum-sigma}
  Split enumerability is closed under \(\Sigma\).
  \begin{agdalisting*}
    \ExecuteMetaDataInline[agda/Cardinality/Finite/SplitEnumerable.tex]{split-enum-sigma}
  \end{agdalisting*}
\end{lemma}
\begin{proof}
  Our task is to construct the two components of the output pair: the support
  list, and the cover proof.
  We'll start with the support list: this is constructed by taking the Cartesian
  product of the input support lists.
  \begin{agdalisting*}
    \ExecuteMetaDataInline[agda/Cardinality/Finite/SplitEnumerable.tex]{sup-sigma}
  \end{agdalisting*}
  We use do notation here because we're working the list monad: this applies the
  latter function (\(ys\)) to every element of the list \(xs\), and concatenates
  the results.

  To show that this does indeed cover every element of the target type is a
  little intricate, but not necessarily difficult.
\end{proof}

Next we'll look at closure under \(\Pi\).
In MLTT, this is of course not provable: since all of the finiteness predicates
we have seen so far imply decidable equality, and since we don't have any kind
of decidable equality on functions in MLTT, we know that we won't be able to
show that any kind of function is finite; even one like \(\AgdaDatatype{Bool}
\rightarrow \AgdaDatatype{Bool}\).

CuTT is not so restricted.
Since we have things like function extensionality and transport, we can indeed
prove the finiteness of function types.
Our proof here makes use directly of the univalence axiom, and makes use
furthermore of all the previous closure proofs.
\begin{theorem} \label{split-enum-pi}
  Split enumerability is closed under dependent functions
  (\(\Pi\)-types).
  \begin{agdalisting*}
    \ExecuteMetaDataInline[agda/Cardinality/Finite/ManifestBishop.tex]{pi-clos}
  \end{agdalisting*}
\end{theorem}
\begin{proof}
  Let \(A\) be a split enumerable type, and \(U\) be a type family from \(A\),
  which is split enumerable over all points of \(A\).

  As \(A\) is split enumerable, we know that it is also manifestly Bishop finite
  (\Cref{split-enum-to-manifest-bishop}), and consequently we know \(A
  \simeq \AgdaDatatype{Fin}\;n\), for some \(n\) (\Cref{bishop-equiv}).
  We can therefore replace all occurrences of \(A\) with \(\AgdaDatatype{Fin}\;n\),
  changing our goal to:
  \begin{equation*}
    \frac{
      \AgdaDatatype{\ensuremath{\mathcal{E}!}}\;(\AgdaDatatype{Fin}\;n) \; \; \; \left((x : \AgdaDatatype{Fin}\;n) \rightarrow \AgdaDatatype{\ensuremath{\mathcal{E}!}}\;\left( U\;x \right)\right)
    }{
      \AgdaDatatype{\ensuremath{\mathcal{E}!}}\left((x : \AgdaDatatype{Fin}\;n) \rightarrow U\;x\right)
    }
  \end{equation*}

  We then define the type of \(n\)-tuples over some type family.
  \begin{agdalisting*}
    \ExecuteMetaDataInline[agda/Data/Tuple/UniverseMonomorphic.tex]{tuple-def}
  \end{agdalisting*}
  We can show that this type is equivalent to functions (proven in our formalisation):
  \begin{agdalisting*}
    \ExecuteMetaDataInline[agda/Data/Tuple/UniverseMonomorphic.tex]{tuple-iso}
  \end{agdalisting*}
  And therefore we can simplify again our goal to the following:
  \begin{equation*}
    \frac{
      \AgdaDatatype{\ensuremath{\mathcal{E}!}}\;(\AgdaDatatype{Fin}\;n) \; \; \; ((x : \AgdaDatatype{Fin}\;n) \rightarrow \AgdaDatatype{\ensuremath{\mathcal{E}!}}\left( U\;x \right))
    }{
      \AgdaFunction{\ensuremath{\mathcal{E}!}}\;\left(\AgdaFunction{Tuple}\;n\;U\right)
    }
  \end{equation*}

  We can prove this goal by showing that \(\AgdaFunction{Tuple}\;n\;U\) is split
  enumerable: it is made up of finitely many products of points of \(U\), which
  are themselves split enumerable, and \agdatop, which is also split enumerable.
  \Cref{split-enum-sigma} shows us that the product of finitely many split
  enumerable types is itself split enumerable, proving our goal.
\end{proof}
\paragraph{Closure on Cardinal Finiteness}
Since we don't have a function of type \(\agdacal{C}\;A \rightarrow
\AgdaDatatype{\ensuremath{\mathcal{B}}}\;A\), closure proofs on \(\AgdaDatatype{\ensuremath{\mathcal{B}}}\) do not transfer over to
\agdacal{C} trivially (unlike with \(\AgdaDatatype{\ensuremath{\mathcal{E}!}}\) and \(\AgdaDatatype{\ensuremath{\mathcal{B}}}\)).
The cases for \agdabot, \agdatop, and \(\AgdaDatatype{Bool}\) are simple to adapt: we
can just propositionally truncate their Bishop finiteness proof.

Non-dependent operators like \AgdaFunction{\(\times\)},
\AgdaFunction{\(\uplus\)}, and \(\rightarrow\) are also relatively
straightforward: since \(\AgdaDatatype{\ensuremath{\lVert\_\rVert}}\) forms a
monad, we can apply \(n\)-ary functions to values inside it, combining them
together.
\begin{agdalisting*}
  \ExecuteMetaDataInline[agda/Cardinality/Finite/ManifestBishop.tex]{times-clos-sig}
\end{agdalisting*}
Into a truncated context:
\begin{agdalisting*}
  \ExecuteMetaDataInline[agda/Cardinality/Finite/Cardinal.tex]{times-clos-impl}
\end{agdalisting*}

Unfortunately, for the dependent type formers like \(\Sigma\) and \(\Pi\), the
same trick does not work.
We have closure proofs like:
\begin{equation*}
  \frac{
    \AgdaDatatype{\ensuremath{\mathcal{B}}}\;A \; \; \; ((x : A) \rightarrow \AgdaDatatype{\ensuremath{\mathcal{B}}}\;(U\;x))
  }{
    \AgdaDatatype{\ensuremath{\mathcal{B}}}\;((x : A) \rightarrow U\;x)
  }
\end{equation*}
If we apply the monadic truncation trick we can derive closure proofs like the
following:
\begin{equation*}
  \frac{
    \AgdaDatatype{\ensuremath{\lVert}}\; \AgdaDatatype{\ensuremath{\mathcal{B}}}\;A \;\AgdaDatatype{\ensuremath{\rVert}} \; \; \; \AgdaDatatype{\ensuremath{\lVert}}\; ((x : A) \rightarrow \AgdaDatatype{\ensuremath{\mathcal{B}}}\;(U\;x)) \;\AgdaDatatype{\ensuremath{\rVert}}
  }{
    \AgdaDatatype{\ensuremath{\lVert}}\; \AgdaDatatype{\ensuremath{\mathcal{B}}}\;((x : A) \rightarrow U\;x) \;\AgdaDatatype{\ensuremath{\rVert}}
  }
\end{equation*}
However our \emph{desired} closure proof is the following:
\begin{equation*}
  \frac{
    \AgdaDatatype{\ensuremath{\lVert}}\; \AgdaDatatype{\ensuremath{\mathcal{B}}}\;A \;\AgdaDatatype{\ensuremath{\rVert}} \; \; \; ((x : A) \rightarrow \AgdaDatatype{\ensuremath{\lVert}}\; \AgdaDatatype{\ensuremath{\mathcal{B}}}\;(U\;x) \;\AgdaDatatype{\ensuremath{\rVert}})
  }{
    \AgdaDatatype{\ensuremath{\lVert}}\; \AgdaDatatype{\ensuremath{\mathcal{B}}}\;((x : A) \rightarrow U\;x) \;\AgdaDatatype{\ensuremath{\rVert}}
  }
\end{equation*}
They don't match!

The solution would be to find a function of the following type:
\begin{equation*}
  ((x : A) \rightarrow \AgdaDatatype{\ensuremath{\lVert}}\; \AgdaDatatype{\ensuremath{\mathcal{B}}}\;(U\;x) \;\AgdaDatatype{\ensuremath{\rVert}}) \rightarrow
  \AgdaDatatype{\ensuremath{\lVert}}\; (x : A) \rightarrow \AgdaDatatype{\ensuremath{\mathcal{B}}}\;(U\;x) \;\AgdaDatatype{\ensuremath{\rVert}}
\end{equation*}
However we might be disheartened at realising that this is a required goal: the
above equation is \emph{extremely} similar to the axiom of choice!
\begin{definition}[Axiom of Choice] \label{axiom-of-choice}
  In HoTT, the axiom of choice is commonly defined as follows \cite[lemma
  3.8.2]{hottbook}.
  For any set \(A\), and a type family \(U\) which is a set at all the points
  of \(A\), the following function exists:
  \begin{equation*}
    \left( (x : A) \rightarrow  \AgdaDatatype{\ensuremath{\lVert}}\; U(x) \;\AgdaDatatype{\ensuremath{\rVert}} \right) \rightarrow \AgdaDatatype{\ensuremath{\lVert}}\; (x : A) \rightarrow U(x) \;\AgdaDatatype{\ensuremath{\rVert}}
  \end{equation*}
\end{definition}
Luckily the axiom of choice \emph{does} hold for cardinally finite types,
allowing us to prove the following:
\begin{lemma}
  The axiom of choice holds for finite sets.
  \begin{equation*}
    \agdacal{C}\;A \rightarrow ((x : A) \rightarrow \AgdaDatatype{\ensuremath{\lVert}}\; U(x) \;\AgdaDatatype{\ensuremath{\rVert}}) \rightarrow \AgdaDatatype{\ensuremath{\lVert}}\; (x : A) \rightarrow U(x) \;\AgdaDatatype{\ensuremath{\rVert}}
  \end{equation*}
\end{lemma}
\begin{proof}
  Let \(A\) be a cardinally finite type, \(U\) be a type family on \(A\), and
  \(f\) be a dependent function of type \(\Pi(x : A) , \AgdaDatatype{\ensuremath{\lVert}}\; U(x) \;\AgdaDatatype{\ensuremath{\rVert}}\).

  First, since our goal is itself propositionally truncated, we have access to
  values under truncations: put another way, in the context of proving our goal,
  we can rely on the fact that \(A\) is manifestly Bishop finite.
  Using the same technique as we did in \Cref{split-enum-pi}, we can switch
  from working with dependent functions from \(A\) to \(n\)-tuples, where \(n\)
  is the cardinality of \(A\).
  This changes our goal to the following:
  \begin{equation}
    \AgdaFunction{Tuple}\;n\;(\AgdaDatatype{\ensuremath{\lVert\_\rVert}}\;\AgdaFunction{\ensuremath{\circ}}\; U) \rightarrow \AgdaDatatype{\ensuremath{\lVert}}\; \AgdaFunction{Tuple}\;n\;U\;\AgdaDatatype{\ensuremath{\rVert}}
  \end{equation}
  Since \(\AgdaDatatype{\ensuremath{\lVert\_ \rVert}}\) is closed under finite products, this function
  exists (in fact, using the fact that \(\AgdaDatatype{\ensuremath{\lVert\_ \rVert}}\) forms a monad, we
  can recognise this function as \verb+sequenceA+ from the \verb+Traversable+
  class in Haskell).
\end{proof}
This lemma is a well-known folklore theorem.

This gets us all of the necessary closure proofs on \agdacal{C}, and as a result
we know the following theorem.
\begin{theorem} \label{kuratowski-topos}
  Decidable Kuratowski finite sets form a \(\Pi\)-pretopos.
\end{theorem}
\subsection{The Absence of the Subobject Classifier}
It's a little unsatisfying that our topos construction has so many caveats: we
have to prove a lot of small, uninteresting lemmas just to get to a
\(\Pi\)-pretopos, all because we can't prove the one or two larger, simple
lemmas which would show that sets form a topos.
So what exactly are we missing?

Well, one of the characteristic features of topos theory is that there are a
wide variety of equivalent ways to show that something is a topos (a natural
consequence of their being a wide variety of things which qualify as toposes).
For the direction we have been going, though, the big missing feature is the
\emph{subobject classifier}.

A subobject in this context refers to a subset.
In set theory, we can often describe a subset of some set \(A\) with the
following notation:
\begin{equation*}
  \left\{ x \;\vert\; x \in A ; P(x) \right\}
\end{equation*}
This is the subset of elements in \(A\) which satisfy some predicate \(P\).

Type theoretically, the way to express the same would be
\(\AgdaDatatype{\(\Sigma\)}\;A\;P\): if we wanted to describe the subset of \Nat
smaller than 10 we would write
\mbox{\(\AgdaDatatype{\(\Sigma\)[}\;\AgdaBound{n}\;\AgdaDatatype{:}\;\Nat\;\AgdaDatatype{]}\;\AgdaBound{n}\;\AgdaFunction{\(<\)}\;\AgdaNumber{10}\)}.
In general, however, this type holds too many elements to properly classify the
subsets of the larger set: there may be more than one inhabitant of \(P\;x\) for
any given \(x\).
For \emph{propositions}, however, (i.e. where \(P\) is a proposition),
\AgdaDatatype{\(\Sigma\)} represents a perfectly valid encoding of subsets.

The subobject classifier is an object within the topos (which must be a
contraction) which classifies monomorphisms (injections).
We can actually show that the ``subset'' notion we just defined does in fact
classify monomorphisms in sets in HoTT (in fact directly through univalence),
but at this point we run into our one and only size problem in this paper.
The actual object corresponding to the subobject classifier is the following:
\begin{agdalisting*}
  \ExecuteMetaDataInline[agda/Snippets/Topos.tex]{prop-univ}
\end{agdalisting*}
The problem here, crucially, is that the universe level of this type is one
higher than the universe level of the types it bounds.
In other words, this is \emph{not} an object in our \(\Pi\)-pretopos of sets,
where the types are all of universe level 0.

Remember that the purpose of universe levels was to prevent Girard's paradox.
However, there is an axiom which removes universe levels to a certain extent
which does \emph{not} imply the paradox: propositional resizing.
\begin{definition}[Propositional Resizing]
  The axiom of propositional resizing states that the following two types, for
  any universe level \(u\), are equivalent:
  \begin{agdalisting*}
    \ExecuteMetaDataInline[agda/Snippets/Topos.tex]{prop-resize}
  \end{agdalisting*}
\end{definition}
If propositional resizing holds, then we \emph{can} in fact construct a
subobject classifier, for both sets and finite sets. \todo{Mention lem?}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "../paper"
%%% End:


\par
\section{Data Structure}
\label{section:SubMtx:dataStructure}
\par
\par
The {\tt SubMtx} structure has the following fields.
\begin{itemize}
\item
{\tt int type} : type of entries.
\begin{itemize}
\item {\tt SPOOLES\_REAL} : double precision real entries.
\item {\tt SPOOLES\_COMPLEX} : double precision complex entries.
\end{itemize}
\item
{\tt int mode} : storage mode.
\begin{itemize}
\item {\tt SUBMTX\_DENSE\_ROWS} : dense, storage by rows.
\item {\tt SUBMTX\_DENSE\_COLUMNS} : dense, storage by columns.
\item {\tt SUBMTX\_SPARSE\_ROWS} : sparse, storage by rows.
\item {\tt SUBMTX\_SPARSE\_COLUMNS} : sparse, storage by columns.
\item {\tt SUBMTX\_SPARSE\_TRIPLES} : sparse, storage by
      $(i,j,a_{i,j})$ triples.
\item {\tt SUBMTX\_DENSE\_SUBROWS} : sparse, storage by dense subrows.
\item {\tt SUBMTX\_DENSE\_SUBCOLUMNS} :
      sparse, storage by dense subcolumns.
\item {\tt SUBMTX\_DIAGONAL} : a diagonal matrix.
\item {\tt SUBMTX\_BLOCK\_DIAGONAL\_SYM} : a symmetric block
      diagonal matrix with $1 \times 1$ and $2 \times 2$ blocks.
\item {\tt SUBMTX\_BLOCK\_DIAGONAL\_HERM} : a hermitian block
      diagonal matrix with $1 \times 1$ and $2 \times 2$ blocks.
\end{itemize}
\item
{\tt int rowid} : object's row id, default value is {\tt -1}.
\item
{\tt int colid} : object's column id, default value is {\tt -1}.
\item
{\tt int nrow} : number of rows
\item
{\tt int ncol} : number of columns
\item
{\tt int nent} : number of stored matrix entries.
\item
{\tt DV wrkDV} :
object that manages the owned working storage.
\item
{\tt SubMtx *next} :
link to a next object in a singly linked list.
\end{itemize}
\par
One can query the type of the object using these simple macros.
\begin{itemize}
\item
{\tt SUBMTX\_IS\_REAL(mtx)} is {\tt 1} if {\tt mtx}
has real entries and {\tt 0} otherwise.
\item
{\tt SUBMTX\_IS\_COMPLEX(mtx)} is {\tt 1} if {\tt mtx}
has complex entries and {\tt 0} otherwise.
\item
{\tt SUBMTX\_IS\_DENSE\_ROWS(mtx)} is {\tt 1} if {\tt mtx}
has dense rows as its storage format,
and {\tt 0} otherwise.
\item
{\tt SUBMTX\_IS\_DENSE\_COLUMNS(mtx)} is {\tt 1} if {\tt mtx}
has dense columns as its storage format,
and {\tt 0} otherwise.
\item
{\tt SUBMTX\_IS\_SPARSE\_ROWS(mtx)} is {\tt 1} if {\tt mtx}
has sparse rows as its storage format,
and {\tt 0} otherwise.
\item
{\tt SUBMTX\_IS\_SPARSE\_COLUMNS(mtx)} is {\tt 1} if {\tt mtx}
has sparse columns as its storage format,
and {\tt 0} otherwise.
\item
{\tt SUBMTX\_IS\_SPARSE\_TRIPLES(mtx)} is {\tt 1} if {\tt mtx}
has sparse triples as its storage format,
{\tt 0} otherwise.
\item
{\tt SUBMTX\_IS\_DENSE\_SUBROWS(mtx)} is {\tt 1} if {\tt mtx}
has dense subrows as its storage format,
{\tt 0} otherwise.
\item
{\tt SUBMTX\_IS\_DENSE\_SUBCOLUMNS(mtx)} is {\tt 1} if {\tt mtx}
has dense subcolumns as its storage format,
{\tt 0} otherwise.
\item
{\tt SUBMTX\_IS\_DIAGONAL(mtx)} is {\tt 1} if {\tt mtx} is diagonal,
{\tt 0} otherwise.
\item
{\tt SUBMTX\_IS\_BLOCK\_DIAGONAL\_SYM(mtx)} is {\tt 1} if {\tt mtx}
is block diagonal and symmetric,
{\tt 0} otherwise.
\item
{\tt SUBMTX\_IS\_BLOCK\_DIAGONAL\_HERM(mtx)} is {\tt 1} if {\tt mtx}
is block diagonal and hermitian,
{\tt 0} otherwise.
\end{itemize}