\chapter{Arithmetic}
\label{chap:Arithmetic}

In this chapter, we will learn how to perform basic
arithmetic with libzahl: addition, subtraction,
multiplication, division, modulus, exponentiation,
and sign manipulation. \secref{sec:Division} is of
special importance.

\vspace{1cm}
\minitoc

\newpage
\section{Addition}
\label{sec:Addition}

To calculate the sum of two terms, we perform
addition using {\tt zadd}.

\vspace{1em}
$r \gets a + b$
\vspace{1em}

\noindent
is written as

\begin{alltt}
   zadd(r, a, b);
\end{alltt}

libzahl also provides {\tt zadd\_unsigned} which
has slightly lower overhead. The calculates the
sum of the absolute values of two integers.

\vspace{1em}
$r \gets \lvert a \rvert + \lvert b \rvert$
\vspace{1em}

\noindent
is written as

\begin{alltt}
   zadd_unsigned(r, a, b);
\end{alltt}

\noindent
{\tt zadd\_unsigned} has lower overhead than
{\tt zadd} because it does not need to inspect
or change the sign of the input, the low-level
function that performs the addition inherently
calculates the sum of the absolute values of
the input.

In libzahl, addition is implemented using a
technique called ripple-carry. It is derived
from that observation that

\vspace{1em}
$f : \textbf{Z}_n, \textbf{Z}_n \rightarrow \textbf{Z}_n$
\\ \indent
$f : a, b \mapsto a + b + 1$
\vspace{1em}

\noindent
only wraps at most once, that is, the
carry cannot exceed 1. CPU:s provide an
instruction specifically for performing
addition with ripple-carry over multiple words,
adds twos numbers plus the carry from the
last addition. libzahl uses assembly to
implement this efficiently. If, however, an
assembly implementation is not available for
the on which machine it is running, libzahl
implements ripple-carry less efficiently
using compiler extensions that check for
overflow. In the event that neither an
assembly implementation is available nor
the compiler is known to support this
extension, it is implemented using inefficient
pure C code. This last resort manually
predicts whether an addition will overflow;
this could be made more efficient, by never
using the highest bit in each character,
except to detect overflow. This optimisation
is however not implemented because it is
not deemed important enough and would
be detrimental to libzahl's simplicity.

{\tt zadd} and {\tt zadd\_unsigned} support
in-place operation:

\begin{alltt}
   zadd(a, a, b);
   zadd(b, a, b);           \textcolor{c}{/* \textrm{should be avoided} */}
   zadd_unsigned(a, a, b);
   zadd_unsigned(b, a, b);  \textcolor{c}{/* \textrm{should be avoided} */}
\end{alltt}

\noindent
Use this whenever possible, it will improve
your performance, as it will involve less
CPU instructions for each character addition
and it may be possible to eliminate some
character additions.

\newpage
\section{Subtraction}
\label{sec:Subtraction}

TODO % zsub zsub_unsigned

\newpage
\section{Multiplication}
\label{sec:Multiplication}

TODO % zmul zmodmul

\newpage
\section{Division}
\label{sec:Division}

To calculate the quotient or modulus of two integers,
use either of

\begin{alltt}
   void zdiv(z_t quotient, z_t dividend, z_t divisor);
   void zmod(z_t remainder, z_t dividend, z_t divisor);
   void zdivmod(z_t quotient, z_t remainder,
                z_t dividend, z_t divisor);
\end{alltt}

\noindent
These function \emph{do not} allow {\tt NULL}
for the output parameters: {\tt quotient} and
{\tt remainder}. The quotient and remainder are
calculated simultaneously and indivisibly, hence
{\tt zdivmod} is provided to calculated both; if
you are only interested in the quotient or only
interested in the remainder, use {\tt zdiv} or
{\tt zmod}, respectively.

These functions calculate a truncated quotient.
That is, the result is rounded towards zero. This
means for example that if the quotient is in
$(-1,~1)$, {\tt quotient} gets 0. That is, this % TODO try to clarify
would not be the case for one of the sides of zero.
For example, if the quotient would have been
floored, negative quotients would have been rounded
away from zero. libzahl only provides truncated
division.

The remainder is defined such that $n = qd + r$ after
calling {\tt zdivmod(q, r, n, d)}. There is no
difference in the remainer between {\tt zdivmod}
and {\tt zmod}. The sign of {\tt d} has no affect
on {\tt r}, {\tt r} will always, unless it is zero,
have the same sign as {\tt n}.

