\documentclass{homework}
\course{Math 5522H}
\author{Jim Fowler}
\input{preamble}

\begin{document}
\maketitle

\begin{inspiration}
Nature laughs at the difficulties of integration.
\byline{Pierre-Simon Laplace} % what's the original source for this quotation?
\end{inspiration}

\section{Terminology}

\begin{problem}
  What does it mean to say that $\Omega \subset \C$ is connected?  Is path-connected?
  \end{problem}
  \begin{solution}
  $\Omega\subset \C$ is said to be connected if, for any two disjoint open sets $U,V\in \C$ containing $\Omega$ in their union, either $U\cap \Omega$ or $V\cap \Omega$ is empty.
  \end{solution}
  \begin{problem}
    What is a piecewise-smooth \textbf{curve}?  When are two curves ``the same''?
    \end{problem}
    \begin{solution}
    A smooth curve is a smooth function $f$ from an interval $[t_0, t_1]: t_0, t_1\in\R$ to the complex numbers $\gamma(t)\in \C$ such that $\gamma'(t)\neq 0$ at any point $t$.A piecewise smooth curve is the function defined on the interval $[t_0, t_n]$ by $n$ smooth curves $$\gamma_i:[t_{i-1}, t_i]\mapsto \C$$ satisfying $\gamma_{i-1}(t_{i}) = \gamma_{i}(t_{i})$ for $i\in [1, n]$.

    Two curves $\gamma_a:[a_0, a_1] \mapsto \C, \gamma_b:[b_0, b_1]\mapsto \C$ are said to be the same if there is a differentiable function $f: [a_0, a_1] \mapsto [b_0, b_1]$ such that $\forall t\in [a_0, a_1], \gamma_a(t) = \gamma_b(f(t))$.
    \end{solution}
    \begin{problem}
        Define $\int_\gamma f(z) \, dz$ and define $\int_\gamma f(z) \, dx$ and define $\int_\gamma f(z) \, dy$.
        \end{problem}
        \begin{solution}
        First, some notation for $\gamma$. Define $\gamma:[a,b]\to \{x + iy:x, y \in \R\}$. Then
        \begin{align*}
        \int_\gamma f(z) \, dz &:= \int_{a}^b \gamma'(t)f(\gamma(t)) \, dt\quad \color{purple}\text{ complex derivative }\\
        \int_\gamma f(z) \, dx &:= \int_{a}^b \gamma_x(t)f(\gamma(t)) \, dt\quad\color{purple}\text{ first coordinate derivative }\\
        \int_\gamma f(z) \, dy &:= i\int_{a}^b \gamma_y(t)f(\gamma(t)) \,dt\quad \color{purple}\text{ second coordinate derivative }
        \end{align*}
        \end{solution}
        \begin{problem}
          Define $\int_\gamma f(z) \, d\conj{z}$.
          \end{problem}
          \begin{solution}
          \begin{align*}
          \int_\gamma f(z) \, \conj{dz} &:= \conj{\int_{a}^b \gamma'(t)\conj{f(\gamma(t))} \, dt}\\
          \end{align*}

          \end{solution}
          \begin{problem}
            Define $\int_\gamma f(z) \, \abs{dz}$.
            \end{problem}
            \begin{solution}
            \begin{align*}
            \int_\gamma f(z) \, \abs{dz} &:= \int_{a}^b \abs{\gamma'(t)}f(\gamma(t)) \,dt \\
            \end{align*}
            \end{solution}

            \begin{problem}
              What does it mean to say that a $1$-form is \textbf{exact}?
              \end{problem}
              \begin{solution}
              $f(z)\,dz$ is \textbf{exact} if there it has a \textbf{primitive} - a complex differentiable function $F(z)$ such that $\frac{d}{dz} F(z) = f(z)$.
              \end{solution}
              \begin{problem}
                What are the \textbf{poles} and \textbf{zeros} of a rational
                  function $p(z)/q(z)$?
                  \end{problem}
                  \begin{solution}
                  Assuming that $p(z)$ and $q(z)$ share no zeros as part of the definition of a rational function:
                  The \textbf{poles} of $p(z)/q(z)$ are the points at which $q(z)=0$. The $\textbf{zeros}$ are the points at which $p(z)=0$.
                  \end{solution}

                  \section{Numericals}

                  \begin{problem}
                  Consider a piecewise smooth curve $\gamma$ tracing the boundary of the square
                    $$S = \{ z = x+iy\in \C : \abs{x} \mbox{ and } \abs{y} \leq 1 \}.$$
                      Compute $\displaystyle\int_\gamma \frac{1}{z} \, dz$ by hand.
                      \end{problem}
                      \begin{solution}
                      Going counter-clockwise, we recognize $\gamma = \gamma_1 + \gamma_2 + \gamma_3 + \gamma_4$ with $\gamma_i$ defined on the interval $[-1, 1]$ satisfying
                      \begin{align*}
                      \gamma_1 = 1 + it &\qquad \gamma_3 = -1 - it \\
                      \gamma_2 = -t + i  &\qquad \gamma_4 = t - i
                      \end{align*}
                      Now we can compute the integral:
                      \begin{align*}
                      \int_{\gamma} \frac{1}{z} \, dz
                      &= \int_{-1}^1 \frac{\gamma_1'}{1 + it} \, dt +
                      \int_{-1}^1 \frac{\gamma_2'}{-t + i} \, dt +
                      \int_{-1}^1 \frac{\gamma_3'}{-1 - it} \, dt +
                      \int_{-1}^1 \frac{\gamma_4'}{t - i} \, dt\\
                      &= \int_{-1}^1 \frac{i}{1 + it} \, dt +
                      \int_{-1}^1 \frac{-1}{-t + i} \, dt +
                      \int_{-1}^1 \frac{-i}{-1 - it} \, dt +
                      \int_{-1}^1 \frac{1}{t - i} \, dt\\
                      &= \int_{-1}^1 \frac{4}{t - i} \, dt\\
                      &= 4\Log(t - i)|_{t=-1}^1\color{purple}\text{ Log is analytic in an open set containing [-1, 1]} \\
                      &= 4(\Log(1 - i)  - \Log(-1 - i))\\
                      &= 4((\sqrt{2} + i7\pi/4) - (\sqrt{2} + i5\pi/4))\\
                      &= 4(i\pi/2) = 2\pi i
                      \end{align*}

                      \end{solution}
                      \begin{problem}\label{integral-powers-of-z}Consider the curve $\gamma : [0,2\pi] \to \C$ given by $\gamma(\theta) = e^{i\theta}$.  For an integer $n \in \Z$, compute $\displaystyle\int_\gamma z^n \, dz$ and $\displaystyle\int_\gamma \conj{z}^n \, dz$.
                      \end{problem}
                      \begin{solution}
                      \begin{align*}
                      \int_\gamma z^ndz &= \int_{0}^{2\pi} \gamma'(\theta) e^{i\theta n} d\theta\\
                      &= i\int_{0}^{2\pi}e^{i(1+n)\theta} d\theta\\
                      &= \begin{cases}i\int_{0}^{2\pi} e^{i(1+n)\theta} d\theta & n \neq -1\\
                      i\int_{0}^{2\pi} 1 d\theta & n = -1 \end{cases}\\
                      &= \begin{cases} 0 & n \neq -1\\
                      2\pi i & n = -1 \end{cases}
                      \end{align*}
                      For the second integral,
                      \begin{align*}
                      \int_\gamma \conj{z}^ndz &= \int_{0}^{2\pi} \gamma'(\theta) e^{-i\theta n} d\theta\\
                      &= i\int_{0}^{2\pi}e^{i(1-n)\theta} d\theta\\
                      &= \begin{cases}i\int_{0}^{2\pi} e^{i(1-n)\theta} d\theta & n \neq 1\\
                      i\int_{0}^{2\pi} 1 d\theta & n = 1 \end{cases}\\
                      &= \begin{cases} 0 & n \neq 1\\
                      2\pi i & n = 1 \end{cases}
                      \end{align*}
                      \end{solution}

                      \begin{problem}\label{one-over-z-around-circle}Let $\gamma:[a, b]\mapsto \C$ be a (positively oriented) parametrization of a circle
                        in the plane, and suppose the image of $\gamma$ does not include the
                          origin.  Compute $\displaystyle\int_\gamma \frac{1}{z} \, dz$.
                          \end{problem}
                          \begin{solution}
                          Since $\gamma$ is a positively oriented parameterization of a circle, we can find a differentiable map $f:[a,b]\mapsto [0, 2\pi]$ such that $r(e^{if(t)} + z)= \gamma(t)$, where $r\in\R$ is the radius of and $rz$ is the center of the circle parameterized by $\gamma$.
                          \begin{align*}
                          \int_\gamma \frac{1}{z} \, dz &= \int_a^{b} \frac{\frac{d}{dt}\gamma(t)}{\gamma(t)}dt\\
                          &= \int_a^{b} \frac{\frac{d}{dt}(re^{if(t)}+ rz)}{re^{if(t)} + rz}dt\\
                          &= \int_a^{b} \frac{if'(t)e^{if(t)}}{e^{if(t)} + z}dt\\
                          &= \int_0^{2\pi} \frac{ie^{iu}}{e^{iu} +  z}du \quad \color{purple} u=f(t), du = f'(t) dt\\
                          &= \log(e^{ui} + z)\big|_{u=0}^{2\pi}
                          \end{align*}
                          Though we may be a bit suspcious about which log we are using. However, since the interior of $\gamma$ does not contain the origin, we can choose a branch of the log such that the angle which has a discontinuity is disjoint from the set of $\theta$ that satisfy $re^{i\theta}= e^{ui} +z$ for some $r, u$. Then the value of the above integral is 0 since $e^0 = e^{2\pi i}$.

                          On the other hand, if the image does include the origin, we can imagine that the argument of the parameter passed to log will continuously increase by $2\pi$, so if we switch branches of the log at the place that the derivative is continuous, the difference between the start and end point will be exactly $2\pi i$.

                          \end{solution}
                          \begin{problem}\label{lacunary-series}What is radius $R$ of convergence of
                            $\displaystyle\sum_{n=1} x^{(n!)}$?  (This is a \textbf{lacunary
                                series} with large gaps between nonzero terms.)
                                \end{problem}
                                \begin{solution}
                                We use the following fact: the radius of convergence of a power series $R$ satisfies
                                \[\frac{1}{R} = \limsup_{k\to \infty} \sqrt[k]{|x_k|}\]
                                So we need to compute
                                \[\limsup_{n\to \infty} \sqrt[n!]{1} = 1\]
                                Therefore, the radius of convergence is 1.
                                \end{solution}

                                \section{Exploration}

                                \begin{problem}For the series in \ref{lacunary-series}, find a dense
                                  subset of the circle $\{ z \in \C : \abs{z} = 1 \}$ where the series
                                    diverges.
                                    \end{problem}
                                    \begin{solution}
                                    Let $z=e^{2i\pi \theta}$ with $\theta$ some rational number $n/m$, this is a dense subset of the circle. Note that for $x>=m$,
                                    \[z^x = (e^{2i\pi n/m})^{x!} = z^x = e^{(2i\pi n)*(x!/m)} = 1\],
                                    so we are taking a sum of a 1 infinitely many times, which does not seem like a good sign for convergence.
                                    \end{solution}

                                    \begin{problem}
                                      For which $z \in \C$ with $|z|=1$ does the series
                                        $\displaystyle\sum_{n=0}^{\infty} \frac{z^n}{n}$ converge?  Diverge?
                                        \end{problem}
                                        \begin{solution}
                                        If $z=1$ then it's the harmonic series so it diverges. Otherwise it goes in a big spiral that obviously converges. But how to prove... Let's check by letting $z=e^{i\theta}, \theta\neq 0$ and using the integral test (checking if the integral from some lower bound to infinity is finite). It suffices to show that the real and imaginary part converge. For the imaginary part:
                                        \begin{align*}
                                        \int_{\pi/\theta}^\infty \frac{\sin(x\theta)}{x} dx &=
                                        \sum_{k=1}^\infty (\int_{(2k + 1)\pi /\theta}^{(2k+2)\pi/\theta} \frac{\sin(x\theta)}{x} dx +
                                        \int_{2k\pi/\theta}^{(2k + 1)\pi/\theta} \frac{\sin(x\theta)}{x} dx)
                                        \end{align*}
                                        Setting $u=x\theta, du = \theta dx$:
                                        \begin{align*}
                                        \int_{\pi/\theta}^\infty \frac{\sin(x\theta)}{x} dx &= \sum_{k=1}^\infty (\int_{(2k + 1)\pi}^{(2k+2)\pi} \frac{\sin(u)}{u} du +
                                        \int_{2k\pi}^{(2k + 1)\pi} \frac{\sin(u)}{u} du)\\
                                        &= \sum_{k=1}^\infty \int_{(2k + 1)\pi}^{(2k+2)\pi} \sin(u)(\frac{1}{u} - \frac{1}{u-\pi}) du  \color{purple} \text{ note it's negative}\\
                                        &\geq -\sum_{k=1}^\infty \int_{(2k + 1)\pi}^{(2k+2)\pi} (\frac{1}{(2k+2)\pi} - \frac{1}{2k\pi}) du \\
                                        &= \sum_{k=1}^\infty \frac{1}{(2k+2)} - \frac{1}{2k} du = -\frac{1}{2}
                                        \end{align*}
                                        This integral is finite so the imaginary part of the series converges. An analogous argument with cosine shows that the real part of the series converges, so the series must converge in the complex plane.
                                        \end{solution}

                                        \begin{problem}
                                        Suppose $f : \C \to \C$ is a rational function which
                                          sends the unit circle to the real line, i.e., for $z \in \C$ with
                                            $|z| = 1$ we have $f(z) \in \R$.  Inspired
                                              by \ref{schwarz-reflection-principle}, compute
                                                $\overline{f(1/\conj{z})}$ and then discuss the relationship between
                                                  the poles and zeros of $f$.
                                                  \end{problem}
                                                  \begin{solution}
                                                  If $|z|=1$, then $f(z)=\conj{f(z)}$ and $1/\conj{z} = z$. Thus
                                                  $\conj{f(1/\conj{z})} = f(z)$ on the unit circle. Since these are rational functions that agree on an infinite set of points, they must be equal everywhere.

                                                  Thus for any zero or pole $z_0$ of $f$, there is another zero or pole respectively at $\frac{1}{\conj{z_0}}= \frac{z_0}{|z_0|}.$
                                                  \end{solution}
                                                  \begin{problem}
                                                  In lecture, we briefly saw an example (the
                                                    \textbf{topologist's sine curve}) of a subset of $\mathbb{C}$ which
                                                      is connected but not path-connected.  For open subsets of
                                                        $\mathbb{C}$, what is the relationship between connectedness and
                                                          path-connectedness?
                                                          \end{problem}
                                                          \begin{solution}
                                                          If a open subset of $\C$ is path-connected, it's also connected: For contradiction, suppose there was a disconnection formed from disjoint open sets $U$, $V$. Choosing points $P_U \in U, P_V \in V$, there is a path $f:[0, 1]\mapsto \C$ from $P_U$ to $P_V$. Let $s = \sup x\in [0,1]: f(x) \in U$. s is not 1 since $f$ is continuous and there is a ball around $f(1)$ contained in $V$. Hence by the defintion of sup, we can find an infinite sequence $u_i \in [0, 1]$ with $f(u_i) \in U$ that converges to $s$ and any sequence of points $v_i \in [0, 1]$ with $v_i < s$ converging to $s$ has $f(v_i) \in V$, so $f(s)$ is a boundary point of both $U$ and $V$ contained in either $U$ or $V$, a contradiction.

                                                          Next we show that if a open subset of $S\in \C$ is connected, it's also path-connected. For each point $p \in S$, define $P$ to be the set of points that we can reach from $p$. $P$ is open since if we can reach any point by a path, we can also reach any point from $p$, then we can also reach any point in the (convex) open ball surrounding that point.

                                                          Now consider the complement $S/P$. This set is also open - supposing that we can't reach a point $s\in S/P$, then we can't reach any point in the open ball surrounding that point. So $P=(S/P)\cup S$ is a union of two disjoint open sets. As $P$ is connected, this implies that one of the $S/P$ and $S$ is empty, and as $s\in S$, this implies $S/P = \empty$ and $S=P$. Recalling the definition of $P$, we can reach any point in $S$ by a path starting at $p$, so we can reach any point from any other point by going first to $p$ and then to the second point.
                                                          \end{solution}
                                                          \begin{problem}\label{argument-principle-numerical}Consider $\gamma : [0,2\pi] \to \C$ given by $\gamma(\theta) = e^{i\theta}$.  For an integer $n \in \Z$ and $f(z) = z^n$, compute
                                                            \[
                                                                \frac{1}{2\pi i} \displaystyle\int_\gamma \frac{f'(z)}{f(z)} \, dz
                                                                  \]
                                                                    in two different ways.  First, evaluate $f'(z)/f(z)$ and invoke
                                                                      \ref{integral-powers-of-z}.  Second, describe a curve $\gamma_n$ and
                                                                        interpret $\int_\gamma \frac{f'(z)}{f(z)} \, dz$ as
                                                                          $\int_{\gamma_n} dz/z$ for that different curve $\gamma_n$.  (This
                                                                            is our first glimpse of the \textbf{argument principle}.)
                                                                            \end{problem}
                                                                            \begin{solution}
                                                                            \[\frac{f'(z)}{f(z)} = \frac{nz^{n-1}}{z^n} = \frac{n}{z}\]
                                                                            Then using \ref{integral-powers-of-z}, the integral's value is $n$.

                                                                            Next, consider going along the curve $\gamma_n = f\circ \gamma$, it's the path that $f(z)$ traces out as we go along the curve. Algebraically, let $u = f(z)$ so $du = f'(z) dz.$
                                                                            Then
                                                                            \[
                                                                            \int_{\gamma} \frac{f'(z)}{f(z)}dz =  \int_{\gamma_n} \frac{1}{u} du = \int \frac{\gamma_n'(t)}{\gamma_n(t)}dt = \int_0^{2\pi} \frac{ine^{int}}{e^{int}}dt = 2in\pi
                                                                            \]
                                                                            Dividing out by $2in\pi$, the value of the integral we were given is $n$.
                                                                            \end{solution}

                                                                            \begin{problem}\label{one-over-z-w-around-circle}
                                                                              Consider the curve $\gamma : [0,2\pi] \to \C$ given by $\gamma(\theta) = e^{i\theta}$.  For $w \in \C$ with $|w| \neq 1$, compute
                                                                                \[
                                                                                \frac{1}{2\pi i} \displaystyle\int_\gamma \frac{1}{z-w} \, dz
                                                                                \]
                                                                                perhaps by invoking \ref{one-over-z-around-circle}.
                                                                                \end{problem}
                                                                                \begin{solution}
                                                                                Let $v = z - w,$ so $dv = dz$. Defining the function $\gamma_2(t) := \gamma(t) - w$, we see that
                                                                                \[\int_\gamma \frac{1}{z-w}dz =  \int_{\gamma_2} \frac{1}{v}dv\]
                                                                                This is exactly the integral of a circle, and \ref{one-over-z-around-circle} shows that if the circle doesn't include the origin, then the integral is 0 and otherwise, (the specific proof I did shows), it is $\frac{1}{2\pi i}$. Thus
                                                                                \[
                                                                                \frac{1}{2\pi i} \displaystyle\int_\gamma \frac{1}{z-w} \, dz = \begin{cases}1 & |w| < 1\\ 0 & |w| > 1\end{cases}.
                                                                                \]
                                                                                \end{solution}

                                                                                \begin{problem}
                                                                                Yet again consider the curve $\gamma : [0,2\pi] \to \C$ given by $\gamma(\theta) = e^{i\theta}$.  For distinct $w_1, w_2 \in \C$ with $\abs{w_1} \neq 1$ and $\abs{w_2} \neq 1$, let $f(z) = (z - w_1) (z - w_2)$ and compute
                                                                                \[
                                                                                \frac{1}{2\pi i} \displaystyle\int_\gamma \frac{f'(z)}{f(z)} \, dz.
                                                                                \]
                                                                                \end{problem}
                                                                                \begin{solution}
                                                                                Let $\gamma' = f\circ \gamma = (e^{i\theta} - w_1)(e^{i\theta} - w_2)$, and make the subsitution $w=f(z)$ so $dw = f'(z)dz$.
                                                                                Then
                                                                                \begin{align*}
                                                                                \frac{1}{2\pi i} \int_\gamma \frac{f'(z)}{f(z)} \, dz
                                                                                &= \frac{1}{2\pi i} \int_{\gamma_1}\frac{1}{w} \, dw\\
                                                                                &= \frac{1}{2\pi i} \int_0^{2\pi} \frac{\gamma_1'(t)}{\gamma_1(t)} \, dt\\
                                                                                &= \frac{1}{2\pi i} \int_0^{2\pi} \frac{ie^{i\theta} - w_1}{e^{i\theta}} + \frac{ie^{i\theta}}{e^{i\theta} - w_1} \, dt\\
                                                                                &= \frac{1}{2\pi i} (\int_0^{2\pi} \frac{ie^{i\theta}}{e^{i\theta} - w_1} dt + \int_0^{2\pi} \frac{ie^{i\theta}}{e^{i\theta} - w_1}dt )\\
                                                                                &= \frac{1}{2\pi i} (\int_{\gamma} \frac{1}{z - w_1} dt + \int_{\gamma} \frac{1}{z - w_1}dt )
                                                                                \end{align*}
                                                                                Invoking \ref{one-over-z-w-around-circle}, this is
                                                                                \[\left(\begin{cases}1 & |w_1| < 1\\ 0 & |w_1| > 1\end{cases} \right) +
                                                                                \left(\begin{cases}1 & |w_2| < 1\\ 0 & |w_2| > 1\end{cases}\right)\]
                                                                                \end{solution}

                                                                                \section{Prove or Disprove and Salvage if Possible}

                                                                                \begin{problem}
                                                                                If $e^z = e^w$, then $z = w$.
                                                                                \end{problem}
                                                                                \begin{solution}
                                                                                False, $e^0 = e^{2\pi i}= 1$. However, since $e^{x + iy} = e^x(\cos(y) + i\sin(y))$, $\exists \, k \, s.t. \, (e^z = e^w \implies z = w + 2\pi ik)$
                                                                                \end{solution}
                                                                                \begin{problem}
                                                                                If $f \, dz$ is exact, then $\displaystyle\int_\gamma f \, dz = 0$.
                                                                                \end{problem}
                                                                                \begin{solution}
                                                                                Additionally we require that $\gamma$ is a closed curve starting and ending at the same point $p$. In this case, letting $F$ be the antiderivative of $f$,
                                                                                \begin{align*}
                                                                                \int_\gamma f \, dz
                                                                                &= \int \gamma'(t)f(\gamma(t)) \, dt \\
                                                                                &= \int_\gamma \frac{d}{dt}(F\circ\gamma)(t) \, dt\\
                                                                                &= (F\circ\gamma)(\gamma(p) - \gamma(p)) = 0
                                                                                \end{align*}
                                                                                \end{solution}

                                                                                \begin{problem}% orientation issues
                                                                                Suppose $\gamma : [0,1] \to \C$ is a smooth curve, and $p : [0,1] \to [0,1]$ is a smooth bijection.
                                                                                Then
                                                                                \[
                                                                                \int_\gamma f(z) \, dz = \int_{\gamma \circ p} f(z) \, dz.
                                                                                \]
                                                                                \end{problem}
                                                                                \begin{solution}
                                                                                If $p(0)=1$, then we will switch the direction of the curve which will make the answer negative.

                                                                                Otherwise, we can prove it as follows.
                                                                                \begin{align*}
                                                                                \int_{\gamma \circ p} f(z) \, dz &= \int_0^1 (\frac{d}{dt}(\gamma\circ p) \times (f \circ \gamma \circ p))t \,dt\\
                                                                                &= \int_{0}^1 \gamma'(p(t))p'(t) (f \circ \gamma \circ p)t\, dt \\
                                                                                &= \int_{0}^1 \gamma'(u) (f \circ \gamma)u\, du \quad \color{purple} u=p(t), du = p'(t)dt\\
                                                                                &= \int_{\gamma} f(z) \, dz
                                                                                \end{align*}
                                                                                \end{solution}

                                                                                \begin{problem}\label{identity-theorem}If $f \in \C[z]$ is a polynomial with infinitely many zeros, then
                                                                                $f \equiv 0$.
                                                                                \end{problem}
                                                                                \begin{solution}
                                                                                By the fundamental theorem of arithmetic, $f$ splits completely in $\C$ as $\prod_{i=0}^n (z - r_i)$ where $r_i$ is a root. At any number that's not a root, this product is clearly nonzero unless it is the empty product, so we must have that $f\equiv 0$.
                                                                                \end{solution}
                                                                                \end{document}


\documentclass{notes}

  \title{Collatz Turing Machine}
  \author{ian.mcloughlin@gmit.ie}
  \date{\today}

\begin{document}

\section*{Language}
  \begin{align*}
    A &= \{ 0, 1 \} \\
    T &= A \ \cup \ \{ , \} \ \cup \  \{ \blm,  X \} \\
    L &= \{1, 10, 11, 100, 101, 110, 111, 1000, 1001, \ldots \} \\
    f(s) &= 1s,s \\
  \end{align*}

\section*{Duplicate and append 1}
  \begin{tabular}{rl}
    \textbf{State} & \textbf{Description} \\
    \midrule
    \( q_0 \) & Append a comma. \\
    \( q_1 \) & Move left to start. \\
    \( q_2 \) & Check if 0 or 1, mark X. \\
    \( q_3 \) & Move right to end, append 0. \\
    \( q_4 \) & Move right to end, append 1. \\
    \( q_5 \) & Move left to X, overwrite 0, back to \(q_2\). \\
    \( q_6 \) & Move left to X, overwrite 1, back to \(q_2\). \\
    \( q_7 \) & Move to start, prepend 1. \\
    \( q_8 \) & Back to start, end. \\
  \end{tabular}

  \begin{center}
    \begin{tabular}{x{1cm}x{1cm}x{1cm}x{1cm}x{1cm}}
      \toprule
      State & Input & Write & Move & Next \\
      \midrule
      % Right to Write a comma at end on input.
      \(q_0\) & \bl &   , & L & \(q_1\) \\
      \(q_0\) &   0 &   0 & R & \(q_0\) \\
      \(q_0\) &   1 &   1 & R & \(q_0\) \\
      \(q_0\) &   , &   , & R & \(q_f\) \\
      \(q_0\) &   X &   X & R & \(q_f\) \\
      \midrule
      % Left to start.
      \(q_1\) & \bl & \bl & R & \(q_2\) \\
      \(q_1\) &   0 &   0 & L & \(q_1\) \\
      \(q_1\) &   1 &   1 & L & \(q_1\) \\
      \(q_1\) &   , &   , & R & \(q_f\) \\
      \(q_1\) &   X &   X & R & \(q_f\) \\
      \midrule
      % Overwrite first symbol with X.
      \(q_2\) & \bl & \bl & R & \(q_f\) \\
      \(q_2\) &   0 &   X & R & \(q_3\) \\
      \(q_2\) &   1 &   X & R & \(q_4\) \\
      \(q_2\) &   , &   , & R & \(q_7\) \\
      \(q_2\) &   X &   X & R & \(q_f\) \\
      \midrule
      % Left to write a 0 at the end.
      \(q_3\) & \bl &   0 & L & \(q_5\) \\
      \(q_3\) &   0 &   0 & R & \(q_3\) \\
      \(q_3\) &   1 &   1 & R & \(q_3\) \\
      \(q_3\) &   , &   , & R & \(q_3\) \\
      \(q_3\) &   X &   X & R & \(q_f\) \\
      \midrule
      % Left to write a 1 at the end.
      \(q_4\) & \bl &   1 & L & \(q_6\) \\
      \(q_4\) &   0 &   0 & R & \(q_4\) \\
      \(q_4\) &   1 &   1 & R & \(q_4\) \\
      \(q_4\) &   , &   , & R & \(q_4\) \\
      \(q_4\) &   X &   X & R & \(q_f\) \\
      \midrule
      % Left to the X, overwrite with 0
      \(q_5\) & \bl & \bl & L & \(q_f\) \\
      \(q_5\) &   0 &   0 & L & \(q_5\) \\
      \(q_5\) &   1 &   1 & L & \(q_5\) \\
      \(q_5\) &   , &   , & L & \(q_5\) \\
      \(q_5\) &   X &   0 & R & \(q_2\) \\
      \midrule
      % Left to the X, overwrite with 1
      \(q_6\) & \bl & \bl & L & \(q_f\) \\
      \(q_6\) &   0 &   0 & L & \(q_6\) \\
      \(q_6\) &   1 &   1 & L & \(q_6\) \\
      \(q_6\) &   , &   , & L & \(q_6\) \\
      \(q_6\) &   X &   1 & R & \(q_2\) \\
      \midrule
      % Left to start, prepend 1.
      \(q_7\) & \bl &   1 & L & \(q_8\) \\
      \(q_7\) &   0 &   0 & L & \(q_7\) \\
      \(q_7\) &   1 &   1 & L & \(q_7\) \\
      \(q_7\) &   , &   , & L & \(q_7\) \\
      \(q_7\) &   X &   X & L & \(q_7\) \\
      \midrule
      % Back to start, accept.
      \(q_8\) & \bl & \bl & R & \(q_a\) \\
      \(q_8\) &   0 &   0 & R & \(q_f\) \\
      \(q_8\) &   1 &   1 & R & \(q_f\) \\
      \(q_8\) &   , &   , & R & \(q_f\) \\
      \(q_8\) &   X &   X & R & \(q_f\) \\
      \bottomrule
    \end{tabular}
  \end{center}

\section*{Add}
  \begin{tabular}{rl}
    \textbf{State} & \textbf{Description} \\
    \midrule
    \( q_0 \) & Right to comma. \\
    \( q_1 \) & Decrease by 1, go to final states if no 1. \\
    \( q_2 \) & Left to start. \\
    \( q_3 \) & Increase by 1, use X as comma if overflow. \\
    \( q_4 \) & Delete to comma/X, replace X with 1. \\
    \( q_5 \) & Left to start. \\
  \end{tabular}

  \begin{center}
    \begin{tabular}{x{1cm}x{1cm}x{1cm}x{1cm}x{1cm}}
      \toprule
      State & Input & Write & Move & Next \\
      \midrule
      % Right to comma/X.
      \(q_0\) & \bl & \bl & L & \(q_f\) \\
      \(q_0\) &   0 &   0 & R & \(q_0\) \\
      \(q_0\) &   1 &   1 & R & \(q_0\) \\
      \(q_0\) &   , &   , & R & \(q_1\) \\
      \(q_0\) &   X &   X & R & \(q_1\) \\
      \midrule
      % Decrease by 1, go to final states if no 1.
      \(q_1\) & \bl & \bl & L & \(q_4\) \\
      \(q_1\) &   0 &   1 & R & \(q_1\) \\
      \(q_1\) &   1 &   0 & L & \(q_2\) \\
      \(q_1\) &   , &   , & L & \(q_f\) \\
      \(q_1\) &   X &   X & L & \(q_f\) \\
      % Left to start.
      \(q_2\) & \bl & \bl & R & \(q_3\) \\
      \(q_2\) &   0 &   0 & L & \(q_2\) \\
      \(q_2\) &   1 &   1 & L & \(q_2\) \\
      \(q_2\) &   , &   , & L & \(q_2\) \\
      \(q_2\) &   X &   X & L & \(q_2\) \\
      \midrule
      % Increase by 1 - use X in place of comma if overflow.
      \(q_3\) & \bl & \bl & R & \(q_f\) \\
      \(q_3\) &   0 &   1 & R & \(q_0\) \\
      \(q_3\) &   1 &   0 & R & \(q_2\) \\
      \(q_3\) &   , &   X & L & \(q_0\) \\
      \(q_3\) &   X &   X & L & \(q_f\) \\
      \midrule
      % Delete to and including comma/X - replace X with 1.
      \(q_4\) & \bl & \bl & L & \(q_f\) \\
      \(q_4\) &   0 & \bl & L & \(q_4\) \\
      \(q_4\) &   1 &   1 & L & \(q_f\) \\
      \(q_4\) &   , & \bl & L & \(q_5\) \\
      \(q_4\) &   X &   1 & L & \(q_5\) \\
      \midrule
      % Left to start.
      \(q_5\) & \bl & \bl & R & \(q_a\) \\
      \(q_5\) &   0 &   0 & L & \(q_5\) \\
      \(q_5\) &   1 &   1 & L & \(q_5\) \\
      \(q_5\) &   , &   , & L & \(q_f\) \\
      \(q_5\) &   X &   X & L & \(q_f\) \\
      \bottomrule
    \end{tabular}
  \end{center}

  \section*{Divide by two}
  \begin{tabular}{rl}
    \textbf{State} & \textbf{Description} \\
    \midrule
    \( q_0 \) & Delete 0, move right. \\
  \end{tabular}

  \begin{center}
    \begin{tabular}{x{1cm}x{1cm}x{1cm}x{1cm}x{1cm}}
      \toprule
      State & Input & Write & Move & Next \\
      \midrule
      % Delete 0, move right.
      \(q_0\) & \bl & \bl & L & \(q_f\) \\
      \(q_0\) &   0 & \bl & R & \(q_0\) \\
      \(q_0\) &   1 &   1 & R & \(q_f\) \\
      \(q_0\) &   , &   , & R & \(q_f\) \\
      \(q_0\) &   X &   X & R & \(q_f\) \\
      \bottomrule
    \end{tabular}
  \end{center}

  \section*{Remove leading zeros}
  Fails on string meaning zero.

  \begin{tabular}{rl}
    \textbf{State} & \textbf{Description} \\
    \midrule
    \( q_0 \) & Move right to end. \\
    \( q_1 \) & Delete zeros. \\
    \( q_2 \) & Left to end. \\
  \end{tabular}

  \begin{center}
    \begin{tabular}{x{1cm}x{1cm}x{1cm}x{1cm}x{1cm}}
      \toprule
      State & Input & Write & Move & Next \\
      \midrule
      % Right to end.
      \(q_0\) & \bl & \bl & L & \(q_1\) \\
      \(q_0\) &   0 &   0 & R & \(q_0\) \\
      \(q_0\) &   1 &   1 & R & \(q_0\) \\
      \(q_0\) &   , &   , & R & \(q_f\) \\
      \(q_0\) &   X &   X & R & \(q_f\) \\
      \midrule
      % Delete zeros.
      \(q_1\) & \bl & \bl & R & \(q_f\) \\
      \(q_1\) &   0 & \bl & L & \(q_0\) \\
      \(q_1\) &   1 &   1 & L & \(q_2\) \\
      \(q_1\) &   , &   , & R & \(q_f\) \\
      \(q_1\) &   X &   X & R & \(q_f\) \\
      \midrule
      % Left to start.
      \(q_2\) & \bl & \bl & R & \(q_a\) \\
      \(q_2\) &   0 &   0 & R & \(q_2\) \\
      \(q_2\) &   1 &   1 & R & \(q_2\) \\
      \(q_2\) &   , &   , & R & \(q_f\) \\
      \(q_2\) &   X &   X & R & \(q_f\) \\
      \bottomrule
    \end{tabular}
  \end{center}

  \section*{Check if 1}

  \begin{tabular}{rl}
    \textbf{State} & \textbf{Description} \\
    \midrule
    \( q_0 \) & Check if first bit 1. \\
    \( q_1 \) & Check if second bit blank. \\
  \end{tabular}

  \begin{center}
    \begin{tabular}{x{1cm}x{1cm}x{1cm}x{1cm}x{1cm}}
      \toprule
      State & Input & Write & Move & Next \\
      \midrule
      % First bit 1.
      \(q_0\) & \bl & \bl & R & \(q_f\) \\
      \(q_0\) &   0 &   0 & R & \(q_f\) \\
      \(q_0\) &   1 &   1 & R & \(q_1\) \\
      \(q_0\) &   , &   , & R & \(q_f\) \\
      \(q_0\) &   X &   X & R & \(q_f\) \\
      \midrule
      % Check if blank.
      \(q_1\) & \bl & \bl & L & \(q_a\) \\
      \(q_1\) &   0 &   0 & L & \(q_f\) \\
      \(q_1\) &   1 &   1 & L & \(q_f\) \\
      \(q_1\) &   , &   , & R & \(q_f\) \\
      \(q_1\) &   X &   X & R & \(q_f\) \\
      \bottomrule
    \end{tabular}
  \end{center}

  \section*{Check if even}

  \begin{tabular}{rl}
    \textbf{State} & \textbf{Description} \\
    \midrule
    \( q_0 \) & Check if first bit 0. \\
  \end{tabular}

  \begin{center}
    \begin{tabular}{x{1cm}x{1cm}x{1cm}x{1cm}x{1cm}}
      \toprule
      State & Input & Write & Move & Next \\
      \midrule
      % Check 0.
      \(q_0\) & \bl & \bl & R & \(q_f\) \\
      \(q_0\) &   0 &   0 & R & \(q_a\) \\
      \(q_0\) &   1 &   1 & R & \(q_f\) \\
      \(q_0\) &   , &   , & R & \(q_f\) \\
      \(q_0\) &   X &   X & R & \(q_f\) \\
      \bottomrule
    \end{tabular}
  \end{center}

  %\bibliography{bibliography}
\end{document}


\section{Termination}\label{sec:termination}

Program divergence is, more often than not,
a bug rather than a feature.
%
To account for the common cases,
by default, \toolname proves termination of each recursive function.
%
Fortunately, refinements make this onerous task quite straightforward.
We need simply associate a \emph{well-founded termination metric} % $\mu$
on the function's parameters, and then use refinement typing to check
that the metric strictly decreases at each recursive call. In practice,
due to a careful choice of defaults, this amounts to about a line
of termination-related hints per hundred lines of source.
%
In Chapter~\ref{chapter:refinedhaskell}
we prove soundness of our refinement type based termination checker
and also we explain how soundness of \toolname crucially depends on
the termination checker.
%
Here, we provide an overview on how one can use \toolname
to prove termination.

\mypara{Simple Metrics}
As a starting example, consider the @fac@ function
%
\begin{code}
  fac :: n:Nat -> Nat / [n]
  fac 0 = 1
  fac n = n * fac (n-1)
\end{code}
%
The termination metric is simply the parameter @n@;
as @n@ is non-negative and decreases at the recursive
call, \toolname verifies that @fac@ will terminate.
%
We specify the termination metric in the type signature
with the @/[n]@.

Termination checking is performed at the same
time as regular type checking, as it can be
reduced to refinement type checking with a
special terminating fixpoint combinator~\ref{chapter:refinedhaskell}.
Thus, if \toolname fails to prove that a given
termination metric is well-formed and decreasing,
it will report a @Termination Check@ @Error@.
%% \RJ{Seems untrue -- I just get a plain old liquid type error?
%% NV: IF you provide termination metrics, you DO get termination check error}.
At this point, the user can either debug
the specification, or mark the function
as non-terminating.

%%\mypara{Refinements Enable Termination}
%%Consider Euclid's GCD:
%%%
%%\begin{code}
%%  gcd :: a:Nat -> {v:Nat | v < a} -> Nat
%%  gcd a 0 = a
%%  gcd a b = gcd b (a `mod` b)
%%\end{code}
%%%
%%Here, the termination metric is the first parameter @a@.
%%To prove that @a@ is decreasing requires
%%the fact that the second parameter is smaller than the first
%%and that @mod@ returns results smaller than its second
%%parameter. Both facts are easily expressed as refinements,
%%but elude non-extensible checkers~\cite{Giesl11}.
%%
%%\mypara{Explicit Termination Metrics}
%%The termination metric can be some parameter \emph{other} than the first
%%argument.
%%For example, consider: % As an example, consider the tail-recursive factorial:
%%%
%%\begin{code}
%%  tfac     :: Nat -> n:Nat -> Nat / [n]
%%  tfac x 0 = if n == 0 then x
%%                       else tfac (n*x) (n-1)
%%\end{code}
%%%
%%%
%%It can be checked that @n@, \ie, the second argument is decreasing at each recursive call.
%%
\mypara{Termination Expressions}
Sometimes, no single parameter decreases across recursive calls,
but there is some \emph{expression} that forms the decreasing
metric.
%
For example recall @range lo hi@ (from \S~\ref{sec:tool:verification})
which returns the list of @Int@s from @lo@ to @hi@:
%
\begin{code}
  range lo hi
    | lo < hi   = lo : range (lo+1) hi
    | otherwise = []
\end{code}
%
Here, neither parameter is decreasing (indeed, the first
one is increasing) but @hi-lo@ decreases across each call.
To account for such cases, we can specify as the termination
metric a (refinement logic) expression over the function
parameters. Thus, to prove termination, we could type @range@ as:
\begin{code}
  lo:Int -> hi:Int -> [(Btwn lo hi)] / [hi-lo]
\end{code}

\mypara{Lexicographic Termination}
The Ackermann function
%
\begin{code}
  ack m n
    | m == 0    = n + 1
    | n == 0    = ack (m-1) 1
    | otherwise = ack (m-1) (ack m (n-1))
\end{code}
%
is curious as there exists no simple, natural-valued,
termination metric that decreases at each recursive call.
%
However @ack@ terminates because at each call \emph{either}
@m@ decreases \emph{or} @m@ remains the same and @n@ decreases.
%
In other words, the pair @(m,n)@ strictly decreases according to a
\emph{lexicographic} ordering.
%
Thus \toolname supports termination metrics that are a
\emph{sequence of} termination expressions. For example,
we can type @ack@ as:
%
\begin{code}
  ack :: m:Nat -> n:Nat -> Nat / [m, n]
\end{code}
%
At each recursive call \toolname uses a lexicographic
ordering to check that the sequence of termination
expressions is decreasing (and well-founded in each component).

\mypara{Mutual Recursion}
%
The lexicographic mechanism lets us check termination of
mutually recursive functions, \eg @isEven@ and @isOdd@
%
\begin{code}
  isEven 0 = True
  isEven n = isOdd $ n-1

  isOdd n  = not $ isEven n
\end{code}
%
Each call terminates as either @isEven@ calls @isOdd@ with a
decreasing parameter, \emph{or} @isOdd@ calls @isEven@ with
the same parameter, expecting the latter to do the decreasing.
%
For termination, we type:
%
\begin{code}
  isEven :: n:Nat -> Bool / [n, 0]
  isOdd  :: n:Nat -> Bool / [n, 1]
\end{code}
%
To check termination, \toolname verifies that at each recursive
call the metric of the caller is less than the metric of the
callee.
%
When @isEven@ calls @isOdd@, it proves that the caller's
metric, namely @[n,0]@ is greater than the callee's @[n-1,1]@.
When \hbox{@isOdd@} calls @isEven@, it proves that the
caller's metric @[n,1]@ is greater than the callee's @[n,0]@,
thereby proving the mutual recursion always terminates.

\mypara{Recursion over Data Types}
The above strategies generalize easily to functions that recurse
over (finite) data structures like arrays, lists, and trees.
In these cases, we simply use \emph{measures} to project the
structure onto @Nat@, thereby reducing the verification to
the previously seen cases.
For example, we can prove that @map@
%
\begin{code}
  map f (x:xs) = f x : map f xs
  map f []     = []
\end{code}
%
terminates, by typing @map@ as
%
\begin{code}
  (a -> b) -> xs:[a] -> [b] / [len xs]
\end{code}
%
\ie, by using the measure @len xs@, from \S~\ref{sec:tool:measures},
as the metric.

%%% %
%%% \begin{code}
%%%   data L [sz] a = N | C a (L a)
%%% \end{code}
%%% %
%%% we can define a \emph{measure}
%%% %
%%% \begin{code}
%%%   measure sz  :: L a -> Nat
%%%   sz (C x xs) = 1 + (sz xs)
%%%   sz N        = 0
%%% \end{code}
%%% %
%%% We prove that @map@ terminates using the type:
%%% %
%%% \begin{code}
%%%   map :: (a -> b) -> xs:L a -> L b / [sz xs]
%%%   map f (C x xs) = C (f x) (map f xs)
%%%   map f N        = N
%%% \end{code}
%%% %
%%% That is, by simply using @(sz xs)@  as the
%%% decreasing metric.

\mypara{Generalized Metrics Over Datatypes}
In many functions there is no single argument
whose measure provably decreases. Consider
%
\begin{code}
  merge (x:xs) (y:ys)
    | x < y     = x : merge xs (y:ys)
    | otherwise = y : merge (x:xs) ys
\end{code}
%
from the homonymous sorting routine. Here, neither
parameter decreases, but the \emph{sum} of their
sizes does. To prove termination, we can type @merge@ as:
%
\begin{code}
  xs:[a] -> ys:[a] -> [a] / [len xs + len ys]
\end{code}

%%%% \begin{figure*}[!t]
%%%% 	\begin{code}
%%%% 	type OL  a   =  [a]<{\fld v -> (v >= fld)}>
%%%%
%%%% 	qsort :: (Ord a) => xs:[a] -> OL a / [(len xs), 0]
%%%% 	qsort []         = []
%%%% 	qsort (x:xs)     = qpart x xs [] []
%%%%
%%%% 	qpart :: (Ord a) => x:a -> q:[a] -> r: [{v:a|v<x}] -> p: [{v:a|v>=x}] -> OL a
%%%% 	       / [((len q) + (len r) + (len p)), ((len q) + 1)]
%%%% 	qpart x []     rlt rge             = app x (qsort rlt) (x:qsort rge)
%%%% 	qpart x (y:ys) rlt rge | x > y     = qpart x ys (y:rlt) rge
%%%% 	                       | otherwise = qpart x ys rlt (y:rge)
%%%%
%%%% 	app k []     ys = ys
%%%% 	app k (x:xs) ys = x : (app k xs ys)
%%%% 	\end{code}
%%%% \caption{Mutual-recursive qsort}
%%%% \label{fig:code:qsort}
%%%% \end{figure*}

\mypara{Putting it all Together}
The above techniques can be combined to prove
termination of the mutually recursive quick-sort (from~\citep{XiTerminationLICS01})% \RJ{from where?}
%
\begin{code}
  qsort (x:xs)   = qpart x xs [] []
  qsort []       = []

  qpart x (y:ys) l r
    | x > y      = qpart x ys (y:l) r
    | otherwise  = qpart x ys l (y:r)
  qpart x [] l r = app x (qsort l) (qsort r)

  app k []     z = k : z
  app k (x:xs) z = x : app k xs z
\end{code}
%
@qsort (x:xs)@ calls @qpart x xs@ to partition @xs@
into two lists @l@ and @r@ that have elements less
and greater or equal than the pivot @x@, respectively.
%
When @qpart@ finishes partitioning it mutually recursively
calls @qsort@ to sort the two list and appends the results
with @app@.
%
\toolname proves sortedness as well~\cite{vazou13} but let us
focus here on termination. To this end, we type the functions
as:
%
\begin{code}
  qsort :: xs:_ -> _
        / [len xs, 0]

  qpart :: _ -> ys:_ -> l:_ -> r:_ -> _
        / [len ys + len l + len r, 1 + len ys]
\end{code}
%
As before, \toolname checks that at each recursive call
the caller's metric is less than the callee's.
%
When @qsort@ calls @qpart@ the length of the unsorted
list @len (x:xs)@ exceeds the \hbox{@len xs + len [] + len []@}.
%
When @qpart@ recursively calls itself the first component
of the metric is the same, but the length of the unpartitioned
list decreases, \ie @1 + len y:ys@ exceeds \hbox{@1 + len ys@}.
%
Finally, when @qpart@ calls @qsort@ we have \hbox{@len ys + len l + len r@}
exceeds both @len l@ and @len r@, thereby ensuring termination.

%%% Before we dive into proving termination, note that the
%%% type alias @OL a@ uses Abstract Refinements~\citep{vazou13} to describe
%%% Ordered Lists.
%%% Thus, when \toolname decides that the @qsort@ is SAFE,
%%% it proves both termination and sortedness.
%%%%Note that classical appending @rlt ++ rge@ of the two sorted lists will lose
%%%%the crucial for sorting information that every element of @rlt@ is less than each element of @rge@.
%%%%%
%%%%Thus we defined a new version of list appending @app@ that uses the pivot element @k@
%%%%as a ghost-parameter.
%%%%%
%%%%Good news is that \toolname will automatically infer the appropriate type of @app@!

%% Let \mus{xs} and \mup{q}{r}{p} be the (well-founded) termination pairs
%% for @qsort xs@ and @qpart x q r p@ respectively, as annotated in the type signatures.
%% %
%%%$\mu_s(x:xs) = (1 + len xs, 0) > (len xs + 0 + 0, len xs + 1) = \mu_p(xs, [], [])$
%%%$\mu_p(y:ys, rlt, rge) = ((1 + (len ys)) + (len rlt) + (len rge), (1 + len ys) + 1) >
%%%(len ys + (1 + (len rlt)) + (len rge), ((len ys) + 1)) = \mu_p(ys, y:rlt, rge) $
%%%$\mu_p([], rlt, rge) = (0 + len rlt + len rge, 1) > (len rlt, 0) = \mu_s(rlt)$

%% Existing techniques~\citep{CookPR11} could be used to
%% come up with termination metrics.
%% We leave embedding these techniques into \toolname as a future work, and instead
%% we use some defaults to automate termination proving
%% on functions with trivial metrics.

\mypara{Automation: Default Size Measures}
%
The @qsort@ example illustrates that while \toolname is
very expressive, devising appropriate termination metrics
can be tricky.
%
Fortunately, such patterns are very uncommon, and the vast
majority of cases in real world programs are just structural
recursion on a datatype.
%
\toolname automates termination proofs for this common case,
by allowing users to specify a \emph{default size measure}
for each data type, \eg @len@ for @[a]@.
%
Now, if no explicit termination metric is given, by default
\toolname assumes that the \emph{first} argument whose type
has an associated size measure decreases.
%
Thus, in the above, we need not specify metrics for @fac@
or @map@ as the size measure is automatically
used to prove termination.
%
This heuristic suffices to \emph{automatically}
prove 67\% of recursive functions terminating.

\mypara{Disabling Termination Checking}
In \texttt{Haskell}'s lazy setting not all functions are terminating.
%
\toolname provides two mechanisms the disable termination proving.
%
A user can disable checking a single function by marking
that function as lazy. For example, specifying @lazy repeat@
tells the tool to not prove @repeat@ terminates.
%
Optionally, a user can disable termination checking for a whole
module by using the command line argument \cmdnotermination
for the entire file.


\section{Image Processing}

\begin{definition}[Aliasing]
    Images are constructed and then reconstructed one or more times -
    each of these reconstructions are referred to as aliases.
\end{definition}

\begin{definition}[Anisotropy]\label{anisotropy}
    Directionally dependent. Opposite of~\nameref{isotropy}.
    Example is to apply a filter to a set of images - consider
    what would happen if one image was tilted.
\end{definition}

\begin{definition}[Anisotropic diffusion]
    also called Perona-Malik diffusion, is a technique aiming at reducing image
    noise without removing significant parts of the image content, typically
    edges, lines or other details that are important for the interpretation of
    the image.

\end{definition}

\begin{definition}[Aperture]
    A hole which light goes through.
\end{definition}

\begin{definition}[Convolution]\label{convolution}
    In image-processing, this can in general be thought of as applying a mask
    that takes the local neighbourhood for a pixel x and calculates its
    gradient.

    Noteworthingly, convolution as an operation to an image is in general
    $O(n^{2})$.
\end{definition}

\begin{definition}[Curve representation]
    How to represent a curve: computational cost is crucial.

    \begin{description}
        \item[Explicit] $y = f(x)$ - only lines but really easy to genereate.
        \item[Implicit] $f(x,y) = 0$. \dots imagine a circle: to know if a point
            is on the line, we can use pythagoras's $x^{2} + y^{2} - r^{2} = 0$. Costs a lot.
        \item[Parametric] $(x,y) = ( f(u), g(u))$. Oh yeah..
    \end{description}

\end{definition}

\begin{definition}[Dilation (of functions)]
    To skew a function, e.g.\ for $f(x) = x$, you skew to $\hat{f}(x) = x + 2$.
\end{definition}

\begin{definition}[Dirichet boundary condtions]
    In image processing, you can say that this boundary condition is to assume
    for a set of differential equations, the unknowns at the borders are
    0: $u(0) = u(1) = 0$.

\end{definition}

\begin{definition}[extrapolation]
    The process of estimating, beyond the original observation range, the value
    of a variable on the basis of its relationship with another variable.

    Creating a tangent line at the end of the known data and extending it
    beyond that limit.

\end{definition}

\begin{definition}[Filter]
    Applying a mask or operation to an image to extract or distort values.

    \begin{description}
        \item[High-pass] takes only the high frequency into play, lower values
            are not affected that strongly (if at all)
        \item[Low-pass] \dots
    \end{description}
\end{definition}

\begin{definition}[Gaussian blur]
    Use a gaussian function on an image to reduce image noise and detail
\end{definition}

\begin{definition}[Gaussian filter]
    Gaussian filters have the properties of having no overshoot to a step
    function input while minimizing the rise and fall time.

\end{definition}

\begin{definition}[Gaussian pyramid]
    A stack of images, where for each iteration, you take neighboring pixels
    and make them into one. That is, you reduce the intensity in the picture.
    This compresses the image.

    It is a pyramid because for each iteration you take neighboring pixels
    from the previous pyramid to generate the current.
\end{definition}

\begin{definition}[Gradient flow]
    $$
        V = \nabla{f} = \left(\
        \frac{\partial{f}}{\partial{x_{1}}},
        \frac{\partial{f}}{\partial{x_{2}}},
        \dots,
        \frac{\partial{f}}{\partial{x_{n}}}
    \right)
    $$

    Note that for an oridnary image, the gradient at a given position is
    given by:
    $$
    \nabla{f} =
    \frac{\partial{f}}{\partial{x}}\hat{x} +
    \frac{\partial{f}}{\partial{y}}\hat{y}
    $$
    Where the first (x) term is the gradient in direction x, and y is the
    gradient in direction y.

    One can also calculate the direction of the gradient using
    $\theta = a\tan^{2}{\frac{\partial{f}}{\partial{y}}, \frac{\partial{f}}{\partial{x}}}$
\end{definition}

\begin{definition}[Grating]
    A collection of identical pararell objects, placed next to each other with
    equal spacing.
    \end{definition}
\begin{definition}[Grayscale]
    an image in which the value of each pixel is a single sample, that is, it
    carries only intensity information

    Grayscale images are distinct from one-bit bi-tonal black-and-white images,
    which in the context of computer imaging are images with only the two
    colors, black, and white (also called bilevel or binary images). Grayscale
    images have many shades of gray in between.

    Grayscale images are often the result of measuring the intensity of light
    at each pixel in a single band of the electromagnetic spectrum

\end{definition}

\begin{definition}[Image gradient]
    Gradual blend of color
\end{definition}

\begin{definition}[Image noise]
    Unwanted signal, electrical signals not wanted in an image
\end{definition}

\begin{definition}[Image masking]
    Apply an image over another. This can be used for e.g.\ putting a square image
    with only contents in the middle over another.
\end{definition}

\begin{definition}[Interpolation]
    A way to estimate (pixel) values when resizing to a larger image.

    An example from calculus is: given temps at noon and midnight, estimate
    the temp at 5 PM as the mean of noon and midnight.

    Interpolation can also (in signal processing) be referred to as
    \textbf{upsampling}.

    Interpolation can be \textit{linear}(1D), \textit{bilinear}(2D) and so on\dots

\end{definition}

\begin{definition}[Isotropy]\label{isotropy}
    Directionally independent. Opposite of~\nameref{anisotropy}.
\end{definition}

\begin{definition}[Laplacian]
    Highlight regions of rapid intensity change and is therefore often used for
    edge detection.

    Often applied to an image that has first been smoothed with something
    approxiating in order to reduce its sensitivity to noise.

\end{definition}

\begin{definition}[Spatial frequency]
    The number of changes in color values that occur per space.

    Images with high spatial frequency are detailed, images with low spatial
    frequency will appear blurred.

    I.e.\ sharp transitions from low/high to high/low intensities are said
    to have a large spatial frequency,

\end{definition}

\begin{definition}[SRGB]
    sRGB is a standard RGB color space created cooperatively by HP and
    Microsoft in 1996 for use on monitors, printers and the Internet.

\end{definition}

\begin{definition}[Poisson image editing]
    Blend two images together and make them seem alike.
\end{definition}

\begin{definition}[Raster order]
    Begin at top left, proceed to right, then at leftmost pixel in next line.
\end{definition}

\begin{definition}[Raster image]
    An image with pixels.
\end{definition}

\begin{definition}[Ringing]
    The size of the oscillations after a peak frequency. E.g.\ if you have a
    sudden overshoot (bright color) the amplitude of the next wave is
    the ringing. (???)
\end{definition}

\begin{definition}[Scalar field]\label{scalarfield}
    Associate a value to every point in a space. E.g.\ for an image, you
    could assign each pixel a color value.
\end{definition}

\begin{definition}[Seperable filter]
    A filter that can be written as the product of two or more small filters.
    This is usually done to reduce the computational costs.
    E.g.~\nameref{convolution} is usually faster in 1d than iterating $nD$, for
    $n \ge 2$.
\end{definition}

\begin{definition}[Spline]
    A line that goes through a series of points.

    The section between the lines is interpolated. This can be done linearly,
    quadratic, \dots so on.
\end{definition}

\begin{definition}[Stencil]
    A figure that connects dots on a grid. Usually a cross-like figure
    (one center dots, and one edge out in 4 directions from the center to
    other dots).
\end{definition}


\documentclass[12pt]{article}

\usepackage{wrapfig}
\usepackage{float}
\usepackage{graphicx}
\usepackage[margin=1in]{geometry}
\usepackage{comment}
\usepackage{amsmath,amsthm,amssymb}
% \usepackage[vlined,linesnumbered,ruled,resetcount]{algorithm2e}

\DeclareMathOperator*{\argmax}{arg\,max}
\DeclareMathOperator*{\argmin}{arg\,min}
\DeclareMathOperator*{\val}{val}
\DeclareMathOperator*{\best}{best}
\DeclareMathOperator*{\worst}{worst}

\usepackage{algorithm}
% \usepackage{algorithmicx}
\usepackage[noend]{algpseudocode}

\newcommand{\R}{\mathbb{R}}
\newcommand{\Rgz}{\mathbb{R}_{\ge 0}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}

\newcommand{\Es}[2]{\mathbb{E}_{#1}\left[{#2}\right]}
\newcommand{\E}[1]{\mathbb{E}\left[{#1}\right]}
\newcommand{\ip}[2]{\left\langle{#1} , {#2}\right\rangle}

\newtheorem{definition}{Definition}[section]
\newtheorem{lemma}[definition]{Lemma}
\newtheorem{corollary}[definition]{Corollary}
\newtheorem{theorem}[definition]{Theorem}
\newtheorem{claim}[definition]{Claim}

\begin{document}

% \renewcommand{\qedsymbol}{\filledbox}

\title{Mechanism Design Notes}
\author{Clay Thomas\\
claytont@princeton.edu}
\maketitle

\clearpage
\section{Regret Minimization and Correlated Equilibrium Concepts}

Consider the following online learning problem:
There is a set of actions $A$, $|A| = n$. For each time step $t=1,\ldots,T$,
the decision maker picks a distribution on actions $p^t$.
Afterwards, a cost vector $c^t : A\to [0,1]$ is selected
(in any way - even by an adversary who can see $p^t$ before it picks $c^t$)
and the decision maker ``incurs cost'' $\Es{a\sim p^t}{c^t(a)} = \ip{p^t}{c^t}$.
Additionally, the decision maker learns the vector $c^t$.
The goal is to minimize the sum of the costs $\Es{a\sim p^t}{c^t(a)}$.

\[ \sum_{t=1}^T \min_a c^t(a) \]
Can we hope to achieve something approximating this?
Not quite: an adversary who can first look at $p^t$ can, for example,
make all costs very high, except the one with smallest probability in $p^t$.
A more realistic goal is to compare our cost to the following:
\[ \min_a \sum_{t=1}^T c^t(a) \]
Intuitively, ``the cost of the best action which, in hindsight, we could've
always taken''.

This leads us to define the \emph{regret} of our learning algorithm as
\[ \Es{a^t\sim p^t, t=1,\ldots,T}{\sum_{t=1}^T c^t(a)} - \min_a \sum_{t=1}^T c^t(a)
  = {\sum_{t=1}^T \ip{p^t}{c^t}} - \min_a \sum_{t=1}^T c^t(a)
\]

It turns out there is a simple algorithm that achieves low regret.
\begin{algorithm}
  \begin{algorithmic}[0]
  \State \textbf{Parameters:} $\epsilon > 0$, $T$
  \State Initialize $w^1(a) = 1$ for each $a\in A$
  \For {$t=1,\ldots,T$}
    \State Choose action $a\in A$ according to distribution $p^t$ proportional to $w^t$
    \State \qquad i.e. let $p^t(a) = w^t(a) / \Phi^t$ where
      $\Phi^t = \sum_{a\in A} w^t(a)$
    \State Observe costs $c^t$ and update weights as follows for each $a\in A$:
    \State \qquad $w^{t+1}(a) = w^t(a) (1 - \epsilon)^{c^t(a)}$.
  \EndFor
\end{algorithmic}
\end{algorithm}

\begin{theorem}
  Let $OPT = \min_a \sum_{t=1}^T c^t(a)$.
  If we set $\epsilon = \sqrt{\log n/T}$, then after $T$ rounds the total realized
  cost of the multiplicative weights algorithm is $OPT + O(\sqrt{T\log n})$.
\end{theorem}
\begin{proof}
  The proof follows two observations.
  First, if the decision maker makes a bad move
  (i.e. the weight of costly moves is high) then lots of total weight is lost.
  Second, the best action in hindsight contributes has weight
  $(1-\epsilon)^{OPT}$ at time $T$.
  Intuitively, this says that the only way for the decision maker to have many
  bad moves is if every action was very bad.

  For the formal proof, start by bounding $\Phi^{t+1}$ in terms of $\Phi^t$.
  In what follows, almost all of the inequalities are standard approximations,
  which can be proved using Taylor series or other estimations.
  This is somewhat interesting, and basically just happens because $\epsilon$ is
  so small we can go back and forth between exponentiation and addition really
  easily. In a sense, this was exactly the property we needed for our
  multiplicative weights to produce a low additive cost measure.
  Denote by $\ell^t = \Es{a\sim p^t}{c^t(a)}$ be the realized cost at time $t$.
  \begin{align*}
    \Phi^{t+1}
      & = \sum_a w^t(a)(1 - \epsilon)^{c^t(a)} \\
      & \le \sum_a w^t(a)(1 - \epsilon c^t(a)) \\
      & = \sum_a w^t(a) - \Phi^t \epsilon \sum_a \frac{w^t(a)}{\Phi^t} c^t(a) \\
      & = \Phi^t \left( 1 -\epsilon \sum_a p^t(a) c^t(a) \right) \\
      & = \Phi^t \left( 1 -\epsilon \ell^t \right) \\
  \end{align*}
  Now we can relate $\Phi^T$ to the total loss $L = \sum_{t=1}^T \ell^t$:
  \begin{align*}
    \Phi^T
      & = \Phi^0 \prod_{t=1}^T \left( 1 -\epsilon \ell^t \right) \\
      & \le n \prod_{t=1}^T \exp\left(-\epsilon \ell^t \right) \\
      & = n \exp\left(-\epsilon \sum_{t=1}^T \ell^t \right) \\
      & = n \exp\left(-\epsilon L\right) \\
  \end{align*}

  On the other hand, for any action $a\in A$ we have
  \[ (1-\epsilon)^{OPT} \le w^T(a) < \Phi^T \le n\exp(-\epsilon L) \]
  \[ OPT(-\epsilon - \epsilon^2/2) \le OPT\log(1 - \epsilon)
    \le \log n - \epsilon L \]
  \begin{align*}
    L & \le OPT + ({\epsilon}/{2}) OPT + \frac{\log n}{\epsilon} \\
      & \le OPT + (\sqrt{\log n/T}/{2}) T + \frac{\log n}{\sqrt{\log n/T}} \\
      & = OPT + \frac{3}{2} \sqrt{T\log n}
  \end{align*}

\end{proof}

Following experts online sounds great and all, but how is this actually useful
for proving theorems? Often time we are interested in the \emph{time averaged}
cost incurred, typically because that allows us to combine all of the
distributions $p^t$ into a distribution which is ``close to optimal'' in some
sense. The following restatement tells you ``how long you need to go'' to get
additive error $\epsilon$.

\begin{corollary}
  For any $\epsilon > 0$, if we run for $T = O(\log n / \epsilon^2)$ steps,
  multiplicative weights will have average regret $\epsilon$.
\end{corollary}

  \subsection{Swap regret}

  The first idea is to run several independent instances of low regret
  algorithm. We'll kind of see that each instance will correspond to a different
  action. However, perhaps the best way to see why this definition works comes
  out in the analysis: we need to align the sum of the costs incurred to the
  individual instances with the swap regret.

  Here's a description of the algorithm:
  Run independent instances of the multiplicative weights algorithm (or any
  low-regret online learning algorithm) $M_j$ for $j=1,\ldots,n = |A|$.
  At each time $t$, the algorithm:
  \begin{itemize}
    \item asks each learner for a distribution $q_j^t$ over actions
    \item calculates $p^t = P(q_1^t,\ldots,q_n^t)$ using some subroutine $P$,
      and acts according to $p^t$
    \item observes ``real'' cost $c^t$, but tells each learner $M_j$ that the
      cost incurred to $M_j$ is $p^t(j)c^t$
  \end{itemize}

  Individually, it is a bit difficult to interpret what each $M_j$ is trying to
  minimize: it's like $M_j$ gets punished, but only if the action he is
  associated with is likely played. Collectively, all the $M_j$s are trying to
  minimize $\sum_j p^t(j) c^t = c^t$, so at least that aligns with what we want.

  More formally, the algorithm looks like this:
  \begin{algorithm}
    \begin{algorithmic}[0]
    \State \textbf{Parameters:} $\epsilon > 0$, $T$
    \State Initialize $w^1_j(a) = 1$ for each $a\in A$ and $j=1,\ldots,n$
    \For {$t=1,\ldots,T$}
      \For {$j=1,\ldots,n$}
        \State Define a distribution $q_j^t$ proportional to $w_j^t$
      \EndFor
      \State Let $p^t = P(q^t_1,\ldots,q^t_n)$, and
        draw an action according to $p^t$
      % \State Define $p^t$ as follows: $p^t$ is any stationary distribution of
      %   the Mark chain whose states
      % \State \qquad are actions $j\in A$ and whose transition
      %   probabilities from $j$ to each $i\in A$ are $q_j^t(i)$
      \State Observe costs $c^t$
      \For {$j=1,\ldots,n$}
        \State Update weights $w^{t+1}_j(a) = w^t_j(a) (1 - \epsilon)^{p^t(j) c^t(a)}$
      \EndFor
    \EndFor
  \end{algorithmic}
  \end{algorithm}

  Let's dive into the analysis, during which we'll finally specify $P$:


\hypertarget{convex-hull}{%
\section{Convex Hull}\label{convex-hull}}

\hypertarget{introduction}{%
\subsection{Introduction}\label{introduction}}

Given a set of points P, the Convex Hull of P, denoted conv(P) is the
smallest closed simple polygon which contains all the points in P. It
can be visualized as a tightly snapped rubber band over the given set of
points.

We would like to compute the convex hull of given set of points by using
a divide and conquer technique. Later, we will also show that the
computational complexity of our algorithm is also O(n log n). Note that
we cannot do better than is for computing convex hull

\hypertarget{how-to-run}{%
\subsection{How to Run}\label{how-to-run}}

The src folder contains the source code for the convex hull program.
\texttt{g++} from the GNU compiler suite is required to compile the
program to a executable.

Steps to Compile:

\begin{enumerate}
\def\labelenumi{\arabic{enumi})}
\tightlist
\item
  \texttt{cd} into the src directory
\item
  Run \texttt{g++\ main.cpp} which generates an executable called
  \texttt{a.out} in the same directory
\item
  Run the executable using \texttt{./a.out} (on linux)

  \begin{enumerate}
  \def\labelenumii{\arabic{enumii})}
  \tightlist
  \item
    The executable takes a dataset from command line argument. For
    example, to use an existing dataset, run
    \texttt{./a.out\ ../datasets/edge.txt}
  \item
    If no command-line argument is given, it takes input from the shell
    directly (stdin)
  \end{enumerate}
\end{enumerate}

\hypertarget{input}{%
\subsection{Input}\label{input}}

The required file format for the algorithm to work correctly is:

\begin{itemize}
\tightlist
\item
  First line must contain the no of Points to be taken as input by the
  program.
\item
  Each of next line must contain 2 integers, space seperated denoting
  the (x, y) coordinates of each point.
\item
  Each coordinate must be of integer type in the range -10\^{}6 to
  10\^{}6.
\item
  No of coordinates must be less than 1 Billion.
\item
  \textbf{Note}: while using floating point datasets, the output
  coordinates might slightly differ as given coordinates are stored as
  floats.
\end{itemize}

\hypertarget{output}{%
\subsection{Output}\label{output}}

Each line of output contains the coordinates of points present on the
convex hull in clockwise order.\\
The last two lines of output show the time taken to take input in
microseconds and the time taken by the algorithm to compute convex hull
(also in microseconds).

Example output:

\includegraphics[width=8cm,height=8cm]{img/CH10.png}\\

\hypertarget{documentation-and-report}{%
\subsection{Documentation and Report}\label{documentation-and-report}}

Documentation of this algorithm, functions and classes can be found in
the \texttt{docs} folder in the current directory. Open the
\href{../ConvexHull/docs/html/index.html}{index.html} file from the docs
directory with your preferred browser to go through the documentation

\hypertarget{performance-analysis}{%
\subsection{Performance Analysis}\label{performance-analysis}}

Analysis is performed with a system running:

\begin{itemize}
\tightlist
\item
  OS: Arch Linux (64Bit) running Linux Kernel version 5.7.2
\item
  Processor: Intel Core i7 7700HQ
\item
  RAM: 8GB
\item
  Compiler: GNU G++ (GCC) 10.1.0
\end{itemize}

\textbf{The following observations are recorded:}

\begin{longtable}[]{@{}lcccc@{}}
\toprule
Filename & Input Dimentions & Output Diemntions & File read time &
Algorithm runtime\tabularnewline
\midrule
\endhead
edge.txt & 5 & 4 & 451 microsec & 8 microsec\tabularnewline
small.txt & 7 & 7 & 495 microsec & 16 microsec\tabularnewline
test.txt & 34 & 7 & 328 microsec & 56 microsec\tabularnewline
radial.txt & 3100 & 9 & 1348 microsec & 6.15 millisec\tabularnewline
\bottomrule
\end{longtable}

Randomly generated points (using python):

\begin{longtable}[]{@{}lcccc@{}}
\toprule
Filename & Input Dimentions & Output Diemntions & File read time &
Algorithm runtime\tabularnewline
\midrule
\endhead
10.txt & 10 & 5 & 314 microsec & 16 microsec\tabularnewline
20.txt & 20 & 7 & 495 microsec & 31 microsec\tabularnewline
50.txt & 50 & 9 & 427 microsec & 108 microsec\tabularnewline
100.txt & 100 & 13 & 348 microsec & 165 microsec\tabularnewline
500.txt & 500 & 13 & 572 microsec & 935 microsec\tabularnewline
1000.txt & 1000 (1K) & 18 & 510 microsec & 2.1 millisec\tabularnewline
3000.txt & 3000 (3K) & 24 & 2.49 millisec & 5.5 millisec\tabularnewline
5000.txt & 5000 (5K) & 22 & 2.12 millisec & 8.0 millisec\tabularnewline
10000.txt & 10000 (10K) & 23 & 3.26 millisec & 17.6
millisec\tabularnewline
50000.txt & 50000 (50K) & 28 & 17.4 millisec & 80.5
millisec\tabularnewline
100000.txt & 100000 (1L) & 31 & 39.3 millisec & 164.1
millisec\tabularnewline
250000.txt & 250000 (2.5L) & 31 & 78.1 millisec & 448.3
millisec\tabularnewline
\bottomrule
\end{longtable}

Using real world datasets:

\begin{longtable}[]{@{}lcccc@{}}
\toprule
Filename & Input Points & Output Diemntions & File read time & Algorithm
runtime\tabularnewline
\midrule
\endhead
subway-entrance-ny.txt & 1929 & 16 & 2.85 millisec & 3.81
millisec\tabularnewline
parking\_meter.txt & 15191 & 16 & 14.15 millisec & 27.04
millisec\tabularnewline
\bottomrule
\end{longtable}

Sources of datasets:

\begin{itemize}
\tightlist
\item
  \href{https://data.world/city-of-ny/5jsj-cq4s}{Parking meter dataset}
\item
  \href{https://data.world/new-york-city/subway-entrances}{New York
  Subway Entrance dataset}
\end{itemize}

It is highly difficult to calculate the time taken exactly up to the
microsecond, hence the value might vary on different executions. Also,
please note that the time calculated here may vary based on the system
load and other background applications.

\hypertarget{algorithm-approach}{%
\subsection{Algorithm Approach}\label{algorithm-approach}}

For more detailed information regarding the algorithm, refer to David
Mount Lecture notes from the course
\href{https://www.cs.umd.edu/class/spring2020/cmsc754/lectures.html}{CMSC
754 - Computational Geometry}

Given set of points P, we would like to first order them according to
the increasing x coordinate. For the sake of simplicity, let us assume
that no two points will have same x or y coordinate and no 3 points are
co-linear.

First, we will recursively compute the upper hull and then similarly
compute the lower hull of the given set of points. Finally, we will join
the upper hull with the lower hull to complete the convex hull and
return/print the clockwise ordering of the points present on the convex
hull

\textbf{Upper Hull Computation}

We divide the input set of points into 2 equal halves and recursively
compute the upper hull for both the half. Then, we compute the upper
tangent to both the hulls by traversing from the right on left hull and
traversing from left on the right hull. We perform orientation tests to
determine when the orientation changes on each hull respectively and
move upwards a point. We perform this operations until we reach the
upper part of both hulls.

All the traversed points on both hulls except the one which lead to
tangent are deleted (because all of them lie under tangent, hence lie
inside the polygon) and the resulting points of both hulls are catenated
to get the upper hull.

\hypertarget{results}{%
\subsection{Results}\label{results}}

The following is the computed convex hull of the
\href{./datasets/parking_meter.txt}{parking\_meter.txt} dataset:

\includegraphics[width=8cm,height=8cm]{img/CHparkingmeter.png}\\

The red boundary in the above image represents the convex hull given as
the output by the program. The blue points are the inputs given to the
program.

By analysis of the above divide and conquer algorithm, It can be
concluded that the algorithm runs in O(n log n) time complexity. From
the above results of testing of various datasets, we can see that the
time increase is proportional to the no of points.

\includegraphics[width=8cm,height=8cm]{img/CHradial.png}\\

The dataset \href{./datasets/radial.txt}{radial.txt} is a hand-crafted
dataset (3K points) which has high concentration of points
(\textasciitilde2.1K) in the center, whereas other randomly generated
datasets (for example: \href{./datasets/3000.txt}{3000.txt})have a
uniform distribution of points (the python random number generator uses
an underlying uniform distribution). From this we can assume that
independant of positioning of points, our algorithm takes approximately
the same time to compute the convex hull.

\hypertarget{conclusion}{%
\subsection{Conclusion}\label{conclusion}}

For the last dataset which contains 2.5L points, its worth noting that
the algorithm runs in just under 500ms which might appear to be fast.
But for applications like graphics, rendering and animations, this poses
a huge overhead in terms of repeated computation of convex hull for
various objects.

One observation we can make here is that the number of points which are
present on the convex hull is usually relatively lower most of the
times. So, using an output sensitive algorithms (Ex: Jarvis's march)
might help us if our application has fewer vertices on the convex hull.

It's worth noting that convex hull computation cannot be done under O(n
log n) which has been proved. Therefore, we can only hope to reduce the
constants of complexity. In our algorithm, we repeatedly recurse, hence
make many function calls which is computationally expensive. Hence by
using algorithms like Chan's or Grahm's, we can do better


\subsection{Input Circuit}
In Europe the mains voltage in houses is 230 volts, all of the input circuit before the transformer needs to be designed to that voltages.
As this part of the input circuit we want to besure that the noise on mains volts do not propegate to our output volts,
we are going to use a main emi filter to filter out some of the noise on the mains system.
This will also double as filter for any noise we are going to send out to the mains voltage.

As a means for input protection we are also going to use fuse (see formula \ref{eq:transfer-transformer} on page \pageref{eq:transfer-transformer}).

\subsubsection{Transformer}
The transfer formula for the transformer
\begin{align}\label{eq:transfer-transformer}
m &= \frac{I_s}{I_p} = \frac{V_p}{V_s}
\end{align}
For the input supply I am going to use a transformer 2x18V at 1.5A, that I already have. And I am going to connect the 18V's in series to get 36V at 1.5A. But for finding the transfer value we can use formula \ref{eq:transfer-transformer}.
\begin{align}
m = \frac{V_p}{V_s} = \frac{230V}{18V} = 12.78 \nonumber
\end{align}
 To find out which size of fuse that we are going to be using, we need to find out how much current that are going to be used on the main side of the transformer.
We are going rearrange in formula \ref{eq:transfer-transformer} as we know what $I_s$ is and we have calculated $m$.
\begin{align}
m = \frac{I_s}{I_p} \nonumber \\
\Rightarrow \nonumber \\
I_p \cdot m = I_p \cdot \frac{I_s}{I_p} = I_s \nonumber \\
\Rightarrow \nonumber \\
\frac{ I_p \cdot m}{m} = \frac{I_s}{m} \nonumber \\
\Rightarrow \nonumber \\
I_p = \frac{I_s}{m} \label{eq:found-Ip}
\end{align}
Now we can calculate $I_p$ with $I_s$ and $m$ from \ref{eq:found-Ip}.
\begin{align}
I_p = \frac{I_s}{m} = \frac{1.5A}{12.78} = 0.117A = \underline{\underline{117mA}} \nonumber
\end{align}
So the primary side of the transformer needs a fuse of 117mA,
but as I can not find that size, the one that is going to be used is a 125mA.
On the secondary side, I am going to use a 1,5A fuse.

\subsubsection{DC voltage}
Now we should calculate if we want to use 2x18V or put them in serial and get 36V.
One thing to keep in mind, is that when using a bridge rectifier the input frequency is doubled.
\paragraph{36 \vac}
The 36 volt ac are going to be converted to dc, via fullwave diode bridge.
Sinces the ac voltage specified on the transformer is RMS voltage, and not the peak voltage.
We can calculate what the dc voltage will become assuming no losses.
\begin{align} \label{eq:calc_vdc}
\text{V}_{ \text{DC} } &= \frac{ 2 \cdot \text{V}_{ \text{max} } }{ \pi } \\
         &= 0.637 \cdot \text{V}_{ \text{max} } \\
         &= 0.9 \cdot \text{V}_{ \text{RMS} } \\
         &= 0.9 \cdot 36 \text{V}_{\text{AC}}\nonumber \\
         &= 32.4 \text{V}_{ \text{DC} } \nonumber
\end{align}
But since we are going to use a bridge rectifier there is two times diode voltage drop that gives total drop of \(1.4V\).
Which meaning that we are properly only going to see around \( 31V \) if measure at the diode bridge output.
\paragraph{18 \vac}
\begin{align} \label{eq:calc_18vdc}
    \text{V}_{ \text{DC} }  &= \frac{ 2 \cdot \text{V}_{ \text{max} } }{ \pi } \\
             &= 0.637 \cdot \text{V}_{ \text{max} } \\
             &= 0.9 \cdot \text{V}_{ \text{RMS} } \\
             &= 0.9 \cdot 18 \text{V}_{ \text{AC} } \nonumber \\
             &= 16.2 \text{V}_{ \text{DC} } \nonumber
\end{align}
And again we need to take into account the diode drop, which gives 14.8V after the diode bridge.

\subsection{Ripple voltage}
The diode bridge does not create a dc voltage, but rather flips over the negativ half of the sinus wave to the positive. So there is a need for smoothering out the sinus wave to get a dc voltage.
We need to figure out how much ripple voltage, that we can get away with at this point. We are going to use formula \ref{eq:calc_vripple} to calculate our $V_{ripple}$.
\begin{align}\label{eq:calc_vripple}
\text{V}_{ \text{PP}_{\text{ripple} }} &= \frac{ \text{I}_{ \text{load} } }{ 2 \cdot f \cdot C } [V]
\end{align}
If we choose a big capacitor, then the time for it going to be discharge from fully charged is going to be long. As long there is voltage on the capacitor the rest of the circuit is working. At table \ref{tab:choose-c} I have made some calculation of a couple of capacitor that I can buy.
\begin{table}[ht]
\centering
\caption{Choosing smooting capacitor} \label{tab:choose-c}
\begin{tabular}{|c|r|r|r|r|}
\hline
C & $V_{ripple}$ \@ 1.5A & $V_{ripple}$ \@ 1A & $V_{ripple}$ \@ 500mA & $V_{ripple}$ \@ 10mA \\ \hline \hline
 47 uF  &  3.525 mV &  2.350 mV &  1.175 mV & 0.024 mV \\ \hline
100 uF  &  7.500 mV &  5.000 mV &  2.500 mV & 0.050 mV \\ \hline
120 uF  &  9.000 mV &  6.000 mV &  3.000 mV & 0.060 mV \\ \hline
150 uF  & 11.250 mV	&  7.500 mV &  3.750 mV & 0.075 mV \\ \hline
180 uF  & 13.500 mV &  9.000 mV &  4.500 mV & 0.090 mV \\ \hline
220 uF  & 16.500 mV & 11.000 mV &  5.500 mV & 0.110 mV \\ \hline
270 uF  & 20.250 mV & 13.500 mV &  6.750 mV & 0.135 mV \\ \hline
330 uF  & 24.750 mV & 16.500 mV &  8.250 mV & 0.165 mV \\ \hline
390 uF  & 29.250 mV & 19.500 mV &  9.750 mV & 0.195 mV \\ \hline
470 uF  & 35.250 mV & 23.500 mV & 11.750 mV	& 0.235 mV \\ \hline
560 uF  & 42.000 mV & 28.000 mV	& 14.000 mV & 0.280 mV \\ \hline
680 uF  & 51.000 mV & 34.000 mV & 17.000 mV & 0.340 mV \\ \hline
820 uF  & 61.500 mV & 41.000 mV & 20.500 mV & 0.410 mV \\ \hline
\end{tabular}
\end{table}


\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath}
\newcommand\numberthis{\addtocounter{equation}{1}\tag{\theequation}}

\usepackage{graphicx}
\usepackage{fullpage}
\title{ISE 538 Homework 1}
\author{Saket Choudhary\\skchoudh@usc.edu}

\begin{document}
\maketitle

\section*{Problem 1.31}
Let $D_i$ represent the score on $i^{th}$ dice.
Then the possible configurations for getting a sum 7 are: $(D_1, D_2): (1,6), (2,5), (3,4), (4,3), (5,2), (6,1) $

Thus $P(D_1=6|D_1+D_2=7) = \frac{1}{6}$
\section*{Problem 1.32}

Let
\begin{align*}
I_i &= \begin{cases}
1 & i^{th}\text{ person gets his hat back}\\
0 & \text{otherwise}
\end{cases}\\
P(\text{no person gets his hat back}) &= 1 - P(I_1 \cup I_2 \dots \cup I_n)\\
P(I_i=1) &= \frac{1}{n}\\
P(I_i=1, I_j=1) &= \frac{1}{n}\frac{1}{n-1} = \frac{1}{n(n-1)} = \frac{(n-2)!}{n!}\\
P(I_i=1, I_j=1) &= \frac{1}{n}\frac{1}{n-1}\frac{1}{n-2} = \frac{(n-3)!}{n!}\\
P(I_1 \cup I_2 \dots \cup I_n) &= \sum_{i=1}^{n}P(I_i) - \sum_{i<j}P(I_iI_j) + \sum_{i<j<k}P(I_iI_jI_k) - \dots - (-1)^n P(I_1I_2\dots I_n)\\
\end{align*}

The number of terms of type $P(I_iI_j) $ for $i<j$ are $n\choose{2}$, similarly of type $P(I_iI_jI_k)$ for $i<j<k$ are $n\choose{3}$ and so on

Thus,

\begin{align*}
P(I_1 \cup I_2 \dots \cup I_n) &= n \times \frac{1}{n} - {n\choose{2}} \times \frac{(n-2)!}{n!} + {n\choose{3}} \times \frac{(n-3)!}{n!} - \dots - (-1)^n {n\choose{n}} \times \frac{1}{n!}\\
&= 1 - \frac{1}{2!} + \frac{1}{3!} - \dots - (-1)^n \frac{1}{n!}\\
P(\text{no person gets his hat back}) &= 1 - (1 - \frac{1}{2!} + \frac{1}{3!} - \dots - (-1)^n \frac{1}{n!})\\
&= \frac{1}{2!} - \frac{1}{3!} + \dots + (-1)^n \frac{1}{n!}\\
\end{align*}

\section*{Problem 1.34}
Assuming that the red and black are equiprobable, choosing a red or black is equally rewarding.
$P(11 \text{consecutive black}) = (\frac{1}{2})^{11}$
$P(11^{th} \text{red} , \text{ last 10 black}) = \frac{1}{2}\frac{1}{2}^{10}$. The probability of observing $10$ blacks continuously is very low, having observed this it is actually likely that the system is biased towards black hits and hence the bet should probably be on a black rather than a red.

\section*{Problem 1.35}
\textbf{(a)} H,H,H,H: $(\frac{1}{2})^4$

\textbf{(b)} T,H,H,H: $(\frac{1}{2})^4$

\textbf{(c)} T,H,H,H occurs before H,H,H,H: The only possible way for $H,H,H,H$ to occur first is that first four flips are heads. And hence the required probability is $1-\frac{1}{2}^4 = \frac{15}{16}$

\section*{Problem 1.36}
Box1 : $1B, 1W$

Box2: $2B, 1W$

$P(B) = \frac{1}{2} \times \frac{1}{2} + \frac{1}{2} \times \frac{2}{3} = \frac{7}{12}$

\section*{Problem 1.37}

$P(Box_1|W) = \frac{P(W,Box1)}{P(W)} = \frac{P(W|Box1)P(Box1)}{P(W)} = \frac{\frac{1}{2}\frac{1}{2}}{1-\frac{7}{12}} = \frac{3}{5}$

\section*{Problem 1.44}
$P(tails|W) = \frac{P(tails)P(W|tails)}{P(W)}$

$P(W) = P(W|heads)P(heads) + P(W|tails)P(tails) = \frac{5}{12}\frac{1}{2} + \frac{3}{15}\frac{1}{2} = \frac{37}{120}$

Thus, $P(tails|W) = \frac{\frac{1}{2}\frac{3}{15}}{\frac{37}{120}} = \frac{12}{37}$

\section*{Problem 1.45}
$P(B1|R2) = \frac{P(R2|B1)P(B1)}{P(R2)}$
\begin{align*}
P(B1) &= \frac{b}{b+r}\\
P(R2) &= P(R2|B1)P(B1) + P(R2|R1)P(R1)\\
&= \frac{r}{r+b+c}\frac{b}{b+r}+ \frac{r+c}{r+b+c}\frac{r}{b+r}\\
&= \frac{br+r^2+rc}{(b+r)(b+r+c)}\\
P(B1|R2) &= \frac{\frac{r}{r+b+c}\frac{b}{b+r}}{\frac{br+r^2+rc}{(b+r)(b+r+c)}}\\
&= \frac{br}{br+r^2+rc}\\
&= \frac{b}{b+r+c}
\end{align*}

\section*{Problem 1.46}
Let's assume WLOG, that the jailer tells $C$ will be set free.
Then $P(\text{C free} | \text{A dies}) = \frac{1}{2}$ because is A is to die the jailer could have also named $B$ with equal probability.
\begin{align*}
P(\text{A dies} | \text{C free}) &= \frac{P(\text{C free}| \text{A dies})P(\text{A dies})}{P(\text{C free})}\\
&= \frac{P(\text{C free}| \text{A dies})P(\text{A dies})}{P(\text{C free} | \text{A dies}) P(\text{A dies}) + P(\text{C free} | \text{B dies}) P(\text{B dies}) + P(\text{C free} | \text{C dies}) P(\text{C dies}) }\\
&= \frac{\frac{1}{2} \frac{1}{3}}{1\frac{1}{3} + 1\frac{1}{3} + 0}\\
&= \frac{1}{3}
\end{align*}

So the jailer is wrong. The probability of $A$ being executed remains the same.

\section*{Problem 1.47}
\begin{align*}
0 \leq P(A|B) \leq 1\\
P(\Omega | B) = \frac{P(\Omega \cap B)}{P(B)} = \frac{P(B)}{P(B)} = 1\\
\end{align*}

For the third property, let's consider disjoint events $A$ and $C$
then
\begin{align*}
P(A \cup C | B) & = \frac{P((A \cup C)B)}{P(B)}\\
&= \frac{P(AB \cup CB)}{P(B)}\\
&= \frac{P(AB)+P(CB)}{P(B)}\\
&= P(A|B)+P(C|B)
\end{align*}

Now,
\begin{align*}
P(A|BC)P(C|B) + P(A|BC^c)P(C^c|B)
&= \frac{P(ABC)}{P(BC)}\frac{P(BC)}{P(B)} + \frac{P(ABC^c)}{P(BC^c)}\frac{P(BC^c)}{P(B)}\\
&= \frac{P(ABC)}{P(B)} + \frac{P(ABC^c)}{P(B)}\\
&= P(AC|B) + P(AC^c|B)\\
&= P(AC \cup AC^c|B)\\
&= P(A|B)
\end{align*}

\end{document}


\documentclass{article}

%% Language and font encodings
%\usepackage[english]{babel}
%\usepackage[utf8x]{inputenc}
%\usepackage[T1]{fontenc}

%% Sets page size and margins
\usepackage[margin=1in]{geometry}

%% Useful packages
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage[colorinlistoftodos]{todonotes}
%\usepackage{mathrsfs}
\usepackage{amsfonts,amssymb}
\usepackage[colorlinks=true, allcolors=blue]{hyperref}
\usepackage{parskip}
\setlength{\parindent}{0pt}

\title{Homework 2}
\author{Group L\\
Yang Li, Jimmy Jusuf, Zhirong Zhang, Tiangang Zhang}

\begin{document}
\maketitle

\section{Problem 1}
\begin{eqnarray*}
L(x,y,\lambda)=(x-a)^2+(y-b)^2-\lambda_1 x+\lambda_2 (x-1)-\lambda_3 y +\lambda_4 (y-1)
\end{eqnarray*}
Then local minimizer should subject to

\begin{equation}
\left\{
             \begin{array}{ll}
             2(x^*-a)-\lambda_1^*+\lambda_2^*=0\\
             2(y^*-a)-\lambda_3^*+\lambda_4^*=0\\
             \lambda_1^* x^*=0\\
             \lambda_2^*(x^*-1)=0\\
             \lambda_3^* y^*=0\\
             \lambda_4^*(y^*-1)=0\\
             \lambda_i^* \geq 0 & \textnormal{for~} i=1,2,3,4
             \end{array}
\right.
\end{equation}

Solve the equations above, the solution is
\begin{equation}
x^*=
\left\{
             \begin{array}{ll}
             0 & a\leq 0\\
             a & a \in (0,1) \\
             1 & a \geq 1
             \end{array}
\right.
\end{equation}
\begin{equation}
y^*=
\left\{
             \begin{array}{ll}
             0 & b\leq 0\\
             b & b \in (0,1) \\
             1 & b \geq 1
             \end{array}
\right.
\end{equation}

\section{Problem 2}
We present two slightly different solutions below.

\subsection{Solution 1}
We replace constraints of the form
\begin{equation} |x_i| \leq b_i \end{equation}
with
\begin{equation}
\begin{split}
	x_i \leq b_i \\
	-x_i \leq b_i
\end{split}
\end{equation}

%To deal with the absolute value in the objective function, we introduce a split variable
%and a free variable $U$,
%\[ y = y^+ - y^- \textnormal{~with constraints~} y^+, y^- \geq 0; y^+ \geq U, y^- \geq -U \]
To deal with the absolute value in the objective function, we introduce a new variable $U$, such that
\[ x_2 -10 \leq U \]
\[ 10 - x_2 \leq U \]

%Note that $|y| = y^+ + y^-$ because by construction, for any value of $U$, $y^+ * y^- = 0,   y^+, y^- \geq 0$.
Note that $U \geq |x_2 -10|$ always holds.

%Thus,
%\begin{gather}
%x_2 - 10 = y \implies x_2 = y + 10 = y^+ - y^- + 10 \\
%|x_2 - 10| = |y| = y^+ + y^- \\
%\end{gather}

By change of variables and substitutions above, the nonlinear problem
\[ \min 2 x_1 + 3|x_2 - 10| \quad \textnormal{subject to~} |x_1 + 2| + |x_2| \leq 5 \]
can be rewritten as LP problem
\begin{equation}
%\min \, 2x_1+3(y^+ + y^-), \textnormal{ subject to}
\min \, 2x_1+3U, \textnormal{ subject to}
\left\{
    \begin{array}{l}
    x_1 + 2 + y^+ - y^- + 10 \leq 5 \\
    -x_1 - 2 + y^+ - y^- + 10 \leq 5 \\
    x_1 + 2 - y^+ + y^- - 10 \leq 5 \\
    -x_1 - 2 - y^+ + y^- - 10 \leq 5 \\
%     y^+, y^- \geq 0
    x_2 -10 \leq U \\
    10 - x_2 \leq U
    \end{array}
\right.
\end{equation}

\subsection{Solution 2}
Let $x_2-10=x^+-x^-,\, x^+,x^-\geq 0$, use the property of absolute value inequalities, the problem become
\begin{equation}
\min \, 2x_1+3|x^+-x^-|, \textnormal{ subject to}
\left\{
             \begin{array}{ll}
             x_1+2+10+x^+-x^-\leq 5\\
             -x_1-2+10+x^+-x^-\leq 5\\
             x_1+2-10-x^++x^-\leq 5\\
             -x_1-2-10-x^++x^-\leq 5\\
             x^+,x^-\geq 0
             \end{array}
\right.
\end{equation}

Notice that
\begin{equation}
2x_1+3(x^++x^-) \geq 2x_1+3|x^+-x^-|
\end{equation}
now we consider
\begin{equation}
\min \,2x_1+3(x^++x^-) , \textnormal{ subject to}
\left\{
             \begin{array}{ll}
             x_1+2+10+x^+-x^-\leq 5\\
             -x_1-2+10+x^+-x^-\leq 5\\
             x_1+2-10-x^++x^-\leq 5\\
             -x_1-2-10-x^++x^-\leq 5\\
             x^+,x^-\geq 0
             \end{array}
\right.
\end{equation}
Let $x_1^*,x^{+*},x^{-*}$ be the minimizer of the problem above, the next step is to prove that  $x_1^*,x^{+*},x^{-*}$ is the minimizer of the original problem, we prove by contradiction.
If there is $x_1',x_2'$ subject to the conditions and
\begin{equation}
2x_1'^*+3|x_2'-10| < 2x_1^*+3(x^{+*}+x^{-*})
\end{equation}
Let
\begin{eqnarray*}
x_{1\star}=x_1'\\
x^{+}_\star=\{x_2'-10\}^+\\
x^{^-}_\star=\{-x_2'+10\}^+
\end{eqnarray*}
Then
\begin{equation}
2x_{1\star}+3(x^{+}_\star+x^{^-}_\star) < 2x_1^*+3(x^{+*}+x^{-*})
\end{equation}
Contradiction!

Recall equation(5) we know that
\begin{equation}
2x_1'^*+3|x_2'-10| = 2x_1^*+3(x^{+*}+x^{-*})
\end{equation}
So we can always use the minimizer of problem (6) to get the minimizer and minimum value of the original problem. Then problem has been transferred into a LP
\begin{equation}
\min \,2x_1+3(x^++x^-) , \textnormal{ subject to}
\left\{
             \begin{array}{ll}
             x_1+2+10+x^+-x^-\leq 5\\
             -x_1-2+10+x^+-x^-\leq 5\\
             x_1+2-10-x^++x^-\leq 5\\
             -x_1-2-10-x^++x^-\leq 5\\
             x^+,x^-\geq 0
             \end{array}
\right.
\end{equation}

\section{Problem 3}
\newcommand{\ccy}[1]{currency#1}

There are $N$ currencies, index $i=1,\dots,N$.

We start with $B$ units of currency 1 and we want to maximize the number
of units of currency $N$ through a sequence of trades. One unit of
currency $i$ can be exchanged for $f_{ij}$ units of currency $j$.

Decision variables: let $x_{ij} =$ amount of currency $i$ to be converted
into currency $j$. Clearly $\sum_{j\not= 1}x_{1j}\leq B$. We also have
constraint $\sum_{j\not= 1}x_{1j}\leq u_1$, since we exchange out of
currency 1 exactly once into currency $j$ ($j \not= 1,N$),
where $u_i$ is the limit on the total amount of currency $i$ that can be
traded. Exchanging any currency into currency $N$ count as a transaction:
$\sum_{i\not= N}x_{iN}\leq u_N$.
If we exchange out of currency 1 into currency $N$ through
intermediate currency $j$, the trading limit becomes
$(\sum_{j \not= 1,N}x_{ij} + \sum_{k \not= 1,N}x_{jk}) \leq u_j$, for $j \not= 1,N$.

Objective function: maximize $\sum_{i \not= N} f_{iN} * x_{iN}$, essential
maximizing the total inflow into currency $N$ from all other currencies.
The constraints are presented next.
No outflow from currency $N$ into other currencies: $x_{Nj}=0$ for $j=1,\dots,N$.
To prevent exchanging out of currency $i$ into the same currency,
we impose constraint $x_{ii}=0$ for $i=1,\dots,N-1$; otherwise $x_{ij}$ can be
any non-negative value
% To ``force'' trading progress towards currency $N$ and avoid ``loops'', we let
$x_{ij}\geq 0$ for $i=1,\dots,N, i\not= j$ and for $i=j=N$.
% and $x_{ij}=0$ for $i=1,\dots,N, j<i$.

The no-arbitrage condition
$f_{i_1 i_2}\,f_{i_2 i_3}\dots f_{i_{k-1} i_k}\,f_{i_k i_1}\leq 1 $ means
that if we start with any currency, continually exchange one currency into another
currency, and eventually back into the original currency, we can never end up
with more units of the original currency, i.e. if we start with 1 unit, at
most we get back 1 unit, or could be less than 1, after exchanging into a series of
other currencies. It also implies there can be no trading ``loops'' or trading
sequences in which one can increase currency count beyond its initial allocation
in this economy.
The trade from one currency to another currency happens at most for one time.

% According to no-arbitrage condition, there is no need to trade a
% currency back to the currency it traded from.
In summary, the LP formulation of this problem is
\begin{equation}
\max \,\sum_{i \not= N} f_{iN} * x_{iN}, \text{ subject to}
\left\{
             \begin{array}{ll}
             \sum_{j\not= 1}x_{1j}\leq B,\\
             \sum_{j\not= 1}x_{1j}\leq u_1,\\
             \sum_{i\not= N}x_{iN}\leq u_N,\\
             \sum_{j\not= 1, i}x_{ij}\leq u_i,& \textnormal{for~} i\not= 1,N\\
             \sum_{j\not= 1, i}x_{ij}\leq \sum_{k\not=i,N}f_{ki} x_{ki},& \textnormal{for~} i\not= 1,N\\
             x_{ij}\geq 0, & \textnormal{for~} i\not= j,i\not=N\\
             x_{ij}=0, & \textnormal{for~} i= j,i=N
             \end{array}
\right.
\end{equation}
\end{document}