\documentclass[9pt]{article}

\usepackage{amsmath}
\usepackage{tcolorbox}
% `parskip` removes indentation for all paragraphs: http://tex.stackexchange.com/a/55016
\usepackage{parskip}
% Allows us to color rows / cols of a table.
% See https://texblog.org/2011/04/19/highlight-table-rowscolumns-with-color/
\usepackage{color, colortbl}

\usepackage{hyperref}
\graphicspath{{images/ps2b/}}

\leftmargin=0.25in
\oddsidemargin=0.25in
\textwidth=6.0in
\topmargin=-0.25in
\textheight=9.25in

\definecolor{Gray}{gray}{0.9}

\begin{document}

\begin{center}
  \large\textbf{MIT 18.01 Problem Set 2B Unofficial Solutions}
\end{center}

\begin{tcolorbox}
  \renewcommand{\thefootnote}{\roman{footnote}}
  \textbf{Q1) Golf balls} The area of a section of a sphere of radius $R$ between two parallel planes that are a distance $h$ apart is \footnote{This formula will be derived in Unit 4. Two examples may convince you that it is reasonable. For $h = R$, it gives the area of the hemisphere, $2 \pi R^2.$ For $h = 2R$ it gives $4 \pi R^2$, the area of the whole sphere.}\\
  \begin{center}
    area of a spherical section = $2 \pi h R$
  \end{center}
  \ \\
  Slice the sphere of radius $R$ by a horizontal plane. The portion of the plane inside the sphere is a disk of radius $r \leq R$. The portion of the spherical surface above the plane is called a \emph{spherical cap}. For example, if the plane passes through the center, then the disk has radius $r = R$, its circumference is the equator, and the spherical cap is the Northern Hemisphere. More generally, a spherical cap is the portion of surface of the Earth north of a latitude line. The formula above applies to regions between two latitude lines, and, in particular, to spherical caps.\\
  \\
  a) Consider a spherical cap which is the portion of the surface of the sphere above horizontal plane that slices the sphere at or above its center. Find the area of the cap as a function of $R$ and $r$. Do this by finding first the formula for the height $h$ of the spherical cap in terms of $r$ and $R$. (This height is the vertical distance from the horizontal slicing plane to the North Pole.) Then use your formula for $h$ and the formula above for the area of spherical sections.
\end{tcolorbox}

\begin{center}
  \includegraphics[scale=0.8]{q1a.jpg}
\end{center}

In the above diagram, we see that the distance between the 2 horizontal slicing planes is $R - h$, where $h$ is the height of the spherical cap. The insight is that, the distance from the center of the sphere to any point on the circumference is $R$. We form a right triangle using this line and the line with the radius $r$ of the spherical cap.

By the Pythagorean formula, $(R - h)^2 = R^2 - r^2$. Hence

\begin{align*}
  R - h &= \sqrt{R^2 - r^2}\\
  h &= R - \sqrt{R^2 - r^2}
\end{align*}

Then the area of the spherical cap is $2 \pi h R = 2 \pi (R - \sqrt{R^2 - r^2}) R = 2 \pi R (R - \sqrt{R^2 - r^2})$

\begin{tcolorbox}
  \textbf{Q1b)} Express the formula for the area of a spherical cap in terms of $R^2$ and $r / R$. (This is natural because the proportional scaling $cr$ and $cR$ changes the area by the factor $c^2$.) Then use the linear and quadratic approximations to $(1 + x)^{1/2}$ near $x = 0$ to find a good and an even better approximation to the area of the spherical cap, appropriate when the ratio $r / R$ is small. (Hint: What is $x$?) Simplify your answers as far as possible: the approximation corresponding to the linear approximation to $(1 + x)^{1/2}$ should be very familiar.
\end{tcolorbox}

In part (a), we see that the area of the spherical cap is $2 \pi R (R - \sqrt{R^2 - r^2})$. This can also be expressed as

\begin{align*}
  2 \pi R (R - \sqrt{R^2 - r^2}) &= 2 \pi R (R - \sqrt{R^2 (1 - \frac{r^2}{R^2})}) \\
                                 &= 2 \pi R (R - R \sqrt{1 - \frac{r^2}{R^2}}) \\
                                 &= 2 \pi R^2 (1 - \sqrt{1 - \frac{r^2}{R^2}}) \\
\end{align*}

When $\frac{r}{R}$ is small, $\frac{r^2}{R^2} \approx 0$. Let $x = -\frac{r^2}{R^2}$. We are going to use the linear approximation of $(1 + x)^{1/2}$. Let $f(x) = (1 + x)^{1/2}$. Then $f(0) = (1 + 0)^{1/2} = 1$ and $f'(x) = \frac{1}{2} (1 + x)^{-1/2}$ and $f'(0) = \frac{1}{2}(1 + 0)^{-1/2} = \frac{1}{2}$.

Hence the linear approximation to $(1 + x)^{1/2} = f(0) + f'(0)(x - 0) = 1 + \frac{1}{2}x$ and the linear approximation to $\sqrt{1 - \frac{r^2}{R^2}}$ is thus $1 + \frac{1}{2}(-\frac{r^2}{R^2}) = 1 - \frac{r^2}{2R^2}$

Linear approximation to area of spherical cap is thus $2 \pi R^2 (1 - (1 - \frac{r^2}{2R^2})) = 2 \pi R^2 (\frac{r^2}{2R^2}) = \pi r^2$

For quadratic approximation to $\sqrt{1 - \frac{r^2}{R^2}}$, we need to compute the 2nd derivative of $f(x) = (1 + x)^{1/2}$, which is $f''(x) = -\frac{1}{4}(1 + x)^{-3/2}$. Now, $f''(0) = -\frac{1}{4}(1 + 0)^{-3/2} = -\frac{1}{4}$

Hence, quadratic approximation to $\sqrt{1 - \frac{r^2}{R^2}}$ is:

\begin{align*}
  1 + \frac{1}{2}(-\frac{r^2}{R^2} - 0) - \frac{\frac{1}{4}}{2}(-\frac{r^2}{R^2} - 0)^2 &= 1 - \frac{r^2}{2R^2} - \frac{r^4}{8R^4}
\end{align*}

Quadratic approximation to area of spherical cap is

\begin{align*}
  2 \pi R^2 (1 - (1 - \frac{r^2}{2R^2} - \frac{r^4}{8R^4})) &= 2 \pi R^2 (\frac{r^2}{2R^2} + \frac{r^4}{8R^4}) \\
                                                            &= \pi r^2 + \pi \frac{r^4}{4R^2}
\end{align*}

\begin{tcolorbox}
  \textbf{Q1c)} The following problem appeared on a middle school math contest exam. The numbers have been changed to protect the innocent. Consider a golf ball that is 3 centimeters in diameter with 100 hemispherical dimples of diameter 3 millimeters. (Note that this is not a realistic golf ball because the dimples are too deep). Find the area of the golf ball rounded to the nearest $1/100$ of a square centimeter using the approximation $\pi \approx 3.14$. (The students were given three minutes. We are spending more time on it.) \\

  Under the rules of the contest, an incorrectly rounded answer was counted as wrong with no partial credit, so correct numerical approximations were crucial. Some students objected that they could not figure out the area of portion of the large sphere that is removed when a dimple is inserted. A careless examiner had assumed that the students would use the approximation that the area removed for each dimple was nearly the same as the area of a flat disk. We are going to figure out whether this approximation is adequate or gives the wrong answer according to the rules.\\

  Write down formulas for the surface area of the golf ball in the three cases listed below. (Put in 100 dimples, but leave $r, R$, and $\pi$ as letters.)\\
  \\
  i) the approximation pretending that the removed surface is flat (what is the relationship between this and the approximations of part (b)?)\\
  \\
  ii) the higher order approximation you derived in part (b)\\
  \\
  iii) the exact formula\\

  Finally, evaluate each of the answers for the given values $r = .15$ and $R = 1.5$ centimeters, and find the accuracy of the approximations.
\end{tcolorbox}

Area of the golf ball rounded to nearest $1 / 100$ of a square centimeter is $4 \pi (3 / 2)^2 \approx 28.27 cm^2$

\textbf{Part (i)}

Each dimple removes an area of approximately $\pi r^2$

Hence removing 100 dimples leaves $4 \pi R^2 - 100 \pi r^2$ surface area remaining

\textbf{Part (ii)}

Each dimple removes an area of approximately $\pi r^2 + \pi \frac{r^4}{4R^2}$

Hence removing 100 dimples leaves $4 \pi R^2 - 100 \pi r^2 - 25 \pi \frac{r^4}{R^2}$ surface area remaining.

\textbf{Part (iii)}

Exact area removed per dimple is $2 \pi R^2 (1 - \sqrt{1 - \frac{r^2}{R^2}})$

Hence removing 100 dimples leaves $4 \pi R^2 - 200 \pi R^2 (1 - \sqrt{1 - \frac{r^2}{R^2}})$ surface area remaining.

\textbf{Calculations using $r = .15$ and R = $1.5$ centimeters}

For part (i), we get $28.27 - 100 \pi (0.15)^2 \approx 21.20 cm^2$

For part (ii), we get $28.27 - 100 \pi (0.15)^2 - 25 \pi \frac{(0.15)^4}{1.5^2} \approx 21.18 cm^2$

For part (iii), we get $28.27 - 200 \pi (1.5)^2 (1 - \sqrt{1 - \frac{0.15^2}{1.5^2}}) \approx 21.18cm^2$

Hence quadratic approximation is more accurate.

\begin{tcolorbox}
  \textbf{Q1d)} Although nobody noticed it at the time, the examiner who created this problem made a much bigger mistake. With the diameters actually given, it would have been impossible for the number of dimples given to be placed on the golf ball without overlap. Give a (reasonable) estimate for the largest number of dimples that can fit on our golf ball.
\end{tcolorbox}

\textbf{Skipped. (Attempted but got very weird figures that contradicted the question)}

