\chapter{Terminology on sets and functions}
\label{ch:sets_functions}
This appendix will cover some notions on sets and functions
such as ``bijections'', ``equivalence classes'', and so on.

Remark for experts: I am not dealing with foundational issues in this chapter.
See \Cref{ch:zfc} (and onwards) if that's what you're interested in.
Consequently I will not prove most assertions.

\section{Sets}
A \vocab{set} for us will just be a collection of elements (whatever they may be).
For example, the set $\NN = \{1, 2, 3, 4, \dots\}$ is the positive integers,
and $\ZZ = \{ \dots, -2, -1, 0, 1, 2, \dots\}$ is the set of all integers.
As another example, we have a set of humans:
\[ H = \left\{ x \mid \text{$x$ is a featherless biped} \right\}. \]
(Here the ``$\mid$'' means ``such that''.)

There's also a set with no elements, which we call the \vocab{empty set}.
It's denoted by $\varnothing$.

It's conventional to use capital letters for sets (like $H$),
and lowercase letters for elements of sets (like $x$).

\begin{definition}
We write $x \in S$ to mean ``$x$ is in $S$'', for example $3 \in \NN$.
\end{definition}

\begin{definition}
	If every element of a set $A$ is also in a set $B$,
	then we say $A$ is a \vocab{subset} of $B$,
	and denote this by $A \subseteq B$.
	If moreover $A \neq B$, we say $A$ is a \vocab{proper subset}
	and write $A \subsetneq B$.
	(This is analogous to $\le$ and $<$.)

	Given a set $A$, the set of all subsets is denoted $2^A$
	or $\PP(A)$ and called the \vocab{power set} of $A$.
\end{definition}
\begin{example}
	[Examples of subsets]
	\listhack
	\begin{enumerate}[(a)]
		\ii $\{1,2,3\} \subseteq \NN \subseteq \ZZ$.
		\ii $\varnothing \subseteq A$ for any set $A$. (Why?)
		\ii $A \subseteq A$ for any set $A$.
		\ii If $A = \{1,2\}$ then $2^A =
		\left\{ \varnothing, \{1\}, \{2\}, \{1,2\} \right\}$.
	\end{enumerate}
\end{example}

\begin{definition}
	We write
	\begin{itemize}
		\ii $A \cup B$ for the set of elements in
		\emph{either} $A$ or $B$ (possibly both),
		called the \vocab{union} of $A$ and $B$.
		\ii $A \cap B$ for the set of elements in \emph{both} $A$ and $B$, and
		called the \vocab{intersection} of $A$ and $B$.
		\ii $A \setminus B$ for the set of elements in $A$ but \emph{not} in $B$.
	\end{itemize}
\end{definition}

\begin{example}
	[Examples of set operations]
	Let $A = \{1,2,3\}$ and $B = \{3,4,5\}$. Then
	\begin{align*}
		A \cup B &= \{1,2,3,4,5\} \\
		A \cap B &= \{3\} \\
		A \setminus B &= \{1,2\}.
	\end{align*}
\end{example}

\begin{exercise}
	Convince yourself: for any sets $A$ and $B$,
	we have $A \cap B \subseteq A \subseteq A \cup B$.
\end{exercise}

Here are some commonly recurring sets:
\begin{itemize}
	\ii $\CC$ is the set of complex numbers, like $3.2 + \sqrt 2 i$.
	\ii $\RR$ is the set of real numbers, like $\sqrt 2$ or $\pi$.
	\ii $\NN$ is the set of positive integers, like $5$ or $9$.
	\ii $\QQ$ is the set of rational numbers, like $7/3$.
	\ii $\ZZ$ is the set of integers, like $-2$ or $8$.
\end{itemize}
(These are pronounced in the way you would expect:
``see'', ``are'', ``en'', ``cue'', ``zed''.)

\section{Functions}
Given two sets $A$ and $B$, a \vocab{function} $f$ from $A$ to $B$
is a mapping of every element of $A$ to some element of $B$.

We call $A$ the \vocab{domain} of $f$, and $B$ the \vocab{codomain}.
We write this as $f \colon A \to B$ or $A \taking f B$.
\begin{abuse}
	If the name $f$ is not important, we will often just write $A \to B$.
\end{abuse}
We write $f(a) = b$ or $a \mapsto b$ to signal that $f$ takes $a$ to $b$.

If $B$ has $0$ as an element and $f(a) = 0$,
we often say $a$ is a \vocab{root} or \vocab{zero} of $f$,
and that $f$ \vocab{vanishes} at $a$.

\subsection{Injective / surjective / bijective functions}

