%!TEX root = ../../dissertation.tex

\section{Programming languages in economics}

In this section, we provide a brief description of the three programming
languages discussed in this paper, MATLAB, Python and Julia. We provide step-by-step
instructions of how to install Julia and Python and a description of the accompanying
software in our corresponding notebooks; see \href{http://notes.quantecon.org}{QuantEcon Notebook site}.

\subsection{Matlab}

In our experience, Matlab is the programming language that most economists
currently use. The most compelling reason for this is that it is a
\textquotedblleft batteries included\textquotedblright solution. What we mean
by \textquotedblleft batteries included\textquotedblright is that Matlab
provides a code editing and execution environment, plotting capabilities, and a
vast catalog of pre-written numerical routines. For example, in this project,
we were able to leverage built-in routines to construct Sobol sequences
(quasi-Monte Carlo grids) and for doing numerical optimization. Another benefit
is that, because so many other economists already use Matlab, collaborating on
research code with others is efficient. There are two main examples of this.
The first is that Dynare, which is mostly used via it's Matlab interface, has become widely used
across academia and policy institutions facilitating communication and sharing
of models. The second is that because many algorithms and routines have been
previously written in Matlab, it should be easy for researchers to adapt these
pieces of code to new projects.

Matlab, however, is not a perfect language for economists. Some of its
downsides are:

\begin{itemize}

  \item \textit{Commercial.} As a commercial product, in order to use Matlab
  users must first obtain a non-free license. This can be costly if an
  institution does not already have a license available for use.

  \item \textit{Closed source.} Users cannot look at the Matlab source code to
  study (or change) the implementation of built-in routines.

  \item \textit{Restricted parallel programming possibilities (due to limited
  license availability).} As computer hardware becomes more powerful and
  computer resources in the cloud become more accessible, economists are
  increasingly implementing parallel versions of their algorithms. While basic
  parallelism is convenient in Matlab, running code at scale requires one
  license per process. This increases the cost of large scale computations.
  \footnote{ As an example, if one wanted to run a Matlab program on 16
  optimized processors on the Amazon Compute Cloud, then in addition to paying
  \$0.80/hour for the processor time, it would cost an additional \$0.20/hour
  per processor to license each processor bringing the total from \$0.80/hour
  to \$4.00/hour. For details see
  \href{https://www.mathworks.com/products/parallel-computing/parallel-computing-on-the-cloud/distriben-ec2-pricing.html}
  {Matlab's pricing site} and
  \href{https://aws.amazon.com/ec2/pricing/on-demand/} {Amazon's pricing
  site}.}

  \item \textit{Limited notion of community packages.} Apart from the official Matlab toolboxes,
  Matlab does not have a well-developed system of add-on packages.\footnote{Matlab does however host
  an open file exchange called {Matlab Central}. Matlab Central has 365,000 current contributors and
  code that is available to do many tasks that are not included in the base Matlab package, but they
  do not provide the same package management services that are available within Python or Julia.}
  Using external libraries requires users to download and place the files in the same directory as
  the scripts they are running or to manage the path Matlab searches when calling functions. Although
  not insurmountable, this friction discourages good software development by making it difficult to
  build and reuse modular libraries.

  \item \textit{Mainly a numerical programming language.} Tasks outside of this
  purview can be more difficult than in a more general programming language.

\end{itemize}

\subsection{Python}

While Matlab has long been widely used in economics, Python has been steadily
gaining users in economics during recent years. Some of Python's most
compelling selling points are as follows:

\begin{itemize}

  \item \textit{A mature and rich package ecosystem}. At the time of writing,
  Python has 112,232 registered packages that perform a variety of tasks and can
  be easily downloaded and installed.

  \item \textit{Advanced data manipulation and numerics libraries.} For example,
  \texttt{pandas} is a very powerful library containing data structures and
  algorithms for doing data analysis. \texttt{numpy} and \texttt{scipy} are the
  base of array-based numerical computing in Python and provide linear algebra,
  optimization, and other foundational numerical algorithms. \texttt{matplotlib}
  is the main plotting library.

  \item \textit{Many users and ample learning resources.} Python is one of the
  top five most widely used programming languages in the world. This large user
  base is beneficial to economists because of the libraries mentioned above and
  the sheer volume of learning materials and resources available online for
  learning Python. As one point of data, at the time of writing there are
  696,984 questions tagged with Python on
  \url{http://stackoverflow.com}\footnote{ This is a popular question-and-answer website for programming.}, while there are only 68,403 for Matlab and
  2,659 for Julia.

  \item \textit{Wide adoption in the private sector.} This is beneficial
  because industry resources are being applied to enhance the language and
  create powerful tools economists can leverage in their computational code.

  \item \textit{Open source.} This means that the source code is publicly
  available on the internet and it is 100 percent free to view, install,
  distribute and use.

\end{itemize}

Python is not perfect, however. Some of its shortcomings are:

\begin{itemize}

  \item \textit{External libraries.} External libraries are needed for
  efficient numerical computation. This means that users need to identify which
  libraries to use, know how to install them properly, and load them into their
  codes. Fortunately this is not as difficult as it sounds thanks to scientific
  python distributions like \href{https://www.continuum.io/downloads}
  {Anaconda} that come with Python and all the most common scientific packages
  installed and pre-configured.

  \item \textit{Slower than other languages for certain types of algorithms.}
  This can often be circumvented by vectorizing operations (as in Matlab) or by
  leveraging tools such as \href{http://numba.pydata.org}{numba} or
  \href{http://cython.org} {Cython} to compile Python code into a fast and
  efficient machine code. In many cases this is a suitable approach, but it
  does require extra effort on behalf of the programmer. In other cases, it is
  difficult or impossible to express only the slower parts of an algorithm
  using these tools.

\end{itemize}

\subsection{Julia}

The most recently released language of the three is Julia. While Julia has not
yet reached a stable 1.0 release, many enthusiastic economists are starting to
pick it up because of the benefits that it offers. The list of Julia's
strengths is extensive and includes the following:

\begin{itemize}

  \item \textit{All of Julia is based around just-in-time (JIT) compilation}\footnote{Just-in-time
  compilation means that a function is compiled the first time it is called and
  all subsequent calls to that function are calls to faster.}. Since all Julia
  code is compiled before it is executed, Julia has the potential to run at the
  same speed as languages like C or Fortran.\footnote{ See
  \url{https://julialang.org/benchmarks/} for some example benchmarks.}
  \footnote{ There has been work to add just-in-time compilation to both Matlab
  and Python, but, because it isn't native to either language, it does not work
  as generically as it does in Julia.}

  \item \textit{Most of Julia is written in Julia.} This offers at least two
  main benefits: (1) core language features and constructs are written and
  defined using the same tools available to user code; (2) users can look to the
  implementation of Julia itself to see best practices and tips to make their
  own code better. One consequence of this that is often surprising to Matlab
  or Python users is that a Julia user can write a routine in Julia that
  performs as well, or often better, than implementations in common libraries.
  In other words, Julia users do not need to reach to a library for every
  operation, because writing the subroutine in Julia can be at least as
  performant.

  \item \textit{Flexibility. }Of the three languages that we analyze, Julia is
  the most expressive and flexible. It features advanced computer science
  concepts such as meta-programming (see below) and multiple dispatch (a choice
  of which method to execute when a function is applied; such a choice is made
  using all of a function's arguments, not just the first one).

  \item \textit{Similarity to Matlab}. Julia's syntax is intentionally similar
  to that of Matlab. In fact, in many instances, copying and pasting Matlab
  code produces the same result in Julia as in Matlab. This was an intentional
  decision intended to allow Matlab users to transition seamlessly from Matlab
  to Julia.

  \item \textit{Built in parallel and multi-threaded programming constructs.}
  Having support at the language level for both distributed and shared memory
  parallel processing means running code in serial or in parallel is often a
  one word change to the user's code. Also, because Julia is open source like
  Python, its programs can scale up to run on arbitrarily many processes
  without incurring the additional costs from licensing.

  \item \textit{Meta-programming.} One relatively advanced and compelling
  feature that is unique to Julia (amongst the languages considered here) is
  meta-programming. A language that allows meta-programming treats the code
  that is executed as data that can be manipulated by the program itself. This
  means users can write code that writes the code that will be run. The most
  complete example of how we leveraged this feature for this project was in
  implementing routines to compute basis functions for complete polynomial. In
  Python and Matlab, our implementation was quite naive. We allow for a
  complete polynomial of degree between two and five and write out one \texttt{
  for} loop for each degree. The structure of each loop is identical. In
  contrast, the Julia's version of this routine allows an arbitrary degree
  complete polynomial. To achieve this, we leverage Julia's meta-programming
  capabilities, receive the desired degree of complete polynomial from the user
  and then instruct Julia to emit code that contains that many for loops --
  each having the same structure as the hand-written loops in our Python and
  Matlab versions. This results in the complete polynomial Julia routines
  having less code written by us and being more flexible and generic.

\end{itemize}

Despite many promising features, there are still some shortcomings to Julia.
These include:

\begin{itemize}

  \item \textit{Lack of stability.} Julia is still very young, it was first
  made public in 2011 and has not reached a stable 1.0 release.\footnote{ The
  Julia team anticipates releasing version 1.0 in Q2 2018.}

  \item \textit{Slow compile times.} Because the JIT compiler aggressively
  optimizes the machine code for each function that is executed, it can feel
  sluggish when doing rapid iterative development (make a change, run code,
  make a change, run code, etc\ldots ) because you pay the fixed compile cost
  after each edit.

  \item \textit{Julia must be able to infer a type for each variable in a
  function }in order to emit fast code\textit{.} This is not usually a problem,
  but sometimes if you are careless then the code will run slower than it
  should ---{\ though, even in the worst case, it is still often as fast as Matlab or Python}.

  \item \textit{The package system is still emerging.} Many of the packages
  throughout the Julia community are surprisingly advanced given that Julia has
  still not reached a stable release, but they are usually not as polished as
  what you would find in the Matlab sponsored toolboxes or the best Python
  packages. That being said, we have not found package availability to be
  prohibitive for our work.\footnote{ When routines are not available, it often
  isn't much extra work to code up an implementation by hand; see the
  discussion above.}

  \item \textit{Lack of tooling.} Unlike the more mature languages Matlab and
  Python, Julia currently lacks polished developer tools like an integrated
  development environment (IDE) or debugger. These are actively being worked on
  and will hopefully emerge soon after the Julia 1.0 release.

\end{itemize}

\subsection{Syntax comparison}

Julia and Matlab have many syntax elements in common. For example, if one wants
to perform matrix multiplication of two matrices $A$ and $B$ then one can
simply write \texttt{A*B}, but if one wants to perform element wise
multiplication then one writes \texttt{A.*B}. Additionally, blocks are started
with the same words (\texttt{for}, \texttt{if}, \texttt{while}) and terminated
with the word \texttt{end}. These similarities make it very easy to translate
code from Matlab to Julia and vice-versa. The one major difference between
Julia and Matlab is that Julia uses square brackets to index into arrays
(\texttt{A[i, j]}) while Matlab uses parenthesis (\texttt{A(i, j)}).

On the other hand, Python differs slightly in its conventions. In Python
\texttt{A*B} represents element-wise multiplication between matrices while
\texttt{A@B} represents matrix multiplications.\footnote{ The use of \texttt{@}
for matrix multiplication was added in Python version 3.5. Readers who have
seen or written Python code written for Python versions earlier than version
3.5 might have come across \texttt{A.dot(B)} or \texttt{np.dot(A, B)} instead.}
Additionally, rather than depend on \texttt{end} to denote the end of blocks,
Python forces each new block to be indented four spaces from the previous one.
Despite these differences, we found that syntax differences did not present a
large obstacle for writing code in all three languages.


\chapter{Procedures}\ttindex{PROCEDURE}

It is often useful to name a statement for repeated use in calculations
with varying parameters, or to define a complete evaluation procedure for
an operator. {\REDUCE} offers a procedural declaration for this purpose. Its
general syntax is:
\begin{verbatim}
  [<procedural type>] PROCEDURE <name>[<varlist>];<statement>;
\end{verbatim}
where
\begin{verbatim}
        <varlist> ::= (<variable>,...,<variable>)
\end{verbatim}
This will be explained more fully in the following sections.

In the algebraic mode of {\REDUCE} the {\tt <procedure type>} can be
omitted, since the default is {\tt ALGEBRAIC}.  Procedures of type {\tt
INTEGER} or {\tt REAL} may also be used.  In the former case, the system
checks that the value of the procedure is an integer.  At present, such
checking is not done for a real procedure, although this will change in
the future when a more complete type checking mechanism is installed.
Users should therefore only use these types when appropriate.  An empty
variable list may also be omitted.

All user-defined procedures are automatically declared to be operators.

In order to allow users relatively easy access to the whole {\REDUCE} source
program, system procedures are not protected against user redefinition. If
a procedure is redefined, a message
\begin{verbatim}
        *** <procedure name> REDEFINED
\end{verbatim}
is printed. If this occurs, and the user is not redefining his own
procedure, he is well advised to rename it, and possibly start over
(because he has {\em already\/} redefined some internal procedure whose correct
functioning may be required for his job!)

All required procedures should be defined at the top level, since they
have global scope throughout a program. In particular, an attempt to
define a procedure within a procedure will cause an error to occur.

\section{Procedure Heading}\index{Procedure heading}

Each procedure has a heading consisting of the word {\tt PROCEDURE}
(optionally preceded by the word {\tt ALGEBRAIC}), followed by the name of
the procedure to be defined, and followed by its formal parameters -- the
symbols that will be used in the body of the definition to illustrate
what is to be done.  There are three cases:
\begin{enumerate}
\item No parameters. Simply follow the procedure name with a terminator
(semicolon or dollar sign).
\begin{verbatim}
        procedure abc;
\end{verbatim}

When such a procedure is used in an expression or command, {\tt abc()}, with
empty parentheses, must be written.

\item One parameter.  Enclose it in parentheses {\em or\/} just leave at
least one space, then follow with a terminator.
\begin{verbatim}
        procedure abc(x);
\end{verbatim}
or
\begin{verbatim}
        procedure abc x;
\end{verbatim}

\item More than one parameter. Enclose them in parentheses, separated by
commas, then follow with a terminator.
\begin{verbatim}
        procedure abc(x,y,z);
\end{verbatim}
\end{enumerate}
Referring to the last example, if later in some expression being evaluated
the symbols {\tt abc(u,p*q,123)} appear, the operations of the procedure
body will be carried out as if {\tt X} had the same value as {\tt U} does,
{\tt Y} the same value as {\tt p*q} does, and {\tt Z} the value 123.  The
values of {\tt X}, {\tt Y}, {\tt Z}, after the procedure body operations
are completed are unchanged.  So, normally, are the values of {\tt U},
{\tt P}, {\tt Q}, and (of course) 123. (This is technically referred to as
call by value.)\index{Call by value}

The reader will have noted the word {\em normally\/} a few lines earlier. The
call by value protections can be bypassed if necessary, as described
elsewhere.

\section{Procedure Body}\index{Procedure body}

Following the delimiter that ends the procedure heading must be a {\em
single} statement defining the action to be performed or the value to be
delivered.  A terminator must follow the statement.  If it is a semicolon,
the name of the procedure just defined is printed.  It is not printed if a
dollar sign is used.

If the result wanted is given by a formula of some kind, the body is just
that formula, using the variables in the procedure heading.

{\it Simple Example:}

If {\tt f(x)} is to mean {\tt (x+5)*(x+6)/(x+7)}, the entire procedure
definition could read
\begin{verbatim}
        procedure f x; (x+5)*(x+6)/(x+7);
\end{verbatim}
Then {\tt f(10)} would evaluate to 240/17, {\tt f(a-6)} to
{\tt A*(A-1)/(A+1)}, and so on.

{\it More Complicated Example:}

Suppose we need a function {\tt p(n,x)} that, for any positive integer
{\tt N}, is the Legendre polynomial\index{Legendre polynomials} of order
{\em n}. We can define this operator using the
textbook formula defining these functions:
\begin{displaymath}
p_n(x) = \displaystyle{1\over{n!}}\
\displaystyle{d^n\over dy^n}\ \displaystyle{{1\over{(y^2 - 2xy + 1)
^{{1\over2}}}}}\Bigg\vert_{y=0}
\end{displaymath}
Put into words, the Legendre polynomial $p_n(x)$ is the result of
substituting $y=0$ in the $n^{th}$ partial derivative with respect to $y$
of a certain fraction involving $x$ and $y$, then dividing that by $n!$.

This verbal formula can easily be written in {\REDUCE}:
\begin{verbatim}
        procedure p(n,x);
           sub(y=0,df(1/(y^2-2*x*y+1)^(1/2),y,n))
               /(for i:=1:n product i);
\end{verbatim}
Having input this definition, the expression evaluation
\begin{verbatim}
        2p(2,w);
\end{verbatim}
would result in the output
\begin{verbatim}
           2
        3*W  - 1 .
\end{verbatim}
If the desired process is best described as a series of steps, then a group
or compound statement can be used.
\extendedmanual{\newpage}
{\it Example:}

The above Legendre polynomial example can be rewritten as a series of steps
instead of a single formula as follows:
\begin{verbatim}
        procedure p(n,x);
          begin scalar seed,deriv,top,fact;
               seed:=1/(y^2 - 2*x*y +1)^(1/2);
               deriv:=df(seed,y,n);
               top:=sub(y=0,deriv);
               fact:=for i:=1:n product i;
               return top/fact
          end;
\end{verbatim}
Procedures may also be defined recursively.  In other words, the procedure
body\index{Procedure body} can include references to the procedure name
itself, or to other procedures that themselves reference the given
procedure.  As an example, we can define the Legendre polynomial through
its standard recurrence relation:
\begin{verbatim}
        procedure p(n,x);
           if n<0 then rederr "Invalid argument to P(N,X)"
            else if n=0 then 1
            else if n=1 then x
            else ((2*n-1)*x*p(n-1,x)-(n-1)*p(n-2,x))/n;
\end{verbatim}

The operator {\tt REDERR}\ttindex{REDERR} in the above example provides
for a simple error exit from an algebraic procedure (and also a block).
It can take a string as argument.

It should be noted however that all the above definitions of {\tt p(n,x)} are
quite inefficient if extensive use is to be made of such polynomials, since
each call effectively recomputes all lower order polynomials. It would be
better to store these expressions in an array, and then use say the
recurrence relation to compute only those polynomials that have not already
been derived. We leave it as an exercise for the reader to write such a
definition.

\section{Using LET Inside Procedures}

By using {\tt LET}\ttindex{LET} instead of an assignment in the procedure
body\index{Procedure body} it is possible to bypass the call-by-value
\index{Call by value} protection.  If {\tt X} is a formal parameter or local
variable of the procedure (i.e. is in the heading or in a local
declaration), and {\tt LET} is used instead of {\tt :=} to make an
assignment to {\tt X}, e.g.

\begin{verbatim}
        let x = 123;
\end{verbatim}
then it is the variable that is the value of {\tt X} that is changed.
This effect also occurs with local variables defined in a block.  If the
value of {\tt X} is not a variable, but a more general expression, then it
is that expression that is used on the left-hand side of the {\tt LET}
statement.  For example, if {\tt X} had the value {\tt p*q}, it is as if
{\tt let p*q = 123} had been executed.

\section{LET Rules as Procedures}

The {\tt LET}\ttindex{LET} statement offers an alternative syntax and
semantics for procedure definition.

In place of
\begin{verbatim}
        procedure abc(x,y,z); <procedure body>;
\end{verbatim}
one can write
\begin{verbatim}
        for all x,y,z let abc(x,y,z) = <procedure body>;
\end{verbatim}
There are several differences to note.

If the procedure body contains an assignment to one of the formal
parameters, e.g.
\begin{verbatim}
        x := 123;
\end{verbatim}
in the {\tt PROCEDURE} case it is a variable holding a copy of the first
actual argument that is changed.  The actual argument is not changed.

In the {\tt LET} case, the actual argument is changed.  Thus, if {\tt ABC}
is defined using {\tt LET}, and {\tt abc(u,v,w)} is evaluated, the value
of {\tt U} changes to 123.  That is, the {\tt LET} form of definition
allows the user to bypass the protections that are enforced by the call
by value conventions of standard {\tt PROCEDURE} definitions.

{\it Example:}  We take our earlier {\tt FACTORIAL}\ttindex{FACTORIAL}
procedure and write it as a {\tt LET} statement.
\begin{verbatim}
        for all n let factorial n =
                    begin scalar m,s;
                    m:=1; s:=n;
                l1: if s=0 then return m;
                    m:=m*s;
                    s:=s-1;
                    go to l1
                end;
\end{verbatim}
The reader will notice that we introduced a new local variable, {\tt S},
and set it equal to {\tt N}.  The original form of the procedure contained
the statement {\tt n:=n-1;}.  If the user asked for the value of {\tt
factorial(5)} then {\tt N} would correspond to, not just have the value
of, 5, and {\REDUCE} would object to trying to execute the statement
5 := $5-1$.

If {\tt PQR} is a procedure with no parameters,
\begin{verbatim}
        procedure pqr;
           <procedure body>;
\end{verbatim}
it can be written as a {\tt LET} statement quite simply:
\begin{verbatim}
        let pqr = <procedure body>;
\end{verbatim}
To call {\em procedure\/} {\tt PQR}, if defined in the latter form, the empty
parentheses would not be used: use {\tt PQR} not {\tt PQR()} where a call
on the procedure is needed.

The two notations for a procedure with no arguments can be combined. {\tt PQR}
can be defined in the standard {\tt PROCEDURE} form. Then a {\tt LET}
statement
\begin{verbatim}
        let pqr = pqr();
\end{verbatim}
would allow a user to use {\tt PQR} instead of {\tt PQR()} in calling the
procedure.

A feature available with {\tt LET}-defined procedures and not with procedures
defined in the standard way is the possibility of defining partial
functions.\index{Function}
\begin{verbatim}
    for all x such that numberp x let uvw(x)=<procedure body>;
\end{verbatim}
Now {\tt UVW} of an integer would be calculated as prescribed by the procedure
body, while {\tt UVW} of a general argument, such as {\tt Z} or {\tt p+q}
(assuming these evaluate to themselves) would simply stay {\tt uvw(z)}
or {\tt uvw(p+q)} as the case may be.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                      %
% GLE - Graphics Layout Engine <http://www.gle-graphics.org/>          %
%                                                                      %
% Modified BSD License                                                 %
%                                                                      %
% Copyright (C) 2009 GLE.                                              %
%                                                                      %
% Redistribution and use in source and binary forms, with or without   %
% modification, are permitted provided that the following conditions   %
% are met:                                                             %
%                                                                      %
%    1. Redistributions of source code must retain the above copyright %
% notice, this list of conditions and the following disclaimer.        %
%                                                                      %
%    2. Redistributions in binary form must reproduce the above        %
% copyright notice, this list of conditions and the following          %
% disclaimer in the documentation and/or other materials provided with %
% the distribution.                                                    %
%                                                                      %
%    3. The name of the author may not be used to endorse or promote   %
% products derived from this software without specific prior written   %
% permission.                                                          %
%                                                                      %
% THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR   %
% IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED       %
% WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE   %
% ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY       %
% DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL   %
% DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE    %
% GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS        %
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER %
% IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR      %
% OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN  %
% IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                        %
%                                                                      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\chapter{SYMBOLS}
The following table lists all the symbols (and the commands for generating
them) available in GLE's text module.

\begin{tabular}{llllll}
\\
\multicolumn{6}{l}{\em Lowercase Greek letters} \\ \hline
$\alpha$             & \verb+\alpha+             & $\iota$                 & \verb+\iota+               & $\sigma$        & \verb+\sigma+ \\
$\beta$              & \verb+\beta+              & $\kappa$                & \verb+\kappa+              & $\varsigma$     & \verb+\varsigma+ \\
$\gamma$             & \verb+\gamma+             & $\lambda$               & \verb+\lambda+             & $\tau$          & \verb+\tau+ \\
$\delta$             & \verb+\delta+             & $\mu$                   & \verb+\mu+                 & $\upsilon$      & \verb+\upsilon+ \\
$\epsilon$           & \verb+\epsilon+           & $\nu$                   & \verb+\nu+                 & $\phi$          & \verb+\phi+ \\
$\varepsilon$        & \verb+\varepsilon+        & $\xi$                   & \verb+\xi+                 & $\varphi$       & \verb+\varphi+ \\
$\zeta$              & \verb+\zeta+              & $\pi$                   & \verb+\pi+                 & $\chi$          & \verb+\chi+ \\
$\eta$               & \verb+\eta+               & $\varpi$                & \verb+\varpi+              & $\psi$          & \verb+\psi+ \\
$\theta$             & \verb+\theta+             & $\rho$                  & \verb+\rho+                & $\omega$        & \verb+\omega+ \\
$\vartheta$          & \verb+\vartheta+          & $\varrho$               & \verb+\varrho+ \\
\end{tabular}
\newpage
\begin{tabular}{llllll}
\\
\multicolumn{6}{l}{\em Uppercase Greek Letters} \\ \hline
$\Gamma$             & \verb+\Gamma+             & $\Xi$                   & \verb+\Xi+                 & $\Phi$         & \verb+\Phi+ \\
$\Delta$             & \verb+\Delta+             & $\Pi$                   & \verb+\Pi+                 & $\Psi$         & \verb+\Psi+ \\
$\Theta$             & \verb+\Theta+             & $\Sigma$                & \verb+\Sigma+              & $\Omega$       & \verb+\Omega+ \\
$\Lambda$            & \verb+\Lambda+            & $\Upsilon$              & \verb+\Upsilon+ \\
\\
\multicolumn{6}{l}{\em Accents\dag} \\ \hline
\`{o}              & \verb+\`{o}+              & \={o}                 & \verb+\={o}+               & \t{oo}       & \verb+\t{oo}+ \\
\'{o}              & \verb+\'{o}+              & \.{o}                 & \verb+\.{o}+               & \c{o}        & \verb+\c{o}+ \\
\^{o}              & \verb+\^{o}+              & \u{o}                 & \verb+\u{o}+               & \d{o}        & \verb+\d{o}+ \\
\"{o}              & \verb+\"{o}+              & \v{o}                 & \verb+\v{o}+               & \b{o}        & \verb+\b{o}+ \\
\~{o}              & \verb+\~{o}+              & \H{o}                 & \verb+\H{o}+ \\
\\
\multicolumn{6}{l}{\em Foreign Symbols\dag} \\ \hline
\oe                & \verb+\oe+                & \aa                   & \verb+\aa+                 & \l           & \verb+\l+ \\
\OE                & \verb+\OE+                & \AA                   & \verb+\AA+                 & \L           & \verb+\L+ \\
\ae                & \verb+\ae+                & \o                    & \verb+\o+                  & \ss          & \verb+\ss+ \\
\AE                & \verb+\AE+                & \O                    & \verb+\O+ \\
\\
\multicolumn{6}{l}{\em Ellipsis} \\ \hline
$\cdots$             & \verb+\cdots+             & $\ddots$                & \verb+\ddots+              & $\vdots$       & \verb+\vdots+ \\
\end{tabular}
\newpage
\begin{tabular}{llllll}
\\
\multicolumn{6}{l}{\em Arrow symbols} \\ \hline
$\leftarrow$         & \verb+\leftarrow+         & $\longleftarrow$        & \verb+\longleftarrow+      & $\uparrow$     & \verb+\uparrow+ \\
$\Leftarrow$         & \verb+\Leftarrow+         & $\Longleftarrow$        & \verb+\Longleftarrow+      & $\Uparrow$     & \verb+\Uparrow+ \\
$\rightarrow$        & \verb+\rightarrow+        & $\longrightarrow$       & \verb+\longrightarrow+     & $\downarrow$   & \verb+\downarrow+ \\
$\Rightarrow$        & \verb+\Rightarrow+        & $\Longrightarrow$       & \verb+\Longrightarrow+     & $\Downarrow$   & \verb+\Downarrow+ \\
$\leftrightarrow$    & \verb+\leftrightarrow+    & $\longleftrightarrow$   & \verb+\longleftrightarrow+ & $\updownarrow$ & \verb+\updownarrow+ \\
$\Leftrightarrow$    & \verb+\Leftrightarrow+    & $\Longleftrightarrow$   & \verb+\Longleftrightarrow+ & $\Updownarrow$ & \verb+\Updownarrow+ \\
$\mapsto$            & \verb+\mapsto+            & $\longmapsto$           & \verb+\longmapsto+         & $\nearrow$     & \verb+\nearrow+ \\
$\hookleftarrow$     & \verb+\hookleftarrow+     & $\hookrightarrow$       & \verb+\hookrightarrow+     & $\searrow$     & \verb+\searrow+ \\
$\leftharpoonup$     & \verb+\leftharpoonup+     & $\rightharpoonup$       & \verb+\rightharpoonup+     & $\swarrow$     & \verb+\swarrow+ \\
$\leftharpoondown$   & \verb+\leftharpoondown+   & $\rightharpoondown$     & \verb+\rightharpoondown+   & $\nwarrow$     & \verb+\nwarrow+ \\
$\rightleftharpoons$ & \verb+\rightleftharpoons+ & $\leadsto$              & \verb+\leadsto+ \\
\\
\multicolumn{6}{l}{\em Binary operation symbols} \\ \hline
$\pm$                & \verb+\pm+                & $\sqcap$                & \verb+\sqcap+              & $\rhd$         & \verb+\rhd+ \\
$\mp$                & \verb+\mp+                & $\sqcup$                & \verb+\sqcup+              & $\unlhd$       & \verb+\unlhd+ \\
$\times$             & \verb+\times+             & $\vee$                  & \verb+\vee+                & $\unrhd$       & \verb+\unrhd+ \\
$\div$               & \verb+\div+               & $\wedge$                & \verb+\wedge+              & $\oplus$       & \verb+\oplus+ \\
$\ast$               & \verb+\ast+               & $\setminus$             & \verb+\setminus+           & $\ominus$      & \verb+\ominus+ \\
$\star$              & \verb+\star+              & $\wr$                   & \verb+\wr+                 & $\otimes$      & \verb+\otimes+ \\
$\circ$              & \verb+\circ+              & $\diamond$              & \verb+\diamond+            & $\oslash$      & \verb+\oslash+ \\
$\bullet$            & \verb+\bullet+            & $\bigtriangleup$        & \verb+\bigtriangleup+      & $\odot$        & \verb+\odot+ \\
$\cdot$              & \verb+\cdot+              & $\bigtriangledown$      & \verb+\bigtriangledown+    & $\bigcirc$     & \verb+\bigcirc+ \\
$\cap$               & \verb+\cap+               & $\triangleleft$         & \verb+\triangleleft+       & $\dagger$      & \verb+\dagger+ \\
$\cup$               & \verb+\cup+               & $\triangleright$        & \verb+\triangleright+      & $\ddagger$     & \verb+\ddagger+ \\
$\uplus$             & \verb+\uplus+             & $\lhd$                  & \verb+\lhd+                & $\amalg$       & \verb+\amalg+ \\
\end{tabular}
\newpage
\begin{tabular}{llllll}
\\
\multicolumn{6}{l}{\em Relational symbols} \\ \hline
$\leq$               & \verb+\leq+               & $\geq$                  & \verb+\geq+                & $\equiv$       & \verb+\equiv+ \\
$\prec$              & \verb+\prec+              & $\succ$                 & \verb+\succ+               & $\sim$         & \verb+\sim+ \\
$\preceq$            & \verb+\preceq+            & $\succeq$               & \verb+\succeq+             & $\simeq$       & \verb+\simeq+ \\
$\ll$                & \verb+\ll+                & $\gg$                   & \verb+\gg+                 & $\asymp$       & \verb+\asymp+ \\
$\subset$            & \verb+\subset+            & $\supset$               & \verb+\supset+             & $\approx$      & \verb+\approx+ \\
$\subseteq$          & \verb+\subseteq+          & $\supseteq$             & \verb+\supseteq+           & $\cong$        & \verb+\cong+ \\
$\sqsubset$          & \verb+\sqsubset+          & $\sqsupset$             & \verb+\sqsupset+           & $\neq$         & \verb+\neq+ \\
$\sqsubseteq$        & \verb+\sqsubseteq+        & $\sqsupseteq$           & \verb+\sqsupseteq+         & $\doteq$       & \verb+\doteq+ \\
$\in$                & \verb+\in+                & $\ni$                   & \verb+\ni+                 & $\propto$      & \verb+\propto+ \\
$\vdash$             & \verb+\vdash+             & $\dashv$                & \verb+\dashv+              & $\perp$        & \verb+\perp+ \\
$\models$            & \verb+\models+            & $\mid$                  & \verb+\mid+                & $\parallel$    & \verb+\parallel+ \\
$\bowtie$            & \verb+\bowtie+            & $\Join$                 & \verb+\Join+               & $\smile$       & \verb+\smile+ \\
$\frown$             & \verb+\frown+ \\
\\
\multicolumn{6}{l}{\em Delimiter symbols} \\ \hline
$($                  & \verb+(+                  & $)$                     & \verb+)+                   & $\uparrow$     & \verb+\uparrow+ \\
$[$                  & \verb+[+                  & $]$                     & \verb+]+                   & $\downarrow$   & \verb+\downarrow+ \\
$\{$                 & \verb+\{+                 & $\}$                    & \verb+\}+                  & $\updownarrow$ & \verb+\updownarrow+ \\
$\lfloor$            & \verb+\lfloor+            & $\rfloor$               & \verb+\rfloor+             & $\Uparrow$     & \verb+\Uparrow+ \\
$\lceil$             & \verb+\lceil+             & $\rceil$                & \verb+\rceil+              & $\Downarrow$   & \verb+\Downarrow+ \\
$\langle$            & \verb+\langle+            & $\rangle$               & \verb+\rangle+             & $\Updownarrow$ & \verb+\Updownarrow+ \\
$/$                  & \verb+/+                  & $\backslash$            & \verb+\backslash+ \\
$|$                  & \verb+|+                  & $\|$                    & \verb+\|+ \\
\end{tabular}
\newpage
\begin{tabular}{llllll}
\\
\multicolumn{6}{l}{\em Miscellaneous symbols} \\ \hline
$\aleph$             & \verb+\aleph+             & $\nabla$                & \verb+\nabla+              & $\sharp$       & \verb+\sharp+ \\
$\hbar$              & \verb+\hbar+              & $\surd$                 & \verb+\surd+               & $\backslash$   & \verb+\backslash+ \\
$\imath$             & \verb+\imath+             & $\top$                  & \verb+\top+                & $\partial$     & \verb+\partial+ \\
$\jmath$             & \verb+\jmath+             & $\bot$                  & \verb+\bot+                & $\infty$       & \verb+\infty+ \\
$\ell$               & \verb+\ell+               & $\|$                    & \verb+\|+                  & $\Box$         & \verb+\Box+ \\
$\wp$                & \verb+\wp+                & $\angle$                & \verb+\angle+              & $\Diamond$     & \verb+\Diamond+ \\
$\Re$                & \verb+\Re+                & $\forall$               & \verb+\forall+             & $\triangle$    & \verb+\triangle+ \\
$\Im$                & \verb+\Im+                & $\exists$               & \verb+\exists+             & $\clubsuit$    & \verb+\clubsuit+ \\
$\mho$               & \verb+\mho+               & $\neg$                  & \verb+\neg+                & $\diamondsuit$ & \verb+\diamondsuit+ \\
$\prime$             & \verb+\prime+             & $\flat$                 & \verb+\flat+               & $\heartsuit$   & \verb+\heartsuit+ \\
$\emptyset$          & \verb+\emptyset+          & $\natural$              & \verb+\natural+            & $\spadesuit$   & \verb+\spadesuit+ \\
\\
\multicolumn{6}{l}{\em Trigonometric symbols} \\ \hline
$\arccos$            & \verb+\arccos+            & $\dim$                  & \verb+\dim+                & $\log$         & \verb+\log+ \\
$\arcsin$            & \verb+\arcsin+            & $\exp$                  & \verb+\exp+                & $\max$         & \verb+\max+ \\
$\arctan$            & \verb+\arctan+            & $\gcd$                  & \verb+\gcd+                & $\min$         & \verb+\min+ \\
$\arg$               & \verb+\arg+               & $\hom$                  & \verb+\hom+                & $\Pr$          & \verb+\Pr+ \\
$\cos$               & \verb+\cos+               & $\inf$                  & \verb+\inf+                & $\sec$         & \verb+\sec+ \\
$\cosh$              & \verb+\cosh+              & $\ker$                  & \verb+\ker+                & $\sin$         & \verb+\sin+ \\
$\cot$               & \verb+\cot+               & $\lg$                   & \verb+\lg+                 & $\sinh$        & \verb+\sinh+ \\
$\coth$              & \verb+\coth+              & $\lim$                  & \verb+\lim+                & $\sup$         & \verb+\sup+ \\
$\csc$               & \verb+\csc+               & $\liminf$               & \verb+\liminf+             & $\tan$         & \verb+\tan+ \\
$\deg$               & \verb+\deg+               & $\limsup$               & \verb+\limsup+             & $\tanh$        & \verb+\tanh+ \\
$\det$               & \verb+\det+               & $\ln$                   & \verb+\ln+ \\
\end{tabular}
\newpage
\begin{tabular}{llllll}
\\
\multicolumn{6}{l}{\em Variable-size symbols} \\ \hline
$\sum$               & \verb+\sum+               & $\bigcap$               & \verb+\bigcap+             & $\bigodot$     & \verb+\bigodot+ \\
$\prod$              & \verb+\prod+              & $\bigcup$               & \verb+\bigcup+             & $\bigotimes$   & \verb+\bigotimes+ \\
$\coprod$            & \verb+\coprod+            & $\bigsqcup$             & \verb+\bigsqcup+           & $\bigoplus$    & \verb+\bigoplus+ \\
$\int$               & \verb+\int+               & $\bigvee$               & \verb+\bigvee+             & $\biguplus$    & \verb+\biguplus+ \\
$\oint$              & \verb+\oint+              & $\bigwedge$             & \verb+\bigwedge+ \\
\\
\multicolumn{6}{l}{\em Punctuation symbols\dag} \\ \hline
\dag               & \verb+\dag+               & \S                    & \verb+\S+                  & \copyright   & \verb+\copyright+ \\
\ddag              & \verb+\ddag+              & \P                    & \verb+\P+                  & \pounds      & \verb+\pounds+ \\
?`                 & \verb+?`+                 & !`                    & \verb+!`+                  & \ldots       & \verb+\ldots+ \\
\end{tabular}

\dag\ These symbols may not be used in math mode unless they are enclosed
in an \verb+\mbox+.


\section{System Implementation} \label{system_partitioning}
As briefly discussed in the \textit{Introduction}, nowadays one of the smartest techniques deployed for autonomous vehicles control is the Model Predictive Control, hence our decision to develop a controller in order to accomplish the path following and obstacle avoidance tasks.
The design of such a controller needs a deep analysis of the project requirements and a smart system partitioning to identify different tasks that can be developed independently from others.

\subsection{Analysis of requirements} \label{subsection:requirements}
Starting from the ``customer" system requirements described in Section \ref{System_Requirements}, we can obtain low level requirements suitable for the controller implementation; each of these is associated with a draft of the validation method needed to assert each of them. Table \ref{tab:requirement} shows the system requirements, low level requirements and the corresponding validation methods.

\newcolumntype{P}[1]{>{\raggedright\arraybackslash}p{#1}}
\begin{table}[H]
\begin{tabular}{P{0.3\textwidth} P{0.3\textwidth} P{0.3\textwidth}}
\hline
\multicolumn{1}{c}{\textbf{System requirements}} & \multicolumn{1}{c}{\textbf{Low level requirements}} & \multicolumn{1}{c}{\textbf{Validation methods}} \\
\hline
Maximum lateral error from reference of 0.75 $m$ & Inequality constraint:\space\space\space\space\space\space\space\space\space\space\space\space $e_{y}=abs(Y_{reference}-Y_{vehicle})$ $<=$ 0.75 $m$ & Check in each point that the difference between the actual vehicle position and the reference path is not greater than 0.75 $m$ \\
\hline
Detection of obstacles within 100 $m$ ahead of vehicle & Sensor fusion able to provide useful data in the range $0\div100$ $m$ from the vehicle & Drive toward a static obstacle and confirm that is detected when is 100 $m$ far \\
\hline
Move on left lane within a predetermined safe zone from the obstacle & Inequality constraint when obstacle detected: $dist = position_{vehicle}-position_{obstacle}  \in Safe Zone$ & Confirm that when changing lane the distance from the obstacle measured by the sensors is greater than the predetermined \textit{Safe Zone} \\
\hline
Once the obstacle has been passed, come back on the right lane at no less than 10 $m$ but no more than 50 $m$ ahead of it & Inequality constraint when obstacle passed: \space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space10 $m$ $<= dist <=$ 50 $m$  & Confirm that when changing lane the distance from the obstacle measured by the sensors is greater than the predetermined \textit{Safe Zone} \\
\hline
Maximum lateral acceleration of 2.0 $m/s^2$ & Constraint on Model Predictive Control input: $a_{max} = 2.0$ $m/s^2$ & Check that the maximum value registered by the IMU$-$accelerometer is not greater than 2.0 $m/s^2$ \\
\hline
All previous requirements satisfied in speed range from 10 $km/h$ to 100 $km/h$ & Verify all the previous constraints when simulating at speed range $10\div100$ $km/h$ & Verify all the above methods with vehicle travelling from 10 $km/h$ to 100 $km/h$ \\
\hline
\end{tabular}
\caption{System requirements and possible validation methods}
\label{tab:requirement}
\end{table}

\subsection{Partitioning} \label{partitioning_subsection}

Regarding the system partitioning which, as already said in Section \ref{system_partitioning}, is a crucial part of the implementation of a system, we have decided to base the implementation of our project on the tasks shown in Figure \ref{fig:partitioning}.

\begin{figure}[H]
    \centering
    \makebox[\textwidth][c]{\includegraphics[width=1.25\textwidth,keepaspectratio]{Figures/partitioning.png}}
    \caption{System Partitioning}
    \label{fig:partitioning}
\end{figure}

As shown in the figure above, our project can be divided into four macro-areas:
\begin{itemize}
    \item \textbf{Plant Model}: is the mathematical model of the vehicle which represents the development environment of our project. We have decided to adopt two models, a simplified one to be used in the MPC and a more accurate one to be used in the simulation;
    \item \textbf{Signal Acquisition}: refers to the gathering of data related to the vehicle position, trajectory, and obstacle acquisition, all coming from the sensors assumed to be mounted on our vehicle;
    \item \textbf{Obstacle Detection}: here the properties of the obstacle such as position, speed, and dimension  are evaluated;
    \item \textbf{Constraint Generator}: in this phase we set the constraints which the MPC needs to comply with. These constraints are necessary in order to make our model as realistic and safe as possible.
\end{itemize}

These sections will be described in depth in the following chapters.