There are of course other ways to define integer
division (that is, \textbf{Z} being the codomain)
than as truncated division. For example integer
divison in Python is floored — yes, you did just
read `integer divison in Python is floored,' and
you are correct, that is not the case in for
example C. Users that want another definition
for division than truncated division are required
to implement that themselves. We will however
lend you a hand.

\begin{alltt}
   #define isneg(x) (zsignum(x) < 0)
   static z_t one;
   \textcolor{c}{__attribute__((constructor)) static}
   void init(void) \{ zinit(one), zseti(one, 1); \}

   static int
   cmpmag_2a_b(z_t a, z_b b)
   \{
       int r;
       zadd(a, a, a), r = zcmpmag(a, b), zrsh(a, a, 1);
       return r;
   \}
\end{alltt}

% Floored division
\begin{alltt}
   void \textcolor{c}{/* \textrm{All arguments must be unique.} */}
   divmod_floor(z_t q, z_t r, z_t n, z_t d)
   \{
       zdivmod(q, r, n, d);
       if (!zzero(r) && isneg(n) != isneg(d))
           zsub(q, q, one), zadd(r, r, d);
   \}
\end{alltt}

% Ceiled division
\begin{alltt}
   void \textcolor{c}{/* \textrm{All arguments must be unique.} */}
   divmod_ceiling(z_t q, z_t r, z_t n, z_t d)
   \{
       zdivmod(q, r, n, d);
       if (!zzero(r) && isneg(n) == isneg(d))
           zadd(q, q, one), zsub(r, r, d);
   \}
\end{alltt}

% Division with round half aways from zero
% This rounding method is also called:
%    round half toward infinity
%    commercial rounding
\begin{alltt}
   /* \textrm{This is how we normally round numbers.} */
   void \textcolor{c}{/* \textrm{All arguments must be unique.} */}
   divmod_half_from_zero(z_t q, z_t r, z_t n, z_t d)
   \{
       zdivmod(q, r, n, d);
       if (!zzero(r) && cmpmag_2a_b(r, d) >= 0) \{
           if (isneg(n) == isneg(d))
               zadd(q, q, one), zsub(r, r, d);
           else
               zsub(q, q, one), zadd(r, r, d);
       \}
   \}
\end{alltt}

\noindent
Now to the weird ones that will more often than
not award you a face-slap. % Had positive punishment
% been legal or even mildly pedagogical. But I would
% not put it past Coca-Cola.

% Division with round half toward zero
% This rounding method is also called:
%     round half away from infinity
\begin{alltt}
   void \textcolor{c}{/* \textrm{All arguments must be unique.} */}
   divmod_half_to_zero(z_t q, z_t r, z_t n, z_t d)
   \{
       zdivmod(q, r, n, d);
       if (!zzero(r) && cmpmag_2a_b(r, d) > 0) \{
           if (isneg(n) == isneg(d))
               zadd(q, q, one), zsub(r, r, d);
           else
               zsub(q, q, one), zadd(r, r, d);
       \}
   \}
\end{alltt}

% Division with round half up
% This rounding method is also called:
%     round half towards positive infinity
\begin{alltt}
   void \textcolor{c}{/* \textrm{All arguments must be unique.} */}
   divmod_half_up(z_t q, z_t r, z_t n, z_t d)
   \{
       int cmp;
       zdivmod(q, r, n, d);
       if (!zzero(r) && (cmp = cmpmag_2a_b(r, d)) >= 0) \{
           if (isneg(n) == isneg(d))
               zadd(q, q, one), zsub(r, r, d);
           else if (cmp)
               zsub(q, q, one), zadd(r, r, d);
       \}
   \}
\end{alltt}

% Division with round half down
% This rounding method is also called:
%     round half towards negative infinity
\begin{alltt}
   void \textcolor{c}{/* \textrm{All arguments must be unique.} */}
   divmod_half_down(z_t q, z_t r, z_t n, z_t d)
   \{
       int cmp;
       zdivmod(q, r, n, d);
       if (!zzero(r) && (cmp = cmpmag_2a_b(r, d)) >= 0) \{
           if (isneg(n) != isneg(d))
               zsub(q, q, one), zadd(r, r, d);
           else if (cmp)
               zadd(q, q, one), zsub(r, r, d);
       \}
   \}
\end{alltt}

% Division with round half to even
% This rounding method is also called:
%     unbiased rounding        (really stupid name)
%     convergent rounding      (also quite stupid name)
%     statistician's rounding
%     Dutch rounding
%     Gaussian rounding
%     odd–even rounding
%     bankers' rounding
% It is the default rounding method used in IEEE 754.
\begin{alltt}
   void \textcolor{c}{/* \textrm{All arguments must be unique.} */}
   divmod_half_to_even(z_t q, z_t r, z_t n, z_t d)
   \{
       int cmp;
       zdivmod(q, r, n, d);
       if (!zzero(r) && (cmp = cmpmag_2a_b(r, d)) >= 0) \{
           if (cmp || zodd(q)) \{
               if (isneg(n) != isneg(d))
                   zsub(q, q, one), zadd(r, r, d);
               else
                   zadd(q, q, one), zsub(r, r, d);
           \}
       \}
   \}
\end{alltt}

% Division with round half to odd
\newpage
\begin{alltt}
   void \textcolor{c}{/* \textrm{All arguments must be unique.} */}
   divmod_half_to_odd(z_t q, z_t r, z_t n, z_t d)
   \{
       int cmp;
       zdivmod(q, r, n, d);
       if (!zzero(r) && (cmp = cmpmag_2a_b(r, d)) >= 0) \{
           if (cmp || zeven(q)) \{
               if (isneg(n) != isneg(d))
                   zsub(q, q, one), zadd(r, r, d);
               else
                   zadd(q, q, one), zsub(r, r, d);
           \}
       \}
   \}
\end{alltt}

% Other standard methods include stochastic rounding
% and round half alternatingly, and what is is
% New Zealand called “Swedish rounding”, which is
% no longer used in Sweden, and is just normal round
% half aways from zero but with 0.5 rather than
% 1 as the integral unit, and is just a special case
% of a more general rounding method.

Currently, libzahl uses an almost trivial division
algorithm. It operates on positive numbers. It begins
by left-shifting the divisor as much as possible with
letting it exceed the dividend. Then, it subtracts
the shifted divisor from the dividend and add 1,
left-shifted as much as the divisor, to the quotient.
The quotient begins at 0. It then right-shifts
the shifted divisor as little as possible until
it no longer exceeds the diminished dividend and
marks the shift in the quotient. This process is
repeated until the unshifted divisor is greater
than the diminished dividend. The final diminished
dividend is the remainder.

\newpage
\section{Exponentiation}
\label{sec:Exponentiation}

Exponentiation refers to raising a number to
a power. libzahl provides two functions for
regular exponentiation, and two functions for
modular exponentiation. libzahl also provides
a function for raising a number to the second
power, see \secref{sec:Multiplication} for
more details on this. The functions for regular
exponentiation are

\begin{alltt}
   void zpow(z_t power, z_t base, z_t exponent);
   void zpowu(z_t, z_t, unsigned long long int);
\end{alltt}

\noindent
They are identical, except {\tt zpowu} expects
an intrinsic type as the exponent. Both functions
calculate

\vspace{1em}
$power \gets base^{exponent}$
\vspace{1em}

\noindent
The functions for modular exponentiation are
\begin{alltt}
   void zmodpow(z_t, z_t, z_t, z_t modulator);
   void zmodpowu(z_t, z_t, unsigned long long int, z_t);
\end{alltt}

\noindent
They are identical, except {\tt zmodpowu} expects
and intrinsic type as the exponent. Both functions
calculate

\vspace{1em}
$power \gets base^{exponent} \mod modulator$
\vspace{1em}

The sign of {\tt modulator} does not affect the
result, {\tt power} will be negative if and only
if {\tt base} is negative and {\tt exponent} is
odd, that is, under the same circumstances as for
{\tt zpow} and {\tt zpowu}.

These four functions are implemented using
exponentiation by squaring. {\tt zmodpow} and
{\tt zmodpowu} are optimised, they modulate
results for multiplication and squaring at
every multiplication and squaring, rather than
modulating the result at the end. Exponentiation
by modulation is a very simple algorithm which
can be expressed as a simple formula

\vspace{-1em}
\[ \hspace*{-0.4cm}
    a^b =
    \prod_{k \in \textbf{Z}_{+} ~:~ \left \lfloor \frac{b}{2^k} \hspace*{-1ex} \mod 2 \right \rfloor = 1}
    a^{2^k}
\]

\noindent
This is a natural extension to the
observations\footnote{The first of course being
that any non-negative number can be expressed
with the binary positional system. The latter
should be fairly self-explanatory.}

\vspace{-1em}
\[ \hspace*{-0.4cm}
    \forall b \in \textbf{Z}_{+} \exists B \subset \textbf{Z}_{+} : b = \sum_{i \in B} 2^i
    ~~~~ \textrm{and} ~~~~
    a^{\sum x} = \prod a^x.
\]

\noindent
The algorithm can be expressed in psuedocode as

\vspace{1em}
\hspace{-2.8ex}
\begin{minipage}{\linewidth}
\begin{algorithmic}
    \STATE $r, f \gets 1, a$
    \WHILE{$b \neq 0$}
      \STATE $r \gets r \cdot f$ {\bf unless} $2 \vert b$
      \STATE $f \gets f^2$ \textcolor{c}{\{$f \gets f \cdot f$\}}
      \STATE $b \gets \lfloor b / 2 \rfloor$
    \ENDWHILE
    \RETURN $r$
\end{algorithmic}
\end{minipage}
\vspace{1em}

\noindent
Modular exponentiation ($a^b \mod m$) by squaring can be
expressed as

\vspace{1em}
\hspace{-2.8ex}
\begin{minipage}{\linewidth}
\begin{algorithmic}
    \STATE $r, f \gets 1, a$
    \WHILE{$b \neq 0$}
      \STATE $r \gets r \cdot f \hspace*{-1ex}~ \mod m$ \textbf{unless} $2 \vert b$
      \STATE $f \gets f^2 \hspace*{-1ex}~ \mod m$
      \STATE $b \gets \lfloor b / 2 \rfloor$
    \ENDWHILE
    \RETURN $r$
\end{algorithmic}
\end{minipage}
\vspace{1em}

{\tt zmodpow} does \emph{not} calculate the
modular inverse if the exponent is negative,
rather, you should expect the result to be
1 and 0 depending of whether the base is 1
or not 1.

\newpage
\section{Sign manipulation}
\label{sec:Sign manipulation}

libzahl provides two functions for manipulating
the sign of integers:

\begin{alltt}
   void zabs(z_t r, z_t a);
   void zneg(z_t r, z_t a);
\end{alltt}

{\tt zabs} stores the absolute value of {\tt a}
in {\tt r}, that is, it creates a copy of
{\tt a} to {\tt r}, unless {\tt a} and {\tt r}
are the same reference, and then removes its sign;
if the value is negative, it becomes positive.

\vspace{1em}
\(
    r \gets \lvert a \rvert =
    \left \lbrace \begin{array}{rl}
        -a & \quad \textrm{if}~a \le 0 \\
        +a & \quad \textrm{if}~a \ge 0 \\
    \end{array} \right .
\)
\vspace{1em}

{\tt zneg} stores the negated of {\tt a}
in {\tt r}, that is, it creates a copy of
{\tt a} to {\tt r}, unless {\tt a} and {\tt r}
are the same reference, and then flips sign;
if the value is negative, it becomes positive,
if the value is positive, it becomes negative.

\vspace{1em}
\(
    r \gets -a
\)
\vspace{1em}

Note that there is no function for

\vspace{1em}
\(
    r \gets -\lvert a \rvert =
    \left \lbrace \begin{array}{rl}
         a & \quad \textrm{if}~a \le 0 \\
        -a & \quad \textrm{if}~a \ge 0 \\
    \end{array} \right .
\)
\vspace{1em}

\noindent
calling {\tt zabs} followed by {\tt zneg}
should be sufficient for most users:

\begin{alltt}
   #define my_negabs(r, a)  (zabs(r, a), zneg(r, r))
\end{alltt}


\documentclass[../../main.tex]{subfiles}

\begin{document}

\subsection{Motivation}

It is considered a problem of constructing a phase trajectory of a video file using tensor-based singular spectrum analysis method. Usually, phase trajectory of a time series is constructed using SSA method that is applied to a matrix with two indices \cite{golyandina, usmanova}. In this work it is proposed to apply tensor-based SSA method that constructs a trajectory matrix that has 3 indices, then apply HOSVD method for extracting principal components.

\subsection{Problem statement}

Given a sequence of images that composes a gray-scale video. Let us consider this as a time series system. Each time series is a sequence of a particular pixel brightness:

$$F^{(k)} = (f_j^{(k)})_{j=0}^{N-1}, \quad k = 1, \dots, hw,$$

\noindent
where $h$ is the video height, $w$ is the video width, $hw$ is a number of signals with length $N$.

The purpose is to construct a phase trajectory of the time series system and make a forecast $\hat{\mathbf{X}}^\prime$ using tensor-based SSA method and HOSVD.

\subsection{Problem solution}

It is proposed to solve a problem with tensor-based singular spectrum analysis and make a forecast using HOSVD.

\textbf{1st step: Embedding}

Let $L$ be a window length, $1 < L < N$. The embedding procedure forms $K = N-L+1$ lagged vectors for every time series:

$$X_l^{(k)} = (f_{l-1}^{(k)}, \dots, f_{l+L-2}^{(k)})^\top.$$

The trajectory matrix of the time series system $F^{(1)}, \dots, F^{(hw)}$ is a tensor that has a form

$$\hat{\mathbf{X}} = (\mathbf{X}^{(1)}, \dots, \mathbf{X}^{(hw)}),$$

\noindent
where $\mathbf{X}^{(k)} = (X_l^{(k)})_{l=0}^{K}$.

\textbf{2nd step: HOSVD}

Truncated HOSVD is performed:

$$\hat{\mathbf{X}} = \mathbf{S} \times_1 U_1 \times_2 U_2 \times_3 U_3,$$

\noindent
where $\mathbf{S}$ is a core tensor, $U_1, U_2, U_3$ are matrices with unitary columns containing a basis of the left singular vectors corresponding to the nonzero singular values of the standard factor-$k$ flattening $X_{(k)}$ of $X$ for $k = 1, 2, 3$.

The HOSVD of $\hat{\mathbf{X}}$ can be represent as

$$\hat{\mathbf{X}} = \hat{\mathbf{X}}_1 + \dots + \hat{\mathbf{X}}_d,$$

\noindent
where $\hat{\mathbf{X}}_i$ has rank equal to 1.

\textbf{3rd step: Grouping}

The grouping procedure partitions the
set of indices $\{1, \dots, d\}$ into $m$ disjoint subsets $I_1, \dots, I_m$. Let $I = {i_1, \dots, i_p}$. Then the resultant matrix $\hat{\mathbf{X}}$ corresponding to the group $I$ is defined as $\hat{\mathbf{X}}_I = \hat{\mathbf{X}}_{i_1} + \dots + \hat{\mathbf{X}}_{i_p}$. Thus, we have the grouped decomposition:

$$\hat{\mathbf{X}} = \hat{\mathbf{X}}_{I_1} + \dots + \hat{\mathbf{X}}_{I_m}.$$

\textbf{4th step: Diagonal averaging}

The last step is in a sense opposite to the first step and transforms each matrix of the grouped decomposition into a system of new (reconstructed) series of length $N$ by hankelization-like procedure.

\subsection{Code analysis}

Code for HOSVD decomposition was taken from GitHub repository\footnote{https://github.com/hottbox/hottbox}. Code of the computational experiment can be found in the repository\footnote{https://github.com/gorpinich-m/Math-methods-of-forecasting}.

\subsection{Experiment}

\begin{figure}[h!]
\centering
\includegraphics[width=0.8\textwidth]{sections/gorpinich/trajectories_base.pdf}
\caption{Some description}
\label{fig:1}
\end{figure}

Fig. \ref{fig:1} plots phase trajectories of 4 pixels of the basic video.

\begin{figure}[h!]
\centering
\includegraphics[width=0.8\textwidth]{sections/gorpinich/mario_trajectory.pdf}
\caption{Some description}
\label{fig:2}
\end{figure}

Fig. \ref{fig:1} plots phase trajectories of 1000 pixels of the video.

\begin{figure}[h!]
\centering
\includegraphics[width=0.6\textwidth]{sections/gorpinich/ssa_base.pdf}
\caption{Some description}
\label{fig:3}
\end{figure}

Fig. \ref{fig:3} plots SSA decomposition of the basic video. We can see that the first component is a trend and other 2 components depict periodicity ans seasonality.

\begin{figure}[h!]
\centering
\includegraphics[width=0.6\textwidth]{sections/gorpinich/ssa_mario.pdf}
\caption{Some description}
\label{fig:4}
\end{figure}

Fig. \ref{fig:4} plots SSA decomposition of the more complex video.

\end{document}


\section{Introduction}
The bread-first search is an algorithm visiting a graph in amplitude. It starts from a node,
often called the root or source node, and continues visiting all its descendants level
by level, whereas the $i$-th level will contain all the nodes at a distance
$i$ from the root.
For each node visited the algorithm checks its label to count the occurrences of the target label.
The assumption underlying all the work is that the input to the algorithm is a direct and
acyclic graph. In addition, for the sake of clarity, the notation used is summarized below:
\subsection{Notation}
Let $\mathcal{G} = (V, E)$, $|V| = n$ is the number of node and $|E| = m$ is the number of edges.
For all the node $v \in V$:
\begin{itemize}
    \item $\mathcal{N}(v) = \{u : (v, u) \in E\}$ is the neighborhood of $v$;
    \item $k_{in}(v) = |\{e: e=(u, v) \in E\}|$ is the in-degree of $v$;
    \item $k_{out}(v) = |\mathcal{N}(v)|$ is the out-degree of $v$;
    \item $k(v) = k_{in}(v) + k_{out}(v)$ is the degree of $v$.
    \item $d(u, v)$ is the distance from $u$ to $v$, i.e. the minimum path from $u$ to $v$.
    \item $C(v)$ is the clustering coefficient, i.e.e $\frac{2 \cdot L_i}{k_i\cdot(k_i - 1)}$, where $L_i$ represents the number of edges between the $k_i$ neighbors of node i.
\end{itemize}
Using the node-focus notation above,
 it is possible to define general properties for the graph:
\begin{itemize}
    \item $\bar{k}(G)$ is the average degree;
    \item $\bar{d}(G)$ is the average distance;
    \item $\bar{h}(G)$ is the diameter, hence the maximum distance among any two nodes.
\end{itemize}
In addition, we can define
the graph inducted by a node $v$ as the subgraph $G'=(V', E')$ containing all the nodes reachable from v.
\subsection{Preliminary analysis}
\subsubsection{Data structure}
Before entering in the algorithmic details let's first introduce the data structure used.
There are many ways to represent a graph; among these the main ones are the adjacency list and the adjacency
matrix. The choices among them is mainly a matter of the usage of the adjacency information and the
expected nature of the graph. As for every node $v$ of the graph, induced by the root, it will be necessary
to go through each node $u \in \mathcal{N}(v)$, the adjacency list is way more efficient since for each
node $v$ the listing of $\mathcal{N}(v)$ takes a time proportional to $k_{out}(v)$, which is thus optimal.
Moreover, if the expected input of the algorithm are "real" graphs, since they are very sparse, the representation as a adjacency list is way
more efficient in terms of space complexity.
\\
In particular, the solution implements a graph $G$ as a vector of nodes.
 The node is a pair, the first component is the label of the node, while the second
 component is the adjacency list containing the indices of the nodes in the graph.
In addition, to mark each node as visited, a vector of boolean is used (having a space complexity of $O(log_2n)$). This vector is called in the report
\textit{vector of visits}.

 \subsubsection{Sequential version}
\label{sub:seq-version}
The sequential version is a straightforward implementation of the problem
description. It uses two vectors $F_i$ and $F_{i+1}$: the former is the frontier
while the latter is used to add new nodes and therefore represents the frontier
to be used in the next iteration. At the beginning, the algorithm initializes the vector of visits,
 the frontier with the neighborhood of the source node and marks each child as visited. Then it marks the source node
as visited and updates occurrences if needed (i.e. if the root has the searched label). After this first phase,
 the algorithm starts the first loop which iterates on each level checking that $F_i$ is not empty.
 Subsequently, for each level, it goes through each node $v \in F_i$, updates occurrences if needed and
 then goes through each node $u \in \mathcal{N}(v)$.
 For each node $u$ in the neighborhood, if the node $u$ is not marked as visited,
 it adds $u$ to $F_{i+1}$. After having exhausted all the nodes of $F_i$, the algorithm
 swaps $F_i$ with $F_{i+1}$ and then clears $F_{i+1}$.


\section{Results and Discussion}
\label{sec:Results_and_Discussion}
This section contains lists of all important results and summarizes the most important things.

\begin{table}[H]
	\centering
	\renewcommand{\arraystretch}{1.3}
	\begin{tabular}{r||c|c|c}
		& \textbf{Measured} ($\mu$m) & \textbf{True Value} ($\mu$m) & \textbf{Deviation} (\%) \\
		\hline\hline
		\textbf{Slit 1} & 39.41 $\pm$ 0.93 & 40 & -1.5 \\
		\textbf{Slit 2} & 101.26 $\pm$ 6.77 & 100 & +1.3 \\ \hline
		\textbf{Anti-Slit 1} & 239.10 $\pm$ 23.34 & 230 & +4.0 \\
		\textbf{Anti-Slit 2} & 123.39 $\pm$ 6.80 & 124 & -0.5 \\ \hline
		\textbf{Circular Aperture 1} & 151.39 $\pm$ 10.24 & 150 & +0.9 \\
		\textbf{Circular Aperture 2} & 101.62 $\pm$ 4.60 & 100 & +1.6 \\ \hline
		\textbf{Cross-Grid 1} & 28.53 $\pm$ 0.57 & 28 & +1.9 \\
		\textbf{Cross-Grid 2} & 50.22 $\pm$ 0.30 & 50 & +0.44 \\ \hline
	\end{tabular}
	\caption{Summary of all Results}
	\label{tab:results}
\end{table}

Table \ref{tab:results} shows that all the calculated mean values are really close to their true values (all values are within < 4 \% deviation). The other important thing is to check, wheter their true value lies between the measured value and its uncertainty. This is shown in the following figure \ref{fig:Graphical_Comparison}:

\begin{figure}[H]
	\centering
	\includegraphics[scale=0.94]{Graphical_Comparison}
	\caption{Graphical Comparison}
	\label{fig:Graphical_Comparison}
\end{figure}

Figure \ref{fig:Graphical_Comparison} shows a graphical comparison between the calculated values (black cross with red uncertainty bar) and their respective true values (blue cross). The display order is the same as in the above table \ref{tab:results}. Although it is difficult to see for the values with a small error bar, \textbf{all their true values lie between their calculated values and their uncertainties}.


\documentclass{beamer}

\usepackage{beamerthemevictor,comment,verbatim,graphicx,rotating}

\input slidemacs
\input{tutmacs}
\input idxmacs
\input logicmacs

\begin{document}

\title{Logic and lambda calculus in \TeX}
\author{Victor Eijkhout}
\date{Notes for CS 594 -- Fall 2004}

\frame{\titlepage}

\section{Introduction}
\subsection{Markup}

\frame[containsverbatim]{
  \frametitle{}
\begin{itemize}
\item Expansion is very powerful
\item Implement logic, numbers, lambda calculus
\end{itemize}
}

\def\Ignore#1{}
\def\Identity#1{#1}
\def\First#1#2{#1}
\def\Second#1#2{#2}

\frame[containsverbatim]{
  \frametitle{auxiliaries}
\begin{inputwithcode}
\def\Ignore#1{}
\def\Identity#1{#1}
\def\First#1#2{#1}
\def\Second#1#2{#2}
\end{inputwithcode}
}

\let\True=\First
\let\False=\Second
\def\And#1#2{#1{#2}\False}
\def\Or#1#2{#1\True{#2}}
\def\Twiddle#1#2#3{#1{#3}{#2}}
\let\Not=\Twiddle

\frame[containsverbatim]{
  \frametitle{Truth}
\begin{inputwithcode}
\let\True=\First
\let\False=\Second
\end{inputwithcode}
and logical operators:
\begin{inputwithcode}
\def\And#1#2{#1{#2}\False}
\def\Or#1#2{#1\True{#2}}
\def\Twiddle#1#2#3{#1{#3}{#2}}
\let\Not=\Twiddle
\end{inputwithcode}
}

\frame[containsverbatim]{
  \frametitle{Truth test}
\begin{quote}
\test{True takes first of TF}{\True}
\test{False takes second of TF}{\False}
\test{Not true is false}{\Not\True}

\test{And truth table TrueTrue}{\And\True\True}
\test{And truth table TrueFalse}{\And\True\False}
\test{And truth table FalseTrue}{\And\False\True}
\test{And truth table FalseFalse}{\And\False\False}

\test{Or truth table TrueTrue}{\Or\True\True}
\test{Or truth table TrueFalse}{\Or\True\False}
\test{Or truth table FalseTrue}{\Or\False\True}
\test{Or truth table FalseFalse}{\Or\False\False}
\end{quote}
}

\sectionframe{Lists}

\frame[containsverbatim]{
  \frametitle{Definition}
We implement a list as an operator with two arguments:
\begin{itemize}
\item If the list is not empty, the first argument is applied to the
  head, and the tail is evaluated;
\item If the list is empty, the second argument is evaluated.
\end{itemize}
In other words
\[ L \, a_1 \, a_2 =
      \left\{\begin{array}{rl}a_2&\mbox{if $L=()$}\\
             a_1(x) \, Y&\mbox{if $L=(x,Y)$}
      \end{array}\right. \]
}

\let\Nil=\Second
% \Cons <head> <tail> <arg1> <arg2>
\def\Cons#1#2#3#4{#3{#1}{#2}}
\def\Error{{ERROR}}
\def\Head#1{#1\First\Error}
\def\Tail#1{#1\Second\Error}

\frame[containsverbatim]{
  \frametitle{Construction}
\begin{inputwithcode}
\let\Nil=\Second
% \Cons <head> <tail> <arg1> <arg2>
\def\Cons#1#2#3#4{#3{#1}{#2}}
\def\Error{{ERROR}}
\def\Head#1{#1\First\Error}
\def\Tail#1{#1\Second\Error}
\end{inputwithcode}
}

\def\Singleton#1{\Cons{#1}\Nil}

\frame[containsverbatim]{
  \frametitle{List examples}
\begin{inputwithcode}
\def\Singleton#1{\Cons{#1}\Nil}
\end{inputwithcode}
\begin{quote}
\test{Head of a singleton}{\Head{\Singleton\True}}
\test{Head of a tail of a 2-elt list}%
     {\Head{\Tail{\Cons\True{\Singleton\False}}}}
\end{quote}
}

\def\Transcribe#1{#1\TranscribeHT\gobbletwo}
\def\TranscribeHT#1#2{1\Transcribe{#2}}

\frame[containsverbatim]{
  \frametitle{Visualization}
\begin{inputwithcode}
\def\gobbletwo#1#2{}
\def\Transcribe#1{#1\TranscribeHT\gobbletwo}
\def\TranscribeHT#1#2{1\Transcribe{#2}}
\end{inputwithcode}
}

\def\ListApply#1#2#3{#3{\ListApplyp{#1}{#2}}{#2}}
\def\ListApplyp#1#2#3#4{#1{#3}{\ListApply{#1}{#2}{#4}}}

\frame[containsverbatim]{
  \frametitle{Functions on lists}
Given function~$f$,
initial argument~$e$, and list~$X$, then
\[ \mathtt{Apply}\, f\, e\, X \Rightarrow
     f\, x_1\,(f\, x_2\, (\ldots (f\, x_n\, e)\ldots)) \]
\begin{inputwithcode}
% #1=function #2=initial arg #3=list
\def\ListApply#1#2#3{#3{\ListApplyp{#1}{#2}}{#2}}
\def\ListApplyp#1#2#3#4{#1{#3}{\ListApply{#1}{#2}{#4}}}
\end{inputwithcode}
}

\def\Cat#1#2{\ListApply\Cons{#2}{#1}}

\frame[containsverbatim]{
  \frametitle{Concatenate}
\begin{inputwithcode}
\def\Cat#1#2{\ListApply\Cons{#2}{#1}}
\end{inputwithcode}
For example:
\begin{quote}
\test{Cat two lists}%
  {\Transcribe{\Cat{\Singleton\Nil}{\Cons\Nil{\Singleton\Nil}}}}
\end{quote}
}

\sectionframe{Numbers}
\subsection{Definition}

\let\Zero\Nil
\def\AddOne#1{\Cons\Nil{#1}}

\frame[containsverbatim]{
  \frametitle{Adding one}
\begin{inputwithcode}
\let\Zero\Nil
\def\AddOne#1{\Cons\Nil{#1}}
\end{inputwithcode}
Examples:
\begin{quote}
\ttest{Transcribe zero}{\Zero}
\ttest{Transcribe one}{\AddOne\Zero}
\ttest{Transcribe three}{\AddOne{\AddOne{\AddOne\Zero}}}
\end{quote}
}

\newtoks\dtoks\newcount\nn
\def\ndef#1#2{\nn=#2 \dtoks={\Zero}\nndef#1}
\def\nndef#1{
  \ifnum\nn=0 \edef\tmp{\def\noexpand#1{\the\dtoks}}\tmp
  \else \edef\tmp{\dtoks={\noexpand\AddOne{\the\dtoks}}}\tmp
        \advance\nn by -1 \nndef#1
  \fi}
\ndef\One1 \ndef\Two2 \ndef\Three3 \ndef\Four4 \ndef\Five5
%\show\Three
\ndef\Seven7\ndef\Six6

\def\Sub#1#2{#1{\SubFrom{#2}}{#2}}
\def\SubFrom#1#2#3{#1{\SubMinOne{#3}}\Error}
\def\SubMinOne#1#2#3{\Sub{#1}{#3}}
\def\SubOne#1{#1\Second\Error}

\frame[containsverbatim]{
  \frametitle{Subtracting one}
\begin{inputwithcode}
\def\SubOne#1{#1\Second\Error}
\end{inputwithcode}
\begin{quote}
\ttest{Predecessor of two}{\SubOne{\AddOne{\AddOne\Zero}}}
\end{quote}
}

\subsectionframe{Arithmetic}

\frame[containsverbatim]{
  \frametitle{Addition}
\begin{inputwithcode}
\let\Add=\Cat
\end{inputwithcode}
\begin{quote}
\ttest{Adding numbers}{\Add{\Three}{\Five}}
\end{quote}
}

\def\GreaterThan#1#2{#2{\GreaterThanp{#1}}\False}
\def\GreaterThanp#1#2#3{#1{\GreaterThanx{#3}}\True}
\def\GreaterThanx#1#2#3{\GreaterThan{#1}{#3}}

\frame[containsverbatim]{
  \frametitle{Comparison}
\begin{inputwithcode}
\def\GreaterThan#1#2{#2{\GreaterThanp{#1}}\False}
\def\GreaterThanp#1#2#3{#1{\GreaterThanx{#3}}\True}
\def\GreaterThanx#1#2#3{\GreaterThan{#1}{#3}}
\end{inputwithcode}
\begin{quote}
\test{Greater (true result)}{\GreaterThan\Two\Five}
\test{Greater (false result)}{\GreaterThan\Three\Two}
\test{Greater (equal case)}{\GreaterThan\Two\Two}
\test{Greater than zero}{\GreaterThan\Two\Zero}
\test{Greater than zero}{\GreaterThan\Zero\Two}
\end{quote}
}

\frame[containsverbatim]{
\begin{quote}
\ttest{Use true result}{\GreaterThan\Two\Five\Three\One}
\ttest{Use false result}{\GreaterThan\Three\Two\Three\One}
\end{quote}
\begin{quote}
\test{$3<(5-1)$}{\GreaterThan\Three{\Sub\One\Five}}
\test{$3<(5-4)$}{\GreaterThan\Three{\Sub\Four\Five}}
\end{quote}
}

\frame[containsverbatim]{
  \frametitle{Integers}
\begin{inputwithcode}
% \StreamOp <operator> <initial value>
\def\StreamOp#1#2{\Cons{#2}{\StreamOp{#1}{#1{#2}}}}
\def\Integers{\StreamOp\AddOne\Zero}
\end{inputwithcode}
\begin{quote}
\ttest{Integers}{\Head{\Tail{\Integers}}}
\ttest{Integers}%
  {\Head{\Tail{\Tail{\Tail{\Tail{\Tail{\Integers}}}}}}}
\end{quote}
}

\end{document}
\frame[containsverbatim]{
  \frametitle{}
}

\frame[containsverbatim]{
  \frametitle{}
}

\frame[containsverbatim]{
  \frametitle{}
}

\end{document}

\frame[containsverbatim]{
  \frametitle{}
\begin{itemize}
\item
\end{itemize}
}


\section{Preliminaries}
\label{sect:ressat-preliminaries}

A random-exist quantified SSAT formula $\Qf$ has the form $\random{} X,\exists Y.\pf(X,Y)$,
where $X$ and $Y$ are two disjoint sets of Boolean variables,
and $\pf(X,Y)$ is a CNF formula.

\subsection{Generalization of SAT/UNSAT minterms}
\label{sect:ressat-generalize}

Given an assignment $\as$ over $X$,
if $\pcf{\pf(X,Y)}{\as}$ is satisfiable (resp. unsatisfiable),
$\as$ is called a SAT (resp. an UNSAT) minterm of $\pf$ over $X$.
The generalization process of a SAT or an UNSAT minterm $\as$ aims at expanding it to a cube $\as^+$,
while maintaining the satisfiability of $\pcf{\pf(X,Y)}{\as^+}$ the same as $\pcf{\pf(X,Y)}{\as}$.
\begin{example}
    \label{ex:ressat-assign}
    Consider formula $\pf(x_1,x_2,y_1,y_2)=x_1 \land (\lnot x_2 \lor y_1 \lor y_2)$.
    The complete assignment $\as = x_1 x_2$ over $X$, i.e., $\as(x_1)=\top, \as(x_2)=\top$,
    is a SAT minterm of $\pf$ over $X$ because $\pcf{\pf}{\as}$ is satisfiable by the assignment $\mu = y_1y_2$.
    On the other hand, the partial assignment $\as^+ = \lnot x_1$, i.e., $\as^+(x_1)=\bot$,
    is an UNSAT cube of $\pf$ as $\pcf{\pf}{\as^+}$ is unsatisfiable.
\end{example}

\subsubsection{Minimum satisfying assignment}
For a CNF formula $\pf(X,Y)$,
let $\as$ be a SAT minterm over $X$ and let $\mu$ be a satisfying complete assignment for the induced formula $\pcf{\pf(X,Y)}{\as}$ over $Y$.
To generalize $\as$ into a cube, one can find a subset of literals from $\as$ and $\mu$ that are able to satisfy all clauses in $\pf$ while the number of literals taken from $\as$ is as few as possible.
If some literals in $\as$ are irrelevant to the satisfiability,
they can be dropped from $\as$, thus expanding $\as$ to a SAT cube $\as^+$.
The generalized cube $\as^+$ is called a \textit{minimum satisfying assignment} if the number of literals taken from $\as$ is minimized.
The process of finding a minimum satisfying assignment is also known as finding a \textit{minimum hitting set}.

\subsubsection{Minimum conflicting assignment}
Given an UNSAT minterm $\as$ of a formula $\pf$,
modern SAT solvers, e.g., \minisat~\cite{Een2003Solver,Een2003Incremental},
are able to compute a conjunction of literals from $\as$ that is responsible for the conflict.
If some literals in $\as$ are irrelevant to the conflict,
they are dropped from $\as$,
thus expanding $\as$ to an UNSAT cube $\as^+$.
If the number of literals in an UNSAT cube $\as^+$ is minimized,
$\as^+$ is called a \textit{minimum conflicting assignment}.
The process of finding a minimum conflicting assignment is also known as finding a \textit{minimum UNSAT core}.


\chapter{Predicates and Connectives}
\label{chapter:predicates}
\marginurl{%
  Connectives and Propositions:\\\noindent
  Introduction to Mathematical Reasoning \#5
}{youtu.be/0unvlq2OTaE}
\section{Propositions and Predicates}
In the previous chapters we used the word ``statement'' without any even
relatively formal definition of what it means. In this chapter we are going to
give a semi-formal definition and discuss how to create complicated statements
from simple statements.

It is difficult to give a formal definition of what a mathematical statement is,
hence, we are not going to do it; the goal of this section is to
enable the reader to recognize mathematical statements.

A \emph{proposition} or a mathematical statement is a declarative sentence
which is either true or false but not both. Consider the following list of
sentences.
\begin{enumerate}
  \item $2 \times 2 = 4$
  \item $\pi = 4$
  \item $n$ is even
  \item 32 is special
  \item The square of any odd number is odd.
  \item The sum of any even number and one is prime.
\end{enumerate}
Of those, the first two are propositions; note that this says nothing about
whether they are true or not. Actually, the first is true and the second is
false. However, the third sentence becomes a proposition only when the value
of $n$ is fixed. The fourth is not a proposition. Finally, the last
two are propositions (the fifth is true and the sixth is false).

The third statement is somewhat special, because there is a simple way to make
it a proposition: one just needs to fix the value of the variable. Such
sentences are called predicates and the variables that need to be specified are
called free variables of these predicates.

Note that the fourth sentence is also interesting, since if we define what it
means to be special, the phrase becames a proposition.
As it is illustrated by the last two statements mathematicians tend to do such
things to give mathematical meanings to everyday words.

\section{Connectives}

Mathematicians often need to decide whether a given proposition is true or
false. Many statements are complicated and constructed from simpler statements
using \emph{logical connectives}. For example we may consider the following
statements:
\begin{enumerate}
  \item $3 > 4$ and $1 < 1$;
  \item $1 \times 2 = 5$ or $6 > 1$.
\end{enumerate}

\paragraph{Logical connective ``OR''.}
The second statement is an example of usage of this connective. The statement
``P or Q'' is true if and only if at least one of P and Q is true. We may
define the connective using the truth table of it.
\begin{center}
    \begin{tabular}{l l l}
        \toprule
        P & Q & P or Q \\
        \midrule
        T & T & T \\
        T & F & T \\
        F & T & T \\
        F & F & F \\
        \bottomrule
  \end{tabular}
\end{center}

The or connective is also called \emph{disjunction} and the disjunction of $P$
and $Q$ is often denoted as $P \lor Q$.

\nomenclature[L]{$P \lor Q$}{denotes the statement saying that at least one of
$P$ and $Q$ is true}

\begin{warning}
  Note that in everyday speech ``or'' is often used in the exclusive case, like
  in the sentence ``we need to decide whether it is an insect or a spider''.
  In this case the precise meaning of ``or'' is made clear by the context.
  However, mathematical language should be formal, hence, we always use ``or''
  inclusively.
\end{warning}

\paragraph{Logical connective ``AND''.}
The first statement is an example of this connective. The statement ``P and Q''
is true if and only if both P and Q are true. We may define the
connective using the truth table of it.
\begin{center}
  \begin{tabular}{l l l}
      \toprule
      P & Q & P and Q \\
      \midrule
      T & T & T \\
      T & F & F \\
      F & T & F \\
      F & F & F \\
      \bottomrule
  \end{tabular}
\end{center}

The and connective is also called \emph{conjunction} and the conjunction of
$P$ and $Q$ is often denoted as $P \land Q$.

\nomenclature[L]{$P \land Q$}{denotes the statement saying that $P$ and $Q$
are both true}

\begin{warning}
  Not all the properties of ``and'' from everyday speech are captured by
  logical conjunction. For example, ``and'' sometimes implies order. For
  example, ``They got married and had a child'' in common language means that
  the marriage came before the child. The word ``and'' can also imply a
  partition of a thing into parts, as ``The American flag is red, white, and
  blue.'' Here it is not meant that the flag is at once red, white, and blue,
  but rather that it has a part of each color.
\end{warning}

\paragraph{Logical connective ``NOT''.}
The last connective is called \emph{negation} and examples of usage of it are
the following:
\begin{enumerate}
  \item 5 is not greater than 8;
  \item Does not exist an integer $n$ such that $n^2 = 2$.
\end{enumerate}

Note that it is not straightforward where to put the negation in these
sentences.

The negation of a statement $P$ is denoted as $\lnot P$ (sometimes it is also
denoted as $\sim P$).
\nomenclature[L]{$\lnot P$}{denotes the statement saying that $P$ is false}

\begin{chapterendexercises}
  \exercise Construct truth tables for the statements
    \begin{itemize}
      \item not ($P$ and $Q$);
      \item (not $P$) or (not $Q$);
      \item $P$ and (not $Q$);
      \item (not $P$) or $Q$;
    \end{itemize}
  \exercise[recommended] Consider the statement ``All gnomes like cookies''. Which of
    the following statements is the negation of the above statement?
    \begin{itemize}
      \item All gnomes hate cookies.
      \item All gnomes do not like cookies.
      \item Some gnomes do not like cookies.
      \item Some gnomes hate cookies.
      \item All creatures who like cookies are gnomes.
      \item All creatures who do not like cookies are not gnomes.
    \end{itemize}
  \exercise Using truth tables show that the following statements are
    equivalent:
    \begin{itemize}
      \item $P \implies Q$,
      \item $(P \lor Q) \iff Q$
        ($A \iff B$ is the same as $(A \implies B) \land (B \implies A)$),
      \item $(P \land Q) \iff P$
    \end{itemize}
  \exercise Prove that three connectives ``or'', ``and'', and ``not'' can
    all be written in terms of the single connective ``notand'' where ``$P$
    notand $Q$'' is interpreted as ``not ($P$ and $Q$)'' (this operation is
    also known as Sheffer stroke or NAND).
  \exercise Show the same statement about the connective ``notor'' where
    ``$P$ notor $Q$'' is interpreted as ``not ($P$ or $Q$)'' (this operation is
    also known as Peirce's arrow or NOR).
\end{chapterendexercises}


\problemname{Beautiful Primes}

\noindent John Nash is a talented mathematician at Princeton. Due to his prolific contributions in academia, he was even recruited to the Pentagon at a young age to crack enemy encrypted telecommunication. In his first task, John was able to decipher the code mentally, much to the astonishment of other decrypters. In his everyday life, he is constantly looking for patterns in magazines to newspapers to keep his senses sharp.\\

Recently, he has been cracking away at a cryptography problem that involves prime numbers. Given a positive integer $N$, the puzzle needs him to produce a ``beautiful" list of $N$ primes. A list of $N$ primes numbers is considered beautiful if each prime is at most $1\,000\,000$, and their product has exactly $N$ digits.\\

For example, if $N = 1$, then the list $[5]$ is beautiful because its length is $1$, and its product $5$ has $1$ digit.\\

For $N = 2$, the list $[3, 7]$ is beautiful because its product $3 \times 7 = 21$ has the required digit count of $2$.\\

For $N = 3$, the list $[5, 5, 7]$ is beautiful because its product $5 \times 5 \times 7 = 175$ has the required digit count of $3$.\\

John wants to practice his mental math and impress his colleagues. He needs you to write a program that helps him practice this interesting task. John Nash has a beautiful mind, so won't you help him find some beautiful primes?

\section*{Input}
The first line of input consists of a single integer $T$ ($1 \leq T \leq 50$), the number of test cases.\\
$T$ lines follow, each of which is a test case consisting of a single integer $N$ ($1 \leq N \leq 1\,000$).

\section*{Output}
For each test case, print, on a separate line, a list of $N$ space-delimited beautiful primes that are each no greater than $1\,000\,000$. The primes do not have to be distinct, and may be printed in any order. If there are multiple answers, you may print any of them.\\


\documentclass[notitlepage]{simple}

\title{Algebraic Properties of the Gaussian Integers}
\author{Matt McCarthy}
\date{April 2016}

\begin{document}

\maketitle

\begin{thm*}
	The Gaussian Integers, denoted $\ZZ(i)$, form a Euclidean domain.
\end{thm*}

\section{Background}

Before we can talk about Euclidean domains, we first need to introduce the definition of a ring.

\begin{definition}[Ring]
	Let $R$ be a nonempty set, and let $+:R^2\rightarrow R$ and $\cdot:R^2\rightarrow R$ be binary operations on $R$.
	Then we say $R$ is a \textit{ring} if all of the following hold.
	\begin{enumerate}
		\item The structure $(R,+)$ is an abelian group whose identity we denote as 0.
		\item For any $a,b,c\in R$, $a(bc)=(ab)c$ (Multiplicative Associativity).
		\item For any $a,b,c\in R$, $a(b+c)=ab+ac$ (Left Distributivity).
		\item For any $a,b,c\in R$, $(a+b)c=ac+bc$ (Right Distributivity).
	\end{enumerate}
	If one says $R$ is a ring, we imply that there exists some addition and some multiplication operators which we denote as $a+b$ and $ab$ respectively.
\end{definition}

\begin{definition}[Ring with Unity]
	Let $R$ be a ring.
	Then $R$ is a \textit{ring with unity} if there exists a $1\in R$ such that for any $a\in R$, $a\cdot 1 = 1\cdot a = a$.
	If such a $1$ exists, we call it the \textit{unity}.
\end{definition}

\begin{definition}[Commutative Ring]
	Let $R$ be a ring.
	Then we say $R$ is \textit{commutative} if for any $a,b\in R$, $ab=ba$.
\end{definition}

The integers, denoted $\ZZ$, are a commutative ring with unity because they satisfy all of the above properties under the usual addition and multiplication.
Another helpful definition is that of a subring.

\begin{definition}[Subring]
	Let $R$ be a ring and let $S$ be a nonempty subset of $R$.
	Then $S$ is a \textit{subring} of $R$ if $(S,+,\cdot)$ is also a ring.
\end{definition}

Furthermore, we have a test which makes it easier to show a subset is a subring.

\begin{proposition}[Subring Test]
	Let $R$ be a ring, and let $S\subseteq R$ be nonempty.
	Then $S$ is a subring of $R$ if and only if for any $a,b\in S$, $a-b$ and $ab$ are also in $S$.
\end{proposition}

Now we need a few more definitions and then we can proceed to proving the theorem.
First, we need to define what a zero divisor is.

\begin{definition}[Zero Divisor]
	Let $R$ be a ring and let $a\in R$ be nonzero.
	We say $a$ is a \textit{zero divisor} if there exists a nonzero $b\in R$ such that $ab = 0$.
\end{definition}

An example of a zero divisor is 2 in $\ZZ_6$, since $2\cdot 3\equiv 0\mod{6}$.
An important property of zero divisors is that they cannot be inverted.
Thus, if our ring has no zero divisors it is fairly nice; in fact it is nice enough that we name it.

\begin{definition}[Integral Domain]
	Let $R$ be a commutative ring with unity.
	Then we say $R$ is an \textit{integral domain} if $R$ has no zero-divisors.
\end{definition}

We call these structures integral domains, because they behave like the integers.
That is there is a unity, multiplication commutes, and we can multiply any nonzero elements together to get another nonzero element.

Next we will define one of the strongest structures in algebra, the field.

\begin{definition}[Field]
	Let $\FF$ be a commutative ring with unity.
	Then $\FF$ is a \textit{field}, if for each $a\in\FF\setminus\set{0}$, there exists a $a^{-1}\in\FF$ such that $aa^{-1}=1$.
\end{definition}

One field that we will use in our proof is the complex numbers, denoted $\CC$.
Lastly, we define Euclidean domains.

\begin{definition}[Euclidean Domain]
	Let $R$ be an integral domain.
	Then we say $R$ is a \textit{Euclidean domain} if there exists a function $d:R\rightarrow(\ZZ^+\cup\set{0})$ such that
	\begin{enumerate}
		\item for any $x,y\in R\setminus\set{0}$, $d(xy)\geq d(x)$,
		\item and there exist $q,r\in R$ where $x=yq+r$ with $r=0$ or $d(r) < d(y)$.
	\end{enumerate}
	Any such $d$ is called a \textit{measure}.
\end{definition}
Essentially, Euclidean domains are rings where the division algorithm works.

\section{Solution}

To start, we define $\ZZ(i)$, the Gaussian Integers.

\begin{definition}[Gaussian Integers]
	The \textit{Gaussian Integers} are
	\[
		\ZZ(i)=\set{a+bi|a,b\in\ZZ}.
	\]
\end{definition}

We first need to show that $\ZZ(i)$ is an integral domain.

\begin{lemma}
	$\ZZ(i)$ is an integral domain under standard complex addition and multiplication.
\end{lemma}
\begin{proof}
	We know that $\CC$ is a field, therefore it is a commutative ring with identity and no zero divisors.
	Thus, it suffices to show that $\ZZ(i)$ is a subring of $\CC$ that contains 1.
	Since $1,0\in\ZZ$, $1+0i=1\in\ZZ(i)$.
	Thus $\ZZ(i)$ is nonempty since it contains the unity.
	We now need to show that for any $z=a+bi,w=c+di\in\ZZ(i)$, $z-w,zw\in\ZZ(i)$.
	We know that $z-w=(a-c)+(b-d)i$ and $zw=(ac-bd)+(ad+bc)i$.
	Since $\ZZ$ is a ring, $a-c,b-d,ac-bd$, and $ad+bc$ are in $\ZZ$ by closure.
	Therefore, $z-w,zw\in\ZZ(i)$.
	Thus $\ZZ(i)$ is a commutative ring with unity that has no zero divisors.
	Hence, $\ZZ(i)$ is an integral domain.
\end{proof}

Since $\ZZ(i)$ is an integral domain, we can embed it in what we call the \textit{field of fractions}, otherwise known as $\QQ(i)$.
We will assume that $\QQ(i)=\set{a+bi|a,b\in\QQ}$, which is true but requires a significant amount of background to show.
The proof of the following theorem hinges upon the previous assumption.

\begin{thm}
	$\ZZ(i)$ is a Euclidean domain.
\end{thm}
\begin{proof}
	In order to show that an integral domain is a Euclidean domain, we need to propose a measure.
	We claim that $d:\ZZ(i)\rightarrow\ZZ^{+}\cup\set{0}$ given by $d(z)=|z|^2$ is such a measure.

	To start we need to show that for any $z,w\in\ZZ(i)\setminus\set{0}$, $d(zw)\geq d(z)$.
	We know that $d(zw)=|zw|^2$.
	However, from Euler's formula, we know that $|zw|=|z||w|$.
	Therefore, $d(zw)=|z|^2|w|^2$.
	Furthermore, by Euler's formula, the only element with modulus less than 1 in $\ZZ(i)$ is 0.
	Therefore, $d(zw)\geq |z|^2=d(z)$.

	Next, we need to find $q,r\in\ZZ(i)$ such that $z=wq+r$ where $r=0$ or $d(r) < d(w)$.
	To do so, we embed $\ZZ(i)$ is $\QQ(i)$ and consider $z/w$.
	We know $z/w=\alpha+\beta i$ with $\alpha,\beta\in\QQ$.
	Let $\alpha',\beta'$ be the nearest integers to $\alpha$ and $\beta$ respectively.
	Then $|\alpha-\alpha'|\leq 1/2$ and $|\beta-\beta'|\leq 1/2$.
	Furthermore,
	\[
		\frac{z}{w}=\alpha-\alpha'+\alpha' + (\beta-\beta'+\beta')i=(\alpha'+\beta' i)+((\alpha-\alpha')+(\beta-\beta')i).
	\]
	Solving for $z$ yields,
	\[
		z=(\alpha'+\beta' i)w+((\alpha-\alpha')+(\beta-\beta')i)w.
	\]
	Since $\alpha',\beta'\in\ZZ$, we know that $\alpha'+\beta' i\in\ZZ(i)$.
	Thus,
	\[
		((\alpha-\alpha')+(\beta-\beta')i)w=z-(\alpha'+\beta' i)w\in\ZZ(i)
	\]
	by closure.
	Take $q=\alpha'+\beta' i$ and $r=((\alpha-\alpha')+(\beta-\beta')i)w$.
	If $r=0$, we are done, otherwise consider $d(r)$.
	\[
		d(r)=|(\alpha-\alpha')+(\beta-\beta')i|^2d(w)=(|\alpha-\alpha'|^2+|\beta-\beta'|^2)d(w)
	\]
	However, we know that $|\alpha-\alpha'|\leq 1/2$ and $|\beta-\beta'|\leq 1/2$.
	Therefore,
	\[
		d(r)\leq \paren{\frac{1}{4}+\frac{1}{4}}d(w)=\frac{1}{2}d(w) < d(w)
	\]
	and $d$ is a measure on $\ZZ(i)$.
	Thus $\ZZ(i)$ is Euclidean.
\end{proof}

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CS630: Database Management Systems
% Copyright 2014 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/beacon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Question 5}

Suppose you are given a relation with four attributes ABCD and the following set of FDs: $\{AB\rightarrow C, BC\rightarrow D\}$.

\begin{enumerate}[label=(\alph*)]
\item Identify the candidate key(s) for $R$.
\item Determine if R is in BCNF, 3NF, or neither of them.
If it is not in BCNF, decompose it into a set of BCNF relations.
\end{enumerate}

\textbf{Solution:}

Table \ref{table1} is constructed to find candidate key(s) for $R$.
Based on attribute closures of each attribute combination, combinations whose closure is equivalent to relation $R$ are underlined as minimal candidate keys.

\begin{table}[H]
\centering\caption{Closure of different combinations of relation $R$}\label{table1}
\begin{tabular}{|c|c||c|c|}
\hline
$X$ & $X^+$ & $X$ & $X^+$\\
\hline
A & A & BC & BCD\\
B & B & BD & BD\\
C & C & CD & CD\\
D & D & ABC & ABCD\\
\underline{\bf AB} & ABCD & ABD & ABCD\\
AC & AC & ACD & ACD\\
AD & AD & BCD & BCD\\
\hline
\end{tabular}
\end{table}

As shown in Table \ref{table1}(a), set of minimal candidate keys are $\{AB\}$.
To check if $R$ is in \textit{BCNF} or \textit{3NF}, closure of functional dependencies are constructed in Equation \ref{equation1}.

\begin{equation}\label{equation1}
F^+ = \{AB\rightarrow C, BC\rightarrow D\} \cup \{AB\rightarrow D\}
\end{equation}

Each functional dependency (FD) is now verified to conform to $BCNF$ and $3NF$ form, as given in Table \ref{table2}.

\begin{table}[H]
\centering\caption{Conformance verification of relation $R$}\label{table2}
\begin{tabular}{|r||c|c|c|}
\hline
 & $AB\rightarrow C$ & $BC\rightarrow D$ & $AB \rightarrow D$\\
\hline
$BCNF$ & \checkmark & $\times$ & \checkmark \\
$3NF$ & \checkmark & $\times$ & \checkmark \\
\hline
\end{tabular}
\end{table}

As shown in Table \ref{table2}, relation $R$ is not \textit{BCNF} since functional dependency $BC\rightarrow D$ violates \textit{BCNF} condition.
To resolve the violation, $R$ is decomposed into $ABC$ and $BCD$ whose closures are respectively $\{AB\rightarrow C, AB\rightarrow D\}$ and $\{BC\rightarrow D\}$ both conforming to \textit{BCNF} form.


\subsection{Test 3}

\begin{enumerate}[label=\arabic*.]
	\item
		Write the integral definition of the Laplace transform.
	\item
		Find the Laplace transform of the following functions. Be sure to give the domains. Use the integral definition for (a) and (e). Be sure to state the domain of the transformed function and any rules of Laplace transforms you use.
		\begin{enumerate}[label = (\alph*)]
			\item
				\begin{equation*}
					f(x) = e^x
				\end{equation*}
			\item
				\begin{equation*}
					h(t) = \sin{t} + 2\cos{3t}
				\end{equation*}
			\item
				\begin{equation*}
					j(t) = e^{3t}\left(t^2 + 3t + 2\right)
				\end{equation*}
			\item
				\begin{equation*}
					b(t) = \dd{}{t}{(e^{3t+1} + e^{1-t})}
				\end{equation*}
%			\item
%				\begin{equation*}
%					\delta(x) = \lim\limits_{a \to 0}{\frac{1}{\abs{x}\sqrt{\pi}}e^{(x/a)^2}}
%				\end{equation*}
		\end{enumerate}
	\item
		Find the inverse Laplace transform of the following functions.
		\begin{enumerate}[label=(\alph*)]
			\item
				\begin{equation*}
					F(s) = \frac{1}{1+s}
				\end{equation*}
			\item
				\begin{equation*}
					H(s) = \frac{s^2+2s+1}{s^3 - 4s^2 + 5s - 2}
				\end{equation*}
			\item
				\begin{equation*}
					J(s) = \frac{s-4}{s^2 -8s + 32}
				\end{equation*}
			\item
				\begin{equation*}
					A(s) = \frac{768}{(2s+3)^5}
				\end{equation*}
		\end{enumerate}
	\item
		Find the general solution to the following differential equation using one of the methods you learned previously and by Laplace transform. Show that the two methods give the same answer.
		\begin{equation*}
			2y'' - 3y' + y = 10\sin{x}
		\end{equation*}
	\item
		Solve the following IVP by Laplace transform.
		\begin{equation*}
			\begin{cases}
				2y'' + 4y' - 6y = te^{-3t} \\
				y'(0) = 0 \\
				y(0) = 1
			\end{cases}
		\end{equation*}
\end{enumerate}


% Copyright (C) He Guanyuming 2020
% The file is licensed under the MIT license.

\section{Integers and Rationals}
Now we are going to extend natural numbers to integers and rationals.

\subsection{The Integers}

\declareexercise{4.1.1}
\begin{proof}
It is immediately given by the fact that
\[
a+b = a+b \equiv a -- b = a -- b
\]
\end{proof}

\paragraph{Lemma 4.1.3}
\[
(m--0)+(n--0) = (m+n)--0
\]
\[
(m--0) \times (n--0) = (mn) -- 0
\]
ensures that the definition $m--0:=m$ is consistent with addition and multiplication.

\declareexercise{4.1.2}
\begin{proof}
\[
a--b = a'--b' \equiv a=b \wedge a'=b'
\]
Then,
\[
(b--a) = (b'--a') \equiv -(a--b) = -(a'--b')
\]
\end{proof}

\declareexercise{4.1.3}
\begin{proof}
\begin{align*}
-1 \times a
&= (0 -- 1) \times (a -- 0) \\
&= (0\times a + 1 \times 0) -- (0 \times 0 + 1 \times a) \\
&= 0 -- a \\
&= -a
\end{align*}
\end{proof}

\declareexercise{4.1.4}
\begin{proof}
Let $x=(a--b),y=(c--d),z=(e--f)$.

(1)
\begin{align*}
(a--b) + (c--d)
&= (a+c) -- (b+d) \\
&= (c+a) -- (d+b) \\
&= (c--d) + (a--b)
\end{align*}

(2)
\begin{align*}
((a--b) + (c--d)) + (e--f)
&= ((a+c)+e) -- ((b+d)+f) \\
&= (a+(c+e)) -- (b+(d+f)) \\
&= (a--b) + ((c--d) + (e--f))
\end{align*}

(3)
First ,
\[
(a--b) + (0--0) = (a--b)
\].

Second, by (1) we have $0+x=x+0$.

(4)
First,
\begin{align*}
(a--b) + (b--a)
&= (a+b) -- (a+b) \\
&= 0 -- 0 \tag{$a+b+0=a+b+0$}
\end{align*}

Second, by (1) we have $x+(-x) = (-x) + x$.

(5)
\begin{align*}
(a--b)(c--d)
&= (ac + bd) -- (ad + bc) \\
&= (ca + db) -- (cb + da) \\
&= (c--d)(a--b)
\end{align*}

(6)
The book proved this.

(7)
First,
\[
(1--0)(a--b) = (1a + 0b) -- (1b+0a) = (a--b)
\]

Second, by (5) we have $1x=x1$.

(8)
\begin{align*}
&(a--b)((c--d)+(e--f)) \\
&= (a--b)((c+e)--(d+f)) \\
&= (a(c+e) + b(d+f)) -- (a(d+f) + b(c+e)) \\
&= ((ac + bd)+(ae + bf)) -- ((ad + bc)+(af + be)) \\
&= (ac+bd)--(ad+bc) + (ae+bf)--(af+be) \\
&= (a--b)(c--d) + (a--b)(e--f)
\end{align*}

(9)
This can be easily concluded from (5) and (8).
\end{proof}

\declareexercise{4.1.5}
\begin{proof}
We need to show that
\[
a \neq 0 \wedge b \neq 0 \Longrightarrow ab \neq 0
\]

Since $a,b$ are not 0, they can be either positive or negative. If they are both positive, the case is
already proven.

When at least one of them is negative, we can divide the $-1$ from the negative ones. That is, if $a=-m$,
where $m$ is positive, then we substitute $a$ with $-1 \times m$. Then we may get $ab$ in either the form
$(-1)(-1) mn$ or $(-1) mn$, where the former is a positive number because $(-1)(-1) =1$ and the latter is
negative.
\end{proof}

\declareexercise{4.1.6}
\begin{proof}
We check the value of $ac-bc$. We know that $ac=bc$, so $ac - bc = 0 - 0 = 0$. According to (9) in
Proposition 4.1.6,
\[
ac - bc = ac+(-b)c = (a+(-b))c = 0
\]

As stated by Proposition 4.1.8, since that $c \neq 0$, $a+(-b) = 0$, which means $a-b=0$. Then we have
$a=b$.
\end{proof}

\declareexercise{4.1.7}
In the following contents, $p$ stands for a positive natural number, $n$ stands for a natural number.

\begin{proof}
(a)
\begin{align*}
a>b
&\equiv a = b+p \\
&\equiv a+(-b) = b + (-b) + p  \tag{See the following explanation} \\
&\equiv a-b = p
\end{align*}
We now explain why $a = b+p \equiv a+(-b) = b + (-b) + p$. Using the substitution law and the
commutativity of addition, it is clear to see that $a = b+p \Longrightarrow a+(-b) = b + (-b) + p$. We now
show the cancellation law of addition, that is,
\begin{lem}
\[
a+c = b+c \Longrightarrow a = b
\]
\end{lem}
\begin{proof}
\begin{align*}
a+c=b+c
&\Longrightarrow a+c+(-c) = b+c+(-c) \\
&\Longrightarrow a+(c+(-c)) = b + (c+(-c)) \\
&\Longrightarrow a=b
\end{align*}
\end{proof}

So we get the inverse result: $a = b+p \Longleftarrow a+(-b) = b + (-b) + p$.

Note that by the definition of integer and what we have know now, we can conclude that
\begin{lem}
For every integer
$i = a - b, j = c - d$, there exists exactly one integer $k$ such that $i = j+k$.
\end{lem}

(b)
\begin{align*}
a>b
&\equiv a = b + p \\
&\Longrightarrow a+c = b+c+p \\
&\Longrightarrow a+c>b+c
\end{align*}

(c)
\begin{align*}
a>b
&\equiv a=b+p \\
&\Longrightarrow ac = (b+p)c = bc + pc \\
&\Longrightarrow ac > bc \tag{$pc > 0$ by Lemma 2.3.3}
\end{align*}

(d)
\[
a>b \equiv a = b+p
\]
Then
\[
-a = -(b+p) = (-1)(b+p) = -b - p
\]
So
\[
-a+p=-b-p+p
\]
That is,
\[
-b=-a+p \equiv -b>-a
\]

(e)
Let
\[
a = b+p_1,b=c+p_2
\]
Then $a = c+(p_1+p_2)$. Obviously $p_1+p_2$ is positive, so $a>c$.

Note that $-a,-b$ are also integers, and plus that $-(-a)=a$, so we can give a stronger conclusion:
\[
a>b \equiv -a<-b
\]

(f)
If $a,b$ are all natural numbers, the statement was proven before.

If one of them (say $a$) is negative,
the other ($b$) is a natural number, then $a=-n$, and we know that $b>0$ and
$0 = a+n \Longrightarrow 0 >-a$, so by (e) we have $b>a$.

If they are both negative, then their negations satisfy the statement. Then
\[
-a<-b \equiv a>b, -a=-b \equiv a=b, -a>-b \equiv a<b
\].
\end{proof}

\declareexercise{4.1.8}
An example: $P(i): i>=0$. It is obvious that $P(0)$ and $P(i) \Longrightarrow P(i+1)$ is true. But for any
negative integer $n$, $P(n)$ is not true.

We additionally prove one more property:
\begin{lem}
For integers $a,b$,
\[
a-b=0\Longrightarrow a=b
\]
\end{lem}
\begin{proof}
We can add $b$ to both side to obtain $a=b$.
\end{proof}

\subsection{The Rationals}
\declareexercise{4.2.1}
\begin{proof}
Reflectivity:
\[
a//b = a//b \equiv ab=ab
\]

Being Symmetric:
\begin{align*}
a//b = c//d
&\equiv ad = bc \\
&\equiv cb = da \\
&\equiv c//d = a//d
\end{align*}

Transitivity:
\[
a//b = c//d \equiv ad = bc
\]
\[
c//d = e//f \equiv cf = de
\]
Thus,
\[
(ad)(cf) = (bc)(de)
\]
We then have
\[
afcd = becd
\]
We can cancel $d$ since $d \neq 0$ to obtain $afc=bec$. If $a=0$, we can conclude that $c,e$ also must be
$0$. Under this occasion, $af=be$ is also true because they all equal to $0$.
\end{proof}

\paragraph{Definition 4.2.2}
It is useful to prove that
\begin{lem} \label{lem4.2.3}
\[
(-a)//b = a//(-b)
\]
,
\[
a//b = (-a)//(-b)
\]
\end{lem}
\begin{proof}
The first is immediately given since $(-a)(-b) = ab$. The latter is proven as $a(-b) = b(-a) = -ab$.
\end{proof}

We may notice that subtraction is not mentioned here. This is because that we can get $a-b$ by adding $a$
and $-b$, where addition $+$ and negation $-$ are mentioned.

\declareexercise{4.2.2}
\begin{proof}
(1) is deduced in the book.

(2)
I don't quiet understand why Tao used this $*$ sign instead of $\times$. I know it is a new definition,
but the $\times$ sign is undefined for rationals (except for integers, but for which we can verify that
the two definitions are the same). We will use the $\times $ sign or just leave it off here.

\[
(a'//b')(c//d) \equiv a'd = b'c \equiv ad=bc \equiv (a//b)(c//d)
\]
Similarly we can verify this for $c'//d'$.

(3)
\[
-ab'=-a'b \equiv (-a)//b = (-a')//b'
\]
\end{proof}

For the sake of simplification, we hereby introduce some useful lemmas:
\begin{lem} \label{lem4.2.1}
\[
b=d\neq 0 \Longrightarrow (a//b = c//d \equiv a=c)
\]
\end{lem}
\begin{proof}
Assume that $b=d\neq 0$.

On one hand, if $a//b=c//d$, then $ad=bc$. Since that $b=d \neq 0$, we can cancel them to obtain $a=c$.

On the other hand, if $a=c$, then if we multiply them by the same integer (namely $b=d$), and the
results are still equal ($ad=bc$). So $a//b=c//d$.
\end{proof}
\begin{lem} \label{lem4.2.2}
\[
c \neq 0 \Longrightarrow a//b=ac//bc
\]
\end{lem}
\begin{proof}
Assume that $c\neq 0$.

First we know that $ab=ab$. Then we can further obtain $abc=abc$, which
means $a//b=ac//bc$.
\end{proof}

\declareexercise{4.2.3}
\begin{proof}
(1)
We have
\[
a//b + c//d = (ad+bc)//(bd)
\]
\[
c//d + a//b = (cb+da)//(db)
\]
It is easy to see that they are equal.

(2)
It is proven in the book.

(3)
We just deduce $x+0=x$ here, for we have $0+x=x+0$ according to (1).
\[
a//b + 0//1 = (a1+b0)//(b1) = a//b
\]

(4)
We only prove $x+(-x)=0$ here, for we have $x+(-x)=(-x)+x$ according to (1).
\[
a//b+(-a)//b= (ab-ab)//bb = 0//b^2=0
\]

(5)
\begin{align*}
a//b \times c//d
&= ac//bd \\
&= ca//db \\
&= c//d \times a//b
\end{align*}

(6)
\begin{align*}
&(a//b \times c//d) \times e//f \\
&= ac//bd \times e//f \\
&= ace//bdf \\
&= a//b \times ce//df \\
&= a//b \times (c//d \times e//f)
\end{align*}

(7)
We only prove $x1=x$ here, for we have $x1=1x$ according to (4).
\[
a//b \times 1//1 = a1//b1 = a//b
\]

(8)
\begin{align*}
&a//b (c//d + e//f) \\
&= a//b ((cf+ed) // (df)) \\
&= a(cf+ed)//bdf \\
&= ab(cf+ed) // b^2df \tag{See Lemma \ref{lem4.2.2}} \\
&= ((ac)(bf) + (bd)(ae)) // (bd)(bf) \\
&= ac//bd + ae//bf \\
&= (a//b \times c//d) + (a//b + e//f)
\end{align*}

(9)
This can be deduced from (5) and (8).

(10)
We merely conclude $xx^{-1} = 1$ here, since we have $xx^{-1} = x^{-1}x$ from (5).

\[
a//b \times b//a  = ab//ba = (ab)1//(ab)1 = 1//1
\]
The last step is done by Lemma \ref{lem4.2.2}.
\end{proof}

\declareexercise{4.2.4}
\begin{proof}
For any rational $r = a/b$, $a,b$ are integers.
They are either positive, $0$, or negative (except that $b$ cannot be 0). When $a,b$ are both positive,
then $r$ is also positive. When $a$ is positive but $b$ is negative, then let $b=-p$, where $p$ is
positive, thus $a/b = a/(-p) = (-a)/p$ is negative. When $a=0$, $r=0$. When $a$ is negative, and $b$ is
positive, then by definition $r$ is negative. When $a,b$ are both negative, according to Lemma
\ref{lem4.2.3}, $r$ is positive.

Therefore, we have iterated through all possible situations and verified that there is and only is one
statement for a rational is true.
\end{proof}

\declareexercise{4.2.5}
\begin{proof}
Let $x=a/b,y=c/d,z=e/f$. Before proving the following components, we will introduce some useful
propositions here.
\begin{lem} \label{lem4.2.4}
\begin{enumerate}
\item $x>0$ is logically equivalent to $x$ being positive.
\item $x<0$ is logically equivalent to $x$ being negative.
\end{enumerate}
\end{lem}
\begin{proof}
\[
x-0 =x
\]
is itself, so whether $x$ is positive or negative, the same is $x-0$, then we can deduce $x>0$ or $x<0$,
and vice versa.
\end{proof}

We can now use simplified notation $x>0$ to express the same meaning: $x$ is positive.

(a)
We check the value of
\[
\delta = x-y = a/b +(-c)/d = (ad-bc)/bd
\]
$\delta$ is also a rational number. According to the previous exercise, it is either positive, negative,
or $0$. So $x$ either $>y$, $<y$, or $=y$ (We haven't yet proven $x-y=0 \Longrightarrow x = y$. Let's
prove it now. We can add $y$ to both side of $x-y=0$ to obtain the result).

(b)
According to Lemma \ref{lem4.2.4}, $x<y \Longrightarrow x-y<0$. Then we multiply $-1/1$ with $x-y$ to
obtain (It is easy to see that for rational number $r$, $-1r = -r$ and $-(-r)=r$)
\[
-1/1 \times (x + (-y)) = -x + -(-y) = y - x
\]
Since $x-y$ is negative, and the negation of a positive number is negative, so the negation of $x-y$,
$y-x$, is positive, which means that $y>x$.

(c)
By the hypothesis, $x-y<0 \wedge y-z<0$. We are now proving that $i,j<0 \Longrightarrow i + j <0$. We can
write $i,j$ as $o/p,q/s$ respectively. Let $p,s>0$, then $o,q<0$. Then $o/p+q/s = (os+pq)/ps$. We know
that $os,pq<0$ (Write a negative integer as a negation of a positive integer to see that the product of a
positive and a negative is also negative).

Now we show that for two positive integers, their sum is still
positive. Integers who are positive are also natural number, and their sum remains a natural number. So
the sum itself equals to $0$ plus itself, which means it is positive. The negation of this sum, which is
also $-m+(-n)$, is thus negative. Since that $-m,-n$ can present any negative integer, the fact means that
the sum of two negative integers remains a negative integer.

So $os+pq<0$. But $ps>0$, so $i+j<0$. Thus, $(x-y) + (y-z) = x-z<0$, which means $x<z$.

(d)
\begin{align*}
x+z-(y+z)
&= x+z + (-)(y+z) \\
&= x+z + (-1)(y+z) \\
&= x+z -z - y \\
&= x-y <0
\end{align*}

(e)
It is easy to verify that the product of two positive rationals is still positive (Writing them as
$a/b,c/d$, where $a,b,c,d>0$, then $ac/bd$ also $>0$). Then $xz-yz=z(x-y)$, which is the product of a
positive number and a negative number, and is thus a negative number.
\end{proof}

\declareexercise{4.2.6}
\begin{proof}
According to (e) of Proposition 4.2.9, we need only to show that $x<y \Longrightarrow -x>-y$. Then we can
multiply $xz>yz$ with $-1$ to obtain what we want.

We know that the negation operation will turn a positive into negative and vice versa. Now we have
$x-y<0$, so the negation $-(x-y) = -x+y = -x - (-y)>0$, which means that $-x>-y$.
\end{proof}

There are still many properties about rationals that we use for granted (e.g. $x^-1$ has the same sign as
$x$; the two definitions of order are the same, that is, $x-y>0 \equiv x=y+p \equiv x>y$, where $p>0$).
Although they need to be proven prior to being used, we can not cover all of them here. We will
prove some of them in the future only if they are used. Also, most of them are not hard to prove. We need
not to worry.

We will prove some important ones here:
\begin{prop} \label{prop.different.def.of.order}
For rational numbers $x,y,p>0$
\[
x-y>0 \equiv x=y+p \equiv x>y
\]
\end{prop}
\begin{proof}
$x-y>0 \equiv x>y$ is the definition of order. We merely need to prove $x-y>0 \equiv x=y+p$ here.

On one hand, if $x-y>0$, then $p$ is not others, but the very number $x-y$.

On the other hand, if there exists a $p>0$ such that $x=y+p$. Then add $-y$ to the both side of the
equation, and we can get $x-y = p$, which means $x-y$ is positive. So $x-y>0$.
\end{proof}

\begin{prop} \label{prop.4.2.add.ineq}
We can add two inequalities together. That is,
\[
a<b \wedge c<d \Longrightarrow a+c<b+d
\]
\end{prop}
\begin{proof}
We know that
\[
a<b \Longrightarrow a+c<b+c
\]
, and
\[
c<d \Longrightarrow b+c<d+b
\]
According to the transitivity of order, we can derive that
\[
a+c<b+c<b+d
\]
\end{proof}

\begin{prop} \label{prop.4.2.multiply.ineq}
We can multiply two inequalities of positives or negatives together. That is,
\[
a,b,c,d >0 \wedge a<b \wedge c<d \Longrightarrow ac<bd
\]
, and
\[
a,b,c,d <0 \wedge a<b \wedge c<d \Longrightarrow ac>bd
\]
\end{prop}
\begin{proof}
When they are all positive,
we know that
\[
a<b \Longrightarrow ac<bc
\]
, and
\[
c<d \Longrightarrow bc<bd
\]
According to the transitivity of order, we can derive that
\[
ac<bc<bd
\]

When they are all negative,
we know that
\[
a<b \Longrightarrow ac>bc
\]
, and
\[
c<d \Longrightarrow bc>bd
\]
According to the transitivity of order, we can derive that
\[
ac>bc>bd
\]
\end{proof}

Note that we can already add or multiply equations because of the axiom of substitution, so we can change
the $<$ in the inequalities to $\leq$ in the previous two propositions whenever needed.

\subsection{Absolute Value and Exponentiation}
\declareexercise{4.3.1}
\begin{proof}
(a)
$x>0 \Rightarrow |x| >0$, $x=0\Rightarrow |x|=0$, $x<0 \Rightarrow |x| >0$. So $|x| \geq 0$.

And we can see that only when $x=0$ can $|x| = 0$.

(b)
This one is very tedious to prove. Let's enumerate all conditions:
\begin{enumerate}
\item $x,y>0$. On this occasion,
\[
|x+y| = x+y = |x|+|y|
\]

\item At least one of them is $0$. On this occasion, let's just let $x$ be 0, the other
situations are similar.
\[
|x+y| = |0+y| = |y| = |0| + |y| = |x| + |y|
\]

\item $x=y>0$. On this occasion, $|x+y| = |2x| = 2x = |x|+|x|$.

\item $x=y<0$. On this occasion, $|x+y| = |2x| = -2x = |x|+|x|$.

\item $x,y<0$. On this occasion, $|x+y| = -(x+y) = -x -y = |x| + |y|$.

\item One of them is positive, the other is negative. We specify $x>0,y<0$ here. But the other conditions
are similar. Under this condition, we further divide the situation into three occasions:
\begin{itemize}
\item $x+y>0$ On this occasion, $|x+y| = x+y$, $|x| + |y| = x-y$. Note that $x-y = x+y +2(-y)$, where
$2(-y) >0$, so $|x+y| < |x|+|y|$ (See Proposition \ref{prop.different.def.of.order}).
\item $x+y<0$ On this occasion, $|x+y| = -x-y$, $|x|+|y|=x-y$. Note that $-x-y - (x-y) = 2(-x) < 0$, so
$|x+y| < |x| + |y|$.
\item $x+y=0$ On this occasion, $|x+y| = 0 \leq |x| + |y|$ (Recall that the sum of two positive rationals
remains positive).
\end{itemize}
\end{enumerate}

We have iterated through all conditions.

(c)
We shall prove that
\[
-|x| \leq x \leq |x|
\]
first.

\begin{enumerate}
\item If $x>0$, then $x=|x|>0$. And $0>-|x|$, so by the transitivity of order, $-|x| < x$.
\item If $x=0$, then $|x|=-|x| = x=0$.
\item If $x<0$, then $x=-|x|<0$ And $0<|x|$, so $x<|x|$.
\end{enumerate}
This also means that $x$ either equals to $|x|$ or $-|x|$.

Then we prove that $-y \leq x \leq y \equiv y \geq |x|$.

On one hand, if $-y \leq x \leq y$, then when $x=|x|$, we have $|x| \leq y$; when $x=-|x|$, we have
$-y \leq -|x| \equiv y \geq |x|$. As stated previously, we know that at least one of the two conditions
are satisfied.

On the other hand, if $y \geq |x|$, then $-y \leq -|x|$. But since that $-|x| \leq x \leq |x|$, we can
obtain what we want by the transitivity of order.

(d)
\begin{enumerate}
\item If $x=y=0$, then $|xy| = 0 = |x||y|$.
\item If $x,y>0$, then $|xy| = xy = |x||y|$.
\item If $x,y<0$, then $xy>0$, $|xy| = xy = (-x)(-y) = |x||y|$.
\item If one of them is positive, and the other is negative, (say $x>0,y<0$), then
$|xy|=-xy=x(-y)=|x||y|$. The other conditions are similar.
\end{enumerate}

Thus, $|-x| = |-1||x| = 1|x| = |x|$.

(e)
This can be easily conclude from (a).

(f)
Since that $|-x|=|x|$, we have $|x-y| = |-(x-y)| = |y-x|$.

(g)
Note that $x-z = (x-y) = (y-z)$. Then from (b) we can deduce that $|x-z| \leq |x-y| + |y-z|$, which is
$d(x,z) \leq d(x,y) + d(y,z)$.
\end{proof}

\declareexercise{4.3.2}
\begin{proof}
(a)
If $x=y$, then $|x-y| = 0$. And any positive rational $\varepsilon >0$, so $|x-y|\leq\varepsilon$.

The other statement is much better easier to prove after we have know the denseness of rationals. We
essentially repeat some of the proof work that are done afterwards here.
On the other hand, suppose the negation, that is, $(\forall \varepsilon>0)(|x-y| \leq \varepsilon)$, but
$x\neq y$. Then $x-y \neq 0$. Let $\delta = |x-y| \neq 0$. We know that $2^{-1} = 1/2 > 0$, so
$\delta / 2 > 0$. Also we have $\delta /2 + \delta /2 = \delta \Longrightarrow \delta /2 < \delta$. Then let
$\varepsilon = \delta /2$. So we have both $|x-y| < \delta/2$ and $|x-y| > \delta /2$, which is impossible.

(b)
It is immediately derived from $|x-y| = |y-x|$.

(c)
\[
|x-z| \leq |x-y| + |y-z| \leq \varepsilon + \delta
\]

(d)
\[
|x+z - (y+w)| = |x-y + z-w| \leq |x-y| + |z-w| \leq \varepsilon + \delta
\]
\[
|x-z - (y-w)| = |x-y + w-z)| \leq |x-y| + |w-z| \leq \varepsilon + \delta
\]

(e)
\[
|x-y| \leq \varepsilon < \varepsilon'
\]

(f)
From (c) of Proposition 4.3.3, we can derive that
\[
|x-z| \leq \varepsilon \equiv -\varepsilon \leq x-z \leq \varepsilon
\equiv z-\varepsilon \leq x \leq z+ \varepsilon
\]
, and that
\[
|x-y| \leq \varepsilon \equiv y-\varepsilon \leq x \leq y + \varepsilon
\]
Thus we have
\[
y-\varepsilon \leq x \leq z + \varepsilon
\]

We will only prove the statement when $z\leq w \leq y$, another one is similar. On this occasion,
$-y \leq -w \leq -z$. Add this inequality to $y-\varepsilon \leq x \leq z + \varepsilon$ to obtain that
\[
-\varepsilon \leq x-w \leq \varepsilon
\]

(g)
\[
|xz-yz| = |x-y||z| \leq \varepsilon|z|
\]

(h)
We will explain why $|a| \leq \varepsilon \wedge |b| \leq \delta$ implies
$|a||z| + |b||x| + |a||b| \leq \varepsilon|z| + \delta|x| + \varepsilon\delta$.

First, multiply $|a| \leq \varepsilon$ with $|z|$ to obtain $|a||z| \leq \varepsilon |z|$. Then add both sides
of the inequality with $|b||x| + |a||b|$ to gain
\[
|a||z| + |b||x| + |a||b| \leq \varepsilon |z| + |b||x| + |a||b| \tag{1}
\]
Similarly,
\[
|a||z| + |b||x| + |a||b| \leq |a||z| + \delta |x| + |a||b| \tag{2}
\]
Finally, we can multiply $|a| \leq \varepsilon$ with $|b| \leq \delta$ as stated by Proposition
\ref{prop.4.2.multiply.ineq} to derive $|a||b| \leq \varepsilon\delta$. So after some addition we have
\[
|a||z| + |b||x| + |a||b| \leq |a||z| + |b||x| + \varepsilon\delta \tag{3}
\]

Using Proposition \ref{prop.4.2.add.ineq}, we add (1),(2) and (3) together:
\[
3(|a||z| + |b||x| + |a||b|) \leq (\varepsilon|z| + \delta|x| + \varepsilon\delta) + 2(|a||z| + |b||x| + |a||b|)
\],
which can be simplified to
\[
|a||z| + |b||x| + |a||b| \leq \varepsilon|z| + \delta|x| + \varepsilon\delta
\]

Also note that if we use $x-y$ as $a$, $z-w$ as $b$, and derive $|xz-yw|$ from
\[
xz=(y+a)(w+b),
\]
then what we will get is that $xz,yw$ are ($\delta|y| + \varepsilon|w| + \delta\varepsilon$) close.

This consequence may seem obvious, but in fact it isn't. And should we change some variables of them, the
result may vary. This example tells us that we should be very cautious when dealing with inequalities. What we
should do is to carefully derive conclusions from what we have proven instead of taking intuitive things for
granted.
\end{proof}

\declareexercise{4.3.3}
\begin{proof}
(a)
\begin{enumerate}
\item Use induction. We induct on $m$. First, $x^nx^0 = x^n1 = x^{n+0}$.

Suppose that for $m$, the statement is already true. Then
\begin{align*}
x^nx^{m+1}
&= x^n(x^m\times x)\\
&= x^nx^m \times x \\
&= x^{n+m} \times x \tag{The induction hypothesis} \\
&= x^{n+m+1}
\end{align*}

\item Use induction. We induct on $m$. First, $(x^n)^0 = 1 = x^{n\times 0}$.

Suppose that for $m$, the statement is already true. Then
\begin{align*}
(x^n)^{m+1}
&= (x^n)^m \times x^n \\
&= x^{mn} \times x^n \tag{The induction hypothesis} \\
&= x^{mn + n} \tag{By the previous statement} \\
&= x^{n(m+1)}
\end{align*}

\item Use induction. We induct on $n$. First, $(xy)^0 = 1 = x^0y^0$.

Suppose that for $m$, the statement is already true. Then
\begin{align*}
(xy)^{m+1}
&= (xy)^m \times xy \\
&= x^my^m \times xy \tag{The induction hypothesis} \\
&= x^m \times x \times y^m \times y \\
&= x^{m+1} y^{m+1}
\end{align*}
\end{enumerate}

(b)
On one hand, if $x=0$, then for $n>0$, $0^n=0$.

On the other hand, if for $n>0$, $x^n=0$, we need to prove $x=0$. We try to show that
$x \neq 0 \Longrightarrow x^n \neq 0$. Use induction. Since that $n\neq 0$, we start from $n=1$.
$x^1 = x^0 \times x = x$.

Suppose that for $n$, the statement is already true. Then
\[
x^{n+1} = x^n \times x,
\]
which is the product of two positive rationals, and which is thus positive.

(c)
(1)
Use induction: $x^0 = y^0 =1>0$.

Suppose that for $n$, the statement is already true. Then we have two inequalities here:
\[
x^n \geq y^n \geq 0
\]
and
\[
x \geq y \geq 0
\]
We can multiply the two because of Proposition \ref{prop.4.2.multiply.ineq}. Then we have
\[
x^{n+1} \geq y^{n+1} \geq 0
\]

If $n>0$, then we induct from $1$. The process resembles to what we have just done, so I don't write it here.

(d)
Use induction: $|x^0| = |1| =1 =|1|^0$.

Suppose that for $n$, the statement is already true. Then
\[
|x^{n+1}| = |x^n \times x| = |x^n| |x| = |x|^n |x| = |x|^{n+1}
\]
\end{proof}

\paragraph{Definition 4.3.11}
We can see that there are now two versions of $x^{-1}$. Now we try to show that they express the same thing.
Write $x$ as $a/b$. The first version is $x^{-1} = b/a$.

The second version is $x^{-1} = 1/x = 1 \times x^{-1}\text{(version 1)} = x^{-1} = b/a$.

Note that only after we have known this can we say that for the second version of $x^{-1}$,
$(x^{-1})^{-1} = x$.

Now we can also say that $x^{-n} = (x^n)^{-1}$

\declareexercise{4.3.4}
\begin{proof}
Except for (3),
we have already proven these properties when $m,n \in \mathbb{N}$. Then we will just write $m,n$ as $-m,-n$.

(a)
(1)
\[
x^{-m}x^{-n} = \frac{1}{x^m}\frac{1}{x^n} = \frac{1}{x^mx^n}=1/x^{m+n} = x^{-m-n}
\]

(2)
Before doing this, we must derive that for integers $a,b$ and natural number $n$, $(a/b)^n = a^n/b^n$. Use
induction: $(a/b)^0 = 1 = 1/1 = a^0/b^0$.

Suppose that for $n$, the statement is already true. Then
\[
(a/b)^{n+1} = (a^n/b^n)(a/b) = (a^{n+1}/b^{n+1})
\]

We can now close the induction.

Thus,
\begin{align*}
&(x^{-n})^{-m} \\
&= ((1/(x^n))^m)^{-1} \tag{$x^{-n} = (x^n)^{-1}$} \\
&= (1/(x^n)^m)^{-1} \\
&= (x^n)^m \\
&= x^{mn}
\end{align*}

(3)
\begin{align*}
(xy)^{-n}
&= 1/(xy)^n \\
&= 1/(x^ny^n) \\
&= (1/x^n) (1/y^n) \\
&= x^{-n}y^{-n}
\end{align*}

(b)
\begin{lem}
For $x = (a/b) >  y = (c/d) > 0$,
\[
0 < x^{-1} < y{-1}
\]
\end{lem}
\begin{proof}
Let $a,b,c,d>0$. We know that
\[
a \times b^{-1} > c \times d^{-1}
\]
Multiply it with $bd>0$, we have
\[
ad>bc
\].
Multiply it with $a^{-1}c^{-1} >0$, we have
\[
d/c>b/a
\]
That is,
\[
y^{-1} > x^{-1}
\]
And they are obviously bigger than $0$.
\end{proof}

According to the lemma,
\[
x^{n} \geq y^{n} \Longrightarrow ((x^{n})^{-1} \leq (y^{n})^{-1} \equiv x^{-n} \leq y^{-n})
\]

(c)
We first show that for positive integer $n$, the statement is true. We assume that $x>y$. Another situation is
similar.
Use induction, we try to prove that $x\neq y \Longrightarrow x^n \neq y^n$. We need to start from $n=1$. First,
$x^1=x > y^1=y$.

Suppose that for $n$, the statement is already true. Then
Multiply $x>y$ with $x^n > y^n$,
we have $x^{n+1} > y^{n+1}$.

We can now close the induction.

Then for negative ones, we know that $1/x = 1/y$ iff $x=y$, so $x^n \neq y^n \equiv x^{-n} \neq y^{-n}$.

(d)
It is immediately derived since $1/x = 1/y \equiv x=y$.
\end{proof}

\declareexercise{4.3.5}
\begin{proof}
Use induction from $1$.

$2^1 = 2 >1$.

Suppose that for $N$, the statement is already true. Then
\[
2^{N+1} = 2\times 2^N > 2N \geq N+1
\]
(Note that $N\geq 1 \Longrightarrow 2N \geq N+1$)
\end{proof}

\subsection{Gaps In The Rational Numbers}
\declareexercise{4.4.1}
\begin{proof}
Existence: We show that when $x \geq 0$, $(\exists n \in \mathbb{Z})(n \leq x < n+1)$. Write $x$ as $a/b$,
where $a \geq 0,b>0$. If $a= 0$, then $x=0$, $n=0$. If $a\neq 0$, then according to Proposition 2.3.9,
\[
\exists m \exists r(a = mb+r),
\]
where $m,r \in \mathbb{N}$ and $r < b$. Because of this, $mb+b >a$, so $(mb+b)/b>a/b=x$, which means $m+1>x$.
On the other hand, $mb \leq a$, so $mb/b \leq a/b =x$, which means $m \leq x$.

Then when $x<0$, then $-x>0$, and $(\exists n \in \mathbb{Z})(n \leq -x < n+1)$, so
\[
-n \geq x > -n -1
\]
$\geq$ means $>$ or $=$ (exclusive). When $-n > x >-n-1$, let $m=-n-1$, then $m \leq x < m+1$ is true. When
$-n =x > -n-1$, let $m=-n$, then $m \leq x < m+1$ is also true. So $m$ is the integer we want if $x<0$.

Uniqueness:
For $n \leq x < n+1$, and $m \neq n$, we try to prove that $m \leq x<m+1$ is not possible. Before doing this,
we need some lemmas:
\begin{lem}
(1) For integers $i,j$, $i<j \equiv i+1\leq j$.
(2) For integer $i$, there is no integer $j$ such that $i<j<i+1$.
\end{lem}
\begin{proof}
(1) It has already been proven for natural numbers. If $i,j<0$, then $-i,-j>0$.
\[
i<j \equiv -i>-j \equiv -i \geq -j+1 \equiv i \leq j-1 \equiv i+1 \leq j
\]

(2)
Suppose the negation, that there exists a integer $j$ such that $i<j<i+1$. We know that
$i<j \equiv i+1 \leq j \equiv j \geq i+1$. But we also have $j<i+1$, which is impossible.
\end{proof}

$m$ either $<$ or $>n$. On the former case, $m+1\leq n\leq x$, so $m+1>x$ is not possible. One the latter case,
$x<n+1\leq m$, so $m \leq x$ is impossible.
\end{proof}

\declareexercise{4.4.2}
\begin{proof}
(a) We will use a different approach from the hint the book provided here. After assuming the negation, we
try to prove that
$a_n \leq a_0 -n$. Note that subtraction may results in a overflow (that is, natural numbers flows to negative
integers). So we will first define $a_n$ as integers. And we try to show that no such infinite descent
sequences can only lie in  $\mathbb{N}$.

Use induction: $a_0 \leq a_0-0$.

Suppose the statement for $n$ is already true, then
$a_{n+1} < a_n \equiv a_{n+1} +1 \leq a_n \equiv a_{n+1} \leq a_n - 1$. Then we have
$a_{n+1} \leq (a_0 -n -1 = a_n -(n+1))$. We can now close the induction.

However, let $n = a_0$, then $a_{n+1} \leq n-n-1 = -1$, which means that $a_{n+1}$ does not lie in
$\mathbb{N}$.

(b)
(1) Yes. For example, $a_n:=-n$ satisfies our restrictions.

(2) Yes. Because it is always possible to find a rational between $0$ and $a_0$.
\end{proof}

\declareexercise{4.4.3}
\begin{proof}
(1) Suppose the negation, that natural number $n=2k=2k'+1$, where $k,k'$ are also natural. Then $2k=2k'+1$
Then we have $2k > 2k' \Longrightarrow k>k'$. But $2k+1 = 2(k'+1)$, so $2(k'+1) > 2k \Longrightarrow k'+1 >k$.
But we know that between $k',k'+1$ there exists no natural numbers. so it is impossible. (Note that we don't
have a proposition saying $ac>bc \Longrightarrow a>b$ for natural numbers, but we can first deal with them with
the range of rationals. Multiply them with $c^{-1}$, and we will see that the result of the two sides are also
natural numbers, so for natural numbers this is true.)

(2)
\[
p^2 = (2k+1)^2 = 4k^2+4k + 1 = 2(2k^2+2k) +1
\]

(3)
Treat $p,q$ as rationals.
$p^2/2=q^2 \Longrightarrow q^2<p^2$. We show that $q \geq p$ can not be true. It is obvious that $q \neq p$.
And when $q>p$, multiply it with itself, $q^2>p^2$, which is impossible.
\end{proof}


\documentclass[11pt]{article}
\title{Lagrange Multipliers}
\author{Suhas Arehalli}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amsthm}

\newtheorem{theorem}{Theorem}[section]

\theoremstyle{definition}
\newtheorem{definition}{Definition}[section]

\begin{document}
\maketitle

\section{Preliminaries}
\begin{definition}[Manifold]
    A smooth $k$-manifold $M$ in $\mathbb{R}^n$ can be defined as a set of points
    \[ M = \{ \mathbf{x} \in \mathbb{R}^n | F(\mathbf{x}) = \mathbf{0}\} \]
    For some function $F: \mathbb{R}^n \rightarrow \mathbb{R}^{n-k}$ such that
    $\left[\mathbf{D}F(\mathbf{x})\right]$ has maximal rank for all $\mathbf{x}$.
    This is equivalent to the conventional definition in terms of local charts and
    such.
\end{definition}
\begin{definition}[Tangent Space]
    The tangent space to a manifold at point $\mathbf{a}$ is defined as the set of vectors
    that locally describe movement on the manifold. Since staying on the manifold requires
    that it's defining constaint function $F$ remains 0, we can formally define the tangent
    space at $\mathbf{a}$ as
    \begin{align*}
        T_{\mathbf{a}}M &= \{ \mathbf{x} \in \mathbb{R}^n | [\mathbf{D}F(\mathbf{a})]\mathbf{x} = \mathbf{0}\} \\
        T_{\mathbf{a}}M &= ker[\mathbf{D}F(\mathbf{a})]
    \end{align*}
\end{definition}
\section{Background}
Given a function $f: \mathbb{R}^n \rightarrow \mathbb{R}$, we'd like to find a vector $\mathbf{x}$ on
a smooth $k$-manifold $M$ embedded in $\mathbb{R}^n$. That maximizes or minimizes the function $f$. This
is called \textit{Constrained Optimization}, and can be solved using the method of Lagrange Multipliers.

Due to the definition of a manifold we gave earlier, we can phrase this problem in a somewhat more
accessable light. Suppose we have a system of $p$ \textit{constraint equations} of $n$ variables
represented by the equation $F(x) = 0$ for  $F: \mathbb{R}^n \rightarrow \mathbb{R}^{p}$. This
defines a set of points in $\mathbb{R}^n$ that satisfy these equations, and if we find that
the derivative of $F$ is onto, then we know that these points form a smooth manifold. Now given
these constraints, it seems natural in many practical scenarios that we'd want to find extremum
that satisfy these constaints. For instance, we'd like to know how to minimize cost or maximize
profit or utility given some set of constraints. Given some cost or utility function $f$, Lagrange
multipliers give us a way to answer that question.

\section{Statement}
\begin{theorem}[Method of Lagrange Multipliers]
    Suppose $M$ is a smooth $k$-manifold described by the function $F: \mathbb{R}^n \rightarrow \mathbb{R}^{n-k}$
    such that $F(\mathbf{x}) = 0$ and $\mathbf{a}$ is an extremum of some function  $f: \mathbb{R}^n
    \rightarrow \mathbb{R}$ on $M$. Then
    \[ \left[ \mathbf{D}f(\mathbf{a}) \right] = \sum_i^{n-k} \lambda_i \left[\mathbf{D}F_i(\mathbf{a})\right] \]
    Or in terms of gradients,
    \[ \nabla f(\mathbf{a}) = \sum_i^{n-k} \lambda_i \nabla F_i(\mathbf{a}) \]
    \[ \nabla f(\mathbf{a}) \in span\left(\{\nabla F_1(\mathbf{a}), \dots,\nabla F_{n-k}(\mathbf{a})\}\right) \]
\end{theorem}

\section{Intuition}
First let's discuss \textit{why} this should be true, and then discuss a strategy for proving it.

Naively, we begin to optimize functions by using the first derivative test - a function is at a local
extremum if movement in any direction will no longer increase/descrease the value of the function.
More precisely, if the directional derivative is $0$ for all vectors ($[\mathbf{D}f(\mathbf{a})]\mathbf{v} = \mathbf{0}, \forall \mathbf{v} \in \mathbb{R}^n$), then we're at a potential extremum.

Note, however, that in order to maximize $f$ on the manifold, we cannot simply seek out a maximum for $f$.
There's no guarantee that any maximum of $f$ exists in $\mathbb{R}^n$, let alone lies on $M$. Thus we must begin
working from the manifold, rather than from the function we optimize.

The main benefit of working on the manifold is that we have few directions we can move, and thus it is easier
to be a local extremum. Consider that the gradient $\nabla f$ is considered the direction of greatest
increase, and it's negation is the direction of greatest decrease. If we want to be at an extremum, these must
be orthogonal to the manifold - no movement on the manifold can change the value of $f$. However, the consider
each of the  individual constraint functions, $F_1, ..., F_{n-k}$. Each of these has a gradient function
$\nabla F_i$ which is orthogonal to the manifold - In fact, these span the orthogonal complement of the
tangent space of the manifold at a point. Thus, at an extrema, $\nabla f$ should be in the span of
$\{ \nabla F_1, ..., \nabla F_{n-k} \}$, which is exactly what the method of Lagrange Multipliers
says.

\section{Proof}
At a local maximum of $f$ on $M$, $\mathbf{a}$, we must be able to move in every direction on the
manifold without increasing the value of the $f$ (otherwise, a point $\varepsilon$ in that direction
would give a greater value of $f$ than $\mathbf{a}$, contradicting the fact that $\mathbf{a}$ is a
local maximum. The same argument goes for minima - we must be able to move in every direction without
decreasing $f$. Since derivatives are continuous on smooth manifolds, we must have
    \[\forall \mathbf{v} \in T_{\mathbf{a}}M = \ker[\mathbf{D}F(\mathbf{a})] \]
that
    \begin{align*}
    [\mathbf{D}f(\mathbf{a})&]\mathbf{v} = 0  \\
    \mathbf{v} \in \ker&[\mathbf{D}f(\mathbf{a})]
    \end{align*}
Which implies that
    \[\ker[\mathbf{D}F(\mathbf{a})] \subset  \ker[\mathbf{D}f(\mathbf{a})] \]
This is often one stopping point for this result, but deriving the original equation requires further
manipulation. First note that since $[\mathbf{D}F(\mathbf{a})]\mathbf{v} = \mathbf{0}$ (since
$\mathbf{v} \in \ker[\mathbf{D}F(\mathbf{a})]$), then $\forall i \in \{1, \dots,  n-k\}$
    \[ [\mathbf{D}F_i(\mathbf{a})]\mathbf{v} = 0 \]
plus, from the above
    \[ [\mathbf{D}f(\mathbf{a})]\mathbf{v} = 0 \]
For all $\mathbf{v} \in T_{\mathbf{a}}M$. We can consider these in terms of gradients:
\begin{align*}
    \nabla F_1 \cdot \mathbf{v} &= 0  \\
    \dots \\
    \nabla F_{n-k} \cdot \mathbf{v} &= 0 \\
    \nabla f \cdot \mathbf{v} &= 0
\end{align*}
Now it is clear to see that $\{\nabla f, \nabla F_1, ..., \nabla F_{n-k}\}$ lie in the orthogonal complement
to the Tangent Space $T_{\mathbf{a}}M$ (they're all orthogonal to every vector in the tangent space!).

Now, since they both live in $\mathbb{R}^n$, the orthogonal complement
should have dimension $n - dim(T_{\mathbf{a}}M) = n - k$, since the sum of dimension of any space and
it's orthogonal complement must be the dimension of the space it's embedded in. Now we have $n-k+1$
vectors in a space with dimension $n-k$. Thus they cannot be linearly independent, and one must be a linear
combination of the others. thus we can write
    \[ \nabla f = \sum_i^{n-k} \lambda_i \nabla F_i \]
Or, back in matrix notation,
    \[ [\mathbf{D}f(\mathbf{a})] = \sum_i^{n-k} [\mathbf{D}F_i(\mathbf{a})] \]
Concluding our proof.
\end{document}