\chapter{Affine varieties}
In this chapter we introduce affine varieties.
We introduce them in the context of coordinates,
but over the course of the other chapters
we'll gradually move away from this perspective to
viewing varieties as ``intrinsic objects'',
rather than embedded in coordinates.

For simplicity, we'll do almost everything over the field of complex numbers,
but the discussion generalizes to any algebraically closed field.

\section{Affine varieties}
\prototype{$\VV(y-x^2)$ is a parabola in $\Aff^2$.}
%An \vocab{affine variety} is just the zero locus of a set of polynomials.
%We think of it as living in the $n$-dimensional space $\Aff^n$.

\begin{definition}
	Given a set of polynomials $S \subseteq \CC[x_1, \dots, x_n]$
	(not necessarily finite or even countable),
	we let $\VV(S)$ denote the set of points vanishing on \emph{all}
	the polynomials in $S$.
	Such a set is called an \vocab{affine variety}.
	It lives in \vocab{$n$-dimensional affine space}, denoted $\Aff^n$
	(to distinguish it from projective space later).
\end{definition}
For example, a parabola is the zero locus of the polynomial $\VV(y-x^2)$. Picture:
\begin{center}
	\begin{asy}
		import graph;
		size(5cm);

		real f(real x) { return x*x; }
		graph.xaxis("$x$");
		graph.yaxis("$y$");
		draw(graph(f,-2,2,operator ..), blue, Arrows);
		label("$\mathcal V(y-x^2)$", (0.8, f(0.8)), dir(-45), blue);
		label("$\mathbb A^2$", (2,3), dir(45));
	\end{asy}
\end{center}

\begin{example}[Examples of affine varieties]
	These examples are in two-dimensional space $\Aff^2$,
	whose points are pairs $(x,y)$.
	\begin{enumerate}[(a)]
	\ii A straight line can be thought of as $\VV(Ax + By + C)$.
	\ii A parabola as above can be pictured as $\VV(y-x^2)$.
	\ii A hyperbola might be the zero locus of the polynomial $\VV(xy-1)$.
	\ii The two axes can be thought of as $\VV(xy)$; this is the set of points
	such that $x=0$ \emph{or} $y=0$.
	\ii A point $(x_0, y_0)$ can be thought of as $\VV(x-x_0, y-y_0)$.
	\ii The entire space $\Aff^2$ can be thought of as $\VV(0)$.
	\ii The empty set is the zero locus of the constant polynomial $1$, that is $\VV(1)$.
	\end{enumerate}
\end{example}

\section{Naming affine varieties via ideals}
\prototype{$\VV(I)$ is a parabola, where $I=(y-x^2)$.}
As you might have already noticed, a variety can be named by $\VV(-)$ in multiple ways.
For example, the set of solutions to
\[ x=3 \text{ and } y=4 \]
is just the point $(3,4)$.
But this is also the set of solutions to
\[ x=3 \text{ and } y=x+1. \]
So, for example
\[ \{(3,4)\}
	= \VV(x-3, y-4)
	= \VV(x-3, y-x-1).
	\]
That's a little annoying, because in an ideal\footnote{Pun not intended
	but left for amusement value.}
world we would have \emph{one} name
for every variety.
Let's see if we can achieve this.

A partial solution is to use \emph{ideals} rather than small sets.
That is, consider the ideal
\[
	I = \left( x-3, y-4 \right)
	= \left\{ p(x,y) \cdot (x-3) + q(x,y) \cdot (y-4)
	\mid p,q \in \CC[x,y] \right\}
\]
and look at $\VV(I)$.
\begin{ques}
	Convince yourself that $\VV(I) = \{(3,4)\}$.
\end{ques}
So rather than writing $\VV(x-3, y-4)$ it makes sense to
think about this as $\VV\left( I \right)$, where $I = (x-3,y-4)$ is the \emph{ideal}
generated by the two polynomials $x-3$ and $y-4$.
This is an improvement because
\begin{ques}
	Check that $(x-3, y-x-1) = (x-3, y-4)$.
\end{ques}

Needless to say, this pattern holds in general.
\begin{ques}
	Let $\{f_i\}$ be a set of polynomials, and consider
	the ideal $I$ generated by these $\{f_i\}$.
	Show that $\VV(\{f_i\}) = \VV(I)$.
\end{ques}

Thus we will only consider $\VV(I)$ when $I$ is an ideal.
Of course, frequently our ideals are generated by one or two polynomials,
which leads to:
\begin{abuse}
	Given a set of polynomials $f_1, \dots, f_m$
	we let $\VV(f_1, \dots, f_m)$ be shorthand for
	$\VV\left( \left( f_1, \dots, f_m \right) \right)$.
	In other words we let $\VV(f_1, \dots, f_m)$
	abbreviate $\VV(I)$, where $I$ is the \emph{ideal} $I=(f_1, \dots, f_m)$.
\end{abuse}

This is where the Noetherian condition really shines:
it guarantees that every ideal $I \subseteq \CC[x_1, \dots, x_n]$
can be written in the form above with \emph{finitely} many polynomials,
because it is \emph{finitely generated}.
(The fact that $\CC[x_1, \dots, x_n]$ is Noetherian follows from the Hilbert basis theorem,
which is \Cref{thm:hilbert_basis}).
This is a relief, because dealing with infinite sets of polynomials is not much fun.