\begin{definition}
	A function $f \colon A \to B$ is \vocab{injective}
	if it is ``one-to-one'' in the following sense:
	if $f(a) = f(a')$ then $a = a'$.
	In other words, for any $b \in B$,
	there is \emph{at most} one $a \in A$ such that $f(a) = b$.

	Often, we will write $f \colon A \injto B$ to emphasize this.
\end{definition}
\begin{definition}
	A function $f \colon A \to B$ is \vocab{surjective}
	if it is ``onto'' in the following sense:
	for any $b \in B$ there is \emph{at least} one $a \in A$
	such that $f(a) = b$.

	Often, we will write $f \colon A \surjto B$ to emphasize this.
\end{definition}
\begin{definition}
	A function $f \colon A \to B$ is \vocab{bijective}
	if it is both injective and surjective.
	In other words, for each $b \in B$,
	there is \emph{exactly} one $a \in A$ such that $f(a) = b$.
\end{definition}

\begin{example}
	[Examples of functions]
	By ``human'' I mean ``living featherless biped''.
	\begin{enumerate}[(a)]
		\ii There's a function taking every human to their
		age in years (rounded to the nearest integer).
		This function is \textbf{not injective},
		because for example there are many people with age $20$.
		This function is also \textbf{not surjective}: no one has age $10000$.

		\ii There's a function taking every
		USA citizen to their social security number.
		This is also \textbf{not surjective} (no one has SSN equal to $3$),
		but at least it \textbf{is injective} (no two people have the same SSN).
	\end{enumerate}
\end{example}

\begin{example}
	[Examples of bijections]
	\listhack
	\begin{enumerate}[(a)]
		\ii Let $A = \{1,2,3,4,5\}$ and $B = \{6,7,8,9,10\}$.
		Then the function $f \colon A \to B$ by $a \mapsto a+5$ is a bijection.
		\ii In a classroom with $30$ seats,
		there is exactly one student in every seat.
		Thus the function taking each student to the seat they're in
		is a bijection; in particular, there are exactly $30$ students.
	\end{enumerate}
\end{example}

\begin{remark}
	Assume for convenience that $A$ and $B$ are finite sets.
	Then:
	\begin{itemize}
		\ii If $f \colon A \injto B$ is injective,
		then the size of $A$ is at most the size of $B$.
		\ii If $f \colon A \surjto B$ is surjective,
		then the size of $A$ is at least the size of $B$.
		\ii If $f \colon A \to B$ is a bijection,
		then the size of $A$ equals the size of $B$.
	\end{itemize}
\end{remark}

Now, notice that if $f \colon A \to B$ is a bijection,
then we can ``apply $f$ backwards'':
(for example, rather than mapping each student to the seat they're in,
we map each seat to the student sitting in it).
This is called an \vocab{inverse function};
we denote it $f\inv \colon B \to A$.

\subsection{Images and pre-images}
Let $X \taking f Y$ be a function.

\begin{definition}
	Suppose $T \subseteq Y$.
	The \vocab{pre-image} $f\pre(T)$ is the set of all
	$x \in X$ such that $f(x) \in T$.
	Thus, $f\pre(T)$ is a subset of $X$.
\end{definition}
\begin{example}
	[Examples of pre-image]
	Let $f \colon H \to \ZZ$ be the age function from earlier.
	Then
	\begin{enumerate}[(a)]
		\ii $f\pre(\{13, 14, 15, 16, 17, 18, 19\})$ is the set of teenagers.
		\ii $f\pre(\{0\})$ is the set of newborns.
		\ii $f\pre(\{1000, 1001, 1002, \dots \}) = \varnothing$,
		as I don't think anyone is that old.
	\end{enumerate}
\end{example}

\begin{abuse}
	By abuse of notation, we may abbreviate $f\pre(\{y\})$ to $f\pre(y)$.
	So for example, $f\pre(\{0\})$ above becomes shortened to $f\pre(0)$.
\end{abuse}

The dual notion is:
\begin{definition}
	Suppose $S \subseteq X$.
	The \vocab{image} $f\im(S)$ is the set of
	all things of the form $f(s)$.
\end{definition}
\begin{example}
	[Examples of images]
	Let $A = \{1,2,3,4,5\}$ and $B = \ZZ$.
	Consider a function $f \colon A \to B$ given by
	\[
		f(1) = 17 \quad
		f(2) = 17 \quad
		f(3) = 19 \quad
		f(4) = 30 \quad
		f(5) = 234.
	\]
	\begin{enumerate}[(a)]
		\ii The image $f\im(\{1,2,3\})$ is the set $\{17, 19\}$.
		\ii The image $f\im(A)$ is the set $\{17, 19, 30, 234\}$.
	\end{enumerate}
\end{example}
\begin{ques}
	Suppose $f \colon A \surjto B$ is surjective.
	What is $f\im(A)$?
\end{ques}

\section{Equivalence relations}
Let $X$ be a fixed set now.
A binary relation $\sim$ on $X$ assigns a truth value ``true''
or ``false'' to $x \sim y$ for each $x$ or $y$.
Now an \vocab{equivalence relation} $\sim$ on $X$ is a binary relation
which satisfies the following axioms:
\begin{itemize}
	\ii Reflexive: we have $x \sim x$.
	\ii Symmetric: if $x \sim y$ then $y \sim x$
	\ii Transitive: if $x \sim y$ and $y \sim z$ then $x \sim z$.
\end{itemize}
An \vocab{equivalence class} is then a
set of all things equivalent to each other.
One can show that $X$ becomes partitioned by these equivalence classes:

\begin{example}
	[Example of an equivalence relation]
	Let $\NN$ denote the set of positive integers.
	Then suppose we declare $a \sim b$ if $a$ and $b$ have the same last digit,
	for example $131 \sim 211$, $45 \sim 125$, and so on.

	Then $\sim$ is an equivalence relation.
	It partitions $\NN$ into ten equivalence classes,
	one for each trailing digit.
\end{example}

Often, the set of equivalence classes will be denoted $X/{\sim}$
(pronounced ``$X$ mod sim'').


% ***********************************************************************************
% Pure LaTeX part to be inserted in a document (be careful of depencies of packages & commands)
% Prepared by Qingan Zhao and Ruitong Zhu under the supervision of Arnaud de La Fortelle
% Fall 2017
% 2D heat diffusion subsection of the modeling part
% ***********************************************************************************

\subgroup{2}{Qingan Zhao and Ruitong Zhu}

\paragraph{Description}
The aim of this part is to describe and model a PDE that describes temperature dynamics in a two-dimensional body via heat conduction.
Basically, heat conduction is the exchange of heat from regions of higher temperatures into regions with lower temperatures, which varies in the transfer rate for different materials.

Consider a thin flat body with a constant thickness $h$ and uniform density $\rho'$. Assume that the faces of the thin body are in perfect insulation, which means there is no heat flow travel in the out-of-plane direction of the body. Hence, heat can only flow in the direction within the plane of the body, which turns into a two-dimensional problem. Then a two-dimensional coordinate system is established such that each point of the body can be described with a coordinate $(x,y)$. Then the (2D-uniform) density of the body is $\rho = \rho' h$. Denote the temperature function of each point by $T$ so that the temperature of the body at position $(x,y)$ and time $t$ are described as $T(x,y,t)$, as shown in Figure~\ref{heatSystem.fig}. The goal is to derive $T(x,y,t)$ when there is no internal heat source.
\begin{figure}[htb]
	\centering
	\includegraphics[width=10cm]{heatSystem.pdf}
	\caption{System description in 2 dimensions}\label{heatSystem.fig}
\end{figure}

\paragraph{Model}
Consider a small rectangular element of the body with vertices $(x,y)$, $(x+\ud x,y)$, $(x, y+\ud y)$, and $(x+\ud x, y+\ud y)$. The heat flows are shown in Figure~\ref{heatElement.fig}.
\begin{figure}[htb]
	\centering
	\includegraphics[width=8cm]{HeatElement.pdf}
	\caption{Heat flows in a small rectangular element of the body}\label{heatElement.fig}
\end{figure}

The heat amount $Q$ (i.e the thermal energy) of the rectangular element at time $t$ is:
\begin{equation}
Q(x,y,t)=C m T(x,y,t)
\end{equation}
where $C$ is called \emph{heat capacity}, which is a supposed to be constant (assuming the material is uniform and temperature do not vary too much); $m = \rho A$ is the mass of the rectangular element where $A$ its surface.

The rate of thermal energy change with respect to time is therefore:
\begin{equation}\label{thermalEnergyChange.eq}
\frac{\partial Q}{\partial t} = C\rho \ud x \ud y\frac{\partial T}{\partial t}
\end{equation}

As shown in Figure~\ref{heatElement.fig}, the incoming flow is $F_1 + F_2 + F_3 + F_4$. Denote the heat flux $\vec q$ in horizontal and vertical directions by $q_x$ and $q_y$, then we have:
\begin{eqnarray}
F_1 &=& q_x(x,y,t)\ud y\label{flow1}\\
F_2 &=& -q_y(x,y+\ud y,t)\ud x\label{flow2}\\
F_3 &=& q_y(x,y,t)\ud x\label{flow3}\\
F_4 &=& -q_x(x+\ud x,y,t)\ud y\label{flow4}
\end{eqnarray}

Now, we know that according to energy conservation, the thermal energy variation of any small element (as in Equation~(\ref{thermalEnergyChange.eq})) is equal to the total incoming heat flow.  By putting the partial flows as in Equations~(\ref{flow1})-(\ref{flow4}), this conservation principle yields:
\begin{equation}\label{thermalEnergyChange.eq2}
C\rho \ud x \ud y\frac{\partial T}{\partial t} = \ud y [q_x(x,y,t)-q_x(x+\ud x,y,t)]+\ud xh[q_y(x,y,t)-q_y(x,y+\ud y,t)]
\end{equation}

Now, another physical principle, \emph{Fourier's Law}, states that the heat flow is (negatively) proportional to the gradient of temperature:
\begin{equation}\label{FourierLaw.eq}
\vec q = -k\nabla T
\end{equation}
where $k$ is known as the thermal conductivity of the material (also considered as a constant). Then $q_x$ and $q_y$ are expressed as:
\begin{equation}
\begin{split}
q_x=-k\frac{\partial T}{\partial x}\\
q_y=-k\frac{\partial T}{\partial y}
\end{split}
\end{equation}

Hence, Equation~(\ref{thermalEnergyChange.eq2}) can be written as:
\begin{equation}\label{thermalEnergyChange.eq3}
\frac{\partial Q}{\partial t}=k\ud yh\left(\frac{\partial T(x+\ud x,y,t)}{\partial x}-\frac{\partial T(x,y,t)}{\partial x}\right)+k\ud xh\left(\frac{\partial T(x,y+\ud y,t)}{\partial y}-\frac{\partial T(x,y,t)}{\partial y}\right)
\end{equation}

Combine Equation~(\ref{thermalEnergyChange.eq}) and~(\ref{thermalEnergyChange.eq3}):
\begin{equation}
\frac{\partial T(x,y,t)}{\partial t}=\frac{k}{c\rho}\left(\frac{\partial ^2 T(x,y,t)}{\partial x^2}+\frac{\partial ^2 T(x,y,t)}{\partial y^2}\right)
\end{equation}

Denote $k/c\rho$ by $a^2$, and the two-dimensional heat equation can be drawn:
\begin{equation}
\frac{\partial T}{\partial t}=a^2\left(\frac{\partial ^2 T}{\partial x^2}+\frac{\partial ^2 T}{\partial y^2}\right)
\end{equation}

If we would like to solve the PDE in practice, the initial conditions and (or) boundary conditions need to be specified. For initial conditions, assume that in domain $\Omega$:
\begin{equation}
T(x,y,0)=\phi(x,y)
\end{equation}

As for boundary conditions, we should know either the value or the gradient of the function at some boundaries (i.e., fixed temperature or fixed flow).

For fixed temperature, for example, at the boundary $x=0$, a boundary condition could be stated as follow:
\begin{equation}
T(0, y, t)=0
\end{equation}
such a boundary condition means the temperature will always be $0$ at the line $x=0$ of the 2D plane.

The boundary condition could aslo be a fixed flow. For example, at the boundary $x=0$:
\begin{equation}
\nabla (0, y, t)=0
\end{equation}
such a boundary condition means that there is no heat transfer at the line $x=0$ of the 2D plane.

When the conditions are given, it is the time to solve the PDE. Most simple PDEs can be solved using analytical methods, such as separation of variables and transform techniques. If the analytical methods do not work, numerical methods can be implemented to find the numerical approximations to the solutions of the PDE. A practical example with boundary conditions will be given in Section 2.3, where the analytical solution will be derived.

Once a system has been solved, we could enhance it by implementing the optimization. One thing we could do is to add some controls. For example, we would like the temperature of a plane less as stable as possible. A proper cost function could be a mathematical expression giving $\ud T/\ud t$ as a function of the external heat sourse. Tools such as Kalman filter can be implemented to do the optimization. A real world example is in aircraft temperature monitoring, for which temperature changes that above a threshold should be warned.


%
% CMPT 354: Database Systems I - A Course Overview
% Section: Relational Model
%
% Author: Jeffrey Leung
%

\section{Relational Model}
	\label{sec:relational-model}
\begin{easylist}

	& \emph{Relational database:} Collection of tables (mathematical concept of a relation
		&& Tables have unique names; rows represent an entity or relationship

	& \emph{Tuple:} Record/row of a relational database

	& \emph{Relation:} Set of unique tuples
		&& \emph{Relation instance:} Actual table with a particular set of rows
			&&& \emph{Cardinality (relation instance):} Number of tuples in a given relation instance
		&& \emph{Relation schema:} Column headings of a table
			&&& Consists of the names of the relation, the names of the columns, and the domain of each field
			&&& \emph{Domain:} Set of possible values
				&&&& \emph{Relation instance:} Subset of the Cartesian product of the domains; set of distinct, valid tuples/records
					&&&&& \emph{Cartesian product:} All elements in a set paired with all elements in another set
					&&&&& I.e. One possible row
			&&& E.g. Customer relation: \\
			Customer = \{ sin, firstName, lastName, age, income \} \\
			Domain: integer(9), char(20), char(20), integer, realNumber
		&& Degree (arity): Number of fields of a relation

\end{easylist}
\subsection{Relational Models vs. Databases}
	\label{subsec:relational-model:relational-models-vs-databases}
\begin{easylist}

	& For differences in terminology, see table~\ref{tab:equivalent-terms-for-relational-models-and-databases}

	\Deactivate
	\begin{table}[!htb]
		\centering
		\caption{Equivalent terms for Relational Models and Databases}
		\label{tab:equivalent-terms-for-relational-models-and-databases}
		\begin{tabular}{ l l }
			Relational models: & Databases: \\
			\hline
			Relation schema & Table schema \\
			Relation instance / relation & Table \\
			Field & Column/attribute \\
			Tuple & Record/row
		\end{tabular}
	\end{table}
	\Activate

\end{easylist}
\clearpage


    \filetitle{arf}{Run autoregressive function on a tseries object}{tseries/arf}

	\paragraph{Syntax}\label{syntax}

\begin{verbatim}
X = arf(X,A,Z,Range,...)
\end{verbatim}

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

\begin{itemize}
\item
  \texttt{X} {[} tseries {]} - Input data from which initial condition
  will be taken.
\item
  \texttt{A} {[} numeric {]} - Vector of coefficients of the
  autoregressive polynomial.
\item
  \texttt{Z} {[} numeric \textbar{} tseries {]} - Exogenous input series
  or constant in the autoregressive process.
\item
  \texttt{Range} {[} numeric \textbar{} Inf {]} - Date range on which
  the new time series observations will be computed; \texttt{RANGE} does
  not include pre-sample initial condition. \texttt{Inf} means the
  entire possible range will be used (taking into account the length of
  pre-sample initial condition needed).
\end{itemize}

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

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \texttt{X} {[} tseries {]} - Output data with new observations created
  by running an autoregressive process described by \texttt{A} and
  \texttt{Z}.
\end{itemize}

\paragraph{Description}\label{description}

The autoregressive process has one of the following forms:

\begin{verbatim}
A1*x + A2*x(-1) + ... + An*x(-n) = z,
\end{verbatim}

or

\begin{verbatim}
A1*x + A2*x(+1) + ... + An*x(+n) = z,
\end{verbatim}

depending on whether the range is increasing (running forward in time),
or decreasing (running backward in time). The coefficients
\texttt{A1},\ldots{}\texttt{An} are gathered in the input vector
\texttt{A},

\begin{verbatim}
A = [A1,A2,...,An].
\end{verbatim}

\paragraph{Example}\label{example}

The following two lines create an autoregressive process constructed
from normally distributed residuals,

\[ x_t = \rho x_{t-1} + \epsilon_t \]

\begin{verbatim}
rho = 0.8;
X = tseries(1:20,@randn);
X = arf(X,[1,-rho],X,2:20);
\end{verbatim}


Intuitively, an abstract state of the Parametric Hypercubes domain (\adomain) tracks disjunctive information relying on floating-point intervals of fixed width. A state of \adomain\ is made by a set of hypercubes of dimension $|\variables|$. Each hypercube has $|\variables|$ sides, one for each variable, and each side contains an abstract non-relational value for the corresponding variable. Each hypercube represents a set of admissible combinations of values for all variables.

The name Hypercubes comes from the geometric interpretation of the elements of \adomain . The concrete state of a program with variables in $\variables$ is an environment in $\funzione{\variables}{\real}$. This can be isomorphically represented by a tuple of values where each item of the tuple represents a program variable. Seen in this way, the concrete state corresponds, geometrically, to \emph{a point} in the $|\variables|$-dimensional space.
%Each dimension of the space represents the possible values that the corresponding variable of the program can assume. The concrete trace of a program is a sequence of points in such space (one for each state of the trace).
The hypercubes of our domain \adomain\ are \emph{volumes} in the same $|\variables|$-dimensional space.
%Each side of the hypercube is the concretization of the abstract value of the corresponding variable, and thus it corresponds to a set of values in that dimension of the space. The concretization of an hypercube is the set of all the points contained in its volume. A state in \adomain\ is composed by a set of hypercubes: its concretization is the union of all the volumes of its hypercubes.  In this way we track disjunctive information.

\vspace{-10pt}
\subsection{Lattice structure}
\vspace{-5pt}
An abstract state of \adomain\ tracks a \emph{set} of hypercubes, and each hypercube is represented by a tuple of abstract values. The dimension of these tuples is equal to the number of program variables.
%: this means that each variable is associated to a given item of the tuple (i.e., to a specific side of the hypercube).
%Consider for instance a program in which $\variables = \{x_1, x_2\}$. In this case, the hypercubes of \adomain\ are 2D-rectangles. In particular, the two sides of a single hypercube are two abstract values, one for $x_1$ and one for $x_2$.
%A priori, our approach is modular w.r.t. the non-relational abstract domain we adopt to approximate the values of single variables inside an hypercube.
We abstract floating-point variables through intervals of real values. A set of hypercubes allows us to track disjunctive information, and this is useful when the values of a variable are clustered in different ranges.
%: instead of having a very big interval to cover them all (and which would cover also a lot of invalid values), we use two (or more) smaller intervals. Since it would be particularly expensive to perform all the lattice operators pointwisely, we partition the possible values into intervals of fixed width. As an example, suppose that the initial vertical velocity of the balls of our case study ranges between $50.0$ and $60.0$ or between $-60.0$ and $-50.0$. A single interval would approximate these values with $[-60.0 .. 60.0]$, while with our approach we track two intervals, $[-60.0 .. -50.0]$ and $[50.0 .. 60.0]$ (with fixed width $10.0$), which distinguish between balls thrown downwards and balls thrown upwards.
The performance of this domain, though, becomes a crucial point, because the number of possible hypercubes in the space is potentially exponential with respect to the number of partitions along each spatial axis.

First of all, the complexity is lightened by the use of a \emph{fixed} width for each variable, by partitioning the possible intervals, and by the efficiency of set operators on tuples. Then, another performance booster is the use of a smart representation for intervals: in order to store the specific interval range we just use a single integer representing it. This is possible because each variable $x_i$ is associated to an interval width (specific only for that variable), which we call $w_i$ and which is a parameter of the analysis. Each width $w_i$ represents the width of all the possible abstract intervals associated to $x_i$. More precisely, given a width $w_i$ and an integer index $m$, the interval uniquely associated to the variable $x_i$ is $[m \times w_i .. (m+1) \times w_i]$. Notice that the smaller the width associated to a variable, the more granular and precise the analysis on that variable (and the heavier computationally the analysis). In Section \ref{sec:semantics} we will show how to compute and adjust automatically the widths.

\textbf{Example:} Consider the case study of Section \ref{sec:case_study} and in particular the two variables \statement{px} and \statement{py}. Suppose that the widths associated to such variables are $w_1 = 10.0, w_2 = 25.0$. The hypercubes in this case are 2D-rectangles that can be represented on the Cartesian plane.
Each side of a hypercube is identified by an integer index, and a 2D hypercube is then uniquely identified by a pair of integers. For instance, the hypercube $h_1 = ( 0, 1 )$ represents $\statement{px} \in [0.0 .. 10.0]$ and $\statement{py} \in [25.0 .. 50.0]$, while the hypercube $h_2 = ( 0, 0 )$  associates $\statement{px}$ to $[0.0 .. 10.0]$ and $\statement{py}$ to $[0.0 .. 25.0]$. Figure \ref{fig:hcExample} depicts the two hypercubes associated to the initialization of the case study (i.e., $h_1$ and $h_2$). Instead, Figure \ref{fig:hcExample2} depicts the six hypercubes obtained after executing the first iteration of the \statement{while} loop.
%The ball is moving towards the right of the screen and is going downwards: this is coherent with the fact that the horizontal velocity is certainly positive (between $0.0$ and $60.0$), while the vertical velocity is certainly negative (between $-30.0$ and $-25.0$).

\vspace{-10pt}
\begin{figure}
\centering
\subfloat[The abstract state after the initialization of the variables \statement{px,py}, when their widths are, respectively, $10.0$ and $25.0$]{
\includegraphics[scale=0.28]{Pics/example_hc_2d.png}
\label{fig:hcExample}
}\hspace{0.3cm}
\subfloat[The abstract state of \statement{px,py} after the first iteration of the loop (widths are, respectively, $10.0$ and $25.0$)]{
\includegraphics[scale=0.28]{Pics/example_hc_2d_2.png}
\label{fig:hcExample2}
}
\caption{Cartesian plans}
\end{figure}
\vspace{-10pt}

%\begin{figure}[ht]
%\begin{centering}
%\includegraphics[scale=0.35]{Pics/example_hc_2d.png}
%\caption{The abstract state of the case study after the initialization of the variables (focusing the attention only on \statement{px,py}, when their widths are, respectively, $10.0$ and $25.0$)}
%\label{fig:hcExample}
%\end{centering}
%\end{figure}
%
%\begin{figure}[ht]
%\begin{centering}
%\includegraphics[scale=0.35]{Pics/example_hc_2d_2.png}
%\caption{The abstract state of the case study after the first iteration of the loop (focusing the attention only on \statement{px,py}, when their widths are, respectively, $10.0$ and $25.0$)}
%\label{fig:hcExample2}
%\end{centering}
%\end{figure}

We now formalize our abstract domain. Each abstract state is a set of hypercubes, where each hypercube is composed by $|\variables|$ integer numbers. The abstract domain is then defined by $\adomain = \wp(\integer^n)$ where $n = |\variables|$. The definition of lattice operators relies on set operators.
%: the partial order is defined through set inclusion, the lub and glb are set union and set intersection, respectively, while bottom and top are the empty set and the set containing all possible $n$-dimensional hypercubes, respectively.
Formally, $\langle\wp(\integer^n), \subseteq, \cup, \cap, \emptyset, \integer^n\rangle$.

%\begin{lemma}
%$\langle\wp(\integer^n), \subseteq, \cup, \cap, \emptyset, \integer^n\rangle$ is a complete lattice.
%\begin{proof}
%The proof follows immediately by basic properties of set operators.
%\end{proof}
%\end{lemma}

\vspace{-10pt}
\subsection{Concretization function}
\vspace{-5pt}
We denote by $\agenericdomain$ the non-relational abstract domain on which our analysis is parameterized, and by $n$ the number of variables of the program. Let $\sigma \in \real^n$ be a tuple and $\sigma_i \in \real$ be the $i$-th element of such tuple. Also, let $\gamma_\agenericdomain : \funzione{\agenericdomain}{\wp(\real)}$ be the concretization function of abstract values of the non-relational abstract domain $\agenericdomain$, and $\function{getAbsValue}_v : \funzione{\naturals}{\agenericdomain}$ be the function that, given an integer index, returns the abstract value (in the domain $\agenericdomain$) which corresponds to that index inside the tuple $v$.
Then, the function $\gamma_{\aval} : \funzione{\wp(\agenericdomain^n)}{\wp(\real^n)}$ concretizes a set of hypercubes to a set of vectors of $n$ floating point values. Formally, $\gamma_{\aval}(\cel{V}) = \{\sigma : \exists v \in \cel{V} : \forall i \in [1..n] : \sigma_i \in \gamma_\agenericdomain(\function{getAbsValue}_v(i)) \}$ where $\cel{V} \in \wp(\agenericdomain^n)$ is a set of hypercubes.
Finally, based on $\gamma_{\aval}$, we can define the function $\gamma_{\adomain}$, which maps a subset $\cel{V}$ of $\wp(\agenericdomain^n)$ into an environment. The function $\gamma_{\adomain} : \funzione{\wp(\agenericdomain^n)}{\wp(\funzione{\variables}{\real})}$ concretizes the hypercubes domain. Formally, $\gamma_{\adomain}(\cel{V}) = \{[\statement{x} \mapsto \cel{\sigma}_{\avariableindex{\statement{x}}} : \statement{x} \in \variables] : \cel{\sigma} \in \gamma_{\aval}(\cel{V})\}$.  The function $\gamma_{\adomain}$ maps the vectors returned by $\gamma_{\aval}$ into concrete environments relying on the function $\avariableindexname : \funzione{\variables}{\naturals}$. The latter, given a variable, returns its index in the tuples which compose the elements of \adomain.

%Intuitively, we represent a concrete value as a tuple of real values (one for each variable of the program). The concretization of an abstract state $h \in \adomain$ is then a set of tuples. The concretization of $h$ is the union of all the concretizations of its hypercubes, i.e., all the points belonging to the volumes of its hypercubes. Each tuple of the concretization of $h$, then, is a point belonging to one hypercube of $h$.

\vspace{-10pt}
\subsection{Convergence of the analysis}
\vspace{-5pt}
%The domain described so far does not ensure the convergence of the analysis. In fact, a \statement{while} loop may add new hypercubes with increased indices at each iteration, and the dimension of the abstract state (i.e., the hypercubes set) would increase at each iteration without converging. Thus, we need a way to force the convergence of the analysis. Given our abstract state representation,
The number of hypercubes in an abstract state may increase indefinitely. In order to make the analysis convergent, we fix for each variable of the program a maximum integer index $n_i$ such that $n_i$ represents the interval $[n_i \times w_i .. +\infty]$. The same happens symmetrically for negative values. In this way, the set of indices of a given variable is finite, the resulting domain has finite height, and the analysis is convergent.

This approach may seem too rough since we establish the bounds of intervals before running the analysis. However,
%this allows us to control the number of possible intervals in our hypercubes, and this is particularly important for the efficiency of the overall analysis. In addition,
when analysing physics simulations we can use the initialization of variables and the property to verify in order to establish convenient bounds for the intervals. For instance, in the case study presented in Section \ref{sec:case_study} we are interested in checking if a ball stays in the screen, that is, if \statement{px} is greater than zero and less than a given value \statement{w} representing the width of the screen. Since we are only interested in proving that, once a ball has exited the screen, it does not come back, we can abstract together all the values that are greater than \statement{w}.

%Observe that more sophisticated widening operators could be used as an alternative to the adopted solution described above, but this could affect the performance of the resulting analysis.

%\subsection{Other data types or non-relational abstractions}
%\todogiulia{Secondo me questa subsection si puo' cancellare}
%As suggested before, for now we focus the application of our abstract domain to physics simulations, and for this reason we abstract floating point variables through intervals of fixed width. However, we may apply other kind of abstractions (e.g., the Sign domain) to our framework to consider other types of variables (integer, boolean, etc.). We will sketch other possible applications of our framework in Section \ref{sec:otherapplications}.

\vspace{-10pt}
\subsection{Offsets}
\vspace{-5pt}
A loss of precision may occur due to the fact that hypercubes proliferate too much, even using small widths. Consider, for example, the statement \statement{x = x + 0.01} (which is repeated at each iteration of the \statement{while} loop) with $1.0$ as the width associated to \statement{x}. If $[0.0 .. 1.0]$ was the initial interval associated to \statement{x}, the sequence of abstract states would be: $\{ [0.0 .. 1.0] \}$, $\{ [0.0 .. 1.0], [1.0 .. 2.0] \}$, $\{ [0.0 .. 1.0], [1.0 .. 2.0], [2.0 .. 3.0] \}$ and so on. At each iteration we would add one interval.
%If the initial interval associated to \statement{x} was $[0.0 .. 1.0]$, after the first iteration we would obtain two intervals ($[0.0 .. 1.0]$ and $[1.0 .. 2.0]$) because the resulting interval would be $[0.01 .. 1.01]$, which spans over two fixed-width intervals. For the same reason, after the second iteration we would obtain three intervals ($[0.0 .. 1.0],[1.0 .. 2.0]$ and $[2.0 .. 3.0]$) and so on: at each iteration we add one interval.

In order to overcome these situations, we further improve the definition of our domain: in each hypercube, each variable $v_i$ (associated to width $w_i$) is related to (other than an integer index $i$ representing the fixed-width interval $[i \times w_i .. (i+1) \times w_i]$) a specific offset $(o_m,o_M)$ \emph{inside} such interval. In this way, we use a sub-interval (of arbitrary width) inside the fixed-interval width, thereby restricting the possible values that the variable can assume. Both $o_m$ and $o_M$ must be smaller than $w_i$, greater than or equal to $0$ and $o_m \leq o_M$. Then, if $i$ and $(o_m,o_M)$ are associated to $v_i$, this means that the possible values of $v_i$ belong to the interval $[(i \times w_i) + o_m .. (i \times w_i) + o_M]$.

An element of our abstract domain is then stored as a map from hypercubes to tuples of offsets. In this way, we can keep the original definition of a hypercube as a tuple of integers, but we also map each hypercube to a tuple of offsets (one for each variable). Now an abstract state is defined by $M : \funzione{\integer^{|Vars|}}{{(\real \times \real)}^{|Vars|}}$, i.e., a map where the domain is the set of hypercubes, and the codomain is the set of tuples of offsets.

The least upper bound between two abstract states ($M=M_1 \sqcup M_2$) is then defined by $dom(M) = dom(M_1) \cup dom(M_2)$, and

$$
\forall h \in dom(M) : M(h) = \begin{cases}
M_1(h) & \mbox{if } h \in dom(M_1) \wedge h \notin dom(M_2) \\
M_2(h) & \mbox{if } h \in dom(M_2) \wedge h \notin dom(M_1) \\
merge(M_1(h),M_2(h)) & \mbox{otherwise }
\end{cases}$$
where $merge(o_1,o_2)$ creates a new tuple of offsets by merging the two tuples of offsets in input: for each pair of corresponding offsets (for example $(m_1,M_1)$ and $(m_2,M_2)$), the new offset is the widest combination possible (i.e., $(\min(m_1,m_2)$ and $\max(M_1,M_2))$). Note that this definition corresponds to the pointwise application of the least upper bound operator over intervals. The widening operator is extended in the same way: it applies the standard widening operators over intervals pointwisely to the elements of the vector representing the offsets.


% COSC 4P03 Project
% sudoku generator
% Taras Mychaskiw

\section{Introduction}

\subsection{Terms}
Table~\ref{tab:terms} describes the terms and names that will be used in this paper.
\begin{table}[H]
\begin{center}\begin{tabular}{c||c}
    \hline
    $p$         &   the width of each box in the sudoku                             \\
    $q$         &   the height of each box in the sudoku                            \\
    $n$         &   the size of the entire sudoku grid, equal to $pq$               \\
    cell        &   each point in the sudoku grid                                   \\
    clue        &   a cell with a value already given in a sudoku puzzle            \\
    candidates  &   all the values that can legally lay in a cell                   \\
    unit        &   a set of cells where no two cells may share a value (eg a row)  \\
    peers       &   the set all of cells that a cell may not share a value with     \\
    formity     &   describes if a puzzle has none, one or many solutions           \\
    \hline
\end{tabular}\end{center}
\caption{Common sudoku terms used in this paper.}
\label{tab:terms}
\end{table}
%\end{Terms}

\subsection{Sudoku}
Sudoku is one of the most popular logic puzzle games of all time. A sudoku puzzle involves placing the numbers $1$ through $n$ in
a $n$x$n$ grid so that each row, each column and each $p$x$q$ box in the grid have each number exactly once. At the beginning of
a puzzle, several cells have values filled in (these are \textit{clues}), and the player's job is to fill in the rest of the grid.
It is always understood that every sudoku puzzle has exactly one solution to it. Figure~\ref{fig:sudoku} depicts a sample sudoku puzzle.
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.67]{sudoku.png}
    \caption{A standard $9$x$9$ sudoku puzzle.}
    \label{fig:sudoku}
\end{figure}

With the growing popularity of sudoku, many $9$x$9$ puzzles are required to feed the demand. Additionally, there is some demand
for larger or smaller sudoku puzzles (rather than $9$x$9$) for a greater or simpler challenge.
%\end{Sudoku}

%\end{Introduction}


\section{Linear Spaces}
In this chapter our approach to estimating $f$ involves the use of
finite dimensional linear spaces.

Remember what a linear space is? Remember definitions of dimension,
linear subspace, orthogonal projection, etc...

Why use linear spaces?
\begin{itemize}
\item Makes estimation and statistical computations easy.
\item Has nice geometrical interpretation.
\item It actually can specify a broad range of models given we have
  discrete data.
\end{itemize}

Using linear spaces we can define many families of function $f$;
straight lines, polynomials, splines, functions with two continuous
derivatives, and many other spaces (these are examples for the case
where $\bx$ is a scalar). The point is: we have many options.

Notice that in most practical situation we will have  observations
$(\bX_i,Y_i), i=1,\dots,n$. In some situations we are only interested
in estimating $f(\bX_i),i=1,\dots,n$. In fact, in many situations it
is all that matters from a statistical point of view. We will write
$\f$ when referring to the this vector and $\hat{\f}$ when referring to
an estimate. Think of how its different to know $f$ and know $\f$.

Let's say we are interested in estimating $\f$. A common practice in
statistics is to assume that $\f$ lies in some {\it linear space}, or
is well approximated by a $\g$ that lies in some {\it linear space}.

For example for simple linear regression we assume that $\f$ lies in the
linear space
of lines:
\[
\alpha + \beta \bx, (\alpha,\beta)' \in {\mathbb R}^2.
\]

For linear regression in general we assume that $\f$ lies in the
linear space of linear combinations of the covariates or rows of the
design matrix. How do we write it out?

Note: Through out this chapter $f$ is used to denote the true
regression function and $g$ is used to denote an arbitrary function in
a particular space of functions. It isn't necessarily true that $f$ lies
in this space of function. Similarly we use $\f$ to denote the true function
evaluated at the design points or observed covariates and $\g$ to
denote an arbitrary function evaluated at the design points or
observed covariates.

Now we will see how and why it's useful to use linear models in a more
general setting.

A linear model  of order $p$ for the
regression function (\ref{fdef}) consists of a $p$-dimensional linear
space $\cal G$, having as a basis the function
\[
B_j(\bx), j=1,\dots,p
\]
defined for $\bx \in I$. Each member  $g \in \cal G$ can be written
uniquely as a linear combination
\[
g(\bx) = g(\bx; \bg{\theta}) = \theta_1 B_1(\bx) + \dots + \theta_p
B_p(\bx)
\]
for some value of the coefficient vector $\bg{\theta} =
(\theta_1,\dots,\theta_p)' \in {\mathbb R}^p$.

Notice that $\bg{\theta}$ specifies the point $g \in \cal G$.

How would you write this out for linear regression?

Given observations $(\bX_i,Y_i), i=1,\dots,n$ the least squares
estimate (LSE) of $\f$ or
equivalently $f(\bx)$ is defined by $\hat{f}(\bx) =
g(\bx;\hat{\bg{\theta}})$,
where
\[
\hat{\bg{\theta}} = \arg\min_{\bg{\theta} \in {\mathbb R}^p} \sum_{i=1}^n \{Y_i -
g(\bX_i,\bg{\theta})\}^2.
\]
Define the vector $\g = \{g(x_1),\dots,g(x_n)\}'$. Then the distribution
of the observations of $Y | X=x$ are in the family
\begin{equation}
\label{nonparametric}
\{ N(\g,\sigma^2 {\mathbf I}_n); \g = [g(x_1),\dots,g(x_n)]', \, g \in {\cal G}\}
\end{equation}
and if we assume the errors $\varepsilon$ are IID normal and that $f \in \cal
  G$ we have that $\hat{\f} =
[g(x_1;\hat{\bg{\theta}}),\dots,g(x_n;\hat{\bg{\theta}})]$ is the
maximum likelihood estimate. The estimand $\f$ is an $n \times 1$
  vector. But how many parameters are we really estimating?

Equivalently we can think of the
distribution is in the family
\begin{equation}
\label{parametric}
\{N(\bB \bg{\theta},\sigma^2); \bg{\theta} \in {\mathbb R}^p\}
\end{equation}
and the maximum likelihood estimate for $\bg{\theta}$ is
$\hat{\bg{\theta}}$. Here $\bB$ is a matrix of basis elements defined soon...

Here we start seeing for the first time where the name {\it non-parametric}
comes from. How are the approaches (\ref{nonparametric}) and
(\ref{parametric}) different?

Notice that obtaining $\hat{\bg{\theta}}$ is easy because of the
linear model set-up. The ordinary least square estimate is
\[
(\bB'\bB)\hat{\bg{\theta}} = \bB'\bY
\]
where $\bB$ is is the $n \times p$ design matrix with elements
$[\bB]_{ij}=B_j(\bX_i)$. When this solution is unique we refer to
$g(x;\hat{\bg{\theta}})$ as the OLS projection of $\bY$ into $\cal G$
(as learned in the first term).

%%%Plagerized
\subsection{Parametric versus non-parametric}
In some cases, we have reason to believe that the function $f$ is
actually a member of some linear space $\cal G$. Traditionally,
inference for regression models depends on $f$ being representable
as some combination of known predictors. Under this assumption, $f$
can be written as a combination of basis elements for some value of
the coefficient vector $\bg{\theta}$. This provides a {\it
parametric} specification for $f$. No matter how many observations we
collect, there is no need to look outside the fixed,
finite-dimensional, linear space $\cal G$ when estimating $f$.

In practical situations, however, we would rarely believe such
relationship to be exactly true. Model spaces $\cal G$ are understood
to provide (at best) approximations to $f$; and as we collect more and
more samples, we have the freedom to audition richer and richer
classes of models. In such cases, all we might be willing to say about
$f$ is that it is {\it smooth} in some sense, a common assumption
being that $f$ have two bounded derivatives. Far from the assumption
that $f$ belong to a fixed, finite-dimensional linear space, we
instead posit a {\it nonparametric} specification for $f$. In this
context, model spaces are employed mainly in our approach to
inference; first in the questions we pose about an estimate, and then
in the tools we apply to address them. For example, we are less
interested in the actual values of the coefficient $\bg{\theta}$,
e.g. whether or not an element of $\bg{\theta}$ is significantly
different from zero to the 0.05 level. Instead we concern ourselves
with functional properties of $g(\bx; \hat{\bg{\theta}})$, the
estimated curve or surface, e.g. whether or not a peak is real.

To ascertain the local behavior of OLS projections onto approximation
spaces $\cal G$, define the pointwise, mean squared error (MSE) of
$\hat{g}(\bx) = g(\bx;\hat{\bg{\theta}})$ as
\[
\E \{ f(\bx) - \hat{g}(\bx) \}^2 = \bias^2\{\hat{g}(\bx)\} +
\var\{\hat{g}(\bx)\}
\]
where
\begin{equation}
\label{bias}
\bias\{\hat{g}(\bx)\} = f(x) - \E\{\hat{g}(\bx)\}
\end{equation}
and
\[
\var\{\hat{g}(\bx)\} = \E\{\hat{g}(\bx) - \E[\hat{g}(\bx)]\}^2
\]
When the input values $\{\bX_i\}$ are deterministic the expectations
above are with respect to the noisy observation ${Y_i}$. In practice,
MSE is defined in this way even in the random design case, so we look
at expectations conditioned on $\bX$.

When we do this, standard results in regression theory can be applied
to derive an expression for the variance term
\[
\var\{\hat{g}(\bx)\} = \sigma^2 \bB(\bx)'(\bB'\bB)^{-1}\bB(\bx)
\]
where $\bB(\bx) = (B_1(\bx),\dots,B_p(\bx))'$, and the error variance is
assumed constant.

Under the parametric specification that
$f \in \cal G$, what is the bias?

 This leads to
classical t- and F-hypothesis tests and associated parametric
confidence intervals for $\bg{\theta}$. Suppose on the other hand,
that $f$ is not a member of $\cal G$, but rather can be reasonably
approximated by an element in $\cal G$. The bias (\ref{bias}) now reflects
the ability of functions in $\cal G$ to capture the essential features
of $f$.

% where the dist$(f,{\cal G})$ is defined as
% \[
%  \mbox{dist}(f,{\cal G}) = \min_{g \in \cal G} ||f-g||,
% \]
% for some norm.
% An example of a norm that is used frequently is the $L_{\infty}$
% \[
% || f - g||_{\infty} = \sup_{\bx \in I} | f(x) - g(x) |.
% \]
% In general we characterize the contribution of bias to the MSE through
% the {\it approximation rate} obtained by $\cal G$ given our
% assumptions about the regularity or smoothness of $f$. Broadly, the
% better the approximation rate (the more flexible the space $\cal G$,
% the lower the bias. As we will see, however, we pay for improved
% approximation power with excess degrees of freedom (e.g. by moving
% from a linear to quadratic or cubic polynomial) and hence incur extra
% variability.
%\newpage


\vspace{-0.2em}
\section{The \tuner tuner}
\label{sec:sync_tuner}
\vspace{-0.2em}
%In this section we describe \tuner, our tuner for momentum SGD.
%We introduce a noisy quadratic model and work on a local quadratic approximation of $f(x)$ to apply the tuning rule of \eqref{eqn:noiseless_tuning_rule} to SGD on an arbitrary objective.
%\tuner is our implementation of that rule.

Here we describe our tuner for momentum SGD that uses the same learning rate for all variables.
We first introduce a noisy quadratic model $f(x)$ as the local approximation of an arbitrary one-dimensional objective. On this approximation, we extend the tuning rule of \eqref{eqn:noiseless_tuning_rule} to SGD. In section~\ref{sec:tuner}, \emph{we generalize the discussion to multidimensional objectives; it yields the \tuner tuning rule}.

\paragraph{Noisy quadratic model}
\label{sec:noisy_quadratics}

\newcommand{\oac}{origin-adjusted curvature }
\newcommand{\bx}{\bar{x}}

We consider a scalar quadratic
\begin{equation}
	f(x) = \frac{h}{2} x^2 + C
	= \sum_i \frac{h}{2n}(x-c_i)^2
	\triangleq \frac{1}{n} \sum_i f_i(x)
%	\quad \sum_i c_i = 0.
	\label{eqn:noise_quad_1d}
\end{equation}
with $\sum_i c_i = 0$. $f(x)$ is a quadratic approximation of the original objectives with $h$ and $C$ derived from measurement on the original objective. The function $f(x)$ is defined as the average of $n$ {\em component functions}, $f_i$.
This is a common model for SGD, where we use only a single data point (or a mini-batch) drawn uniformly at random, $S_t \sim \mathrm{Uni}([n])$ to compute a noisy gradient, $\nabla f_{S_t}(x)$, for step $t$.
Here, $C=\frac{1}{2n}\sum_i h c_i^2$ denotes the {\em gradient variance}.
As optimization on quadratics decomposes into scalar problems along the principal eigenvectors of the Hessian, the scalar model in~\eqref{eqn:noise_quad_1d} is sufficient to study local quadratic approximations of multidimensional objectives.
Next we get an {\em exact} expression for the mean square error after running momentum SGD on the scalar quadratic in~\eqref{eqn:noise_quad_1d} for $t$ steps.

\begin{lemma}
\label{lem:main_lemma}
Let $f(x)$ be defined as in \eqref{eqn:noise_quad_1d},
$x_1=x_0$ and $x_t$ follow the momentum update \eqref{eqn:momentum_gd} with stochastic gradients $\nabla f_{S_t}(x_{t-1})$ for $t \geq 2$.
Let $\mat{e}_1=[1, 0]^T$, the expectation of squared distance to the optimum $x^*$ is
	\begin{equation}
	\begin{aligned}
		\E (x_{t+1} - x^{*})^2 & = (\mat{e}^{\top}_1 \mat{A}^t [x_1 - x^{*}, x_0-x^{*}]^{\top})^2 \\
		& + \alpha^2 C \mat{e}^{\top}_1 (\mat{I} - \mat{B}^t)(\mat{I} - \mat{B})^{-1}\mat{e}_1	,
		\label{equ:squared_dist_exact}
	\end{aligned}
	\end{equation}
where the first and second term correspond to squared bias
and variance, and their corresponding momentum dynamics are captured by operators
	\begin{equation}
	\begin{gathered}
		\mat{A} = \begin{bmatrix}
		1-\alpha h + \mu & - \mu\\
		1 & 0 \\
		\end{bmatrix}, \\
%		\quad
		\mat{B} =
		\begin{bmatrix}
		(1-\alpha h + \mu)^2 &  \mu^2 & -2\mu(1-\alpha h + \mu)\\
		1 & 0 & 0 \\
		1-\alpha h + \mu & 0 & - \mu
		\end{bmatrix}.
		\label{equ:mat_def}
	\end{gathered}
	\end{equation}
\end{lemma}

\yell{
Even though it is possible to numerically work on~\eqref{equ:squared_dist_exact} directly,
we use a scalar, asymptotic surrogate in~\eqref{eqn:asymptotic_surrogate} based on the spectral radii of operators to simplify analysis and expose insights.
This decision is supported by our findings in Section~\ref{sec:momentum_operator}: the spectral radii can capture empirical convergence rate.}
\begin{equation}
\begin{aligned}
%	\E ( x_{t+1} - x^{*} )^2
%	\approx  \rho(\mat{A})^{2t} ( x_0 - x_{*} )^2
%		+ (1-\rho(\mat{B})^{t}) \frac{\alpha^2 C}{1-\rho(\mat{B})}
	&\E ( x_{t+1} - x^{*} )^2 \\
	\approx & \rho(\mat{A})^{2t} ( x_0 - x_{*} )^2
		+ (1-\rho(\mat{B})^{t}) \frac{\alpha^2 C}{1-\rho(\mat{B})}
	\label{eqn:asymptotic_surrogate}
\end{aligned}
\end{equation}

One of our design decisions for \tuner
is to always work in the robust region of Lemma~\ref{lem:robustness}.
We know that this implies a spectral radius $\sqrt{\mu}$ of the momentum operator, $\mat{A}$, for the bias.
Lemma~\ref{lem:spectral_var_control} shows that under the exact same condition, the variance operator $\mat{B}$ has spectral radius $\mu$.

\begin{lemma}
\label{lem:spectral_var_control}
The spectral radius of the variance operator, $\mat{B}$ is $\mu$, if ${(1-\sqrt{\mu})^2} \leq  \alpha h \leq {(1+\sqrt{\mu})^2}$.
\end{lemma}

As a result, the surrogate objective of \eqref{eqn:asymptotic_surrogate}, takes the following form in the robust region.
\begin{equation}
	\E ( x_{t+1} - x^{*} )^2
	\approx \mu^t ( x_0 - x^{*} )^2
		+ (1-\mu^t) \frac{\alpha^2 C}{1-\mu}
	\label{eqn:noisy_square_dist}
\end{equation}
We extend this surrogate to multidimensional cases to extract a noisy tuning rule for \tuner.

%\vspace{-0.25em}
\subsection{Tuning rule}
\label{sec:tuner}
\vspace{-0.25em}

In this section, we present \textsc{SingleStep}, the tuning rule of YellowFin (Algorithm~\ref{alg:basic-algo}). Based on the surrogate in~\eqref{eqn:noisy_square_dist}, \textsc{SingleStep} is a multidimensional SGD version of the noiseless tuning rule in~\eqref{eqn:noiseless_tuning_rule}. We first generalize~\eqref{eqn:noiseless_tuning_rule} and~\eqref{eqn:noisy_square_dist} to multidimensional cases, and then discuss \textsc{SingleStep}.% in details.

As discussed in Section~\ref{sec:robust_properties}, GCN $\nu$ captures the dynamic range of generalized curvatures in a one-dimensional objective with varying curvature. The consequent robust region described by~\eqref{eqn:noiseless_tuning_rule} implies homogeneous spectral radii.
%\emph{In multidimensional cases, we use a single learning rate and momentum for the entire model.}
On a multidimensional non-convex objective, each one-dimensional slice passing a minimum $x^*$ can have \emph{varying curvature}. As we use \emph{a single $\mu$ and $\alpha$ for the entire model}, if $\nu$ simultaneously captures the dynamic range of generalized curvature over all these slices, $\mu$ and $\alpha$ in~\eqref{eqn:noiseless_tuning_rule} are in the robust region for all these slices. This implies homogeneous spectral radii $\sqrt{\mu}$ according to Lemma~\ref{lem:robustness}, empirically facilitating convergence at a common rate along all the directions.

Given homogeneous spectral radii $\sqrt{\mu}$ along all directions, the surrogate in~\eqref{eqn:noisy_square_dist} generalizes on the local quadratic approximation of multiple dimensional objectives. On this approximation with minimum $x^*$, the expectation of squared distance to $x^*$, $\E \| x_0 - x^*\|^2$, decomposes into independent scalar components along the eigenvectors of the Hessian. We define gradient variance $C$ as the sum of gradient variance along these eigenvectors. The one-dimensional surrogates in~\eqref{eqn:noisy_square_dist} for the independent components sum to $\mu^t\| x_0 - x^* \|^2 + (1-\mu^t)\alpha^2 C / (1 - \mu)$, the \emph{multidimensional surrogate} corresponding to the one in~\eqref{eqn:noisy_square_dist}.
%Given homogeneous spectral radii $\sqrt{\mu}$ along all directions, the surrogate in~\eqref{eqn:noisy_square_dist} generalizes via multidimensional local quadratic approximations. Assuming the quadratic approximation aligns with standard axes, the expectation of squared distance to the optimum of the approximation, $\E\|x_t - x^*\|^2$, decomposes along the standard axes. As the initial squared distance $\| x_0 - x^*\|^2$ and gradient variance $C$ also decompose along the axes, the one dimensional surrogates along axes sums to $\mu^t\| x_0 - x^* \|^2 + (1-\mu^t)\alpha^2 C / (1 - \mu)$, the surrogates corresponding to the one in~\eqref{eqn:noisy_square_dist}.
%	Note for quadratic approximation not aligned with the axes, this \emph{multidimensional surrogates} is attained by decomposing along eigenvectors of the quadratic approximation's Hessian, instead of standard axes.

%%\begin{minipage}{0.5\linewidth}
%%\vspace{-0.25em}
%\begin{equation}
%\begin{aligned}
%	\textsc{(SingleStep)} \notag \\
%	 \mu_t, \alpha_t & = && \arg \min_{\mu} \mu D^2
%		+ \alpha^2 C \\
%	s.t. & &&\mu \geq \left(\frac{\sqrt{h_{\max}/h_{\min} }-1}{\sqrt{h_{\max}/h_{\min}}+1}\right)^2 \\
%	& &&\alpha = \frac{(1-\sqrt{\mu})^2}{h_{\min}}
%\end{aligned}
%\label{equ:noisy_min}
%\end{equation}
%%\end{minipage}
%\begin{minipage}{0.025\linewidth}
%\
%\end{minipage}
%\begin{minipage}{0.45\linewidth}
%\vspace{-1.5em}
%\begin{algorithm}[H]
%	\footnotesize
%	\caption{\jianedits{\tuner}}
%	\begin{algorithmic}
%	\State \textbf{state: } $\alpha \gets 1.0$, $\mu \gets 0.0$%, $w\gets20$
%	\Function{\tuner}{$\text{gradient } g_t$, $\beta$}
%	\State $h_{\max}, h_{\min} \gets \Call{CurvatureRange}{g_t, \beta}$
%	\State $C \gets \Call{Variance}{g_t, \beta}$
%	\State $D \gets \Call{Distance}{g_t, \beta}$
%
%	\State $\mu_t, \alpha_t \gets \Call{SingleStep}{C, D, h_{\max}, h_{\min}}$
%	\State $\mu \gets \beta \cdot \mu + (1 - \beta) \cdot \mu_t$
%	\State $\alpha \gets \beta \cdot \alpha + (1 - \beta) \cdot \alpha_t$ %\Comment{Smoothing learning rate and momentum for stable control}
%	\Return $\mu, \alpha$
%	\EndFunction
%	\end{algorithmic}
%	\label{alg:basic-algo}
%\end{algorithm}
%\end{minipage}
%%%%%%%%%%%%%%%%%%%%%%%%% new algorithm %%%%%%%%%%%%%%%%%%%%%%%%
%%\begin{minipage}{0.475\linewidth}
%%\vspace{-0.25em}
%\begin{algorithm}[H]
%	\footnotesize
%	\caption{\jianedits{\tuner}}
%	\begin{algorithmic}
%%	\State \textbf{state: } $\alpha \gets 1.0$, $\mu \gets 0.0$%, $w\gets20$
%	\Function{\tuner}{$\text{gradient } g_t$, $\beta$}
%	\State $h_{\max}, h_{\min} \gets \Call{CurvatureRange}{g_t, \beta}$
%	\State $C \gets \Call{Variance}{g_t, \beta}$
%	\State $D \gets \Call{Distance}{g_t, \beta}$
%
%	\State $\mu_t, \alpha_t \gets \Call{SingleStep}{C, D, h_{\max}, h_{\min}}$
%%	\State $\mu_t, \alpha_t \gets \Call{SingleStep}{C, D, h_{\max}, h_{\min}}$
%%	\State $\mu \gets \beta \cdot \mu + (1 - \beta) \cdot \mu_t$
%%	\State $\alpha \gets \beta \cdot \alpha + (1 - \beta) \cdot \alpha_t$ %\Comment{Smoothing learning rate and momentum for stable control}
%	\Return $\mu_t, \alpha_t$
%	\EndFunction
%	\end{algorithmic}
%	\label{alg:basic-algo}
%\end{algorithm}
%%\end{minipage}
%\begin{minipage}{0.475\linewidth}
%\vspace{-0.25em}
\vspace{-0.25em}
\begin{algorithm}[h]
%	\footnotesize
	\caption{\jianedits{\tuner}}
	\begin{algorithmic}
%	\State \textbf{state: } $\alpha \gets 1.0$, $\mu \gets 0.0$%, $w\gets20$
	\Function{\tuner}{$\text{gradient } g_t$, $\beta$}
	\State $h_{\max}, h_{\min} \gets \Call{CurvatureRange}{g_t, \beta}$
	\State $C \gets \Call{Variance}{g_t, \beta}$
	\State $D \gets \Call{Distance}{g_t, \beta}$

	\State $\mu_t, \alpha_t \gets \Call{SingleStep}{C, D, h_{\max}, h_{\min}}$
%	\State $\mu_t, \alpha_t \gets \Call{SingleStep}{C, D, h_{\max}, h_{\min}}$
%	\State $\mu \gets \beta \cdot \mu + (1 - \beta) \cdot \mu_t$
%	\State $\alpha \gets \beta \cdot \alpha + (1 - \beta) \cdot \alpha_t$ %\Comment{Smoothing learning rate and momentum for stable control}
	\Return $\mu_t, \alpha_t$
	\EndFunction
	\end{algorithmic}
	\label{alg:basic-algo}
\end{algorithm}
\vspace{-0.25em}
%\end{minipage}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%% new algorithm %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{table*}[t]
\begin{minipage}{0.37\textwidth}
\vspace{-1em}
\algrenewcommand\alglinenumber[1]{\scriptsize #1:}
	\begin{algorithm}[H]
	\small
	\setstretch{1.01}
	\caption{\small Curvature range}
	\begin{algorithmic}
		\State \textbf{state: } $h_{\max}$, $h_{\min}$, $h_i, \forall i \in\{1,2,3,...\}$
		\Function{CurvatureRange}{gradient $g_t$, $\beta$}
			\State $h_t \gets \| g_t \|^2$
			\State $h_{\max,t}\gets\!\!\!\max\limits_{t - w \leq i \leq t}\!h_i$, $h_{\min,t}\gets\!\!\!\min\limits_{t - w \leq i \leq t}\!h_i$
			\State $h_{\max} \gets \beta \cdot h_{\max} + (1 - \beta) \cdot h_{\max,t}$ %\hfill Smoothed largest curvature.
			\State $h_{\min} \gets \beta \cdot h_{\min} + (1 - \beta) \cdot h_{\min,t}$ %\hfill Smoothed smallest curvature.
%			\State $h_{\max} \gets \beta \  h_{\max} + (1 - \beta) \  h_{\max,t}$ %\hfill Smoothed largest curvature.
%			\State $h_{\min} \gets \beta \ h_{\min} + (1 - \beta) \ h_{\min,t}$ %\hfill Smoothed smallest curvature.
			\Return $h_{\max}$, $h_{\min}$
		\EndFunction
	\end{algorithmic}
	\label{alg:curv_func}
	\end{algorithm}
\end{minipage}
\begin{minipage}{0.315\textwidth}
\vspace{-1em}
\algrenewcommand\alglinenumber[1]{\scriptsize #1:}
	\begin{algorithm}[H]
	\small
	\setstretch{1.5}
	\caption{\small Gradient variance}
	\begin{algorithmic}
	\State \textbf{state: } $\overline{g^2}\gets0$, $\overline{g}\gets0$
	\Function{Variance}{gradient $g_t$, $\beta$}
		\State $\overline{g^2}\gets\beta \cdot \overline{g^2} + (1 - \beta) \cdot g_t \odot g_t$
		\State $\overline{g}\gets\beta \cdot \overline{g} + (1 - \beta) \cdot g_t$
%		\Return $\| \overline{g^2} - \overline{g}^2 \|_1$ %\hfill Sum of elements in the vect
		\Return $\bm{1}^T\!\!\cdot\left(\overline{g^2} - \overline{g}^2\right)$ %\hfill Sum of elements in the vect
	\EndFunction
	\end{algorithmic}
	\label{alg:var_func}
	\end{algorithm}
\end{minipage}
\begin{minipage}{0.3\textwidth}
\vspace{-1em}
\algrenewcommand\alglinenumber[1]{\scriptsize #1:}
	\begin{algorithm}[H]
	\small
	\setstretch{1.25}
	\caption{\small Distance to opt.}
	\begin{algorithmic}
	\State \textbf{state: } $\overline{\|g\|}\gets0$, $\overline{h}\gets0$
		\Function{Distance}{gradient $g_t$, $\beta$}
		\State $\overline{\|g\|}\gets \beta \cdot \overline{\|g\|} + (1 - \beta) \cdot \|g_t\|$
		\State $\overline{h} \gets \beta \cdot \overline{h} + (1 - \beta) \cdot \| g_t \|^2$
		\State $D \gets \beta \cdot D + (1 - \beta) \cdot \overline{\|g\|} /\overline{h}$
		\Return $D$
	\EndFunction
	\end{algorithmic}
	\label{alg:dist_func}
	\end{algorithm}
\end{minipage}
\end{table*}
%%%%%%%%%%%%%%%%%%%%%%%%%%%% end of new algorithm

\begin{wrapfigure}[9]{R}{0.55\linewidth}
\vspace{-2.5em}
\hspace{-1em}
\begin{minipage}{\linewidth}
	\begin{equation}
	\begin{aligned}
	&\textsc{(SingleStep)} \\
	 \mu_t, \alpha_t = & \arg \min_{\mu} \mu D^2
		+ \alpha^2 C \\
	s.t.\  \mu \geq & \left(\frac{\sqrt{h_{\max}/h_{\min} }-1}{\sqrt{h_{\max}/h_{\min}}+1}\right)^2 \\
	\alpha =& \frac{(1-\sqrt{\mu})^2}{h_{\min}}
	\end{aligned}
	\label{equ:noisy_min}
	\end{equation}
\end{minipage}
\end{wrapfigure}
Let $D$ be an estimate of the current model's distance to a local quadratic approximation's minimum, and $C$ denote an estimate for gradient variance.
\textsc{SingleStep} minimizes the \emph{multidimensional surrogate} after a single step (i.e. $t=1$) while ensuring $\mu$ and $\alpha$ in the robust region for all directions. \emph{A single instance of \textsc{SingleStep} solves a single momentum and learning rate for the entire model at each iteration.}
Specifically, the extremal curvatures $h_{min}$ and $h_{max}$ denote estimates for the largest and smallest generalized curvature respectively. They are meant to capture both generalized curvature variation along all different directions (like the classic condition number)
and also variation that occurs as the {\em landscape evolves}. The constraints keep the global learning rate and momentum in the robust region (defined in Lemma~\ref{lem:robustness})
for slices along all directions.
%along eigenvectors of the quadratic approximation's Hessian.
%\textsc{SingleStep} can be solved in closed form; we refer to Appendix~\ref{sec:opt} for relevant details on the closed form solution.

The problem in~\eqref{equ:noisy_min} does not need iterative solver but has an analytical solution. Substituting only the second constraint, the objective becomes $p(x)=x^2D^2 + (1-x)^4/h_{\min}^2C$ with $x=\sqrt{\mu} \in [0, 1)$. By setting the gradient of $p(x)$ to 0, we can get a cubic equation whose root $x=\sqrt{\mu_p}$ can be computed in closed form using Vieta's substitution. As $p(x)$ is uni-modal in $[0, 1)$, the optimizer for \eqref{equ:noisy_min} is exactly the maximum of $\mu_p$ and $(\sqrt{h_{\max}/h_{\min} }-1 )^2 / (\sqrt{h_{\max}/h_{\min}}+1)^2$, the right hand-side of the first constraint in~\eqref{equ:noisy_min}.

\tuner uses functions \textproc{CurvatureRange}, \textproc{Variance} and \textproc{Distance} to measure quantities $h_{\max}$, $h_{\min}$, $C$ and $D$ respectively. These measurement functions can be designed in different ways.
We present the implementations we used for our experiments,
based completely on gradients,  in Section~\ref{sec:oracles}.

%Let $D$ denote an estimate of the current model's distance to a local quadratic approximation's minimum, and $C$ denote an estimate for gradient variance. The extremal curvatures $h_{min}$ and $h_{max}$ denote estimates for the largest and smallest generalized curvature respectively. They are meant to capture both local curvature variation along all different directions (like the classic condition number)
%and also variation that occurs as the {\em landscape evolves}. Thus the constraints keep the global learning rate and momentum in the robust region (defined in Lemma~\ref{lem:robustness}) along all eigendirections of the quadratic approximation. According to Lemma~\ref{lem:robustness} and~\ref{lem:spectral_var_control}, these constraints support the decomposition of \textsc{SingleStep} objective, as the sum of ~\eqref{eqn:noisy_square_dist} (with $t=1$) along the eigendirections of the quadratic approximation.
%\textsc{SingleStep} can be solved in closed form; we refer to Appendix~\ref{sec:opt} for discussion on the closed form solution.
%\tuner uses functions \textproc{CurvatureRange}, \textproc{Variance} and \textproc{Distance} to measure quantities $h_{\max}$, $h_{\min}$, $C$ and $D$ respectively. These measurement functions can be designed in different ways.
%We present the implementations we used for our experiments,
%based completely on gradients,  in Section~\ref{sec:oracles}.

%\textsc{SingleStep} minimizes the surrogate for the expected squared distance from the optimum of a local quadratic approximation  \eqref{eqn:noisy_square_dist} after a single step ($t=1$),
%while keeping all directions in the robust region \eqref{eqn:robust_region}.
%This is the SGD version of the noiseless tuning rule in \eqref{eqn:noiseless_tuning_rule}.
%It can be solved in closed form; we refer to Appendix~\ref{sec:opt} for discussion on the closed form solution.
%\tuner uses functions \textproc{CurvatureRange}, \textproc{Variance} and \textproc{Distance} to measure quantities $h_{\max}$, $h_{\min}$, $C$ and $D$ respectively. These measurement functions can be designed in different ways.
%We present the implementations we used for our experiments,
%based completely on gradients,  in Section~\ref{sec:oracles}.

\input{oracles}


\documentclass[11pt]{scrartcl}
\usepackage{atonu}
\usepackage{fullpage}
\usepackage{amsmath,amsthm, amsfonts}

\begin{document}
\title{Diophantine Equations}
\subtitle{BdMO National Camp 2021}
\author{\scshape{Atonu Roy Chowdhury} \\
\mailto{atonuroychowdhury@gmail.com}}
\date{\today}
\maketitle

\section{What is Diophantine Equation?}
In our school textbooks, we often solve equations. But most of the equations are ``algebraic''. By ``algebraic'', I mean you had to solve them for real numbers. But in Diophantine Equations, we are given an equation and we have to find the integer solutions or positive integer solutions of that equation. Sounds fun, right? Alright, let's dive into some diophantine equations, shall we?
% Some historical remark: Diophantine equation is named after greek mathematician Diophantus. In his book \textit{Arithmetica}, he discussed about the solution of algebraic equations and the theory of numbers. It contains about 150 peoblems about solvability of equations. There were some cool number theoretic results, such that ``no integer of the form \(8n+ 7\) can be written as the sum of three squares.'' Diophantus is the first known human to work with integer solutions of equations. That's why Diophantine equation is named after him. \\

\section{Linear Diophantine Equations}
If I ask you, ``How many solutions are there to the equation \(3x+6y=7\)?'' you'll probably say that ``There are infinitely many solutions. \(3x+6y=7\) denotes a line in the Cartesian coordinate so it contains infinitely many points.'' And you are absolutely correct. But things get more interesting when I tell you to find the integer solutions.\\
Well, if you play around with things a bit, you will find that there is \textbf{no} integer solution to this equation. Because the LHS is \(3x+6y = 3(x+2y)\) which is obviously divisible by \(3\) as \(x,y\) are integers. But the RHS is \(7\), which is not divisible by 7. Hence, a contradiction. \\
Now, if the RHS was divisible by \(3\), for instance \(\text{RHS} = 9\), would the equation have integer solutions? If yes, how many? \\
Well, one solution is very easy to find, that is \(x=1, y=1\). But are there other solutions? Turns out, there are. \(x=-1, y=2\) is another possible solution. If you work around with things, you will find that there are actually infinitely many solutions to this particular equation. \\
Now a natural question arises: when does a linear diophantine equation have solutions? To find the answer, we need Bezout's Identity.
\begin{theorem}[Bezout's Identity]
Let \(a\) and \(b\) be positive integers and \(\gcd(a,b)=d\). Then there exists some \textbf{integer} \(x\) and \(y\) such that
\[ax+by = d\]
\end{theorem}
This is actually not that hard to prove. We can use extremal principle to prove this.
\begin{proof}
Let \(S\) be the set of all positive integers that can be written as \(ax+by\) for some \textbf{integer} \(x\) and \(y\). That is
\[S = \left\{ n\in \NN : n = ax+by \text{ for some integer } x \text{ and } y \right\} \]
Since it's a subset of \(\NN\), there exists a smallest value in this set. Let \(m = \min(S)\). We claim that \(m = d\). \\
To show that \(m=d\), it's enough to show that \( m \mid d\) and \(d \mid m\). The latter is actually easy to see. Since \(m \in S\), \(m = an + bk\) for some integer \(n\) and \(k\).
\[d = \gcd(a,b) \implies d \mid a, d \mid b \implies d\mid an + bk \implies d \mid m\]
Now we wanna show the other direction. If we divide \(a\) by \(m\), we shall get some quotient and some remainder. So, \(a = qm + r\), where \(0 \leq r < m\). Now,
\[ r = a - qm = a - q (an + bk) = a (1-qn) - b(qk)\]
Therefore, \(r\) can also be written as the form \(ax+by\). If \(r>0\), then \(r \in S\) which contradicts the minimality of \(m\). Therefore \(r=0\) and hence \(m \mid a\). In a similar manner, it can be shown that \(m \mid b\). Therefore \(m \mid \gcd(a,b)=d\) and we are done.
\end{proof}
\begin{corollary}
The equation \(ax+by=c\) has solutions in integer if and only if \(\gcd(a,b) \mid c\).
\end{corollary}
This follows immediately from Bezout's identity. So I'm not gonna state the proof here. Now we will see that, whenever a linear diophantine equation has solution, it actually has infinitely many solutions. Furthermore, the solutions have a common form.
\begin{lemma}
If \((x_0, y_0)\) is a solution to \(ax+by = c\), then all the solutions of this equation are of the form \[x = x_0 + t\frac{b}{d}, \quad y= y_0 - t\frac{a}{d}\]
where \(d\) is the gcd of \(a\) and \(b\).
\end{lemma}
\begin{proof}
\(d = \gcd(a,b) \implies a = da', b=db'\) where \(\gcd(a,b)=1\). \((x_0, y_0)\) is a solution to \(ax+by = c\), let \((x_1, y_1)\) be another solution. Then,
\begin{equation*}
\begin{split}
&\textcolor{white}{\implies} ax_1+by_1 = c = ax_0+by_0 \\
&\implies da'(x_1- x_0) = db'(y_0 - y_1) \\
&\implies a'(x_1- x_0) = b'(y_0 - y_1) \\
&\implies a'\mid y_0 - y_1 , \quad b' \mid x_1- x_0 \\
&\implies y_1 = y_0 - a't , \quad x_1 = x_0 + b't
\end{split}
\end{equation*}
Hence, we are done.
\end{proof}
Now we wish to apply this into a real problem.
\begin{exercise}
Suppose you went to a restaurant where they sell Chicken Nuggets in packs of \(9\) and packs of \(20\). What is the largest number of nuggets that you can't get from that restaurant?
\end{exercise}
The problem is basically asking that, what is the largest value of \(n\) such that \(n = 9x + 20 y\) does not have any solution in non-negative integers? \(9\) and \(20\) are fairly small numbers. So you can find out by getting your hand dirty that the highest number of nuggets that you can't buy from that restaurant is \(151\). But if I ask you the same question with \(289\) and \(475\) instead of \(9\) and \(20\), can you still find it out by getting your hand dirty? I suppose not. So we need some kind of general formula for it. And that general formula is \textit{Chicken-McNugget Theorem}.
\begin{theorem}[Chicken-McNugget Theorem]
Let \(a\) and \(b\) be two coprime positive integers. Suppose \(f(a,b)\) denotes the largest number \(n\) such that the equation \(n = ax+by\) has no solution in non-negative integers. Then \[f(a,b) = ab - a - b\]
\end{theorem}
\begin{proof}
The proof consists of two parts. The first part is showing that \(n = ab - a - b\) leads us no solution in non-negative integers to the equation \(n = ax+by\). The second part is showing that for every \(n> ab - a - b\), we can always find a solution in non-negative integers to the equation \(n = ax+by\). \\
For the first part, assume for the sake of contradiction that there exists some non-negative integers \(x\) and \(y\) such that \(ax + by = ab - a - b\). Taking the equation in \(\text{mod }a\), we get
\[by \equiv -b \amod{a} \implies y \equiv -1 \amod{a} \implies \boxed{y \geq a-1}\]
Here we could divide both sides of the modular equation by \(b\) because \(b\) is coprime to \(a\). Similarly, by taking \(\text{mod }b\), we will get that \(\boxed{x \geq b-1}\). Therefore,
\[ab - a - b = ax + by \geq a(b-1) + b(a-1) = 2ab - a -b\]
Contradiction!\\
Now for the second part, consider any integer \(n > ab - a -b\). Since \(\gcd(a,b)=1\), by \textit{Bezout's Identity} we can find integers \(x'\) and \(y'\) such that \(ax' + by' = 1\). Multiplying both sides by \(n\), we get an integer solution to the solution \(ax+by=n\).
\[ax' + by' = 1 \implies a(x'n) + b(y'n) = n \implies ax_0 + by_0 = n\]
But we had to find non-negative integer solution to this equation. No worries, we showed in \textit{Lemma 2.3} that, if we have one solution to the linear diophantine equation then we can find all the solutions. So the general solution to \(ax+by = n\) is given by
\[x = x_0 + tb, \quad y = y_0 - ta\]
So we need to show that, upon choosing the correct \(t\), we can make both \(x\) and \(y\) non-negative.
\[ax+by = n > ab-a-b \implies \boxed{b(y+1) > a(b -1 -x)}\]
Therefore, \(y+1\) is positive (in other words, \(y\) is non-negative) if \(b-1 \geq x\). So if we can keep \(x\) between \(0\) and \(b-1\) inclusive, then we are basically done. \\
In fact, it's not actually hard to achieve. If we divide \(x_0\) by \(b\), we get some quotient and remainder. So \(x_0 = qb + r\). Notice that, remainder is always non-negative, so \(0\leq r \leq b-1\). Now if we choose \(t = -q\), then \(x = x_0 + tb = qb + r -qb = r\). Thus we can achieve \(0 \leq x \leq b-1\). \\
Now, the conclusion becomes trivial.
\[b(y+1) > a(b -1 -x) \geq 0 \implies y+1 >0 \implies y \geq 0\]
So \(ax+by=n\) has solution in non-negative integers.
\end{proof}
Now, what about equations with more than \(2\) variables? Let's see an example.
\begin{exercise}
Find all integer solutions of the following equation:
\[3x+4y+5z=6\]
\end{exercise}
\begin{soln}
Here we have 3 variables, but we can make it 2-variable equation. One way to do it is taking the mod of any coefficient. Generally it's a good practice to take mod of the highest coefficient. So taking mod \(5\), we get
\[3x + 4y \equiv 1 \amod 5 \implies \boxed{3x + 4y = 1 + 5s} \implies 6 - 5z = 1+5s \implies \boxed{ z = 1-s}\]
Now we have a 2-variable linear diophantine equation to deal with: \(3x + 4y = 1 + 5s\). Can you find one solution to this equation? A bit of trial and error gives us \(x = -1+3s , y = 1-s\) is one solution. So by \textit{Lemma 2.3} we can get all the solutions:
\[x = -1+3s + 4t , y = 1-s -3t , z = 1-s\]
It is easy to verify that these values indeed satisfy the equation.
\end{soln}

\section{Whenever in confusion, factor it out}
Factoring is often useful in solving diophantine equations. For instance, if you have an equation like \(xy = 6\), then you can reduce it into 4 cases: \(x=1, y=6\); \(x=2, y=3\); \(x=3, y=2\); \(x=6, y=1\). And this might often reduce the complexity of the problem.
\begin{exercise}
Solve in positive integers:
\[xyz+x+y+z = 2 + xy+yz+zx\]
\end{exercise}
\begin{soln}
If we isolate the variables,
\[xyz+x+y+z -xy -yz -zx= 2\]
Let's try to factorize the LHS.
\[x(yz+1 -y - z) - (yz -y - z) = 2\]
If we had a \(-1\) in the LHS, then we could factorize it easily. So let's borrow a \(-1\) from the RHS.
\[x(yz+1 -y - z) - (yz -y - z +1) = 1 \implies (x-1)(y-1)(z-1)=1\]
\(x,y,z\) are positive integers, so \(x-1, y-1, z-1\) are non-negative integers. Three non-negative integer's product can be \(1\) only if they are all \(1\). Therefore,
\[x-1 = y-1 = z-1 =1 \implies \boxed{x=y=z=2}\]
\end{soln}
There is a popular factoring trick in Olympiad Folklore. It's popularly known as \textbf{SFFT} or \textit{Simon's Favorite Factoring Trick}. What does this trick do? It basically factorises an equation of the form \[Axy + Bx + Cy + D =0\]
After factorizing, things get easier to work with. The best idea of illustrating this trick would be showing an example.
\begin{exercise}
Find all primes \(p,q,r\) such that
\[pqr = 19(p+q+r)\]
\end{exercise}
\begin{soln}
The RHS is divisible by \(19\), which is a prime number. The LHS is the product of three prime numbers. So one of them must be \(19\). WLOG, \(r = 19\). So the equation becomes
\[pq = p+q+19 \implies pq-p-q=19\]
If we had a \(1\) in the LHS, then we could factorize it without any trouble. So let's add \(1\) on both sides:
\[pq-p-q = 19 \implies pq-p-q+1=20 \implies (p-1)(q-1)=20\]
The rest is left as an exercise for the reader.
\end{soln}
\begin{exercise}
Find the smallest value of \(n\) for which the following equation has \(69\) different solutions for \((x,y)\):
\[\frac1x + \frac1y = \frac1n\]
\end{exercise}
\begin{soln}
Let's try to isolate the variables in the given equation:
\[\frac1x + \frac1y = \frac1n \implies \frac{x+y}{xy}= \frac{1}{n} \implies xy - nx -ny =0\]
As we try to factorize this, we feel the absence of \(n^2\). We have the liberty to add it on both sides, so why don't we do it?
\[xy - nx -ny =0 \implies xy - nx -ny +n^2=n^2 \implies \boxed{(x-n)(y-n) = n^2}\]
Now, we have to find out the smallest \(n\) for which there are \(69\) different pairs of \((x,y)\) satisfying \((x-n)(y-n) = n^2\). If you play around things a bit, you'll get that, the number of solutions is precisely the number of divisors of \(n^2\). So the question now translates into: find the smallest \(n\) with \(\tau(n^2)=69\).\\
\(69 = 1\times 69 = 3 \times 23\). So the possible values for \(n^2\) are either \(p^{68}\) and \(p^{22}q^2\) where \(p\) and \(q\) are primes. The smallest value of \(p^{68}\) is \(2^{68}\). The smallest value of \(p^{22}q^2\) is \(2^{22}3^2\). As \(2^{68} > 2^{22}\ 3^2\), we can conclude that, \(2^{22}\ 3^2\) is the smallest possible value of \(n^2\). Therefore, the smallest possible value of \(n\) is \(2^{11}\ 3\).
\end{soln}

\section{The Legend, The Myth -- The ``Chipa'' Trick}
The ``Chipa'' Trick is a bounding strategy for solving diophantine equations. It's specially useful when the problem statement looks like this: ``Find all integer \(x\) such that \(f(x)\) is a perfect \(n\)-th power.'' In that case if you can show that \((y+1)^n < f(x) < y^n\), then you can conclude that no such \(x\) exists. Because if there existed such \(x\), \(f(x)\) couldn't lie strictly between two consecutive \(n\)-th power. \\
In this trick, we try to show something like this: \(X \leq Y \leq Z\), which can be interpreted as \(Y\) inside the ``Chipa'' of \(X\) and \(Z\). That's why this trick is known as ``Chipa'' trick in BdMO camps. Let's try some problems using this trick.
\begin{exercise}
Find all positive integers such that \(n^2 -19n +89\) is a perfect square.
\end{exercise}
\begin{soln}
The given expression is \(n^2 -19n +89\). Here \(19\) is an odd number. If it were even, we could try something like this: \(n^2 - 2kn + k^2\). But as it's odd number, it's inside the ``chipa'' of two even numbers. So,
\[n^2 -20n < n^2 -19 n < n^2 - 18n\]
The constants don't matter much. Therefore, after some \(n\), we shall get that,
\[ n^2 -20n +100< n^2 -19 n + 89< n^2 - 18n + 81 \implies (n-10)^2 < n^2 -19n +89 < (n-9)^2\]
We got our ``chipa''! Now if you play with this inequality a bit, you will find that this inequality holds for \(n>11\). That means, when \(n\geq 12\), \(n^2 -19n +89 \) lies strictly between two consecutive squares, so it can't be square. \\
We still have a bit of labour left, We have to calculate by hand for \(n = 1\) to \(11\). Checking these, we get that, only \(n=8\) and \(n=11\) makes \(n^2 -19n +89\) a perfect square.
\end{soln}
\begin{exercise}
Find all positive integers \(x,y,z\) such that
\[x^2 + y^2 + z^2 + 2xy + 2y(z-1) + 2x(z+1)\]
is a perfect square.
\end{exercise}
\begin{soln}
Let \(x^2 + y^2 + z^2 + 2xy + 2y(z-1) + 2x(z+1) = n^2\). The ``chipa'' is fairly easy to find here. Because the given expression kinda looks like \((x+y+z)^2\), but with \(z+1\) and \(z-1\) instead of \(z\). So the ``chipa'' is:
\[(x+y+z-1)^2 < n^2 < (x+y+z+1)^2\]
So \(n\) must be \(x+y+z\). Substituting this, we shall get that \(x = y\). So \((x,y,z)=(m,m,k)\) is all the solutions.
\end{soln}
\begin{exercise}
Find all solutions in positive integers of the equation
\[
x^{3}+(x+1)^{3}+(x+2)^{3}+\cdots+(x+7)^{3}=y^{3}
\]
\end{exercise}
\begin{soln}
It's not hard to see that \(x^{3}+(x+1)^{3}+(x+2)^{3}+\cdots+(x+7)^{3} = 8x^3 + 84x^2 + +420x + 784\). So you get the idea that the ``chipa'' should be \((2x+a)^3 < y^3 < (2x+b)^3\). You should be able to figure out what \(a\) and \(b\) should be. I'll leave the rest as an exercise for you.
\end{soln}

\section{Discriminant}
You've probably learned about quadratic equation \(ax^2 + bx + c =0\) in high school. The solution of this kind of 2-degree equation is given by:
\[x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}\]
Notice that, the nature of the solutions depend on \(b^2-4ac\). This expression is called discriminant. We want integer solutions here. So we must have a perfect square discriminant. \[\Delta = b^2 -4ac = k^2\]
This trick is often useful. When we have 2 variables, we can find the value of one variable using \textbf{discriminant is perfect square}. Then solving for the other variable becomes a much easier job.
\begin{exercise}
Find all positive integer \(n\) such that \(n^2 - 59n + 881\) is a perfect square.
\end{exercise}
I believe many of you can solve this problem using ``chipa'' trick. So I'm gonna show a solution using discriminants.
\begin{soln}
\(n^2 - 59n + 881 = k^2 \implies n^2 - 59n + 881 - k^2 =0\). Now if we treat \(n\) as variable and \(k\) as constant, then the equation becomes a quadratic. So the discriminant must be a perfect square.
\[a^2 = \Delta = 59^2 - 4 (881-k^2) = 4k^2 - 43 \implies 43= 4k^2 - a^2 = (2k+a)(2k-a)\]
which gives us \(k=11\). Plugging this into our main equation,
\[n^2 - 59n + 881 = 121 \implies n^2 - 59n +760 = 0\]
which has solutions \(n=19\) and \(n=40\).
\end{soln}
\begin{exercise}
Solve in positive integers:
\[(x^2+y)(x+y^2)=(x-y)^3\]
\end{exercise}
\begin{soln}
Expanding out, we get
\begin{equation*}
\begin{split}
&\textcolor{white}{\implies} (x^2+y)(x+y^2)=(x-y)^3 \\
&\implies x^3 + y^3 + x^2 y^2 + xy = x^3 - y^3 - 3 x^2 y + 3xy^2 \\
&\implies y(y^2 + x^2 y + x ) = y(-y^2 -3x^2 + 3xy)\\
&\implies y^2 + x^2 y + x +y^2 +3x^2 - 3xy = 0\\
&\implies 2y^2 + (x^2 -3x) y + (3x^2 +x) = 0
\end{split}
\end{equation*}
This is a quadratic equation on \(y\). So the discriminant \(\Delta = (x^2 -3x)^2 - 4\cdot 2 (3x^2 +x)\) must be a perfect square. The rest is left as an exercise for the reader.
\end{soln}

\section{Infinite Descent}
Infinite descent is often a very useful trick to solve diophantine equations. In this trick, we assume that some solution exist and we take the solution with least sum. Then we show that there exists another solution with even less sum. Hence we arrive at a contradiction. Let's jump into some examples:
\begin{exercise}
Prove that the equation \(a^2+b^2= 3c^2\) has no solutions in positive integers.
\end{exercise}
\begin{soln}
Assume for the sake of contradiction that some solutions exist. We take one such solution \(a_1, b_1, c_1\) such that \(a_1 + b_1 + c_1\) is the smallest. \\
Notice that \(\qr(3) = \left\{ 0,1 \right\} \). The RHS is divisible by \(3\), hence \(3 \mid a_1^2 + b_1^2\). Then it's easy to see that, we must have \(a_1^2 \equiv 0 \amod 3\) and \(b_1^2 \equiv 0 \amod 3\). In other words, \(3 \mid a_1\) and \(3\mid b_1\). Substituting \(a_1 = 3a_2\) and \(b_1 = 3b_2\) we get,
\[
	(3a_2)^2 + (3b_2)^2 = 3c_1^2 \implies 3 (a_2^2+b_2^2) = c_1^2 \implies 3 \mid c_1
\]
Now, substituting \(c_1 = 3c_2\), we get
\[
	3 (a_2^2+b_2^2) = (3c_2)^2 \implies a_2^2+b_2^2 = 3c_2^2
\]
Here \(a_2 = \frac{a_1}{3}, b_2 = \frac{b_1}{3}, c_2 = \frac{c_1}{3}\) and \((a_2, b_2, c_2)\) is also a solution to the give equation. So we found a solution with even less sum than the least sum. Hence contradiction!
\end{soln}
\begin{exercise}
Solve in positive integers: \(x^3 + 2y^3 = 4z^3\)
\end{exercise}
The solution is left as an exercise for the reader.

\section{A Magical Mod}
These type of problems require a magical mod. Like if you take mod \(n\) on both sides, you might arrive at some contradiction, or you might get some new information about the problem. But when you are reading the solution, you might be wondering: how did this specific mod came out of nowhere? Well, I'm gonna try to explain the intuitions behind these mods.
\begin{exercise}
Let \(d\) be any positive integer not equal to \(2, 5, 13\). Show that one can find distinct \(a, b\) in the set \(\{2,5,13, d\}\) such that \(ab−1\) is not a perfect square.
\end{exercise}
The problem is basically saying that: whatever \(d\) is, not all of \(2d-1, 5d-1, 13d-1\) are perfect squares. So intending to show a contradiction, we assume otherwise. We got three diophantine equations to solve:
\begin{equation*}
\begin{split}
2d-1 &= x^2 \\
5d -1 &= y^2 \\
13d-1 &= z^2
\end{split}
\end{equation*}
Here we have perfect squares to deal with. We need a magical mod. What should it be? Keep in mind that, our magical mod should have a relatively smaller quadratic residue class. Because if the quadratic residue class is too large, then we will have LOTS of cases to consider. That's why primes are not a good candidate for this. Because
\[ \abs{\qr(p)} = 1 + \frac{p-1}{2}\]
which is not really much of improvement. Turns out powers of \(2\) are the best candidates. It can be proved that
\[\abs{ \qr \left(2^n\right) } = \ceiling{\frac{2^n}{6}} + 1\]
which is almost thrice as good as primes. So the lesson from this problem is: \textbf{Whenever you have squares to deal with, try taking mods of power of \(2\). Such as: 4, 8, 16 etc.}. Let's dive into the solution then.
\begin{soln}
We have to show that, there does not exist any \(d\) that satisfies all of the following equations:
\begin{equation*}
\begin{split}
2d-1 &= x^2 \\
5d -1 &= y^2 \\
13d-1 &= z^2
\end{split}
\end{equation*}
You can try taking mod \(4\) or mod \(8\). But they don't produce any contradiction. Do we give up? \textbf{NO!} We can try taking mod \(16\). It's not hard to verify that
\[ \qr(16) = \left\{ 0,1,4,9 \right\}  \]
Therefore, \(2d-1\) is a perfect square if \(2d-1 \in \left\{ 1,9 \right\} \amod{16}  \implies d \in \left\{ 1,5 \right\} \amod{8} \implies \boxed{d \in \left\{ 1,5,9,13 \right\} \amod{16}}\). \\
\(5d-1\) is a perfect square if \(5d-1 \in \left\{ 0,1,4,9 \right\}\). If you work with this, you'll find that this is equivalent to \(\boxed{d \in \left\{ 1,2,10,13 \right\} \amod{16}} \). Similarly, \(13d-1\) is a perfect square if \(\boxed{d \in \left\{ 2,5,9,10 \right\} \amod{16}}\). \\
There is no common \(d\) in these three sets. So there does not any \(d\) for which all of \(2d-1, 5d-1, 13d-1\) are squares.
\end{soln}
Okay, we've learnt that \(2^n\) is a very good candidate when we need to deal with squares. But what about the higher powers? If we need to deal with \(x^d\), then it's often a good practice to deal with mod \(p\), where \(p\) is a prime with \(d \mid p-1\). \\
Now you may ask why. The answer is: whenever \(d \mid p-1\), \(x^d\) can take exactly \(1+\frac{p-1}{d}\) different remainders upon division by \(p\).\footnote{You can prove it, I'll leave it as an exercise for you.} \(1+\frac{p-1}{d}\) is a pretty small number when \(d\) gets larger. So it's not that hard to work with such \(p\). \\
Alright, let's look at some examples.
\begin{exercise}
Find all integer solutions: \(x^3+y^4=7\)
\end{exercise}
\begin{soln}
We have power \(3\) and power \(4\) here. So a prime \(p\) with \(3 \mid p-1\) and \(4 \mid p-1\) might do the job. Turns out, \(13\) is one such prime. If we take mod \(13\),
\[x^3 \equiv 0,1,5,8,12 \amod{13}, \quad y^4 \equiv 0,1,3,9 \amod{13}\]
We cannot make the sum \(7\). Thus, there does not exist any integer with \(x^3+y^4=7\).
\end{soln}
\begin{exercise}
Solve in integers: \(x^5 - y^2 = 4\)
\end{exercise}
\begin{soln}
We need a prime such that \(5 \mid p-1\). \(p=11\) is one such prime. Taking mod \(11\),
\[x^5 \equiv 0,1,-1 \amod{11} \implies y^2 = x^5 - 4 \equiv 6,7,8 \amod{11}\]
But \(\qr(11) = \left\{ 0,1,4,9,5,3 \right\} \). So no solution.
\end{soln}
\begin{exercise}
Solve in positive integers: \(3^x - 2^y = 7\)
\end{exercise}
\begin{soln}
If \(y=1\), we get a solution \((x,y) = (2,1)\). So assume \(y\geq 2\). Therefore \(4 \mid 2^y\). Taking mod \(4\), we get
\[3^x \equiv -1 \amod 4\]
If \(x\) is even, then it's never possible. So we must have \(x=2k+1\) for some integer \(k\). We need to improve our mod now. So let's take mod \(8\).
\[3^{x} = 3\cdot 3^{2k} = 3 \cdot 9^k \equiv 3 \amod{8} \implies 7+ 2^y \equiv 3 \amod{8}\]
which is not possible for \(y\geq 3\). We can check \(y=2\) by hand but it does not produce any solution. \\
Hence the only solution is \((x,y) = (2,1)\).
\end{soln}

\section{Practice Problems}
\begin{problem}
Prove that the expression
\[\frac{\gcd(m,n)}{n} \binom{n}{m}\]
is an integer for all pairs of integers \(n \geq m \geq 1\).
\end{problem}
\begin{problem}
Let \(a\) and \(b\) be coprime positive integers. Prove that there are exactly \(\frac{(a-1)(b-1)}{2}\) integers that cannot be written as \(ax+by\) for non-negative integer \(x,y\).
\end{problem}
\begin{problem}
Let \(a, b\), and \(c\) be positive integers, no two of which have a common divisor greater than 1. Show that \(2 a b c-a b-b c-c a\) is the largest integer that cannot be expresed in the form \(x b c+y c u+z a b\), where \(x, y\), and \(z\) are nonnegative integcrs.
\end{problem}
\begin{problem}
Let \(n>1\) be an odd integer. Prove that there exist positive integers \(x\) and \(y\) such that
\[
\frac{4}{n}=\frac{1}{x}+\frac{1}{y}
\]
if and only if \(n\) has a prime factor of the form \(4 k-1 .\)
\end{problem}
\begin{problem}
Find all positive integers \(m, n\), where \(n\) is odd, that satisfy
\[
\frac{1}{m}+\frac{4}{n} \quad \frac{1}{12}
\]
\end{problem}
\begin{problem}
Find all \(m,p,q\) with \(2^mp^2 + 1 = q^7\), where \(m\in\NN\) and \(p,q\) are primes.
\end{problem}
\begin{problem}
Find all integers \(n\) for which the equalion
\[
x^{3}+y^{3}+z^{3}-3 x y z=n
\]
is solvable in posilive inlegers.
\end{problem}
\begin{problem}
Find all triples \((x, y, p)\), where \(x\) and \(y\) are positive integers
and \(p\) is a prime, satisfying the equation
\[
x^{5}+x^{4}+1=p^{y}
\]
\end{problem}
\begin{problem}
Determine all triples \((x, y, z)\) of positive integers such that
\[
(x+y)^{2}+3 x+y+1=z^{2} \text { . }
\]
\end{problem}
\begin{problem}
Determine all pairs \((x, y)\) of integers that satisfy the equation
\[
(x+1)^{4}-(x-1)^{4}=y^{3} \text { . }
\]
\end{problem}
% \begin{problem}
% Let \(a\) and \(b\) be positive integers such that \(a b+1\) divides \(a^{2}+b^{2}\).
% Prove that \(\frac{a^{2}+b^{2}}{a b+1}\) is the square of an integer.
% \end{problem}
\begin{problem}
Find the maximal value of \(m^{2}+n^{2}\) if \(m\) and \(n\) are integers between 1 and 1981 satisfying \(\left(n^{2}-m n-m^{2}\right)^{2}=1\).
\end{problem}
\begin{problem}
Find all integers \(x, y, z\) satisfying
\[
x^{2}+y^{2}+z^{2}-2 x y z=0 .
\]
\end{problem}
\begin{problem}
Solve the following equation in integers \(x, y, z, u\) :
\[
x^{4}+y^{4}+z^{4}=9 u^{4} \text { . }
\]
\end{problem}
\begin{problem}
Solve the following equation in positive integers:
\[
x^{2}-y^{2}=2 x y z
\]
\end{problem}
\begin{problem}
Prove that there are no integer solutions (x, y) to \(y^2=x^3+23\).
\end{problem}
\begin{problem}
Determine all integral solutions to the equation
\[
a^{2}+b^{2}+c^{2}=a^{2} b^{2} .
\]
\end{problem}
\begin{problem}
Find all pairs \((p, q)\) of prime numbers such that
\[
p^{3}-q^{5}=(p+q)^{2} .
\]
\end{problem}
\begin{problem}
Prove that if \(n\) is a positive integer such that the
equation
\[
x^{3}-3 x y^{2}+y^{3}=n
\]
has a solulion in inlegers \(x, y\), then il has at leasl three such solu-
tions. Prove thal the equalion has no integer solution when \(n=2891 .\)
\end{problem}
\begin{problem}
Find all triples \((x, y, z)\) of nonnegative integers such that
\[
5^{x} 7^{y}+4=3^{z}
\]
\end{problem}
\begin{problem}
Solve in positive integers: \(n^7+7=k^2\)
\end{problem}
\begin{problem}
Determine all pairs $(x, y)$ of integers such that
\[1+2^{x}+2^{2x+1}= y^{2}.\]
\end{problem}
\begin{problem}
Find all pairs $(k,n)$ of positive integers such that \[ k!=(2^n-1)(2^n-2)(2^n-4)\cdots(2^n-2^{n-1}). \]
\end{problem}
\begin{problem}
Find all triples $(a, b, c)$ of positive integers such that $a^3 + b^3 + c^3 = (abc)^2$.
\end{problem}
\begin{problem}
Find all pairs $(m,n)$ of nonnegative integers for which \[m^2 + 2 \cdot 3^n = m\left(2^{n+1} - 1\right).\]
\end{problem}
\begin{problem}
Find all integer solutions of the equation \[\frac{x^{7}-1}{x-1}=y^{5}-1\]
\end{problem}
\end{document}


\documentclass[12pt]{cdblatex}
\usepackage{exercises}
\usepackage{fancyhdr}
\usepackage{footer}

\begin{document}

% --------------------------------------------------------------------------------------------
\section*{Exercise 6.08a A problem with {\tt evaluate} }

\CdbSetup{action=verbatim}% the following code will raise a run time error

\begin{cadabra}
   {\theta, \varphi}::Coordinate.
   {a,b,c,d,e,f,g,h#}::Indices(values={\theta, \varphi}, position=independent).

   \partial{#}::PartialDerivative.

   V_{a}::Depends(\theta,\varphi,\partial{#}).

   dVrule := { \partial_{\theta}{V_{\varphi}} = \sin(\theta),
               \partial_{\varphi}{V_{\theta}} = \cos(\theta)}.  # cdb(ex-0608.101,dVrule)
   dV := \partial_{b}{V_{a}} - \partial_{a}{V_{b}}.             # cdb(ex-0608.102,dV)

   evaluate (dV, dVrule)                                        # cdb(ex-0608.103,dV)
\end{cadabra}

\vskip 1cm
\bgroup
\lstset{numbers=none,backgroundcolor=\color{white}}
\begin{lstlisting}
   Traceback (most recent call last):
     File "/usr/local/bin/cadabra2", line 248, in <module>
       exec(cmp)
     File "ex-0608.py", line 27, in <module>
       evaluate (dV, dVrule)
   RuntimeError: Dependencies on derivatives are not yet handled in the SymPy bridge
\end{lstlisting}
\egroup

\clearpage

% --------------------------------------------------------------------------------------------
\section*{Exercise 6.08b A work around}

\CdbSetup{action=show}

\begin{cadabra}
   {\theta, \varphi}::Coordinate.
   {a,b,c,d,e,f,g,h#}::Indices(values={\theta, \varphi}, position=independent).

   \partial{#}::PartialDerivative.

   V_{a}::Depends(\theta,\varphi,\partial{#}).

   hide := \partial_{a}{V_{b}} -> dV_{a b}.

   dVrule := { dV_{\theta\varphi} = \sin(\theta),
               dV_{\varphi\theta} = \cos(\theta)}.              # cdb(ex-0608.201,dVrule)
   dV := \partial_{b}{V_{a}} - \partial_{a}{V_{b}}.             # cdb(ex-0608.202,dV)

   substitute (dV, hide)                                        # cdb(ex-0608.212,dV)
   evaluate (dV, dVrule)                                        # cdb(ex-0608.203,dV)
\end{cadabra}

The workaround here is to to hide the derivatives before calling {\tt evaluate}.

\begin{align*}
   \Cdb{ex-0608.212}\\
   dV_{a b} &= \Cdb{ex-0608.202}\\[10pt]
            &= \Cdb{ex-0608.203}\\[10pt]
\end{align*}

\end{document}


\chapter{Digital Image Processing}

We worked with images from
CRIC Database.
% TODO Add citation
The 400 images
have \(1376~\times~1020\)~pixels.

\begin{figure}
  \centering
  \includegraphics{R/img/be340ee72689dfe3f8dc9c24de6127f4.png}
  \caption{Example of image from CRIC}
  % TODO Add DOI
\end{figure}

We store the image as 3-D array \(A\),
containing
\(M\) rows,
\(N\) columns,
and
3 channels
(red, green, and blue).
For clarity
and
convenience,
since we used Python as programming language,
we use integer values for the discrete coordinates:
\(x = 0, 1, 2, \ldots, M - 1\),
\(y = 0, 1, 2, \ldots, N - 1\),
\(z = 0, 1, 2\).
\(x\), \(y\), and \(z\)
are referred as spatial variables.

The value \(a_{x, y, z}\)
is referred as intensity of \(A\)
at \(x\), \(y\), and \(z\)
and they are integers
in the interval
\([0, 255]\).

A pixel \(p\)
at coordinates \((x, y, z\)
has four horizontal and vertical neighbours,
denoted by \(N_4(p)\):
\begin{itemize}
\item \(x - 1, y, z\)
\item \(x + 1, y, z\)
\item \(x, y - 1, z\)
\item \(x, y + 1, z\)
\end{itemize}
It also has for diagonal neighbours,
denoted by \(N_D(p)\):
\begin{itemize}
\item \(x - 1, y - 1, z\)
\item \(x + 1, y - 1, z\)
\item \(x + 1, y + 1, z\)
\item \(x - 1, y + 1, z\)
\end{itemize}
The \(N_4(p) \cup N_D(p)\)
is called 8-neighbours of \(p\)
and denoted by \(N_8(p)\).

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "masters_dissertation"
%%% End: