\documentclass{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

% PYTHON
\usepackage{xcolor}
\usepackage{listings}
\usepackage{setspace}
\lstset{
basicstyle=\tiny,
  backgroundcolor=\color{white},   % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}; should come as last argument
  breakatwhitespace=false,         % sets if automatic breaks should only happen at whitespace
  breaklines=true,                 % sets automatic line breaking
  captionpos=b,                    % sets the caption-position to bottom
  commentstyle=\color{green},    % comment style
  deletekeywords={...},            % if you want to delete keywords from the given language
  escapeinside={\%*}{*)},          % if you want to add LaTeX within your code
  extendedchars=true,              % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8
  keepspaces=true,                 % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)
  keywordstyle=\color{blue},       % keyword style
  language=Octave,                 % the language of the code
  morekeywords={*,...},            % if you want to add more keywords to the set
  numbers=left,                    % where to put the line-numbers; possible values are (none, left, right)
  numbersep=5pt,                   % how far the line-numbers are from the code
  numberstyle=\tiny\color{gray}, % the style that is used for the line-numbers
  rulecolor=\color{black},         % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (green here))
  showspaces=false,                % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'
  showstringspaces=false,          % underline spaces within strings only
  showtabs=false,                  % show tabs within strings adding particular underscores
  stringstyle=\color{cyan},     % string literal style
  tabsize=2,	                   % sets default tabsize to 2 spaces
  title=\lstname                   % show the filename of files included with \lstinputlisting; also try caption instead of title
}

\usepackage{hyperref}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{amsmath}
\usepackage{comment}

\title{Spherical Pendulum with Python}
\author{Arun Leander}
\date{2019}

\begin{document}

\maketitle

\section{Introduction}

Simulate a spherical pendulum with python.

\section {Equations}

The spherical pendulums position can be described with two coordinates. $\theta$ is the angle in a plane which includes the Z-axis and $\phi$ is the angle on the plane perpendicular to the Z-axis. With $l$ as the length of the pendulum, the position in cartesian coordinates is

\begin{align}
x& = l \sin{\theta} \cos{\phi}                 \\
y& = l \sin{\theta} \sin{\phi}                 \\
z& = -l \cos{\theta}
\end{align}

The energies.

\begin{align}
E_{kin} &= T = \frac{1}{2} mv^2                 \\
E_{pot} &= V = mgh
\end{align}

The Lagrangian.
\begin{equation}
L = T - V
\end{equation}

To calculate the kinetic energy $T$, I have to replace $v^2$.

\begin{equation}
v^2 =   \dot{x}^2 + \dot{y}^2 + \dot{z}^2
\end{equation}

\subsection{Velocity}
\label{ssec:velocity}

$v^2$ can be written using $\theta$ and $\phi$ only. This requires the derivates in respect to the change of time $t$.

% x
\begin{align}
\begin{split}
\dot{x}^2& = (\frac{\partial x}{\partial t})^2			\\
& = (\frac{\partial }{\partial t} l \sin{\theta} \cos{\phi})^2	\\
& = l^2(\sin{\theta} (-\sin{\phi}) \dot{\phi} + \cos{\theta}\cos{\phi} \dot{\theta})^2				\\
& = l^2 [
	\sin^2\theta \sin^2 \phi \dot{\phi}^2 +
	\cos^2\theta \cos^2\phi \dot{\theta}^2 -
	2 \sin\theta\cos\theta\dot{\theta}\sin{\phi}\cos\phi\dot{\phi}
	]
%y
\end{split}
\\
\begin{split}
\dot{y}^2& = (\frac{\partial y}{\partial t})^2			\\
& = (\frac{\partial }{\partial t} l \sin{\theta} \sin{\phi})^2	\\
& = l^2(\sin{\theta} \cos\phi \dot{\phi} + \cos\theta\sin{\phi} \dot{\theta})^2				\\
& = l^2 [
	\sin^2\theta \cos^2 \phi \dot\phi^2 +
	\cos^2\theta \sin^2\phi \dot{\theta}^2 +
	2 \sin{\theta} \cos\theta \dot{\theta} \sin{\phi}\cos\phi \dot{\phi}
	]
\end{split}
\\
%z
\begin{split}
\dot{z}^2& = (\frac{\partial z}{\partial t})^2			\\
& = (\frac{\partial }{\partial t} - l\cos{\theta} )^2	\\
& = l^2 ( \sin^2\theta \dot{\theta}^2 )
\end{split}
\end{align}

\begin{align}
%x+y+z
\begin{split}
\frac{v^2}{l^2}& = (\sin^2\theta \sin^2 \phi \dot{\phi}^2 +
	\cos^2\theta \cos^2\phi \dot{\theta}^2 -
	2 \sin\theta\cos\theta\dot{\theta}\sin{\phi}\cos\phi\dot{\phi})\\
&\phantom{{}={}} + (\sin^2\theta \cos^2 \phi \dot\phi^2 +
	\cos^2\theta \sin^2\phi \dot{\theta}^2 +
	2 \sin{\theta} \cos\theta \dot{\theta} \sin{\phi}\cos\phi \dot{\phi} ) \\
&\phantom{{}={}} +	\sin^2\theta \dot{\theta}^2
\end{split}
\\
\begin{split}
\frac{v^2}{l^2}& = \sin^2\theta \sin^2 \phi \dot{\phi}^2 + \sin^2\theta \cos^2 \phi \dot\phi^2 \\
&\phantom{{}={}} + \cos^2\theta \cos^2\phi \dot{\theta}^2 +\cos^2\theta \sin^2\phi \dot{\theta}^2  \\
&\phantom{{}={}} +	\sin^2\theta \dot{\theta}^2
\end{split}
\\
\begin{split}
\frac{v^2}{l^2}& = \sin^2\theta \dot{\phi}^2 (\sin^2 \phi +\cos^2 \phi) + \cos^2\theta \dot{\theta}^2 (\cos^2\phi + \sin^2\phi) \\
&\phantom{{}={}} +	\sin^2\theta \dot{\theta}^2
\end{split}
\\
\begin{split}
\frac{v^2}{l^2}& = \sin^2\theta \dot{\phi}^2 + (\cos^2\theta + \sin^2\theta) \dot{\theta}^2
\end{split}
\\
\begin{split}
v^2& = l^2 (\sin^2\theta \dot{\phi}^2 + \dot{\theta}^2)
\end{split}
\end{align}

Now the energies and the lagrangian can be written using $\theta$ and $\phi$.

\begin{align}
T& = \frac{1}{2} m l^2 (\sin^2\theta \dot{\phi}^2 + \dot{\theta}^2)               \\
V_g& = - m g l \cos\theta								\\
V_f& = m l (f_z \cos\theta - (f_x \cos\phi + f_y sin(\phi)) \sin(\theta))								              \\
L& = \frac{1}{2} m  l^2 (\sin^2\theta \dot{\phi}^2 + \dot{\theta}^2) + m g l \cos\theta	    \\
L_f& = \frac{1}{2} m  l^2 (\sin^2\theta \dot{\phi}^2 + \dot{\theta}^2) - m l (f_z \cos\theta - (f_x \cos\phi + f_y sin(\phi)) \sin(\theta))
\end{align}

I inserted a generalized force $f$. For gravity use $f = \left(\begin{smallmatrix}0\\0\\-10\end{smallmatrix}\right)$.

\subsection {Relative Velocity}
\label{ssec:rvelocity}
The velocity relative to a moving system can be expressed with $v_w = v - w$ where $w$ is the velocity of the system.

\begin{align}
  {v_w}^2& = (\dot{x} - w_x)^2 + (\dot{y} - w_y)^2 + (\dot{z} - w_z)^2 \\
  \begin{split}
  {v_w}^2 &=
      (-l \sin{\theta} \sin{\phi} \dot{\phi} + l \cos{\theta}\cos{\phi} \dot{\theta} - w_x)^2
      \\ &\qquad + (l \sin{\theta} \cos\phi \dot{\phi} + l \cos\theta\sin{\phi} \dot{\theta} - w_y)^2
    + (l \sin\theta \dot{\theta} - w_z)^2
  \end{split}
\end{align}

\subsection{Second Lagrange Equation}

The second Lagrange equation for each coordinate $q_i$ is
\begin{equation}
\frac{\partial }{\partial t} \frac{\partial L}{\partial \dot{q}_i} - \frac{\partial L}{\partial q_i} = 0
\end{equation}

Insert the friction.
\begin{equation}
\frac{\partial }{\partial t} \frac{\partial L}{\partial \dot{q}_i}
- \frac{\partial L}{\partial q_i}
+ \frac{\partial C}{\partial \dot{q}_i}
= 0
\end{equation}

Solve it for $\ddot{\theta}$ and $\ddot{\phi}$.
\begin{equation}
\begin{split}
0& = \frac{\partial }{\partial t} \frac{\partial L}{\partial \dot{\theta}} - \frac{\partial L}{\partial \theta} + \frac{\partial C}{\partial \dot{q}_i}	\\
& = \frac{\partial }{\partial t} ( \frac{1}{2} m  l^2 2 \dot{\theta})
 - [
      \frac{1}{2} m  l^2 2 \sin\theta \cos\theta \dot{\phi}^2
      - ml ( -f_z \sin\theta + (f_x\cos\phi-f_y\sin(-\phi))(-\cos(\theta))  )
    ]
    + \frac{\partial C}{\partial \dot{q}_i}					\\
%
& =  m  l^2 \ddot{\theta}
 - m  l^2 \sin\theta \cos\theta \dot{\phi}^2 - m l f_z \sin\theta - ml(f_x\cos\phi - f_y\sin(-\phi))\cos\theta
 + \frac{\partial C}{\partial \dot{q}_i}											\\
%
& =  \ddot{\theta}
- \sin\theta \cos\theta \dot{\phi}^2 - \frac{\sin\theta}{l} f_z - \frac{\cos\theta}{l}(f_x\cos\phi - f_y\sin(-\phi))
+ \frac{1}{ml^2}\frac{\partial C}{\partial \dot{q}_i}        \\
%
\ddot{\theta}& = \frac{\sin(2\theta)}{2} \dot{\phi}^2
+ \frac{\sin\theta}{l} f_z + \frac{\cos\theta}{l}(f_x\cos\phi - f_y\sin(-\phi))
- \frac{1}{ml^2}\frac{\partial C}{\partial \dot{q}_i}
\end{split}
\end{equation}

\begin{equation}
\begin{split}
0& = \frac{\partial }{\partial t} \frac{\partial L}{\partial \dot{\phi}} - \frac{\partial L}{\partial \phi} + \frac{\partial C}{\partial \dot{\phi}}	\\
& = \frac{\partial }{\partial t} ( m  l^2 \sin^2\theta \dot{\phi})
- (-1) m l (-f_x \sin\phi + f_y \cos(\phi)) \sin(-\theta)
+ \frac{\partial C}{\partial \dot{\phi}}								\\
%
& = m  l^2(\sin^2\theta \ddot{\phi} + 2 \sin\theta \cos\theta \dot{\theta} \dot{\phi})
+ml (-f_x\sin\phi + f_y \cos(\phi)) \sin(-\theta)
+ \frac{\partial C}{\partial \dot{\phi}}	\\
%
& = \ddot{\phi} + 2\frac{\cos\theta}{\sin\theta} \dot{\theta} \dot{\phi}
+ \frac{\sin(-\theta)}{l}(-f_x\sin\phi + f_y \cos\phi)
+ \frac{1}{ml^2\sin^2\theta}\frac{\partial C}{\partial \dot{\phi}}		\\
\ddot{\phi}& = - 2\cot{\theta} \dot{\theta} \dot{\phi}
+ \frac{\sin(\theta)}{l}(-f_x\sin\phi + f_y \cos\phi)
- \frac{1}{ml^2\sin^2\theta}\frac{\partial C}{\partial \dot{\phi}}
\end{split}
\end{equation}

%% MAIN EQUATIONS DONE

\section{Friction and Drag}

The non generalized \emph{angular} friction power $C_A$ and \emph{drag} friction power $C_D$ are as follows.
For the drag, I will pretend the the surface $A$ and the density $p$ (through volume) is proportional to $l$. The extra components are included in the drag coefficient $c_d$. While the velocity for $C_A$ can be described only through $\theta$ and $\phi$, $C_D$ depends on the velocity of the system and has to include the wind speed.

\begin{align}
  \begin{split}
  C_A &= \frac{1}{2} c_a v^2							                              \\
  C_A &= \frac{1}{2} c_a l^2 (\sin^2\theta \dot{\phi}^2 + \dot{\theta}^2)
  \end{split}
  \\
  \begin{split}
  C_D &= \frac{1}{2} c_{d0} p A (v - w)^3    							            \\
  C_D &= \frac{1}{2} c_{d} \frac{m}{l} l (|v - w|)^3                               \\
  C_D &= \frac{1}{2} c_{d} m l^3 (\frac{{v_w}^2}{l^2})^{\frac{3}{2}}                              \\
  \end{split}
\end{align}

\subsection{Derivates of the frictions for the main equation}
\subsubsection{Angular Friction}
\begin{align}
  \frac{\partial C_A}{\partial \dot{\theta}} &= c_a l^2 \dot{\theta} 	\\
  \frac{\partial C_A}{\partial \dot{\phi}}   &= c_a l^2 \sin^2\theta\dot{\phi} \\
\end{align}

\subsubsection{Drag}

${v_w}^2$ has to be inserted, see \hyperref[ssec:rvelocity]{this previous section (click)}.
\begin{align}
  \begin{split}
  \frac{\partial C_D}{\partial \dot{\theta}}   &=\frac{\partial }{\partial \dot{\theta}} \frac{1}{2} c_{d} m ({v_w}^2)^{\frac{3}{2}}      \\
  & = \frac{3}{4} c_{d} m
  (\frac{{v_w}^2}{l^2})^\frac{1}{2}
  (2 l \cos\theta \cos\phi (l \dot{\theta} \cos\theta \cos\phi - l \dot{\phi} \sin\theta \sin\phi - x_w)
  \\&\phantom{{}={}}+ 2 l \cos\theta  \sin\phi (l \dot{\theta} \cos\theta \sin\phi +l \dot{\phi} \sin\theta \cos\phi - y_w)
  + 2 l \sin\theta (l \dot{\theta} \sin\theta - z_w))
  \end{split}
  \\
  \begin{split}
  \frac{\partial C_D}{\partial \dot{\phi}}
  & = \frac{1}{2} c_{d} m \frac{\partial (\frac{{v_w}^2}{l^2})^{\frac{3}{2}} }{\partial \dot{\phi}} \\
  & = \frac{3}{4} c_{d} m
  (\frac{{v_w}^2}{l^2})^\frac{1}{2}
  \\&\phantom{{}={}}(2 l \sin\theta (l \dot{\phi} \sin\theta (\sin^2\phi + \cos^2\phi + x_w \sin\phi - y_w \cos\phi)))
\end{split}
\end{align}

\section{Multiple Segments}

Here is an attempt to generalize the equation to compute the motion of multiple pendulums concatenated.

\begin{align}
  \begin{split}
    x& = l \sin{\theta} \cos{\phi}\\
    y& = l \sin{\theta} \sin{\phi}\\
    z& = -l \cos{\theta}\\
  \end{split}
  \begin{split}
    \dot{x}& = -l\sin{\theta} \sin{\phi} \dot{\phi} + l\cos{\theta}\cos{\phi} \dot{\theta}\\
    \dot{y}& = l\sin{\theta} \cos\phi \dot{\phi} + l\cos\theta\sin{\phi} \dot{\theta}\\
    \dot{z}& = l\sin\theta \dot{\theta}
  \end{split}
\end{align}

Some partial derivates.

\begin{align}
  \begin{split}
    \frac{\partial x}{\partial \theta}& = x_{n\theta} = l \cos{\theta} \cos{\phi} \\
    \frac{\partial y}{\partial \theta}& = y_{n\theta} = l \cos{\theta} \sin{\phi} \\
    \frac{\partial z}{\partial \theta}& = z_{n\theta} = l \sin{\theta}
  \end{split}
  \\
  \begin{split}
    \frac{\partial \dot{x}}{\partial t} & = \dot{x}_{t}
      = - l\cos{\theta} \sin{\phi} \dot{\theta} \dot{\phi}
      - l\sin{\theta} \cos{\phi} \dot{\phi}^2
      - l\sin{\theta} \sin{\phi} \ddot{\phi}
      \\&\phantom{{}=xn={}}- l\cos{\theta}\sin{\phi} \dot{\theta} \dot{\phi}
      - l\sin{\theta}\cos{\phi} \dot{\theta}^2
      + l\cos{\theta}\cos{\phi} \ddot{\theta}
    \\
    \frac{\partial \dot{y}}{\partial t} & = \dot{y}_{t}
      = l\cos{\theta} \cos\phi \dot{\theta} \dot{\phi}
      - l\sin{\theta} \sin\phi \dot{\phi}^2
      + l\sin{\theta} \cos\phi \ddot{\phi}
      \\&\phantom{{}=xn={}}
      + l\cos\theta\cos{\phi} \dot{\theta} \dot{\phi}
      - l\sin\theta\sin{\phi} \dot{\theta}^2
      - l\cos\theta\sin{\phi} \ddot{\theta}
    \\
    \frac{\partial \dot{z}}{\partial t} & = \dot{z}_{t}
      = l\cos\theta \dot{\theta}^2 + l\sin\theta \ddot{\theta}
  \end{split}
  \\
  \begin{split}
    \frac{\partial \dot{x}}{\partial \dot{\theta}}& = \dot{x}_{\dot{\theta}}
      = l\cos{\theta}\cos{\phi} \\
    \frac{\partial \dot{y}}{\partial \dot{\theta}}& = \dot{y}_{\dot{\theta}}
      = l\cos\theta\sin{\phi}\\
    \frac{\partial \dot{z}}{\partial \dot{\theta}}& = \dot{z}_{\dot{\theta}}
      = l\sin\theta
  \end{split}
  \\
  \begin{split}
    \frac{\partial}{\partial t}\frac{\partial \dot{x}}{\partial \dot{\theta}}& = \dot{x}_{\dot{\theta}t}
      = -l\sin{\theta}\cos{\phi} \dot{\theta} - l\cos{\theta}\sin{\phi}  \dot{\phi}\\
      \frac{\partial}{\partial t}\frac{\partial \dot{y}}{\partial \dot{\theta}}& = \dot{y}_{\dot{\theta}t}
      = -l\sin{\theta}\sin{\phi} \dot{\theta} + l\cos{\theta}\cos{\phi}  \dot{\phi}\\
      \frac{\partial}{\partial t}\frac{\partial \dot{z}}{\partial \dot{\theta}}& = \dot{z}_{\dot{\theta}t}
      = l\cos\theta \dot{\theta}
  \end{split}
\end{align}

\begin{comment}

\begin{split}
  \frac{\partial x_n}{\partial \phi_n}& = x_{n\phi}  = l_n \sin{\theta_n} \sin{\phi_n} \phi_n\\
  \frac{\partial y_n}{\partial \phi_n}& = y_{n\phi}  = l_n \sin{\theta_n} \cos{\phi_n} \phi_n\\
  \frac{\partial z_n}{\partial \phi_n}& = z_{n\phi}  = 0
\end{split}
\begin{split}
  \frac{\partial \dot{x}_n}{\partial \phi_n}& = \dot{x}_{n\phi}
    = -l_n\sin{\theta_n} \cos{\phi_n} \dot{\phi}_n - l_n\cos{\theta_n}\sin{\phi_n} \dot{\theta}_n\\
  \frac{\partial \dot{y}_n}{\partial \phi_n}& = \dot{y}_{n\phi}
    = -l_n\sin{\theta_n} \sin\phi_n \dot{\phi}_n + l_n\cos\theta_n\cos{\phi_n} \dot{\theta}_n\\
  \frac{\partial \dot{z}_n}{\partial \phi_n}& = \dot{z}_{n\phi}
    = 0
\end{split}
\begin{split}
  \frac{\partial \dot{x}_n}{\partial \dot{\phi}}& = \dot{x}_{n\dot{\phi_n}}
    = -l_n\sin{\theta_n} \sin{\phi_n} \\
  \frac{\partial \dot{y}_n}{\partial \dot{\phi}}& = \dot{y}_{n\dot{\phi_n}}
    = l_n\sin{\theta_n} \cos\phi_n\\
  \frac{\partial \dot{z}_n}{\partial \dot{\phi}}& = \dot{z}_{n\dot{\phi_n}}
    = 0
\end{split}
\end{comment}

The updated lagrangian.

\begin{align}
  \begin{split}
    T &= \frac{1}{2} m v^2                 \\
    &= \frac{1}{2} ((\sqrt{m}\dot{x})^2 + (\sqrt{m}\dot{y})^2 + (\sqrt{m}\dot{z})^2)                 \\
    &= \frac{1}{2} ((\sqrt{m_a}\dot{x}_a + \sqrt{m_b}\dot{x}_b \dots)^2 + (\sqrt{m_a}\dot{y}_a + \sqrt{m_b}\dot{y}_b \dots)^2 + (\sqrt{m_a}\dot{z}_a + \sqrt{m_b}\dot{z}_b \dots)^2)
  \end{split} \\
  \begin{split}
    V &= -(((m_a + m_b) x_a + m_b x_b) x_f) + ((m_a + m_b) y_a + m_b y_b) y_f) + ((m_a + m_b) z_a + m_b x_b) z_f))
  \end{split}
\end{align}

\begin{align}
  \begin{split}
    L &= \frac{1}{2} (m_a\dot{x}_a^2 + 2 \sqrt{m_a + m_b} \dot{x}_a \dot{x}_b + m_b\dot{x}_b^2
    +m_a\dot{y}_a^2 + 2 \sqrt{m_a + m_b} \dot{y}_a \dot{y}_b + m_b\dot{y}_b^2
    \\&\phantom{{}=\frac{1}{2} (}+m_a\dot{z}_a^2 + 2 \sqrt{m_a + m_b} \dot{z}_a \dot{z}_b + m_b\dot{z}_b^2)
    \\&\phantom{{}={}}+ ((m_a + m_b) x_a + m_b x_b) x_f + ((m_a + m_b) y_a + m_b y_b) y_f + ((m_a + m_b) z_a + m_b x_b) z_f
  \end{split}
\end{align}

For the topmost $\theta$ and $\phi$, this will result in.

\begin{align}
\frac{\partial }{\partial t} \frac{\partial L}{\partial \dot{q}_i} - \frac{\partial L}{\partial q_i} = 0
\end{align}

\section {Numpy ODE}

By entering $\theta$, $\dot{\theta}$, $\phi$ and $\dot{\phi}$ into an ODE, they motion can be solved numerically.

\begin{lstlisting}[language=Python]

def pend(y, t, mass, gravity, L, c):
    theta, d_theta, phi, d_phi = y
    dydt = [d_theta,
            np.square(d_phi) * np.sin(theta) * np.cos(theta) - gravity/L * np.sin(theta) - c/mass * d_phi,
            d_phi,
            (-2) * d_theta * d_phi / np.tan(theta) - c/mass * d_phi
            ]
    return dydt

mass = 10
gravity = 10
L = 0.2
friction = 8

# intial values
y0 = [np.pi/2.5, 0.0, 0.0, 4]

# time values
t = np.linspace(0, 20, 400)

# create the solver
sol = scipy.integrate.odeint(pend, y0, t, args=(mass, gravity, L, friction))
\end{lstlisting}

\newpage
\tableofcontents
\end{document}


\chapter{Chapter 7. Soundness and Completeness}

\section*{7.6 Self-Study Questions}

	\begin{enumerate}

		\item[7.6.1] (a) contradicts completeness, not soundness; (b) is in direct contradiction to soundness; (c) this just means that the set is not consistent, in classical propositional logic, we have, for example, $\{p,\neg p\}\vdash p$ and $\{p,\neg p\}\vdash \neg p$, (d) this would quickly lead to a contradiction if the calculus were sound: if you can derive any formula whatsoever from m$\Gamma$, you get $\Gamma\vdash p\land \neg p$; by soundness  $\Gamma\vDash p\land \neg p$; by $\Gamma$ being satisfiable, say via $v$, we get that $\llbracket p\land\neg p\rrbracket_v=1$; contradiction.

		\item[7.6.2] (a) directly contradicts completeness; (b) doesn't contradict completeness but soundness (note that a calculus can be complete but not sound: take the trivial calculus in which everything can be derived from everything, this calculus is certainly complete but not sound); (c) every logical truth follows from every set (you can see this from the definition of logical truth $\emptyset\vDash\phi$ and monotonicity (5.2.6.(iii)); (d) this is perfectly fine since there are sets from which both a formula and a negation do follow (see above).

	\end{enumerate}

\section*{7.7 Exercises}

	\begin{enumerate}

		\item[7.7.3] \begin{enumerate}[(a)]

			\item \emph{Claim}. Every proof theoretically inconsistent set is unsatisfiable.

			\begin{proof}
			Suppose that $\Gamma$ is proof-theoretically inconsistent, i.e. there exists a $\phi$ such that $\Gamma\vdash\phi$ and $\Gamma\vdash\neg\phi$. By the soundness theorem, we can infer that $\Gamma\vDash\phi$ and $\Gamma\vDash\neg\phi$. We now show that $\Gamma$ is unsatisfiable using indirect proof. For suppose that $\Gamma$ is satisfiable, i.e. there exists a $v$ such that all the formulas in $\Gamma$ are true under $v$. Since  $\Gamma\vDash\phi$, it follows that $\llbracket\phi\rrbracket_v=1$. And since $\Gamma\vDash\neg\phi$, it follows that $\llbracket\neg\phi\rrbracket_v=1$. But since $\llbracket\neg\phi\rrbracket_v=1-\llbracket\phi\rrbracket_v=1$, we can infer that $\llbracket\phi\rrbracket_v=0$. Contradiction. Hence $\Gamma$ must be unsatisfiable, which is what we needed to show.
			\end{proof}

			\item \emph{Claim} Every proof theoretically consistent set is satisfiable.

			\begin{proof}
			We're going to prove the contrapositive, i.e. that if $\Gamma$ is unsatisfiable, then $\Gamma$ is proof-theoretically inconsistent. Suppose that $\Gamma$ is unsatisfiable. We're going to show that it follows that $\Gamma\vDash p$ for some arbitrary $p\in\mathcal{P}$. To see this, note that if $\Gamma$ is unsatisfiable, since $\Gamma\subseteq \Gamma\cup\{\neg p\}$, it follows by Proposition 6.2.5.(c) that $\Gamma\cup\{\neg p\}$ is also unsatisfiable. But this, by Theorem 6.2.6, is equivalent to $\Gamma\vDash p$. Similarly, we can see that $\Gamma\vDash\neg p$. For since $\Gamma$ is unsatisfiable and $\Gamma\subseteq\Gamma\cup\{\neg\neg p\}$, we have that $\Gamma\cup\{\neg\neg p\}$ is unsatisfiable, which gives us $\Gamma\vDash\neg p$. So, we have $\Gamma\vDash p$ and $\Gamma\vDash\neg p$. By the completeness theorem, this means that $\Gamma\vdash p$ and $\Gamma\vdash\neg p$, which means that $\Gamma$ is proof-theoretically inconsistent and is what we wanted to show.
			\end{proof}

		\end{enumerate}

	\end{enumerate}

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


\section{The Real Numbers}

\subsection{Cauchy Sequences}
\declareexercise{5.1.1}
\begin{proof}
Since $(a_n)^\infty_{n=1}$ is a Cauchy sequence, it is 1-steady for some $N$. That implies,
\[
\forall n \geq N(|a_n-a_N| \leq 1)
\]
And we know that $a_1,a_2,\dots,a_N$ is bounded by some number $M$, which means $|a_N| \leq M$. Expand $|a_n-a_N| \leq 1$ to obtain
\[
a_N-1 \leq a_n \leq a_N+1
\]
If $a_N \geq 0$, then $a_n \geq a_N-1 \geq -a_N-1$, then
\[
-(a_N+1) \leq a_n \leq a_N+1 \equiv |a_n| \leq |a_N+1|
\]
So
\[
|a_n| \leq |a_N+1| \leq |a_N| + |1| \leq M+1
\]
If $a_N < 0$, then $a_n \leq a_N +1 < -a_N+1$, then
\[
a_N - 1 \leq a_n \leq -(a_N-1) \equiv |a_n| \leq |a_N-1|
\]
We can also get
\[
|a_n| \leq |a_N-1| \leq |a_N| + |1| \leq M+1
\]
Therefore, we know that $(a_n)^\infty_1$ is bounded by $M+1$.
\end{proof}

\subsection{Equivalent Cauchy Sequences}
\declareexercise{5.2.1}
\begin{proof}
Although we need to prove that $a_n$ being a Cauchy sequence is logically equivalent to $b_n$ being a Cauchy sequence, showing that one
implies another is enough due to the structure of these statements.

Now we show that $a_n$ being a Cauchy sequence implies that $b_n$ being a Cauchy sequence. We need to show that for any $\varepsilon >0$,
there exists a $N$ such that for all $m,n\geq N$, $|a_m-a_n| \leq \varepsilon$.

First we know that for any $\varepsilon >0$, there exists a $N$ such that $\forall m,j\geq N(|a_m-b_j|\leq \varepsilon)$. By substituting $m$
with $n$ we have both $|a_m-b_j|\leq \varepsilon$ and $|a_n-b_j|\leq \varepsilon$.
Thus,
\[
|a_m-a_n| = |(a_m-b_j) - (a_n-b_j)| \leq |a_m-b_j| + |a_n-b_j| \leq 2\varepsilon
\]

Then we find the $N'$ such that $\forall m,n\geq N(|a_m-b_n| \leq \varepsilon/2)$, and then for $i,j \geq N$, $|a_i-a_j| \leq \varepsilon$.
\end{proof}

\declareexercise{5.2.2}
\begin{proof}
We need only to show that $a_n$ being bounded implies that $b_n$ being so because of the structure of these statements.

Consider \exerciseref{5.1.1}. Since that $(a_n)^\infty_{n=1},(b_n)^\infty_{n=1}$ are eventually $\varepsilon$-close, the sequence
$(a_n-b_n)^\infty_{n=1}$ is eventually $\varepsilon$-steady. Thus, according to the exercise mentioned, it is bounded by some number $M$. And
we say that $a_n$ is bounded by some number $N$.

So $|a_n-b_n| \leq M$. Again, similar to the proof of $|a_n| \leq |a_N|+1$ in \exerciseref{5.1.1}, we can obtain that
$|b_n| \leq |a_n| + M \leq N+M$. Therefore, $b_n$ is also bounded.
\end{proof}

\subsection{The Construction of the Real Numbers}
\declareexercise{5.3.1}
\begin{proof}
Reflectivity: It is immediately derived from $|a_n-a_n| = 0$.

Symmetry: It is immediately derived from $|a_n-b_n| = |b_n-a_n|$.

Transitivity: For any $\varepsilon >0$, we can find $M,N$ such that $\forall n\geq M(|a_n-b_n| \leq \varepsilon)$ and
$\forall n\geq N(|b_n-c_n| \leq \varepsilon)$. Let $B = \max{(M,N)}$. Then for $n\geq B$,
\[
|a_n-c_n| \leq |a_n-b_n| + |b_n-c_n| \leq 2\varepsilon
\]
This can also be deduced by (c) in Proposition 4.3.7
So $a_n$ and $c_n$ are also equal.
\end{proof}

\declareexercise{5.3.2}
\begin{proof}
(1)
We need to show that $(a_nb_n)^\infty_{n=1}$ is a Cauchy sequence. For any $\varepsilon>0$, we can find $M,N$ such that
$\forall i,j\geq M(|a_i-a_j| \leq \varepsilon)$ and $\forall i,j\geq N(|b_i-b_j| \leq \varepsilon)$. Let $B = \max{(M,N)}$. Then for
$i,j \geq B$, (See (h) in Proposition 4.3.7)
\[
|a_ib_i - a_jb_j| \leq \varepsilon(|a_i| + |a_j|) + \varepsilon^2
\]
Note that $(a_n)^\infty_{n=1}$ is a Cauchy sequence, so it is bounded by some number $M$. Thus,
$|a_i| + |a_j| \leq 2M$.

For any $\varepsilon' >0$, we need to find a $\varepsilon >0$ such that
$\varepsilon(|a_i| + |a_j|) + \varepsilon^2 \leq \varepsilon'$. First, if $\varepsilon' \geq 1$, then by
setting $\varepsilon < 1$ we can obtain $\varepsilon^2 < 1 \leq \varepsilon'$; if $\varepsilon' < 1$, then we let $\varepsilon < \varepsilon'$, and multiply it with $\varepsilon < 1$, we then have
$\varepsilon^2 < \varepsilon'$. After these steps, we can ensure that $\varepsilon' - \varepsilon^2>0$.

Consider the number $t = \dfrac{\varepsilon' - \varepsilon^2}{2M}>0$. If $\varepsilon$ already satisfies
$\varepsilon <t$, then it is the number we want. If it doesn't, then we can shrink it. That is, let
$\varepsilon'' < t \leq \varepsilon$. $\varepsilon'' < \varepsilon$ gives
$(\varepsilon'')^2 < (\varepsilon)^2$, then $-(\varepsilon'')^2 > -(\varepsilon)^2$, and finally
\[
t'' = \frac{\varepsilon' - (\varepsilon'')^2}{2M}>\frac{\varepsilon' - \varepsilon^2}{2M}
\]
So $\varepsilon'' < t < t''$. We can set $\varepsilon$ to this $\varepsilon''$. Then
\begin{align*}
\varepsilon < \frac{\varepsilon' - \varepsilon^2}{2M} &\Longrightarrow \\
\varepsilon \times 2M < \varepsilon' - \varepsilon^2 &\Longrightarrow \\
\varepsilon \times 2M + \varepsilon^2 < \varepsilon'
\end{align*}

So no matter what $\varepsilon'>0$ is, we can always find $\varepsilon >0$ such that
\[
\varepsilon(|a_i| + |a_j|) + \varepsilon^2 \leq \varepsilon'
\].
And for this $\varepsilon'$, there exists $N\geq 1$ such that
\[
\forall i,j\geq N(|a_ib_i - a_jb_j| \leq \varepsilon(|a_i| + |a_j|) + \varepsilon^2 \leq \varepsilon')
\]
Then, $(a_nb_n)^\infty_{n=1}$ is a Cauchy sequence. So is $xy$ a real number.

(2)
For any $\varepsilon >0$, we can find $N$ such that $\forall n\geq N(|a_n-a'_n|\leq \varepsilon)$. Thus, for
such $n$,
\[
|a_nb_n - a'_nb_n| = |b_n||a_n - a'_n| \leq \varepsilon|b_n|
\]
Note that $(b_n)^\infty_{n=1}$ is bounded by some number $M$. So $|a_nb_n - a'_nb_n| \leq \varepsilon M$.
Therefore, we find the $N'$ such that $\forall n\geq N'(|a_n-a'_n|\leq \varepsilon/M)$. Then for such $n$,
$|a_nb_n - a'_nb_n| \leq \varepsilon$. Thus $\sequence{a_nb_n}{1} = \sequence{a'_nb_n}{1}$.
\end{proof}

\declareexercise{5.3.3}
\begin{proof}
On one hand, if $a=b$, then obviously $a,a,\cdots = b,b,\cdots$.

On the other hand, if $a,a,\cdots \neq b,b,\cdots$, we try to show that $a=b$. Presume the negation, that is,
$a \neq b$. Then, $|a_n-b_n| = |a-b| \geq |a-b|$. For any $0<\varepsilon< |a-b|$, the two Cauchy sequences cannot be
$\varepsilon-$close, which is impossible.
\end{proof}

\paragraph{Lemma 5.3.14}
Here it is asked that why can we deduce $|b_n| \geq \varepsilon/2$ from $|b_{n0} - b_n| \leq \varepsilon/2$ and
$|b_{n0} > \varepsilon$. The book says that the triangle inequality can be used. In fact, we use the fact
\[
||b_{n0}| - |b_n|| \leq |b_{n0} - b_n|
\]
instead of $|b_{n0} - b_n| \leq |b_{n0}| + |b_n|$. Since that $|b_{n0}| - |b_n| \leq ||b_{n0}| - |b_n||$,
we have
\[
|b_{n0}| - |b_n| \leq \varepsilon/2 \equiv |b_{n0}| \leq \varepsilon/2 + |b_n|
\]
But $|b_{n0}| > \varepsilon$, so $\varepsilon/2 + |b_n| > \varepsilon \equiv |b_n| > \varepsilon/2$.

It is not mentioned in (b) of Proposition 4.3.3, but it can be easily proven if we divide conditions, though
the process is indeed very tedious.

\declareexercise{5.3.4}
\begin{proof}
Since that the two Cauchy sequences are equivalent, they are eventually $\varepsilon-$steadiness for any $\varepsilon>0$.
Then, according to \exerciseref{5.2.2}, $(a_n)^\infty_{n=1}$ being bounded implies that $(b_n)^\infty_{n=1}$ being so.
\end{proof}

\declareexercise{5.3.5}
\begin{proof}
We show that $(\frac{1}{n})^\infty_{n=1} = (0)^\infty_{n=1}$.

For each $\varepsilon>0$, we want to find $N \in \mathbb{N}$ such that
$n\geq N \longrightarrow |\frac{1}{n}-0|\leq \varepsilon$. Note that
\[
|\frac{1}{n}-0|\leq \varepsilon \equiv \frac{1}{n} \leq \varepsilon \equiv \frac{1}{\varepsilon} \leq n
\],
which means that we need to find $N \geq \frac{1}{\varepsilon}$. This is always possible as stated by Proposition 4.4.1.

Then the two sequences are equivalent, which proves our proposition.
\end{proof}

\subsection{Ordering the Reals}
\declareexercise{5.4.1}
\begin{proof}
We try to show that if a real number $a$ is non-zero, then it must be either positive or negative (not both). We already know from
Lemma 5.3.14 that $a$ can equal to $\LIM{a_n}$, where $\sequence{a_n}{1}$ is bounded away from zero. Now we show that every Cauchy
sequence that is bounded away from zero can always equal to either a positively bounded away from zero or a negatively bounded away
from zero Cauchy sequence.

Let $\sequence{a_n}{1}$ be a Cauchy sequence that is bounded away from zero. Then for every $n$, $|a_n| \geq c$. Choose $\varepsilon$
so that $0<\varepsilon<c$. We can find $N$ such that $m,n \geq N \longrightarrow |a_n-a_m| \leq \varepsilon$. We know that
$|a_n| \geq c \longrightarrow a_n\leq -c \vee a_n \geq c$. We will only show that $\sequence{a_n}{1}$ equals to some sequences
positively bounded away from zero on the latter condition. It is easy to derive that $\sequence{a_n}{1}$ equals to some sequences
negatively bounded away from zero on the former condition.

We have
\[
|a_n-a_m| \leq \varepsilon \longrightarrow a_n -\varepsilon <a_m
\]
and hence $a_m >c-\varepsilon>0$ since $\varepsilon<c \wedge a_n \geq c$.

Let $\sequence{b_n}{1}$ be such a sequence that
\begin{itemize}
\item $m \geq N \longrightarrow b_n = a_n$,
\item $0<n<N \longrightarrow b_n$ be any value bigger than $c-\varepsilon$.
\end{itemize}

Thus, $\sequence{b_n}{1}$ is positively bounded away from zero, and is also equivalent to $\sequence{a_n}{1}$ since that
$m \geq N \longrightarrow b_n = a_n$.

Now we show that it cannot equal to both. Denote it by $a_n$. Presume the negation, that is, it is equal to both a sequence
$x_n \geq c$ and $y_n \leq -c$ for $c>0$. Choose $\varepsilon$ so that $0<\varepsilon<c$. It equals to $x_n$ implies that we can always
find $N_1$ such that
\[
n \geq N \longrightarrow |a_n-x_n| \leq \varepsilon \longrightarrow a_n > x_n -\varepsilon >c-\varepsilon >0
\]
Similarly, we can find $N_2$ such that
\[
a_n < -(c-\varepsilon)<0
\]

This is impossible, as $a_n$ cannot be eventually $2(c-\varepsilon)$-close.

From what we have shown, we can easily derive that a real number is either positive, negative, or zero.

Now we show that $x$ is positive iff $-x$ is negative. We know $x = \LIM{a_n}$, where $a_n > c>0$. Then
$-x = \LIM{-a_n}$. Since that $-a_n < -c <0$, $\sequence{-a_n}{1}$ is negatively bounded away from zero. So $-x$ is
negative, as desired.

Finally we show that if $x=\LIM{a_n},y=\LIM{b_n}$ are both positive, then $x+y=\LIM{a_n+b_n},xy = \LIM{a_nb_n}$ is also
positive. It is immediately leaded to since
\[
a_n >c>0 \wedge b_n >d>0 \Longrightarrow a_nb_n > cd>0 \wedge a_n+b_n > c+d>0
\]
\end{proof}

\declareexercise{5.4.2}
\begin{proof}
(a) It is immediately derived since $x-y$ satisfies Proposition 5.4.4.

(b)
Denote $x,y$ as $\LIM{a_n},\LIM{b_n}$, respectively. Note that by definition
$y-x = \LIM{b_n-a_n} = \LIM{-(a_n-b_n)} = -(x-y)$. So from Proposition 5.4.4 we can see that
\[
x>y \equiv x-y>0 \equiv y-x<0 \equiv y<x
\]
One might notice that we use $x-y>0$ to represent $x-y$ being positive here. It is quite easy to prove. Just notice that
$x$ being positive is logically equivalent to $x-0$ being so, and thus is to $x>0$. We can further prove that $x<0$ is
equivalent to $x$ being negative.

(c)
We know by Proposition 5.4.4 that
\[
x-y >0 \wedge y-z>0 \longrightarrow (x-y)+(y-z) = x-z >0
\]
So $x>y \wedge y>z \longrightarrow x>z$.

(d)
It is immediately deduced as $(x+z)-(y+z) = x-y$.

(e)
$x>y \equiv x-y>0$. As stated by Proposition 5.4.4, $z(x-y) >0$. So $xz-yz>0 \longrightarrow xz>yz$.

\end{proof}

\begin{prop} \label{prop.5.4.basicproperties}
Since that reals numbers possess the same basic algebraic properties as the properties possessed by rational numbers, we
can ascertain that the corollaries of them are also right. For example,
\[
a<b\wedge c<d \longrightarrow a+c<b+d
\]
\[
a,b,c,d>0\wedge a<b\wedge c<d \longrightarrow ac<bd
\]
\end{prop}

\declareexercise{5.4.3}
\begin{proof}
Existence:
For a $\varepsilon>0$, there exists a $N$ such that $n\geq N \longrightarrow |a_n-a_N| \leq \varepsilon$. Choose an
arbitrary $c>0$, and let a rational number $y$ be $\LIM{a_N-\varepsilon-c}$. On this occasion, the real number
$y-x<0$, which means that $y<x$.

Since $y$ is a rational number, there exists a natural number $M$ such that $M \leq y$. So $M<x$ the number $M+1$ may be
bigger than $x$, and if it is, $M$ is the number we want.

If $M+1 \leq x$, then we check if $(M+1)+1$ is bigger than $x$, and we repeat the step until we find the first natural
number $M'$ such that $M'+1>x$. Hence $M'$ is the number we want.

Uniqueness:
We have already shown that $\exists M(M\leq x<M+1)$.
Suppose that there exists another natural number $K$ such that $K\leq x<K+1$. We show that $K=M$. Suppose the negation.
Then $K$ either $<M$ or $>M$. Under the former condition, $K+1\leq M\leq x$, which is impossible. Under the latter
condition, $x<M+1\leq K$, which is also impossible.
\end{proof}

\declareexercise{5.4.4}
\begin{proof}
\[
x>0\rightarrow \frac{1}{x}>0\rightarrow \exists N(N>\frac{1}{x}>0)
\]
So, according to Proposition 5.4.8,
\[
0<\frac{1}{N}<x
\]
, as desired.
\end{proof}

\declareexercise{5.4.5}
\begin{proof}
Let $x=\LIM{x_n},y=\LIM{y_n}$. Since $x>y$, the sequence $\sequence{x_n-y_n}{1}$ equals to a sequence that is positively
bounded
away from zero. We may just let $\sequence{x_n-y_n}{1}$ be such a sequence. (This is always possible. Given a sequence
$\sequence{z_n}{1}$, which is positively bounded away from zero, and satisfies $\LIM{z_n}=x-y$, we can define $x_n$ as
$y_n+z_n$) This way, for some $c>0$, $x_n-y_n>c\equiv x_n>c+y_n$.

Moreover, we can find $N$ such that for $0<\varepsilon<\frac{c}{2}$ and $n\geq N$,
$|x_n-x_N|\leq \varepsilon\wedge|y_n-y_N|\leq \varepsilon$, which means
\[
x_n\geq x_N -\varepsilon\wedge y_n \leq y_N+\varepsilon
\]
Adding $c>2\varepsilon$ to the inequality $x_N \geq y_N+c$ gives
\[
x_N>y_N+2\varepsilon \equiv x_N-\varepsilon>y_N+\varepsilon
\]

This simplifies our work because both $x_N-\varepsilon$ and $y_N+\varepsilon$ are rational numbers, and we know that
there exists a rational number $q$ such that $x_N-\varepsilon<q<y_N+\varepsilon$. We now know that for $n\geq N$,
\[
x_n \geq x_N -\varepsilon \longrightarrow x_n-q \geq x_N-\varepsilon-q>0
\]
and
\[
y_n \leq y_N +\varepsilon \longrightarrow q-y_n \geq q-(y_N+\varepsilon)>0
\]
Define a new sequence as the following: $x_n'=x_n$ if $n\geq N$, and $x_n'$ be any rational number such that $
|x_n'-x_N|\leq \varepsilon$, and define $y_n'$ in the same way. Obviously $x=\LIM{x_n'},y=\LIM{y_n'}$. And the sequences
$\sequence{x_n'-q}{1},\sequence{q-y_n'}{1}$ are both positively bounded away from zero. Hence $x<q<y$, as desired.
\end{proof}

\declareexercise{5.4.6}
\begin{proof}
On one hand, suppose that $|x-y|<\varepsilon$. If $x=y$, then $y-\varepsilon<x<y+\varepsilon$ is obvious. If $x>y$, then
$|x-y|=x-y<\varepsilon \rightarrow x<y+\varepsilon$, and $x>y \rightarrow x>y-\varepsilon$. If $x<y$, then
$x<y+\varepsilon$ and $|x-y|=y-x<\varepsilon\rightarrow x>y-\varepsilon$.

On the other hand, conversely, suppose that
\[
y-\varepsilon<x<y+\varepsilon \rightarrow x-y<\varepsilon \wedge y-x<\varepsilon
\]. If $x>y$, then $|x-y|=x-y<\varepsilon$. If $x=y$, then $|x-y|=0<\varepsilon$. If $x<y$, then
$|x-y|=y-x<\varepsilon$.
\end{proof}

\declareexercise{5.4.7}
\begin{proof}
(a)
On one hand, if $x\geq y$, then add $0<\varepsilon$ to it (See Proposition \ref{prop.5.4.basicproperties}), we have
$x \geq y+\varepsilon$.

On the other hand, conversely, if for all real number $\varepsilon>0$, $x \leq y+\varepsilon$, we show that $x\leq y$.
Presume the negation, that is, $x>y$, then as stated by Proposition 5.4.14, we can find $q$ such that $x>q>y$, so
$x>y+(q-y)$, a contradiction.

(b)
On one hand, suppose that $|x-y|\leq \varepsilon$ for all $\varepsilon>0$. If $x\geq y$, then
$|x-y|=x-y \leq \varepsilon$, so we have $x\leq y$ by (a). If $x\leq y$, we can conclude that $x\geq y$ as well. So
either way we will have $x=y$.

On the other hand, if $x=y$, then $|x-y|=0<\varepsilon$.
\end{proof}

\declareexercise{5.4.8}
\begin{proof}
We shall just prove the first one here.

Presume the negation, that is, $\LIM{a_n}>x$. Then we can find a rational $q$ such that $x<q<\LIM{a_n}$
(Proposition 5.4.14). Thus
$a_n\leq x<q$,
and then $\LIM{a_n}\leq\LIM{q}=q$ (Corollary 5.4.10). But we have $\LIM{a_n}>q$, a contradiction.
\end{proof}

\subsection{The Least Upper Bound Property}

\paragraph{Example 5.5.3}
This set has no upper bound because
\begin{itemize}
\item If $x$ is greater than an element in the set, then $x$ must also be an element in the set;
\item $\forall x \in \mathbb{R}^+(\exists y(y \in \mathbb{R}^+ \wedge y>x))$.
\end{itemize}
\begin{proof}
(1)
This is immediately derived from the fact that order is transitive (Proposition 5.4.7).

(2)
Consider the number $x+1$, which, according to (1), is also in the set, and which, is greater than $x$ as $y-x = 1>0$.
\end{proof}

\declareexercise{5.5.1}
\begin{proof}
First we show that every upper bound of $E$, $N$, is a lower bound for $-E$, and vice versa. This can be concluded from
\[
x \leq N \equiv -x \geq -N
\]
Then we show that $-M$ is the greatest lower bound. In fact,
\[
M \leq N \to -M \geq -N
\]
\end{proof}

\declareexercise{5.5.2}
\begin{proof}
We show that for all $L<m\leq K$, there at least exists one $m_0$ such that we can not have both $\frac{m_0}{n}$ and $\frac{m_0-1}{n}$ being upper bounds and they not being upper bounds. That is, for this $m_0$, either
\[
\frac{m_0}{n} \text{ is an upper bound} \wedge \frac{m_0-1}{n} \text{ is not}
\]
, which is impossible as $\frac{m_0}{n}>\frac{m_0-1}{n}$, or
\[
\frac{m_0-1}{n} \text{ is an upper bound} \wedge \frac{m_0}{n} \text{ is not}.
\]

Presume the negation, that is, (Note that this statement includes the situation when $\frac{m_0}{n}$ and $\frac{m_0-1}{n}$ are both not upper bounds)
\[
(\forall L<m\leq K)(\frac{m_0}{n} \text{ is an upper bound} \equiv \frac{m_0-1}{n} \text{ is an upper bound})
\]
We know that when $m=K$, $m/n$ is an upper bound. Then $(m-1)/n$ also is. Repeat the step until $m=L+1$, then we can
conclude that $\frac{m-1=L}{n}$ is an upper bound, a contradiction.
\end{proof}

\declareexercise{5.5.3}
\begin{proof}
\emph{This is a different approach.}

On one hand, for all $x<m_n \to x \leq m_n-1$, $x/n\leq (m_n-1)/n$, which means such $x/n$ cannot be upper bounds. On the other hand,
for all $x>m_n \to x-1\geq m_n$, $(x-1)/n\geq m_n/n$.
\end{proof}

\declareexercise{5.5.4}
\begin{proof}
(1) Simply take $M \geq \varepsilon$, then $|m_n/n-m_{n'}/n'|\leq 1/M\leq \varepsilon$.

(2)
Since we want to use \exerciseref{5.4.8}, we need to prove that for some $N$, $(\forall n \geq N, q_n < q_M) \vee (\forall n \geq N, q_n > q_M)$. This thought introduces the following lemma:
\begin{lem}
For any Cauchy sequence $a_n$, if $a_M \neq \LIM{a_n}$, then there exists a $N$ such that for all $n$ greater than or equal to it, $a_n$ are either all lesser than $a_M$ or all greater than $a_M$.
\end{lem}
\begin{proof}
Since $q_M \neq \LIM{a_n}$,
\[
\neg (\forall \varepsilon>0(\exists N(n\geq N \to |a_n-a_M| \leq \varepsilon) ))
\]
, which is
\[
\exists \varepsilon >0 (\forall N(\exists n\geq N(|a_n-a_M| > \varepsilon)))
\]
For this $\varepsilon$, $\exists N_1(n,n'\geq N_1 \to |a_n-a_{n'}| \leq \varepsilon)$. But we know $\exists N_2 \geq N_1(|a_{N_2}-a_M| > \varepsilon)$. So for all $n \geq N_2$, we have
\[
|a_n-a_{N_2}| \leq \varepsilon \wedge |a_{N_2} - a_M| > \varepsilon
\]
This means either $a_n < q_{M}$ (when $a_M > a_{N_2}$), or $a_n > a_{M}$ (when $a_M < a_{N_2}$).
\end{proof}

If $q_M = S$, then $|q_M-S| = 0 \leq \frac{1}{M}$.

If $q_M \neq S$, then according to the previous lemma, for some $N$ and all $n\geq N$, $q_n > q_M$ or $q_n<q_M$. We suppose that $q_n > q_M$ here. We also know that for all $n\geq M$, $|q_M - q_n| \leq \frac{1}{M}$. Then for $n \geq \max(N,M)$, we can say that $\frac{1}{M} \geq q_n - q_M >0$. The limit of the sequence $(q_n-q_M)_1^n$ is $S-q_M$ (To really make it a sequence, we define its value to be any number between $\frac{1}{M}$ and 0 when $n < \max(N,M)$). We can now apply \exerciseref{5.4.8} to it to obtain $\frac{1}{M}\geq S-q_M >0$ (Note that $S \neq q_M$ is our hypothesis.) A similar process produces $\frac{1}{M}\geq q_M-S >0$ when $q_n<q_M$.

We can now finish the proof.
\end{proof}

\declareexercise{5.5.5}
\begin{proof}
We will prove that there exists an irrational number $0<i<y-x$ and then $x+i$ automatically becomes the number we want.

Then we only need to prove that irrational numbers can be arbitrarily small. So we think of $\frac{\sqrt{2}}{N}$, which becomes smaller than any given $\varepsilon>0$ as $N$ approaches to infinite.

But first we need to prove that it is an irrational number. Presume the negation, that is, it is rational. We know that rationals are closed under multiplication, which means $\frac{\sqrt{2}}{N} \cdot N = \sqrt{2}$ is rational, a contradiction.
\end{proof}

\subsection{Real Exponentiation, Part I}
\paragraph{Lemma 5.6.5}
The set contains 0 because $0 \geq 0$ and for $n \geq 1$, $0^n = 0$.

If $y>1$, then according to Proposition 5.4.7 and Proposition 4.2.9, we know that we can multiply $y>1\wedge y>1$ to get $y^2>1^2=1$,
and so on. In fact, $y>1 \to y^2 > y$ and $y>1\to y \times 1 > 1 \times 1 \equiv y >1$, so $y^2>y>1$. We can use induction to prove
$y^n>1$.

Since that we can multiply such inequalities where both sides are positive, we can conclude from $y>x$ and $y>1$ that $y^n > x \times 1^{n-1} \equiv y^n > x$.

\declareexercise{5.6.1}
\begin{proof}
Let's denote the set $\{y \in \mathbb{R}:y \geq 0 \wedge y^n \leq x\}$ as $S$. Furthermore, we assert the following lemma without proof (the proof can be given by induction):
\begin{lem}
\[
(x+\varepsilon)^n = \sum_{i=0}^n{\binom{n}{i}n^i\varepsilon^{n-i}}
\]
\end{lem}

(a) If $y^n < x$, then there exists a real number $c$ such that $y^n < c < x$. By definition, $c \in$ the set that determines $x^{1/n}$. But $y$

(c) By definition, for any $y \in S$, $y \geq 0$.
\end{proof}


\section{Language}
\begin{figure}[!htb]
\small
\centering
\begin{tabular}{l | l}
\begin{tabular}{r l l}
\nonterm{non-track} & ::= & non track \\
& 0 & \\
$\alpha$, $\beta$, $\gamma$ & ::= &  track \\
& $\emptyset$ &  emptyset \\
& \{\nonterm{x}\} & singleton set \\
& $\alpha_1 \cup \alpha_2$  & set union \\
& $\alpha_1 \backslash \alpha_2$ & set subtraction\\
\nonterm{d} & ::=  & Dependency \\
& \nonterm{non-track} 	& not track \\
& \nonterm{track} & track \\
\nonterm{T} & ::= & Type \\
& \nonterm{Int}$^{0}$ & Integer \\
& \nonterm{\&Int}$^{\alpha}$ & Reference type \\
& (\nonterm{T}$_1$ $\to$ \nonterm{T}$_2)^{\alpha}$ & type of functions \\
\nonterm{t} & ::= & Term \\
& \nonterm{c} & constant \\
& \nonterm{x} & variable \\
& \nonterm{alloc} & allocation \\
& \nonterm{inc}(\nonterm{x}) & mutation \\
& \KW{let} \nonterm{x} = \nonterm{t}$_1$ \KW{in} \nonterm{t}$_2$  & let binding \\
& $\lambda$\nonterm{x}.\nonterm{t} & abstraction \\
& \nonterm{t}$_1$\nonterm{t}$_2$ & application \\
\end{tabular} &
\begin{tabular}{r l l}
$\Gamma$ & :=  & Type context \\
& $\emptyset$ & Empty context \\
& $\Gamma$, \nonterm{x}:\nonterm{T} \\
\nonterm{l} & $\in \mathcal{N}$  & Address \\
\nonterm{H} & := & Value environment \\
& $\emptyset$ & empty env \\
& \nonterm{H}, \nonterm{x}:\nonterm{v}  \\
\nonterm{v} & ::= & Value \\
& \nonterm{c} & constant \\
& \nonterm{l} & address \\
& <\nonterm{H}, $\lambda$\nonterm{x}.\nonterm{t}> & closure \\
\nonterm{e} $\in$ \nonterm{E} & ::= & Evaluation context \\
& [] & \\
& \KW{let} \nonterm{x}=[] \KW{in} \nonterm{e} \\
& \KW{let} \nonterm{x} = \nonterm{v} \KW{in} \nonterm{e} \\
& \nonterm{e} t \\
& \nonterm{v} e
\end{tabular}
\end{tabular} %
\caption{The syntax and semantic definitions of the programming language.}
\label{fig:syntax}
\end{figure}

\begin{figure}[!htb]
\begin{mathpar}
\begin{array}{l l}
\inferrule{}
{\Gamma \vdash c : Int^{0}} & \mbox{(TCST)}
\\
\inferrule{\Gamma(x) = T}
{\Gamma \vdash x : T} & \mbox{(TVAR)}
\\
\inferrule{}
{\Gamma \vdash alloc : \&Int^\emptyset} & \mbox{(TREF)}
\\
\inferrule{\Gamma \vdash x : \&Int^{\alpha}}
{\Gamma \vdash inc(x) : Int^{0}} & \mbox{(TINC)}
\\
\inferrule{\Gamma \vdash t_1: Int^{0}  \\ \Gamma, x:Int^{0} \vdash t_2 : T_2^{d}}
{\Gamma \vdash \KW{let} ~ x = t_1 \KW{in} ~ t_2 : T_2^{d}} & \mbox{(TLET1)}
\\
\inferrule{\Gamma \vdash t_1: T_1^{~\alpha}  \\ \Gamma, x:T_1^{~\alpha \cup \{x\}} \vdash t_2 : T_2^{0}}
{\Gamma \vdash \KW{let} ~ x = t_1 \KW{in} ~ t_2 : T_2^{0}} & \mbox{(TLET2)}
\\
\inferrule{\Gamma \vdash t_1: T_1^{~\alpha}  \\ \Gamma, x:T_1^{~\alpha \cup \{x\}} \vdash t_2 : T_2^{~\beta}}
{\Gamma \vdash \KW{let} ~ x = t_1 \KW{in} ~ t_2 : T_2^{~\beta \backslash \{x\}}} & \mbox{(TLET3)}
\\
%\inferrule{ \Gamma, x : T_1^{\{x\}} \vdash t : T_2^{\emptyset} \\ \gamma = MV(t) }
%{\Gamma \vdash \lambda x. t : (T_1^{\emptyset} \to T_2^{\emptyset})^{~\gamma}} & \mbox{(TABS1)}\\
%\mbox{where } \gamma \cap \{x\} = \emptyset \\
%\\
%\inferrule{ \Gamma, x : T_1^{\{x\}} \vdash t : T_2^{\{x\}} \\ \gamma = MV(t) }
%{\Gamma \vdash \lambda x. t : (T_1^{\{\emptyset\}} \to T_2^{~\{x\}})^{~\gamma}} & \mbox{(TABS2)} \\
%\mbox{where } \gamma \cap \{x\} = \emptyset \\
\inferrule{ \Gamma, x : T_1^{0} \vdash t : T_2^{d} \\ \gamma = MV(t) }
{\Gamma \vdash \lambda x. t : (T_1^{0} \to T_2^{d})^{~\gamma}} & \mbox{(TABS1)} \\
\mbox{where } \gamma \cap \{x\} = \emptyset \\
\inferrule{ \Gamma, x : T_1^{\alpha \cup \{x\}} \vdash t : T_2^{d} \\ \gamma = MV(t) }
{\Gamma \vdash \lambda x. t : (T_1^{\alpha} \to T_2^{d})^{~\gamma}} & \mbox{(TABS2)} \\
\mbox{where } \gamma \cap \{x\} = \emptyset \\
\\
\inferrule{ \Gamma \vdash t_1 : (T_1^{~\alpha} \to T_2^{~\beta})^{~\gamma} \\ \Gamma \vdash t_2 : T_1^{\delta} \\ \gamma \cap \delta = \alpha }
{\Gamma \vdash t_1t_2 : T_2^{~[\delta \backslash x]\beta} } & \mbox{(TAPP)}
\end{array}
\end{mathpar}
\caption{Types}
\end{figure}

\begin{figure}[!htb]
\begin{mathpar}
\begin{array}{r l l l}
MV(c) & = & \emptyset \\
MV(x) & = & \{x\} & \mbox{if } \Gamma(x) = \&Int^{~\alpha}\\
MV(alloc) & = & \emptyset \\
MV(inc(x)) & = & \{x\} \\
MV(\KW{let} ~ x = t_1 ~ \KW{in} ~ t_2) & = & MV(t_1) \cup MV(t_2) - \{x\} \\
MV(\lambda x. t) & = & MV(t) - \{x\} \\
MV(t_1t_2) & = & MV(t_1) \cup MV(t_2)
\end{array}
\end{mathpar}
\caption{Definition of MV}
\end{figure}
\clearpage
\subsection{Let rules}
\begin{figure}[!htb]
\begin{mathpar}
\begin{array}{l l}
\inferrule{\Gamma \vdash t_1: Int^{0}  \\ \Gamma, x:Int^{0} \vdash t_2 : T_2}
{\Gamma \vdash \KW{let} ~ x = t_1 ~ \KW{in} ~ t_2 : T_2} & \mbox{(TLET1)}
\\
\inferrule{\Gamma \vdash t_1: \&Int^{~\alpha}  \\ \Gamma, x:T_1^{~\alpha \cup \{x\}} \vdash t_2 : Int^{0}}
{\Gamma \vdash \KW{let} ~ x = t_1 ~ \KW{in} ~ t_2 : Int^{0}} & \mbox{(TLET2)}
\\
\inferrule{\Gamma \vdash t_1: \&Int^{~\alpha}  \\ \Gamma, x:\&Int^{~\alpha \cup \{x\}} \vdash t_2 : \&Int^{~\beta}}
{\Gamma \vdash \KW{let} ~ x = t_1 ~ \KW{in} ~ t_2 : \&Int^{~\beta \backslash \{x\}}} & \mbox{(TLET3)}
\\
\inferrule{\vdash t_1 : (y: T_1 \to T_2)^{\alpha} \\ \Gamma, x: (y: T_1 \to T_2)^{\alpha} \vdash t_2 : T}
{\Gamma \vdash \KW{let} ~ x = t_1 ~ \KW{in} ~ t_2 : T} & \mbox{(TLET4)}

\end{array}
\end{mathpar}
\begin{lstlisting}[language=Scala,basicstyle=\footnotesize\ttfamily]
let x1 = 1 in 1           // TLET1

let x2 = 1 in alloc()     // TLET1

let x3 = alloc in 1       // TLET2

let x4 = alloc in x4      // TLET3

let x5 = alloc in
  let x6 = alloc in
    x5                    //TLET3

let x7 = alloc in
  let x8 = inc(x7) in     // TLET1
     x8
\end{lstlisting}
\caption{Program examples of applying the rule \emph{LET}}

\begin{mathpar}
\begin{array}{l l}
\inferrule{\Gamma \vdash 1 : Int^{0} \\ \Gamma, x1: Int^{0} \vdash 1 : Int^{0}}
{\Gamma \vdash \KW{let} ~ x1 = 1 ~ \KW{in} ~ 1 : Int^{0}} & \mbox{(TLET1)}
\\
\inferrule{\Gamma \vdash 1 : Int^{0} \\ \Gamma, x2: Int^{0} \vdash alloc : \&Int^{\emptyset}}
{\Gamma \vdash \KW{let} ~ x2 = 1 ~ \KW{in} ~ alloc : \&Int^{\emptyset}} & \mbox{(TLET1)}
\\
\inferrule{\Gamma \vdash alloc : \&Int^{\emptyset} \\ \Gamma, x3: \&Int^{\{x3\}} \vdash 1 : Int^{0}}
{\Gamma \vdash \KW{let} ~ x3 = alloc ~ \KW{in} ~ 1 : Int^{0}} & \mbox{(TLET2)}
\\
\inferrule{\Gamma \vdash alloc : \&Int^{\emptyset} \\ \Gamma, x4: \&Int^{\{x4\}} \vdash x4 : \&Int^{\{x\}}}
{\Gamma \vdash \KW{let} ~ x4 = alloc ~ \KW{in} ~ x4 : \&Int^{\{x4\} \backslash \{x4\}}} & \mbox{(TLET3)}
\\
\inferrule{\Gamma \vdash alloc : \&Int^{\emptyset} \\ \Gamma, x6 : \&Int{\{x6\}} \vdash x5 : \&Int^{\{x5\}} }
{\Gamma \vdash \KW{let} ~ x6 = alloc ~ \KW{in} ~ x5 : \&Int^{\{x5\} \backslash \{x6\}}} & \mbox{(TLET3)}
\\
\inferrule{\Gamma \vdash inc(x7) : Int^{\emptyset} \\ \Gamma, x8: Int^{\emptyset} \vdash x8 : Int^{0}}
{\Gamma \vdash \KW{let} ~ x8 = inc(x7) ~ \KW{in} ~ x8 : Int^{0}} & \mbox{(TLET1)}
\\
\end{array}
\end{mathpar}
\caption{Examples of applying LET rules}
\end{figure}

\subsection{Abstraction rules}
\begin{enumerate}
\item $\gamma$ is a set of variables that may be aliased.
\item $\forall x \in \gamma, \Gamma(x) : \&Int^{\alpha}$
\end{enumerate}
\begin{figure}[!htb]
\begin{mathpar}
\begin{array}{l l}
\inferrule{ \Gamma, x : Int^{0} \vdash t : T \\ \gamma = MV(t) }
{\Gamma \vdash \lambda x. t : (Int^{0} \to T)^{~\gamma}} & \mbox{(TABS1)} \\
\inferrule{ \Gamma, x : \&Int^{\alpha \cup \{x\}} \vdash t : T \\ \gamma = MV(t) }
{\Gamma \vdash \lambda x. t : (\&Int^{\alpha} \to T)^{~\gamma}} & \mbox{(TABS2)} \\
\inferrule{ \Gamma, x : (y: T_1 \to T_2)^{\alpha} \vdash t : T_3 \\ \gamma = MV(t) }
{\Gamma \vdash \lambda x. t : ((T_1 \to T_2)^{\alpha} \to T_3)^{\gamma}} & \mbox{(TABS3)} \\
\end{array}
\end{mathpar}

\begin{lstlisting}[language=Scala,basicstyle=\footnotesize\ttfamily]
def f1(x: Int) = alloc()        // (Int$^{0}$ -> &Int$^{\emptyset}$)$^{\emptyset}$ (by TABS1)
def f2(x: &Int) = alloc()       // (&Int$^{\emptyset}$ -> &Int$^{\emptyset}$)$^{\emptyset}$ (by TABS2)
def f3(x: &Int) = x             // (&Int$^{\emptyset}$ -> &Int$^{\{x\}}$)$^{\emptyset}$ (by TABS2)

let y = alloc() in
  def f4(x: &Int) = y           // (&Int$^{\emptyset}$ -> &Int$^{y}$)$^{\{y\}}$ (by TABS2)

def f5(x: (&Int$^{\emptyset}$ -> Int$^{x}$)$^{0}$ = { 1 }  // f5: (&Int$^{\emptyset}$ -> Int$^{x}$)$^{0}$) -> Int$^{\emptyset}$
\end{lstlisting}
\caption{Program example of applying the rule \emph{TABS}}

\begin{mathpar}
\begin{array}{l l}
\inferrule{ \Gamma, x : Int^{0} \vdash t : \&Int^{\emptyset} \\  MV(alloc) = \emptyset}
{\Gamma \vdash \lambda x. alloc : (Int^{0} \to \&Int^{\emptyset})^{\emptyset}} & \mbox{(TABS1)}
\\
\inferrule{ \Gamma, x : \&Int^{\{x\}} \vdash alloc : \&Int^{\emptyset} \\ MV(alloc) = \emptyset }
{\Gamma \vdash \lambda x. alloc : (\&Int^{\emptyset} \to \&Int^{\emptyset})^{\emptyset}} & \mbox{(TABS2)}
\\
\inferrule{ \Gamma, x : \&Int^{\{x\}} \vdash x : \&Int^{\{x\}} \\ MV(x) - \{x\} = \emptyset }
{\Gamma \vdash \lambda x. x : (\&Int^{\emptyset} \to \&Int^{~\{x\}})^{~\emptyset}} & \mbox{(TABS2)}
\\
\inferrule{ \Gamma, x : \&Int^{\{x\}} \vdash y : \&Int^{\{x\}} \\ MV(y) - \{x\} = \{y\} }
{\Gamma \vdash \lambda x. y : (\&Int^{\emptyset} \to \&Int^{~\{y\}})^{y}} & \mbox{(TABS2)}
\\
\inferrule{ \Gamma, x : \&Int^{\{x\}} \vdash y : \&Int^{\{x\}} \\ MV(y) - \{x\} = \{y\} }
{\Gamma \vdash \lambda x. y : (\&Int^{\emptyset} \to \&Int^{~\{y\}})^{y}} & \mbox{(TABS2)}
\end{array}
\end{mathpar}
\caption{Example of appying the rule TABS.}
\end{figure}

\clearpage
\subsection{Application rules}
\begin{figure}[!htb]
\begin{mathpar}
\begin{array}{l l}
\inferrule{ \Gamma \vdash t_1 : (x:Int^{0} \to T)^{~\gamma} \\ \Gamma \vdash t_2 : Int^{0} }
{\Gamma \vdash t_1t_2 : T} & \mbox{(TAPP1)}
\\
\inferrule{ \Gamma \vdash t_1 : (x:\&Int^{~\alpha} \to \&Int^{~\beta})^{~\gamma} \\ \Gamma \vdash t_2 : \&Int^{\delta} \\ \gamma \cap \delta = \alpha }
{\Gamma \vdash t_1t_2 : \&Int^{~[\delta / x]\beta} } & \mbox{(TAPP2)}
\end{array}
\end{mathpar}
\end{figure}

\begin{lstlisting}[language=Scala,basicstyle=\footnotesize\ttfamily]
def f1(x: Int) = alloc()        // f1 : (Int$^{0} \to$ &Int$^{\emptyset})^{\emptyset}$
f1(0)  // f(0):  &Int$^{\emptyset}$

let y = alloc() in
  def f2(x: Int) = y           // (Int$^{0}$ -> &Int$^{\{y\}}$)$^{\{y\}}$
  f2(0)                        // f2(0): &Int$^{\{y\}}$

def f3(x: &Int) = alloc()       // (&Int$^{\emptyset}$ -> &Int$^{\emptyset}$)$^{\emptyset}$
let z = alloc() in
  f3(z)                         // f3(z): &Int$^{\emptyset}$

def f4(x: &Int) = x             // (&Int$^{\emptyset}$ -> &Int$^{\{x\}}$)$^{\emptyset}$
let t = alloc() in
  f4(t)                         // f4(t): &Int$^{\{t\}}$

\end{lstlisting}

\begin{mathpar}
\begin{array}{l l}
\inferrule{ \Gamma \vdash f1 : (x:Int^{0} \to \&Int^{\emptyset})^{\emptyset} \\  \Gamma \vdash 0 : Int^{0} \\ }
{\Gamma \vdash f1~ 0 : \&Int^{\emptyset}} & \mbox{(TAPP1)} \\
\inferrule{ \Gamma \vdash f2 : (x:Int^{0} -> \&Int^{\{y\}})^{\{y\}} \\  \Gamma \vdash 0 : Int^{0} \\ }
{\Gamma \vdash f2~ 0 : \&Int^{\{y\}}} & \mbox{(TAPP1)} \\
\inferrule{ \Gamma \vdash f3 : (x:\&Int^{\emptyset} -> \&Int^{\emptyset})^{\emptyset} \\  \Gamma \vdash z : \&Int^{\{z\}} \\ \emptyset \cap \{z\} = \emptyset  }
{\Gamma \vdash f3 ~z : \&Int^{[z/x]\emptyset}} & \mbox{(TAPP2)}

\end{array}
\end{mathpar}

\clearpage
\subsection{Inductive cases}
\begin{lstlisting}[language=Scala,basicstyle=\footnotesize\ttfamily]
let f1 = fun(x: Int => { alloc() } ) in  // f1: (Int$^{0}$ -> &Int$^{\emptyset}$)$^{\emptyset}$
  let f2 = f1 in
    f2(2)                                // &Int$^{\emptyset}$

\end{lstlisting}
\begin{figure}[!htb]
\begin{mathpar}
\begin{array}{l l}
\inferrule{\Gamma \vdash f1 : (x: Int^{0} \to \&Int^{\emptyset})^{\emptyset}  \\ \Gamma, f2: (x: Int^{0} \to \&Int^{\emptyset})^{\emptyset} \vdash f2(2) : Int^{0}}
{\Gamma \vdash \KW{let} ~ f2 = f1 ~ \KW{in} ~ f2(2) : Int^{0}  } & \mbox{(TLET???)}  \\

\end{array}
\end{mathpar}
\end{figure}

\begin{figure}
\begin{lstlisting}[language=Scala,basicstyle=\footnotesize\ttfamily]
let y = alloc() in
  let f3 = fun(x: &Int => { y } ) in      // (&Int$^{\emptyset}$ -> &Int$^{y}$)$^{\{y\}}$ (by TABS2)
     let f4 = f3 in
        f4(y)
\end{lstlisting}
\begin{mathpar}
\begin{array}{l l}
\inferrule*
{\Gamma \vdash f3 : (x: \&Int^{\emptyset} \to \&Int^{y})^{\{y\}} \\ \Gamma \vdash y : \&Int^{\{y\}}}
{\inferrule{ \Gamma, f3: (x: \&Int^{\emptyset} \to \&Int^{y})^{\{y\}} \vdash f4(y) :  \&Int^{\{y\}} \\ \{y\} \cap \{y\} = \{y\}}
{\Gamma \vdash \KW{let} ~ f4 = f3 ~ \KW{in} ~ f4(y) : Int^{0}}}
\end{array}
\end{mathpar}
\caption{Examples that get type errors}
\end{figure}
\clearpage

\begin{figure}[!htb]
\begin{mathpar}
\begin{array}{l l}
%\inferrule[(ECST)]{}
%{H ~ | ~ c \mapsto H ~ | ~ c }
%
%\inferrule[(EVAR)]{ x : v \in H }
%{H ~ | ~ x \mapsto H ~ | ~ v }
%
%\inferrule[(EREF)]{ fresh(l) }
%{H ~ | ~ alloc  \mapsto H ~ | ~ l }
%
%\inferrule[(ELET-VAR)]{}
%{H ~ | ~ \KW{let} ~ x = y ~ \KW{in} ~ t \mapsto H ~ | ~ [y / x]t }

\inferrule{}
{\KW{let} ~ x = y ~ \KW{in} ~ t \mapsto [y/x]t} &
\mbox{(ELET-VAR)}
\\

\inferrule{}
{\KW{let} ~ x = \lambda z.t  ~ \KW{in} ~ E[x y] \mapsto \KW{let} ~ x = \lambda z.t \KW{in} ~ E[[y/z]t] } &
\mbox{(ELET-ABS)}
\\

\inferrule{}
{\KW{let} ~ x = (\KW{let} ~ y = t_1 \KW{in} ~ t_2) ~ \KW{in} ~ t_3 \mapsto \KW{let} ~ y = t_1 ~ \KW{in} ~ \KW{let} ~ x = t_2 ~ \KW{in} ~ t_3} &
\mbox{(ELET-LET)}
\\

\inferrule{}
{E[t_1] \mapsto E[t_2]} \quad \mbox{\textbf{if} } t_1 \mapsto t_2 &
\mbox{(ET)}
\end{array}
\end{mathpar}
\caption{Semantics (Reduction)}
\end{figure}

\begin{lemma}[Substitution] If $\Gamma, x : T^{~\alpha \cup \{x\}} \vdash t : T^{\beta}$ and $\Gamma \vdash [y / x]t : [y/x]T^{\beta}$.
\end{lemma}

\begin{theorem}[Soundness] If $\Gamma \vdash E[t]:T^{~\alpha}$, then either $t$ is an answer $(t = x)$ or there exists $\Gamma', E', t'$, such that $E[t] \to E'[t']$ and $\Gamma, \Gamma' \vdash E'[t']: T^{~\alpha}$.
\end{theorem}


\documentclass[a4paper, 10pt]{article}

\usepackage{minted}
\usepackage{graphicx}
\usepackage{tabularx}
\usepackage{syntax}
\usepackage{fullpage}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{hyperref}
\usepackage{libertine}
%\usepackage[T1]{fontenc}

\title{Channel Allocation in SC-FDMA: Research assignment}
\author{Noric Couderc}
\date{\today}

\begin{document}

\maketitle
\tableofcontents

\section{Background}

\subsection*{System model, problem definition and notation}
In order to test the current implementation of the algorithm, a reference
implementation is used. The reference implementation uses constraint programming
to find optimal solutions to the problem at hand. Therefore, the mathematical
model of a setup for \textbf{Min-Power} is hereby stated.

\subsubsection*{Parameters}

\begin{itemize}
    \item Set of users: $\mathcal{M} = \{1,\dots,M\}, M \in \mathbb{N}$.
    \item Set of channels: $\mathcal{N} = \{1,\dots,N\}, N \in \mathbb{N}$.
    \item Power limit function: $P^u = \mathcal{M} \rightarrow \mathbb{R}^+$.
    \item Channels peak power limit: $P^s = \mathcal{N} \rightarrow \mathbb{R}^+$.
    \itam Power function: $P_{uc} : \mathcal{M} \times \mathcal{N} \rightarrow \mathbb{R}$.
    % \item Utility value of assigning block $b$ to user $i$: $u_{ib} \in \mathbb{R}^+$.
    \item Demand target of user $i$: $d_i \in \mathbb{R}+$.
    \item The gain of user $i$ on channel $j$: $g_{ij} \in \mathbb{R}^+$.
    \item The channel bandwith: $B \in \mathbb{R}^+$.
    \item The noise power spectral density times the channel bandwidth:
    $\sigma \in \mathbb{R}^+$.
    \item The rate of user $i$, on channel $j$, with power $p_{j_i}$:
    $f(i,j,p_{j_i})
    \rightarrow \mathbb{R}^+$, where $p_{j_i}$ is the power consumed by user
    $i$ on channel $j$ (depends of the number of
        channels assigned to $i$, defined below).
\end{itemize}
\textbf{Note:}``user $i$" refers to $i \in \mathcal{M}$, ``channel $j$" refers to
$j \in \mathcal{N}$, ``block $b$" refers to $b \in \mathcal{B}$.

\subsubsection*{Variables}
\begin{itemize}
    \item Set of channel blocks: $\mathcal{B}$ where each element $b_i \in
    \mathcal{B}$ is itself a set of channels allocated to user $i$.
    \item Each user $i$, allocated $n$ channels will use, for each
          allocated channel $c$~: $p_{c_i} = \min\{ P^u / n, P^s\}$.

          Therefore, each user $i$, allocated a block $b_i$ will use:
          $p_{b_i} = |b_i| \min\{ P^u / |b_i|, P^s\}$.
\end{itemize}

\subsubsection*{Cost function}
The problem of \textbf{Min-Power} is to minimize:
\begin{align*}
     P &= \sum\limits_{i \in \mathcal{M}} p_{b_i}
 \end{align*}

\subsubsection*{Constraints}
\begin{itemize}
    \item Each user is allocated one block: $|\mathcal{B}| = |\mathcal{M}|$.
    \item All blocks contain channels: $\forall i \in \mathcal{M}: b_i \subseteq \mathcal{N}$.
    \item Each pair of user is allocated different channels.
    \begin{align*}
    \forall i,j \in \mathcal{M}: i \neq j \leftrightarrow b_i \cap b_j = \emptyset
    \end{align*}
    \item The block $b_i$ allocated to user $i$ should satisfy user $i$'s
    demand:
    \begin{align*}
        \forall i, b : \sum\limits_{j \in b}
        f(i, j, p_{j_i}) \geq d_i \text{where:}\\
        f(i, j, p_{j_i}) = B \log_2(1 + \frac{p_{j_i}g_{ij}}{\sigma^2})
    \end{align*}
    \item For each user $i$, the power level $p_{b_i}$ is less than the users power limit.
    \begin{equation*}
        \forall i \in \mathcal{M} : p_{b_i} \leq P^u
    \end{equation*}
    \item The power level must stay under the channels peak power limit.
    \begin{equation*}
    \forall i \in \mathcal{M} : p_{b_i} \leq P^s
    \end{equation*}
\end{itemize}

\subsubsection*{Allocation Schemes}
The constraints on blocks change depending of the allocation scheme.
\begin{itemize}
    \item Localized: All blocks are convex sets (they contain only
            consecutive channels).
    \begin{equation*}
    \forall b \in \mathcal{B}: 1 + \max(b) - \min(b) = |b|
    \end{equation*}
    \item Interleaved: All channels in a block are equally spaced.
    That is, for each block $b \in \mathcal{B}$ its elements are spaced by a value $s$:
    \begin{equation*}
        \forall c, c' \in b : c \neq c' \leftrightarrow |c' - c| = s
    \end{equation*}
    %For this particular constraint, we construct the set in several
    %steps. Given a spacing parameter $s$, the desired size $c$, and
    %the desired first channel $m$, each potential block $b$ is built:
    %\begin{enumerate}
    %    \item Build the set $b_1 =
    %    \{-\lfloor c/2 \rfloor,\cdots,\lfloor c/2 \rfloor\}$.
    %    \item Multiply all components of $b_1$ by the spacing parameter
    %    $s$. $\forall i \in b_1 : b_{2}(i) = s i$
    %    \item $\min b_3 = m$ : $\forall j \in b_2 : b_3(j) = \min(s_2) + m + j$
    %\end{enumerate}
\end{itemize}

\section{A variation of the algorithm: details}

\section{Algorithm description}

\section{Performance comparison}

\section{Results, discussion and analysis}

\section{Conclusion}

\bibliographystyle{ieeetr}
\bibliography{literature}

\end{document}


\section{Conclusion}
\label{sec:conclusion}
%Summarize the entire report.
%(Summarize the entire report)
A spectral approach for intrusion detection is presented.
The unsupervised definition of anomalies used in the report is that
\begin{inparaenum}[\itshape a\upshape)]
\item the pattern similar to the majority of abnormal patterns or dissimilar to the majority of normal patterns, and
\item infrequent occurrence.
\end{inparaenum}
Graph Laplacian captures those patterns well from the data transported by similarity function that is constructed by GMM.
Experimental results on the NSL-KDD dataset indicate that spectral approach is very effective both unseen known types of anomalies and unseen unknown type of anomalies.
%limitations of eigensolver's sensitivity, one-to-one clustering is not applicable when the data have considerable noises.
%The proposed algorithm with multiclass spectral clustering has better detection performance than that of one-to-one spectral clustering but density based approach is not very effective.
The proposed algorithm with multiclass spectral clustering has very good detection performance but density based approach is not very effective.
%However, multiclass spectral clustering's performance is good on those situation. %the number of cluster is fixed.
%The approximated k-NN might improve the applicability.
%The proposed algorithm has the same detection performace as (sth), but it is (sth) efficient.

% (Future work)
%% Outline what needs to/can be done in the future.
Future work on more robust multiclass approach is needed.
More specifically, it would be interesting to design an algorithm with robust eigensolver, where the most noise will have the highest influence on the clustering performance.
In addition, an extension of density sensitive similarity function is also worth considering.
%(salability, sensitivity, coverage)


\hypertarget{group__numpp__differentiation}{}\section{Differentiation}
\label{group__numpp__differentiation}\index{Differentiation@{Differentiation}}
\subsection*{Modules}
\begin{DoxyCompactItemize}
\item
\hyperlink{group__numpp__differentiation__automatic}{Automatic Differentiation}
\begin{DoxyCompactList}\small\item\em This module provides the means for performing automatic differentiation. \end{DoxyCompactList}\item
\hyperlink{group__numpp__differentiation__finite}{Finite Differentiation}
\begin{DoxyCompactList}\small\item\em This module provides the means for performing finite difference differentiation. \end{DoxyCompactList}\item
\hyperlink{group__numpp__differentiation__symbolic}{Symbolic Differentiation}
\begin{DoxyCompactList}\small\item\em This module provides the means for performing symbolic differentiation. \end{DoxyCompactList}\end{DoxyCompactItemize}

\subsection{Detailed Description}
Module provides all means for differentiation provided by numpp It includes 3 submodules\+:
\begin{DoxyItemize}
\item automatic
\item finite difference
\item symbolic
\end{DoxyItemize}

\begin{DoxyCode}
\textcolor{preprocessor}{#include"numpp/differentiation/differentiation.hpp"}
\end{DoxyCode}

{\bfseries Check each of the modules to see provided functionality}


\chapter{Basic concepts}
\label{Chp:Concepts}

The LAGraph library is a collection of high level graph algorithms
based on the GraphBLAS C API.  These algorithms construct
graph algorithms expressed ``in the language of linear algebra.''
Graphs are expressed as matrices, and the operations over
these matrices are generalized through the use of a
semiring algebraic structure.

In this chapter, we will define the basic concepts used to
define the LAGraph Library  We provide the following elements:

\begin{itemize}
\item Glossary of terms and notation used in this document.
\item The LAGraph objects.
\item Return codes and other constants used in LAGraph.
\end{itemize}

Currently, I've kept the text from the GraphBLAS concepts chapter in
this document.  We may want to borrow some of the GraphBLAS glossary items
and perhaps use some of the table formatting in LAGraph.

\section{Glossary}

%=============================================================================

\subsection{LAGraph basic definitions}

\glossBegin

\glossItem{application} A program that calls methods from the GraphBLAS C API to
solve a problem.

\glossItem{GraphBLAS C API} The application programming interface that fully defines the types, objects,
literals, and other elements of the C binding to the GraphBLAS.

\glossItem{function} Refers to a named group of statements in the C programming language.  Methods, operators,
and user-defined functions are typically implemented as C functions.
When referring to
the code programmers write, as opposed to the role of functions as an element of the GraphBLAS, they may
be referred to as such.

\glossItem{method} A function defined in the GraphBLAS C API that manipulates
GraphBLAS objects or other opaque features of the implementation of the GraphBLAS API.

\glossItem{operator} A function that performs an operation on the elements stored in GraphBLAS matrices and vectors.

\glossItem{GraphBLAS operation} A mathematical operation defined in the
GraphBLAS mathematical specification. These operations (not to be confused with \emph{operators}) typically act
on matrices and vectors with elements defined in terms of an algebraic semiring.
\glossEnd

%=============================================================================

\subsection{LAGraph objects and their structure}

\glossBegin
\glossItem{non-opaque datatype} Any datatype that exposes its internal structure and
can be manipulated directly by the user.

\glossItem{opaque datatype} Any datatype that hides its internal structure and can
be manipulated only through an API.

\glossItem{GraphBLAS object}  An instance of an \emph{opaque datatype} defined
by the \emph{GraphBLAS C API} that is manipulated only through the GraphBLAS
API. There are four kinds of GraphBLAS opaque objects: \emph{domains} (i.e., types),
\emph{algebraic objects} (operators, monoids and semirings),
\emph{collections} (scalars, vectors, matrices and masks), and descriptors.

\glossItem{handle}  A variable that holds a reference to an instance of one of
the GraphBLAS opaque objects.  The value of this variable holds a reference to
a GraphBLAS object but not the contents of the object itself.  Hence, assigning
a value to another variable copies the reference to the GraphBLAS object of one
handle but not the contents of the object.

\glossItem{domain} The set of valid values for the elements stored in a
GraphBLAS \emph{collection} or operated on by a GraphBLAS \emph{operator}.
Note that some GraphBLAS objects involve functions that map values from
one or more input domains onto values in an output domain.  These GraphBLAS
objects would have multiple domains.

\glossItem{collection} An opaque GraphBLAS object that holds a number of
elements from a specified \emph{domain}. Because these objects are based on an
opaque datatype, an implementation of the GraphBLAS C API has the flexibility
to optimize the data structures for a particular platform.  GraphBLAS objects
are often implemented as sparse data structures, meaning only the subset of the
elements that have values are stored.

\glossItem{implied zero}  Any element that has a valid index (or indices)
in a GraphBLAS vector or matrix but is not explicitly identified in the list of
elements of that vector or matrix. From a mathematical perspective, an
\emph{implied zero} is treated as having the
value of the zero element of the relevant monoid or semiring.
However, GraphBLAS operations are purposefully defined using set notation in such a way
that it makes it unnecessary to reason about implied zeros.
Therefore, this concept is not used in the definition of GraphBLAS methods and operators.

\glossItem{mask} An internal GraphBLAS object used to control how values
are stored in a method's output object.  The mask exists only inside a method; hence,
it is called an \emph{internal opaque object}.  A mask is formed from the elements of
a collection object (vector or matrix) input as a mask parameter to a method. GraphBLAS
allows two types of masks:
\begin{enumerate}
\item In the default
case, an element of the mask exists for each element that exists in the
input collection object when the value of that element, when cast to a Boolean type, evaluates to
{\tt true}.
\item In the {\it structure only} case, masks have structure but no values.
The input collection describes a structure whereby an
element of the mask exists for each element stored in the input collection regardless of its value.
\end{enumerate}

\glossItem{complement} The \emph{complement} of a
GraphBLAS mask, $M$, is another mask, $M'$, where the elements of $M'$
are those elements from $M$ that \emph{do not} exist.
\glossEnd

%=============================================================================

\subsection{Algebraic structures used in the GraphBLAS}

\glossBegin
\glossItem{associative operator} In an expression where a binary operator is used
two or more times consecutively, that operator is \emph{associative} if the result
does not change regardless of the way operations are grouped (without changing their order).
In other words, in a sequence of binary operations using the same associative
operator, the legal placement of parenthesis does not change the value resulting
from the sequence operations.  Operators that are associative over infinitely
precise numbers (e.g., real numbers) are not strictly associative when applied to
numbers with finite precision (e.g., floating point numbers). Such non-associativity
results, for example, from roundoff errors or from the fact some numbers can not
be represented exactly as floating point numbers.   In the GraphBLAS specification,
as is common practice in computing, we refer to operators as \emph{associative}
when their mathematical definition over infinitely precise numbers is associative
even when they are only approximately associative when applied to finite precision
numbers.

No GraphBLAS method will imply a predefined grouping over any associative operators.
Implementations of the GraphBLAS are encouraged to exploit associativity to optimize
performance of any GraphBLAS method with this requirement. This holds even if the
definition of the GraphBLAS method implies a fixed order for the associative operations.

\glossItem{commutative operator} In an expression where a binary operator is used (usually
two or more times consecutively), that operator is \emph{commutative} if the result does
not change regardless of the order the inputs are operated on.

No GraphBLAS method will imply a predefined ordering over any commutative operators.
Implementations of the GraphBLAS are encouraged to exploit commutativity to optimize
performance of any GraphBLAS method with this requirement. This holds even if the
definition of the GraphBLAS method implies a fixed order for the commutative operations.

\glossItem{GraphBLAS operators} Binary or unary operators that act on elements of GraphBLAS
objects.  \emph{GraphBLAS operators} are used to express algebraic structures used in the
GraphBLAS such as monoids and semirings. They are also used as arguments to several
GraphBLAS methods. There are two types of \emph{GraphBLAS operators}:
(1) predefined operators found in Table~\ref{Tab:PredefOperators} and (2) user-defined
operators created using {\sf GrB\_UnaryOp\_new()} or {\sf GrB\_BinaryOp\_new()}.

\glossItem{monoid} An algebraic structure consisting of one domain, an associative
binary operator, and the identity of that operator.  There are two types
of GraphBLAS monoids: (1) predefined monoids found in
Table~\ref{Tab:PredefinedMonoids} and (2) user-defined monoids created using .

\glossItem{semiring} An algebraic structure consisting of a set of allowed values
(the \emph{domain}), a commutative and associative binary operator called addition, a binary operator
called multiplication (where multiplication distributes over addition),
and identities over addition (\emph{0}) and multiplication (\emph{1}).  The additive
identity is an annihilator over multiplication.

\glossItem{GraphBLAS semiring} is allowed to diverge from the mathematically
rigorous definition of a \emph{semiring} since certain combinations of domains, operators, and identity
elements are useful in graph algorithms even when they do not strictly match the mathematical
definition of a semiring.
There are two types
of \emph{GraphBLAS semirings}: (1) predefined semirings found in
Tables~\ref{Tab:PredefinedTrueSemirings} and~\ref{Tab:PredefinedUsefulSemirings}, and (2) user-defined semirings created using
{\sf GrB\_Semiring\_new()} (see Section~\ref{Sec:AlgebraMethods}).

\glossItem{index unary operator} A variation of the unary operator that operates
on elements of GraphBLAS vectors and matrices along with the index values
representing their location in the objects.  There are predefined index unary
operators found in Table~\ref{Tab:PredefIndexOperators}), and user-defined
operators created using {\sf GrB\_IndexUnaryOp\_new} (see Section~\ref{Sec:AlgebraMethods}).
\glossEnd

%=============================================================================

\subsection{The execution of an application using the GraphBLAS C API}

\glossBegin
\glossItem{program order} The order of the GraphBLAS method calls in a
thread, as defined by the text of the program.

\glossItem{host programming environment} The GraphBLAS specification defines an API.
The functions from the API appear in a program.  This program is written using a programming language
and execution environment defined outside of the GraphBLAS.  We refer to this programming environment
as the ``host programming environment''.

\glossItem{execution time} time expended while executing instructions defined by a program.
This term is specifically used in this specification in the context of computations
carried out on behalf of a call to a GraphBLAS method.

%% The original definition was too narrow by only referencing GraphBLAS methods.
%% Also, I didn't see the reason why the comment about execution time was retrieved was included.
%\glossItem{execution time} The time it takes to execute a GraphBLAS
%method call. Implementations are free, but not mandated, to specify how
%the execution time of a method call can be retrieved.

\glossItem{sequence} A GraphBLAS application uniquely defines a directed
acyclic graph (DAG) of GraphBLAS method calls based on their program order.
At any point in a program, the state of any GraphBLAS object is defined by a
subgraph of that DAG.  An ordered collection of GraphBLAS method calls in program order that
defines that subgraph for a particular object is the \emph{sequence} for that object.

\glossItem{complete}  A GraphBLAS object is complete when it can be used in a happens-before relationship
with a method call that reads the variable on another thread.  This concept is used
when reasoning about memory orders in multithreaded programs.  A GraphBLAS object defined on one thread
that is complete can be safely used as an {\sf IN} or {\sf INOUT} argument
in a method-call on a second thread assuming the method calls are correctly synchronized so the definition on
the first thread \emph{happens-before} it is used on the second thread.  In blocking-mode, an object is
complete after a GraphBLAS method call that writes to that object returns.   In nonblocking-mode, an object is complete
after a call to the {\sf GrB\_wait()} method with the {\sf GrB\_COMPLETE} parameter.

\glossItem{materialize} A GraphBLAS object is materialized when it is (1) complete, (2) the computations
defined by the sequence that define the object have finished (either fully or stopped at an error) and will not consume any
additional computational resources, and (3) any errors associated with that sequence are available to be read according to the
GraphBLAS error model.  A GraphBLAS object that is never loaded into a non-opaque data structure may
potentially never be materialized.  This might happen, for example, if the operations
associated with the object are fused or otherwise changed by the runtime system
that supports the implementation of the GraphBLAS C API.   An object can be materialized by a call
to the materialize mode of the {\sf GrB\_wait()} method.

\glossItem{context}  An instance of the GraphBLAS C API implementation
as seen by an application.  An application can have only one context between the
start and end of the application.
A context begins with the first thread that calls {\sf GrB\_init()} and ends with the
first thread to call {\sf GrB\_finalize()}.
It is an error for {\sf GrB\_init()} or {\sf GrB\_finalize()} to be called more than one
time within an application.  The context is used to constrain the behavior of an
instance of the GraphBLAS C API implementation and support various execution strategies.
Currently, the only
supported constraints on a context pertain to the mode of program execution.

\glossItem{program execution mode} Defines how a GraphBLAS sequence executes, and is associated
with the {\it context} of a GraphBLAS C API implementation. It is set by an
application with its call to {\sf GrB\_init()} to one of two possible states.
In \emph{blocking mode}, GraphBLAS methods return after the computations
complete and any output objects have been materialized.  In {\it nonblocking mode}, a
method may return once the arguments are tested as consistent with
the method (\ie, there are no API errors), and potentially before any computation
has taken place.
\glossEnd

%=============================================================================

\subsection{GraphBLAS methods: behaviors and error conditions}
\glossBegin
\glossItem{implementation-defined behavior} Behavior that must be documented
by the implementation and is allowed to vary among different
compliant implementations.

\glossItem{undefined behavior} Behavior that is not specified by the GraphBLAS C API.
A conforming implementation is free to choose results delivered from a method
whose behavior is undefined.

\glossItem{thread-safe}  Consider a function called from multiple threads with
arguments that do not overlap in memory (i.e. the argument lists do not share
memory).  If the function is \emph{thread-safe} then it will behave the same
when executed concurrently by multiple threads or sequentially on a single
thread.

\glossItem{dimension compatible} GraphBLAS objects (matrices and vectors) that are
passed as parameters to a GraphBLAS method are dimension (or shape) compatible if
they have the correct number of dimensions and sizes for each dimension to satisfy
the rules of the mathematical definition of the operation associated with the method.
If any \emph{dimension compatibility} rule above is violated, execution of the GraphBLAS
method ends and the {\sf GrB\_DIMENSION\_MISMATCH} error is returned.

\glossItem{domain compatible} Two domains for which values from one domain can be
cast to values in the other domain as per the rules of the C language. In particular,
domains from Table~\ref{Tab:PredefinedTypes}
are all compatible with each other, and a domain from a user-defined type is only
compatible with itself. If any \emph{domain compatibility} rule above is
violated, execution of the GraphBLAS method ends and the {\sf GrB\_DOMAIN\_MISMATCH}
error is returned.
\glossEnd

\vfill

\newgeometry{left=2.5cm,top=2cm,bottom=2cm}

%=============================================================================
%=============================================================================

\section{Notation}

\begin{tabular}[H]{l|p{5in}}
Notation & Description \\
\hline
$\Dout, \Dinn, \Din1, \Din2$  & Refers to output and input domains of various GraphBLAS operators. \\
$\bDout(*), \bDinn(*),$ & Evaluates to output and input domains of GraphBLAS operators (usually \\
~~~~$\bDin1(*), \bDin2(*)$ & a unary or binary operator, or semiring). \\
$\mathbf{D}(*)$   & Evaluates to the (only) domain of a GraphBLAS object (usually a monoid, vector, or matrix). \\
$f$             & An arbitrary unary function, usually a component of a unary operator. \\
$\mathbf{f}(F_u)$ & Evaluates to the unary function contained in the unary operator given as the argument. \\
$\odot$         & An arbitrary binary function, usually a component of a binary operator. \\
$\mathbf{\bigodot}(*)$ & Evaluates to the binary function contained in the binary operator or monoid given as the argument. \\
$\otimes$       & Multiplicative binary operator of a semiring. \\
$\oplus$        & Additive binary operator of a semiring. \\
$\mathbf{\bigotimes}(S)$ & Evaluates to the multiplicative binary operator of the semiring given as the argument. \\
$\mathbf{\bigoplus}(S)$ & Evaluates to the additive binary operator of the semiring given as the argument. \\
$\mathbf{0}(*)$   & The identity of a monoid, or the additive identity of a GraphBLAS semiring. \\
$\mathbf{L}(*)$   & The contents (all stored values) of the vector or matrix GraphBLAS objects.  For a vector, it is the set of (index, value) pairs, and for a matrix it is the set of (row, col, value) triples. \\
$\mathbf{v}(i)$ or $v_i$   & The $i^{th}$ element of the vector $\vector{v}$.\\
$\mathbf{size}(\vector{v})$ & The size of the vector $\vector{v}$.\\
$\mathbf{ind}(\vector{v})$ & The set of indices corresponding to the stored values of the vector $\vector{v}$.\\
$\mathbf{nrows}(\vector{A})$ & The number of rows in the $\matrix{A}$.\\
$\mathbf{ncols}(\vector{A})$ & The number of columns in the $\matrix{A}$.\\
$\mathbf{indrow}(\vector{A})$ & The set of row indices corresponding to rows in $\matrix{A}$ that have stored values.  \\
$\mathbf{indcol}(\vector{A})$ & The set of column indices corresponding to columns in $\matrix{A}$ that have stored values. \\
$\mathbf{ind}(\vector{A})$ & The set of $(i,j)$ indices corresponding to the stored values of the matrix. \\
$\mathbf{A}(i,j)$ or $A_{ij}$ & The element of $\matrix{A}$ with row index $i$ and column index $j$.\\
$\matrix{A}(:,j)$ & The $j^{th}$ column of matrix $\matrix{A}$.\\
$\matrix{A}(i,:)$ & The $i^{th}$ row of matrix $\matrix{A}$.\\
$\matrix{A}^T$ &The transpose of matrix $\matrix{A}$. \\
$\neg\matrix{M}$ & The complement of $\matrix{M}$.\\
s$(\matrix{M})$ & The structure of $\matrix{M}$.\\
$\vector{\widetilde{t}}$ & A temporary object created  by the GraphBLAS implementation. \\
$<type>$ & A method argument type that is {\sf void *} or one of the types from Table~\ref{Tab:PredefinedTypes}. \\
{\sf GrB\_ALL} & A method argument literal to indicate that all indices of an input array should be used.\\
{\sf GrB\_Type} & A method argument type that is either a user defined type or one of the  types from Table~\ref{Tab:PredefinedTypes}.\\
{\sf GrB\_Object} &  A method argument type referencing any of the GraphBLAS object types.\\
{\sf GrB\_NULL} & The GraphBLAS NULL.
\end{tabular}

\restoregeometry

\section{Mathematical foundations}

Graphs can be represented in terms of matrices. The values stored in these
matrices correspond to attributes (often weights) of edges in the graph.\footnote{More information on the mathematical foundations can be found in the following paper: J. Kepner, P. Aaltonen, D. Bader,  A. Buluç, F. Franchetti, J. Gilbert, D. Hutchison, M. Kumar, A. Lumsdaine, H. Meyerhenke, S. McMillan, J. Moreira, J. Owens, C. Yang, M. Zalewski, and T. Mattson. 2016, September. Mathematical foundations of the GraphBLAS. In \emph{2016 IEEE High Performance Extreme Computing Conference (HPEC)} (pp. 1-9). IEEE.}
Likewise, information about vertices in a graph are stored in vectors.
The set of valid values that can be stored in either matrices or vectors
is referred to as their domain. Matrices are usually sparse because the
lack of an edge between two vertices means that nothing is stored at the
corresponding location in the matrix.  Vectors may be sparse or dense, or they may
start out sparse and become dense as algorithms traverse the graphs.

Operations defined by the GraphBLAS C API specification operate on these
matrices and vectors to carry out graph algorithms.  These GraphBLAS
operations are defined in terms of GraphBLAS semiring algebraic
structures. Modifying the underlying semiring changes the result of
an operation to support a wide range of graph algorithms.
Inside a given algorithm, it is often beneficial to change the GraphBLAS
semiring that applies to an operation on a matrix.  This has two
implications for the C binding of the GraphBLAS API.

First, it means that we define a separate object for the semiring
to pass into methods.  Since in many cases the full
semiring is not required, we also support passing monoids or
even binary operators, which means the semiring is implied rather than
explicitly stated.

Second, the ability to change semirings impacts the meaning of
the \emph{implied zero} in a sparse representation of a matrix or vector.
This element in real arithmetic is zero, which is the
identity of the \emph{addition} operator and the annihilator of the
\emph{multiplication} operator.  As the semiring changes, this
implied zero changes to the identity of the \emph{addition} operator
and the annihilator (if present) of the \emph{multiplication} operator
for the new semiring. Nothing changes regarding what is stored in the sparse
matrix or vector, but the implied zeros within them change with respect to a
particular operation. In all cases, the nature of the implied zero does not
matter since the GraphBLAS C API requires that implementations treat them as
nonexistent elements of the matrix or vector.

As with matrices and vectors, GraphBLAS semirings have domains
associated with their inputs and outputs.  The semirings in the
GraphBLAS C API are defined with two domains associated with the input operands and one
domain associated with output.  When used in the GraphBLAS C API these
domains may not match the domains of the matrices and vectors supplied in
the operations.  In this case, only valid \emph{domain compatible} casting
is supported by the API.

The mathematical formalism for graph operations in the language of
linear algebra often assumes that we can operate in the field of real numbers.
However, the GraphBLAS C binding is designed for implementation on computers,
which by necessity have a finite number of bits to represent numbers.
Therefore, we require a conforming implementation to use floating point
numbers such as those defined by the IEEE-754 standard (both single- and double-precision)
wherever real numbers need to be represented. The practical implications of
these finite precision numbers is that the result of a sequence of
computations may vary from one execution to the next as the grouping of operands
(because of associativity) within the operations changes.  While techniques are known to
reduce these effects, we do not require or even expect an implementation
to use them as they may add considerable overhead. In most
cases, these roundoff errors are not significant. When they are significant,
the problem itself is ill-conditioned and needs to be reformulated.

\section{LAgraph  objects}

Objects defined in the GraphBLAS standard include types (the domains of
elements), collections of elements (matrices, vectors, and scalars), operators
on those elements (unary, index unary, and binary operators), algebraic
structures (semirings and monoids), and descriptors.   GraphBLAS objects are
defined as opaque types; that is, they are managed, manipulated, and accessed
solely through the GraphBLAS application programming interface. This gives an
implementation of the GraphBLAS C specification flexibility to optimize objects
for different scenarios or to meet the needs of different hardware platforms.

A GraphBLAS opaque object is accessed through its \emph{handle}.  A handle is
a variable that references an instance of one of the types from
Table~\ref{Tab:ObjTypes}.  An implementation of the GraphBLAS specification
has a great deal of flexibility in how these handles are implemented.  All
that is required is that the handle corresponds to a type defined in the
C language that supports assignment and comparison for equality.  The
GraphBLAS specification defines a literal {\sf GrB\_INVALID\_HANDLE} that is
valid for each type.  Using the logical equality operator from C, it must be
possible to compare a handle to {\sf GrB\_INVALID\_HANDLE} to verify that a
handle is valid.

\begin{table}
\hrule
\begin{center}
\caption{Types of GraphBLAS opaque objects.}
\label{Tab:ObjTypes}
~\\
\begin{tabular}{l|l}
{\sf GrB\_Object types} & Description \\
\hline
{\sf GrB\_Type}           & Scalar type.     \\ \hline
{\sf GrB\_UnaryOp}        & Unary operator.     \\
{\sf GrB\_IndexUnaryOp}   & Unary operator, that operates on a single value and its location index values.     \\
{\sf GrB\_BinaryOp}       & Binary operator.     \\
{\sf GrB\_Monoid}         & Monoid algebraic structure.     \\
{\sf GrB\_Semiring}       & A GraphBLAS semiring algebraic structure. \\ \hline
{\sf GrB\_Scalar}         & One element; could be empty. \\
{\sf GrB\_Vector}         & One-dimensional collection of elements; can be sparse.     \\
{\sf GrB\_Matrix}         & Two-dimensional collection of elements; typically sparse.    \\ \hline
{\sf GrB\_Descriptor}     & Descriptor object, used to modify behavior of methods (specifically \\
                          & GraphBLAS operations).     \\
\end{tabular}
\end{center}
\hrule
\end{table}

Every GraphBLAS object has a \emph{lifetime}, which consists of
the sequence of instructions executed in program order between the
\emph{creation} and the \emph{destruction} of the object.  The GraphBLAS C
API predefines a number of these objects which are created
when the GraphBLAS context is initialized by a call to {\sf GrB\_init}
and are destroyed when the GraphBLAS context is terminated by a call to
{\sf GrB\_finalize}.

An application using the GraphBLAS API can create additional objects by
declaring variables of the appropriate type from Table~\ref{Tab:ObjTypes} for
the objects it will use.  Before use, the object must be initialized
with a call call to one of the object's respective \emph{constructor} methods.
Each kind of object has at least one explicit constructor method of the form
{\sf GrB\_*\_new} where `{\sf *}' is replaced with the type of object (e.g.,
{\sf GrB\_Semiring\_new}). Note that some objects, especially collections,
have additional constructor methods such as duplication, import, or
deserialization.  Objects explicitly created by a call to a constructor
should be destroyed by a call to {\sf GrB\_free}. The behavior of a program
that calls {\sf GrB\_free} on a pre-defined object is undefined.

%This is typically done with one of
%the methods that has a ``{\sf \_new}'' suffix in its name (e.g.,
%{\sf GrB\_Vector\_new}).  If available, an object can also be initialized by
%duplicating an existing object with one of the methods that has the
%``{\sf \_dup}'' suffix in its name  (e.g., {\sf GrB\_Vector\_dup}).  Note that
%there are other valid constructor methods included in the API (e.g.,
%``{\sf \_diag}'', ``{\sf \_import}'', and ``{\sf \_deserialize}'' matrix
%methods).  Regardless of the method of construction, any resources associated
%with that object can be released (destructed) by a call to the {\sf GrB\_free}
%method when an application is finished with an object.

These constructor and destructor methods are the only methods that change
the value of a handle.  Hence, objects changed by these methods are passed
into the method as pointers.  In all other cases, handles are not changed by the
method and are passed by value.  For example, even when multiplying matrices,
while the contents of the output product matrix changes, the handle for that
matrix is unchanged.

Several GraphBLAS constructor methods take other objects as input arguments
and use these objects to create a new object. For all these
methods, the lifetime of the created object must end strictly before
the lifetime of any dependent input objects. For example, a vector constructor
{\sf GrB\_Vector\_new} takes a {\sf GrB\_Type} object as input. That type
object must not be destroyed until after the created vector is destroyed.
Similarly, a {\sf GrB\_Semiring\_new} method takes a monoid and
a binary operator as inputs. Neither of these can be destroyed until
after the created semiring is destroyed.

Note that some constructor methods like {\sf GrB\_Vector\_dup} and
{\sf GrB\_Matrix\_dup} behave differently. In these cases, the input
vector or matrix can
be destroyed as soon as the call returns. However, the original type
object used to create the input vector or matrix cannot be destroyed
until after the vector or matrix created by {\sf GrB\_Vector\_dup} or
{\sf GrB\_Matrix\_dup} is destroyed.  This behavior must hold for any
chain of duplicating constructors.

Programmers using GraphBLAS handles must be careful to distinguish between a
handle and the object manipulated through a handle.  For example, a program may
declare two GraphBLAS objects of the same type, initialize one, and then assign
it to the other variable.  That assignment, however, only assigns the handle to
the variable.  It does not create a copy of that variable (to do that, one
would need to use the appropriate duplication method).  If later the object is
freed by calling {\sf GrB\_free} with the first variable, the object is
destroyed and the second variable is left referencing an object that no longer
exists (a so-called ``dangling handle'').

In addition to opaque objects manipulated through handles, the GraphBLAS C API
defines an additional opaque object as an internal object; that is, the object
is never exposed as a variable within an application.  This opaque object is
the mask used to control which computed values can be stored in the output
operand of a \emph{GraphBLAS operation}.  .


\documentclass[12pt]{article}

\usepackage[left=1cm,right=1cm,
    top=2cm,bottom=2cm,bindingoffset=0cm]{geometry}
\usepackage{braket}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{mathrsfs}
\usepackage[T2A]{fontenc}
\usepackage[utf8x]{inputenc}
\usepackage[english]{babel}
\usepackage{graphicx}
\parindent=0.5cm

\usepackage{hyperref}
\usepackage{indentfirst}

\numberwithin{equation}{section}
%\usepackage{showkeys}

\begin{document}
\section*{MSAI Statistics \& Probability – Week 4 Seminar \& HW}\\

\textbf{Problem 1:} Let $x$ have exponential distribution with parameter $\lambda>0$. Find the probability density functions of
\begin{enumerate}
    \item $x^n,~~n\in\mathbb{N}$
    \item $1-e^{- x}$
\end{enumerate}

\textbf{Problem 2:} Let $x$ and $y$ be independent random variables uniformly distributed on some interval $[0,a]$. Find the probability density functions of $x-y$ and $x\cdot y$.

\end{document}