\begin{tcolorbox}
  \textbf{Q2)} Draw the graph of $f(x) = 1/(1 + x^2)$ and, directly underneath, it with the graphs of $f'(x)$ and $f''(x)$. Label critical points and inflection points on the graph of $f$ with their coordinates. Draw vertical lines joining these special points of the graph of $f$ to the corresponding points on the graphs below.
\end{tcolorbox}

\begin{center}
  \includegraphics[]{q2.jpg}
\end{center}

Now for the hard work. Let's compute $f'(x), f''(x), f'''(x)$ and $f''''(x)$.

\begin{align*}
  f'(x) = \frac{d}{dx} \frac{1}{1 + x^2} &= \frac{d}{dx}(1 + x^2)^{-1} \\
                                         &= -1 \cdot (1 + x^2)^{-2} \cdot 2x \\
                                         &= -\frac{2x}{(1 + x^2)^2} \\
\\
f''(x) &= \frac{d}{dx} (-\frac{2x}{(1 + x^2)^2}) \\
       &= \frac{d}{dx} \frac{-2x}{(1 + x^2)^2} \\
       &= \frac{-2(1 + x^2)^2 - (-2x)\cdot 2(1 + x^2)(2x)}{((1 + x^2)^2)^2} \\
       &= \frac{-2(1 + x^2)^2 + 8x^2 (1 + x^2)}{(1 + x^2)^4} \\
       &= \frac{-2(1 + x^2) + 8x^2}{(1 + x^2)^3} \\
       &= \frac{-2 - 2x^2 + 8x^2}{(1 + x^2)^3} \\
       &= \frac{6x^2 - 2}{(1 + x^2)^3} \\
\\
f'''(x) &= \frac{d}{dx} \frac{6x^2 - 2}{(1 + x^2)^3} \\
        &= \frac{12x (1 + x^2)^3 - (6x^2 - 2)3(1 + x^2)^2 (2x)}{((1 + x^2)^3)^2} \\
        &= \frac{12x (1 + x^2)^3 - (36x^3 - 12x)(1 + x^2)^2}{(1 + x^2)^6} \\
        &= \frac{12x (1 + x^2) - 36x^3 + 12x}{(1 + x^2)^4} \\
        &= \frac{12x + 12x^3 - 36x^3 + 12x}{(1 + x^2)^4} \\
        &= \frac{-24x^3 + 24x}{(1 + x^2)^4} \\
\\
f''''(x) &= \frac{d}{dx} \frac{-24x^3 + 24x}{(1 + x^2)^4} \\
         &= \frac{(-72x^2 + 24) (1 + x^2)^4 - (-24x^3 + 24x)4(1 + x^2)^3 2x}{((1 + x^2)^4)^2} \\
         &= \frac{(-72x^2 + 24) (1 + x^2)^4 - (-192x^4 + 192x^2)(1 + x^2)^3}{(1 + x^2)^8} \\
         &= \frac{(-72x^2 + 24) (1 + x^2) + 192x^4 - 192x^2}{(1 + x^2)^5} \\
         &= \frac{-72x^2 - 72x^4 + 24 + 24x^2 + 192x^4 - 192x^2}{(1 + x^2)^5} \\
         &= \frac{120x^4 - 240x^2 + 24}{(1 + x^2)^5}
\end{align*}

\subsection*{For the graph of $f(x) = \frac{1}{1 + x^2}$}

There are no discontinuities for $f(x) = \frac{1}{1 + x^2}$. There are also no x-intercepts. In fact, this graph is always positive because the numerator $1$ is positive and the denominator $1 + x^2$ is always positive so dividing the numerator by the denominator is also always positive.

For the y-intercept, $x = 0$. In this case, $y = \frac{1}{1 + 0^2} = 1$ so $(0, 1)$ is the y-intercept.

Now for the critical points of $f(x) = \frac{1}{1 + x^2}$. These occur when $f'(x) = -\frac{2x}{(1 + x^2)^2} = 0$, or equivalently when $-2x = 0$ or $x = 0$. At $x = 0$, $f(x) = \frac{1}{1 + 0^2} = 1$ so $(0, 1)$ is a critical point (this point also happens to be the y-intercept). Computing the second derivative, we see that $f''(0) = \frac{6(0)^2 - 2)}{(1 + 0^2)^3} = -2 < 0$ so $(0, 1)$ is a maximum point.

For the inflection points of $f(x) = \frac{1}{1 + x^2}$. These occur when $f''(x) = \frac{6x^2 - 2}{(1 + x^2)^3} = 0$. Or equivalently, when $6x^2 - 2 = 0$ and $6x^2 = 2$ and $3x^2 = 1$ and $x^2 = \frac{1}{2}$ and $x = \pm \frac{1}{\sqrt{3}}$. At both these points, $f(x) = \frac{1}{1 + 1/3} = \frac{1}{4/3} = \frac{3}{4}$. Hence $(-\frac{1}{\sqrt{3}}, \frac{3}{4})$ and $(\frac{1}{\sqrt{3}}, \frac{3}{4})$ are the inflection points.

Now for the limits. $\lim\limits_{x \rightarrow \pm\infty} \frac{1}{1 + x^2} = 0$

With the above information, we can construct the graph of $f(x) = \frac{1}{1 + x^2}$

