\documentclass[twoside]{MATH77}
\usepackage{multicol}
\usepackage[fleqn,reqno,centertags]{amsmath}
\usepackage{euscript}
\begin{document}
\hyphenation{DZVALS}
\begmath 12.4 C$^0$ and C$^1$ Surface Interpolation to Scattered Data

\silentfootnote{$^\copyright$1997 Calif. Inst. of Technology, \thisyear \ Math \`a la Carte, Inc.}

\subsection{Purpose}
Given a set of triples $(x_i,\ y_i,\ z_i)$,
$i$ = 1, \ldots, NP, these routines implicitly construct a conveniently
computable function $f(x,y)$ satisfying the conditions
\begin{equation*}
z_i = f(x_i, y_i), \ \ i=1, \ldots , NP,
\end{equation*}
and support evaluation of $f(x,y)$ and its first partial derivatives for
user-provided values of $(x,y)$.  The data $(x_i,\ y_i)$ are not assumed
to lie in any special pattern, but it is assumed that $(x_i,\ y_i) =
(x_j,\ y_j)$ only if $i = j$.

\subsection{Usage}

To apply this method the user must first call STGGRD to build a Delaunay
triangular grid having the given $(x_i,\ y_i)$ points as nodal points.
For convenience from here on we shall let {$\EuScript S$} denote the given
set of $(x_i,\ y_i)$ points.  The user must specify that $f$ is to have
either C$^0$ or C$^1$ continuity.  For C$^1$ continuity the interpolation
method needs values of first partial derivatives of the desired surface at
the nodal points.  Most commonly the user will not have these values, in
which case the subroutine STGPD can be called to compute estimated partial
derivatives.

Once the grid and partial derivatives (for the C$^1$ method) have been
established, one can evaluate the interpolation function $f$ at any
$(x,y)$ point by calling STGFI.  This evaluation operation can be repeated
for any number of points.  Subroutine STGFI uses STGFND to determine which
triangle of the grid contains the point $(x,y)$, and STGC0 or STGC1 to
evaluate $f(x,y)$ in that triangle.  If the point is outside the
triangular grid STGFI calls STGEXT to compute extrapolated values.

We do not describe the separate usage of STGFND, STGEXT, STGC0, and STGC1,
but a user could learn this from the comments in these subroutines and the
code in STGFI, if desired for specialized applications.

In some applications one wishes to produce a table of values of the
interpolated surface at points of a regular rectangular grid.  The
subroutine STGREC can be called to produce such a table.  STGREC makes
a sequence of calls to STGFI.  The user must call STGGRD and optionally
STGPD before calling STGREC.

The subroutine STGPGR is provided to print the data structures that
define the triangular grid.  This routine may also be useful as a
model if one wishes to interface to a graphics system to draw the
triangular grid.

The following sections address usage for the following:

\begin{tabular}{lp{2.2in}}
B.1. STGGRD & Construct a Delaunay triangular grid\\
B.2. STGPD & Estimate partial derivatives at grid nodes\\
B.3. STGFI & Lookup and interpolate in a triangular grid\\
B.4. STGREC & Construct a rectangular grid of function values by
interpolation in a triangular grid\\
B.5. STGPGR & Print the data structures defining the triangular grid
\end{tabular}

\subsubsection{Construct a Delaunay Triangular Grid}

\paragraph{Program Prototype, Single Precision}

\begin{description}
\item[INTEGER] NP, IP($\geq$NP), MT, TRIANG(MT), MB, B(4,~MB), NT, INFO(3)
\item[REAL] X($\geq$NP), Y($\geq$NP), W($\geq$NP)
\end{description}
Assign values to NP, MT, MB, X(1:NP), and Y(1:NP).
\begin{center}
\fbox{\begin{tabular}{@{\bf }c}
CALL STGGRD (X, Y, NP, IP, W,\\
\rule{.25in}{0pt}TRIANG, MT, B, MB, NT, INFO)
\end{tabular}}
\end{center}
Results are returned in TRIANG(), B(,), NT, and INFO(1:3)
\paragraph{Argument Definitions}

\begin{description}
\item[X(1:NP), Y(1:NP)] [in] The $(x,\ y)$
coordinates of the NP data points constituting the set {$\EuScript S$}.
\item[NP] [in] Number of data points.  Require NP $\geq $ 3.  The
upper limit on NP will depend on the storage available for the array
TRIANG().
\item[IP()] [scratch] Working space.
\item[W()] [scratch] Working space.
\item[TRIANG()] [out] Defines the triangular grid.
Each triangle is defined by six integer indices
identifying up to three neighboring triangles and three vertex
points.  The number of triangles will be
NT = $2\times (\text{NP}-1)-\text{NB}$
where NB is the number of points on the boundary of the convex hull
of {$\EuScript S$}.
\item[MT] [in] The declared dimension of TRIANG.  Must be $\geq
6\times[2\times(\text{NP}-1)-\text{NB}]$.  If NB is not known, $12
\times (\text{NP}-1)$ provides an upper bound for the required value of MT.
\item[B(,)] [out] On return this contains a two--way linked list
identifying the boundary points of the grid.
\item[MB] [in] The declared second dimension of B(,).  Must be larger
by one than the largest number of boundary points in any of the
subsets of {$\EuScript S$} that STGGRD considers.  Setting MB = NP + 1
would always
be large enough, however, a much smaller value for MB will usually
suffice.\newline
The expected number of boundary points in {$\EuScript S$} when
{$\EuScript S$} is a random set of
points uniformly distributed in a disk is $3.4 \times \text{NP}^{1/3}$
for NP $>$ 100.\newline
We suggest setting MB = NP + 1 for NP $\leq $ 15, and MB = an integer
exceeding $6 \times \text{NP}^{1/3}$ for NP $\geq $ 16.
\item[NT] [out] The number of triangles in the
triangular grid.  NT, NB, and NP are related by the equation
$\text{NT} = 2 \times (\text{NP} - 1) - \text{NB}.$
\item[INFO(1:3)] [out] Termination information.\vspace{-5pt}
\begin{description}
\item[INFO(1)] Status on termination.  The grid is complete if INFO(1) = 0
and incomplete or not optimal otherwise.
\begin{itemize}
\item[=0] Normal termination, the triangular grid is complete and optimized.
\item[=1] A complete triangular grid has been constructed, but the process
of optimizing the shapes of the triangular cells did not complete.
This should not happen.
\item[=2] Either all given points are colinear or else some
pair of the points are duplicates.
\item[=3] Some pair of the points are duplicates.
\item[=4] The dimension MB is not large enough.  Setting MB =
NP + 1 will suffice.
\item[=5] The dimension MT is not large enough.
Setting $\text{MT} = 12 \times (\text{NP}-1)$ will suffice.
\end{itemize}
\item[INFO(2)] = NB, the number of boundary points in the convex
hull of {$\EuScript S$}.
\item[INFO(3)] = The minimum value that would have sufficed for
the dimension MB on this execution of STGGRD.
\end{description}
\end{description}

\subsubsection{Estimate Partial Derivatives at Grid Nodes}

\paragraph{Program Prototype, Single Precision}

\begin{description}
\item[INTEGER]  NP, TRIANG($\geq 6 \times$NP), NT,\newline
IWORK($\geq $NP)
\item[REAL] X($\geq$NP), Y($\geq$NP), Z($\geq$NP), DZ(2, $\geq$NP)
\end{description}
If one does not have partial derivative values available, this
subroutine can be used to compute estimates of the partial derivatives
after using subroutine STGGRD to
construct a triangular grid.  The values of X(), Y(), and NP
should be the same as were input to STGGRD.  The values of TRIANG()
and NT should be those produced by STGGRD.  Values must be
assigned to Z(1:NP).
\begin{center}
\fbox{\begin{tabular}{@{\bf }c}
CALL STGPD (X, Y, Z, DZ, NP,\\
\rule{.25in}{0pt}TRIANG, NT, IWORK)
\end{tabular}}
\end{center}
Results are returned in DZ(1:2, 1:NP).
\paragraph{Argument Definitions}

\begin{description}
\item[X(1:NP), Y(1:NP), Z(1:NP)] [in] The user supplied data; Z(i) is
the function value associated with the point (X(i), Y(i)).
\item[DZ(1:2, 1:NP)] [out] Estimates of the first partial derivatives,
stored as:\newline
\hspace{.25in} DZ(1,i) = $\partial f / \partial x$ at (X(i), Y(i))
\newline
\hspace{.25in} DZ(2,i) = $\partial f / \partial y$ at (X(i), Y(i))
\item[NP] [in] Number of data points.
\item[TRIANG(), NT] [in] Defined as for STGGRD.
\item[IWORK(1:NP)] [scratch] Working space.
\end{description}

\subsubsection{Lookup and Interpolate in a Triangular Grid}

\paragraph{Program Prototype, Single Precision}

\begin{description}
\item[INTEGER]  TRIANG($\geq 6 \times$NT), NT, NCONT,\newline
MODE, MB, B(4,~MB)
\item[REAL] X($\geq$NP), Y($\geq$NP), Z($\geq$NP), DZ(2,$\geq$NP),\newline
Q(2), ZOUT, DZOUT(2), SAVWRK(28)
\item[LOGICAL] WANTDZ
\end{description}

This subroutine is intended to be used after a triangular grid has been
constructed using subroutine STGGRD.  The values of X(), Y(), and MB
should be the same as were input to STGGRD.  The values of TRIANG(), NT,
and B(), should be those produced by STGGRD.  The value of NP (used only
in the text here) should be the same as was used in the call to STGGRD.
Values must be assigned to Z(1:NP), NCONT, Q(1:2), WANTDZ, and SAVWRK(1).
If C$^1$ interpolation is being requested, (NCONT = 1), values must be
assigned to DZ(1:2, 1:NP), possibly by use of subroutine STGPD.

\begin{center}
\fbox{\begin{tabular}{@{\bf }c}
CALL STGFI (X, Y, Z, DZ, TRIANG, NT,\\
\rule{.25in}{0pt}
B, MB, NCONT, Q, ZOUT, WANTDZ,\\
\rule{.25in}{0pt}DZOUT, MODE, SAVWRK)
\end{tabular}}
\end{center}

Results are returned in ZOUT, DZOUT(1:2), and MODE, and the contents of
SAVWRK(1:28) will generally be changed.

\paragraph{Argument Definitions}

\begin{description}
\item[X(1:NP), Y(1:NP)] [in] The $(x,\ y)$ coordinates of the data points
constituting the set {$\EuScript S$}, and thus the nodes of the triangular
grid.
\item[Z(1:NP)] [in] Z(i) is the value at (X(i), Y(i)) of the data to
be interpolated.
\item[DZ(1:2, 1:NP)] [in] Values, or estimates of values, of first
partial derivatives at the nodal points.  These are needed only when
NCONT = 1.  Otherwise this array will not be referenced.
\newline
\hspace{.25in} DZ(1,i) = $\partial f / \partial x$ at (X(i), Y(i))
\newline
\hspace{.25in} DZ(2,i) = $\partial f / \partial y$ at (X(i), Y(i))
\item[TRIANG(), NT] [in] Defined as for STGGRD.
\item[B(), MB] [in] Defined as for STGGRD.
\item[NCONT] [in] Set NCONT = 0 for C$^0$ interpolation,
and = 1 for C$^1$ interpolation.
\item[Q(1:2)] [in] Lookup and interpolation will be done for the point
$q = (x, y) =$ (Q(1), Q(2)).
\item[ZOUT] [out] Interpolated value computed by this subroutine.
\item[WANTDZ] [in] Turns on or off the computation of DZOUT(1:2).
\item[DZOUT(1:2)] [out] If WANTDZ = .true., interpolated values of the
partial derivatives at the point $q$ will be computed and returned in
DZOUT(1:2).  No partial derivative interpolation is done if WANTDZ = .false.
\item[MODE] [out] Indicates status on return.
\begin{itemize}
\item[=0] Point $q$ is within the convex hull of the set {$\EuScript S$}, or possibly
outside this triangle by a small tolerance.  Interpolated results are
returned.
\item[=1] Point $q$ is outside the convex hull of {$\EuScript S$} by more
than the built-in small tolerance.  Extrapolated results are returned.
Accuracy degrades rapidly as the evaluation points move away from the
convex hull.
\item[=2] Error condition.  STGFND has tested the point $q$
against
NT triangles without resolving its status.  This would indicate an error
in the definition of the triangular grid or in the design or coding
of the subroutine.  No results are returned.
\end{itemize}
\item[SAVWRK(1:28)] [inout] Array used as work space and to save values
that, in some circumstances, can be reused to reduce the amount of
computation on subsequent calls.  The user must set SAVWRK(1) = 0.0 on the
first call to this subroutine to signal that SAVWRK() does not contain any
saved information at that time.  Generally SAVWRK(1) will be nonzero on
returns from STGFI.  If subsequent calls do not involve changes to X, Y,
Z, DZ, TRIANG, NT, B, MB, or NCONT, the user should not alter SAVWRK() on
the subsequent calls.  If the user changes any of these quantities, the
user should reset SAVWRK(1) = 0.0 to indicate that saved information
should be ignored.  See Section C for further discussion of SAVWRK().
\end{description}

\subsubsection{Construct a Rectangular Grid of Function Values by
Interpolation in a Triangular Grid}

\paragraph{Program Prototype, Single Precision}

\begin{description}
\item[LOGICAL] WANTPD
\item[INTEGER]  NP, TRIANG($\geq 6 \times$NT), NT, NX, NY,\newline
MX, MY, NCONT, MB, B(4,~MB)
\item[REAL] X($\geq$NP), Y($\geq$NP), Z($\geq$NP),
DZ(2, $\geq$NP),\newline
XYLIM(4), ZFILL, ZVALS(MX, MY),\newline
DZVALS(MX, MY, 2)
\end{description}
Assign values to all of the subroutine arguments except ZVALS() and
DZVALS().  STGGRD should be used to set values for TRIANG(), NT and B().
Subroutine STGPD can be used to set values for DZ(,).
\begin{center}
\fbox{\begin{tabular}{@{\bf }c}
CALL STGREC (X, Y, Z, DZ, NP,\\
\rule{.15in}{0pt}TRIANG, NT, B, NB, XYLIM,\\
\rule{.15in}{0pt}NX, NY, ZFILL, ZVALS, MX, MY,\\
\rule{.15in}{0pt}NCONT, WANTPD, DZVALS)
\end{tabular}}
\end{center}
Results are returned in ZVALS(,) and DZVALS(,,1:2)
\paragraph{Argument Definitions}

\begin{description}
\item[X(1:NP), Y(1:NP), Z(1:NP), DZ(1:2, 1:NP)] [in] Values of $x$, $y$,
$z$, $\partial z / \partial x$, and $\partial z / \partial y$ at the NP
points of the triangular grid.  Values are needed in DZ() only if NCONT =
1.  See subroutine STGPD for computation of DZ().  \item[NP] [in] Number
of data points.
\item[TRIANG(), NT] [in] Defined as for STGGRD.
\item[B(), MB] [in] Defined as for STGGRD.
\item[XYLIM(), NX, NY] [in] Specifies the desired rectangular
grid.  The $x$ grid values will run from XYLIM(1) to XYLIM(2) with $\text{NX}
- 2$ equally spaced intermediate values.  The $y$ grid values will run
from XYLIM(3) to XYLIM(4) with $\text{NY} - 2$ equally spaced intermediate
values.
\item[ZFILL] [in] Determines the action to be taken when a point of the
rectangular grid is outside the convex region covered by the triangular
grid.  If ZFILL is zero, extrapolated values will be returned.  Otherwise
the value, ZFILL, will be returned as a function value in ZVALS() and
optionally as a partial derivative value in DZVALS().
\item[ZVALS(,)] [out] An MX by MY array in which STGREC stores the NX
by NY set of values of the function $z = f(x, y)$ defined by
interpolation in the triangular grid.  Thus for $i$ = 1, \ldots, NX, $j$
= 1, \ldots, NY, STGREC computes
\newline
$x_i = \text{XYLIM}(1) + (i-1) \times $\newline
\rule{.75in}{0pt}$[\text{XYLIM(2)} - \text{XYLIM(1)}] /
(\text{NX} - 1)$\newline
$y_j = \text{XYLIM}(3) + (j-1) \times $\newline
\rule{.75in}{0pt}$[\text{XYLIM(4)} - \text{XYLIM(3)}] /
(\text{NY} - 1)$\newline
IF ($(x_i, y_j)$ is in the region covered by the triangular grid)
THEN \newline
\rule{.2in}{0pt}ZVAL($i,j$) = $f(x_i, y_j)$ (using
interpolation)\newline
ELSE\newline
\rule{.2in}{0pt}IF (ZFILL .eq. 0.0E0) THEN\newline
\rule{.4in}{0pt}$\text{ZVAL}(i,j) = f(x_i, y_j)$ (using
extrapolation).\newline
\rule{.2in}{0pt}ELSE\newline
\rule{.4in}{0pt}$\text{ZVAL}(i,j) =$ ZFILL\newline
\rule{.2in}{0pt}END IF\newline
END IF
\item[MX, MY] [in] Dimensions for the array ZVALS(,) and first two
dimensions for DZVALS(,,1:2).  Require MX $\geq $ NX and MY $\geq $ NY.
\item[NCONT] [in] Set NCONT = 0 for C$^0$ interpolation,
and = 1 for C$^1$ interpolation.
\item[WANTPD] [in] Set to .TRUE. if computation of DZVALS(,,1:2) is
desired in addition to ZVALS(,) and to .FALSE. if only ZVALS(,) is
desired.
\item[DZVALS(,,1:2)] [out] An MX by MY by 2 array in which the NX by NY
by 2 set of values of $\partial z / \partial x$ and
$\partial z / \partial y$
will be stored if WANTPD = .TRUE.  As in the computation of ZVALS(,),
the value ZFILL determines the action when an $(x_i, y_j)$ point is
outside the triangular grid.\newline
If WANTPD = .FALSE. the array DZVALS() will not be referenced and the
user can reduce all dimensions of DZVALS() to 1.

\end{description}

\subsubsection{Print the Data Structures defining a Triangular Grid}

\paragraph{Program Prototype, Single Precision}

\begin{description}
\item[INTEGER] NP, TRIANG($\geq 6 \times$NT), B(4,~MB), NB, NT
\item[REAL] X($\geq$NP), Y($\geq$NP)
\end{description}
\begin{center}
\fbox{\begin{tabular}{@{\bf }c}
CALL STGPRG (X, Y, NP, TRIANG,\\
\rule{.25in}{0pt}B, NB, NT)
\end{tabular}}
\end{center}

\paragraph{Argument Definitions}

All arguments are defined as for STGGRD, and should have the values
they had on a return from subroutine STGGRD (NB = INFO(2)).

\subsubsection{Modifications for Double Precision}

Change the first letter of every subroutine name from ``S'' to ``D'', and
change all REAL type statements to DOUBLE PRECISION.

\subsection{Examples and Remarks}
{\bf Example.}
The program DRSTGFI1 contains a set of data:
$(x_i,\ y_i), i = 1, \ldots, 28$.  The program calls STGGRD
to generate a triangular grid having these points as
nodal points.  The program runs two cases of C$^1$ interpolation to data
over this grid.  The interpolation is done at a set of points along
a line cutting across the region covered by the grid.

For the first case, data are generated at the nodal points using a
quadratic function.  Mathematically, the partial derivative
estimation method in STGPD and the C$^1$ interpolation method
in STGC1 are exact when the given $z_i$ data are a quadratic
function of the $(x_i, y_i)$ data.
Thus we observe that the errors in
the interpolated values are of the order of magnitude of the
precision of the machine arithmetic, which was IEEE single
precision arithmetic for the output listed with this writeup.

For the second case, data are generated using an
exponential function.  The partial derivative
estimation method in STGPD and the C$^1$ interpolation method
in STGC1 will both introduce errors in processing this data,
with the result that the interpolated values
will deviate from
the exponential function at points away from the nodal points.

The first and last points at which interpolation is requested in each case
are outside the convex hull of the given $(x_i, y_i)$ data.  The program
used extrapolation for these points.

Note that this program can be altered to run ten cases rather than
just two by changing the comment status of the loop control lines
involving the loop indices ``ncont" and ``KF".  A version of this
program called DRSTGFI has this change made.

{\bf Choosing between C$^0$ and C$^1$ interpolation.}
Since these are interpolation, and not smoothing, methods, they are
most appropriate for data that has very little noise.
The C$^1$ method requires first partial derivative data, whereas the C$^0$
method does not.  If the first partial derivative data have been estimated
from the $z_i$ data by using STGPD, they will have errors that are
larger as the data deviate more from having a locally quadratic shape.
Estimates of first partial derivative values at points $(x_i, y_i)$
on the boundary of the convex hull of {$\EuScript S$} may
be particularly bad.  The interpolation surface defined by the
C$^1$ method can easily have unwanted sharp peaks and valleys due to
errors in the first partial derivative data, or simply due to the
possibility that the shape of the data cannot be well represented
by the type of piecewise cubic surface produced by this method.

The C$^0$ method is more stable than the C$^1$ method,
in the sense that it will not produce
maxima higher than the data or minima lower than the data, however,
being piecewise linear it will have a choppy appearance due to
corners along all the edges of the triangular grid.

{\bf Using multiple copies of SAVWRK().}

Let us say the settings of X(), Y(), Z(), DZ(,), NP, TRIANG(), NT, B(,),
and MB define a {\em grid and data}.  Applications may arise in which the
user wishes to alternate between interpolations using STGFI with different
{\em grid and data} sets.  In such cases one can simply reset SAVWRK(1) =
0.0 each time the {\em grid and data} is changed.  For more computational
efficiency, however, one could use a distinct SAVWRK() array for each
distinct {\em grid and data}, say SAVW1(), SAVW2(), $\ldots$ One could set
SAVW1(1) = 0, SAVW2(1) = 0, $\ldots$, only once initially.  Then on each
call to STGFI, use the SAVW$n$() array associated with the current {\em
grid and data}.  This permits some saving of computational time in lookups
and C$^1$ interpolation.

\subsection{Functional Description}

{\bf Delaunay Triangulation.}
Given a finite set, {$\EuScript S$}, of points in the plane there are
generally many ways one could connect pairs of these points to form
a triangular grid, i.e., a grid having triangular cells.  The
Delaunay triangulation, constructed by subroutine STGGRD, has a number
of favorable mathematical properties, specifically the max-min angle
property, the empty circumcircle property, and duality with
a proximity region tessellation.

The max-min angle property has both a local and a global implication.
Locally, in any pair of triangles that share a side and whose union
is a strictly convex quadrilateral, the size of the smallest of the
six interior angles of the two triangles would not be increased by
replacing these two triangles by two triangles formed by
partitioning the quadrilateral using its other diagonal.
Globally, the smallest interior angle occurring in the whole triangulation
could not be made larger by any other triangulation.

The empty circumcircle property means that for each triangle of the grid,
its circumcircle, i.e., the unique circle passing through its three
vertices, contains no points of the set {$\EuScript S$} in its interior.

The Delaunay triangulation of a set {$\EuScript S$} is unique, with the
exception that if any set of four or more points of {$\EuScript S$} lie
on a circle that contains no points of {$\EuScript S$} in its interior,
it is arbitrary how triangles are formed using these vertices.

A Delaunay triangulation is dual to a proximity tessellation
associated with the names Dirichlet, Voronoi, and Thiessen.
A Dirichlet tessellation associated with a set {$\EuScript S$} is
a partitioning of the whole plane into regions with the property that
the interior of region {\em i} contains all the points of the plane that
are closer to point $(x_i, y_i)$ of {$\EuScript S$} than to any other
point of {$\EuScript S$}.  These regions will be convex, polygonal, and
some will be of infinite extent.  Clearly a one-to-one correspondence
exists between regions of a Dirichlet tessellation and nodes of a
Delaunay triangulation.  It also happens that a correspondence exists
between triangles of a Delaunay triangulation and nodes of a
Dirichlet tessellation.  Furthermore  edges in a Dirichlet tessellation
are perpendicular bisectors of edges in a Delaunay triangulation.

{\bf Algorithms.} The triangulation algorithm implemented in STGGRD is
described in \cite{Lawson:SC1:1977}.  From both theoretical analysis and
timing tests it appears that the execution time for STGGRD is proportional
to NP$^{4/3}$.

The method of estimating partial derivatives implemented in STGPD is
described in \cite{Lawson:SC1:1977}.  To estimate the partial derivatives
at a point $(x_i, y_i)$ of {$\EuScript S$}, this method locates up to 16
points of {$\EuScript S$} that are near to $(x_i, y_i)$ in the sense of
the grid connectivity, and does a uniformly weighted least squares fit of
a 6-parameter quadratic polynomial to the grid data at these points.  A
modification of this method is used if NP $<$ 6.  The first partial
derivatives of this polynomial at $(x_i, y_i)$ are returned as the
estimated partial derivatives.

The lookup method used in STGFND is described in \cite{Lawson:SC1:1977}.
This method starts by testing the given point $q$ for containment in the
triangle whose index is INDTRI (or 1 if INDTRI is out of range.) If $q$ is
outside of this triangle relative to side $j$, the search moves to the
triangle adjacent to the current triangle across side $j$.  This process
is repeated until either a triangle is found that contains $q$, or $q$ is
found to be outside some triangle relative to an edge that is a boundary
edge of the convex hull of {$\EuScript S$}.

The tests for containment produce values that are also needed in
both the C$^0$ and C$^1$ interpolation methods.  To avoid recomputation,
these quantities are stored in an array by STGFND for subsequent
use in STGC0 or STGC1.

This method is quite efficient in general, and is particularly efficient
when a sequence of lookups is done at points that are near to each
other relative to the grid connectivity, and with each lookup being started
in the triangle at which the previous lookup terminated.

C$^0$ continuity implies continuity of values but allows the possibility
of discontinuity of first partial derivatives, whereas
C$^1$ continuity implies continuity of values and first partial
derivatives,  but allows the possibility
of discontinuity of second partial derivatives.

The C$^0$ interpolation method implemented in STGC0
defines a planar surface over each triangular
cell of the grid.  Each such planar surface matches the values at
the three vertices of its triangle.  This gives a piecewise linear
surface over the whole grid.  The surface is continuous over the
whole grid, but generally has jumps in its first derivative values
when crossing from one triangle to another.

For C$^1$ interpolation we use the Clough-Tocher method,
\cite{Clough:1965:FES}, used in finite element computations.  The
particular implementation in STGC1 is described in \cite{Lawson:1976:C1C}.
Additional properties of the induced surface are described in
\cite{Lawson:1976:IC1}.

For one triangular cell of a grid this method implicitly partitions
the triangle into three subtriangles, by inserting new edges from
the vertices to the centroid.  Over each of these subtriangles the
method implicitly defines a cubic polynomial having C$^1$ continuity
across the newly inserted internal edges.  Along each of the original
edges of a triangle the values and first partial derivatives depend
only on the values and first partial derivatives
given at the two vertices of that edge.
This assures that two triangles that share an edge will have the
same values and first partial derivatives along the common edge, and
thus the surface over the whole triangular grid will have C$^1$
continuity.

The C$^0$ method is exact for linear data both in interpolation and
extrapolation.  The C$^1$ method is exact for quadratic data in
interpolation and for linear data in extrapolation.

\bibliography{math77}
\bibliographystyle{math77}

\subsection{Error Procedures and Restrictions}
STGGRD returns status information in INFO(1).
STGFI returns status information in MODE.
The user should encode tests of these status flags.
IERV1 is used for the output of some error messages.
\subsection{Supporting Information}

The source language is ANSI Fortran~77.  A common block STGCOM is
used in the program units in the file STGPD.
A common block DTGCOM is
used in the program units in the file DTGPD.

\begin{tabular}{@{\bf}l@{\hspace{5pt}}l}
\bf Entry & \hspace{.35in} {\bf Required Files}\vspace{2pt} \\
STGGRD & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
SSORTP, STGGRD, STGSET\rule[-5pt]{0pt}{8pt}}\\
STGPD & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
SROT, SROTG, STGPD, STGSET\rule[-5pt]{0pt}{8pt}}\\
STGFI & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
ERFIN, ERMSG, IERM1, IERV1, STGC0, STGC1, STGEXT, STGFI,
STGFND, STGSET\rule[-5pt]{0pt}{8pt}}\\
STGREC & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
ERFIN, ERMSG, IERM1, IERV1, STGC0, STGC1, STGEXT, STGFI,
STGFND, STGREC, STGSET\rule[-5pt]{0pt}{8pt}}\\
STGPRG & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
STGPRG, STGSET\rule[-5pt]{0pt}{8pt}}\\
DTGGRD & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
DSORTP, DTGGRD, DTGSET\rule[-5pt]{0pt}{8pt}}\\
DTGPD & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
DROT, DROTG, DTGPD, DTGSET\rule[-5pt]{0pt}{8pt}}\\
DTGFI & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
DTGC0, DTGC1, DTGEXT, DTGFI, DTGFND, DTGSET, ERFIN,
ERMSG, IERM1, IERV1\rule[-5pt]{0pt}{8pt}}\\
DTGREC & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
DTGC0, DTGC1, DTGEXT, DTGFI, DTGFND, DTGREC, DTGSET,
ERFIN, ERMSG, IERM1, IERV1\rule[-5pt]{0pt}{8pt}}\\
DTGPRG & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
DTGPRG, DTGSET}\\
\end{tabular}

Algorithm developed and subroutine designed and written by
Charles L. Lawson, JPL, 1976.
Adapted to Fortran 77 and the MATH77 library, 1991 and January, 1996.

\begcodenp

\lstset{language=[77]Fortran,showstringspaces=false}
\lstset{xleftmargin=.8in}

\centerline{\bf \large DRSTGFI1}\vspace{10pt}
\lstinputlisting{\codeloc{stgfi1}}
\newpage

\vspace{30pt}\centerline{\bf \large ODSTGFI1}\vspace{10pt}
\lstset{language={}}
\lstinputlisting{\outputloc{stgfi1}}

\end{document}


\documentclass[journal,hidelinks]{IEEEtran}
\usepackage[utf8]{inputenc}
\usepackage[
  pdftitle={Assignment \#2},
  pdfauthor={Andrei Purcarus},
  pdfsubject={ECSE-543 -- Numerical Methods in EE}
]{hyperref}
\usepackage{graphicx}
\usepackage[all]{hypcap}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{cleveref}
\usepackage{indentfirst}
\usepackage[per-mode=symbol]{siunitx}
\usepackage{listings}
\lstset{showstringspaces=false}

\title{ECSE-543 \\ Numerical Methods in EE \\ Assignment \#2}
\author{Andrei~Purcarus,~260631911,~\IEEEmembership{McGill~University}}

\begin{document}
\sloppy

\maketitle

\section*{Code Listings and Unit Testing}

The source code used for this assignment is listed in the appendices. In order to save space, we did not include the unit tests. For the full code, see the \href{https://github.com/Gripnook/ECSE543-F17-A2}{GitHub repository}.

\Cref{sec:q2a-py} contains the code used in Question 2 to generate the input data file for Simple2D. \Cref{sec:q2c-py} contains the code used in Question 2 to solve for the capacitance per unit length. \Cref{sec:main} contains the main function for Question 3. \Cref{sec:matrix,sec:matrix-util} define a matrix library and helper functions. \Cref{sec:cholesky} defines functions that perform Cholesky decomposition using banded and non-banded methods. \Cref{sec:solver} defines a generic solver for systems of equations that have a positive definite coefficient matrix. \Cref{sec:finite-differences-h,sec:finite-differences-cpp} define a finite difference problem generator. Finally, \Cref{sec:conjugate-gradient} defines a conjugate gradient solver for systems of equations that have a positive definite coefficient matrix.

\section*{Question 1}

We started by computing the potential for the triangle composed of nodes $(1,2,3)$. Using a linear approximation for the potential inside the triangle, we have
\begin{equation}
\label{eq:potential}
U = a + b x + c y =
\begin{bmatrix}
1 & x & y \\
\end{bmatrix}
\begin{bmatrix}
a \\
b \\
c \\
\end{bmatrix}
= \boldsymbol{x}^T \boldsymbol{\alpha}
\end{equation}

Using \Cref{eq:potential} and the potentials $U_1$, $U_2$, and $U_3$ at the nodes, we obtain
\begin{equation}
\label{eq:potential_system}
\boldsymbol{u} =
\begin{bmatrix}
U_1 \\
U_2 \\
U_3 \\
\end{bmatrix}
=
\begin{bmatrix}
1 & x_1 & y_1 \\
1 & x_2 & y_2 \\
1 & x_3 & y_3 \\
\end{bmatrix}
\begin{bmatrix}
a \\
b \\
c \\
\end{bmatrix}
=
\boldsymbol{X} \boldsymbol{\alpha} \\
\end{equation}

Using \Cref{eq:potential,eq:potential_system} together yields
\begin{equation}
U = \boldsymbol{x}^T \boldsymbol{X}^{-1} \boldsymbol{u}
\end{equation}

We next moved to finding the energy in the triangle, which is given by
\begin{align}
W &= \frac{1}{2} \int_{\Delta} |\boldsymbol{\nabla} U|^2 dS \\
  &= \frac{1}{2} \int_{\Delta} |\boldsymbol{\nabla} (\boldsymbol{x}^T \boldsymbol{X}^{-1} \boldsymbol{u})|^2 dS \\
  &= \frac{1}{2} \int_{\Delta} |\boldsymbol{Z} \boldsymbol{X}^{-1} \boldsymbol{u}|^2 dS \\
  &= \frac{1}{2} \boldsymbol{u}^T (\int_{\Delta} (\boldsymbol{Z} \boldsymbol{X}^{-1})^T \boldsymbol{Z} \boldsymbol{X}^{-1} dS) \boldsymbol{u} \\
  &= \frac{1}{2} \boldsymbol{u}^T (A (\boldsymbol{Z} \boldsymbol{X}^{-1})^T \boldsymbol{Z} \boldsymbol{X}^{-1}) \boldsymbol{u} \\
  &= \frac{1}{2} \boldsymbol{u}^T \boldsymbol{S} \boldsymbol{u}
\end{align}
where $A$ is the area of the triangle, and
\begin{equation}
\boldsymbol{Z} =
\begin{bmatrix}
0 & 1 & 0 \\
0 & 0 & 1 \\
\end{bmatrix}
\end{equation}

We then computed by hand
\begin{equation}
A_1 = \frac{1}{2} 0.02 \cdot 0.02 = \frac{1}{5000}
\end{equation}
\begin{equation}
\boldsymbol{X}^{-1}_1 =
\begin{bmatrix}
1 & 0.00 & 0.02 \\
1 & 0.00 & 0.00 \\
1 & 0.02 & 0.02 \\
\end{bmatrix}^{-1} =
\begin{bmatrix}
0 & 1 & 0\\
0 & -50 & 50 \\
-50 & 50 & 0 \\
\end{bmatrix}
\end{equation}
\begin{equation}
\boldsymbol{Z} \boldsymbol{X}^{-1}_1 =
\begin{bmatrix}
0 & -50 & 50 \\
-50 & 50 & 0 \\
\end{bmatrix}
\end{equation}
\begin{align}
\boldsymbol{S}_{dis,1} &= A_1 (\boldsymbol{Z} \boldsymbol{X}^{-1}_1)^T \boldsymbol{Z} \boldsymbol{X}^{-1}_1 \\
&= \begin{bmatrix}
0.5 & -0.5 & 0.0 \\
-0.5 & 1.0 & -0.5 \\
0.0 & -0.5 & 0.5 \\
\end{bmatrix}
\end{align}

Similarly, we computed
\begin{align}
\boldsymbol{S}_{dis,2} &= A_2 (\boldsymbol{Z} \boldsymbol{X}^{-1}_2)^T \boldsymbol{Z} \boldsymbol{X}^{-1}_2 \\
&= \begin{bmatrix}
1.0 & -0.5 & -0.5 \\
-0.5 & 0.5 & 0.0 \\
-0.5 & 0.0 & 0.5 \\
\end{bmatrix}
\end{align}

To compute the conjoint matrix $\boldsymbol{S}_{con}$, we used the fact that
\begin{align}
W &= \frac{1}{2} \boldsymbol{u}^T_{con} \boldsymbol{S}_{con} \boldsymbol{u}_{con} \\
  &= \frac{1}{2} \boldsymbol{u}^T_{dis} \boldsymbol{S}_{dis} \boldsymbol{u}_{dis}
\end{align}
where
\begin{equation}
\boldsymbol{u}_{dis} =
\begin{bmatrix}
U_1 \\
U_2 \\
U_3 \\
U_4 \\
U_5 \\
U_6 \\
\end{bmatrix} =
\begin{bmatrix}
1 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 \\
0 & 0 & 1 & 0 \\
0 & 0 & 0 & 1 \\
1 & 0 & 0 & 0 \\
0 & 0 & 1 & 0 \\
\end{bmatrix}
\begin{bmatrix}
U_1 \\
U_2 \\
U_3 \\
U_4 \\
\end{bmatrix} =
\boldsymbol{C} \boldsymbol{u}_{con}
\end{equation}
and
\begin{equation}
\boldsymbol{S}_{dis} =
\begin{bmatrix}
\boldsymbol{S}_{dis,1} & 0 \\
0 & \boldsymbol{S}_{dis,2} \\
\end{bmatrix}
\end{equation}

Putting all of this together, we obtained
\begin{align}
\boldsymbol{S}_{con} &= \boldsymbol{C}^T \boldsymbol{S}_{dis} \boldsymbol{C} \\
\label{eq:s-con}
&=
\begin{bmatrix}
1.0 & -0.5 & 0.0 & -0.5 \\
-0.5 & 1.0 & -0.5 & 0.0 \\
0.0 & -0.5 & 1.0 & -0.5 \\
-0.5 & 0.0 & -0.5 & 1.0 \\
\end{bmatrix}
\end{align}

\section*{Question 2}

\subsection*{(a)}

We divided the mesh as instructed with a uniform $\SI{0.02}{\meter}$ spacing on each axis, then subdivided each resulting square with the same triangle setup as was given in Question 1.
In order to generate the finite elements mesh file for use with Simple2D, we used the program shown in \Cref{sec:q2a-py}. This program generated the file shown in \Cref{sec:file-dat}.

\subsection*{(b)}

We then used the file in \Cref{sec:file-dat} as input to the Simple2D Matlab program provided for this assignment and exported the resulting node potentials to a file, as given in \Cref{sec:results-dat}. From this file, we can see that the potential at the point (0.06, 0.04), which corresponds to node 21, is $\SI{5.5263}{\volt}$. This agrees with the value we obtained in Assignment \#1 for the same grid spacing, which was exactly the same when using the SOR method.

\subsection*{(c)}
\label{sec:q2c}

To compute the capacitance per unit length, we used the fact that the electrical energy density $w_e$ is given by
\begin{align}
w_e &= \frac{1}{2} \epsilon_o |\boldsymbol{E}|^2 \\
    &= \frac{1}{2} \epsilon_o |\boldsymbol{\nabla} V|^2
\end{align}

We could therefore compute the total energy per unit length in a finite element square as
\begin{align}
W &= \int_{\square} w_e dS \\
  &= \int_{\square} \frac{1}{2} \epsilon_o |\boldsymbol{\nabla} V|^2 dS \\
\label{eq:capacitance}
  &= \frac{1}{2} \epsilon_o \boldsymbol{v}^T \boldsymbol{S}_{con} \boldsymbol{v}
\end{align}
where $\boldsymbol{v}$ is the electric potential vector and $\boldsymbol{S}_{con}$ is as given in \Cref{eq:s-con}.

Then, after summing up all the energies per unit length between the conductors, we could obtain the capacitance per unit length from
\begin{equation}
W = \frac{1}{2} C' V^2
\end{equation}

A program that takes the results in \Cref{sec:results-dat} and computes the capacitance per unit length is shown in \Cref{sec:q2c-py}. The result of this program is
\begin{equation}
C' = \SI{52.1}{\pico\farad\per\meter}
\end{equation}

\section*{Question 3}

\subsection*{(a)}

We first re-purposed the old code we used in Assignment \#1 to produce the finite difference mesh, and modified it to produce matrices that could be used to solve for the potentials at each node as a system of linear equations. The resulting code is shown in \Cref{sec:finite-differences-h,sec:finite-differences-cpp}. Then, we tested the matrix $\boldsymbol{A}$ to ensure that it is positive definite using the Cholesky decomposition we wrote for Assignment \#1, and the code that does this is shown in \Cref{sec:main}. The resulting output is shown in \Cref{fig:positive_definite}. We can see that the matrix $\boldsymbol{A}$ by itself is not positive definite. To make it so, we left multiply each side of $\boldsymbol{A} \boldsymbol{x} = \boldsymbol{b}$ by $\boldsymbol{A}^T$, since $\boldsymbol{A}^T \boldsymbol{A}$ is symmetric and therefore is more likely to be positive definite. As \Cref{fig:positive_definite} shows, $\boldsymbol{A}^T \boldsymbol{A}$ is indeed positive definite, and we can use this modified system of equations instead.

\begin{figure}[!htb]
  \centering
  \includegraphics[width=\columnwidth]{question-3/positive_definite.png}
  \caption{The output of the program in \Cref{sec:main} for Question 3, Part (a).}
  \label{fig:positive_definite}
\end{figure}

\subsection*{(b)}

Next, we solved the system of equations given by
\begin{equation}
\boldsymbol{A}^T \boldsymbol{A} \boldsymbol{x} = \boldsymbol{A}^T \boldsymbol{b}
\end{equation}
using both Cholesky decomposition and the conjugate gradient method. The code that performs this task is shown in \Cref{sec:main}, and the code for the solvers is shown in \Cref{sec:solver,sec:conjugate-gradient}. The resulting program outputs are shown in \Cref{fig:cholesky,fig:cg}, which show the potentials in the bottom left quarter of the conductor.

\begin{figure}[!htb]
  \centering
  \includegraphics[width=\columnwidth]{question-3/cholesky.png}
  \caption{The output of the program in \Cref{sec:main} for the Cholesky solver in Question 3, Part (b).}
  \label{fig:cholesky}
\end{figure}

\begin{figure}[!htb]
  \centering
  \includegraphics[width=\columnwidth]{question-3/cg.png}
  \caption{The output of the program in \Cref{sec:main} for the conjugate gradient solver in Question 3, Part (b).}
  \label{fig:cg}
\end{figure}

\subsection*{(c)}

When we executed the conjugate gradient solver, we also added a callback that computed the 2-norm and infinity norm for the residual at each iteration. The results are plotted in \Cref{fig:cg_results}. This figure shows that the conjugate gradient method does indeed reduce the residual (as given by the 2-norm) at each step, getting closer and closer to a solution. In addition, it converges to a tolerance of $10^{-5}$ in about $24$ steps for $24$ nodes, showing that it does indeed take $O(n)$ steps.

\begin{figure}[!htb]
  \centering
  \includegraphics[width=\columnwidth]{question-3/cg_results.eps}
  \caption{2-norm and infinity norm vs. iterations for the conjugate gradient solver.}
  \label{fig:cg_results}
\end{figure}

\subsection*{(d)}

From \Cref{fig:cholesky,fig:cg}, we found that the potential at the point (0.06, 0.04) was $\SI{5.52634}{\volt}$ for both the Cholesky solver and the conjugate gradient solver. We also found a potential of $\SI{5.5263}{\volt}$ using the finite elements method, and a potential of $\SI{5.52634}{\volt}$ using the SOR method in Assignment \#1. Clearly, all of these different methods converge to the same result, which makes sense since they all use the same grid spacing and work to achieve the same tolerance of $10^{-5}$. The main difference between them is therefore the speed of convergence.

\subsection*{(e)}

To compute the capacitance per unit length using the finite difference solution obtained, we could use \Cref{eq:capacitance} to obtain the energy between the conductors, since this formula only relies on the electric potentials at the nodes in a square mesh, and hence can be used with our finite difference mesh. The rest of the calculation would be identical to the one carried out previously.

\newpage
\onecolumn

\begin{appendices}

\section{Q2a.py}
\label{sec:q2a-py}
\lstinputlisting[language=Python]{src/q2a.py}
\newpage

\section{Q2c.py}
\label{sec:q2c-py}
\lstinputlisting[language=Python]{src/q2c.py}
\newpage

\section{File.dat}
\label{sec:file-dat}
\lstinputlisting{question-2/file.dat}
\newpage

\section{Results.dat}
\label{sec:results-dat}
\lstinputlisting{question-2/results.dat}
\newpage

\section{Main.cpp}
\label{sec:main}
\lstinputlisting[language=C++]{src/main.cpp}
\newpage

\section{Matrix.h}
\label{sec:matrix}
\lstinputlisting[language=C++]{src/matrix.h}
\newpage

\section{Matrix-util.h}
\label{sec:matrix-util}
\lstinputlisting[language=C++]{src/matrix-util.h}
\newpage

\section{Cholesky.h}
\label{sec:cholesky}
\lstinputlisting[language=C++]{src/cholesky.h}
\newpage

\section{Solver.h}
\label{sec:solver}
\lstinputlisting[language=C++]{src/solver.h}
\newpage

\section{Finite-differences.h}
\label{sec:finite-differences-h}
\lstinputlisting[language=C++]{src/finite-differences.h}
\newpage

\section{Finite-differences.cpp}
\label{sec:finite-differences-cpp}
\lstinputlisting[language=C++]{src/finite-differences.cpp}
\newpage

\section{Conjugate-gradient.h}
\label{sec:conjugate-gradient}
\lstinputlisting[language=C++]{src/conjugate-gradient.h}
\newpage

\end{appendices}

\end{document}


\providecommand{\main}{..}
\documentclass[../../main.tex]{subfiles}

\begin{document}
  \subsubsection{Solutions to Exercises}
    \begin{enumerate}
      \item This should be the minimum number of replacements. Just as a comment this
      is basically emulating a shift register.

        \begin{equation*}
          \begin{split}
            t &\leftarrow a\\
            a &\leftarrow b\\
            b &\leftarrow c\\
            c &\leftarrow d\\
            d &\leftarrow t\\
          \end{split}
        \end{equation*}

        As you can see this will result in $(b,c,d,a)$.

      \item We will define a run of an algorithm as each recursive call to the algorithm in question.

      \begin{theorem}
        If it is not the first run of the \textbf{Algorithm E}, then $m \geq n$.
      \end{theorem}

      \begin{proof}
        We will show with a proof by contradiction that if it is not first run of \textbf{Algorithm E}
        then $m \geq n$. We will label each successive run of the \textbf{Algorithm E} as a natural number
        $q$ starting from 1. We will assume that on a given run $q > 1$ that $m < n$ at step \textbf{E1}.

        \par If $m < n$ then we know that the result of step \textbf{E3} at run $q-1$ resulted in $m < n$. For that
        to have been the case we know that after step \textbf{E1} on run $q-1$, $n$ must be less than $r$. However
        this contradicts the condition that after step \textbf{E1}
        \begin{equation*}
          0 \leq r < n.
        \end{equation*}

        Thus by contradiction we have shown that if is not the first run of \textbf{Algorithm E} then $m \geq n$.
      \end{proof}
    \end{enumerate}

\end{document}


\documentclass[12]{scrartcl}
\usepackage{amssymb,amsmath,gensymb,dsfont,calc,multicol,fullpage}
\makeatletter
\newcommand\Aboxed[1]{
   \@Aboxed#1\ENDDNE}
\def\@Aboxed#1&#2\ENDDNE{%
   &
   \settowidth\@tempdima{$\displaystyle#1{}$}
   \setlength\@tempdima{\@tempdima+\fboxsep+\fboxrule}
   \kern-\@tempdima
   \boxed{#1#2}
}
\makeatother

\begin{document}

\title{Homework 9, Section 1.9; 8, 26, 30, 35 }
\author{Alex Gordon}
\date{\today}
\maketitle
\section*{Homework}
\subsection*{8.}
The matrix for the transformation is
$\begin{bmatrix} 0&-1 \\ -1&2  \end{bmatrix}$\\
\subsection*{26.}

\subsection*{30.}
By theorem 12, the columns of the standard matrix A must span $ \mathbb{R}^3$.
By theorem 4, the matrix must have a pivot in each row.
There are four possibilities for the echelon form, however they result in an inconsistent system.  Below is one.
$\begin{bmatrix} 1& * & * & * \\ 0&1 & * & * \\ 0 & 0 & 0 & 1 \end{bmatrix}$\\ where the asterisk can be any value.
\subsection*{35. A)}
If $ T : \mathbb{R}^n \rightarrow  \mathbb{R}^m maps  \mathbb{R}^n onto  \mathbb{R}^M, $ then its standard matrix A has a pivot in each row, by theorem 12 and 4. \\
This means that A must have at least as many columns as rows, so $ \mathbb{R}^m \leq  \mathbb{R}^n$. When T is one to one, A must have a pivot in each column by theorem 12, so $ \mathbb{R}^m \geq  \mathbb{R}^n$.

\end{document}


\section{Cut Property and Cycle Property}

%%%%%%%%%%%%%%%%%%%%
\begin{frame}{A generic MST algorithm}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Cut property (strong)}
  \begin{exampleblock}{Cut property (strong)}
	\begin{itemize}
	  \item Graph $G = (V, E)$
	  \item $X$ is some part of an MST $T$ of $G$
	  \item Any cut $(S, V \setminus S)$ \emph{s.t.} $X$ does not cross $(S, V \setminus S)$
	 \item Let $e$ be a lightest edge across $(S, V \setminus S)$
	\end{itemize}
	Then $X \cup \set{e}$ is some part of an MST $T'$ of $G$.
  \end{exampleblock}

  \begin{proof}
	\centerline{Exchange argument}
  \end{proof}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Cut property (strong)}
  Correctness of Prim's and Kruskal's algorithms.
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Cut property (weak)}
  \begin{exampleblock}{Cut Property \pno{3.6.18 (a)}}
    \begin{itemize}
	  \item Graph $G = (V, E)$
	  \item Any cut $(S, V \setminus S)$ where $S, V-S \neq \emptyset$
	  \item Let $e = (u,v)$ be \emph{a} minimum-weight edge across $(S, V \setminus S)$
	\end{itemize}
	Then $e$ must be in \emph{some} MST of $G$.
  \end{exampleblock}

  \begin{center}
	``a'' $\to$ ``the'' $\implies$ ``some'' $\to$ ``any''
  \end{center}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Applications of cut property}
  \begin{exampleblock}{Application of cut property (Problem 6.10)}
	\begin{enumerate}[(1)]
	  \setcounter{enumi}{2}
	  \item $e \in G$ is a lightest edge $\implies$ $e \in \exists$ MST of $G$
	  \item $e \in G$ is the unique lightest edge $\implies$ $e \in \forall$ MST of $G$
	\end{enumerate}
  \end{exampleblock}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Applications of cut property}
  \begin{exampleblock}{Wrong divide\&conquer algorithm for MST (Problem 6.14)}
    \begin{itemize}
      \item $G = (V, E, w)$
      \item $(V_{1}, V_{2}): ||V_{1}| - |V_{2}|| \le 1$
      \item $T_{1} + T_{2} + \set{e}$: $e$ is a lightest edge across $(V_{1}, V_{2})$
    \end{itemize}
  \end{exampleblock}

  \fignocaption{width = 0.30\textwidth}{figs/divide-conquer-mst-counterexample.pdf}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Cycle property (weak)}
  \begin{exampleblock}{Cycle property (Problem 6.13--2)}
	\begin{itemize}
	  \item $G = (V,E,w)$
	  \item Let $C$ be any cycle in $G$
	  \item $e = (u,v)$ is \emph{a} maximum-weight edge in $C$
	\end{itemize}
	Then $\exists \textrm{ MST } T \text{ of } G: e \notin T$.
  \end{exampleblock}

  \begin{center}
	``a'' $\to$ ``the'' $\implies$ ``some'' $\to$ ``any''
  \end{center}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Applications of cycle property}
  \begin{exampleblock}{Anti-Kruskal algorithm (Problem 6.13--3)}
	\centerline{\href{https://en.wikipedia.org/wiki/Reverse-delete_algorithm}{Reverse-delete algorithm (wiki)}}

	\[
	  O(m \log n (\log \log n)^3)
	\]
  \end{exampleblock}

  \begin{proof}
	\textbf{Invariant: } If $F$ is the set of edges remained at the end of the while loop,
	then there is some MST that are a subset of $F$.
  \end{proof}

  \begin{alertblock}{Reference}
	\begin{itemize}
	  \item ``On the Shortest Spanning Subtree of a Graph and the Traveling Salesman Problem'' by Kruskal, 1956.
	\end{itemize}
  \end{alertblock}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Application of cycle property}
  \begin{exampleblock}{(Problem 6.13--1)}
	\begin{enumerate}[(1)]
	  \item $e \notin$ any cycle of $G$ $\implies$ $e \in \forall$ MST
	\end{enumerate}
  \end{exampleblock}

  \centerline{By contradiction.}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Application of cycle property}
  \begin{exampleblock}{(Problem 6.10)}
	\begin{enumerate}[(1)]
	  \item $|E| > |V| - 1$, $e$ is the unique max-weight edge of $G$ $\implies$ $e \notin \forall$ MST
	  \item $\exists C \subseteq G$, $e$ is the unique max-weight edge of $C$ $\implies$ $e \notin \forall$ MST
	  \setcounter{enumi}{4}
	  \item Cycle $C \subseteq G$, $e \in C$ is the unique lightest edge of $G$ $\implies$ $e \in \forall$ MST
	\end{enumerate}
  \end{exampleblock}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Unique MST}
  \begin{exampleblock}{Unique MST (Problem 6.12--1)}
    Distinct weights $\implies$ Unique MST.
  \end{exampleblock}

  \[
	\Delta E = \set{e \mid e \in T_1 \setminus T_2 \lor e \in T_2 \setminus T_1}
  \]
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Unique MST}
  \begin{exampleblock}{Unique MST (Problem 6.12--2)}
	Unique MST $\centernot\implies$ Equal weights.
  \end{exampleblock}

  \fignocaption{width = 0.20\textwidth}{figs/unique-mst-unique-weight-counterexample.pdf}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Unique MST}
  \begin{exampleblock}{Unique MST (Problem 6.12--3)}
	Unique MST $\centernot\implies$ Minimum-weight edge across any cut is unique.
  \end{exampleblock}

  \fignocaption{width = 0.20\textwidth}{figs/unique-mst-cut-counterexample.pdf}

  \begin{theorem}
    Minimum-weight edge across any cut is unique $\implies$ Unique MST.
  \end{theorem}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Unique MST}
  \begin{exampleblock}{Unique MST (Problem 6.12--3)}
    Unique MST $\centernot\implies$ Maximum-weight edge in any cycle is unique.
  \end{exampleblock}

  \fignocaption{width = 0.20\textwidth}{figs/unique-mst-cycle-counterexample.pdf}

  \begin{theorem}[Conjecture]
	Maximum-weight edge in any cycle is unique $\implies$ Unique MST.
  \end{theorem}

  \fignocaption{width = 0.15\textwidth}{figs/unique-mst-cycle-noncounterexample.pdf}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Unique MST}
  \begin{exampleblock}{Unique MST (Problem 6.12--4)}
	Decide whether a graph has a unique MST?
  \end{exampleblock}

  \vspace{0.60cm}
  \begin{enumerate}
	\item Exchange argument based on cut property and cycle property
	\item Ties breaking in Prim's and Kruskal's algorithms
  \end{enumerate}
\end{frame}
%%%%%%%%%%%%%%%%%%%%


\chapter{Solution development}

\section{Behaviour design}

\subsection{NFA}

The behavior of each robot is modeled using a non-deterministic finite state automata (\textit{NFA})(see figure \ref{fig:NFA}). Such an architecture has been chosen for the robot since its behavior is not trivial and more tasks must be executed at different times. By using a FSA, it is easy to explicitly express activities, conditions and non-deterministic transitions. Moreover, each state can be developed as a separated module, and the most suitable solution can be implemented case by case.

\noindent
The robot starts in the ``resting'' state. A non-deterministic transition makes the robot leave the base and start the exploration with a probability $P1$.

\noindent
When exploring, the robot looks for any close landmark and, if it is the case, it joins the cluster around such landmark with a probability $P2$ and enters in ``waiting for cluster to complete" state. Moreover, the robot may quit the exploration task at any moment and return to the base with a probability $P3$. In the latter casuistry, the robot enters in the ``returning to base" state.

\noindent
When a cluster is completed by reaching the required number of robots $N$, the robot enters in the ``reconnaissance'' state. Such a state conceptually correspond to the phase during which the robots in the cluster should explore the area near the landmark. Such a task is not executed in this project for simplicity, but few different algorithms have been created to let a set of robots to explore and map a given area.

\noindent
The robot ends the reconnaissance task after $t_1$ seconds, and it returns to the base.

\noindent
When returning to the base, the robot executes a phototaxis task. The robot enters in the ``resting'' state once it is detected that it successfully returned to the base.

\noindent
In the end, the robot may leave the cluster before it is completed with a probability $P4$. In this case, it enters in the ``biased exploration" state, that is a state where the robot executes the regular exploration tasks
but with inhibited stimuli from landmarks. After $t_2$ seconds it returns in the normal exploration behaviour. Such a intermediate state is required so that the robot may step away from the landmark (otherwise, even though it is a probabilistic transition, it may join the cluster again suddenly.

\begin{figure}[H]
\centering
\includegraphics[width=\linewidth]{images/NFA.png}
\caption{\textit{NFA representing the behaviour of the robot. $P_i$ represent a non-deterministic transition that happens with such probability.}}
\label{fig:NFA}
\end{figure}

The implementation of the FSA is executed by using a ``states'' table to store the code be executed in each state. The variable ``current\_state'' represents the current active state, while transition are executed by checking simple``if-then-else'' conditions.

\noindent
In the end, a variable ``t'' stores the time spent by the robot in the current state.

\subsection{Motor schema}

When a state requires motion, its design has been developed using a motor schema approach and, once the total force suffered by the robot has been calculated, the following formula is used to switch from the translational-angular model to differential one (i.e., find the velocity of each wheel out from the direction and length of the total suffered force):

\begin{equation}
\begin{bmatrix}
v_l \\
v_r
\end{bmatrix}
=
\begin{bmatrix}
1 & -L/2 \\
1 & L/2
\end{bmatrix}
\begin{bmatrix}
v \\
\omega
\end{bmatrix}
\tag{3.0}\label{eq:3.0}
\end{equation}

\noindent
where:

\smallskip
\noindent
$v_l$ = velocity of the left wheel

\noindent
$v_r$ = velocity of the right wheel

\noindent
$L$ = distance between the two wheels

\noindent
$v$ = translational velocity

\noindent
$\omega$ = angular velocity

\section{Resting}

The robot just checks if a transition to the exploration state should occur, and so no particular architectural style has been adopted for this module. The following function has been used to model the probability:

\begin{equation}
    p = tanh((t - Shift) / Patience) + 1 \tag{3.1}\label{eq:3.1}
\end{equation}

\noindent
The idea is to create a probability that is directly proportional to the time spent in the current state using a non linear relation. Moreover, the probability should grow very slowly at the beginning so that the robot will remain in the resting state for a while.

\noindent
The function $tanh(x)$ is the basic function used to represent such relation\footnote{A reasonable alternative would be the exponential function, but it requires the tuning of few parameters so that the ``tail'' of the function take reasonable values} (figure \ref{fig:tanh}\footnote{The figure illustrates the regular \textit{tanh} function instead of the one exposed in formula \ref{eq:3.1} since the latter one, despite having a ``similar shape'', can hardly be graphically shown.}). The $Shift$ value is constant (500) so we can take the slice of the function having up concavity while dealing with positive time values. The $Patience$ value is a parameter used to tone all values down. In the end, the $+1$ factor let the function have positive values.

\noindent
Table \ref{tab:f-values} reports few reference values of the function.

\begin{table}[H]
\centering
\begin{tabular}{| c | c |}

\hline
t & p \\
\hline
 0  & 0.013 \\
 10 & 0.015 \\
 50 & 0.022 \\
100 & 0.036 \\
500 & 1 \\
\hline

\end{tabular}
\caption{\label{tab:f-values}\textit{Few example values of the function used to model dependency with elapsed time using a non linear relation.}}
\end{table}

\begin{figure}[H]
\centering
\includegraphics[width=8cm, keepaspectratio]{images/tanh.png}
\caption{\textit{Graph of the tanh function in range [-4;4].}}
\label{fig:tanh}
\end{figure}

\section{Exploration}

The robot executes a ballistic motion. A single potential field is created since the obstacle avoidance feature is implicitly given from the random walk itself. The direction of such potential field is randomly chosen each time an obstacle is detected, while its strength is given by a constant value ``CRUISE\_VELOCITY''.

\smallskip
When exploring, the robot has a fixed probability to quit the task. Such parameter has been tuned with a very low vale (0.001) in order to avoid too much segmented behaviours where the robots keep on fluctuating just between the two states of ``exploration'' and ``returning to base''.

\section{Clustering}

A robot may join/create a cluster when close to a landmark with a probability described by the following function:

\begin{equation}
    p =  \frac{(tanh((t - Shift) / Patience) + 1) * tanh(n / NeighborInfluenceLimiter)}{(NeighborInfluenceLimiter * PatienceEnhancer)} \tag{3.2}\label{eq:3.2}
\end{equation}

The probability is proportional to number of robots already in the cluster ($n$) and to the time spent exploring ($t$) both. The same formula in \ref{eq:3.1} has been used the time dependence term.
The parameter $NeighborInfluenceLimiter$, as the name hints, limits the influence of number of neighbors in the final result.

\noindent
The denominator is a normalization term so reasonable values can be obtained, and it can be tuned by the parameter $PatienceEnhancer$ that makes the robot wait longer as it is increased.

\noindent
Figure \ref{fig:cluster-join} reports the graph of the function in formula \ref{eq:3.2}.

\begin{figure}[H]
\centering
\includegraphics[width=\linewidth]{images/cluster_join.png}
\caption{\textit{x-axis = time, y-axis = number of neighbors, z-axis = probability. Graph of the plot in domain [-500;0], [1;500]  showing the total trend of the function (left) so the time relation can be shown well. On the right, a zoom in in range [-500; -492], [1:10] is executed to show the dependence upon neighbors (indeed, the number of robots in the cluster will never be about the previous order of magnitude ($10^2$) , and so low values, that cannot be appreciated in the left graph, are the the ones of interest.}}
\label{fig:cluster-join}
\end{figure}

Similarly, the probability for a robot to leave a cluster before it is completed is modeled using the following function:

\begin{equation}
    p =  \frac{(tanh((t - Shift) / Patience) + 1) / tanh(n / NeighborInfluenceLimiter)}{(NeighborInfluenceLimiter * PatienceEnhancer)} \tag{3.3}\label{eq:3.3}
\end{equation}

Formula \ref{eq:3.2} and \ref{eq:3.3} are exactly the same function except for a sign (division instead of multiplication of the two terms that model relation with time and number of neighbors). Indeed, the robot increases its probability to leave as time passes (direct proportionality) but, at the same time, its probability to leave is decreased as the number of perceived neighbors increases (inverse proportionality)(the more robots, the more probability to complete the cluster sooner is the reason that may lay under such behavior).

\noindent
Figure \ref{fig:cluster-leave} reports the graph of the function in \ref{eq:3.3}.

\begin{figure}[H]
\centering
\includegraphics[width=\linewidth]{images/cluster_leave.png}
\caption{\textit{Similar reasonings of figure \ref{fig:cluster-join}. The probability slowly increases as time passes (x-axis) and rapridly decreases as numbe of neighbors increases (y-axis).}}
\label{fig:cluster-leave}
\end{figure}

\section{Returning to base}

This state mixes phototaxis and obstacle avoidance, and so a motor schema approach has been used to easily blend such two tasks.

\smallskip
The phototaxis capability is created by using an attractive potential field produced by the light source. Hence, the resulting force, whose direction guides to robot toward the light source, has a value inversely proportional to the perceived light.

\smallskip
The obstacle avoidance capability is created by using a tangential field around any obstacle so the robot can circumnavigate boxes or other robots. By exploiting proximity sensors, a perpendicular force is created ($\pi/2$ from the sensor orientation, and proportional to the proximity).

\noindent
A possible issue is given from the boundary walls that may create local minima in some rare cases, but such problem may be fixed by introducing a third potential field: a perpendicular one coming from the walls. However, when travelling to base, the robot should not slip into any wall in most of the cases, and so the issue is not of primary importance for the time being.

\section{Landmark}

Landmarks have been modeled using robots too\footnote{Notice that it is not a constraint nor a simplification. Any other environmental bias (black spots etc.) would also work. Robots have been chosen so that a nice graphic with landmarks status can be seen (red/green leds) and for convenience (e.g., logging, easy to move them instead of creating lots of arenas etc.)}.

\noindent
Landmarks just have two states: not explored, explored.

\noindent
A landmark starts in the ``not explored'' state and continuously notifies all the nearby robots (see section \ref{sec:communication}).
A permanent switch to the ``explored'' state is executed once a cluster is created nearby. In this state the landmark do not send notifications any more, and so it becomes a regular object to be just avoided.

\section{Communication}\label{sec:communication}

\smallskip
Robots of the swarm communicate with landmarks and with other robots by using the range and bearing functionality. A total of three communication channels have been used, that is:

\begin{itemize}

  \item [1] \textbf{Landmark signal}: used by robots to perceive a nearby landmark.
  \item [2] \textbf{Landmark explored notification}: used to notify a landmark that it should change its state to the ``explored'' one.
  \item [3] \textbf{Neighbour signal}: used to count the number of sensed neighbors from the robots.

\end{itemize}

\noindent
The communication range is fixed for channel [1] and [2], while it varies case by case for [3]. Indeed, notice that each robot will have its own distance under which it may join the cluster. Such value is randomly selected in a fixed range, and it is required to prevent all robots stopping at the same distance from the landmark (fact that makes the creation of big clusters difficult).


\subsection{Distortion and entropy measures}

\begin{figure}
  \includegraphics[width=\columnwidth]{experiment-2D-uniform-activation-distortion.pdf}
  \caption{\textbf{Two dimensional uniform dataset (measures)}. Measure of distortion and mean activation over 10,000 samples.}%
\end{figure}

\begin{figure}
  \includegraphics[width=\columnwidth]{experiment-2D-holes-activation-distortion.pdf}
  \caption{\textbf{Two dimensional uniform dataset with holes (measures)}Measure of distortion and mean activation over 10,000 samples.}%
\end{figure}

\begin{figure}
  \includegraphics[width=\columnwidth]{experiment-3D-uniform-activation-distortion.pdf}
  \caption{\textbf{Three dimensional uniform dataset (measures)}. Measure of distortion and mean activation over 10,000 samples.}%
\end{figure}

\begin{figure}
  \includegraphics[width=\columnwidth]{experiment-MNIST-activation-distortion.pdf}
  \caption{\textbf{MNIST dataset (measures)}. Measure of distortion and mean activation over 10,000 samples.}%
\end{figure}


\documentclass[11pt]{article}
\usepackage{url}
\setlength\topmargin{-0.6cm}
\setlength\textheight{23.4cm}
\setlength\textwidth{17.0cm}
\setlength\oddsidemargin{0cm}
\begin{document}
\title{Ling 572 HW1 }
\author{Daniel Campos  \tt {dacampos@uw.edu}}
\date{01/16/2019}
\maketitle
\section{ Q1: X and Y be two random variables.}
  \begin{description}
   \item [(a)]$P(X)$
\begin{table}
\label{q1-table1}
\centering
\caption{P(X) and P(Y)}
\begin{tabular}{|r|r|r|r|r|r|}  \hline
    & X=1 & X=2 & X=3 & P(Y) \\ \hline
Y=a & 0.10 & 0.20 &  0.30 & 0.60 \\ \hline
Y=b & 0.05 & 0.15 &  0.20 &0.40 \\ \hline
P(X=x) & 0.15 & 0.35 & 0.50 & 1 \\ \hline
\end{tabular}
\end{table}
    Shown in Table 1
    \begin{equation}
     P(X=x) = P(X=1) + P(X=2) + P(X=3) = 0.15 + 0.35 + 0.5 = 1
    \end{equation}
   \item [(b)]$P(Y)$
    Shown in Table 1
    \begin{equation}
      P(Y=x) = P(Y=a) + P(Y=b) = 0.6 + 0.4 = 1
    \end{equation}
   \item [(c)]$P(X \mid Y)$
\begin{table}
\label{q1-table2}
\centering
\caption{$P(X \mid Y)$}
\begin{tabular}{|r|r|r|r|r|}  \hline
    & X=1 & X=2 & X=3 \\ \hline
Y=a & 1/6 & 1/3 &  1/2 \\ \hline
Y=b & 1/8 & 3/8 &  1/2 \\ \hline
\end{tabular}
\end{table}
     Shown in Table 2
    \begin{equation}
      P(X \mid Y) = P(X | Y=a) + P(X | Y=b)
    \end{equation}
   \item [(d)]$P(Y \mid X)$
\begin{table}
\label{q1-table3}
\centering
\caption{$P(Y \mid X)$}
\begin{tabular}{|r|r|r|r|r|}  \hline
    & X=1 & X=2 & X=3 \\ \hline
Y=a & 2/3 & 4/7 &  3/5 \\ \hline
Y=b & 1/3 & 3/7 &  2/5 \\ \hline
    \end{tabular}
    \end{table}
    Shown in table 3
   \item [(e)]Are X and Y independent? No since for two random variables to be independent P(X|Y) = P(X) and P(Y|X) = P(Y) and that is not the case.
   \item [(f)]$H(X)$
    \begin{equation}
      H(X) = -\displaystyle\sum_{x} p(x)\log p(x).
    \end{equation}
    \begin{equation}
     H(X) = H(X=1)+H(X=2)+H(X=3) = 1.44065
   \end{equation}
   \item [(g)]$H(Y)$
   \begin{equation}
     H(Y) = H(Y=a)+H(Y=b) = 0.97095
    \end{equation}
   \item [(h)]$H(X,Y)$
   \begin{equation}
     H(X,Y) = H(X=1,Y=a)+H(X=2,Y=a)+H(X=3,Y=a)
   \end{equation}
\begin{equation}
+H(X=1,Y=b)+H(X=2,Y=b)+H(X=3,Y=b) = 2.408694
\end{equation}
   \item [(i)]$H(X \mid Y)$
	   \begin{equation}
     H(X|Y) = H(X,Y) - H(Y) = 2.408694-0.97095 = 1.4377440000000001
   \end{equation}
   \item [(j)]$H(Y \mid X)$
	   \begin{equation}
     H(Y|X) =  H(X,Y) - H(X) = 2.408694 - 1.44065= 0.9680440000000001
   \end{equation}
   \item [(k)]$MI(X,Y)$
   \begin{equation}
     MI(Y|X) = \displaystyle\sum_{x}\sum_{y}p(x,y)log((p(x,y)/(p(x)p(y)) = 0.002901074507172899
   \end{equation}
   \item [(l1)]What is the value for $KL(P(X,Y) \mid\mid Q(X,Y))$?
      \begin{equation}
     KL(p,q) = \displaystyle\sum_{x}p(x)log2(p(x)/p(q))= 0.10212999408564584
   \end{equation}
   \item [(l2)]What is the value for $KL(Q(X,Y) \mid\mid P(X,Y))$?
      \begin{equation}
     KL(p,q) = \displaystyle\sum_{x}p(x)log2(p(x)/p(q))= 0.07646881528770542
   \end{equation}
   \item [(l3)]Are they the same? No
  \end{description}
\section{ Q2: Random Variable from coin toss}
 \begin{description}
  \item [(a)]Formula for $H(X)$.
   \begin{equation}
H(X) = -\displaystyle\sum_{x} p(x)\log p(x).
\end{equation}
  \item [(b)]What is $p^*$? \\ .368 with a entropy of 0.530737816926673
  \item [(c)]Prove that the answer you give in (b) is correct. \\ To find the maximum value we can take the derivative of the equation and solve for y= 0. This gives us x = .368.
   \begin{equation}
H'(X)=\log p(x) +1)/log(2) = 0
\end{equation}
 \end{description}
\section{ Q3: Permutations and combinations}
 \begin{description}
  \item [(a)]How many distinct ways are there to form the teams for the class(Including formula)? \\For 2, ways = 1, for 4 = 3,
\begin{equation}
Teams(n) = ((n-1)!/(2!*((n-1)-2)!)
\end{equation}
  \item [(b)]How many different color sequences are there? \\Since each color sequence acts independently the amount of cololor sequences is n! the amount of balls in that color
\begin{equation}
Sequences() = 5!*3!*2! = 1440
\end{equation}
  \item [(c1)]How many different word sequences are there which contain exactly $t_i$ $w_i$'s for each $w_i$ in $\Sigma$? \\ To figure this out I started out with a Vocabulary of 0 and 1s of various sizes and tried to understand the document size and the possibilies and how many documents match the desired conditions. Looking at a bunch of examples I was able to confirm that there are N! different word sequences that contain the correct frequency
  \item [(c2)]What is the probability that you will end up with a document where the occurrence of the word $w_i$ (for each $w_i \in \Sigma$) in the document is exactly $t_i$? \\
Where V is the length of the word array
\begin{equation}
P =\prod_{i=1}^n N!/V!
\end{equation}
 \end{description}
\section{ Q4: POS Tagger}
 \begin{description}
  \item [(a)]Write down the formula for calculating $P(w_1, ..., w_n, t_1, ..., t_n)$,
\begin{equation}
 P =\prod_{i=1}^n P(wi|ti)P(ti|ti-2, ti-1...ti-n)
\end{equation}
  \item [(b1)]What does each state in HMM correspond to? \\ In an HMM each state corresponds to a word in a sentence or chain.
  \item [(b2)]How many states are there? \\Depending on how large of a model we want to train whatever our look back window is. N states
\item [(b3)]What probabilities in the formula for (a) the transition probability is the $p_i$ given $t_(i-1)$  and transmison probability is $t_i$ given $w_i$
 \end{description}
\section{ Q5: POS Classifier}
 \begin{description}
  \item [(a)]How many unique features are there? \\ 4V + $3V^2$
  \item [(b)]What is x? what is y? The X is a current word while the Y is the predicted POS tag for the current word.
  \item [(c)]For the sentence {\bf Mike/NN likes/VBP cats/NNS},write down the feature vector for each word in the sentence.\\ Mike NN PreviousWord BOS CurrentWord Mike NextWord likes surroundingWords (BOS,likes) PreviousTag BOS Prev2tags (BOS,BOS) \\ likes VBP PreviousWord Mike CurrentWord likes NextWord cats surroundingwords (Mike,cats) PreviousTag NN Prev2tags(NN,BOS) \\ cats NNS PreviousWord likes CurrentWord cats surroundingwords (likes,EOS)  PreviousTag VBP Prev2tags(NN, VBP)
 \end{description}
\section{Q6: Language Identifier}
 \begin{description}
  \item [(a)]How do you plan to build the LangID system? \\ I would treat this as a classification problem. My input would be a document and the output would be a ID which corresponds to a specific langauge. A good set of features would be characters(as in what unicode characters), word unigrams, word bigrams, word trigrams along with POS unigrams, POS bigrams and POS trigrams.
  \item [(b)]What factors could affect the system performance? \\ The origin of the training data could really affect the training data because some documents in some langauges may come from new and others may come from literature. The system could also be affected by how similair much of the training data is, if its too similair or has too much overlap making a confident model will be trickly. Finally, since my model would use POS tagging to predict langauge the quality of our POS tagger would be hugely impactful. If the POS tagger is low quality tahn we would just learn noise.
 \end{description}
\end{document}


\section{Overview}

In the main paper, we empirically studied the connection between adversarial robustness (in terms of the \emph{robust} loss \RCE, \ie, the cross-entropy loss on adversarial examples) and flatness of the \RCE landscape \wrt changes in the weight space. In this context, we also consider the phenomenon of robust overfitting \cite{RiceICML2020}, \ie, that robustness on training examples increases consistently throughout training while robustness on test examples eventually \emph{decreases}. Based on average- and worst-case metrics of flatness in \RCE, which we ensure to be scale-invariant,  we show a \textbf{clear relationship between adversarial robustness and flatness}. This takes into account many popular variants of adversarial training (AT), \ie, training on adversarial examples: TRADES \cite{ZhangICML2019}, MART \cite{WangICLR2020}, AT-AWP \cite{WuNIPS2020} AT with self-supervision \cite{HendrycksNIPS2019} or additional unlabeled examples \cite{CarmonNIPS2019}. All of them improve adversarial robustness \emph{and} flatness. Vice versa, approaches known to improve flatness, \eg, Entropy-SGD \cite{ChaudhariICLR2017}, weight clipping \cite{StutzMLSYS2021} or weight averaging \cite{GowalARXIV2020} also improve adversarial robustness. Finally, we found that even simple regularization schemes, \eg, AutoAugment \cite{CubukARXIV2018}, weight decay or label noise, also improve robustness by finding flatter minima.

\subsection{Contents}

This supplementary material is organized as follows:
\begin{itemize}
	\item \secref{sec:supp-related-work}: additional discussion of \textbf{related work}.
	\item \secref{sec:supp-visualization}: details on \textbf{\RCE landscape visualization} and comparison to \cite{LiNIPS2018} (\cf \figref{fig:supp-visualization} and \ref{fig:supp-visualization-li}).
	\item \secref{sec:supp-flatness-computation}: details on how to \textbf{compute our average- and worst-case flatness measures}, including ablation studies in \secref{sec:supp-flatness-ablation} (\cf \figref{fig:supp-flatness-epochs}, \ref{fig:supp-flatness-misc} and \ref{fig:supp-flatness-ablation}).
	\item \secref{sec:supp-scaling}: discussion of \textbf{scale-invariance} of our visualization and flatness measures (\cf \figref{fig:supp-scaling} and \tabref{tab:supp-convexity}).
	\item \secref{sec:supp-setup}: specifics on our \textbf{experimental setup} (training and evaluation details).
	\item \secref{sec:supp-methods}: discussion of all individual \textbf{methods}, including ablation regarding hyper-parameters in \secref{sec:supp-methods-ablation} (\cf \figref{fig:supp-ii-pll} and \ref{fig:supp-training-ablation}) and flatness in \secref{sec:supp-methods-flatness} (\cf \figref{fig:supp-methods-flatness-epochs} and \ref{fig:supp-flatness-methods}). Training curves for all methods in \figref{fig:supp-training-curves}.
	\item \secref{sec:supp-results}: all \textbf{results in tabular form} (\tabref{tab:supp-table-robustbench-error}, \ref{tab:supp-table-robustbench-loss}, \ref{tab:supp-table-loss}, \ref{tab:supp-table-error})
\end{itemize}