\section{Radical ideals and Hilbert's Nullstellensatz}
\prototype{$\sqrt{(x^2)} = (x)$ in $\CC[x]$, $\sqrt{(12)} = (6)$ in $\ZZ$.}
You might ask whether the name is unique now:
that is, if $\VV(I) = \VV(J)$, does it follow that $I=J$?
The answer is unfortunately no: the counterexample can be found in just $\Aff^1$.
It is
\[ \VV(x) = \VV(x^2). \]
In other words, the set of solutions to $x=0$
is the same as the set of solutions to $x^2=0$.

Well, that's stupid.
We want an operation which takes the ideal $(x^2)$ and makes it into the ideal $(x)$.
The way to do so is using the radical of an ideal.

\begin{definition}
	Let $R$ be a ring.
	The \vocab{radical} of an ideal $I \subseteq R$, denoted $\sqrt I$,
	is defined by
	\[ \sqrt I = \left\{ r \in R
			\mid r^m \in I \text{ for some integer $m \ge 1$} \right\}. \]
	If $I = \sqrt I$, we say the ideal $I$ itself is \vocab{radical}.
\end{definition}
For example, $\sqrt{(x^2)} = (x)$.
You may like to take the time to verify that $\sqrt I$ is actually an ideal.

\begin{remark}
	[Number theoretic motivation]
	This is actually the same as the notion of ``radical'' in number theory.
	In $\ZZ$, the radical of an ideal $(n)$ corresponds to just
	removing all the duplicate prime factors, so for example
	\[ \sqrt{(12)} = (6). \]
	In particular, if you try to take $\sqrt{(6)}$,
	you just get $(6)$ back;
	you don't squeeze out any new prime factors.

	This is actually true more generally,
	and there is a nice corresponding alternate definition:
	for any ideal $I$, we have
	\[ \sqrt I = \bigcap_{I \subseteq \kp \text{ prime}} \kp. \]
	Although we could prove this now,
	it will be proved later in \Cref{thm:radical_intersect_prime},
	when we first need it.
\end{remark}

Here are the immediate properties you should know.
\begin{proposition}
	[Properties of radical]
	\label{prop:radical}
	In any ring:
	\begin{itemize}
		\ii If $I$ is an ideal, then $\sqrt I$ is always a radical ideal.
		\ii Prime ideals are radical.
		\ii For $I \subseteq \CC[x_1, \dots, x_n]$
		we have $\VV(I) = \VV(\sqrt I)$.
	\end{itemize}
\end{proposition}
\begin{proof}
	These are all obvious.
	\begin{itemize}
		\ii If $f^m \in \sqrt I$ then $f^{mn} \in I$, so $f \in \sqrt I$.
		\ii If $f^n \in \kp$ for a prime $\kp$,
		then either $f \in \kp$ or $f^{n-1} \in \kp$,
		and in the latter case we may continue by induction.
		\ii We have $f(x_1, \dots, x_n) = 0$
		if and only if $f(x_1, \dots, x_n)^m = 0$ for some integer $m$.
		\qedhere
	\end{itemize}
\end{proof}

The last bit makes sense: you would never refer to $x=0$ as $x^2=0$,
and hence we would always want to call $\VV(x^2)$ just $\VV(x)$.
With this, we obtain a theorem called Hilbert's Nullstellensatz.
\begin{theorem}[Hilbert's Nullstellensatz]
	\label{thm:hilbert_null}
	Given an affine variety $V = \VV(I)$,
	the set of polynomials which vanish
	on all points of $V$ is precisely $\sqrt I$.
	Thus if $I$ and $J$ are ideals in $\CC[x_1, \dots, x_n]$, then
	\[ \VV(I) = \VV(J) \text{ if and only if $\sqrt I = \sqrt J$}. \]
\end{theorem}
In other words
\begin{moral}
	Radical ideals in $\CC[x_1, \dots, x_n]$ correspond
	exactly to $n$-dimensional affine varieties.
\end{moral}
The proof of Hilbert's Nullstellensatz will be given in
\Cref{prob:hilbert_from_weak}; for now it is worth remarking that
it relies essentially on the fact that $\CC$ is
\emph{algebraically closed}.
For example, it is false in $\RR[x]$,
with $(x^2+1)$ being a maximal ideal with empty vanishing set.

\section{Pictures of varieties in $\Aff^1$}
\prototype{Finite sets of points (in fact these are the only nontrivial examples).}
Let's first draw some pictures.
In what follows I'll draw $\CC$ as a straight line\dots sorry.

First of all, let's look at just the complex line $\Aff^1$.
What are the various varieties on it?
For starters, we have a single point $9 \in \CC$,
generated by $(x-9)$.

\begin{center}
	\begin{asy}
		size(6cm);
		pair A = (-9,0); pair B = (9,0);
		draw(A--B, Arrows);
		label("$\mathcal V(x-9)$", (0,0), 2*dir(-90), blue);
		dot("$9$", (3,0), dir(90), blue);
		label("$\mathbb A^1$", A+(2,0), dir(90));
	\end{asy}
\end{center}

Another example is the point $4$.
And in fact, if we like we can get an ideal consisting of just these two points;
consider $\VV\left( (x-4)(x-9) \right)$.

\begin{center}
	\begin{asy}
		size(6cm);
		pair A = (-9,0); pair B = (9,0);
		draw(A--B, Arrows);
		label("$\mathcal V( (x-4)(x-9) )$", (0,0), 2*dir(-90), blue);
		dot("$4$", (-1,0), dir(90), blue);
		dot("$9$", (3,0), dir(90), blue);
		label("$\mathbb A^1$", A+(2,0), dir(90));
	\end{asy}
\end{center}

In general, in $\Aff^1$ you can get finitely
many points $\left\{ a_1, \dots, a_n \right\}$ by
just taking \[ \VV\left( (x-a_1)(x-a_2)\dots(x-a_n) \right). \]
On the other hand, you can't get the set $\{0,1,2,\dots\}$ as an affine variety;
the only polynomial vanishing
on all those points is the zero polynomial.
In fact, you can convince yourself that these
are the only affine varieties, with two exceptions:
\begin{itemize}
	\ii The entire line $\Aff^1$ is given by $\VV(0)$, and
	\ii The empty set is given by $\VV(1)$.
\end{itemize}
\begin{exercise}
	Show that these are the only varieties of $\Aff^1$.
	(Let $\VV(I)$ be the variety and pick a $0 \neq f \in I$.)
\end{exercise}

As you might correctly guess, we have:
\begin{theorem}[Intersections and unions of varieties]
	\label{thm:many_aff_variety}
	\listhack
	\begin{enumerate}[(a)]
		\ii The intersection of affine varieties
		(even infinitely many) is an affine variety.
		\ii The union of finitely many affine varieties
		is an affine variety.
	\end{enumerate}
	In fact we have
	\[ \bigcap_\alpha \VV(I_\alpha)
		= \VV\left( \sum_\alpha I_\alpha \right)
		\qquad\text{and}\qquad
		\bigcup_{k=1}^n \VV(I_k)
		= \VV\left( \bigcap_{k=1}^n I_k \right). \]
\end{theorem}
You are welcome to prove this easy result yourself.
\begin{remark}
	Part (a) is a little misleading in that the sum $I+J$ need not be radical:
	take for example $I = (y-x^2)$ and $J = (y)$ in $\CC[x,y]$,
	where $x \in \sqrt{I+J}$ and $x \notin I+J$.
	But in part (b) for radical ideals $I$ and $J$,
	the intersection $I \cap J$ is radical.
\end{remark}

\section{Prime ideals correspond to irreducible affine varieties}
\prototype{$(xy)$ corresponds to the union of two lines in $\Aff^2$.}

Note that most of the affine varieties of $\Aff^1$, like $\{4,9\}$,
are just unions of the simplest ``one-point'' ideals.
To ease our classification,
we can restrict our attention to the case of \emph{irreducible} varieties:
\begin{definition}
	A variety $V$ is \vocab{irreducible} if it cannot be written
	as the union of two proper sub-varieties $V = V_1 \cup V_2$.
\end{definition}
\begin{abuse}
	Warning: in other literature,
	irreducible is part of the definition of variety.
\end{abuse}

\begin{example}
	[Irreducible varieties of $\Aff^1$]
	The irreducible varieties of $\Aff^1$ are:
	\begin{itemize}
		\ii the empty set $\VV(1)$,
		\ii a single point $\VV(x-a)$, and
		\ii the entire line $\Aff^1 = \VV(0)$.
	\end{itemize}
\end{example}
\begin{example}
	[The union of two axes]
	Let's take a non-prime ideal in $\CC[x,y]$, such as $I = (xy)$.
	Its vanishing set $\VV(I)$ is the union of two lines $x=0$ and $y=0$.
	So $\VV(I)$ is reducible.
\end{example}

%We have already seen that the radical ideals
%are in one-to-one correspondence with affine varieties.
%In the next sections we answer the two questions:
%\begin{itemize}
%	\ii What property of $\VV(I)$ corresponds to $I$ being prime?
%	\ii What property of $\VV(I)$ corresponds to $I$ being maximal?
%\end{itemize}
%The first question is easier to answer.

In general:
\begin{theorem}[Prime $\iff$ irreducible]
	Let $I$ be a radical ideal, and $V = \VV(I)$ a nonempty variety.
	Then $I$ is prime if and only if $V$ is irreducible.
\end{theorem}
\begin{proof}
	First, assume $V$ is irreducible; we'll show $I$ is prime.
	Let $f,g \in \CC[x_1, \dots, x_n]$ so that $fg \in I$.
	Then $V$ is a subset of the union $\VV(f) \cup \VV(g)$;
	actually, $V = \left( V \cap \VV(f) \right) \cup \left( V \cap \VV(g) \right)$.
	Since $V$ is irreducible, we may assume $V = V \cap \VV(f)$,
	hence $f$ vanishes on all of $V$. So $f \in I$.

	The reverse direction is similar.
\end{proof}

%\begin{remark}
%	The above proof illustrates the following principle:
%	Let $V$ be an irreducible variety.
%	Suppose that $V \subseteq V_1 \cup V_2$;
%	this implies $V = (V_1 \cap V) \cup (V_2 \cap V)$.
%	Recall that the intersection of two varieties is a variety.
%	Thus an irreducible variety can't even be \emph{contained}
%	in a nontrivial union of two varieties.
%\end{remark}

\section{Pictures in $\Aff^2$ and $\Aff^3$}
\prototype{Various curves and hypersurfaces.}

With this notion, we can now draw pictures in
``complex affine plane'', $\Aff^2$.
What are the irreducible affine varieties in it?

As we saw in the previous discussion,
naming irreducible affine varieties in $\Aff^2$
amounts to naming the prime ideals of $\CC[x,y]$.
Here are a few.
\begin{itemize}
	\ii The ideal $(0)$ is prime. $\VV(0)$ as usual corresponds to the entire plane.
	\ii The ideal $(x-a, y-b)$ is prime,
	since $\CC[x,y] / (x-a, y-b) \cong \CC$ is an integral domain.
	(In fact, since $\CC$ is a field, the ideal $(x-a,y-b)$ is \emph{maximal}).
	The vanishing set of this is $\VV(x-a, y-b) = \{ (a,b) \} \in \CC^2$,
	so these ideals correspond to a single point.
	\ii Let $f(x,y)$ be an irreducible polynomial, like $y-x^2$.
	Then $(f)$ is a prime ideal! Here $\VV(I)$ is a ``degree one curve''.
\end{itemize}

By using some polynomial algebra
(again you're welcome to check this; Euclidean algorithm),
these are in fact the only prime ideals of $\CC[x,y]$.
Here's a picture.

\begin{center}
	\begin{asy}
		import graph;
		graph.xaxis("$x$", -4, 4);
		graph.yaxis("$y$", -4, 4);

		real f (real x) { return x*x; }
		draw(graph(f,-2,2,operator ..), blue);
		label("$\mathcal V(y-x^2)$", (1,1), dir(-45), blue);
		dot("$\mathcal V(x-1,y+2)$", (1,-2), dir(-45), red);
	\end{asy}
\end{center}

As usual, you can make varieties which are just unions of these irreducible ones.
For example, if you wanted the variety consisting of a parabola $y=x^2$
plus the point $(20,15)$ you would write
\[ \VV \left( (y-x^2)(x-20), (y-x^2)(y-15) \right). \]

The picture in $\Aff^3$ is harder to describe.
Again, you have points $\VV(x-a, y-b, z-c)$ corresponding to
be zero-dimensional points $(a,b,c)$, and two-dimensional surfaces
$\VV(f)$ for each irreducible polynomial $f$ (for example, $x+y+z=0$ is a plane).
But there are more prime ideals, like $\VV(x,y)$, which corresponds to the
intersection of the planes $x=0$ and $y=0$: this is the one-dimensional $z$-axis.
It turns out there is no reasonable way to classify the ``one-dimensional'' varieties;
they correspond to ``irreducible curves''.

Thus, as Ravi Vakil \cite{ref:vakil} says:
the purely algebraic question
of determining the prime ideals of $\CC[x,y,z]$
has a fundamentally geometric answer.

\section{Maximal ideals}
\prototype{All maximal ideals are $(x_1-a_1, \dots, x_n-a_n)$.}
We begin by noting:
\begin{proposition}
	[$\VV(-)$ is inclusion reversing]
	If $I \subseteq J$ then $\VV(I) \supseteq \VV(J)$.
	Thus $\VV(-)$ is \emph{inclusion-reversing}.
\end{proposition}
\begin{ques}
	Verify this.
\end{ques}
Thus, bigger ideals correspond to smaller varieties.
As the above pictures might have indicated,
the smallest varieties are \emph{single points}.
Moreover, as you might guess from the name,
the biggest ideals are the \emph{maximal ideals}.
As an example, all ideals of the form
\[ \left( x_1-a_1, \dots, x_n-a_n \right) \]
are maximal, since the quotient
\[ \CC[x_1, \dots, x_n] / \left( x_1-a_1, \dots, x_n-a_n \right) \cong \CC \]
is a field.
The question is: are all maximal ideals of this form?

The answer is in the affirmative.
%It's equivalent to:
%\begin{theorem}
%	[Weak Nullstellensatz, phrased as nonempty varieties]
%	Let $I \subsetneq \CC[x_1, \dots, x_n]$ be a proper ideal.
%	Then the variety $\VV(I) \neq \varnothing$.
%\end{theorem}
% From this we can deduce that all maximal ideals are of the above form.
\begin{theorem}
	[Weak Nullstellensatz, phrased with maximal ideals]
	Every maximal ideal of $\CC[x_1, \dots, x_n]$
	is of the form $(x_1-a_1, \dots, x_n-a_n)$.
\end{theorem}
The proof of this is surprisingly nontrivial,
so we won't include it here yet; see \cite[\S7.4.3]{ref:vakil}.
%% TODO we might include this eventually
%\begin{proof}
%	[WN implies MI]
%	Let $J$ be a maximal ideal, and consider the corresponding variety $V = \VV(J)$.
%	By WN, it contains some point $p=(a_1, \dots, a_n)$.
%	Now, define $I = (x_1-a_1, \dots, x_n-a_n)$; this ideal contains all polynomials
%	vanishing at $p$, so necessarily $J \subseteq I \subsetneq \CC[x_1, \dots, x_n]$.
%	Then by maximality of $J$ we have $J=I$.
%\end{proof}
Again this uses the fact that $\CC$ is algebraically closed.
(For example $(x^2+1)$ is a maximal ideal of $\RR[x]$.)
Thus:
\begin{moral}
	Over $\CC$, maximal ideals correspond to single points.
\end{moral}

Consequently, our various ideals over $\CC$ correspond to various flavors
of affine varieties:
\begin{center}
	\begin{tabular}[h]{|cc|}
		\hline
		Algebraic flavor & Geometric flavor \\ \hline
		radical ideal & affine variety \\
		prime ideal & irreducible variety \\
		maximal ideal & single point \\
		any ideal & (scheme?) \\ \hline
	\end{tabular}
\end{center}
There's one thing I haven't talked about: what's the last entry?

\section{Motivating schemes with non-radical ideals}
One of the most elementary motivations for the scheme
is that we would like to use them to count multiplicity.
That is, consider the intersection
\[ \VV(y-x^2) \cap \VV(y) \subseteq \Aff^2 \]
This is the intersection of the parabola with the tangent $x$-axis,
this is the green dot below.

\begin{center}
	\begin{asy}
		import graph;
		size(5cm);

		real f(real x) { return x*x; }
		graph.xaxis("$x$", red);
		graph.yaxis("$y$");
		draw(graph(f,-2,2,operator ..), blue, Arrows);
		label("$\mathcal V(y-x^2)$", (0.8, f(0.8)), dir(-45), blue);
		label("$\mathbb A^2$", (2,3), dir(45));
		dotfactor *= 1.5;
		dot(origin, heavygreen);
	\end{asy}
\end{center}

Unfortunately, as a variety, it is just a single point!
However, we want to think of this as a ``double point'':
after all, in some sense it has multiplicity $2$.
You can detect this when you look at the ideals:
\[ (y-x^2) + (y) = (x^2,y) \]
and thus, if we blithely ignore taking the radical, we get
\[ \CC[x,y] / (x^2,y) \cong \CC[\eps] / (\eps^2). \]
So the ideals in question are noticing the presence of a double point.

In order to encapsulate this, we need a more refined object than
a variety, which (at the end of the day) is just a set of points;
it's not possible using topology along to encode more information
(there is only one topology on a single point!).
This refined object is the \emph{scheme}.

\section\problemhead
\todo{some actual computation here would be good}

\begin{problem}
	Show that a \emph{real} affine variety $V \subseteq \Aff_\RR^n$
	can always be written in the form $\VV(f)$.
	\begin{hint}
		Squares are nonnegative.
	\end{hint}
	\begin{sol}
		If $V = \VV(I)$ with $I = (f_1, \dots, f_m)$
		(as usual there are finitely many polynomials since $\RR[x_1, \dots, x_n]$ is Noetherian)
		then we can take $f = f_1^2 + \dots + f_m^2$.
	\end{sol}
\end{problem}

\begin{problem}
	[Complex varieties can't be empty]
	\label{prob:complex_variety_nonempty}
	Prove that if $I$ is a proper ideal in $\CC[x_1, \dots, x_n]$
	then $\VV(I) \ne \varnothing$.
	\begin{hint}
		This is actually an equivalent formulation
		of the Weak Nullstellensatz.
	\end{hint}
	\begin{sol}
		Let $I$ be an ideal, and let $\km$ be a maximal ideal contained in it.
		(If you are worried about the existence of $\km$,
		it follows from Krull's Theorem, \Cref{prob:krull_max_ideal}).
		Then $\km = (x_1 - a_1, \dots, x_n - a_n)$ by Weak Nullstellensatz.
		Consequently, $(a_1, \dots, a_n)$ is the unique point of $\VV(\km)$,
		and hence this point is also in $\VV(I)$.
	\end{sol}
\end{problem}

\begin{problem}
	\yod
	\label{prob:hilbert_from_weak}
	Show that Hilbert's Nullstellensatz in $n$ dimensions
	follows from the Weak Nullstellensatz.
	(This solution is called the \vocab{Rabinowitsch Trick}.)
	\begin{hint}
		Use the weak Nullstellensatz on $n+1$ dimensions.
		Given $f$ vanishing on everything,
		consider $x_{n+1}f-1$.
	\end{hint}
	\begin{sol}
		The point is is to check that if $f$ vanishes on all of $\VV(I)$,
		then $f \in \sqrt I$.

		Take a set of generators $f_1, \dots, f_m$,
		in the original ring $\CC[x_1, \dots, x_n]$;
		we may assume it's finite by the Hilbert basis theorem.

		We're going to do a trick now:
		consider $S = \CC[x_1, \dots, x_n, x_{n+1}]$ instead.
		Consider the ideal $I' \subseteq S$ in the bigger ring
		generated by $\{f_1, \dots, f_m\}$ and the polynomial $x_{n+1} f - 1$.
		The point of the last guy is that its zero locus
		does not touch our copy $x_{n+1}=0$ of $\Aff^n$
		nor any point in the ``projection'' of $f$ through $\Aff^{n+1}$
		(one can think of this as $\VV(I)$ in the smaller ring
		direct multiplied with $\CC$).
		Thus $\VV(I') = \varnothing$, and by the weak Nullstellensatz
		we in fact have $I' = \CC[x_1, \dots, x_{n+1}]$.
		So
		\[ 1 = g_1f_1 + \dots + g_mf_m + g_{m+1} \left( x_{n+1}f-1 \right). \]
		Now the hack: \textbf{replace every instance of $x_{n+1}$ by $\frac 1f$},
		and then clear all denominators.
		Thus for some large enough integer $N$ we can get
		\[ f^N = f^N(g_1f_1 + \dots + g_mf_m) \]
		which eliminates any fractional powers of $f$ in the right-hand side.
		It follows that $f^N \in I$.
	\end{sol}
\end{problem}


\section{MATLAB}

\subsection{Plots}
\par First things first: to create a vector of equally spaced elements is best to use \bb{linspace}. It guarantees the same amount of points in each array more easily.

\begin{lstlisting}[language=matlab]
t = linspace(-Ts, Ts, 200);
plot(t, 2*t);
xlabel('x axis, duh');
ylabel('y axis, hud');
title('guess?');
grid minor; %sometimes..
\end{lstlisting}

\par Use \bb{meshgrid} and \bb{surf}\hspace{-.18cm}ace to plot functions with multiple variables:

\begin{lstlisting}
[xx, yy] = meshgrid(x,y);
surf(xx,yy, 2 * xx, 'EdgeColor', 'none');
\end{lstlisting}

\par \bb{Meshgrid} is necessary because matlab doesn't know how to take do value iteration! The \bb{surf} function DOES know that should take one value from x, run for all values of y and get the (x,y) value for each iteration from z. THEREFORE, z must be an length(x) by length(y) matrix! This is why the mesh grid command is used. It allows for this matrix creation just in the same way as one creates values for a 2D plot.
\par What \bb{surf} will do is: check the sizes of the x and y vectors to see if it should iterate or just choose the right values. If x and y are row vectors, it will have to manually calculate all the possible combinations. If they are matrices (like the ones returned by meshgrid), it is able to just choose the appropriate combinations: take each element of each vector and plot it in space - notice what is returned by meshgrid is exactly the same dimensions as the Z vector will be.
\par To conclude: is possible to give x and y or X and Y as inputs for the \bb{surf} function of MATLAB, however, Z must be a matrix! And that will only happen with meshgrid, otherwise it will be a vector without all combinations of values in x an y.

\subsubsection{Contour}
In portuguese "contorno", returns the levels curves of the function.

contour(X1, X2, Z, 10.\textasciicircum(-20:3:0)') will plot the Z points of the coordinates X1, X2, on the levels 10.\textasciicircum[-20 -17 -14 -11 -8 -5 -2].
Note that if the points X1 and X2 are not provided, there is no "set" where to which is possible to compute the level curves, so Matlab chooses one and it may not fit your previous graph.

\subsubsection{Extra Stuff for Graphs}
Check all properties of a graph \href{https://nl.mathworks.com/help/matlab/ref/matlab.graphics.chart.primitive.line-properties.html}{\uline{here}}. Some of them:

\bb{Subplots} - allow several figures in the same window.
    Writing subplot(1,2,1) divides a window in a grid of 1x2 and will access the first element of it. Increasing the final number changes the division of the grid selected which is where the subsequent plots are going to end up on.

\bb{xlabel, ylabel} - labels for the Graphs

\bb{xlim, ylim} - limits for the Graphs

\bb{set current figure position (and size)} - set(gcf,'position',[x0, y0, width, height]); x0 is measure from the left of the screen. y0 is measure from the bottom of the screen. width is along x, height along y.

\bb{imagesc} - Image with scaled colours. imagesc(A); colorbar; shows quite fast the values that the matrix A has. A very useful tool to debug and to quickly have an insight on the matrix contents and patterns.

\bb{LineWidth}: a number for the thickness of the line

\bb{Dashed line}: '--' makes the line dashed.

\bb{DisplayName}: To specify a specific legend

\bb{legend} is possible to position the legend according to the cardinal points, "North", etc\dots Use \uline{legend('Location', 'North')}.It is also possible to put it in a desired location with legend('Position', [.25 .25 .25 .25]).

\bb{'HandleVisibility'} 'on' by default. This doesn't show a legend for a certain graph, useful for when we need to plot thresholds or something that we don't want to have a legend about.

\subsection{Functions}

To make a function, create a new file and write in the first line:

\begin{lstlisting}[language=matlab]
function [o1,o2,o3] = nameOfTheFunction(i1,i2,i3)
    %func code here
end
\end{lstlisting}
Where the o's are the outputs and the i's are inputs.

Is also possible to pass a function as an argument! For instance, if that function is what is suppose to be used inside another function.

integral(@log, x) is passing the built-in function \ii{log} as an argument.

\subsection{Set and Matlab Objects}
Since you've probably done something with Java or something with python oriented in that sense, you probably know what an object is. In Matlab, there are many object as well. Instead of changing its properties in the arguments of the instantiation of that object, one may create that object and then use the \uline{set} method.

The following two pieces of code do exactly the same thing regarding legends.

\begin{lstlisting}[language=Matlab]
    h = legend;
    rect = [0.25, 0.25, .25, .25];
    set(h, 'Position', rect);

    legend('Position', rect);
\end{lstlisting}

The rectangle should be [x0, y0, width, height], in percentages, given that 1 is the size of the figure.

\todo{check}

Actually, I have the sensation that in this case, "Position" is nothing more than an internal variable.

\subsection{Save images}

\begin{lstlisting}[language=matlab,numbers=none]
fig = figure;
plot(t,x);
pause(0.2); %may be necessary so that the figures ends plotting, before it starts saving
print(fig,'-dpng','img_filename'); %as png
\end{lstlisting}

\par Or use the following for printing the current figure:
\begin{lstlisting}[language=matlab,numbers=none]
plot(t,x);
print('myfig.png', '-dpng');
\end{lstlisting}

\par \bb{OR EVEN} imagining it was necessary to open a file to plot that picture. File named 'ola.bin' (binary file), for instance. If the image is going to be included in some report, using .eps is a good idea since it allows "infinite zoom" because it describes the picture by vectors, instead of saving the information of each pixel of compressed.
\par The code below will save as .eps (Encapsulated PostScript) file.
\begin{lstlisting}[language=matlab]
plot(t,x, 'r');
hold on;
plot(t, x_window, 'b');
print([file(1:end-4) '-hamming'], '-depsc')
\end{lstlisting}
\par Check more formats in \href{https://nl.mathworks.com/help/matlab/ref/print.html}{print page of matlab}.

\subsection{Opening stuff}
\par In CSV format:
\begin{lstlisting}[language=matlab]
file = csvread('file.csv');
\end{lstlisting}

\par But better is to open as a matrix, in a big variety of formats:
\begin{lstlisting}[language = matlab]
    %Excel
    A = readmatrix('File.xlsx','Sheet', 3,'Range','A1:AX5000');
    A = readmatrix('file.xlsx','Sheet', "name of sheet3",'Range','A1:AX5000');
    %More formats coming when I use it for them
\end{lstlisting}

\subsection{Max and Min}
\par \bb{Max} function is incredibly useful. Returns the maximum of a vector and the place it appears in! Of course, the \bb{min} flavour exists too.
    \begin{lstlisting} [language = matlab]
        [maximum, index] = max(1:10)
    \end{lstlisting}

You can even specify the dimension on which you want the maximum. So, if you want the maximum of each row, max(A, 2) will do a column vector with as many maximums as there are rows, thus a column vector.

\subsection{Other useful tools}

\begin{itemize}
	\item It is possible to see \bb{EVERY} command written in matlab. Just write 'commandhistory' or press the above arrow. The commands there are OLD!

	\item \bb{Load}, \bb{save}, \bb{exist}, \bb{return} to stop execution and \bb{clear} one variable:
	\begin{lstlisting}[language=matlab]
        try
            B = load("A.mat");
            B = B.A; %to get the matrix out of the structure
        catch
            if ~exist('Traces.xlsx', 'file')
                return
            end
            %create A code here;
            clear aux;
            save('A.mat', 'A');
        end
    \end{lstlisting}
    \item \bb{squeeze} to take away not needed dimensions. For instance in a 1x1x5000 vector, the following code will result in a 5000x1 vector.
    \begin{lstlisting}[language = matlab]
        squeeze(R(prb,1,:))
    \end{lstlisting}
    \item \bb{find} to find an element in an array! Simply returns a vector with the indexes of where that element appears.
    \begin{lstlisting}[language=matlab]
        find(a == 0)
        find(a < 5)
        find(a == b) %return all i that a(i) == b(i), i.e all elements that are in both vectors
    \end{lstlisting}
    ``The relational operators ($>$, $<$, $>=$, $<=$, $==$, $\sim =$) impose conditions on the array, and you can apply multiple conditions by connecting them with the logical operators and, or, and not, respectively denoted by the symbols \&, $|$, and $\sim$.'' From the \href{https://nl.mathworks.com/help/matlab/matlab_prog/find-array-elements-that-meet-a-condition.html}{\ul{Matlab Docs}}.
    \par Additionally: ``[row,col] = find(X) returns the row and column subscripts of each non-zero element in array X '' And this explains the above uses of this function.
    \item A cool way of \bb{copying an array size}:
    \begin{lstlisting}[language=matlab]
        a = zeros(size(b)) %copies size of b to the use we want to give a
    \end{lstlisting}

    \item To select random elements of an array: permute the indexes and select the first n elements of that permutation.
    \begin{lstlisting}[language=matlab]
        m = 100; n = 10;
        a = 1:m;
        randIndexes = randperm(m);
        b = a(randIndexes(1:n));
    \end{lstlisting}

    \item Cell to Logical to Double:
    \vspace{-.1cm}
    \quickimage{cellToLogicalToDouble.png}{.4}
\end{itemize}

\subsection{Label data in Scatter plots}
\par By far the best way of getting a decent result is to use an already written function. Adam Danz published \href{https://nl.mathworks.com/matlabcentral/fileexchange/46891-labelpoints}{\ul{here}} a function that does this perfectly for you.

\par From the examples it becomes very evident the use. The function requires at least the coordinates of every point and the labels to put in every data point.

\begin{lstlisting}[language=matlab]
    labelpoints(x, y, 'Color', [1 0.5 0.5], 'FontSize', 12);
\end{lstlisting}

\subsection{Create Gif from plots}
\par It can't get easier than this. Chad Greene wrote a miraculous script and published it \href{https://nl.mathworks.com/matlabcentral/fileexchange/63239-gif}{\ul{here}}.

\par Before any plot, write:
\begin{lstlisting}[language=matlab]
    gif('mygiffilename.gif');
    gif(gif_name,'DelayTime',0.2,'LoopCount',1,'frame',gcf);
\end{lstlisting}
\par Then, to insert a frame simply write \bb{gif}:

\begin{lstlisting}[language=matlab]
    for i = 1:10
        plot(x,y);
        gif;
    end

    web('mygiffilename.gif'); %will open on matlab web.
\end{lstlisting}

\par That is it. It can't get simpler. To view the gif with controls you can use a video player.

\subsection{Write table to Excel}
\par A matrix looks like a table but will look like a line if you use the writematrix. Instead, use \bb{writetable} and convert the matrix to a table before!

\begin{lstlisting}[language=matlab]
    table = array2table(squeeze(avg_rates_perTest(1,:,:)))

    for scheduler = 1:length(schedulers_for_testing)
        writetable(table, ...
                    num2str(scheduler) +"-avg_rates_per_test"+ ".xlsx");
    end
\end{lstlisting}

\par Additionally, is possible to add the correct names to the columns, but they have to have possible variable names... So there are some limitations: just letters, numbers and underscores and has to start with a letter.

\begin{lstlisting}[language=matlab]
    table = array2table(array, 'VariableNames', {'first_name', 'second', 'etc'});
\end{lstlisting}


\subsection{Graph Theoretical Approach}

This section aims to investigate the connection between Markov chains and graph
theory by exploring alternative ways of calculating the steady state probabilities
and considering the problem from different perspectives.

\subsubsection{Parameters}
The parameters considered as inputs are:
\begin{multicols}{2}
    \begin{itemize}
        \item the number of servers \(C\),
        \item the threshold \(T\),
        \item the capacity of the service centre \(N\),
        \item the capacity of the buffer centre \(M\).
    \end{itemize}
\end{multicols}

Additional parameters of the model are the class 1 individuals arrival rate,
the class 2 individuals
arrival rate and the service rate (\(\lambda_2, \lambda_1, \mu\)).
More specifically, the way these parameters are translated into the model are:

\begin{itemize}
    \item \textbf{Number of servers (\(C\)):} Affects the weight of all edges
    \((v_i, v_j) \in E\) in the Markov chain that correspond to a service rate.
    These edges have a weight of:
    \begin{equation*}
        w_{(v_i, v_j)} = q_{v_i, v_j}
    \end{equation*}
    where \(q_{i,j}\) is defined in equation \ref{eq:markov_transition_rate}.
    Thus, the coefficients of the service rate have a lower bound of \(1\) and
    an upper bound of \(C\).
    \item \textbf{Threshold (\(T\)):} Determines the length of the left
    \textit{arm} of the model.
    In essence the threshold acts as a breakpoint between states where \(u=0\)
    and states where \(0 \leq u \leq M\).
    Increasing \(T\) results in having more set of states where \(u\) can only
    be \(0\).
    \item \textbf{Service centre capacity (\(N\)):} Is the upper bound of \(v\) for all
    states \((u,v)\).
    \item \textbf{Buffer centre capacity (\(M\)):} Is the upper bound of \(u\) for all
    states \((u,v)\) such that \(v \geq T\).
\end{itemize}

\subsubsection{Example figure of Markov Model}

\begin{figure}[h]
    \centering
    \scalebox{0.7}{
        \input{MarkovChain/closed_form_state_probs/example_model_1352/main.tex}
        }
    \caption{\(C=1, T=3, N=5, M=2\)}
    \label{fig:Markov_1352_example_for_closed_form}
\end{figure}

In figure \ref{fig:Markov_1352_example_for_closed_form} an example of such a
Markov model is shown where \(C=1\), \(T=3\) which means that the \textit{left
arm}
of the model has a length of \(3\), \(N=5\) that indicates that the right-most
states \((u,v)\) are of the form \((u,5)\) and \(M=2\) that equivalently shows
that the bottom states are of the form \((2,v)\).

\subsubsection{A graph theoretic model underling the Markov chain}

An additional approach that one may consider to get the state probabilities is
the graph theoretical approach for state probabilities.
Thus, it can be assumed that a Markov chain model \(M\) can be translated as a
weighted directed graph \(G_M = (V, E)\) where \(V=S\) from equation
\ref{eq:state_space} and \((v_i, v_j)\in E\) if and only if \(q_{v_i, v_j}>0\).
Furthermore, the weights are given by:
\[
    w(v_i, v_j) = q_{v_i, v_j}
\]

A \textit{directed spanning tree} of a directed graph is defined as a subset of
the graph that visits all the vertices of the graph and does not include any cycles.
Unlike undirected spanning trees, directed ones also have a root which means
that a directed spanning tree that is rooted at a vertex \(v\) has to have a
path from any other vertex to vertex \(v\).
For example, consider the graph shown in figure \ref{fig:example_spanning_tree}.
The graph points out a spanning tree that is rooted at vertex 3.

\begin{figure}[h]
    \centering
    \begin{tikzpicture}
        \node[state](u1){1};
        \node[state, right=of u1](u2){2};
        \node[state, right=of u2](u3){3};
        \node[state, right=of u3](u4){4};
        \node[state, below=of u2](u5){5};
        \node[state, below=of u3](u6){6};
        \node[state, below=of u4](u7){7};
        \draw[->, thick] (u1) -- (u2);
        \draw[->, thick] (u2) -- (u3);
        \draw[->, thick] (u4) -- (u3);
        \draw[->, thick] (u5) -- (u2);
        \draw[->, thick] (u6) -- (u5);
        \draw[->, thick] (u7) -- (u6);
    \end{tikzpicture}
    \caption{Spanning tree of a graph rooted at vertex 3}
    \label{fig:example_spanning_tree}
\end{figure}

Additionally, let us denote the set of all spanning trees of \(G\) as \(T(G)\)
and the subset of \(T(G)\) that includes only the spanning trees that are rooted
at vertex \(v\) as \(T_v(G)\).
The weight of a spanning tree \(t\) can be defined as the product of the weights
of the edges it contains:

\[w(t)=\prod_{e \in t} w(e)\]

\textbf{Theorem: Markov chain tree theorem} \cite{broder1989generating} \newline
\textit{Let M be an irreducible Markov chain on n states with stationary
distribution \(\pi_1, \pi_2, \dots, \pi_n\).
Let \(G_M\) be the directed graph associated with \(M\).
Then the probability of being at state \(u\) is given by:}

\begin{equation}\label{markov-chain-tree-theorem}
    \pi_i = \frac{\sum_{t \in T_i(G_M)} w(t)}{\sum_{t \in T(G_M)}w(t)}
\end{equation}

Equation \ref{markov-chain-tree-theorem} states that the probability of being at
state \(u\) can be found by dividing the sum of the weights of all trees in
\(T_u(G)\) by the sum of the weights of all tress in \(T(G)\).
Let us ignore the denominator of that fraction for now and focus only on the
numerator denoted as \(\tilde{\pi}_i=\sum_{t \in T_i(G_M)} w(t)\)

\newpage
\subsubsection{Spanning Trees rooted at \((0,0)\)}

Let us now consider some examples of spanning trees that are rooted at \((0,0)\).
For each of the following examples the complete graph \(G\) is shown, then all
possible trees of \(T_{(0,0)}(G)\) along with the weight associated with each
spanning tree.
As well as this, the sum of all the weights of the spanning trees denoted by
\(\tilde{\pi}_{(0,0)}\) is also included.

\begin{figure}[h]
    \centering
    \input{MarkovChain/closed_form_state_probs/example_model_1121/main.tex}
\end{figure}

\begin{multicols}{2}
    \begin{center}
        \input{MarkovChain/closed_form_state_probs/spanning_trees_1121/main_0.tex}
    \end{center}

    \begin{flalign*}
        \xrightarrow{\hspace*{2cm}} \hspace{1cm} \lambda_2 \mu^3
    \end{flalign*}
\end{multicols}

\begin{multicols}{2}
    \begin{center}
        \input{MarkovChain/closed_form_state_probs/spanning_trees_1121/main_1.tex}
    \end{center}

    \begin{flalign*}
        \xrightarrow{\hspace*{2cm}} \hspace{1cm} \mu^4
    \end{flalign*}
\end{multicols}

\begin{equation*}
    \tilde{\pi}_{(0,0)} = \mu^4 + \lambda_2 \mu^3
\end{equation*}

\newpage
\begin{figure}[h]
    \centering
    \input{MarkovChain/closed_form_state_probs/example_model_1131/main.tex}
\end{figure}

\begin{multicols}{2}
    \begin{figure}[H]
        \centering
        \scalebox{0.7}{
            \input{MarkovChain/closed_form_state_probs/spanning_trees_1131/main_0.tex}}
    \end{figure}

    \begin{flalign*}
        \xrightarrow{\hspace*{2cm}} \hspace{1cm} (\lambda_2)^2 \mu^4
    \end{flalign*}
\end{multicols}

\begin{multicols}{2}
    \begin{figure}[H]
        \centering
        \scalebox{0.7}{
            \input{MarkovChain/closed_form_state_probs/spanning_trees_1131/main_1.tex}}
    \end{figure}

    \begin{flalign*}
        \xrightarrow{\hspace*{2cm}} \hspace{1cm} \lambda_2 \mu^5
    \end{flalign*}
\end{multicols}

\begin{multicols}{2}
    \begin{figure}[H]
        \centering
        \scalebox{0.7}{
            \input{MarkovChain/closed_form_state_probs/spanning_trees_1131/main_2.tex}}
    \end{figure}

    \begin{flalign*}
        \xrightarrow{\hspace*{2cm}} \hspace{1cm} \lambda_2 \mu^5
    \end{flalign*}
\end{multicols}

\begin{multicols}{2}
    \begin{figure}[H]
        \centering
        \scalebox{0.7}{
            \input{MarkovChain/closed_form_state_probs/spanning_trees_1131/main_3.tex}}
    \end{figure}

    \begin{flalign*}
        \xrightarrow{\hspace*{2cm}} \hspace{1cm} \lambda_2 \lambda_1 \mu^4
    \end{flalign*}
\end{multicols}

\begin{multicols}{2}
    \begin{figure}[H]
        \centering
        \scalebox{0.7}{
            \input{MarkovChain/closed_form_state_probs/spanning_trees_1131/main_4.tex}}
    \end{figure}

    \begin{flalign*}
        \xrightarrow{\hspace*{2cm}} \hspace{1cm} \mu^6
    \end{flalign*}
\end{multicols}

\begin{equation*}
    \tilde{\pi}_{(0,0)} = (\lambda_2)^2 \mu^4 + 2 \lambda_2 \mu^5 +
    \lambda_2 \lambda_1 \mu^4 + \mu^6
\end{equation*}

\newpage
\begin{figure}[h]
    \centering
    \input{MarkovChain/closed_form_state_probs/example_model_1122/main.tex}
\end{figure}

\begin{multicols}{4}
    \begin{figure}[H]
        \centering
        \scalebox{0.6}{
            \input{MarkovChain/closed_form_state_probs/spanning_trees_1122/main_0.tex}}
    \end{figure}
    \vspace*{\fill}
    \columnbreak
    \vspace*{0cm}
    \begin{equation*}
        (\lambda_2)^2 \mu^4
    \end{equation*}
    \vspace*{\fill}
    \columnbreak
    \begin{figure}[H]
        \centering
        \scalebox{0.6}{
            \input{MarkovChain/closed_form_state_probs/spanning_trees_1122/main_1.tex}}
    \end{figure}
    \vspace*{\fill}
    \columnbreak
    \vspace*{0.3cm}
    \begin{equation*}
        \lambda_2 \mu^5
    \end{equation*}
\end{multicols}

\begin{multicols}{4}
    \begin{figure}[H]
        \centering
        \scalebox{0.6}{
            \input{MarkovChain/closed_form_state_probs/spanning_trees_1122/main_2.tex}}
    \end{figure}
    \vspace*{\fill}
    \columnbreak
    \vspace*{0.3cm}
    \begin{equation*}
        \lambda_2 \mu^5
    \end{equation*}
    \vspace*{\fill}
    \columnbreak
    \begin{figure}[H]
        \centering
        \scalebox{0.6}{
            \input{MarkovChain/closed_form_state_probs/spanning_trees_1122/main_3.tex}}
    \end{figure}
    \vspace*{\fill}
    \columnbreak
    \vspace*{0.3cm}
    \begin{equation*}
        \mu^6
    \end{equation*}
\end{multicols}

\begin{equation*}
    \tilde{\pi}_{(0,0)} = (\lambda_2)^2 \mu^4 + 2 \lambda_2 \mu^5 + \mu^6
\end{equation*}

\newpage
\subsubsection{Conjecture of adding rows}

Let us consider three Markov models with the same number of servers \(C=1\),
the same threshold \(T=1\), the same service centre capacity \(N=2\) but
\(M\in\{1, 2, 3\}\).

\begin{multicols}{3}
    \begin{figure}[H]
        \centering
        \scalebox{0.8}{
            \input{MarkovChain/closed_form_state_probs/example_model_1121/main.tex}}
        \caption{\(M=1\)}
    \end{figure}
    \columnbreak
    \begin{figure}[H]
        \centering
        \scalebox{0.8}{
            \input{MarkovChain/closed_form_state_probs/example_model_1122/main.tex}}
        \caption{\(M=2\)}
    \end{figure}
    \begin{figure}[H]
        \centering
        \scalebox{0.8}{
            \input{MarkovChain/closed_form_state_probs/example_model_1123/main.tex}}
        \caption{\(M=3\)}
    \end{figure}
\end{multicols}

By increasing the buffer centre capacity of the system it can be observed that
\(|T_{(0,0)}(G)|\) increases as well since more combinations of paths can be
generated using the new edges and vertices.
The corresponding values of \(\tilde{\pi}_{(0,0)}\) of the three systems are:

\begin{align}
    M = 1: \tilde{\pi}_{(0,0)} &= \mu^4 + \mu^3 \lambda_2 =
    \mu^3 (\mu + \lambda_2) \label{eq:rows-conjecture-1}\\
    M = 2: \tilde{\pi}_{(0,0)} &= \mu^6 + 2\mu^5 \lambda_2 + \mu^4 (\lambda_2)^2
    = \mu^4(\mu^2 + 2\mu \lambda_2 + (\lambda_2)^2)
    = \mu^4 (\mu + \lambda_2) ^ 2 \label{eq:rows-conjecture-2}\\
    M = 3: \tilde{\pi}_{(0,0)} &= \mu^8 + 3 \mu^7 \lambda_2 +
    3 \mu^6 (\lambda_2)^2 + \mu^5(\lambda_2)^3 \nonumber \\
    &= \mu^5 (\mu^3 + 3 \mu ^2 \lambda_2 + 3 \mu (\lambda_2)^2 + (\lambda_2)^3)
    \nonumber \\
    &= \mu^5 (\mu + \lambda_2) ^ 3 \label{eq:rows-conjecture-3}
\end{align}

Note that in equations (\ref{eq:rows-conjecture-1}),(\ref{eq:rows-conjecture-2})
and (\ref{eq:rows-conjecture-3}), the following equation holds:

\begin{equation}\label{eq:rows-conjecture-general}
    \tilde{\pi}_{(0,0)} = \mu^{(N+M)} (\mu + \lambda_2)^M
\end{equation}

% TODO: Perform experiments to validate this
This relationship has been verified experimentally for ... and the data set is
archived at ...
A generalisation of equation \ref{eq:rows-conjecture-general}, where \(N \geq 1\),
is given in terms of an unknown function \(k(C,T,N)\) as:

\begin{equation}
    \tilde{\pi}_{(0,0)} = \mu^{(N+M)} (k(C,T,N))^M
\end{equation}

Thus, having investigated the effect of adding rows (increasing \(M\)) it remains
to investigate the effect of adding columns (increasing \(N\)) and finding an
expression for \(k(C,T,N)\).

\subsubsection{The effect of increasing \(N\) (Incomplete section)}
In this section we will consider a buffer centre capacity of \(M=1\) and see the
effect
of modifying other parameters on \(k(C, T, N)\).

\begin{multicols}{2}
    \begin{figure}[H]
        \centering
        \scalebox{0.9}{
            \input{MarkovChain/closed_form_state_probs/example_model_1121/main.tex}}
    \end{figure}
    \columnbreak
    \begin{figure}[H]
        \centering
        \scalebox{0.9}{
            \input{MarkovChain/closed_form_state_probs/example_model_1131/main.tex}}
    \end{figure}
\end{multicols}
\vspace{-0.5cm}
\begin{alignat}{2} \label{eq:00_rate_1131}
    \hspace{4em} & \tilde{\pi}_{(0,0)} = \mu^3[\lambda_2 + \mu] \hspace{8em} &
    \tilde{\pi}_{(0,0)} = \mu^4[(\lambda_2)^2 + \lambda_2 \lambda_1 + 2\lambda_2
    \mu + \mu^2]
\end{alignat}

\begin{figure}[h]
    \centering
    \scalebox{0.8}{
        \input{MarkovChain/closed_form_state_probs/example_model_1141/main.tex}}
\end{figure}
\begin{equation}\label{eq:00_rate_1141}
    \tilde{\pi}_{(0,0)} = \mu^5[(\lambda_2)^3 + 2(\lambda_2)^2 \lambda_1 +
    3(\lambda_2)^2 \mu + \lambda_2 (\lambda_1)^2 + 2\lambda_2 \lambda_1 \mu +
    3\lambda_2 \mu^2 + \mu^3]
\end{equation}

\begin{figure}[h]
    \centering
    \scalebox{0.8}{
        \input{MarkovChain/closed_form_state_probs/example_model_1151/main.tex}}
\end{figure}
\begin{align}\label{eq:00_rate_1151}
    \tilde{\pi}_{(0,0)} =& \mu^6[(\lambda_2)^4 + 3(\lambda_2)^3 \lambda_1 +
    4(\lambda_2)^3 \mu + 3(\lambda_2)^2 (\lambda_1)^2 +
    6(\lambda_2)^2 \lambda_1 \mu \\
    & + 6(\lambda_2)^2 \mu^2 + \lambda_2 (\lambda_1)^3 +
    2\lambda_2 (\lambda_1)^2 \mu + 3\lambda_2 \lambda_1 \mu^2 +
    4\lambda_2 \mu^3 + \mu^4] \nonumber
\end{align}

\newpage
As explained in equation \ref{eq:rows-conjecture-general} the expressions defined
above can boil down to a general form equation of the form
\(\tilde{\pi}_{(0,0)} = \mu^{(N+M)} (k(C,T,N))^M\).
The only thing missing is an expression for \(k(C,T,N)\).
An initial attempt to get such an expression can be seen below:

\begin{align}\label{eq:columns-conjecture-general}
    k(C,T,N) &= \sum_{p_1=0}^{C-1} \sum_{p_2=0}^{C-p_1-1}
    \sum_{p_3=C - p_1 - p_2 - 1}^{C - p_1 - p_2 - 1} R(p_1, p_2, p_3)
    (\lambda_2)^{p_1} (\lambda_1)^{p_2} \mu^{p_3} \nonumber \\
    &= \sum_{p_1=0}^{C-1} \sum_{p_2=0}^{C-p_1-1} R(p_1, p_2, C-p_1-p_2-1)
    (\lambda_2)^{p_1} (\lambda_1)^{p_2} \mu^{C-p_1-p_2-1}
\end{align}

In equation \ref{eq:columns-conjecture-general} the coefficient function
\(R(p_1,p_2,p_3)\) is introduced were takes as arguments the powers of
\(\lambda_2, \lambda_1 \text{ and } \mu\).
Note here that \(p_3\), the power of \(\mu\), is defined as \(p_3=C-p_1-p_2-1\)
since for all base models they need to satisfy \(p_1 + p_2 + p_3 = C-1\).
For the starting coefficients of the model the function \(R(p_1,p_2,p_3)\) gives
the values of the coefficients and is defined as:

\begin{equation} \label{eq:coefficient-function}
    R(p_1,p_2,p_3) =
    \begin{cases}
        0 & \text{if } p_1 = 0 \text{ and } p_2 > 0 \vspace{0.1cm} \\
        1 & \text{if } p_1, p_2 = 0 \text{ and } p_3 > 0 \vspace{0.2cm} \\
        \binom{p_1 + p_3}{p_3} & \text{if } p_2 = 0
        \text{ and } p_1 > 0 \vspace{0.2cm} \\
        \binom{p_1 + p_2 - 1}{p_2} & \text{if } p_3 = 0
        \text{ and } p_1, p_2 > 0 \vspace{0.2cm} \\
        p_3 + 1 & \text{if } p_1 = 1 \vspace{0.2cm} \\
        \binom{p_1 + p_3 + 1}{p_1} + p_3 \binom{p_1 + p_3}{p_3+1} -
        \binom{p_1 + p_3}{p_3} & \text{if } p_2 = 1
        \text{ and } p_1 > 1 \vspace{0.2cm} \\
        \binom{p_1 + p_2 + 1}{p_1} - \binom{p_1 + p_2 - 1}{p_2-1} +
        \sum_{i=p_2}^{p_1 + p_2 - 2} i \binom{i-1}{p_2 - 1} & \text{if } p_3 = 1
        \text{ and } p_1,p_2 > 1 \vspace{0.2cm} \\
        U_{p_1,p_2,p_3} & \text{otherwise}
    \end{cases}
\end{equation}

Note here that the final value \(U_{p_1,p_2,p_3}\) corresponds to coefficients
that are unknown and are currently investigated.
The function \(R\) takes as arguments a possible combination of numbers of
\(\lambda_2, \lambda_1 \text{ and } \mu \) for a given system and outputs the
coefficient of that term which in turn represents how many spanning trees exist
in the graph with that specific combination.
For instance consider the coefficients \((p_1,p_2,p_3)\) of some of the terms
from the equations above:

\begin{multicols}{2}
    \begin{itemize}
        \item (\ref{eq:00_rate_1131}) \( \Rightarrow (\lambda_2)^2\):
        \(R(2,0,0) = \binom{2+0}{0} = 1\)
        \item (\ref{eq:00_rate_1131}) \( \Rightarrow \lambda_2 \lambda_1\):
        \(R(1,1,0) = \binom{1+1-1}{1} = 1\)
        \item (\ref{eq:00_rate_1131}) \( \Rightarrow 2 \lambda_2 \mu\):
        \(R(1,0,1) = \binom{1+1}{1} = 2\)
        \item (\ref{eq:00_rate_1131}) \( \Rightarrow \mu^2\): \(R(0,0,2) = 1\)
        \item (\ref{eq:00_rate_1141}) \( \Rightarrow 2(\lambda_2)^2 \lambda_1\):
        \(R(2,1,0) = \binom{2+1-1}{1} = 2\)
        \item (\ref{eq:00_rate_1141}) \( \Rightarrow 3(\lambda_2)^2 \mu\):
        \(R(2,0,1) = \binom{2+1}{1} = 3\)
        \item (\ref{eq:00_rate_1141}) \( \Rightarrow 3 \lambda_2 \mu^2\):
        \(R(1,0,2) = \binom{1+2}{2} = 3\)
        \item (\ref{eq:00_rate_1151}) \( \Rightarrow 3 (\lambda_2)^3 \lambda_1\):
        \(R(3,1,0) = \binom{3+1-1}{1} = 3\)
        \item (\ref{eq:00_rate_1151}) \( \Rightarrow 3 (\lambda_2)^2 (\lambda_1)^2 \):
        \(R(2,2,0) = \binom{3}{2} = 3\)
        \item (\ref{eq:00_rate_1151}) \( \Rightarrow 6 (\lambda_2)^2 \mu ^ 2\):
        \(R(2,0,2) = \binom{2+2}{2} = 6\)
    \end{itemize}
\end{multicols}

\begin{itemize}
    \item (\ref{eq:00_rate_1151}) \( \Rightarrow 6 (\lambda_2)^2 \lambda_1 \mu\):
    \(R(2,1,1) = \binom{2+1+1}{2} + 1\binom{2+1}{1+1} - \binom{2+1}{1} = 6 + 3 - 3 = 6\)
    \item \small{(e.g)} \( \Rightarrow (\lambda_2)^2 (\lambda_1)^2 \mu\):
    \(R(2,2,1) = \binom{2+2+1}{2} - \binom{2+2-1}{2-1} + \sum_{i=2}^{2+2-2}
    i\binom{i-1}{2-1} = 10 - 3 + (2 \times 1) = 9\)
\end{itemize}

\subsubsection{Unknown terms}

The terms that remain unknown are the terms where \(p_1, p_2, p_3 \geq 2\).
Here are some of these values with the corresponding values of the
\(R(p_1,p_2,p_3)\) function.

\begin{multicols}{2}
    \begin{itemize}
        \item \(R(2,2,2) = 18\)
        \item \(R(3,2,2) = 60\)
        \item \(R(2,3,2) = 24\)
        \item \(R(2,2,3) = 30\)
        \item \(R(4,2,2) = 150\)
        \item \(R(3,3,2) = 100\)
        \item \(R(3,2,3) = 120\)
        \item \(R(2,4,2) = 30\)
        \item \(R(2,3,3) = 40\)
        \item \(R(2,2,4) = 45\)
    \end{itemize}
\end{multicols}

\subsubsection{DRL arrays}

In this section a new combinatorial object is defined: DRL arrays.
It will be shown that there is a bijection between DRL arrays and the spanning
trees in \(G_M\).
DRL arrays will then be enumerated which in turn enumerates the trees of
\(T_{(0,0)}(G_M)\).
Consider the following Markov model and the spanning trees rooted at state
\((0,0)\) that are associated with it.

\begin{figure}[h]
    \centering
    \scalebox{0.8}{
        \input{MarkovChain/closed_form_state_probs/example_model_1131/main.tex}}
        \vspace{0.8cm} \\
    \scalebox{0.6}{
        \input{MarkovChain/closed_form_state_probs/spanning_trees_1131/main_0.tex}}
        \hspace{0.7cm}
    \scalebox{0.6}{
        \input{MarkovChain/closed_form_state_probs/spanning_trees_1131/main_1.tex}}
        \vspace{0.4cm} \\
    \scalebox{0.6}{
        \input{MarkovChain/closed_form_state_probs/spanning_trees_1131/main_2.tex}}
        \hspace{0.7cm}
    \scalebox{0.6}{
        \input{MarkovChain/closed_form_state_probs/spanning_trees_1131/main_3.tex}}
        \vspace{0.4cm} \\
    \scalebox{0.6}{
        \input{MarkovChain/closed_form_state_probs/spanning_trees_1131/main_4.tex}}
\end{figure}

Looking at these spanning trees from a different perspective it can be observed
that all spanning trees of the specific model have some edges in common.

\begin{multicols}{4}
    \begin{itemize}
        \item \((0,1) \rightarrow (0,0)\)
        \item \((1,1) \rightarrow (0,1)\)
        \item \((1,2) \rightarrow (1,1)\)
        \item \((1,3) \rightarrow (1,2)\)
    \end{itemize}
\end{multicols}

These edges are the ones on the bottom row of the model, on the
\textit{threshold column} and on the \textit{arm} of the model.
In general the set of edges that are present on all spanning trees can be
denoted by:
\begin{align} \label{eq:common_edges_set}
    S &= S_1 \cup S_2 \cup S_3 \nonumber\\
    S_2 &= \{(M,v) \rightarrow (M,v-1) \; | \; T < v \leq N\} \nonumber \\
    S_1 &= \{(u,T) \rightarrow (u-1,T) \; | \; 0 < u \leq M\} \nonumber \\
    S_3 &= \{(0,v) \rightarrow (0,v-1) \; | \; 0 < v \leq T\} \nonumber \\
\end{align}

In addition, these edges that are common to every spanning tree (for a threshold
of \(T=1\)) have the same weight of \(\mu\).
In this specified model there are four of these edges, each with a weight of
\(\mu\).
Thus, since these edges exist on all spanning trees, the weight of every spanning
tree must have include a term \(\mu^4\).
Consider the expression of \(\tilde{\pi}_{(0,0)}\) associated with this Markov
model:

\begin{equation}\label{eq:pi_00_rate_example}
    \tilde{\pi}_{(0,0)} = \mu^4[(\lambda_2)^2 + \lambda_2 \lambda_1
    + 2\lambda_2 \mu + \mu^2]
\end{equation}

It can be seen that there is a \(\mu^4\) term that is a common factor of all the
 terms.
This term can be more generally calculated as \(\mu^{M+N}\) and by not worrying
about all these edges that belong in \(S\) the problem can be slightly simplified.

\begin{figure}[h]
    \centering
    \scalebox{0.7}{
        \begin{tikzpicture}[-, node distance = 1cm, auto]
            \node[state] (u0v0) {(0,0)};
            \node[state, right=of u0v0] (u0v1) {(0,1)};
            \draw[->](u0v1) edge node {\(\mu \)} (u0v0);
            \node[state, below=of u0v1] (u1v1) {(1,1)};
            \draw[->](u1v1) edge node {\(\mu \)} (u0v1);
            \node[state, right=of u0v1] (u0v2) {(0,2)};
            \node[state, right=of u1v1] (u1v2) {(1,2)};
            \draw[->](u1v2) edge node {\(\mu \)} (u1v1);
            \node[state, right=of u0v2] (u0v3) {(0,3)};
            \node[state, right=of u1v2] (u1v3) {(1,3)};
            \draw[->](u1v3) edge node {\(\mu \)} (u1v2);
        \end{tikzpicture}
    }
\end{figure}

The specific problem has now been reduced to finding all possible combinations
of two edges where one starts from \((0,2)\) and the other from \((0,3)\).
The possible edges that can be utilised here may have a direction of either
left, right, or down.
Thus, the objective of the problem can be transformed into finding all possible
permutations of an array of size \(2\) where elements can be \(L, R \text{ or } D\)
and obey certain rules so that the permutation corresponds to a valid spanning
 tree.
 These rules are:

\begin{enumerate}
    \item Permutations ending with an \(R\) are not valid. \label{rule1}
    \item Permutations that have an \(R\) followed by an \(L\) are not valid. \label{rule2}
\end{enumerate}

If any of these two rules does not hold then the permutation is immediately invalid.
Rule \ref{rule1} points to the cases where the final state has an edge pointing
to the right of it, which cannot occur since that state is the right-most state
of the first row.
Rule \ref{rule2} makes sure that there are no neighbour states that point to
each other since that would create a cycle and would not generate a valid
spanning tree.

For instance, consider the model above.
Shown below, are all possible permutations of the array along with the excluded
cases.
The valid permutations (on the left) are shown in the same order with their
corresponding spanning trees from the above figure and the invalid permutations
(on the right) are followed by the rule that determines them invalid.

\begin{multicols}{2}
    \begin{itemize}
        \item \([D, D]\)
        \item \([L, D]\)
        \item \([D, L]\)
        \item \([R, D]\)
        \item \([L, L]\)
        \item \(\xcancel{[R, R]} \rightarrow \text{ Rule 1}\)
        \item \(\xcancel{[L, R]} \rightarrow \text{ Rule 1}\)
        \item \(\xcancel{[R, L]} \rightarrow \text{ Rule 2}\)
        \item \(\xcancel{[D, R]} \rightarrow \text{ Rule 1}\)
    \end{itemize}
\end{multicols}

\subsubsection{
    Examples of mappings of directed spanning trees to permutation arrays}

\begin{multicols}{2}
    \begin{figure}[H]
        \centering
        \scalebox{0.6}{
            \input{MarkovChain/closed_form_state_probs/spanning_trees_1131/main_0.tex}}
    \end{figure}

    \begin{flalign*}
        \hspace*{-4cm} \xrightarrow{\hspace*{2cm}} \hspace{1cm} [D, D]
    \end{flalign*}
\end{multicols}

\begin{multicols}{2}
    \begin{figure}[H]
        \centering
        \scalebox{0.6}{
            \input{MarkovChain/closed_form_state_probs/spanning_trees_1131/main_1.tex}}
    \end{figure}

    \begin{flalign*}
        \hspace*{-4cm} \xrightarrow{\hspace*{2cm}} \hspace{1cm} [L, D]
    \end{flalign*}
\end{multicols}

\begin{multicols}{2}
    \begin{figure}[H]
        \centering
        \scalebox{0.6}{
            \input{MarkovChain/closed_form_state_probs/spanning_trees_1131/main_2.tex}}
    \end{figure}

    \begin{flalign*}
        \hspace*{-4cm} \xrightarrow{\hspace*{2cm}} \hspace{1cm} [D, L]
    \end{flalign*}
\end{multicols}

\begin{multicols}{2}
    \begin{figure}[H]
        \centering
        \scalebox{0.6}{
            \input{MarkovChain/closed_form_state_probs/spanning_trees_1131/main_3.tex}}
    \end{figure}

    \begin{flalign*}
        \hspace*{-4cm} \xrightarrow{\hspace*{2cm}} \hspace{1cm} [R, D]
    \end{flalign*}
\end{multicols}

\begin{multicols}{2}
    \begin{figure}[H]
        \centering
        \scalebox{0.6}{
            \input{MarkovChain/closed_form_state_probs/spanning_trees_1131/main_4.tex}}
    \end{figure}

    \begin{flalign*}
        \hspace*{-4cm} \xrightarrow{\hspace*{2cm}} \hspace{1cm} [L, L]
    \end{flalign*}
\end{multicols}

\subsubsection{Closed-form formula}
A general formula for finding all such permutations can be found, where the
inputs are \( p_1\), \(p_2\) and \(p_3\) that correspond to the number of \(D\),
\(R\) and \(L\) respectively and the output would be the coefficient of the term
\((\lambda_2)^{p_1} (\lambda_1)^{p_2} \mu^{p_3}\).
For instance, by applying such a formula to the example in equation
\ref{eq:pi_00_rate_example}, the desired output should be:

\begin{itemize}
    \item \((\lambda_2)^2 \hspace{0.95cm} \rightarrow \hspace{1cm}
    p_1 = 2, p_2=0, p_3=0 \hspace{1cm} \rightarrow \hspace{1cm}
    \text{coefficient } = 1\)
    \item \(\lambda_2 \lambda_1 \hspace{1cm} \rightarrow \hspace{1cm}
    p_1 = 1, p_2=1, p_3=0 \hspace{1cm} \rightarrow \hspace{1cm}
    \text{coefficient } = 1\)
    \item \(2 \lambda_2 \mu \hspace{1cm} \rightarrow \hspace{1cm}
     p_1 = 1, p_2=0, p_3=1 \hspace{1cm} \rightarrow \hspace{1cm}
     \text{coefficient } = 2\)
    \item \(\mu^2 \hspace{1.45cm} \rightarrow \hspace{1cm}
    p_1=0, p_2=0, p_3=2 \hspace{1cm} \rightarrow \hspace{1cm}
    \text{coefficient } = 1\)
\end{itemize}

Thus, given all possible and valid combinations of powers among \(\lambda_2,
\lambda_1 \text{ and } \mu\) (i.e. \(p_1,p_2,p_3\)) generated by equation
\ref{eq:columns-conjecture-general}, an alternative and improved form of the
value of \(R(p_1, p_2, p_3)\) described in equation \ref{eq:coefficient-function}
is given by:

\begin{equation} \label{eq:permutation formula}
    R(p_1, p_2, p_3) = T(p_1, p_2, p_3) - E_R(p_1, p_2, p_3) - E_D(p_1, p_2, p_3)
    - E_L(p_1, p_2, p_3) - E_{RL}(p_1, p_2, p_3)
\end{equation}

Consider the above undefined equations.
The term \(T(p_1,p_2,p_3)\) denotes the number of all permutations where neither
rule is applied, i.e. all possible ways one can arrange the elements of the array.
The term \(E_R(p_1,p_2,p_3)\) denotes the number of permutations that end in \(R\),
which needs to be removed from the total of all permutations so that rule \ref{rule1}
is satisfied.
Having excluded all permutations that end in \(R\) it only remains to leave out
permutations that have an \(R\) followed by an \(L\) (rule \ref{rule2}).
Although, removing all permutations ending in \(R\) was relatively straight forward,
removing all permutations that follow rule \ref{rule2} is not that easy.
This is because equation \(E_R(p_1,p_2,p_3)\) already considers some cases where
there is an \(R\) followed by an \(L\).
Therefore, in order to consider only new cases, permutations of rule \ref{rule2}
are split into three new terms; \(E_D\), \(E_L\) and \(E_{RL}\).
These terms denote the permutations that have an \(R\) followed by an \(L\) AND
do not end in \(R\).
The term \(E_D\) considers all permutations that end in \(D\) while \(E_L\) the
ones that end in \(L\).
Finally, the last term (\(E_{RL}\)) denotes all permutations that end in \(R\),
\(L\) where there is no other \(R\) followed by an \(L\) in any other position apart
from the last two.
This term is used because in the \(E_L\) term, such cases (where \(R\) and \(L\)
are in the last two positions) are only considered when there is another \(R\) followed
by an \(L\) somewhere.
Thus, the term \(E_{RL}\) is a particular set of permutations that the formula of
\(E_L\) fails to include by itself.

\begin{align}
    T(p_1, p_2, p_3) &= \frac{(p_1 + p_2 + p_3)!}{p_1! \times p_2! \times p_3!} \\
    E_R(p_1, p_2, p_3) &= \frac{(p_1 + p_2 + p_3 - 1)!}
    {p_1! \times (p_2-1)! \times p_3!} \\
    E_D(p_1, p_2, p_3) &= \sum_{i=1}^{\min(R,L)} (-1)^{i+1}
    \frac{(p_1 + p_2 + p_3 - i - 1)!}
    {(p_1 - 1)! \times (p_2 - i)! \times (p_3 - i)! \times (i)!} \\
    E_L(p_1, p_2, p_3) &= \sum_{i=1}^{\min(R,L-1)} (-1)^{i+1}
    \frac{(p_1 + p_2 + p_3 - i - 1)!}
    {p_1! \times (p_2 - i)! \times (p_3 - i - 1)! \times (i)!} \\
    E_{RL}(p_1, p_2, p_3) &= \sum_{i=1}^{\min(R,L)} (-1)^{i+1}
    \frac{(p_1 + p_2 + p_3 - i - 1)!}
    {p_1! \times (p_2 - i)! \times (p_3 - i)! \times (i - 1)!}
\end{align}

\begin{equation*}
    R(p_1, p_2, p_3) = T(p_1, p_2, p_3) - E_R(p_1, p_2, p_3) - E_D(p_1, p_2, p_3)
    - E_L(p_1, p_2, p_3) - E_{RL}(p_1, p_2, p_3)
\end{equation*}

\subsubsection{Example of the permutation algorithm}
Consider the term \((\lambda_2) (\lambda_1) \mu^2\) and the above expressions.
In order to get the coefficient of that term the permutation algorithm needs to
be applied with an input of \(p_1=1, p_2=1, p_3=2\), i.e. 1 \(D\), 1 \(R\) and 2
\(L\)s in the array.
The permutations that correspond to each expression can be seen below:

\begin{equation*}
    T(p_1, p_2, p_3) = \frac{(1+1+2)!}{1! \; 1! \; 2!} = 12
\end{equation*}

\begin{align*}
    & [D, R, L, L] \quad [R, D, L, L] \quad [D, L, R, L] \quad
    [R, L, D, L] \quad [D, L, L, R] \quad [R, L, L, D] \\
    & [L, D, R, L] \quad [L, R, D, L] \quad [L, D, L, R] \quad
    [L, R, L, D] \quad [L, L, D, R] \quad [L, L, R, D]
\end{align*}

\begin{equation*}
    E_R(p_1, p_2, p_3) = \frac{(1+1+2-1)!}{1! \; (1-1)! \; 2!} = 3
\end{equation*}

\begin{align*}
    & [D, L, L, | R] \quad [L, D, L, | R] \quad [L, L, D, | R]
\end{align*}

\begin{equation*}
    E_D(p_1, p_2, p_3) = \sum_{i=1}^{1} (-1)^{i+1} \frac{(1+1+2-i-1)!}{0! \;
    (1-i)! \; (2-i)! \; (i)!} = 1 \times \frac{2}{0! \; 0! \; 1! \; 1!} = 2
\end{equation*}

\begin{align*}
    & [R, L, L, | D] \quad [L, R, L, | D]
\end{align*}

\begin{equation*}
    E_L(p_1, p_2, p_3) = \sum_{i=1}^{1} (-1)^{i+1}
    \frac{(1+1+2-i-1)!}{1! \; (1-i)! \; (2-i-1)! \; (i)!}
    = 1 \times \frac{2}{1! \; 0! \; 0! \; 1!} = 2
\end{equation*}

\begin{align*}
    & [D, R, L, | L] \quad [R, L, D, | L]
\end{align*}

\begin{equation*}
    E_{RL}(p_1, p_2, p_3) = \sum_{i=1}^{1} (-1)^{i+1}
    \frac{(1+1+2-i-1)!}{1! \; (1-i)! \; (2-i)! \; (i_1)!}
    = 1 \times \frac{2}{1! \; 0! \; 1! \; 0!} = 2
\end{equation*}

\begin{align*}
    & [D, L, | R, L] \quad [L, D, | R, L]
\end{align*}

\subsubsection{Possibly useful theorem: Matrix-tree theorem for directed graphs
(Kirchhoff's theorem) \cite{chaiken1978matrix}: }
\textit{The number of directed spanning trees rooted at a state \(i\) can be found
by calculating the determinant of the Laplacian matrix \(Q\) of the directed graph
and removing row \(i\) and column \(i\).}


Regression reports are generated using software you have already encountered: \code{lm} to fit a model and \code{summary} to construct the report from
the fitted model.  To illustrate: \datasetSwimming
\begin{Schunk}
\begin{Sinput}
> swim = fetchData("swim100m.csv")
> mod = lm(time ~ year + sex, data=swim)
> summary(mod)
\end{Sinput}
\begin{Soutput}
...
            Estimate Std. Error t value Pr(>|t|)
(Intercept) 555.7168    33.7999   16.44  < 2e-16 ***
year         -0.2515     0.0173  -14.52  < 2e-16 ***
sexM         -9.7980     1.0129   -9.67  8.8e-14 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 3.98 on 59 degrees of freedom
Multiple R-squared: 0.844,	Adjusted R-squared: 0.839
F-statistic:  160 on 2 and 59 DF,  p-value: <2e-16
\end{Soutput}
\end{Schunk}

\subsection{Confidence Intervals from Standard Errors}

\index{P}{Confidence Intervals}

Given the coefficient estimate and the standard error
from the regression report, the confidence interval is easily generated.
For a 95\% confidence interval, you just multiply the standard error by 2
to get the margin of error.  For example, in the above, the
margin of error on \indicatorVar{sex}{M} is $2 \times 1.013 = 2.03$, or, in
computer notation:
\begin{Schunk}
\begin{Sinput}
> 2 * 1.0129
\end{Sinput}
\begin{Soutput}
[1] 2.03
\end{Soutput}
\end{Schunk}

If you want the two endpoints of the confidence interval, rather than
just the margin of error, do these
simple calculations: (1) subtract the margin of error from the estimate;
(2) add the margin of error to the estimate.  So,
\begin{Schunk}
\begin{Sinput}
> -9.798 - 2*1.0129
\end{Sinput}
\begin{Soutput}
[1] -11.8
\end{Soutput}
\begin{Sinput}
> -9.798 + 2*1.0129
\end{Sinput}
\begin{Soutput}
[1] -7.77
\end{Soutput}
\end{Schunk}
The key thing is to remember the multiplier that is applied
to the standard error.  A multiplier of approximately 2 is for
a 95\% confidence level.

The \function{confint} function provides a convenient way to calculate
confidence intervals directly.
\index{C}{confidence interval!computation}
\index{P}{confint@\texttt{confint}}
\index{P}{Confidence Intervals!confint@\texttt{confint}}
It calculates the exact multiplier (which depends somewhat on the
sample size)  and applies it to the standard error to produce the confidence intervals.
\begin{Schunk}
\begin{Sinput}
> mod = lm(time ~ year + sex, data=swim)
> confint(mod)
\end{Sinput}
\begin{Soutput}
              2.5 %  97.5 %
(Intercept) 488.083 623.350
year         -0.286  -0.217
sexM        -11.825  -7.771
\end{Soutput}
\end{Schunk}

It would be convenient if the regression report included confidence
intervals rather than the standard error.  Part of the reason it
doesn't is historical: the desire to connect to the traditional by-hand calculations.

\subsection{Bootstrapping Confidence Intervals}

Confidence intervals on model coefficients can be computed using the
same bootstrapping technique introduced in Chapter \ref{chap:statistical-inference}.

Start with your fitted model. To illustrate, here is a model of
swimming time over the years, taking into account sex:
\begin{Schunk}
\begin{Sinput}
> swim = fetchData("swim100m.csv")
> lm(time ~ year + sex, data=swim)
\end{Sinput}
\begin{Soutput}
...
(Intercept)         year         sexM
    555.717       -0.251       -9.798
\end{Soutput}
\end{Schunk}
These coefficients reflect one hypothetical draw from the
population-based sampling
distribution.  It's impossible to get another draw from the
``population'' here: the actual records are all you've got.

But to approximate sampling variation, you can treat the sample as
your population and re-sample:
\begin{Schunk}
\begin{Sinput}
> lm(time ~ year + sex, data=resample(swim))
\end{Sinput}
\begin{Soutput}
...
(Intercept)         year         sexM
    495.600       -0.221       -8.717
\end{Soutput}
\end{Schunk}

Constructing many such re-sampling trials and collect the results
\begin{Schunk}
\begin{Sinput}
> s = do(500) * lm(time ~ year + sex, data=resample(swim))
\end{Sinput}
\end{Schunk}
\begin{Schunk}
\begin{Sinput}
> s
\end{Sinput}
\end{Schunk}
\begin{Schunk}
\begin{Soutput}
  Intercept   year   sexM sigma r-squared
1       531 -0.239  -9.60  3.71     0.853
2       514 -0.230 -10.25  3.28     0.893
3       525 -0.236  -9.70  3.67     0.827
4       562 -0.255  -9.41  3.97     0.846
5       568 -0.257 -10.76  4.65     0.819
... for 500 cases altogether ...
\end{Soutput}
\end{Schunk}

To find the standard error of the coefficients, just take the standard
deviation across the re-sampling trials:
\begin{Schunk}
\begin{Sinput}
> sd(s)
\end{Sinput}
\begin{Soutput}
Intercept      year      sexM     sigma r-squared
  43.5426    0.0220    0.9530    0.8057    0.0343
\end{Soutput}
\end{Schunk}

Multiplying the standard error by 2 gives the approximate 95\% margin
of error.  Alternatively, you can use the \function{confint} function
to calculate this for you:
\begin{Schunk}
\begin{Sinput}
> confint(s, method="stderr")
\end{Sinput}
\begin{Soutput}
       name    2.5%   97.5%
1 Intercept 472.355 643.454
2      year  -0.296  -0.209
3      sexM -11.621  -7.876
4     sigma   2.215   5.381
5 r-squared   0.788   0.923
\end{Soutput}
\end{Schunk}

\subsection{Prediction Confidence Intervals}

\index{C}{confidence interval!for prediction}
\index{C}{prediction!confidence interval}

When a model is used to make a prediction, it's helpful to be able to
describe how precise the prediction is.  For instance, suppose you \datasetFeet
want to use the \texttt{kidsfeet.csv} data set to make a prediction of
the foot width of a girl whose foot length is 25 cm.

\index{P}{predict@\texttt{predict}}
\index{P}{Modeling!predict@\texttt{predict}}

First, fit your model:
\begin{Schunk}
\begin{Sinput}
> feet = fetchData("kidsfeet.csv")
> names(kids)
\end{Sinput}
\begin{Soutput}
 [1] "Gender"      "Grade"       "Age"         "Race"
 [5] "Urban.Rural" "School"      "Goals"       "Grades"
 [9] "Sports"      "Looks"       "Money"
\end{Soutput}
\begin{Sinput}
> levels(kids$sex)
\end{Sinput}
\begin{Soutput}
NULL
\end{Soutput}
\begin{Sinput}
> mod = lm(width ~ length + sex, data=feet)
\end{Sinput}
\end{Schunk}

Now apply the model to the new data for which you want to make a
prediction.  Take care to use the right coding for categorical variables.
\begin{Schunk}
\begin{Sinput}
> predict(mod, newdata=data.frame( length=25, sex="G" ))
\end{Sinput}
\begin{Soutput}
   1
8.93
\end{Soutput}
\end{Schunk}

In order to generate a confidence interval, the \code{predict}
operator needs to be told what type of interval is wanted.  There are
two types of prediction confidence intervals:
\begin{description}
\item[Interval on the model value] which reflects the sampling
  distributions of the coefficients themselves.  To calculate this,
  use the \code{interval="confidence"} named argument:
\begin{Schunk}
\begin{Sinput}
> predict(mod, newdata=data.frame( length=25, sex="G" ),
         interval="confidence")
\end{Sinput}
\begin{Soutput}
   fit  lwr  upr
1 8.93 8.74 9.13
\end{Soutput}
\end{Schunk}

The components named \code{lwr} and \code{upr} are the lower and upper
limits of the confidence interval, respectively.
\index{P}{interval@\texttt{interval=}!in \texttt{predict}}
\index{P}{Confidence Intervals!interval@\texttt{interval=}!in \texttt{predict}}

\item[Interval on the prediction] which includes the variation due to
  the uncertainty in the coefficients as well as the size of a typical
  residual.  To find this interval, use the
  \code{interval="prediction"} named argument:
\begin{Schunk}
\begin{Sinput}
> predict(mod, newdata=data.frame( length=25, sex="G" ),
         interval="confidence")
\end{Sinput}
\begin{Soutput}
   fit  lwr  upr
1 8.93 8.74 9.13
\end{Soutput}
\end{Schunk}

The prediction interval is larger than the model-value confidence
interval because the residual always gives additional uncertainty
around the model value.
\end{description}


\subsection{Precision}
\label{chp:fundamentals:sec:metrics:subsec:precision}

The first metric used is \textit{precision}.
It is defined \textit{"as the probability that an item is relevant given that it is detected by the algorithm"} \parencite{Zhu:2004}.
This means if an algorithm is optimized for precision, the aim is that all selected items are relevant.
However, this measure only considers the items selected by the algorithm meaning it neglects the relevant items which were not selected.
Precision is defined as:

\begin{equation}\label{eq:precision}
    prec = \frac{\acp{TP}}{\acp{TP}+\acp{FP}}
\end{equation}

As an example the algorithm which was used to classify the elements in \cref{fig:metrics:tp_vis} has a precision $prec = \frac{3}{3+4} = \frac{3}{7}\approx 0.43$.


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}

\title{Vibing Math (Floor Function Problem)}
\author{Shreenabh Agrawal}
\date{\today}
\usepackage{amsmath}
\usepackage{geometry}
\geometry{a4paper, portrait, margin=1in}
\usepackage{natbib}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage[makeroom]{cancel}

\begin{document}

\maketitle

\section{Question}

    Find $x$ if
$$x{\lfloor x {\lfloor x {\lfloor x \rfloor}\rfloor}\rfloor}=88$$
Here $x>0$, $\lfloor x \rfloor$ is the floor function.

\section{Solution}
Approximating given expression as $x^4\approx88$, we have $$x\approx3.0628$$Also, the given expression can be rewritten as: $${\lfloor x {\lfloor x {\lfloor x \rfloor}\rfloor}\rfloor}=\frac{88}{x}$$
This implies that $\frac{88}{x}$ is an integer. By substituting our approximate value of $x$, we get $$\frac{88}{x} \approx 28.7318$$
This further means that $$x = \frac{88}{28} \: \left(=\frac{22}{7}\right) \:or\: \frac{88}{29}$$
Substituting back and checking, we find that,
$$\frac{22}{7} {\left\lfloor \frac{22}{7} {\left\lfloor \frac{22}{7} {\left\lfloor \frac{22}{7} \right\rfloor}\right\rfloor}\right\rfloor} = 88$$
Therefore
$$\boxed{x = \frac{22}{7}}$$
\end{document}


\chapter{Bijections, Surjections, and Injections}
\label{chapter:bijections-surjections-injections}
\marginurl{%
  Bijections, Surjections, and Injections:\\\noindent
  Introduction to Combinatorics \#1
}{youtu.be/fW5Zxg0TMDc}

Combinatorics is an area of mathematics primarily concerned with counting;
hence, the questions studied in combinatorics are usually formulated as results
about the sizes of sets. This chapter uses informal notion of size of a set, for
the formal definition see \Cref{chapter:cardinality}.
\section{Bijections}
The simplest way to explain that one set has the same number of elements as
another is to show a correspondence between elements of these sets. For example,
in order to explain that the set $\set{0, \pi, 1 / 4}$ has the same number of
elements as $\set{1, 2, 3}$ we may just say that $0$ corresponds to $1$,
$\pi$ corresponds to $2$, and $1 / 4$ corresponds to $3$. More formally such a
correspondence is defined using the following definition.
\begin{definition}
    Let $f : X \to Y$ be a function. We say that $f$ is a bijection iff the
    following properties are satisfied.
    \begin{itemize}
        \item Every element of $Y$ is an image of some element of $X$. In other
            words,
            \[
                \forall y \in Y~\exists x \in X\ f(x) = y.
            \]
        \item Images of any two elements
            of $X$ are different. In other words,
            \[
                \forall x_1, x_2 \in X\ f(x_1) \neq f(x_2).
            \]
    \end{itemize}
\end{definition}

Let us consider the following example. Let $f : \mathbb{R} \to \mathbb{R}$ be a
function such that $f(x) = x + 1$; Note that it is a bijection:
\begin{itemize}
    \item For any $y \in \mathbb{R}$, $f(y - 1) = (y - 1) + 1 = y$.
    \item If $f(x_1) = f(x_2)$, then $x_1 + 1 = x_2 + 1$ i.e. $x_1 = x_2$.
\end{itemize}

\begin{exercise}
    Show that $x^3$ is a bijection.
\end{exercise}

One of the nicest properties of bijections is that composition of two bijections
is a bijection.
\begin{theorem}
\label{theorem:bijections-composition}
    Let $X$, $Y$, and $Z$ be some sets and $f : X \to Y$ and $g : Y \to Z$ be
    bijections. Then $(g \circ f) : X \to Z$ is also a bijection.
\end{theorem}
\begin{proof}
    We need to check two properties.
    \begin{itemize}
        \item Let $x_1 \neq x_2 \in X$. Note that $f(x_1) \neq f(x_2)$ since $f$
            is a bijection. Hence, $g(f(x_1)) \neq g(f(x_2))$ since $g$ is a bijection
            as well. As a result, $(g \circ f)(x_1) \neq (g \circ f)(x_2)$.
        \item Let $z \in Z$; we need to find $x \in X$ such that
            $(g \circ f)(x) = z$. Note that since $g$ is a bijection there is
            $y \in Y$ such that $g(y) = z$. Additionally, there is $x \in X$ such
            that $f(x) = y$ since $f$ is a bijection. Thus,
            $(g \circ f)(x) = g(f(x)) = z$.
    \end{itemize}
\end{proof}

Another important property of bijections is that they can be inverted.
\begin{theorem}
\label{theorem:inverse-of-bijections}
    Let $f : X \to Y$ be a function. $f$ is invertible (i.e. there is a function
    $g : Y \to X$ such that $(f \circ g)(y) = y$ and $(g \circ f)(x) = x$ for all
    $x \in X$ and $y \in Y$) iff $f$ is a bijection.
\end{theorem}
\begin{proof}
    \begin{description}
        \item[$\Rightarrow$] Let's assume that $f$ is invertible. We need to prove
            that $f$ is a bijection.
            \begin{itemize}
                \item Let's assume that $f$ does not satisfy the first property in the
                    definitions of bijections i.e. there are
                    $x_1, x_2 \in X$ such that $f(x_1) = f(x_2)$ but
                    $x_1 = g(f(x_1)) = g(f(x_2)) = x_2$, which is a
                    contradiction.
                \item Let $y \in Y$. Note that $f(g(y)) = y$, hence,
                    $\Im f = Y$.
            \end{itemize}

        \item[$\Leftarrow$] Let's assume that $f$ is bijective. We need to define a
            function $g : Y \to X$ which is an inverse of $f$. Let $y \in Y$, note
            that there is a unique $x$ such that $f(x) = y$, we define $g(y) = x$.
            Note that $f(g(y)) = y$ for every $y$ by the construction of $g$.
            Additionally, $g(f(x)) = x$ since $f(g(f(x))) = f(x)$ and $f$ is a
            bijection.
    \end{description}
\end{proof}
\noindent We denote $g$ from this theorem as $f^{-1}$ and in case when $f$ is
not a bijection $f^{-1}(y)$ denotes the set $\set[f(x) = y]{x \in X}$.
\nomenclature[F]{$f^{-1}$}{denotes the inverse of the function $f$ (it's
defined only when $f$ is a bijection)}
\nomenclature[F]{$f^{-1}(y)$}{depend on the context if $f$ is not a bijection
it denotes the set $\set[f(x) = y]{x \in X}$ and it denotes the value of
$f^{-1}$ at $y$ if $f$ is a bijection}

Because of the following theorem, bijections are very useful in combinatorics.
\begin{theorem}
\label{theorem:bijection-to-equality}
    Let $X$ and $Y$ be two finite sets such that there is a bijection $f$ from
    $X$ to $Y$. Then $\cardinality{X} = \cardinality{Y}$.
\end{theorem}

Using this result we can make prove the following equality.
\begin{corollary}
\label{corollary:power-set-and-set-of-binary-strings}
    Let $X$ be a finite set of cardinality $n$. Then $2^X$ has the same
    cardinality as $\set{0, 1}^{\cardinality{X}}$.
\end{corollary}
\begin{proof}
  We are going to prove this statement for $X = \range{n}$ since for the full
  proof we would need a formal definition a finite set.

  Now we need to construct a bijection $g$ from $2^{\range{n}}$ to
  $\set{0, 1}^n$ such that $g(Y) = (u_1, \dots, u_n)$, where $u_i = 1$ iff $i \in Y$.
  It is clear that $g^{-1}(u_1, \dots, u_n) = \set[u_i = 1]{i \in \range{n}}$ is an
  inverse of $g$ so $g$ is indeed a bijection.
\end{proof}

\section{Surjections and Injections}

It is possible to note that the definition of the bijection consists of two part.
Both of these parts are interesting in their own regard, so they have their own
names.
\begin{definition}
  Let $f : X \to Y$ be a function.
  \begin{itemize}
    \item We say that $f$ is a surjection iff every element of $Y$ is an image
      of some element of $X$. In other words,
      \[
        \forall y \in Y~\exists x \in X\ f(x) = y.
      \]
      \item We say that $f$ is an injection iff images of any two elements
        of $X$ are different. In other words,
        \[
          \forall x_1, x_2 \in X\ f(x_1) \neq f(x_2).
        \]
  \end{itemize}
\end{definition}

\begin{remark}
  Let $f : X \to Y$ be an injection. Then $g : X \to \Im f$ such that
  $f(x) = g(x)$ is a bijection.
\end{remark}

\begin{exercise}
  Let $\R^+ = \set[x > 0]{x \in \R}$. Is $f : \R^+ \to \R^+$ such that
  $f(x) = x + 1$ a surjection/injection?
\end{exercise}

Like in the case of the bijection we may use surjections and injections to
compare sizes of sets.
\begin{theorem}
\label{theorem:injections-surjections-inequalities}
  Let $X$ and $Y$ be finite sets.
  \begin{itemize}
    \item If there is an injection from $X$ to $Y$, then $\cardinality{X}
      \le \cardinality{Y}$.
    \item If there is a surjection from $X$ to $Y$, then $\cardinality{X}
      \ge \cardinality{Y}$.
  \end{itemize}
\end{theorem}

\begin{chapterendexercises}
  \exercise Let $p$ be a polynomial of even degree. Is it possible that $p$ is a
    bijection from $\R$ to $\R$?
  \exercise[recommended] Construct a bijection from
    \[
      \set[{A, B \subseteq \range{n} \text{ and } A, B \text{ are disjoint}}]{(A, B)}.
    \]
    to $\set{0, 1, 2}^n$.
    \begin{solution}
      Let us start from an informal proof. For each $i \in \range{n}$ there are
      three ways to put or not put it into sets $A$ and $B$ such that $A \cap B
      = \emptyset$:
      \begin{enumerate}
        \item we may not put $i$ to both of them,
        \item we may put $i$ only to $A$,
        \item we may put $i$ only to $B$.
      \end{enumerate}
      Which defines a bijection from
      $\set[{A, B \subseteq [n] \text{ and } A \cap B = \emptyset}]{(A, B)}$ to
      $\set{0, 1, 2}^n$.

      Now we are ready give a more formal construction of the bijection from
      $\set[{A, B \subseteq [n] \text{ and } A \cap B = \emptyset}]{(A, B)}$ to
      $\set{0, 1, 2}^n$.

      Consider
      $f : \set[{A, B \subseteq [n] \text{ and } A \cap B = \emptyset}]{(A, B)} \to
        \set{0, 1, 2}^n$ such that
      $f(A, B) = (x_1, \dots, x_n)$, where
      \[
        x_i =
        \begin{cases}
          0 & \text{if } i \in A \text{ and } i \not\in B \\
          1 & \text{if } i \not\in A \text{ and } i \in B \\
          2 & \text{if } i \not\in A \text{ and } i \not\in B
        \end{cases}.
      \]
      Note that the function is well defined due to the fact that $A \cap B =
      \emptyset$.
      We need to prove now that it is a bijection. To do this we can simply
      construct the inverse function $e$; $e : \set{0, 1, 2}^n \to
      \set[{A, B \subseteq [n] \text{ and } A \cap B = \emptyset}]{(A, B)}$ such
      that $e(x_1, \dots, x_n) = (A, B)$, where
      $A = \set[x_i = 1]{i \in \range{n}}$ and
      $B = \set[x_i = 2]{i \in \range{n}}$.
      Obviously $e$ is the inverse of $f$, hence, $f$ is a bijection.
    \end{solution}
  \exercise[recommended] Construct a bijection from  $\range{2n}$ to
    $\set{0, 1} \times \range{n}$.
    \begin{solution}
      Consider the function
      \[
        f(i) =
        \begin{cases}
          (0, i) & i \le n \\
          (1, i - n) & n < i \le 2n
        \end{cases}.
      \]
      Note that the function is defined correctly since the first coordinate is
      always $0$ or $1$ and the second coordinate is always from $\range{n}$ due to the
      fact that if $i \le n$, then $i \in \range{n}$ and if $i > n$, then
      $(i - n) \in \range{n}$.
      We are going to prove now that it is a bijection.
      \begin{description}
        \item[injection:]
          Assume the opposite i.e. that there are $i_0 \neq i_1$ such that
          $f(i_0) = f(i_1)$. Without loss of generality we may assume that $i_0
          < i_1$.
          Consider three cases:
          \begin{enumerate}
            \item ($i_0, i_1 \le n$) Note that $(0, i_0) = f(i_0)$ and
              $f(i_1) = (0, i_1)$. So $i_0 = i_1$ since $f(i_0) = f(i_1)$, which
              is a contradiction.
            \item ($i_0, i_1 > n$) Note that $(1, i_0 - n) = f(i_0)$ and
              $f(i_1) = (1, i_1 - n)$. So $i_0 = i_1$ since $f(i_0) = f(i_1)$, which
              is a contradiction.
            \item ($i_0 \le n < i_1$) Note that $(0, i_0) = f(i_0)$ and
              $f(i_1) = (1, i_1 - n)$. So $0 = 1$ since $f(i_0) = f(i_1)$, which
              is a contradiction.
          \end{enumerate}
        \item[surjection:]
          Let us fix some $(a, b) \in \set{0, 1} \times \range{n}$. We need to show that
          it belongs to $\Im f$.
          \begin{enumerate}
            \item ($a = 0$) Note that $b \le n$, hence, $f(b) = (0, b) =
              (a, b)$. So $(a, b) \in \Im f$.
            \item ($a = 1$) Note that $2n \ge n + b > n$, hence,
              $f(n + b) = (1, (n + b) - b) = (1, b) = (a, b)$.
              So $(a, b) \in \Im f$.
          \end{enumerate}
      \end{description}
    \end{solution}
  \exercise We say that $u, v \in \set{0, 1}^n$ are orthogonal over $\F_2$ iff
    $\sum_{i = 1}^n u_i v_i$ is even. Let $v \in \set{0, 1}^n$ be a tuple such
    that $v_i \neq 0$ for some $i \in \range{n}$. Show that if we choose a
    tuple $u \in \set{0, 1}^n$ uniformly at random, then $u$ and $v$ are
    orthogonal over $\F_2$ with probability $1 / 2$.
\end{chapterendexercises}


\section{Spans}

\begin{outcome}
  \begin{enumerate}
  \item Determine the span of a set of vectors.
  \item Determine if a vector is contained in a specified span.
  \end{enumerate}
\end{outcome}

Let $\vect{u}$ and $\vect{v}$ be two non-parallel vectors in
$\R^n$. We can picture the set of their linear combinations as follows:
\begin{center}
  \begin{tikzpicture}[scale=1.5]
    \draw[->, thick, blue] (0,0) -- node[below]{$\vect{u}$} (1.5,0.5);
    \draw[->, thick, red] (0,0) -- node[left]{$\vect{v}$} (0.5,1);
    \draw[-, gray] (-2,-1.5)--(4,0.5);
    \draw[-, gray] (-1.5,-0.5)--(0,0);
    \draw[-, gray] (1.5,0.5)--(4.5,1.5);
    \draw[-, gray] (-1,0.5)--(5,2.5);
    \draw[-, gray] (-0.5,1.5)--(5.5,3.5);
    \draw[-, gray] (0,2.5)--(6,4.5);
    \draw[-, gray] (-2,-1.5)--(0,2.5);
    \draw[-, gray] (-0.5,-1)--(0,0);
    \draw[-, gray] (0.5,1)--(1.5,3);
    \draw[-, gray] (1,-0.5)--(3,3.5);
    \draw[-, gray] (2.5,0)--(4.5,4);
    \draw[-, gray] (4,0.5)--(6,4.5);
    \draw (-2.0,-1.5) node[left]{$-1\vect{u}-1\vect{v}$};
    \draw (-1.5,-0.5) node[left]{$-1\vect{u}+0\vect{v}$};
    \draw (-1.0,0.5) node[left]{$-1\vect{u}+1\vect{v}$};
    \draw (-0.5,1.5) node[left]{$-1\vect{u}+2\vect{v}$};
    \draw (0.0,2.5) node[left]{$-1\vect{u}+3\vect{v}$};
    \draw (1.5,3.0) node[above=0.75ex]{$0\vect{u}+3\vect{v}$};
    \draw (3.0,3.5) node[above=0.75ex]{$1\vect{u}+3\vect{v}$};
    \draw (4.5,4.0) node[above=0.75ex]{$1\vect{u}+3\vect{v}$};
    \draw (6.0,4.5) node[right]{$3\vect{u}+3\vect{v}$};
    \draw (5.5,3.5) node[right]{$3\vect{u}+2\vect{v}$};
    \draw (5.0,2.5) node[right]{$3\vect{u}+1\vect{v}$};
    \draw (4.5,1.5) node[right]{$3\vect{u}+0\vect{v}$};
    \draw (4.0,0.5) node[right]{$3\vect{u}-1\vect{v}$};
    \draw (2.5,0.0) node[below=0.75ex]{$2\vect{u}-1\vect{v}$};
    \draw (1.0,-0.5) node[below=0.75ex]{$1\vect{u}-1\vect{v}$};
    \draw (-0.5,-1.0) node[below=0.75ex]{$0\vect{u}-1\vect{v}$};
    \draw[fill] (0,0) circle [radius=1.2pt] node[below right]{$\vect{0}$};
  \end{tikzpicture}
\end{center}
As the picture shows, the linear combinations of $\vect{u}$ and
$\vect{v}$ form a $2$-dimensional plane through the origin. We say
that this plane is \textbf{spanned}%
\index{span}%
\index{vector!span} by the vectors $\vect{u}$ and $\vect{v}$.  This
concept generalizes to more than two vectors. For example, three
vectors may span a $3$-dimensional space (although sometimes, they
span only a $2$-dimensional space, or even a line). This motivates the
following definition.

\begin{definition}{Span of a set of vectors}{span}
  The set of all linear combinations of the vectors
  $\vect{u}_1, \ldots,\vect{u}_k$ in $\R^n$ is known as the
  \textbf{span}%
  \index{span}%
  \index{vector!span} of these vectors and is written as
  $\sspan\set{\vect{u}_1,\ldots,\vect{u}_k}$. Using set notation, we
  can write
  \begin{equation*}
    \sspan\set{\vect{u}_1,\ldots,\vect{u}_k}
    ~=~ \set{a_1\vect{u}_1+\ldots+a_k\vect{u}_k \mid a_1,\ldots,a_k\in\R}.
  \end{equation*}
\end{definition}

\begin{example}{Vectors in a span}{vector-in-span}
  Let $\vect{u}=\begin{mymatrix}{r} 1 \\ 1 \\ 1 \end{mymatrix}$ and
  $\vect{v}=\begin{mymatrix}{r} 3 \\ 2 \\ 1 \end{mymatrix}$. Which
  of the following vectors are elements of
  $\sspan\set{\vect{u},\vect{v}}$?
  \begin{equation*}
    (a)\quad\vect{w} = \begin{mymatrix}{r} 2 \\ 3 \\ 4 \end{mymatrix},
    \qquad
    (b)\quad\vect{z} = \begin{mymatrix}{r} 1 \\ 1 \\ 2 \end{mymatrix}.
  \end{equation*}
\end{example}

\begin{solution}
  (a) For a vector to be in $\sspan\set{\vect{u},\vect{v}}$, it must
  be a linear combination of $\vect{u}$ and $\vect{v}$. Therefore,
  $\vect{w}\in\sspan\set{\vect{u},\vect{v}}$ if and only if we can
  find find scalars $a,b$ such that
  $a\,\vect{u} + b\,\vect{v} = \vect{w}$. We must therefore solve the
  equation
  \begin{equation*}
    a \begin{mymatrix}{r} 1 \\ 1 \\ 1 \end{mymatrix}
    + b \begin{mymatrix}{r} 3 \\ 2 \\ 1 \end{mymatrix}
    = \begin{mymatrix}{r} 2 \\ 3 \\ 4 \end{mymatrix}.
  \end{equation*}
  We write this as an augmented matrix and solve.
  \begin{equation*}
    \begin{mymatrix}{rr|r}
      1 & 3 & 2 \\
      1 & 2 & 3 \\
      1 & 1 & 4 \\
    \end{mymatrix}
    \roweq\ldots\roweq
    \begin{mymatrix}{rr|r}
      1 & 0 & 5 \\
      0 & 1 & -1 \\
      0 & 0 & 0 \\
    \end{mymatrix}.
  \end{equation*}
  The solution is $a=5$ and $b=-1$. This means that
  $\vect{w} = 5\vect{u} + (-1)\vect{v}$. Therefore, $\vect{w}$ is an
  element of $\sspan\set{\vect{u},\vect{v}}$.

  (b) We repeat the same method with the vector $\vect{z}$. This time,
  we have to find $a,b$ such that
  $a\,\vect{u} + b\,\vect{v} = \vect{z}$. The system of equations is
  \begin{equation*}
    \begin{mymatrix}{rr|r}
      1 & 3 & 1 \\
      1 & 2 & 1 \\
      1 & 1 & 2 \\
    \end{mymatrix}
    \roweq\ldots\roweq
    \begin{mymatrix}{rr|r}
      1 & 3 & 1 \\
      0 & -1 & 0 \\
      0 & 0 & 1 \\
    \end{mymatrix},
  \end{equation*}
  which is inconsistent. Therefore, there is no solution. We conclude
  that $\vect{z}$ is not an element of
  $\sspan\set{\vect{u},\vect{v}}$.
\end{solution}

\begin{example}{Describing the span}{describe-span}
  Describe the span of the vectors
  $\vect{u}=\begin{mymatrix}{r} 1 \\ 1 \\ 1 \end{mymatrix}$ and
  $\vect{v}=\begin{mymatrix}{r} 3 \\ 2 \\ 1 \end{mymatrix}$ in
  $\R^3$.
\end{example}

\begin{solution}
  Let $\vect{w} = \mat{x, y, z}^T$ be any vector. Proceeding as in the
  previous example, we know that $\vect{w}$ is an element of
  $\sspan\set{\vect{u},\vect{v}}$ if and only if the equation
  \begin{equation*}
    a \begin{mymatrix}{r} 1 \\ 1 \\ 1 \end{mymatrix}
    + b \begin{mymatrix}{r} 3 \\ 2 \\ 1 \end{mymatrix}
    = \begin{mymatrix}{r} x \\ y \\ z \end{mymatrix}
  \end{equation*}
  is consistent. Note that the variables of this equation are $a,b$;
  we regard $x,y,z$ as constants for the moment. We write the augmented
  matrix of this system and reduce to {\ef}:
  \begin{equation*}
    \begin{mymatrix}{rr|c}
      1 & 3 & x \\
      1 & 2 & y \\
      1 & 1 & z \\
    \end{mymatrix}
    \stackrel{R_2\leftarrow R_2-R_1}{\stackrel{R_2\leftarrow R_3-R_1}{\roweq}}
    \begin{mymatrix}{rr|c}
      1 & 3 & x \\
      0 & -1 & y-x \\
      0 & -2 & z-x \\
    \end{mymatrix}
    \stackrel{R_3\leftarrow R_3-2R_2}{\roweq}
    \begin{mymatrix}{rr|c}
      1 & 3 & x \\
      0 & -1 & y-x \\
      0 & 0 & (z-x)-2(y-x) \\
    \end{mymatrix},
  \end{equation*}
  From the {\ef}, we see that the system is consistent if and only if
  $(z-x)-2(y-x)=0$, or equivalently $x - 2y + z = 0$. Therefore, the
  vector $\vect{w}$ is in $\sspan\set{\vect{u},\vect{v}}$ if and only
  if $x - 2y + z = 0$. In other words, the span of $\vect{u}$ and
  $\vect{v}$ is the plane $x - 2y + z = 0$.
\end{solution}

\begin{example}{Span of redundant vectors}{redundant-span}
  Let $\vect{u}=\begin{mymatrix}{r} 1 \\ 1 \\ 1 \end{mymatrix}$,
  $\vect{v}=\begin{mymatrix}{r} 3 \\ 2 \\ 1 \end{mymatrix}$, and
  $\vect{w}=\begin{mymatrix}{r} 11 \\ 8 \\ 5 \end{mymatrix}$.
  Show that
  $\sspan\set{\vect{u},\vect{v},\vect{w}} =
  \sspan\set{\vect{u},\vect{v}}$.
\end{example}

\begin{solution}
  Observe that $\vect{w} = 2\,\vect{u}+3\,\vect{v}$. Therefore,
  $\vect{w}$ is already in the span of $\vect{u}$ and $\vect{v}$.  Two
  sets are equal if they have the same elements, i.e., each element of
  the first set is an element of the second set and vice
  versa. Therefore, to show
  $\sspan\set{\vect{u},\vect{v},\vect{w}} =
  \sspan\set{\vect{u},\vect{v}}$, we must show (a) that every element
  of $\sspan\set{\vect{u},\vect{v},\vect{w}}$ is an element of
  $\sspan\set{\vect{u},\vect{v}}$ and (b) vice versa.

  (a) Let $\vect{z}$ be an arbitrary element of
  $\sspan\set{\vect{u},\vect{v},\vect{w}}$. Then, by definition of
  span, there exist scalars $a,b,c$ such that
  \begin{eqnarray*}
    \vect{z} &=& a\,\vect{u} + b\,\vect{v} + c\,\vect{w}.
  \end{eqnarray*}
  But as observed above, we have $\vect{w} = 2\,\vect{u}+3\,\vect{v}$,
  and therefore we can also write
  \begin{eqnarray*}
    \vect{z}
    &=& a\,\vect{u} + b\,\vect{v} + c(2\,\vect{u}+3\,\vect{v}) \\
    &=& (a+2c)\vect{u} + (b+3c)\vect{v}.
  \end{eqnarray*}
  It follows that $\vect{z}$ is a linear combination of $\vect{u}$ and
  $\vect{v}$, and therefore, $\vect{z}\in \sspan\set{\vect{u},\vect{v}}$.

  (b) Clearly every linear combination of $\vect{u}$ and $\vect{v}$ is
  also a linear combination of $\vect{u}$, $\vect{v}$, and $\vect{w}$,
  namely, taking the coefficient of $\vect{w}$ to be $0$. Therefore,
  every element of $\sspan\set{\vect{u},\vect{v}}$ is an element of
  $\sspan\set{\vect{u},\vect{v},\vect{w}}$.

  Because we have shown that every element of
  $\sspan\set{\vect{u},\vect{v},\vect{w}}$ is an element of
  $\sspan\set{\vect{u},\vect{v}}$ and vice versa, it follows that
  $\sspan\set{\vect{u},\vect{v},\vect{w}}$ and
  $\sspan\set{\vect{u},\vect{v}}$ are the same set of vectors.
\end{solution}

In the situation of the last example, we say that the vector
$\vect{w}$ is \textbf{redundant}%
\index{redundant vector}%
\index{vector!redundant}; it does not contribute anything to
$\sspan\set{\vect{u},\vect{v},\vect{w}}$. Geometrically, the three
vectors $\vect{u}$, $\vect{v}$, and $\vect{w}$ lie in a plane. Since
the two vectors $\vect{u}$ and $\vect{v}$ are sufficient to span this
plane, the third vector $\vect{w}$ is not really needed. We will study
this situation more systematically in the next section.

\begin{example}{Span of the empty set}{span-empty-set}
  We talked about the span of $k$ vectors
  $\vect{u}_1,\ldots,\vect{u}_k$.  What if $k=0$? What is
  the span of an empty set of vectors?
\end{example}

\begin{solution}
  Consider what happens when we compute the sum of three numbers. We
  usually write this as $b_1+b_2+b_3$. We can also compute the sum of
  three numbers by starting from $0$ and then adding each of the three
  numbers to it. I.e., the sum can be computed as
  $0+b_1+b_2+b_3$. Similarly, we can write the sum of two numbers as
  $0+b_1+b_2$, and the sum of just one number as $0+b_1$. Continuing
  the pattern, it follows that the sum of zero numbers should be $0$:
  \begin{center}
    \begin{tabular}{ll}
      Sum of 3 numbers: & $0+b_1+b_2+b_3$. \\
      Sum of 2 numbers: & $0+b_1+b_2$. \\
      Sum of 1 numbers: & $0+b_1$. \\
      Sum of 0 numbers: & $0$. \\
    \end{tabular}
  \end{center}
  The sum of zero numbers is also called the \textbf{empty sum}%
  \index{empty sum}. It is equal to the unit of addition, i.e., $0$.
  By an analogous argument, the empty sum of vectors is equal to the
  unit of vector addition, i.e., to the zero vector $\vect{0}$%
  \index{zero vector}%
  \index{vector!zero vector}.  In general, if we have $k$ vectors
  $\vect{u}_1,\ldots,\vect{u}_k$, the span consists of all vectors of
  the form $a_1\vect{u}_1+\ldots+a_k\vect{u}_k$, which is a sum of $k$
  vectors.  In case $k=0$, the span consists only of the empty sum,
  i.e., the zero vector $\vect{0}$, which we also call the
  \textbf{empty linear combination}%
  \index{empty linear combination}%
  \index{vector!empty linear combination}%
  \index{linear combination!empty}. Therefore, the span of the empty
  set of vectors is $\set{\vect{0}}$.
\end{solution}