\subsection*{For the graph of $f'(x) = -\frac{2x}{(1 + x^2)^2}$}

The x-intercept occurs at $f'(x) = -\frac{2x}{(1 + x^2)^2} = 0$ which is when $x = 0$. So $(0, 0)$ is the x-intercept. For the y-intercept, $f'(0) = -\frac{2(0)}{(1 + 0^2)^2} = 0$ so $(0, 0)$ is also the y-intercept.

The inflection points of $f(x)$ are at $x = \pm\frac{1}{\sqrt{3}}$. These now become the critical points of $f'(x)$. At $x = -\frac{1}{\sqrt{3}}, f'(x) = -\frac{2 (- 1 / \sqrt{3})}{(1 + (- 1 / \sqrt{3})^2)^2} = \frac{2 / \sqrt{3}}{(1 + 1/3)^2} = \frac{2}{\sqrt{3} \cdot \frac{16}{9}} = \frac{18}{16 \sqrt{3}} = \frac{9}{8 \sqrt{3}}$ so $(-\frac{1}{\sqrt{3}}, \frac{9}{8 \sqrt{3}})$ is a critical point. At $x = \frac{1}{\sqrt{3}}$, $f'(x) = -\frac{2 (1 / \sqrt{3})}{(1 + (1 / \sqrt{3})^2)^2} = -\frac{2}{\sqrt{3} (1 + 1/3)^2} = -\frac{2}{\sqrt{3} \cdot \frac{16}{9}} = -\frac{2 * 9}{\sqrt{3} \cdot 16} = -\frac{9}{8 \sqrt{3}}$ so $(\frac{1}{\sqrt{3}}, -\frac{9}{8 \sqrt{3}})$ is the other critical point. Now, $f'''(-\frac{1}{\sqrt{3}}) \approx -2.9228357377724787 < 0$ so $(-\frac{1}{\sqrt{3}}, \frac{9}{8 \sqrt{3}})$ is a maximum point. Similarly $f'''(\frac{1}{\sqrt{3}}) \approx 2.9228357377724787 > 0$ so $(\frac{1}{\sqrt{3}}, -\frac{9}{8 \sqrt{3}})$ is a minimum point.

$\lim\limits_{x \rightarrow \pm\infty} -\frac{2x}{(1 + x^2)^2} = \lim\limits_{x \rightarrow \pm\infty} -\frac{2}{2(1 + x^2)2x} = 0$.

Notice that at $x < 0$, $-2x > 0$ and $(1 + x^2)^2 > 0$ so $f'(x) = -\frac{2x}{(1 + x^2)^2} > 0$. At $x > 0, -2x < 0$ and $(1 + x^2)^2 > 0$ so $f'(x) = -\frac{2x}{(1 + x^2)^2} < 0$. The crossover point comes at $(0, 0)$.

With the above information, we can construct the graph of $f'(x) = -\frac{2x}{(1 + x^2)^2}$

\subsection*{For the graph of $f''(x) = \frac{6x^2 - 2}{(1 + x^2)^3}$}

The inflection points of $f(x)$ are now the x-intercept of $f''(x)$. At $x = \pm \frac{1}{\sqrt{3}}$, $f''(x) = 0$.

For the y-intercept, $f''(0) = \frac{6(0)^2 - 2}{(1 + 0^2)^3} = -2$ so $(0, -2)$ is the y-intercept.

Notice also that the only $x$ terms are $x^2$ which have even powers. So this is an even function. Let us then consider the branch where $x > 0$ and we can construct the $x < 0$ case from there.

For the critical points, let $f'''(x) = \frac{-24x^3 + 24x}{(1 + x^2)^4} = 0$. Then $-24x^3 + 24x = 0$ and $24x(- x^2 + 1) = 0$ so $x = 0$ or $-x^2 + 1 = 0$ which is equivalent to $x^2 - 1 = 0$ or $x = \pm 1$. At $x = 1$, $f''(1) = \frac{6(1)^2 - 2}{(1 + 1^2)^3} = \frac{4}{2^3} = \frac{1}{2}$ so $(1, \frac{1}{2})$ is a critical point. Using the second derivative of $f''(x)$ which is $f''''(x)$, we see that $f''''(\frac{1}{2}) = -9.33888 < 0$ so $(1, \frac{1}{2})$ is a maximum point. Similarly, $f''''(0) = 24 > 0$ so $(0, -2)$ is a minimum point.

Now for the limits. $\lim\limits_{x \rightarrow \infty}\frac{6x^2 - 2}{(1 + x^2)^3} = \lim\limits_{x \rightarrow \infty}\frac{12x}{3(1 + x^2)^2 2x} = \lim\limits_{x \rightarrow \infty}\frac{2}{(1 + x^2)^2} = 0$.

With the above information, we can construct the graph of $f''(x) = \frac{6x^2 - 2}{(1 + x^2)^3}$

\end{document}


\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{
    amsmath,
    amssymb,
    graphics,
    setspace,
    tcolorbox,
    fancyhdr,
    accents,
    cancel,
    graphicx,
    adjustbox,
    xcolor,
}
\pagestyle{fancy}
\setlength{\headheight}{20pt}
\graphicspath{ {./images/} }

\definecolor{soft-grey}{rgb}{0.6941176470588235,0.7294117647058823,0.7607843137254902}
\definecolor{blue-ui}{rgb}{0.023529411764705882,0.06666666666666667,0.10980392156862745}

\makeatletter
\newcommand{\globalcolor}[1]{%
  \color{#1}\global\let\default@color\current@color
}
\makeatother
\AtBeginDocument{\globalcolor{soft-grey}}
\pagecolor{blue-ui}

\newcommand{\DD}[2]{\frac{d #1}{d #2}}
\newcommand{\At}[1]{\Bigr|_{#1}}
\newcommand{\PD}[2]{\frac{\partial #1}{\partial #2}}

\begin{document}
\lhead{Jacovie Rodriguez}
\rhead{Modern Physics Summary}

\section{Relativity and Spacetime}
\subsection{Galilean Transform}
In classical physics we learn the concept of

\adjincludegraphics[width=.5\textwidth,trim={{.21\width} 0 {.21\width} 0}, clip]{DemoSpacetimeAxes}
A Lagrangian is dependent on a given function $f$:
\begin{equation}
    L[f] = L \left( x, f(x), f'(x) \right)
\end{equation}

The Action is defined as a path integral for the Lagrangian:
\begin{equation}
    A[f] = \int_a^b L[f] dx
\end{equation}

Suppose $f(x)$ extremizes $A$. Then let's define $\zeta$ as small arbitrary perturbation of $f$:
\begin{equation}
    \label{eqn:perturbation}
    \zeta(x) = f(x) + \epsilon \eta(x)
\end{equation}
\begin{itemize}
    \setlength{\itemindent}{2em}
    \item The $\eta(x)$ is the difference between $f$ and its perturbation $\zeta$. Since $\zeta$ must start on $a$ and end on $b$ just as $f$, this requires that $\eta(a) = \eta(b) = 0$.
    \item The $\epsilon$ here is a small constant. By definition, $\zeta \to f$ as $\epsilon \to 0$.
\end{itemize}

If we analyze the perturbed Action
\begin{equation}
    A[\zeta] = \int_a^b L \left( \zeta \right) dx
\end{equation}

\hspace{\parindent} then we see as $\epsilon \to 0$, $A[\zeta] \to A[f]$.
\\

Since $A[f]$ is an extremum, then it must be the case that:
\begin{equation}
    \label{eqn:actionlimit}
    \lim_{\epsilon \to 0} \DD{A[\zeta]}{\epsilon} = 0
\end{equation}

Let's see how we can evaluate the total derivative via chain rule:
\begin{align}
    \DD{A[\zeta]}{\epsilon}
     & = \DD{}{\epsilon} \int_a^b L [\zeta] dx \\
     & = \int_a^b \DD{L}{\epsilon} dx          \\
     & = \int_a^b \left[
        \DD{x}{\epsilon} \PD{L}{x}
        + \DD{\zeta}{\epsilon} \PD{L}{\zeta}
        + \DD{\zeta'}{\epsilon} \PD{L}{\zeta'}
        \right] dx
\end{align}

We can evaluate each total derivative:

\begin{center}
    \begin{tabular}{ c|c|c }
        $\displaystyle \DD{x}{\epsilon}  = 0$
         & $\displaystyle \DD{\zeta}{\epsilon}  = \eta(x)$
         & $\displaystyle \DD{\zeta'}{\epsilon} = \eta'(x)$   \\
         &                                                  & \\
        since $x$ doesn't depend on $\epsilon$
         & since $\zeta(x) = f(x) + \epsilon \eta(x)$
         & since $\zeta'(x) = f'(x) + \epsilon \eta'(x)$      \\
    \end{tabular}
\end{center}

Altogether then,
\begin{align}
    \DD{A[\zeta]}{\epsilon}
     & = \int_a^b
    \left[
        \eta(x) \PD{L}{\zeta}
        + \eta'(x) \PD{L}{\zeta'}
    \right] dx    \\
    \lim_{\epsilon \to 0}
    \DD{A[\zeta]}{\epsilon}
     & = \int_a^b
    \left[
        \eta(x) \PD{L}{f}
        + \eta'(x) \PD{L}{f'}
        \right] dx
    = 0
\end{align}

\hspace{\parindent} where the last line goes to zero following from (\ref{eqn:actionlimit}).
\\

Now, we have information about $\eta$ and its axes, but not $\eta'$.

\end{document}


\chapter{Link Model}

\begin{algorithm}[H]
    \DontPrintSemicolon
    \SetKwFunction{Frssidiff}{rssidiff}
    \SetKwProg{Fn}{Function}{}{}

    % \Fn{\Frssidiff{l1, l2}}{
    \Fn{\Frssidiff{links}}{
        result $\leftarrow$ \{ \}\;
        \ForEach{$\alpha \in$ (0, 5, 10, ..., 180)}{

            bucket $\leftarrow$ \{ \}\;
            \ForEach{l1, l2 $\in$ links}{
                \If{share\_node(l1, l2) \KwAnd $\alpha < \theta$(l1, l2)$\le \alpha + 5$}{
                    l1\_rssi $\leftarrow$ ($tx_{power}$ - l\_d(l1.distance)) - l1.rssi\;
                    l2\_rssi $\leftarrow$ ($tx_{power}$ - l\_d(l2.distance)) - l2.rssi\;
                    %bucket $\leftarrow$ bucket $\cup$ {abs(l1\_rssi - l2\_rssi)}\;
                    bucket $\leftarrow$ bucket $\cup$ \{|l1\_rssi - l2\_rssi|\}\;
                }
            }
            result $\leftarrow$ result $\cup$ bucket\;
        }
        \KwReturn result\;
    }
    \caption{Pseoducode for the RSSI difference for all link pairs}
    \label{algo:linkmodel}
\end{algorithm}


\documentclass[11pt]{report}
\usepackage{geometry}
\usepackage{graphicx}
\usepackage{titling}
\usepackage{fancyhdr}
\usepackage{enumerate}
\usepackage{ulem}
\usepackage{latexsym}
\usepackage{float}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage[labelfont=bf]{caption}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{listings}
\usepackage{lmodern}
\parindent=0pt
%\frenchspacing

\pagestyle{fancy}

\fancyhead[L]{\slshape\footnotesize March 10, 2014\\\textsc{01410 Cryptology 1}}
\fancyhead[R]{\slshape\footnotesize \textsc{Andreas Kjeldsen (s092638)}\\\textsc{Morten Eskesen (s133304)}}
\fancyfoot[C]{\thepage}
\newcommand{\HRule}{\rule{\linewidth}{0.5mm}}

\begin{document}

\begin{titlepage}
\begin{center}

\includegraphics[scale=2.0]{../GFX/dtu_logo.pdf}\\[1cm]
\textsc{\LARGE Technical University of Denmark}\\[1.5cm]
\textsc{\Large 01410 Cryptology 1}\\[0.5cm]

% Title
\HRule \\[0.4cm]
{\huge \bfseries Homework 1}\\[0.1cm]
\HRule \\[1.5cm]

% Author and supervisor
{\large
\emph{Authors:} \\[10pt]
Andreas Hallberg \textsc{Kjeldsen}\\
\emph{s092638@student.dtu.dk} \\[10pt]
Morten Chabert \textsc{Eskesen}\\
\emph{s133304@student.dtu.dk}
}
\vfill

% Bottom of the page
{\large March 10, 2014}

\end{center}
\end{titlepage}

\section*{Exercise 1.1}
\subsection*{1.1.1}
We are considering the Hill cipher. We have plaintext $\mathcal{P}$, {\tt\bfseries crypto}, which map to values {\tt\bfseries [2,17,24,15,19,14]}. We also have ciphertext $\mathcal{C}$, {\tt\bfseries LSDKDH}, which map to values {\tt\bfseries [11,18,3,10,3,7]}. We have that $\mathcal{P} = \mathcal{C} = \mathbb{Z}_{26}$.
\\
\\
We wish to find the $2 \times 2$ key matrix $K$ used in the encryption. To do this we first split the plaintext and ciphertext into pairs of two, this gives that {\tt\bfseries cr} is encrypted to {\tt\bfseries LS}, {\tt\bfseries yp} to {\tt\bfseries DK} and {\tt\bfseries to} to {\tt\bfseries DH}. Now we must find two pairs we can use to figure out the key. We start off with {\tt\bfseries cr} and {\tt\bfseries yp}.\\
\\
First we wish to check if the plaintext message $m$ is invertible.
$$m = \begin{bmatrix}
       c & r\\
       y & p
	\end{bmatrix} (\mathtt{mod}\;26) = \begin{bmatrix}
       2 & 17\\
       24 & 15
	\end{bmatrix} (\mathtt{mod}\;26)$$
$$m^{-1} =\;?$$

The plaintext message $m$ is not invertible. We skip the plaintext {\tt\bfseries cr} and use the next pair from the plaintext. Now we have {\tt\bfseries yp} and {\tt\bfseries to}.

$$m = \begin{bmatrix}
       	y & p\\
     	t & o
	\end{bmatrix} (\mathtt{mod}\;26) = \begin{bmatrix}
		24 & 15\\
		19 & 14
	\end{bmatrix} (\mathtt{mod}\;26)$$
$$m^{-1} = \begin{bmatrix}
		12 & 15\\
		19 & 2
	\end{bmatrix} (\mathtt{mod}\;26)$$

The message $m$ is invertible. Now onto finding the key. First we define $c$, the ciphertext that $m$ encrypts to.
$$c = \begin{bmatrix}
		3 & 10\\
		3 & 7
	\end{bmatrix} (\mathtt{mod}\;26)$$

Now we can derive the key.
$$c = mK (\mathtt{mod}\;26)$$
$$K = m^{-1}c \;(\mathtt{mod}\;26) = \begin{bmatrix}
		12 & 15\\
		19 & 2
	\end{bmatrix} \begin{bmatrix}
		3 & 10\\
		3 & 7
	\end{bmatrix} (\mathtt{mod}\;26) = \uuline{\begin{bmatrix}
		3 & 17\\
		11 & 22
	\end{bmatrix}}$$

\subsection*{1.1.2}
With the key $K$ obtained, we now wish to decrypt the text {\tt\bfseries HFFP} mapping to values {\tt\bfseries [7,5,5,15]}.
$$m = cK^{-1} = \begin{bmatrix}
		7 & 5\\
		5 & 15
	\end{bmatrix} \begin{bmatrix}
		3 & 17\\
		11 & 22
	\end{bmatrix}^{-1} (\mathtt{mod}\;26) = \begin{bmatrix}
		7 & 5\\
		5 & 15
	\end{bmatrix} \begin{bmatrix}
		14 & 1\\
		19 & 9
	\end{bmatrix} (\mathtt{mod}\;26) = \begin{bmatrix}
		11 & 0\\
		17 & 10
	\end{bmatrix}$$

The values {\tt\bfseries [11,0,17,10]} maps to the text \uuline{{\tt\bfseries lark}}.

\newpage

\section*{Exercise 1.2}
\subsection*{1.2.1}
We are to consider $m \times m$ matrices with entries from $\mathbb{Z}_{26}$. We know that the following holds:
$$\det(AB)=\det(A) \cdot \det(B)$$

We have an invertible $m \times m$ matrix $A$ over $\mathbb{Z}_{26}$, where $A = A^{-1}$. We wish to prove:
$$\det(A) = \pm 1\;(\mathtt{mod}\;26)$$
\\
We know that $A$ is invertible, hence we can claim that $A$ is orthogonal.
$$A^{-1}=A^T$$

We know the definition for an inverse matrix, using identity matrix $I$.
$$A \cdot A^{-1}\;(\mathtt{mod}\;26) = I$$

We can now substitute $A^{-1}$ with $A^T$ and reduce.
$$A \cdot A^T\;(\mathtt{mod}\;26)= I$$
$$\det(A \cdot A^T\;(\mathtt{mod}\;26)) = \det(I) = 1$$
$$\det(A) \cdot \det(A^T)\;(\mathtt{mod}\;26) = 1$$

We know that the determinant of a square matrix and it's transpose are equal.
$$\det(A) \cdot \det(A)\;(\mathtt{mod}\;26) = (\det(A))^2\;(\mathtt{mod}\;26) = 1$$

We can now conclude our proof:
$$\det(A) = \pm 1\;(\mathtt{mod}\;26)$$

\newpage

\subsection*{1.2.2}
We wish to show that there are 736 $2 \times 2$ matrices $A$ over $\mathbb{Z}_{26}$ for which it holds that $A= A^{-1}$. We split the problem into two cases. We first consider the problem modulo 2, then modulo 13 and at last modulo 26.\\
\\
We assume the following syntax for a $2 \times 2$ matrix:
$$\begin{pmatrix}
a & b\\
c & d
\end{pmatrix}$$

\subsubsection*{$\mathbb{Z}_{2}$ (modulo 2)}
A matrix modulo 2 that is the inverse of itself only has 4 types where $b = 0$:
$$\begin{pmatrix}
\pm 1 & 0\\
0 & \pm 1
\end{pmatrix}$$
In total we have 4 self-inverse matrices over $\mathbb{Z}_{2}$.

\subsubsection*{$\mathbb{Z}_{13}$ (modulo 13)}
A matrix modulo 13 that is the inverse of itself has likewise modulo 2 those 4 types as wel where $b = 0$:\\
$$\begin{pmatrix}
\pm 1 & 0\\
0 & \pm 1
\end{pmatrix}$$

If $a = 0$ and $b \neq 0$ then $\frac{1}{ad - bc} = -1$ has a solution where $c=\frac{1}{b}$. Since there are 12 possible values for $b$ where $b \neq 0$ there are 12 more types of this kind:\\
$$\begin{pmatrix}
0 & b\\
\frac{1}{b} & 0
\end{pmatrix}$$

If $bc \neq 0$ then we have $a^2 + bc = 1$. Therefore $1 - bc$ has to be square modulo 13. Hence we have:\\
$$1 - bc \in \{0,1,3,4,9,10,12\}$$
Since $bc \neq 0$ then:
$$bc \in \{1,3,4,9,10,12\}$$
There are 12 invertible elements in $\mathbb{Z}_{13}$. For any choice of invertible $b$, there are 6 options for $bc$, each gives one option for $c$, each, in turn gives 2 options for $a$.\\
$$6 * 12 * 1 * 2 = 144$$
We isolate $c$ in $a^2 + bc = 1$ and get that $c = \frac{1-a^2}{b}$. Therefore we have 144 types of this kind of matrix:\\
$$\begin{pmatrix}
a & b\\
\frac{1-a^2}{b} & -a
\end{pmatrix}$$
Lastly we have 12 each of these types:
$$
\begin{pmatrix}
1 & 0\\
c & -1
\end{pmatrix}
,
\begin{pmatrix}
-1 & 0\\
c & 1
\end{pmatrix}
,
\begin{pmatrix}
1 & b\\
0 & -1
\end{pmatrix}
,
\begin{pmatrix}
-1 & b\\
0 & 1
\end{pmatrix}
$$
But however only the two first types have to be counted, because the last two types have already been counted among the type of size 144. Which means that we have:
$$4 + 12 + 144 + 24 = 184$$

In total we have 184 self-inverse matrices over $\mathbb{Z}_{13}$.

\subsubsection*{$\mathbb{Z}_{26}$ (modulo 26)}
Since $\mathbb{Z}_{26}$ is the direct product of $\mathbb{Z}_{13}$ and $\mathbb{Z}_{2}$ we can multiply the number of self-inverse matrices over these two and get the number of self-inverse matrices over $\mathbb{Z}_{26}$.
$$184 * 4 = 736$$
We have therefore shown that there are 736 $2 \times 2$ matrices $A$ over $\mathbb{Z}_{26}$ for which it holds that $A= A^{-1}$.

\newpage

\section*{Exercise 1.3}
\subsection*{1.3.1}
We consider the Vig\'{e}nere cryptosystem. We know the following ciphertext encrypted using the Vig\'{e}nere cryptosystem.
\begin{center}
\begin{tabular}{| l |}
\hline
{\tt PATGSJKGSPFPCTSSKHOIGSDHNBCUHVIHKSHVBKPBQLEGVFSHPLTQFLYRWS}\\
{\tt RLYBSSRPPPPGIUOTUSHVPTZSVLNBCHCIWMIZSZKPWWZLZKXJWUCMWFCBCA}\\
{\tt ACBKKGDBHOAPPMHVBKPBQLDXKWGPPXSZCUZHCNCVWGSOGRAWIVSTPHROFL}\\
{\tt BHGHVLYNQIBAEEWWGYAMJFBDDBRVVLKIIWAPOMXQOSHRPBHPYBEOHLZPDI}\\
{\tt ZKXXCCZVJZTFHOWGIKCDAXZGCMYHJFGLPATKOYSTHBCAPHTBRZKJJWQRHR}\\
\hline
\end{tabular}
\end{center}

We wish to compute the most likely period of the key by using the index of coincidence. We do this by arranging the ciphertext in ($j \times c$) matrices. Instead of showing all our created matrices when computing index of coincidence. We skip right ahead to the (6 x c) matrix, we found to be the likely period of key. We arrange the ciphertext in this matrix.
\begin{center}
\begin{tabular}{| l |}
\hline
{\tt PKCONIBEPYYPOPNWKZCCKABDPZWAPBYEADKOHYZXJWAYPSPKH}\\
{\tt AGTIBHKGLRBPTTBMPKMAGPKXXHGWHHNEMBIMRBPXZGXHATHJR}\\
{\tt TSSGCKPVTWSPUZCIWXWADPPKSCSIRGQWJRIXPEDCTIZJTHTJ}\\
{\tt GPSSUSBFQSSGSSHZWJFCBMBWZNOVOHIWFVWQBOICFKGFKBBW}\\
{\tt SFKDHHQSFRRIHVCSZWCBHHQGCCGSFVBGBVAOHHZZHCCGOCRQ}\\
{\tt JPHHVVLHLLPUVLIZLUBKOVLPUVRTLLAYDLPSPLKVODMLYAZR}\\
\hline
\end{tabular}
\end{center}

We compute the index of coincidence for each row vector and we get the following six numbers:
\begin{center}
0.065,0.059,0.051,0.061,0.071,0.082.
\end{center}

We have computed the index of coincidence of each row in ($j \times c$) matrix for all (likely) values of $j$, where $j = 1, \dots 7$. This gives us the following:
\begin{center}
\begin{tabular}{| c l |}
\hline
Period & Index of coincidence\\ \hline
1 & 0.0433\\
2 & 0.047 0.044\\
3 & 0.049 0.054 0.049\\
4 & 0.052 0.036 0.044 0.043\\
5 & 0.039 0.038 0.039 0.046 0.042\\
6 & \textbf{0.065 0.059 0.051 0.061 0.071 0.082}\\
7 & 0.033 0.038 0.035 0.041 0.043 0.04 0.041\\
\hline
\end{tabular}
\end{center}

We are therefore able to conclude that the period is most likely six.

\subsection*{1.3.2}
We wish to find the plaintext message of the ciphertext. We arrange the ciphertext in a $6 \times c$ matrix. First we have to find the key therefore for each row we do the following:\\
For $i = 0 \ldots 25$ do
\begin{enumerate}
\item shift all letters $i$ positions backwards in the alphabet
\item compute the distribution of the resulting characters
\item compute the distance of this distribution from the distribution in a typical English text
\end{enumerate}

We define the distance between two distributions as the "sum of squared differences". We therefore use this formula for calculating the distance:

$$d(i) = \sum_{\ell=1}^{25} (p_{\ell} - q_{\ell}(i))^2$$

The equation above is run for $i = 0,\ldots,25$. $p_{\ell}$ for $\ell = 0,\dots,25$ is the probability distribution of letters in English text and $q_{\ell}(i)$ for $\ell = 0,\dots,25$ is the probability distribution of letters in this row when each letter is shifted $i$ positions back in the alphabet.\\
\\
We do the above for each row.

\subsubsection{First row}
\begin{tabular}{| c | c c c c c c c c c c c c c |}
\hline
$i$ & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12\\
$d(i)$ & 0.07 & 0.08 & 0.07 & 0.08 & 0.08 & 0.08 & 0.06 & 0.05 & 0.07 & 0.07 & 0.05 & 0.04 & 0.08\\
\hline
\end{tabular}
\begin{tabular}{| c | c c c c c c c c c c c c c |}
\hline
$i$ & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 20 & 21 & 22 & 23 & 24 & 25\\
$d(i)$ & 0.09 & 0.07 & 0.07 & 0.09 & 0.08 & 0.08 & 0.08 & 0.07 & 0.06 & \textbf{0.02} & 0.06 & 0.07 & 0.08\\
\hline
\end{tabular}
${}$\\
It follows that the computed distances are lowest for $i$ = 22, which corresponds to the key
character "W".

\subsubsection{Second row}
\begin{tabular}{| c | c c c c c c c c c c c c c |}
\hline
$i$ & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12\\
$d(i)$ & 0.06 & 0.07 & 0.07 & 0.06 & 0.06 & 0.07 & 0.06 & 0.07 & 0.05 & 0.07 & 0.08 & 0.08 & 0.07\\
\hline
\end{tabular}
\\[0.05cm]
\begin{tabular}{| c | c c c c c c c c c c c c c |}
\hline
$i$ & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 20 & 21 & 22 & 23 & 24 & 25\\
$d(i)$
& 0.06
& 0.07
& 0.04
& 0.08
& 0.09
& 0.08
& \textbf{0.02}
& 0.07
& 0.08
& 0.07
& 0.06
& 0.07
& 0.07\\
\hline
\end{tabular}
${}$\\
It follows that the computed distances are lowest for $i$ = 19, which corresponds to the key
character "T".

\subsubsection{Third row}
\begin{tabular}{| c | c c c c c c c c c c c c c |}
\hline
$i$ & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12\\
$d(i)$ & 0.06
& 0.05
& 0.05
& 0.07
& 0.04
& 0.05
& 0.06
& 0.07
& 0.06
& 0.07
& 0.07
& 0.04
& 0.07\\
\hline
\end{tabular}
\\[0.05cm]
\begin{tabular}{| c | c c c c c c c c c c c c c |}
\hline
$i$ & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 20 & 21 & 22 & 23 & 24 & 25\\
$d(i)$ & 0.08
& 0.05
& \textbf{0.01}
& 0.06
& 0.06
& 0.05
& 0.07
& 0.08
& 0.06
& 0.05
& 0.07
& 0.06
& 0.06\\
\hline
\end{tabular}
${}$\\
It follows that the computed distances are lowest for $i$ = 15, which corresponds to the key
character "P".

\subsubsection{Fourth row}
\begin{tabular}{| c | c c c c c c c c c c c c c |}
\hline
$i$ & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12\\
$d(i)$ & 0.08
& 0.04
& 0.07
& 0.07
& 0.07
& 0.06
& 0.08
& 0.08
& 0.06
& 0.08
& 0.06
& 0.08
& 0.08 \\
\hline
\end{tabular}
\\[0.05cm]
\begin{tabular}{| c | c c c c c c c c c c c c c |}
\hline
$i$ & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 20 & 21 & 22 & 23 & 24 & 25\\
$d(i)$ & 0.07
& \textbf{0.02}
& 0.08
& 0.08
& 0.08
& 0.05
& 0.08
& 0.07
& 0.08
& 0.08
& 0.06
& 0.07
& 0.06\\
\hline
\end{tabular}
${}$\\
It follows that the computed distances are lowest for $i$ = 14, which corresponds to the key
character "O".

\subsubsection{Fifth row}
\begin{tabular}{| c | c c c c c c c c c c c c c |}
\hline
$i$ & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12\\
$d(i)$ & 0.08
& 0.09
& 0.07
& 0.05
& 0.08
& 0.1
& 0.09
& 0.08
& 0.09
& 0.08
& 0.08
& 0.09
& 0.09\\
\hline
\end{tabular}
\\[0.05cm]
\begin{tabular}{| c | c c c c c c c c c c c c c |}
\hline
$i$ & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 20 & 21 & 22 & 23 & 24 & 25\\
$d(i)$ & 0.07
& \textbf{0.02}
& 0.07
& 0.09
& 0.08
& 0.08
& 0.09
& 0.08
& 0.07
& 0.1
& 0.09
& 0.06
& 0.05\\
\hline
\end{tabular}
${}$\\
It follows that the computed distances are lowest for $i$ = 14, which corresponds to the key
character "O".

\subsubsection{Sixth row}
\begin{tabular}{| c | c c c c c c c c c c c c c |}
\hline
$i$ & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12\\
$d(i)$ & 0.10
& 0.10
& 0.10
& 0.06
& 0.08
& 0.12
& 0.10
& \textbf{0.02}
& 0.08
& 0.10
& 0.10
& 0.07
& 0.11\\
\hline
\end{tabular}
\\[0.05cm]
\begin{tabular}{| c | c c c c c c c c c c c c c |}
\hline
$i$ & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 20 & 21 & 22 & 23 & 24 & 25\\
$d(i)$ & 0.09
& 0.10
& 0.10
& 0.10
& 0.08
& 0.08
& 0.09
& 0.09
& 0.09
& 0.09
& 0.08
& 0.09
& 0.10\\
\hline
\end{tabular}
${}$\\
It follows that the computed distances are lowest for $i$ = 7, which corresponds to the key
character "H".

\subsubsection*{Finding the plaintext message}
We have found the key which is "WTPOOH". We follow the decryption for the Vig\'{e}nere crpytosystem which is:
$$d_k(y_1,\dots,y_n) = (y_1 - k_1,\dots,y_n - k_n)$$

\begin{figure}[H]
\centering
\begin{tabular}{| l |}
\hline
{\tt thesecondbrigadewaspreparingtomovetofrance}\\
{\tt ingreatsecrecyhedecideditwasunsafetotakeher}\\
{\tt intobattlesowhiledrivingthroughlondonontheway}\\
{\tt tofrancehevisitedlondonzooandaskedthemtocare}\\
{\tt forthecubuntilhisreturnwhichheoptimistically}\\
{\tt anticipatedwouldbenolongerthantwoweeksof}\\
{\tt coursethewarwasnottoendsoquickly}\\
\hline
\end{tabular}
\caption{The plaintext message}
\end{figure}

\begin{figure}[H]
\centering
\begin{tabular}{| l |}
\hline
{\tt the second brigade was preparing to move to france}\\
{\tt in great secrecy he decided it was unsafe to take her}\\
{\tt into battle so while driving through london on the way}\\
{\tt to france he visited london zoo and asked them to care}\\
{\tt for the cub until his return which he optimistically}\\
{\tt anticipated would be no longer than two weeks of}\\
{\tt course the war was not to end so quickly}\\
\hline
\end{tabular}
\caption{The plaintext message with space added for readability}
\end{figure}
The figures above describe the plaintext message.

\end{document}

01410 - Cryptology 1/Homework 1/Latex/Homework 1.tex


\documentclass{subfile}

\begin{document}
	\section{The UVW/PQR/ABC Method}\label{sec:uvw}
	The UVW method was popularized by \textcite{rozenberg_2011}. It has also been known as the ABC method or the PQR method although it is unclear what exactly the origin of this method is. Although some people believe it was originated at Vietnam where it was called the ABC (Abstract concreteness) method. Currently, \textcite{knudsen} is the most popular version of this technique but at the core, they all use the same idea. We will try to explain this method as clearly as possible with examples.

	Consider an inequality in three variables $a,b,c\in\mathbb{R}$. If the expression is symmetric on $a,b,c$, then the initial idea of PQR method was to write
		\begin{align*}
			a+b+c
				& = p\\
			ab+bc+ca
				& = q\\
			abc
				& = r
		\end{align*}
	so that $a,b,c$ are the roots of the equation
		\begin{align*}
			x^{3}-px^{2}+qx-r
				& = 0
		\end{align*}
	However, nowadays the most popular way to convert these is to write
		\begin{align*}
			a+b+c
				& = 3u\\
			ab+bc+ca
				& = 3v^{2}\\
			abc
				& = w^{3}
		\end{align*}
	Hence, the name UVW. While working with such transformations, be careful not to assume $v^{2}\geq 0$ by default. We have to consider the case where $v^{2}$ is negative as well. We do not know for sure that $a,b,c$ are all positive unless it is stated specifically. However, the following result is very nice when they are indeed positive. We will use the notations as stated above throughout this section and the names of the following theorems follow \textcite{knudsen}.
		\begin{theorem}[The Idiot Theorem]
			If $a,b,c\geq 0$, then $u\geq v\geq w$.
		\end{theorem}

		\begin{proof}
			Using $a^{2}+b^{2}+c^{2}\geq ab+bc+ca$ and $a+b+c\geq 3\sqrt[3]{abc}$, we immediately see that
				\begin{align*}
					(a+b+c)^{2}
						& \geq 3(ab+bc+ca)
				\end{align*}
			which implies $9u^{2}\geq 9v^{2}$ or $u^{2}\geq v^{2}$ and $u\geq w$. For proving $v\geq w$, we can use arithmetic-harmonic mean inequality
				\begin{align*}
					\dfrac{3}{\dfrac{1}{a}+\dfrac{1}{b}+\dfrac{1}{c}}
						& \leq \sqrt[3]{abc}\\
					\iff \dfrac{3abc}{ab+bc+ca}
						& \leq \sqrt[3]{abc}\\
					\iff ab+bc+ca
						& \geq 3\sqrt[3]{(abc)^{2}}\\
					\iff 3v^{2}
						& \geq 3w^{2}
				\end{align*}
			This proves the theorem.
		\end{proof}

		\begin{theorem}[The UVW theorem]\label{thm:uvw}
			Let $u,v,w$ be numbers such that $u,v^{2},w^{3}$ are real numbers. Then there exists real numbers $a,b,c$ such that
				\begin{align*}
					a+b+c
						& = 3u\\
					ab+bc+ca
						& = 3v^{2}\\
					abc
						& = w^{3}
				\end{align*}
			if and only if $u^{2}\geq v^{2}$ and
				\begin{align*}
					3uv^{2}-2u^{3}-2\sqrt{(u^{2}-v^{2})^{3}}
						& \leq w^{3} \leq 3uv^{2}-2u^{3}+2\sqrt{(u^{2}-v^{2})^{3}}
				\end{align*}
		\end{theorem}
	For proving this theorem, we will need the following result first.
		\begin{theorem}\label{thm:abc}
			$a,b,c$ are real numbers if and only if $(a-b)(b-c)(c-a)$ is a real number.
		\end{theorem}

		\begin{proof}
			The if part is obvious. So we will focus on the only if part. If $(a-b)(b-c)(c-a)$ is not real, then $a,b,c$ are not real either. Clearly, $a,b,c$ are the roots of $x^{3}-3ux^{2}+3vx-w^{3}=0$ by Vieta's formulas. If one of the roots is complex, say $a$, then another of $b,c$ is complex, say $b$. Moreover, we must have $a=z$ and $b=\bar{z}$ for some complex number $z$ since complex roots can appear only in conjugates. Then
				\begin{align*}
					(a-b)(b-c)(c-a)
						& = (z-\bar{z})(\bar{z}-c)(c-z)
				\end{align*}
			Letting $z=u+iv$ for some $u,v\in\mathbb{R}$, we have $\bar{z}=u-iv$ and
				\begin{align*}
					(a-b)(b-c)(c-a)
						& = -2iv(u-c+iv)(u-c-iv)\\
						& = -2iv\left((u-c)^{2}+v^{2}\right)
				\end{align*}
			This is obviously a complex number, so the claim is true unless $v=0$ which cannot hold since $z\not\in\mathbb{R}$.
		\end{proof}

		\begin{proof}[Proof of \nameref{thm:uvw}]
			For any $x\in\mathbb{R}$, we have $x^{2}\geq 0$. By \eqref{thm:abc}, $a,b,c\in\mathbb{R}$ implies that $(a-b)(b-c)(c-a)\in\mathbb{R}$ and so $(a-b)^{2}(b-c)^{2}(c-a)^{2}\in\mathbb{R}$. Then
				\begin{align*}
					(a-b)^{2}(b-c)^{2}(c-a)^{2}
						& \geq 0\\
					\iff 27\left(-(w^{3}-(3uv^{2}-2u^{3}))^{2}+4(u^{2}-v^{2})^{3}\right)
						& \geq 0\\
					\iff 4(u^{2}-v^{2})^{3}
						& \geq (w^{3}-(3uv^{2}-2u^{3}))^{2}\\
					\iff 2\sqrt{(u^{2}-v^{2})^{3}}
						& \geq \left|w^{3}-(3uv^{2}-2u^{3})\right|
				\end{align*}
			This proves the theorem.
		\end{proof}

		\begin{theorem}[The positivity theorem]\label{thm:positivity}
			$a,b,c$ are non-negative real numbers if and only if $u,v^{2},w^{3}$ are non-negative real numbers.
		\end{theorem}

		\begin{proof}
			The if part is obvious, so we prove the only if part. So we should prove that if any of $a,b,c$ are negative, then at lest one of $u,v^{2},w^{3}$ is negative. It is easy to see that $w^{3}=abc$ is negative if one or three of $a,b,c$ are negative. So, we are left with the case where two of $a,b,c$ are negative. Without loss of generality, assume that $a$ and $b$ are negative whereas $c$ is non-negative. Let $a=-x,b=-y$ so that
				\begin{align*}
					a+b+c
						& = 3u\\
					c-x-y
						& = 3u\\
					ab+bc+ca
						& = 3v^{2}\\
						& = xy-c(x+y)
				\end{align*}
			From this we can show that at least one of $u$ or $v$ has to be negative. Otherwise, if both $u$ and $v$ are non-negative, then $c-x-y\geq 0$ and $xy-c(x+y)\geq 0$. But this leads to a contradiction since
				\begin{align*}
					xy
						& \geq c(x+y)\\
						& \geq (x+y)^{2}\\
						& \geq 2xy
				\end{align*}
			by \nameref{thm:amgm}.
		\end{proof}

		\begin{theorem}[Tej's theorem]\label{thm:tej}
			Let $a,b,c$ be non-negative real numbers. Then we have the following:
				\begin{enumerate}
					\item If at least one value of $w^{3}$ exists for fixed $u$ and $v^{2}$ corresponding to $a,b,c$, then $w^{3}$ has a global maximum and minimum (see \gls{gls:globalextreme}). This maximum value is achieved when at least two of $a,b,c$ are equal. The minimum value is achieved when two of $a,b,c$ are equal or when one of them is $0$.
					\item If at least one value of $v^{2}$ exists for fixed $u$ and $w^{3}$ corresponding to $a,b,c$, then $v^{2}$ has a global maximum and minimum. This maximum value is achieved when at least two of $a,b,c$ are equal. The minimum value is achieved when two of $a,b,c$ are equal or when one of them is $0$.
					\item If at least one value of $u$ exists for fixed $v^{2}$ and $w^{3}$ corresponding to $a,b,c$, then $u$ has a global maximum and minimum. This maximum value is achieved when at least two of $a,b,c$ are equal. The minimum value is achieved when two of $a,b,c$ are equal or when one of them is $0$.
				\end{enumerate}
		\end{theorem}

		\begin{theorem}
			Every symmetric inequality of degree at most $5$ has to be proved only for $a_{1}=a_{2}$ or $a_{n}=0$.
		\end{theorem}

		\begin{proof}
			Using \gls{gls:elempoly},
		\end{proof}
\end{document}


\newcommand\mcase[1]{\noindent\textbf{Case }#1\\\noindent}

\subsection{Soundness}

\begin{lemma}
  \label{lemma:constrsubst}
  Given constraints $C$ and $D$ and substitution $\unif$, if $\entail{D}{C}$
  then $\entail{\unif D}{\unif C}$.
  \begin{proof}
    By induction over the entailment judgment.
  \end{proof}
\end{lemma}

\begin{lemma}
  \label{lemma:constrimply}
  Given a typing derivation $\inferS{C}{\E}{e}{\tau}$ and
  a constraint $D \in \mathcal S$ in solved form such that $\entail{D}{C}$, then
  $\inferS{D}{\E}{e}{\tau}$
  \begin{proof}
    By induction over the typing derivation
  \end{proof}
\end{lemma}

\begin{lemma}
  \label{lemma:typ:weakening}
  Given a type environment $\E$, $\E' \subset \E$, a term $e$ and a variable $x\in\E$,\\
  if $\inferS{C}{\E'}{e}{\tau}$
  then $\inferS{C\Cand \Weaken_{x}(\E')}{\E';\bvar{x}{\E(x)}}{e}{\tau}$

  \begin{proof}
    Trivial if $x \in \Sv$. Otherwise, by induction over the typing derivation.
  \end{proof}
\end{lemma}

% We say that a use map $\Sv$ and an environment $\E$ are equivalent,
% written as $\Sv \equivC \E$, if, for
% any kind $k$, $\Cleq{\Sv}{k} \equivC \Cleq{\E|_{dom(\Sv)}}{k}$.

We define the flattening $\Eflat\E$ of an environment $\E$, as the environment
where all the binders are replaced by normal ones. More formally:
\begin{align*}
  \Eflat\E
  =& \left\{ \bvar x \schm \mid \bvar{x}{\schm} \in\E
    \vee \bbvar {x}{k}{\schm}\in\E
    \vee \svar{x}{\schm}^n\in\E
    \right\}\\
  &\cup \left\{ \bvar{\tvar}{k} \mid \bvar{\tvar}{k}\in\E \right\}
\end{align*}

\begin{lemma}
  \label{lemma:env:flat}
  Given a type environment $\E$ and a term $e$ such
  that $\inferW{\Sv}{(C,\unif)}{\E}{e}{\tau}$
  then $\Eflat\Sv \subset \E$.
  \begin{proof}
    By induction over the typing derivation.
  \end{proof}
\end{lemma}

% \begin{lemma}
%   \label{lemma:equivsplit}
%   Given a type environment $\E$ and
%   use maps $\Sv_1$, $\Sv_2$,
%   if $\bsplit{C}{\Sv}{\Sv_1}{\Sv_2}$, then
%   there exists $\E_1$, $\E_2$ such that $\Sv_1 \equivC \E_1$,
%   $\Sv_2 \equivC \E_2$,
%   $\Sv \equivC \E$, $\bsplit{C'}{\E}{\E_1}{\E_2}$ and $C\equivC C'$.
%   \begin{proof}
%     TODO
%   \end{proof}
% \end{lemma}

\begin{theorem}[Soundness of inference]
  Given a type environment $\E$ containing only value bindings,
  $\E|_\tau$ containing only a type binding and a term $e$,\\
  if $\inferW{\Sv}{(C,\unif)}{\E;\E_\tau}{e}{\tau}$
  then $\inferS{C}{\unif(\Sv;\E_\tau)}{e}{\tau}$, $\unif C = C$ and $\unif \tau = \tau$
\begin{proof}
  We proceed by induction over the derivation of $\vdash_w$.
  Most of the cases follow the proofs from \hmx closely.
  For brevity, we showcase three rules: the treatment of binders
  and weakening, where our inference algorithm differ significantly
  from the syntax-directed rule, and the $Pair$ case
  which showcase the treatment of environment splitting.

  \mcase{$\ruleIVar$}

  We have $\Sv = \Sone{x}{\sigma}$.
  Without loss of generality, we can consider $\unif_x = \unif'|_{\fv{\E}} = \unif'|_{\fv{\sigma}}$.
  Since $\Sv\Sdel{x}$ is empty and by definition of normalize, we
  have that
  $\entail C \unif'(C_x) \Cand \Cleq{\unif_x\Sv\Sdel{x}}{\kaff_\infty}$,
  $\unif' \leq \unif$ and $\unif'C = C$.
  By definition, $\unif_x\unif' = \unif'$.
  By rule $Var$, we obtain
  $\inferS{C}{\unif_x(\Sv;\E_\tau)}{x}{\unif_x\unif'\tau}$, which concludes.
  \\

  \mcase{$\ruleIAbs$}

  By induction, we have
  $\inferS{C'}{\Sv_x;\E_\tau}{e}{\tau}$, $\unif C = C$
  and $\unif \tau = \tau$.\\
  By definition of normalize and \cref{lemma:constrsubst}, we have
  $\entail{C}{C'\Cand\Cleq{\unif'\Sv}{\unif'\kvar} \Cand \Weaken_{x}(\unif'\Sv_x)}$ and
  $\unif \leq \unif'$.
  By \cref{lemma:constrsubst}, we have $\entail{C}{\Cleq{\Sv}{\unif\kvar}}$.

  We now consider two cases:
  \begin{enumerate}[leftmargin=*,noitemsep,topsep=5pt]
  \item If $x\in\Sv_x$, then $\Weaken_{x}(\unif\Sv_x) = \Ctrue$
    and by \cref{lemma:env:flat}, $\Sv_x = \Sv;\bvar{x}{\alpha}$.
    We can deduce\\
    $\inferS{C'\Cand \Weaken_{\bvar{x}{\tvar}}(\unif\Sv_x)}{\unif\Sv;\E_\tau;\bvar{x}{\unif(\tvar)}}{e}{\tau}$.
  \item If $x\notin\Sv_x$, then $\Sv = \Sv_x$ and
    $\Weaken_{\bvar{x}{\tvar}}(\unif\Sv_x) = \Cleq{\unif\tvar}{\kaff_\infty}$.
    By \cref{lemma:typ:weakening},
    we have\\
    $\inferS{C'\Cand \Weaken_{\bvar{x}{\tvar}}(\unif\Sv_x)}{\unif\Sv;\E_\tau;\bvar{x}{\unif(\tvar)}}{e}{\tau}$
  \end{enumerate}
  % By \cref{lemma:env:flat}, we know that if $x\in\Sv_x$, then
  % By \cref{lemma:typ:weakening,}, we deduce
  % that
  % $\inferS{C'\Cand \Weaken_{x}(\unif\Sv_x)}{\unif\Sv;\bvar{x}{\unif(\tvar)}}{e}{\tau}$.
  By \cref{lemma:constrimply}, we
  have $\inferS{C}{\unif(\Sv;\E_\tau);\bvar{x}{\unif(\tvar)}}{e}{\tau}$.\\
  By rule $Abs$, we obtain
  $\inferS{C}{\unif(\Sv;\E_\tau)}{\lam{x}{e}}{\unif(\tvar)\tarr{\unif(\kvar)}\tau}$ which concludes.
  \\

  % $\ruleSDLam$

  % \mcase{$\ruleIApp$}

  % By induction, we have
  % $\inferS{C_1}{\unif_1\Sv_1}{e_1}{\tau_1}$, $\unif_1 C_1 = C_1$,
  % and $\unif_1 \tau_1 = \tau_1$
  % and
  % $\inferS{C_2}{\unif_2\Sv_2}{e_2}{\tau_2}$, $\unif_2 C_2 = C_2$
  % and $\unif_2 \tau_2 = \tau_2$.
  % %
  % By normalization, $\entail{C}{D}$, $\unif \leq \unif'$ and $\unif C = C$.
  % By \cref{lemma:constrimply} and by substitution, we have
  % $\inferS{C}{\unif\Sv_1}{e_1}{\unif\tau_1}$ and
  % $\inferS{C}{\unif\Sv_2}{e_2}{\unif\tau_2}$.
  % We directly have that
  % $\bsplit{\unif C_s}{\unif \Sv}{\Sv_1}{\unif\Sv_2}$
  % and by \cref{lemma:constrimply}, $\entail{\unif C}{\unif C_s}$.
  % %
  % Finally, since the constraint $\Cleq{\tau_1}{\tau_2\tarr{\kvar}\tvar}$
  % has been solved by normalization,
  % we have $\unif'\tau_1 = \unif\tau_2\tarr{k}\unif\tvar$ for some kind $k$.
  % By rule $App$, we obtain
  % $\inferS{C}
  % {\unif\Sv}{\app{e_1}{e_2}}{\unif(\tvar)}$,
  % which concludes.
  % \\

  % \mcase{$\ruleILet$}

  % By induction, we have
  % $\inferS{C_1}{\unif_1\Sv_1}{e_1}{\tau_1}$, $\unif_1 C_1 = C_1$,
  % and $\unif_1 \tau_1 = \tau_1$
  % and
  % $\inferS{C_2}{\unif_2\Sv_2}{e_2}{\tau_2}$, $\unif_2 C_2 = C_2$
  % and $\unif_2 \tau_2 = \tau_2$.
  % %
  % By definition of $\text{gen}$, $\entail{C_\schm}{C_1}$.
  % %
  % By normalization, $\entail{C}{D}$, $\unif \leq \unif'$ and $\unif C = C$.
  % By \cref{lemma:constrimply} and by substitution, we have
  % $\inferS{C}{\unif\Sv_1}{e_1}{\unif\tau_1}$ and
  % $\inferS{C}{\unif\Sv_2}{e_2}{\unif\tau_2}$.
  % %

  % \TODO{}

  % $
  % \inferrule[Let]
  % { \inferS{C \Cand D}{\E_1}{e_1}{\tau_1} \\
  %   (C_\schm,\schm) = \generalize{D}{\E}{\tau_1}\\
  %   \entail{C}{C_\schm} \\
  %   \inferS{C}{\E;\bvar{x}{\sigma}}{e_2}{\tau_2} \\
  %   \addlin{\lsplit{C}{\E}{\E_1}{\E_2}}\\
  % }
  % { \inferS{C}
  %   {\E}{\letin{x}{e_1}{e_2}}{\tau_2} }$

  \mcase{$\ruleIPair$}

  By induction, we have
  $\inferS{C_1}{\unif_1(\Sv_1;\E^1_\tau)}{e_1}{\tau_1}$, $\unif_1 C_1 = C_1$,
  and $\unif_1 \tau_1 = \tau_1$
  and
  $\inferS{C_2}{\unif_2(\Sv_2;\E^2_\tau)}{e_2}{\tau_2}$, $\unif_2 C_2 = C_2$
  and $\unif_2 \tau_2 = \tau_2$.
  %
  Wlog, we can rename the type $\E^1_\tau$ and $\E^2_\tau$ to be disjoint
  and define $\E_\tau = \E^1_\tau \cup \E^2_\tau$.
  By normalization, $\entail{C}{D}$, $\unif \leq \unif'$ and $\unif C = C$.
  By \cref{lemma:constrimply} and by substitution, we have
  $\inferS{C}{\unif\Sv_1}{e_1}{\unif\tau_1}$ and
  $\inferS{C}{\unif\Sv_2}{e_2}{\unif\tau_2}$.
  We directly have that
  $\bsplit{\unif C_s}{\unif \Sv}{\Sv_1}{\unif\Sv_2}$
  and by \cref{lemma:constrimply}, $\entail{\unif C}{\unif C_s}$.
  %
  By rule $Pair$, we obtain
  $\inferS{C}
  {\unif(\Sv;\E_\tau)}{\app{e_1}{e_2}}{\unif(\tyPair{\tvar_1}{\tvar_2}))}$,
  which concludes.
  \\

  % \mcase{$\ruleIMatch$}

  % \mcase{$\ruleIBorrow$}

  % We trivially have
  % $\bvar{\borrow x}{\borrowty{\unif(k)}{\unif(\tau)}} \in \unif\Sv$.
  % By induction, we have $\unif C = C$ and $\unif(\tau) = \tau$.
  % Since $\kvar$ is new, $\unif(\kvar) = \kvar$.
  % By rule $Borrow$, we obtain
  % $\inferS{C}{\unif\Sv}{\borrow{x}}{\borrowty{\kvar}{\tau}}$,
  % which concludes.
  % \\

  % \mcase{$\ruleIRegion$}

\end{proof}
\end{theorem}

\subsection{Completeness}

We now state our algorithm is complete: for any given
syntax-directed typing derivation, our inference algorithm can find
a derivation that gives a type at least as general.
For this, we need first to provide a few additional definitions.

\begin{definition}[More general unifier]
  Given a set of variable $U$ and $\unif$, $\unif'$ and $\phi$
  substitutions. \\
  Then
  $\unif \leq^{\phi}_{U} \unif'$ iff $(\phi \circ \unif)|_{U} = \unif'|_U$.
\end{definition}

\begin{definition}[Instance relation]
  Given a constraints $C$ and two schemes
  $\schm = \forall \Multi\tvar. \qual{D}{\tau}$ and
  $\schm' = \forall \Multi\tvar'. \qual{D'}{\tau'} $.
  Then $\entail{C}{\schm \preceq \schm'}$
  iff $\entail{C}{\subst{\tvar}{\tau''} D}$
  and $\entail{C\Cand D'}{\Cleq{\subst{\tvar}{\tau''}\tau}{\tau'}}$
\end{definition}

We also extend the instance relation to environments $\E$.

We now describe the interactions between splitting and the
various other operations.

\begin{lemma}
  \label{split:flat}
  Given $\bsplit{C}{\E}{\E_1}{\E_2}$, Then $\Eflat\E = \Eflat\E_1 \cup \Eflat\E_2$.
  \begin{proof}
    By induction over the splitting derivation.
  \end{proof}
\end{lemma}

\begin{lemma}
  \label{split:gen}
  Given $\lsplit{C}{\E_1}{\E_2}{\E_3}$,
  $\bsplit{C'}{\E'_1}{\E'_2}{\E'_3}$
  and $\unif$ such that
  $\E'_i\subset\E''_i$ and $\entail{}{\unif\E''_i \preceq \E_i}$
  for $i\in\{1;2;3\}$.

  Then $\entail{C}{\unif C'}$.
\begin{proof}
  By induction over the derivation of $\bsplit{C'}{\E'_1}{\E'_2}{\E'_3}$.
\end{proof}
\end{lemma}

We can arbitrarily extend the initial typing environment in an inference
derivation, since
it is not used to check linearity.

\begin{lemma}
  \label{infer:extend}
  Given $\inferW{\Sv}{(C,\unif)}{\Eflat\E}{e}{\tau}$ and $\E'$ such
  that $\E \subseteq \E'$, then $\inferW{\Sv}{(C,\unif)}{\Eflat\E'}{e}{\tau}$
  \begin{proof}
    By induction over the type inference derivation.
  \end{proof}
\end{lemma}

Finally, we present the completeness theorem.

\begin{theorem}[Completeness]
  Given $\inferS{C'}{\E'}{e}{\tau'}$ and
  $\entail{}{\unif'\E \preceq \E'}$.
  Then $$\inferW{\Sv}{(C,\unif)}{\Eflat\E}{e}{\tau}$$
  for some environment $\Sv$,
  substitution $\unif$, constraint $C$ and type $\tau$ such
  that
  \begin{align*}
    \unif &\leq^{\phi}_{\fv{\E}} \unif'
    &\entail{C'&}{\phi C}
    &\entail{&}{\phi \schm \preceq \schm'}
    &\Sv&\subset\E
    % &( C, \schm, \unif) &\leq (C',\schm',\unif')
  \end{align*}
  where $\schm' = \generalize{C'}{\E'}{\tau'}$
  and $\schm = \generalize{C}{\E}{\tau}$
\end{theorem}
\begin{proof}
  Most of the difficulty of this proof comes from proper handling of
  instanciation and generalization for type-schemes.
  This part is already proven
  by \citet{sulzmann1997proofs} in the context
  of \hmx. As before, we will only present few cases
  which highlights the handling of bindings and environments.
  For clarity, we will only present the part of the proof that
  only directly relate to the new aspect introduced by \lang.
  %
  \\

  % \mcase{$\ruleSDVar$}

  % $\ruleIVar$

  \mcase{$
    \inferrule[Abs]
    {
      \inferS{C'}
      {\E'_x;\bvar{x}{\tau'_2}}{e}{\tau'_1} \\
      \addlin{\entail{C}{\Cleq{\E'_x}{k}}}
    }
    { \inferS{C'}{\E'_x}
      {\lam[k]{x}{e}}{\tau'_2\tarr{k}\tau'_1} }
    $ and $\entail{}{\unif'\E \preceq \E'}$.}

  Let us pick $\tvar$ and $\kvar$ fresh.
  Wlog, we choose $\unif'(\tvar) = \tau_2$ and $\unif'(\kvar) = k$
  so that $\entail{}{\unif'\E_x \preceq \E'_x}$.
  By induction:
  \begin{align*}
    \inferW{\Sv_x}{(C,\unif)}{\Eflat\E_x;\bvar{x}{\tvar}&}{e}{\tau}
    &\unif &\leq^{\phi}_{\fv{\E_x}\cup\{\tvar;\kvar\}} \unif'
  \end{align*}
  \begin{align*}
    \entail{C'&}{\phi C}
    &\entail{&}{\phi \schm \preceq \schm'}
    &\Sv_x&\subset\E_x;\bvar{x}{\tvar}
  \end{align*}
  \begin{align*}
    \schm' &= \generalize{C'}{\E_x';\bvar{x}{\tau'_2}}{\tau'_1}
    &\schm &= \generalize{C}{\E_x;\bvar{x}{\tvar}}{\tau_1}
  \end{align*}

  Let $C_a = C\Cand \Cleq{\Sv}{\kvar} \Cand \Weaken_{\bvar{x}{\tvar}}(\Sv_x)$
  and
  By definition, $\unif_D\Sdel{\alpha;\kvar} \leq^{\phi_D}_{\fv{\E_x}} \unif$
  which means we have
  $\unif_D\Sdel{\alpha;\kvar} \leq^{\phi\circ\phi_D}_{\fv{\E_x}} \unif'$.
  We also have that $\Sv_x\Sdel{x}\subset \E_x$.

  Since $\entail{C}{\Cleq{\E'_x}{k}}$, we have $\entail{C}{\unif'\Cleq{\Sv}{\kvar}}$.
  If $x\in\Sv_x$, then $\Weaken_{\bvar{x}{\tvar}}(\Sv_x) = \Ctrue$.
  Otherwise we can show by induction
  that $\entail{C'}{\unif'\Weaken_{\bvar{x}{\tvar}}(\Sv_x)}$.
  We also have $\unif C = C$, which gives us $\entail{C'}{\unif'(C_a)}$.
  We can deduce
  $\entail{C'}{\unif'(C_a)}$.\\
  This means $(C',\unif')$ is a normal form of $C_a$, so a principal normal form
  exists. Let $(D,\unif_D) = \normalize{C_a}{\unif\Sdel{\alpha;\kvar}}$.
  By the property of principal normal forms,
  we have $\entail{C'}{\rho D}$ and
  $\unif_D \leq^{\rho}_{\fv{\E_x}} \unif'$.

  By application of {\sc Abs$_I$}, we have
  $\inferW{\Sv_x\Sdel{x}}{(C,\unif_D\Sdel{\tvar,\kvar})}{\Eflat\E_x}
  {\lam{x}{e}}{\unif_D(\tvar)\tarr{\unif_D(\kvar)}\tau_1}$.\\
  The rest of the proof proceeds as in the original \hmx proof.
  \\

  % $\ruleIAbs$

  \mcase{$
    \inferrule[Pair]
    {
      \inferS{C'}{\E'_1}{e_1}{\tau'_1} \\
      \inferS{C'}{\E'_2}{e_2}{\tau'_1} \\
      \lsplit{C}{\E'}{\E'_1}{\E'_2}
    }
    { \inferS{C'}
      {\E'}{\app{e_1}{e_2}}{\tyPair{\tau'_1}{\tau'_2}} }
    $\\
    and $\entail{}{\unif'\E \preceq \E'}$}

  The only new elements compared to \hmx is
  the environment splitting.
  By induction:
  \begin{align*}
    \inferW{\Sv_1}{(C_1,\unif_1)}{\Eflat\E_1&}{e}{\tau_1}
    &\unif_1 &\leq^{\phi_2}_{\fv{\E}} \unif'_1
    &\entail{C'&}{\phi C_1}
    &\entail{&}{\phi_2 \schm_1 \preceq \schm'_1}
  \end{align*}
  \begin{align*}
    \Sv_1&\subset\E_1
    &\schm'_1 &= \generalize{C'}{\E'_1}{\tau'_1}
    &\schm_1 &= \generalize{C}{\E_1}{\tau_1}
  \end{align*}
  and
  \begin{align*}
    \inferW{\Sv_2}{(C_2,\unif_2)}{\Eflat\E_2&}{e}{\tau_2}
    &\unif_2 &\leq^{\phi_2}_{\fv{\E}} \unif'_2
    &\entail{C'&}{\phi C_2}
  \end{align*}
  \begin{align*}
    \entail{&}{\phi_2 \schm_2 \preceq \schm'_2}
    &\Sv_2&\subset\E_2
  \end{align*}
  \begin{align*}
    \schm'_2 &= \generalize{C'}{\E'_2}{\tau'_2}
    &\schm_2 &= \generalize{C}{\E_2}{\tau_2}
  \end{align*}

  By \cref{split:flat,infer:extend}, we have
  \begin{align*}
    \inferW{\Sv_1}{(C_1,\unif_1)}{\Eflat\E&}{e}{\tau_1}
    &\inferW{\Sv_2}{(C_2,\unif_2)}{\Eflat\E&}{e}{\tau_2}
  \end{align*}

  Let $\bsplit{C_s}{\Sv}{\Sv_1}{\Sv_2}$.
  We know that $\entail{}{\unif'\E \preceq \E'}$,
  $\entail{}{\unif'_i\E_i \preceq \E'_i}$ and $\Sv_i\subset\E_i$.
  By \cref{split:gen},
  we have $\entail{C}{\unif' C_s}$.
  The rest of the proof follows \hmx.

\end{proof}

\begin{corollary}[Principality]
  Let $\inferS{\Ctrue}{\E}{e}{\schm}$ a closed typing judgment.\\
  Then $\inferW{\Sv}{(C,\unif)}{\Eflat\E}{e}{\tau}$
  such that:
  \begin{align*}
    (\Ctrue,\schm_o) &= \generalize{C}{\unif\E}{\tau}
    &\entail{&}{\schm_o \preceq \schm}
    % &( C, \schm, \unif) &\leq (C',\schm',\unif')
  \end{align*}

\end{corollary}

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