\documentclass[twoside]{MATH77}
\usepackage[\graphtype]{mfpic}
\usepackage{multicol}
\usepackage[fleqn,reqno,centertags]{amsmath}
\begin{document}
\opengraphsfile{pl02-03}
\begmath 2.3 Gamma and Log-Gamma Functions

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

\subsection{Purpose}

These subprograms compute values of the gamma function and the natural
logarithm of the gamma function, \cite{ams55} and~\cite{Hart:1968:CA:gam}.
The gamma function is defined by%
\begin{equation*}
\Gamma (x)=\int_0^\infty e^{-s}s^{x-1}ds.
\end{equation*}
For integer values of $n\geq 0$, it satisfies the relation
\begin{equation*}
n!=\Gamma (n+1).
\end{equation*}
\subsection{Usage}

\subsubsection{Program Prototype, Single Precision}

\begin{description}
\item[REAL]  \ {\bf SGAMMA, SLGAMA, X, Y}
\end{description}

Assign a value to X and obtain gamma or the natural logarithm of gamma,
respectively, through use of the function statements:
$$
\fbox{{\bf Y =SGAMMA(X)}} \hspace{.5in} \fbox{{\bf Y =SLGAMA(X)}}
$$

\subsubsection{Argument Definition}

\begin{description}
\item[X]  \ [in] Argument at which function evaluation is desired. See
Section E for restrictions on X.
\end{description}

\subsubsection{Modifications for Double Precision}

For double precision usage, change the REAL type statement to DOUBLE
PRECISION and change the function names from SGAMMA to DGAMMA and SLGAMA to
DLGAMA.

\subsubsection{Program Prototype, Complex}

\begin{description}
\item[INTEGER]  \ {\bf MODE}

\item[COMPLEX]  \ {\bf CARG, CVAL}

\item[REAL]  \ {\bf ERREST}
\end{description}

Assign the complex argument in CARG. Set MODE to 0 or 1.

\begin{center}
\fbox{%
\begin{tabular}{@{\bf }c}
CALL CGAM(CARG, CVAL,\\
ERREST, MODE)
\end{tabular}}
\end{center}

The complex result will be in CVAL with an error estimate in ERREST.

\subsubsection{Argument Definitions}

\begin{description}
\item[CARG]  \ [in] Complex argument. The argument must not be zero or a
negative real integer value.

\item[CVAL]  \ [out] Computed complex value of either log-gamma or gamma.
The imaginary part of log-gamma, say $v$, will be standardized to satisfy $%
-\pi <v\leq \pi .$

\item[ERREST]  \ [out] On return ERREST gives an estimate of the absolute
error (for log-gamma) or the relative error (for gamma) of the computed
value.

\item[MODE]  \ [in] Set value to 0 for log-gamma and 1 for gamma.
\end{description}

\subsubsection{Program Prototype, Double Precision Complex}

\begin{description}
\item[INTEGER]  \ {\bf MODE}

\item[DOUBLE PRECISION]  \ {\bf CARG}(2){\bf , CVAL}(2){\bf , ERREST}
\end{description}

Assign the complex argument in CARG(), with real part in CARG(1) and
imaginary part in CARG(2). Set MODE to 0 or 1.

\begin{center}
\fbox{%
\begin{tabular}{@{\bf }c}
CALL ZGAM(CARG, CVAL,\\
ERREST, MODE)
\end{tabular}}
\end{center}

The result will be in CVAL() with an error estimate in ERREST.

\subsubsection{Argument Definitions}

\begin{description}
\item[CARG()]  \ [in] Array of 2 values representing the complex argument,
CARG(1) for the real part and CARG(2) for the imaginary part. The argument
must not be zero or a negative real integer value.

\item[CVAL()]  \ [out] Array of 2 values representing the complex function
value returned: CVAL(1) and CVAL(2) for real and imaginary parts,
respectively. The imaginary part of log-gamma, say $v$, will be standardized
to satisfy $-\pi <v\leq \pi .$

\item[ERREST]  \ [out] On return ERREST gives an estimate of the absolute
error (for log-gamma) or the relative error (for gamma) of the answer.

\item[MODE]  \ [in] Set value to 0 for log-gamma and 1 for gamma.
\end{description}

Although the Fortran 77 standard does not support a double precision complex
type, many vendors do provide for this using a declaration COMPLEX*16 or
DOUBLE COMPLEX. With such a compiler one can probably use ZGAM with its
first two arguments declared as double precision complex.

\subsection{Examples and Remarks}

\subsubsection{Example}

The gamma function satisfies a ``duplication'' identity which may be written
as%
\begin{equation*}
z=\frac{2\sqrt{\pi }\Gamma (x)}{2^x\Gamma (x/2)\Gamma (x/2+\frac 12)}-1=0.
\end{equation*}
The listing of DRSGAMMA and ODSGAMMA gives an example of using these
subprograms to evaluate this identity. The program DRCGAM and output ODCGAM
illustrate the use of CGAM.

\subsubsection{Remarks}

Neither the Fortran 77 standard nor the Fortran 90 standard includes
intrinsic functions for gamma or log-gamma; however, such functions with the
names GAMMA, DGAMMA, LGAMMA, ALGAMA, and DLGAMA are provided as intrinsic
functions in some vendors' Fortran systems. In particular these latter
functions are provided with the UNISYS ASCII Fortran and IBM VS-Fortran, but
not with the VAX-11 Fortran. In the UNISYS and IBM systems GAMMA and LGAMMA
are ``generic" names and thus can be used with either single or double
precision arguments.

In a system having DGAMMA or DLGAMA as intrinsics a reference to one of
these function names will cause the vendor-supplied code to be used. If one
wishes to override this and use the code from this library one must declare
the function name to be EXTERNAL in the referencing program unit.

\subsection{Functional Description}

The gamma function is defined and takes positive values for all real $x>0$,
becoming unbounded both as $x\rightarrow 0$ and as $x\rightarrow +\infty $.
As $x\rightarrow 0$, $\Gamma (x)$ is asymptotic to $1/x$, and as $%
x\rightarrow \infty $, $\Gamma (x)$ is asymptotic to%
\begin{equation*}
h(x)=\left( x/e\right) ^x\sqrt{2\pi /x}.
\end{equation*}
As an estimate of the size of $\Gamma (x)$ for large positive real $x$, note
that for $x>9$, $\Gamma (x)$ satisfies $h(x)<\Gamma (x)<1.01\ h(x)$. The
gamma function is also defined for negative $x$ except at the negative
integers where it has poles.

As a function of a complex variable the gamma function is analytic
throughout the complex plane except for poles at zero and the negative real
integer points. See \cite{ams55} and \cite{Hart:1968:CA:gam} for further
discussion of the gamma function.

The subprograms SGAMMA, DGAMMA, SLGAMA, and DLGAMA are based on subprograms
developed by W.J. Cody, Argonne National Laboratory, which were designed for
$10^{-20}$ precision. These subprograms use rational minmax approximations
on selected subintervals as well as asymptotic formulas and argument
reduction techniques. The degrees of the rational approximations are not
varied between the single precision and double precision subprograms or as
a function of the host computer's precision.
\vspace{10pt}

\hspace{5pt}\mbox{\input pl02-03a }

Subprograms CGAM and ZGAM are based on \cite{Kuki:1972:CGF}, making use of
the Stirling asymptotic series and recursive formulas.  The accuracy
adjusts to the host machine up to about 17 significant decimal digits.
CGAM and ZGAM reference I1MACH(10) to find the radix of the host system's
floating point arithmetic.

\subparagraph{Accuracy tests}

Accuracy tests were run on a Univac 1108 in 1969 and on a Univac 1100 in
1983. The arithmetic precision of these systems is $\rho _1 = 2^{-27}
\approx 0.$ 745E$-$8 for single precision and $\rho _2 = 2^{-60}
\approx 1.15$E$-$18 for double precision.

Subprograms SGAMMA and SLGAMA were tested in 1983 on a Univac 1100 by
comparison with DGAMMA and DLGAMA at 18,000 points. The test results may be
summarized as follows:

\begin{center}
\begin{tabular}{l@{}lr}
& \multicolumn{1}{c}{\bf Argument} & \multicolumn{1}{c}{\bf Max. Rel.}\\
\multicolumn{1}{l}{\bf Function} & \multicolumn{1}{c}{\bf Interval} &
\multicolumn{1}{c}{\bf Error}\\
SGAMMA & \rule{.17in}{0pt}[0., 1.] & $2.5\rho _1$\rule{.12in}{0pt}\\
& \rule{.17in}{0pt}[1., 2.] & $1.4\rho _1$\rule{.12in}{0pt}\\
& \rule{.17in}{0pt}[2., 10.] & $9.2\rho _1$\rule{.12in}{0pt}\\
& \rule{.17in}{0pt}[10., 17.] & $100.0\rho _1$\rule{.12in}{0pt}\\
& \rule{.17in}{0pt}[17., 30.] & $180.0\rho _1$\rule{.12in}{0pt}\\
SLGAMA & \rule{.17in}{0pt}[0.0, 0.5] & $2.7\rho _1$\rule{.12in}{0pt}\\
& \rule{.17in}{0pt}[0.5, 4.0] & $6.5\rho _1$\rule{.12in}{0pt}\\
& \rule{.17in}{0pt}[4.0, 35.0] & $2.8\rho _1$\rule{.12in}{0pt}
\end{tabular}
\end{center}

To test the double precision functions, and as an additional test of the
single precision functions, the function $z(x)$ defined in Section C and
a logarithmic form of $z(x)$ defined as

\hspace{.2in}$w(x) =\text{LGAM}(x) + 0.5\ \ln (2\pi ) + (0.5 - x) \ln(2)$
\vspace{-2pt}

\hspace{.8in}$- \text{LGAM}(0.5x) - \text{LGAM}(0.5x + 0.5) = 0$

were evaluated on a Univac 1100 in 1983 at about 100 points. These tests are
summarized as follows (``Max. Error" is the maximum magnitude of
the test function):

\begin{tabular}{@{}l@{}l@{\ \ }c@{ }r}
\multicolumn{1}{@{}c}{\bf Subprogram} & \multicolumn{1}{c}{\bf Argument} &
\multicolumn{1}{c}{\bf Test} & \multicolumn{1}{c}{\bf Max.}\\
\multicolumn{1}{@{}c}{\bf Used} & \multicolumn{1}{c}{\bf Interval} &
\multicolumn{1}{c}{\bf Function} & \multicolumn{1}{c}{\bf Error}\\
SGAMMA & [$-$20.75, $-$11.25] & $z$ & $114\rho _1$\\
 & [$-$11.25, 11.50] & $z$ & $6\rho _1$\\
 & [11.50, 34.00] & $z$ & $100\rho _1$\\
DGAMMA & [$-$20.75, 43.60] & $z$ & $130\rho _2$\\
 & [43.60, 106.80] & $z$ & $384\rho _2$\\
 & [106.80, 170.00] & $z$ & $1186\rho _2$\\
SLGAMA & [0.0625, 3.1] & $w$ & $3\rho _1$\\
 & [3.1, 11.5] & $w$ & $8\rho _1$\\
 & [11.5, 0.12E37] & $w/\text{SLGAMA}$ & $2.7\rho _1$\\
DLGAMA & [0.0625, 12.0] & $w$ & $110\rho _2$\\
 & [12.0, 0.77D305] & $w/\text{DLGAMA}$ & $2.9\rho _2$
\end{tabular}

Subprogram ZGAM was tested in 1969 on a Univac 1108. The maximum relative
error noted for complex arguments, $x$, satisfying $|x| < 30$, was $345\rho
_2$ for gamma and $93\rho _2$ for log-gamma. The test function, $z$, was
evaluated at a few complex points, $x$, of magnitude near one on a Univac
1100 in 1983. The maximum value of $|z|$ was $30\rho _2.$

\bibliography{math77}
\bibliographystyle{math77}

\subsection{Error Procedures and Restrictions}

Subprograms SGAMMA and DGAMMA accept negative as well as positive arguments.
These subprograms will issue an error message and return the largest machine
number, $\Omega $, if $x$ is zero or a negative integer, or if $x \geq x_g.$

The subprograms SLGAMA and DLGAMA are designed only for positive arguments.
These subprograms will issue an error message and return the value, $\Omega $%
, if $x \leq 0$, or if $x \geq x_\lambda .$

The complex gamma subroutines CGAM and ZGAM issue an error message and
return the complex value $(\Omega $,\ $\Omega )$ if the argument is zero, a
negative real integer, or a large argument that would cause overflow.

Let $\Omega $ denote the machine overflow limit. Let $x_g$ denote the value
of $x$ for which $\Gamma (x) = 0.875\ \Omega $, and let $x_\lambda $ denote
the value of $x$ for which $\ln (\Gamma (x)) = 0.875\ \Omega $. Some
examples of these values follow.

\begin{center}
\begin{tabular}{lrl}
{\bf Computing System}&{\bf $x_g $}\rule{3pt}{0pt}&\ \ \ {\bf $x_\lambda $}\\
VAX 11/780, SP \& DP & 34.8 & 1.8e36\\
UNISYS (Sperry) 1100, SP & 34.5 & 1.8e36\\
IEEE Math processor, SP & 35.0 & 3.6e36\\
IBM Mainframe 3xxx, DP & 57.5 & 3.8e73\\
UNISYS (Sperry) 1100, DP & 171.5 & 1.1e305\\
IEEE Math processor, DP & 171.6 & 2.2e305\\
Cray, SP \& DP & 966.9 & 8.5e2461
\end{tabular}
\end{center}

The values listed above are computed on a first-time flag at run time making
use of the overflow limit obtained from R1MACH(2) or D1MACH(2).

\subsection{Supporting Information}

\begin{tabular}{@{\bf}l@{\hspace{5pt}}l}
\bf Entry & \hspace{.35in} {\bf Required Files}\vspace{2pt} \\
CGAM & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
AMACH, CGAM, ERFIN, ERMSG, SERM1, SERV1\rule[-5pt]{0pt}{8pt}}\\DGAMMA & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
AMACH, DERM1, DERV1, DGAMMA, ERFIN, ERMSG\rule[-5pt]{0pt}{8pt}}\\DLGAMA & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
AMACH, DERM1, DERV1, DGAMMA, DLGAMA, ERFIN, ERMSG\rule[-5pt]{0pt}{8pt}}\\SGAMMA & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
AMACH, ERFIN, ERMSG, SERM1, SERV1, SGAMMA\rule[-5pt]{0pt}{8pt}}\\SLGAMA & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
AMACH, ERFIN, ERMSG, SERM1, SERV1, SGAMMA, SLGAMA\rule[-5pt]{0pt}{8pt}}\\ZGAM & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
AMACH, DERM1, DERV1, ERFIN, ERMSG, ZGAM\rule[-5pt]{0pt}{8pt}}\\\end{tabular}

Subprograms SGAMMA, DGAMMA, SLGAMA, and DLGAMA were developed by W.J. Cody,
Argonne National Lab., 1982, and adapted to the JPL MATH 77 library by C.
Lawson and S. Chiu, JPL, 1983.

Subroutines CGAM and ZGAM were developed by H. Kuki with the name CDLGAM
(\cite{Kuki:1972:CGF} above), adapted for JPL Univac 1108 usage by E.W.~%
Ng, 1969, and adapted to the JPL MATH 77 library by C.~Lawson and S.~%
Chiu, JPL, 1983.

\begcodenp

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

\centerline{\bf \large DRSGAMMA}\vspace{10pt}
\lstinputlisting{\codeloc{sgamma}}

\vspace{20pt}\centerline{\bf \large ODSGAMMA}\vspace{10pt}
\lstset{language={}}
\lstinputlisting{\outputloc{sgamma}}

\newpage

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

\centerline{\bf \large DRCGAM}\vspace{10pt}
\lstinputlisting{\codeloc{cgam}}

\vspace{20pt}\centerline{\bf \large ODCGAM}\vspace{10pt}
\lstset{language={}}
\lstinputlisting{\outputloc{cgam}}

\closegraphsfile
\end{document}


\documentclass[10pt]{article}

\usepackage{color,times,graphicx,epstopdf,fancyhdr,amsfonts,amsthm,amsmath,algorithm,algorithmic,xspace,hyperref}
\usepackage[left=1in,top=1in,right=1in,bottom=1in]{geometry}
\usepackage{sect sty}	%For centering section headings
\usepackage{enumerate}	%Allows more labeling options for enumerate environments
\usepackage{epsfig}
\usepackage[space]{grffile}
\usepackage{booktabs}

% This will set LaTeX to look for figures in the same directory as the .tex file
\graphicspath{.} % The dot means current directory.

\pagestyle{fancy}

\lhead{\YOURID}
\chead{Square Root List}
\rhead{\today}
\lfoot{Williams College}
\cfoot{\thepage}
\rfoot{Fall 2020}

% Some commands for changing header and footer format
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\headwidth}{\textwidth}
\renewcommand{\footrulewidth}{0.4pt}
\newcommand{\nonterm}[1]{$\langle$#1$\rangle$}

% These let you use common environments
\newtheorem{claim}{Claim}
\newtheorem{definition}{Definition}
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}
\newtheorem{observation}{Observation}
\newtheorem{question}{Question}

\setlength{\parindent}{0cm}

\setlength{\columnseprule}{0.1pt}
\def\columnseprulecolor{\color{black}}

% Edit below as instructed
\newcommand{\YOURID}{Petros Markopoulos}
\newcommand{\ProblemHeader}

\begin{document}

\vspace{\baselineskip}	% Add some vertical space

\section{Introduction}
\subsection{What is a Linked List}
A Linked List is a simple data structure consisting of what is usually referred to as \textit{Nodes}. A Node contains some piece of data as well as a pointer to the next node (it can also contain a pointer to the previous node, to create a Doubly Linked List, which is actually what is used in the implementation of SqrtList). Thus, the whole list can be accessed by starting at the first node (usually referred to as the \textit{head}) and following the pointers until the end of the list (\textit{tail}) is reached. A Linked List does not have random access, and thus although given a specific node all operations (inserting a new node next to it, removing the node, getting its value or setting its value) are done in $O(1)$ time, doing the same operations given an \textit{index} instead of a node are all done in $O(n)$ time (where $n$ is the length of the list), as, in the worst case, the entire list needs to be traveresed to access the node at that index and perform the operation.

\begin{figure}[hbp]
	\centering
	\textbf{A Singly Linked List}\par\medskip
	\includegraphics[width=.5\textwidth]{./img/singly_linked_list.png}
\end{figure}
\begin{figure}[hbp]
	\centering
	\textbf{A Doubly Linked List}\par\medskip
	\includegraphics[width=.5\textwidth]{./img/doubly_linked_list.png}
\end{figure}
\subsection{What is the Square Root List}
The \textit{Square Root List} is an idea I had after examining Linked Lists in my Data Structures class and it is an attempt to improve on the runtime of the aforementioned operations by storing some extra pointers along with the real list. A second list is maintained, which stores pointers for about every $\sqrt{n}$ elements of the main list, where $n$ is the length of the main list. When an element of the main list needs to be accessed, instead of traversing the main list directly, the appropriate element is selected from the secondary list as the traversal starting point, thus "skipping" over many elements; then the traversal of the main list starts and the element is reached. We will show later that this traversal is done in $O(\sqrt{n})$ time.
\begin{figure}[hbp]
	\centering
	\textbf{A Square Root List}\par\medskip
	\includegraphics[width=.7\textwidth]{./img/square_root_list.png}
\end{figure}
\section{Operation Runtime for the Square Root List}
\subsection{Notes}
Before showing that the runtime is indeed what was claimed before, a point needs to be clarified. I said that the secondary list is about $\sqrt{n}$ elements long. That "about" means the following: because add and remove operations are contantly changing the length $n$ of the list, instead of constantly keeping up with those changes, the list uses another number $m$ as a stand-in for the length of the list. $m$ is updated in the buildMeta() method of the class, where it is set to be equal to the real size of the list. buildMeta is only called when the size of the list has diverged too much from the value of $m$. When an insertion happens but the threshold for calling buildMeta is not surpassed, then retreat is called. retreat traverses the secondary list from the site of the insertion until the end and updates each stored node to be equal to its next node. This ensures that the chunks between two stored nodes remain equally sized and that the list works correctly. Similarly, when a removal happens, advance is called which works identically to retreat, but updates nodes to be their previous node instead of their next.
\subsection{Proofs}
Claim: $\sqrt{m}$ is bounded by $O(\sqrt{n})$.
\begin{proof}
	$ $\\
	We can see from the code that $m - \sqrt{m} \leq n \leq m + \sqrt{m}$, since when either of those bounds is exceeded, buildMeta is called and then $m = n$.\\
	$m - \sqrt{m} \leq n \iff m - \sqrt{m} - n \leq 0$ (inequality (1))\\
	By setting $x = \sqrt{m}$ inequality (1) becomes $x^2 - x - n \leq 0$ (inequality (2))\\
	$x^2 - x - n = 0 \iff x = \frac{1 + \sqrt{1 + 4n}}{2} \; or \; x = \frac{1 - \sqrt{1 + 4n}}{2}$\\
	Therefore inequality (2) is satisfied by $\frac{1 - \sqrt{1 + 4n}}{2} \leq x \leq \frac{1 + \sqrt{1 + 4n}}{2}$.\\
	This gives us that $0 \leq \sqrt{m} \leq \frac{1 + \sqrt{1 + 4n}}{2}$.\\
	$\sqrt{m} \leq \frac{1}{2} + \frac{\sqrt{1 + 4n}}{2} \leq \frac{\sqrt{n}}{2} + \frac{\sqrt{5n}}{2}$\\
	Thus $\sqrt{m} \leq \frac{\sqrt{5} + 1}{2}\sqrt{n}$ and therefore $\sqrt{m}$ is bounded by $O(\sqrt{n})$.

\end{proof}

Claim: The getNode(idx) method works in $O(\sqrt{n})$ time.
\begin{proof}
	$ $\\
	The appropriate starting node from the secondary list is selected in constant time. Then, a traversal is needed, from the node stored in the secondary list up to the desired index. That traversal has at most $\sqrt{m}$ nodes, since, the secondary list stores one node every $\sqrt{m}$ nodes. Thus, the entire traversal is at most $\sqrt{m} - 1$ nodes long, which means that it is bounded by $O(\sqrt{m})$ and thus $O(\sqrt{n})$.
\end{proof}

Claim: The get(idx) method works in $O(\sqrt{n})$ time.
\begin{proof}
	$ $\\
	The method consists of a single call to getNode(idx)a field access. Since getNode(idx) works in $O(\sqrt{n})$ time, this method does too.
\end{proof}

Claim: The set(idx) method works in $O(\sqrt{n})$ time.
\begin{proof}
	$ $\\
	The method consists of a single call to getNode(idx) and a value update. Since getNode(idx) works in $O(\sqrt{n})$ time, this method does too.
\end{proof}

Claim: The insert(val, idx) method works in amortized $O(\sqrt{n})$ time.
\begin{proof}
	$ $\\
	The method calls getNode(idx) which is $O(\sqrt{n})$ and then creates a new node (which takes constant time with respect to the length of the list).\\
	In every call it will make a call to retreat(inseartionIdx), which traverses the entire secondary list and thus takes $O(\sqrt{m})$ time.\\
	Once every $\sqrt{m}$ calls it will make a call to $buildMeta()$ which traverses the entire main list so it takes $O(n)$ time. Distributing the cost of that call over the $\sqrt{m}$ calls yields $O(\frac{n}{\sqrt{m}})$ time.\\
	Therefore, overall, insert(val, idx) runs in $O(\sqrt{n}) + O(\sqrt{m}) + O(\frac{n}{\sqrt{m}})$ time, which is bounded by $O(\sqrt{n})$.
\end{proof}

Claim: The remove(idx) method works in amortized $O(\sqrt{n})$ time.
\begin{proof}
	$ $\\
	This method works identically with the insert(val, idx) method, and therefore has the same runtime.
\end{proof}

\end{document}


\numberwithin{definition}{section}
\numberwithin{example}{section}
\numberwithin{equation}{section}
\numberwithin{figure}{section}

\addbibresource{../bib/taiga.bib}
\addbibresource{../bib/tex.bib}
%-----------------------------------------------------------------
\title{Flat predictive models}
\author{\textsc{John Alan McDonald }}
\date{draft of \today}
%-----------------------------------------------------------------
\begin{document}
\maketitle
%-----------------------------------------------------------------

Taiga's implementation of linear models is expressed in a way
that is different from the treatment in most statistics texts.
I happen to think that Taiga's approach is simpler, and better,
particularly because it allows us to discuss these models as
special cases of a general approach to predictive models.
Whether you agree with that or not, you will need to understand
Taiga's point of view if you want to use it successfullly.

\textit{Flat} means linear or affine.

\section{\label{sec:Linear} Linear spaces and functions}

A \textit{real linear space}
(commonly refered to as a
\textit{real vector space})
\(\mathbb{V}\) is a set of elements, called vectors,
\(\mathsf{v}_0\), \(\mathsf{v}_1\), \ldots that is closed under
\textit{linear combinations}:
\begin{equation}
\mathsf{v} \; = \; a_0 \mathsf{v}_0 \, + \, a_1 \mathsf{v}_1
\in \mathbb{V}
\end{equation}
for all \(a_0, a_1 \in \mathbb{R}\).
Note that \(\mathbb{R}\) itself is a linear space under this
definition.

(This can be generalized to other scalar fields,
but \(\mathbb{R}\) is sufficient for this discussion,
and will be assumed in what follows.)

A \textit{linear function} \(\mathsf{f}\) from linear space
\(\mathbb{V}\) to linear space \(\mathbb{W}\)
preserves linear combinations:
\begin{equation}
\mathsf{f}(a_0 \mathsf{v}_0 + a_1 \mathsf{v}_1 )
\; = \;
a_0 \mathsf{f}(\mathsf{v}_0) \, + \,
a_1 \mathsf{f}(\mathsf{v}_1)
\end{equation}
A \textit{linear functional} is just a real-valued linear
function: \(\mathsf{f} : \mathbb{V} \rightarrow \mathbb{R}\).

The canonical linear space is \(\mathbb{R}^n\),
which we will take here to be the set of tuples of \(n\) real
numbers. Implementations in Clojure/Java will most often
approximate real tuples with instances of \texttt{double[n]},
often with instances of classes wrapping \texttt{double[n]},
and occasionally with \texttt{List<Number>.}

Note that the set of linear functions between two linear
spaces, \(\mathcal{L}(\mathbb{V},\mathbb{W})\) is itself a linear
space.

In fact, given any set of functions \(\mathcal{F} = {\mathsf{f} :
\mathbb{D} \rightarrow \mathbb{V}}\),
from any domain \(\mathbb{D}\) to a
linear space \(\mathbb{V}\),
we get a linear space by closing those functions under linear
combinations of their values:
\(\mathbb{F} = \{ \mathsf{f}() = \sum_i a_i\mathsf{f_i}() \; : \;
\mathsf{f_i} \in \mathcal{F}, a_i \in \mathbb{R} \}\)

See Halmos \cite{halmos-1958} for thorough background.

\section{Inner product space}

An \textit{inner product space} is a linear space
together with an \textit{inner product}, a symmetric,
positive semidefinite, bilinear function of 2 arguments:
\begin{equation}
\begin{split}
\mathrm{dot}(\mathsf{v_0},\mathsf{v_1}) & =
\mathrm{dot}(\mathsf{v_1},\mathsf{v_0}) \\
\mathrm{dot}(\mathsf{v},\mathsf{v}) & \geq 0 \\
\mathrm{dot}(\mathsf{v},a_0\mathsf{v_0}+a_1\mathsf{v_1}) & =
a_0\mathrm{dot}(\mathsf{v},\mathsf{v_0}) +
a_1\mathrm{dot}(\mathsf{v},\mathsf{v_1})
\end{split}
\end{equation}

Skipping some details, we can identify
the linear functionals on an inner product space with the
vectors in that space:
\begin{equation}
\mathsf{v}^{\dagger}(\mathsf{w}) =
\mathrm{dot}(\mathsf{v},\mathsf{w}) \forall \mathsf{w} \in
\mathbb{V}
\end{equation}

L2 norm is
\(\|\mathsf{v}\|_2 = \sqrt(\mathrm{dot}(\mathsf{v},\mathsf{v})
\)

L2 distance is \(\|\mathsf{v_0}-\mathsf{v_0}\|_2 \).

\section{\label{sec:Affine} Affine spaces and functions}

An \textit{affine space} is similar to a linear space, only
closed under \textit{affine combinations} rather than linear.
That is,
\(\mathbb{A}\) is a set of elements, called points,
\(\mathsf{p}_0\), \(\mathsf{p}_1\), \ldots such that:
\begin{equation}
\mathsf{p} \; = \; a_0 \mathsf{p}_0 \, + \, a_1 \mathsf{p}_1
\in \mathbb{A} \forall \mathsf{p}_0, \mathsf{p}_1 \in
\mathbb{A}; a_0, a_1 \in \mathbb{R}; a_0 + a_1 = 1
\end{equation}
for all \(a_0, a_1 \in \mathbb{R}\).
The key distinction is the constraint that \(a_0 + a_1 = 1\).

Abstract differences\(\rightarrow\) linear translation space.

Note that every linear space is automatically an affine space,
but not the reverse.

A canonical example of an affine space that is not a linear
space is a hyperplane in \(\mathbb{R}^n\) that doesn't go thru
the origin, such as the set of all vectors whose first
coordinate is 1. (TODO: picture of such a hyperplance in
\(\mathbb{R}^2\)).

Linearization via equivalence classes of homogeneous
coordinates.

Implementations:
\begin{enumerate}
  \item (Coordinate frame) Pick an origin. Identify elements
  with elements of \( \mathbb{R}^n \). An affine function on
  \( \mathbb{R}^{m} \mapsto \mathbb{R}^n \) is a linear
  function plus an 'intercept' term in \( \mathbb{R}^n \).
  \item (Linear Lifting) Identity elements with equivalence
  classes in \( \mathbb{R}^{n+1} \).
  \( [p_0, p_1, \ldots, p_n] \sim [p_0/p_n, p_1/p_n, \ldots, 1]
  \). Every linear function \(\mathbb{R}^{m+1} \mapsto
  \mathbb{R}^{n+1} \) is an affine function on the equivalence classes.
\end{enumerate}

\section{Euclidean space}

 Affine space where translation space is an inner product space.

\section{Flat predictive models}

Idea is to restrict model space to linear or affine functions.
Although still probably taught as the default model type,
very tricky to use well. Rigidity leads to lack of
expressiveness and instability, change any training data can
have unbounded effects on predictions far from the change.

\section{Embedding data in flat spaces}

Practical issue is that linear functions only make sense on
linear domains; affine on affine domains (which includes
linear.)

Data comes as lists of records, with attributes some of whose
values are numerical, but many taking value in more complicated
domains (eg an address).

Clojure implementation: record = object; dataset = list of
records; attribute = function.

After numerical attributes, the next simplest are probably
categorical, by which I mean an attribute whose values fall in
some finite set. Tricky issue is whether the set of possible
value is know ahead of time (usually assumed and usually wrong)
or whether every new data set may have new values
(eg USA set of states might change, more common is need to cover
territories missing from a training set, etc.)

Whatever set of attributes we start with, we will need to
construct an embedding of the record objects into a flat space.

\section{Regression}

Codomain is \( \mathbb{R} \), so we want flat functionals.
Complete model is \( \mathsf{g} : \mathbb{D} \rightarrow
\mathbb{R} \) constructed from
\( \mathsf{e} : \mathbb{D} \rightarrow \mathbb{F}^n \)
and
\( \mathsf{f} : \mathbb{F}^n \rightarrow \mathbb{R} \)
via \( \mathsf{g} = \mathsf{f} \circ \mathsf{e} \).

\section{Training by minimizing L2 cost}

\section{Training by minimizing L1 and quantile cost}

\section{Regularization}

'Ridge regression' = minimize L2 norm of parameter vector;
'Lasso' = L1 norm.
Neither makes sense
\newpage{}
\input{../tex/typesetting}
\input{../tex/tail}
\end{document}


\documentclass[letterpaper,11pt]{article}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[margin=1in,nohead]{geometry}
\usepackage{tikz}

\newcommand{\sR}{\mathbb{R}}

\newcommand{\figlab}[1]{\label{fig:#1}}
\newcommand{\figref}[1]{Figure~\ref{fig:#1}}

\tikzstyle{coordaxis}=[draw=red!50!black, thick, ->]
\tikzstyle{mapping}=[draw=blue!50!black, thick, ->]
\tikzstyle{connect}=[draw=green!50!black, thick, ->]

\author{Carsten Burstedde}
\title{Documentation on octree and quadrant connectivity}

\begin{document}

\maketitle

\section{Mappings between octrees and physical space}

See \figref{octreemap} for an illustration.

\begin{figure}[b!]
\centering
\begin{tikzpicture}
  % first tree
  \begin{scope}[xshift=0cm]
  \draw (0,0) rectangle (3,3) node (m1) [midway] {$k_1$};
  \draw [style=coordaxis] (0,0) -- (1.5,0) node [below] {$x_1$};
  \draw [style=coordaxis] (0,0) -- (0,1.5) node [left] {$y_1$};
  \filldraw (3,0.75) node (r1) {} circle (2pt) node [left] {$r_1$};
  \end{scope}

  % second tree
  \begin{scope}[xshift=4.4cm]
  \draw (0,0) rectangle (3,3) node (m2) [midway] {$k_2$};
  \draw [style=coordaxis] (3,3) -- (3,1.5) node [right] {$x_2$};
  \draw [style=coordaxis] (3,3) -- (1.5,3) node [above] {$y_2$};
  \filldraw (0,0.75) node (r2) {} circle (2pt) node [right] {$r_2$};
  \end{scope}

  % physical space
  \begin{scope}[xshift=1.2cm, yshift=4cm]
  \draw (0,0) to [bend left=20] (5,1) node (pm1) [midway] {}
              to [bend left=10] (6,3)
              to [bend right=20] (-1,2) node (pm2) [midway] {}
              to [bend right=10] (0,0);
  \draw (pm1.center) to [bend left=5] (pm2.center) node (pmm) [pos=.25] {};
  \filldraw (pmm) circle (2pt) node [above right] {$p \in \sR^3$};
  \end{scope}

  % connections
  \draw [connect] (r1) to [bend right=20] (r2)
        node [midway, below] {$T_{1,2}$};
  \draw [connect] (r2) to [bend right=20] (r1)
        node [midway, above] {$T_{2,1}$};
  \draw [mapping] (r1) to [bend left=40] (pmm) node [pos=0.55, left] {$\phi_1$};
  \draw [mapping] (r2) to [bend right=40] (pmm) node [pos=0.6, right] {$\phi_2$};

\end{tikzpicture}
\caption{%
Two octrees $k_1$ and $k_2$ and their individual octree coordinate systems
(red).  Quadrant coordinates at subdivision level $\ell \ge 0$ (not shown) are
discrete numbers that are integer multiples of $2^{-\ell}$.  In this example
the octrees connect through a common face, which defines coordinate
transformations $T_{1,2} = T_{2,1}^{-1}$ across this face.  The octree points
$r_1 = (1, \frac14)$ and $r_2 = (\frac34, 1)$ are identified: $T_{i,j}(r_i) =
r_j$ (green).  This identification is specified solely based on the
connectivity relation between the two octrees.  The functions $T_i$ can be
implemented in integer arithmetic, entirely without using physical coordinates.
To map the octrees to physical space, geometry transformations $\phi_1$,
$\phi_2$ (blue) are introduced that satisfy the compatibility condition $p =
\phi_1 (r_1) = \phi_2 (r_2)$.  This offers the alternate identification
criterion $r_j = (\phi_j ^{-1} \circ \phi_i) (r_i)$.  However, this approach is
not recommended since it is much more expensive and, more importantly, depends
on the floating-point functions $\phi_i$ that suffer from roundoff error and
make it difficult to determine uniqueness.
}
\figlab{octreemap}
\end{figure}

\end{document}


\chapter{Domain adaptation using Monge mappings}

\section*{Introduction}
Previously, we discussed two methods of regularized optimal transport for domain adaptation:
\begin{itemize}
	\item Entropic regularization
	\item Regularization with respect to dual potentials
\end{itemize}

However, the emphasis earlier was not on finding the explicit mapping between datasets. One may instead seek to directly estimate the Monge map between source and target datasets. In this chapter, we show how to establish the optimal linear transport map under arbitrary distributions with finite second moments, demonstrate that this is also the optimal transport map, and propose another regularization scheme that incorporates the explicit transport map. The first result was also discovered concurrently by \cite{Flamary2019}.

\section*{Linear Monge Mappings}
Previous work \cite{Flamary2019} estimated the linear Monge mapping between Gaussian distributions and used this to provide a domain adaptation generalization bound.

We show why estimating the linear Monge map may be of importance if we know the second-order moments. And, as stated previously, the optimal linear transport map is the optimal transport map in this setting.

%We conjecture that their result about the optimal transport map can be extended to say that this particular function of the second moments of the source and target distributions gives the optimal linear transport map, even if the optimal transport map isn't linear.

%This corresponds to finding a solution to
%$$\min_A (E_P \frac{1}{2} x'Ax + E_Q \frac{1}{2} y'A^{-1}y),$$ which can be rewritten as
%$$\min_A Tr(A E_P xx' + A^{-1} E_Q yy')/2.$$

\section*{Notation}

Let $A$ and $B$ be positive matrices.

\begin{definition}
	An $n\times n$ symmetric matrix $M$ is a positive matrix if and only if $x^{'} M x>0$ for all $x\in \mathbb{R}^n$.
\end{definition}

And we also introduce the concept of fidelity measure between matrices.

\begin{definition}[Fidelity]
	If $A$ and $B$ are positive matrices, then the fidelity $F(A,B) = tr(A^{1/2}BA^{1/2})^{1/2}$.
\end{definition}

Also let $A\#B=A^{1/2}(A^{-1/2}BA^{-1/2})^{1/2}A^{1/2}$ be the matrix geometric mean. With this established, we can introduce our main lemma.

\begin{lemma}
	Let $\mu_1$ and $\mu_2$ be probability measures with zero means and positive-definite covariance matrices $A,B$ respectively. Then the optimal linear transport map coincides with the optimal transport map.
\end{lemma}

\section*{Minimizing over set of linear maps \cite{Bhatia2019}}
Let $x$ and $y$ be random vectors with values in $\mathbb{C}^{n}$, each having zero mean WLOG, and with covariance matrices $A$ and $B$, respectively. This last statement means that
\begin{center}
	$A=[E(\overline{x}_{i}x_{j})],\ B=[E(\overline{y}_{i}y_{j})]$.
\end{center}

We want to find $x$ and $y$ for which $E\Vert x-y\Vert^{2}$ is minimal.

The covariance matrix of the vector $(x,\ y)$ is

$$
\left[ \begin{matrix}
	[E(\overline{x}_{i}x_{j})] & [E(\overline{x}_{i}y_{j})] \\
	[E(\overline{y}_{i}x_{j})] & [E(\overline{y}_{i}y_{j})]
\end{matrix}\right]
=
\left[ \begin{matrix}
	A & M \\
	M^{*} & B
\end{matrix}\right]
$$

We seek to minimize the following:
\begin{align*}
E\Vert x-y\Vert^{2} &= E(\displaystyle \sum_{i=1}^{n}(|x_{i}|^{2}+|y_{i}|^{2}-2\mathrm{Re}\overline{x}_{i}y_{i})) \\
&= \sum_{i=1}^{n}E(|x_{i}|^{2}+|y_{i}|^{2}-2\mathrm{Re}\overline{x}_{i}y_{i}) \\
&= \mathrm{tr}(A+B)-2\mathrm{Re} (tr M).
\end{align*}

This is equivalent to the following optimization problem:

$\displaystyle \max\{|\mathrm{tr}M|$ : $C=\left[ \begin{matrix}
	A & M \\
	M^{*} & B
\end{matrix}\right] \geq 0\}.$

The value of the maximum is $F(A,\ B)$. So
$$
\min E\Vert x-y\Vert^{2}\ =\ \mathrm{t}\mathrm{r}(A+B)-2\mathrm{t}\mathrm{r}(A^{1/2}BA^{1/2})^{1/2}
$$
$$
=\ d^{2}(A,\ B).
$$

Let $x$ be a vector with mean $0$ and covariance matrix $A$. Then for any $T\in \mathrm{F}\mathrm{M}(n)$ we have

\begin{align*}
E(\langle x,\ Tx\rangle)\ &= E(\sum_{i,j}t_{ij}\overline{x_{i}}x_{j}) =\sum_{i,j}t_{ij}E(\overline{x_{i}}x_{j}) \\
&= \displaystyle \sum_{i,j}t_{ij}a_{ij}=\mathrm{tr} TA.
\end{align*}

Hence,
\begin{align*}
E\Vert x-Tx\Vert^{2} &= E(\Vert x\Vert^{2}+\Vert Tx\Vert^{2} - 2\textrm{Re}\langle x,\ Tx\rangle) \\
&= \textrm{tr}A+\textrm{tr}T^{*}TA-2\textrm{Re} \textrm{tr} TA \\
&= \textrm{tr} A+\textrm{tr}TAT^{*}-2\textrm{Re} \textrm{tr} A^{1/2}TA^{1/2}
\end{align*}

If we choose $T=A^{-1}\# B$, then  we see that tr $A^{1/2}TA^{1/2}=$ tr $(A^{1/2}BA^{1/2})^{1/2}$, and that $\mathrm{tr}TAT=\mathrm{tr}B$. Thus, for this choice of $T$, we have
\begin{center}
	$$E\Vert x-Tx\Vert^{2} = tr(A+B)-2 tr (A^{1/2}BA^{1/2})^{1/2}
	=\ d^{2}(A,\ B)\ .
	$$
\end{center}
Thus the problem
$$
\min E\Vert x-y\Vert^{2}
$$
where $x, y$ are vectors with mean zero and covariance matrices $A$ and $B,$ respectively, has as its solution the pairs $(x,\ y)$ , where $x$ is any vector and $y=Tx$, with $T=A^{-1}\# B$.

Let $x$ be a vector with covariance matrix $A$, and let $y=Tx$. Then
$$
E(\overline{y}_{i}y_{j})\ =\ E\sum_{k,l}t_{ik}t_{kl}\overline{x}_{k}x_{l}
$$
$$
=\ \sum_{k,l}t_{ik}t_{kl}a_{kl}=(TAT)_{ij}.
$$
If $T$ is the optimal transport map from $A$ to $B$, then $TAT=B$. This shows that the covariance matrix of the vector $y$ is $B.$

%\section*{Which cross-covariance matrices are feasible with given marginal distributions?}
%
%If we have marginal distributions $P$ and $Q$ we want to determine possible cross-covariances between the two distributions. Let $M$ denote the cross-covariance between $\mu$ and $\nu$.
%
%\subsection*{What if we knew the marginal covariances?}
%In the case where we know $\Sigma_{P}$ and $\Sigma_{Q}$, we have necessary and sufficient conditions for the block matrix
%$$
%\left( \begin{matrix}
%	\Sigma_{P} & M \\
%	M^{*} & \Sigma_{Q}
%\end{matrix}\right)
%$$
%
%to be PSD.
%
%These conditions are the following:
%\begin{enumerate}
%	\item $A\geq M\Sigma_{Q}^{-1}M^{*}$
%	\item There exists a contraction operator $K$ with $\norm{K}\leq 1$ such that $$M=\Sigma_{P}^{1/2}K\Sigma_{Q}^{1/2}.$$
%\end{enumerate}

%\subsection*{General Case}
%Back to our setting, we don't necessarily know the covariance matrices on the marginals.
%
%Consider the discrete setting where the distributions $\mu$ and $\nu$ can be represented by probability vectors $p$ and $q$. Also let $X$ and $Y$ represent random vectors from $\mu$ and $\nu$ and define the respective covariance matrices as $\Sigma_X$ and $\Sigma_Y$.
%
%Transform $X$ and $Y$ as follows
%\begin{align*}
%	\tilde{X} &= diag(\sqrt{p})X \\
%	\tilde{Y} &= diag(\sqrt{q})Y \\
%	\tilde{P} &= diag(1/\sqrt{p})\cdot diag(1/\sqrt{q})
%\end{align*}
%
%Let $$K=(X^{\perp}diag(\sqrt{p})X)^{-1/2}(X^{\perp}PY)(Y^{\perp}diag(\sqrt{q})Y)^{-1/2}.$$
%
%Note that
%$$\tilde{X}^{\perp}\tilde{P}\tilde{Y}=X^{\perp}PY=\Sigma_{XY}=\Sigma_X^{1/2}K\Sigma_Y^{1/2}$$
%
%We now want to solve the following optimization problem:
%
%Maximize $$\norm{K\Sigma_{\nu}^{1/2}}_{\mathcal{F}}$$ where
%$P$ satisfies constraints
%\begin{align*}
%	P\vec{1} &=p \\
%	P^{\perp}\vec{1} &=q.
%\end{align*}
%
%\subsection*{Pythagorean identity for Wasserstein}
%
%\begin{equation}
%	W(P,Q)=\inf_T[d(P,T_{\#}P)+W(T_{\#}P,Q)].
%\end{equation}
%
%Indeed,
%\begin{align*}
%	d(P,T_{\#}P)+W(T_{\#}P,Q)&=\inf_\gamma\Big[\sqrt{\int_{S\times S}(x-T(x))^2\gamma(dx,dy)}+\sqrt{\int_{S\times S}(T(x)-y)^2\gamma(dx,dy)}\Big] \\
%	&\ge\inf_\gamma\sqrt{\int_{S\times S}(x-y)^2\gamma(dx,dy)}=W(P,Q)
%\end{align*}
%where $\inf\limits_\gamma$ is taken over all couplings $\gamma$ of $P$ and $Q$. So, the right-hand side of (1) is no less than its left-hand side. On the other hand, taking $T$ to be the identity map of $S$, we see that the right-hand side of (1) is no greater than its left-hand side. Thus, (1) is proved.

%
%\section*{A Two-Step Computation for Wasserstein}
%Define $$d^2(p,q) = E\norm{X-Y}^2$$ with $X\sim p$ and $Y\sim q.$
%
%We conjecture that $W^2(p,q) = \min_{T} d^2(p,T_{\#}p) + W^2(T_{\#}p,q)$ where $T$ is the set of linear mappings. This is promising since if $T$ is chosen to be either the identity map or $\Sigma_X^{-1}\#\Sigma_Y$, then $d^2(p,T_{\#}p) + W^2(T_{\#}p,q) = W^2(p,q)$.
%
%One could extend the set $T$ to a set of mappings closed under addition and composition (i.e. $f,g\in T$ implies $f+g$ and $f(g)\in T$) as well.
%
%This idea is similar to the concept of $I$-projection for KL-divergences. I'm not sure if this will hold because the I-projection concept extends to Bregman divergences, but the Wasserstein isn't a Bregman type.
%
%\subsection*{Counterexample to our claim}
%Suppose that the underlying measurable space
%$(S,\Si)$ on which $P$ and $Q$ are defined is $(\R,\B(\R))$, where $\B(\R)$ is the Borel $\sigma$-algebra over $\R$. Let $P$ be the uniform distribution on $(0,1)$. Then any distribution $R$ on $(\R,\B(\R))$ is of the form $T_{\#}P$ for some $T$; namely, $T$ is the quantile transformation given by the formula
%$$T(u)=H^{-1}(u):=\inf\{x\in\R\colon H(x)\ge u\}$$
%for $u\in(0,1)$, where $H$ is the cdf of the distribution $R$. Let $Q$ be any distribution on $(\R,\B(\R))$ other than $P$. There are real-valued random variables (r.v.'s) $X$ and $Y$ with respective distributions $P$ and $Q$ such that $W^2(P,Q)=E(X-Y)^2$; for instance, take any r.v. $X\sim P$ and let then $Y:=G^{-1}(X)$, where $G$ is the cdf of $Q$. Now let $Z:=(X+Y)/2$ and let $R$ be the distribution of $Z$. Then the right-hand side of our conjectured identity is
%$$\le d^2(P,R) + W^2(R,Q)\le E(X-Z)^2+E(Z-Y)^2=\frac12\,E(X-Y)^2=\frac12\,W^2(P,Q)<W^2(P,Q);$$
%the latter inequality holding because $Q\ne P$.
%
%\section*{What if $Q$ corresponds to a subspace?}
%Here, one must generalize the claim
%$$F(\Sigma_X,\Sigma_Y) = \max_{M>0} \{ \abs{trX}: \Sigma_X\geq M\Sigma_Y^{-1}M^{*}\}$$ to the case where $\Sigma_Y$ is not full rank.
%
%First, let's introduce the following lemma.
%\begin{lemma}
%	$$
%	\left( \begin{matrix}
%		\Sigma_{X} & M \\
%		M^{*} & \Sigma_{Y}
%	\end{matrix}\right)
%	$$
%	 is PSD if $\Sigma_{X}\geq M\Sigma_{Y}^{+}M^{*}$, where $\Sigma_Y^{+}$ denotes the pseudoinverse of $\Sigma_Y$.
%\end{lemma}
%
%\begin{proof}
%$$
%	\left( \begin{matrix}
%		\Sigma_{X}-M\Sigma_{Y}^{+}M^{*} & O \\
%		O & \Sigma_{Y}
%	\end{matrix}\right)\sim
%	\left( \begin{matrix}
%		I & M\Sigma_{Y}^{+} \\
%		O & I
%	\end{matrix}\right)
%	\left( \begin{matrix}
%		\Sigma_{X}-M\Sigma_{Y}^{+}M^{*} & O \\
%		O & \Sigma_{Y}
%	\end{matrix}\right)
%	\left( \begin{matrix}
%		I & O \\
%		\Sigma_{Y}^{+}M^{*} & I
%	\end{matrix}\right)
%= 	\left( \begin{matrix}
%	\Sigma_{X} & M \\
%	M^{*} & \Sigma_{Y}
%\end{matrix}\right).
%$$
%
%It is clear from this that $\left( \begin{matrix}
%\Sigma_{X} & M \\
%M^{*} & \Sigma_{Y}
%\end{matrix}\right)$ is positive if and only if $\Sigma_{X}- M\Sigma_{Y}^{+}M^{*}\geq 0.$
%\end{proof}
%
%One also notes that the positive-definiteness of the cross-covariance matrix implies
%$$
%M=\Sigma_X^{1/2}K\Sigma_Y^{1/2},
%$$
%where $K$ is a contraction. By the Schwarz inequality we have
%
%\begin{align*}
%	|\mathrm{tr}M|=|\mathrm{tr}(\Sigma_X^{1/2}K\Sigma_Y^{1/2})|\ &\leq \norm*{\Sigma_X^{1/2}K}_2 \norm*{\Sigma_Y^{1/2}}_2 \\
%	&\leq \norm*{\Sigma_X^{1/2}}_2 \norm*{\Sigma_Y^{1/2}}_2 = \sqrt{\textrm{tr}\Sigma_X\textrm{tr}\Sigma_Y}
%\end{align*}
%
%If $$
%\left( \begin{matrix}
%	\Sigma_{X} & M \\
%	M^{*} & \Sigma_{Y}
%\end{matrix}\right) \geq 0
%$$
%then for every $Z>0$ we have
%
%$$
%	0\leq
%	\left( \begin{matrix}
%		Z^{1/2} & O \\
%		O & Z^{-1/2}
%	\end{matrix}\right)
%	\left( \begin{matrix}
%		\Sigma_{X} & M \\
%		M^{*} & \Sigma_{Y}
%	\end{matrix}\right)
%	\left( \begin{matrix}
%		Z^{1/2} & O \\
%		O & Z^{-1/2}
%	\end{matrix}\right)
%	= \left( \begin{matrix}
%		Z^{1/2}\Sigma_X Z^{1/2} & Z^{1/2}M Z^{-1/2} \\
%		Z^{-1/2}M^{*} Z^{1/2} & Z^{-1/2}\Sigma_Y Z^{-1/2}
%	\end{matrix}\right)
%$$
%
%Hence
%$$
%|\mathrm{tr}Z^{1/2}M Z^{-1/2}|\leq\sqrt{\mathrm{tr}(Z^{1/2}\Sigma_X Z^{1/2})\mathrm{tr}(Z^{-1/2}\Sigma_Y Z^{-1/2})}.
%$$
%
%This simplifies to
%$$
%|\mathrm{tr}M|\leq\sqrt{\mathrm{tr}(\Sigma_X Z)\mathrm{tr}(\Sigma_Y Z^{-1})}.
%$$
%
%This is true for all $M$ satisfying the condition $\Sigma_{X}\geq M\Sigma_{Y}^{+}M^{*}$ and for all $Z>0$. So
%
%$$
% \max\{|\mathrm{tr}M|\ :\ \Sigma_{X}\geq M\Sigma_{Y}^{+}M^{*}\} \leq \min_{Z>0} \sqrt{\mathrm{tr}(\Sigma_X Z)\mathrm{tr}(\Sigma_Y Z^{-1})}
%$$
%
%Showing equality is a problem I'm having at the moment.
%\blacksquare

%\subsection*{Parts I Need To Clarify}
%Why do we use $\norm{K\Sigma_{Y}^{1/2}}_{\mathcal{F}}$?
%And what was the closed set we could extend to from the set of linear mappings (for the two-step Wasserstein computation)?
%
\section*{Regularized Optimization}
One may want to look at a regularization of the combination of the squared Bures distance and the expected distance moved under the transport map $T$ by the squared Hilbert-Schmidt norm of $T$.
$$
\min_T Q(T) \textrm{ where }Q(T) = \lambda E \left\| TX-X \right\|^2 + d_B^2(Cov(TX),\Sigma_v) + \mu \left\|T\right\|^2_{HS}
$$

Differentiating $Q(T)$, we get
$$2\lambda (TX-X)X^{T} + (\frac{2}{n-1})CTX(I-(BA)^{-1/2}B)X^{T} + 2\mu T$$ where $A=Cov(TX)=(\frac{1}{n-1})(TX)^T C(TX)$, $B=\Sigma_v$, and $C=(1-\frac{1}{n}J)=C^T$ is a centering matrix.

Solving for $\hat{T}$ such that $Q'(\hat{T})=0$, we get

$$
\lambda XX^{T} = \lambda \hat{T} XX^{T} + \mu \hat{T} + \frac{1}{n-1} C\hat{T}[X(I-(BA)^{-1/2}B)]X^{T}.
$$

\subsection*{Differentiating the Bures distance part I}
For PSD matrices a drastic
simplification is possible:
$$
\textrm {Tr}((A^{1/2}BA^{1/2})^{1/2}) = \textrm{Tr}((BA)^{1/2})
$$
In addition, there is a general result for the differential of the trace of any matrix function
$$
d\,\textrm{Tr}\big(f(X)\big) = f'(X^T):dX
$$
where $f'$ is the ordinary derivative of the scalar function $f;\,$ both $f$ and $f'$ are evaluated using their respective matrix arguments.

Combining these yields a straightforward solution for the problematic term
\begin{align*}
	\phi &= \textrm {Tr}\Big((BA)^{1/2}\Big) \\
	d\phi
	&=  \tfrac 12\big((BA)^T\big)^{-1/2}:d(BA) \\
	&= \tfrac 12(AB)^{-1/2}:B\,dA \\
	&= \tfrac 12 B(AB)^{-1/2}:dA \\
	\frac{\partial\phi}{\partial A}
	&= \tfrac 12 B(AB)^{-1/2}
	\;=\; \tfrac 12 (BA)^{-1/2}B
\end{align*}
Where the final equality is a theorem due to Higham
$$B\cdot f(AB) = f(BA)\cdot B$$

Therefore the gradient of the Bures Distance is
\begin{align*}
	\beta(A,B) &= \textrm {Tr}\Big(A+B - 2(BA)^{1/2} \Big) \\
	d\beta &= \Big(I - B(AB)^{-1/2}\Big):dA \\
	\frac{\partial\beta}{\partial A}
	&= I - B(AB)^{-1/2} \;\;=\; I - (BA)^{-1/2}B \\
	&= I - A^{-1}(AB)^{1/2} \;=\; I - (BA)^{1/2}A^{-1}
\end{align*}

\subsection*{Differentiating the Bures distance part II}
Let $J$ be the all-ones matrix and
\begin{align*}
	C &=(I-\tfrac 1nJ) = C^T \qquad\qquad\big(\textrm{ Centering\,Matrix}\big) \\
	B &= \Sigma_v \\
	A &= \textrm{Cov}(TX) \\
	&= \left(\tfrac 1{n-1}\right)(TX)^TC\,(TX)
\end{align*}

From earlier, the Bures distance function and its differential can be simplified to
\begin{align*}
	\beta(A,B) &= \textrm{Tr}\Big(A+B - 2(BA)^{1/2} \Big) \\
	d\beta &=\Big(I - (BA)^{-1/2}B\Big):dA
\end{align*}
Now change the differentiation variable from $\;dA\to dT$.
\begin{align*}
	d\beta
	&= \Big(I - (BA)^{-1/2}B\Big):\left(\tfrac 2{n-1}\right)\textrm{ Sym}(X^TT^TC\,dT\,X) \\
	&= \left(\tfrac 2{n-1}\right)\Big(I - (BA)^{-1/2}B\Big):(X^TT^TC\,dT\,X) \\
	&= \left(\tfrac 2{n-1}\right)CTX\Big(I - (BA)^{-1/2}B\Big)X^T:dT \\
	\frac{\partial\beta}{\partial T}
	&= \left(\tfrac 2{n-1}\right)CTX\Big(I - (BA)^{-1/2}B\Big)X^T \\
\end{align*}

In the above derivation, the function
\begin{align*}
	\textrm{Sym}(M) = \tfrac 12(M+M^T) \\
\end{align*}
was utilized, as well as the trace/Frobenius product
\begin{align*}
	P:M = \textrm{Tr}(P^TM) = \textrm{Tr}(M^TP) = M:P \\
\end{align*}
These have the following interaction
\begin{align*}
	P:\textrm{Sym}(M) = \textrm{Sym}(P):M \\
\end{align*}


% Preamble.
\documentclass[12pt]{article}
\usepackage[margin=1.25in]{geometry}
\usepackage[fleqn]{amsmath}
\usepackage{textcomp}
\usepackage{gensymb}
\usepackage{amsfonts}
\usepackage{enumitem}
%\usepackage{tikz}  % Include for figures.
%\usepackage{subfiles}  % Include for subfiles.

%% Title macros.
\newcommand{\HOMEWORKNUM}{19}
\newcommand{\NAME}{D. Choi}
\newcommand{\DATE}{2020-06-22}

\title{\vspace{-2\baselineskip}MATH 225 - Homework \#\HOMEWORKNUM}
\author{\NAME}
\date{\DATE}

%% Formatting options.
%\pagenumbering{gobble}  % Include for single-page document.

% Macros.
%% Contextualized by input/output bases.
\newcommand{\based}[3]{{\{#1\}}_{#2}^{#3}}

% Document.
\begin{document}
\maketitle

\section*{1.}
\textit{Let $\mathcal{S}$ be the standard basis, and let}
\begin{gather*}
	\mathcal{A}
	=
	\left\{
		\begin{pmatrix} -1 \\ 0 \end{pmatrix},
		\begin{pmatrix} 0 \\ -1 \end{pmatrix}
	\right\}
	,
	\\
	\mathcal{B}
	=
	\left\{
		\begin{pmatrix} -1 \\ 1 \end{pmatrix},
		\begin{pmatrix} 1 \\ 1 \end{pmatrix}
	\right\}
	.
\end{gather*}
\textit{Also, let}
\begin{equation*}
	\based{T}{\mathcal{S}}{\mathcal{S}}
	=
	\begin{pmatrix}
		-1 & 3 \\
		3 & -1
	\end{pmatrix}
	.
\end{equation*}
\begin{enumerate}[label=(\alph*)]
	\item \textit{Find $\based{T}{\mathcal{B}}{\mathcal{A}}$.}
	\\[\baselineskip]
	Let $\vec{w}_1$ and $\vec{w}_2$ be the vectors of $\mathcal{B}$ so that
	\begin{equation*}
		\vec{w}_1 = \begin{pmatrix} -1 \\ 1 \end{pmatrix},
		\vec{w}_1 = \begin{pmatrix} 1 \\ 1 \end{pmatrix}.
	\end{equation*}
	Using $\based{I}{\mathcal{B}}{\mathcal{S}}$ (the change of basis matrix
	from $\mathcal{B}$ to standard basis) and
	$\based{I}{\mathcal{S}}{\mathcal{A}}$ (the change of basis matrix from
	standard basis to $\mathcal{A}$), $\based{T}{\mathcal{B}}{\mathcal{A}}$
	can be expressed as
	\begin{align*}
		\based{T}{\mathcal{B}}{\mathcal{A}}
		&=
		\based{I}{\mathcal{S}}{\mathcal{A}}
		\based{T}{\mathcal{S}}{\mathcal{S}}
		\based{I}{\mathcal{B}}{\mathcal{S}}
		\\
		&=
		\begin{pmatrix}
			\vert & \vert \\
			\based{I}{\mathcal{S}}{\mathcal{A}}
			\based{T}{\mathcal{S}}{\mathcal{S}}
			\vec{w}_1 &
			\based{I}{\mathcal{S}}{\mathcal{A}}
			\based{T}{\mathcal{S}}{\mathcal{S}}
			\vec{w}_2 \\
			\vert & \vert
		\end{pmatrix}
	\end{align*}
	Or, more concisely,
	\begin{equation*}
		\based{T}{\mathcal{B}}{\mathcal{A}}
		=
		\begin{pmatrix}
			\vert & \vert \\
			{\{T \vec{w}_1\}}_{\mathcal{A}} &
			{\{T \vec{w}_2\}}_{\mathcal{A}} \\
			\vert & \vert
		\end{pmatrix}
		.
	\end{equation*}

	\item \textit{Find $\based{T}{\mathcal{B}}{\mathcal{B}}$.}
	\begin{equation*}
		\based{T}{\mathcal{B}}{\mathcal{B}}
		=
		\begin{pmatrix}
			\vert & \vert \\
			{\{T \vec{w}_1\}}_{\mathcal{B}} &
			{\{T \vec{w}_2\}}_{\mathcal{B}} \\
			\vert & \vert
		\end{pmatrix}
		.
	\end{equation*}
\end{enumerate}

\section*{2.}
\begin{equation*}
	\vec{a} = \begin{pmatrix} -4 \\ -3 \\ 0 \end{pmatrix},
	\vec{b} = \begin{pmatrix} 3 \\ 4 \\ 1 \end{pmatrix}
\end{equation*}
\textit{The vectors $\vec{a}$ and $\vec{b}$ are orthogonal. Find a vector
$\vec{c}$ that is orthogonal to both of these vectors.} \\[\baselineskip]
The vector that is the cross product of two other vectors is, by definition,
orthogonal to both of the two other vectors. \\
Thus, let $\vec{c}$ be the cross product of $\vec{a}$ and $\vec{b}$:
\begin{equation*}
	\vec{c}
	=
	\vec{a} \times \vec{b}
	=
	\begin{pmatrix} -4 \\ -3 \\ 0 \end{pmatrix}
	\times
	\begin{pmatrix} 3 \\ 4 \\ 1 \end{pmatrix}
	=
	\boxed{
		\begin{pmatrix} -3 \\ 4 \\ -7 \end{pmatrix}
	}
	.
\end{equation*}
The vector $\vec{c}$'s orthogonality to both $\vec{a}$ and $\vec{b}$ can be
verified by confirming that
\begin{gather*}
	\vec{c} \cdot \vec{a} = 0, \\
	\vec{c} \cdot \vec{b} = 0.
\end{gather*}
Any other vector that fulfills the criteria can be found by computing
\begin{equation*}
	s \cdot \vec{c}
\end{equation*}
with an arbitrary scalar $s$.

\end{document}


[40 min late]

\section{Equivalence of microcanonical and canonical ensembles}
\begin{align}
    Z &= \Tr e^{-\beta H}\\
    &=
    \int_{0}^{\infty}\, \Gamma(E) e^{-\beta E}
\end{align}
where the density of states is $\Gamma(E)=e^{S(E)/k_B}$.
Then
\begin{align}
    E &=
    \int_{0}^{\infty} dE\,
    e^{\frac{S(E)}{k_B} - \frac{E}{k_B T}}\\
    &\approx
    \left. \Delta e^{\frac{1}{k_B}\left( S - \frac{E}{T} \right)}
    \right|_{E = E^*}
\end{align}
and we don't care about the $\Delta$ because it's just a constant factor that
goes away in the log.
It is sharpley bpead as
\begin{align}
    \frac{\Delta E}{E} \to \frac{1}{\sqrt{N}}
\end{align}

\section{``Derivation'' of the canonical ensemble from the microcanonical}
Allow heat exchange.
Subsystem.
Heat reservoir.
\begin{align}
    \rho_T (\underbrace{q, p}_{\text{subsystem}}, \underbrace{Q,
    P}_{\text{reservoir}}) &\sim
    \delta_\Delta\left(
    H(p,q,P,Q) - E_T
    \right)
\end{align}
where $\delta_\Delta$ is a delta function with finite width $\Delta$.
I'm going to sum over all values of the reservoir of that probability
distribution and I'm going to get a function of that subsystem only.
\begin{align}
    \rho(q, p) &=
    \int dQ\, dP\,
    \underbrace{\rho_T\left( q, p, Q, P \right)}_{%
    \delta_\Delta\left( H(q, p) + H_R(Q,P) - E_T \right)
    }\\
    &= \Gamma_R\underbrace{\left( H(q,p) - E_T \right)}_{E_R}
\end{align}


\documentclass{article}

\usepackage{amsmath,amssymb,siunitx,graphicx}
\usepackage[margin=1in]{geometry}
\DeclareSIUnit\ergs{ergs}
\DeclareSIUnit\yr{yr}
\DeclareSIUnit\AU{AU}
\DeclareSIUnit\msun{\ensuremath{\mathrm{M}_{\odot}}}

\title{Snowfall}
\author{Matthias J. Raives}

\begin{document}

    \maketitle{}

    \section{Mass of Snowmelt}
    The OOM method of solving this problem is to equate the kinetic energy of a falling layer of snow to the thermal energy required to melt the snow.

    Consider an idealized layer of falling snow, of mass $M$, which falls at the terminal velocity of a single snowflake:
    \begin{equation}
        v_{T}^{2} = \frac{2mg}{\rho_\mathrm{air} \sigma C_{d}}
    \end{equation}
    Where $m$ is the mass of a snowflake, $\sigma$ is it's projected area, and $\rho_\mathrm{air}$ is the density of air. For simplicity, we will assume that $C_{d}=1$.  The snow layer then has kinetic energy:
    \begin{equation}
        K = \frac{1}{2}Mv_{T}^{2} = \frac{Mmg}{\rho_\mathrm{air}\sigma}
    \end{equation}
    This can melt a mass of snow:
    \begin{equation}
        \mathrm{d}M = \frac{K}{\Delta\epsilon_\mathrm{fus}} = \frac{Mmg}{\rho_\mathrm{air}\sigma\Delta\epsilon_\mathrm{fus}}
    \end{equation}
    where $\Delta\epsilon_\mathrm{fus}=\SI{3e9}{\ergs\per\gram}$ is the specific heat of fusion of water (the energy per unit mass required to melt ice at \SI{0}{\celsius}).  Thus we can write:
    \begin{equation}
        \frac{\mathrm{d}M}{M} = \frac{mg}{\rho_\mathrm{air}\sigma\Delta\epsilon_\mathrm{fus}}
    \end{equation}
    The mass of a snowflake can be written as:
    \begin{equation}
        m = \rho_\mathrm{ice}\sigma\ell
    \end{equation}
    where $\ell$ is the size of the flake normal to the projection (i.e., it's length along the direction of its motion).  Thus we are left with:
    \begin{equation}
        \frac{\mathrm{d}M}{M} = \frac{\rho_{\mathrm{ice}}g\ell}{\rho_\mathrm{air}\Delta\epsilon_{\mathrm{fus}}}\label{eq:dmM}
    \end{equation}
    Assuming that snowflakes fall standing up (i.e., not face down), then $\ell$ is the diameter of a snowflake, so on order $\ell\sim\SI{0.5}{\cm}$.  The density of ice is approximately that of water $(\rho_{\mathrm{ice}}\sim\SI{1}{\gram\per\cubic\cm})$, and the density of air is $\rho_{\mathrm{air}}\sim\SI{e-3}{\gram\per\cubic\cm}$.  Thus:
    \begin{equation}
        \frac{\mathrm{d}M}{M} \sim \frac{1\times10^{3}\times0.5}{10^{-3}\times\num{3e9}} \sim \num{2e-4}
    \end{equation}

    \section{Height of Snowmelt}
    We could equivalently write this as d$h/h$, by writing the mass of a snow layer as
    \begin{align}
        M &= f_\mathrm{fresh}A\rho_\mathrm{ice}h & \mathrm{d}M &= f_\mathrm{melt}A\rho_\mathrm{ice}\mathrm{d}h
    \end{align}
    where $A$ is the area of the snow layer and $f$ is the packing efficiency of snow.  We see then that $\frac{\mathrm{d}M}{M}=\frac{\mathrm{d}h}{h}$, assuming that the falling snow is packed with the same efficiency as the layers on the ground.  That this is a good assumption is not immediately obvious: falling snow is packed much less efficiently than snow is on the ground.  But by considering the snowfall as a single layer, we are effectively projecting a volume of snowfall onto a surface, which increases the effective packing efficiency.  It is also true, however, that the packing efficiency of snow on the ground is increased by compression due to the weight of new layers of snow.  In generality, then:
    \begin{align}
        \frac{\mathrm{d}h}{h} = \frac{f_{\mathrm{fresh}}}{f_\mathrm{melt}}\frac{\mathrm{d}M}{M}
    \end{align}
    i.e., the more tightly packed the already fallen snow is compared to fresh snow, the shorter the melt layer will be.

    \section{Hail}
    For this problem, we can just consider a single hailstone.  A hailstone of radius $r$ has mass:
    \begin{equation}
        M = \frac{4\pi}{3}\rho_{\mathrm{ice}}r^{3} = \frac{4}{3}\rho_{\mathrm{ice}}\sigma r
    \end{equation}
    and thus, we can write, in analogy to Equation~\eqref{eq:dmM}:
    \begin{equation}
        \frac{\mathrm{d}M}{M} = \frac{4\rho_{ice}gr}{3\rho_{\mathrm{air}}\Delta\epsilon_{\mathrm{fus}}}
    \end{equation}
    For the hail to melt more ice than it deposits, then we must have $\frac{\mathrm{d}M}{M}\geq1$, or:
    \begin{equation}
        r\geq\frac{3\rho_{\mathrm{air}}\Delta\epsilon_{\mathrm{fus}}}{4\rho_{\mathrm{ice}}g}\sim\frac{3\times\num{e-3}\times\num{3e9}}{4\times 1\times\num{e3}}\sim \SI{2e3}{\cm}
    \end{equation}

\end{document}


\documentclass[a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage{a4wide}
\usepackage{indentfirst}
\usepackage{hyperref}
\usepackage[capitalise]{cleveref}
\usepackage{minted}
\usepackage{graphicx}
\usepackage{float}
\usepackage{witharrows}

\AtBeginEnvironment{minted}{\let\itshape\relax}

\begin{document}

\section*{Exercise 1}

To show that $(m_1, m_2) = (m'_1, m'_2)$, we have to show that
$m_1 \equiv x_T^{-1} c_1 \ (\mathrm{mod} \ p)$ and $m_2 \equiv y_T^{-1}c_2 \ (\mathrm{mod} \ p)$.

\vspace{\baselineskip}

\[
\setlength{\jot}{10pt}
\begin{WithArrows}
m_1 \equiv x_T^{-1} c_1 \ (\mathrm{mod} \ p) \ &\wedge \ m_2 \equiv y_T^{-1} c_2 \ (\mathrm{mod} \ p) \Arrow{$c_1 \equiv x_Sm_1 \ (\mathrm{mod} \ p)$ and \\$c_2 \equiv y_Sm_2 \ (\mathrm{mod} \ p)$} \\
m_1 \equiv x_T^{-1} x_Sm_1 \ (\mathrm{mod} \ p) &\wedge m_2 \equiv y_T^{-1} y_Sm_2 \ (\mathrm{mod} \ p)
\end{WithArrows}
\]

\vspace{\baselineskip}

For $(m_1, m_2) = (m'_1, m'_2)$ to hold, we just have to show that $T^{-1}S \equiv 1 \ (\mathrm{mod} \ p)$.

\[
\setlength{\jot}{10pt}
\begin{WithArrows}
        &\Arrow{$T = n_AR$ and $S = kQ_A$}\\
T^{-1}S &\equiv (n_AR)^{-1}kQ_A \ (\mathrm{mod} \ p) \Arrow{$R = kP$ and $Q_A = n_AP$} \\
        &\equiv n_A^{-1}(kP)^{-1}kn_AP \ (\mathrm{mod} \ p) \Arrow{$n_An_A^{-1} = 1$, $kk^{-1} = 1$\\ and $PP^{-1} = 1$} \\
        &\equiv 1 \ (\mathrm{mod} \ p)
\end{WithArrows}
\]

\vspace{\baselineskip}

Thus, $(m_1, m_2) = (m'_1, m'_2)$.

\section*{Exercise 2}

\vspace{\baselineskip}

\begin{minted}{py}
from sage.all import *

def gen_pub_key(A, B, p, x_p, y_p):
    Fp = FiniteField(p)
    E = EllipticCurve(Fp, [A, B])
    assert(E.is_on_curve(x_p, y_p))

    P = E([x_p, y_p])
    n_a = ZZ(Fp.random_element())
    Q_A = n_a * P

    return (Q_A, n_a)

def encrypt(A, B, p, x_p, y_p, Q_A, m_1, m_2):
    Fp = FiniteField(p)
    E = EllipticCurve(Fp, [A, B])
    assert(E.is_on_curve(x_p, y_p))

    P = E([x_p, y_p])
    k = ZZ(Fp.random_element())
    R = k * P

    S = k * Q_A
    c_1 = (S[0] * m_1) % p
    c_2 = (S[1] * m_2) % p

    return (R, c_1, c_2)
\end{minted}

\begin{minted}{py}
def decrypt(A, B, p, x_p, y_p, R, n_a, c_1, c_2):
    Fp = FiniteField(p)
    E = EllipticCurve(Fp, [A, B])
    assert(E.is_on_curve(x_p, y_p))

    T = n_a * R
    m_1 = (T[0] ** (-1) * c_1) % p
    m_2 = (T[1] ** (-1) * c_2) % p

    return (m_1, m_2)
\end{minted}

\vspace{2\baselineskip}

Running this functions with the Curve P-384, we can see the functions are defined
correctly (\cref{fig:sage}).

\vspace{\baselineskip}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{img/ex2.png}
    \caption{Menezes–Vanstone variant for ECC ElGamal}
    \label{fig:sage}
\end{figure}

\end{document}


% LaTeX file for a 1 page document
\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
%\usepackage{physymb}
\usepackage{graphicx}
%\usepackage{wrapfig}
\usepackage{tikz}
\usetikzlibrary{calc,patterns,decorations.pathmorphing,decorations.markings}

\title{EP 222 Assigment 2}
\date{October 10, 2013}
\author{Manish Goregaokar (120260006)}

\begin{document}
\maketitle

\section{Question 1}

\includegraphics[scale=1]{1.png}

We shall take $\theta_1,\theta_2$ as our coordinates. Let the rigid supports be of length $l$, and let the distance between the supports be equal to the equilibrium length of the spring, $d$. The kinetic energy of this system is $T=\frac{l^2}2(m_1\dot\theta_1^2+m_2\dot\theta_2^2)$\\
Rewriting $T$ as a matrix, $\mathbf T=[t_{ij}]=\left[\frac{\partial^2 T}{\partial \dot\theta_i\partial \dot\theta_j}\right]=l^2\begin{pmatrix}
m_1 & 0\\
0 & m_2
\end{pmatrix}$\\

The potential energy is \begin{align}V &=& &m_1gl(1-\cos\theta_1)+m_2gl(1-\cos\theta_2)+\frac12k(l(\theta_2-\theta_1))^2\notag\\ &\approx & &m_1gl\theta_1^2 +m_2gl\theta_2^2+\frac12kl^2(\theta_1^2+\theta_2^2+2\theta_1\theta_2)\notag
\end{align}
\\ Rewriting $V$ as a matrix, \begin{align}\mathbf V &=& &[V_{ij}]\notag\\ &=& &\left[\frac{\partial^2 V}{\partial \theta_i\partial \theta_j}\right]\notag\\ &=& &\begin{pmatrix}
2m_1gl+kl^2 & -kl^2\\
-kl^2 & 2m_2gl+kl^2
\end{pmatrix}\notag
\end{align}\\
Now, the normal mode frequencies are given by the eigenvalue equation $$|\mathbf V-\omega^2\mathbf T|=0$$

$$\therefore\left|\begin{pmatrix}
2m_1gl+kl^2 & -kl^2\\
-kl^2&  2m_2gl+kl^2
\end{pmatrix}-\omega^2l^2\begin{pmatrix}
m_1 &0\\0&m_2
\end{pmatrix}\right|=0$$
Rewriting $\lambda=\omega^2l^2$
\begin{align}
&\therefore \begin{vmatrix}k l^2+2 g m_1 l-\lambda  m_1 & k l^2 \\
 k l^2 & k l^2+2 g m_2 l-\lambda  m_2 \\
\end{vmatrix} &=& 0\notag\\ &\implies 4 g^2 l^2 m_1 m_2+2 g k l^3 m_1+2 g k l^3 m_2\notag\\ &-4 g \lambda  l m_1 m_2-k \lambda  l^2 m_1-k \lambda  l^2 m_2+\lambda ^2 m_1 m_2 &=& 0\notag
\end{align}

From this we get roots for $\lambda$ as $2gl$ and $\frac{kl^2}{\mu}+2gl$, so the normal modes are $\boxed{\omega=\sqrt{2\frac{g}{l}}}$ anand $\boxed{\omega=\sqrt{2\frac{g}{l}+\frac{k}{\mu}}}$ where $\mu$ is the reduced mass.\\

The eigenvectors can be found by solving $\mathbf{VA}=\omega^2\mathbf{TA}$. This expands to $$\begin{pmatrix}
 2 g l m_1+k l^2 & -k l^2 \\
 -k l^2 & 2 g l m_2+k l^2 \\
\end{pmatrix}
\begin{pmatrix}
 A_1 \\
 A_2 \\
\end{pmatrix}
=\lambda
\begin{pmatrix}
 m_1 & 0 \\
 0 & m_2 \\
\end{pmatrix}
\begin{pmatrix}
 A_1 \\
 A_2 \\
\end{pmatrix}
$$

which gives rise to eigenvectors $\boxed{\mathbf A= \begin{pmatrix}1 \\ 1\end{pmatrix},\begin{pmatrix}m_2 \\ -m_1\end{pmatrix}}$, or relative amplitudes ($\theta_2:\theta_1$) $1$ and $-\frac{m_1}{m_2}$.

\section{Question 2}
\includegraphics[scale=1]{2.png}

Our generalized coordinates shall be the elongations in each spring, $x_1,x_2$ ($x_1$ is for the upper spring).\\
The velocities of the masses are $\dot x_1$ for $2m$ and $\dot x_1 +\dot x_2$ for $m$.\\
$\therefore T=\frac12 2m \dot x_1^2 + \frac12 m (\dot x_1+\dot x_2)^2=\frac12(3m\dot x_1^2 + m\dot x_2^2+2m\dot x_1\dot x_2)$\\
Rewriting $T$ as a matrix, $\mathbf T=[t_{ij}]=\left[\frac{\partial^2 T}{\partial \dot x_i\partial \dot x_j}\right]=\frac{m}2\begin{pmatrix}
6 & 2\\
2 &2
\end{pmatrix}$\\
The potential energy is simply $\frac12(kx_1^2+kx_2^2)-2mg(l_1+x_1)-mg(l_1+x_1+l_2+x_2)$ ($l_1,l_2$ are natural lengths of springs), giving $\mathbf V=[V_{ij}]=\left[\frac{\partial^2 V}{\partial x_i\partial x_j}\right]=\frac{k}2\begin{pmatrix}
2 & 0\\
0 &2
\end{pmatrix}$\\
Now, the normal mode frequencies are given by the eigenvalue equation $$|\mathbf V-\omega^2\mathbf T|=0$$
$$\therefore\left| \frac{k}2\begin{pmatrix}
2 & 0\\
0 &2
\end{pmatrix}-\omega^2\frac{m}2\begin{pmatrix}
6 & 2\\
2 &2
\end{pmatrix}\right|=0$$
Rewriting $\lambda=\omega^2\frac{m}{k}$
\begin{align}
&\therefore \begin{vmatrix}
2 -6\lambda & 0-2\lambda\\
0-2\lambda &2-2\lambda
\end{vmatrix} &=& 0\notag \\
&\implies (2-6\lambda)(2-2\lambda)-(-2\lambda)^2 &=& 0\notag\\
&\implies  8\lambda^2 -16\lambda +4 &=& 0\notag\\
&\implies  2\lambda^2 -4\lambda +1 &=& 0\notag\\
&\implies \lambda &=& \frac{4\pm\sqrt{16-8}}{4}\notag\\
&~ &=& 1\pm\frac1{\sqrt{2}}\notag\\
&\implies \omega &=&\sqrt{\frac{k}{m}\left(1\pm\frac1{\sqrt{2}}\right)}\notag
\end{align}

Thus, the normal modes are $\boxed{\omega_1=\sqrt{\frac{k}{m}\left(1+\frac1{\sqrt{2}}\right)},~~\omega_2=\sqrt{\frac{k}{m}\left(1-\frac1{\sqrt{2}}\right)}}$
\section{Question 3}
\begin{tikzpicture}
\node[circle,fill=gray,inner sep=2.5mm] (a) at (0,3) {$m$};
\node[above=0.1cm, above of= a] {$(x_3,y_3)$};
\node[circle,fill=gray,inner sep=2.5mm] (b) at (0,0) {$m$};\node[below=0.1cm, below of= b] {$(x_1,y_1)$} ;
\node[circle,fill=gray,inner sep=2.5mm] (c) at (3,0) {$m$}; \node[below=0.1cm, below of= c] {$(x_2,y_2)$};
\draw[decoration={aspect=0.3, segment length=3mm, amplitude=2mm,coil},decorate] (b) -- (a) node [midway, left=0.4cm] {$k$};

\draw[decoration={aspect=0.3, segment length=3mm, amplitude=2mm,coil},decorate] (b) -- (c)  node [midway, below=0.4cm] {$k$};
\end{tikzpicture}\\
 Let $q_1,q_2,q_3$ be the $x$ coordinates, and $q_4,q_5,q_6$ be the y coordinates.

The kinetic energy is $T=\frac{m}2(\sum \dot q_i^2)$, giving $\mathbf T=[t_{ij}]=\left[\frac{\partial^2 T}{\partial \dot q_i \dot q_j}\right]=m\mathbf I$, where $\mathbf I$ is the 6$\times$6 identity matrix.

\newcommand{\dxsq}[2]{(x_#1-x_#2)^2}
\newcommand{\dysq}[2]{(y_#1-y_#2)^2}
The potential energy is $$V=\frac{k}2\left(\left(\sqrt{\left(x_1-x_2\right){}^2+\left(y_1-y_2\right){}^2}-a\right){}^2+\left(\sqrt{\left(x_3-x_1\right){}^2+\left(y_3-y_1\right){}^2}-a\right){}^2\right)$$. This gives us a matrix $\mathbf V=[V_{ij}]=\left[\frac{\partial^2 V}{\partial q_i  q_j}\right]$, which when expanded gives us an unprintably large matrix. However, on substituting the equilibrium values of the coordinates, we get the much more manageable
$$\frac{k}2\begin{pmatrix}

 2 & -2 & 0 & 0 & 0 & 0 \\
 -2 & 2 & 0 & 0 & 0 & 0 \\
 0 & 0 & 0 & 0 & 0 & 0 \\
 0 & 0 & 0 & 2 & 0 & -2 \\
 0 & 0 & 0 & 0 & 0 & 0 \\
 0 & 0 & 0 & -2 & 0 & 2 \\

\end{pmatrix}$$

Now, the eigenvalue equation is $|\mathbf V-\omega^2 \mathbf T|=0$, giving us $$\left|\frac{k}{2}\begin{pmatrix}
 2 & -2 & 0 & 0 & 0 & 0 \\
 -2 & 2 & 0 & 0 & 0 & 0 \\
 0 & 0 & 0 & 0 & 0 & 0 \\
 0 & 0 & 0 & 2 & 0 & -2 \\
 0 & 0 & 0 & 0 & 0 & 0 \\
 0 & 0 & 0 & -2 & 0 & 2 \\
\end{pmatrix}-\omega^2 m\mathbf I\right|=0$$
Writing $\lambda=\frac{2m\omega^2}{k}$
$$\therefore\begin{vmatrix}

 2-\lambda  & -2 & 0 & 0 & 0 & 0 \\
 -2 & 2-\lambda  & 0 & 0 & 0 & 0 \\
 0 & 0 & -\lambda  & 0 & 0 & 0 \\
 0 & 0 & 0 & 2-\lambda  & 0 & -2 \\
 0 & 0 & 0 & 0 & -\lambda  & 0 \\
 0 & 0 & 0 & -2 & 0 & 2-\lambda  \\

\end{vmatrix}=0$$
$$\therefore \lambda ^2 \left(\lambda ^4-8 \lambda ^3+16 \lambda ^2\right)=0$$
   $$\therefore (\lambda -4)^2 \lambda ^4=0$$

   This gives us eigenvalues $\lambda=0,0,0,0,4,4$. Thus, the normal modes are $\boxed{\sqrt{2\frac{k}{m}} }$ with multiplicity 2.
\section{Question 4}
\includegraphics[scale=1]{4.png}

We shall take our coordinates as $\theta_1,\theta_2$. The velocity of the upper bob is $\frac12ml_1^2\dot\theta_1^2$. For the second bob, its position is $\mathbf r_2=l_1(\cos\theta_1\hat i +\sin\theta_1\hat j)+ l_2(\cos\theta_2\hat i +\sin\theta_2\hat j)$.  This gives us velocity $$\dot{\mathbf{r_2}}=l_1\dot\theta_1(-\sin\theta_1\hat i +\cos\theta_1\hat j)+ l_2\dot\theta_2(-\sin\theta_2\hat i +\cos\theta_2\hat j)$$, which can be rewritten as $$\dot{\mathbf{r_2}}=\hat i(-l_1\dot\theta_1\sin\theta_1-l_2\dot\theta_2\sin\theta_2)+ \hat j(l_1\dot\theta_1\cos\theta_1+l_2\dot\theta_2\cos\theta_2)$$. The kinetic energy $$T=\frac12ml_1^2\dot\theta_1^2+\frac12m|\dot{\mathbf{r_2}}|^2=\frac12m\left(2\dot{\theta }_1^2 l_1^2+\dot{\theta }_2^2
   l_2^2+2 \dot{\theta }_2 \dot{\theta }_1 l_1
   l_2 \cos \left(\theta _1-\theta _2\right)\right)$$

Rewriting as a matrix,  $$\mathbf T=[t_{ij}]=\left[\frac{\partial^2 T}{\partial \dot \theta_i \dot \theta_j}\right]=m\begin{pmatrix}
2l_1^2 & l_1l_2\cos(\theta_1-\theta_2)\\
l_1l_2\cos(\theta_1-\theta_2) & l_2^2
\end{pmatrix}$$
Near equilibrium ($\theta_1,\theta_2=0$), $\mathbf T=\begin{pmatrix}
2l_1^2 & l_1l_2\\
l_1l_2 & l_2^2
\end{pmatrix}$\\

The potential energy $V=-mgl_1\cos\theta_1-(mgl_1\cos\theta_1+mgl_2\cos\theta_2)$. This gives us a potential matrix $$\mathbf V =\left[\frac{\partial^2 V}{\partial \theta_i \theta_j}\right]=\begin{pmatrix}
2mgl_1\cos\theta_1&0\\
0& mgl_2\cos\theta_2
\end{pmatrix}$$

which, at equilibrium, is $mg\begin{pmatrix}
2l_1&0\\0&l_2
\end{pmatrix}$

The eigenvalue equation is $|\mathbf V-\omega^2 \mathbf T|=0$, which comes out to be $$\left|mg\begin{pmatrix}
2l_1&0\\0&l_2
\end{pmatrix}-\omega^2\begin{pmatrix}
2l_1^2 & l_1l_2\\
l_1l_2 & l_2^2
\end{pmatrix}\right|=0$$

With $\lambda=\frac{\omega^2}{mg}$, we get

\begin{align*}
&\left|\begin{pmatrix}
2l_1&0\\0&l_2
\end{pmatrix}-\lambda\begin{pmatrix}
2l_1^2 & l_1l_2\\
l_1l_2 & l_2^2
\end{pmatrix}\right| &=0\notag\\
&\therefore \lambda ^2 l_2^2 l_1^2-2 \lambda  l_2 l_1^2-2 \lambda  l_2^2 l_1+2 l_2 l_1 &=0 \notag\\
&\therefore \lambda &= \frac{l_1+l_2\pm\sqrt{l_1^2+l_2^2}}{l_1 l_2}\notag\\
&\therefore \omega &= \sqrt{mg\frac{l_1+l_2\pm\sqrt{l_1^2+l_2^2}}{l_1 l_2}}\notag
\end{align*}

To find the eigenvectors, we solve $\mathbf {VA}=\omega^2\mathbf{TA}$, which expands to $$\begin{pmatrix}
2l_1&0\\0&l_2
\end{pmatrix}\begin{pmatrix}
A_1\\A_2
\end{pmatrix}=\lambda\begin{pmatrix}
2l_1^2 & l_1l_2\\
l_1l_2 & l_2^2
\end{pmatrix}\begin{pmatrix}
A_1\\A_2
\end{pmatrix}$$

This gives us eigenvectors $$\mathbf A= \begin{pmatrix}
l_2 \left(\pm(l_1+l_2)+\sqrt{l_1^2+l_2^2}\right)\\
-2 l_1 \left(\sqrt{l_1^2+l_2^2}\pm l_1\right)
\end{pmatrix}$$

These are already orthogonal ($\mathbf A_1^T\mathbf{TA_2}=0$). On normalizing them ($\mathbf A_i^T\mathbf{TA_i}=1$), we get $$\boxed{\mathbf A=\frac1{2 l_1 l_2 \sqrt{l_1^2\pm  l_1\sqrt{l_1^2+l_2^2}+l_2^2}}\begin{pmatrix}
l_2 \left(\pm(l_1+l_2)+\sqrt{l_1^2+l_2^2}\right)\\
-2 l_1 \left(\sqrt{l_1^2+l_2^2}\pm l_1\right)
\end{pmatrix}}$$ for corresponding normal modes $\boxed{ \omega = \sqrt{mg\frac{l_1+l_2\pm\sqrt{l_1^2+l_2^2}}{l_1 l_2}}}$
\end{document}
}

Course material/EP 222 - Classical Mechanics/Assignment 2/Assign2.tex


% !TEX root = Thesis.tex
\section{Document Model}
\label{sec:document-model}
	In contrast to the relational model, the document model represents semi-structured as well as unstructured data.  Examples of information suitable to the document model includes emails, memos, and book chapters.

	These pieces, or units, of information are broken into documents.  Groups of related documents (e.g.~a library catalogue) are referred to as a document collection.

	\begin{defn}[Terms and Documents]
	\label{def:document}
		A term, \(\term\), is an indivisible string, e.g., a proper noun, word, or a phrase.	A document, \(\doc\), is a bag of words; order is irrelevant.

		Let \(\freq(\term, \doc)\) be the frequency of term \(\term\) in \(\doc\), \(\terms\) denote all possible terms, and \(\bag{\terms}\) be all possible bag of terms.
	\end{defn}

	\begin{remark}
		We use the bag-of-words model for documents.  This means that position information of terms in a document is irrelevant, but the frequency of terms are kept in the document.  Documents are non-distinct sets.
	\end{remark}

	\begin{defn}[Document Collection]
	\label{def:document-collection}
		A document collection \(\dc\) is a set of documents, written \(\dc = \{\doc_1, \doc_2, \dotsc, \doc_k\}\).  The cardinality of \(\dc\) is denoted by \(\gls{ndocs}\).
	\end{defn}

	\begin{ex}
	\label{ex:documents}
		Consider the following short phrases

		\begin{enumerate}
			\item MATH 360 is a math class
			\item CDPS 101 is a boring lecture
			\item mathematics lecture was great
		\end{enumerate}

		Each sentence phrase produces a document, giving us the following documents
		\begin{align}
			\doc_1 &= \{\text{``math''}: 2, \text{``a''}: 1, \text{``is''}: 1, \text{``360''}: 1, \text{``class''}: 1\} \\
			\doc_2 &= \{\text{``a''}: 1, \text{``boring''}: 1, \text{``is''}: 1, \text{``cdps''}: 1, \text{``lecture''}: 1, \text{``101''}: 1\} \\
			\doc_3 &= \{\text{``mathematics''}: 1, \text{``great''}: 1, \text{``was''}: 1, \text{``lecture''}: 1\}
		\end{align}

		with \(\dc = \{\doc_1, \doc_2, \doc_3\}\) and \(\gls{ndocs} = 3\).
	\end{ex}

	\subsection{Vectorization of Documents}
	\label{sec:vectorization-of-documents}
		The most fundamental approach for searching documents is to treat documents as high-dimensional vectors, and the document collection as a subset in a vector space.  Search queries become a nearest neighbour search in a vector space using a distance metric.

		The first step is to convert a bag of terms into vectors.  The standard technique \cite{ir-08} uses a scoring function that measures the relative importance of terms in documents.

		\begin{defn}[\Gls{tfidf} Score]
			The term frequency is the number of times a term \(\term\) appears in a document \(\doc\), as given by \(\freq(\term, \doc)\).	The document frequency of a term \(\term\), denoted by \(\df(\term)\), is the number of documents in \(\dc\) that contains \(\term\).	 It is defined as
			\[
				\df(\term) = \abs{\{\doc \in \dc: \term \in \doc\}}
			\]

			The combined \gls{tfidf} score of \(\term\) in a document \(\doc\) is given by
			\[
				\tfidf(\dc, \term, \doc) = \frac{\freq(\term, \doc)}{\abs{\doc}} \cdot \log{\frac{N}{\df(\term)}}
			\]
		\end{defn}

		The first component, \(\frac{\freq\left(\term, \doc\right)}{\abs{\doc}}\), measures the importance of a term within a document.  It is normalized to account for document length.	 The second component, \(\log{\frac{N}{\df\left(\term\right)}}\), is a measure of the rarity of the term within the document collection \(\dc\).

		\begin{ex}
			Using the documents from \cref{ex:documents}, the \gls{tfidf} scores are as follows.
			\[
				\bordermatrix{
					~ & d_1 & d_2 & d_3 \cr
					\term_{1} : \text{``101''} & 0.0000 & 0.2642 & 0.0000 \cr
					\term_{2} : \text{``360''} & 0.3170 & 0.0000 & 0.0000 \cr
					\term_{3} : \text{``a''} & 0.1170 & 0.0975 & 0.0000 \cr
					\term_{4} : \text{``boring''} & 0.0000 & 0.2642 & 0.0000 \cr
					\term_{5} : \text{``cdps''} & 0.0000 & 0.2642 & 0.0000 \cr
					\term_{6} : \text{``class''} & 0.3170 & 0.0000 & 0.0000 \cr
					\term_{7} : \text{``great''} & 0.0000 & 0.0000 & 0.3962 \cr
					\term_{8} : \text{``is''} & 0.1170 & 0.0975 & 0.0000 \cr
					\term_{9} : \text{``lecture''} & 0.0000 & 0.0975 & 0.1462 \cr
					\term_{10} : \text{``math''} & 0.6340 & 0.0000 & 0.0000 \cr
					\term_{11} : \text{``mathematics''} & 0.0000 & 0.0000 & 0.3962 \cr
					\term_{12} : \text{``was''} & 0.0000 & 0.0000 & 0.3962 \cr
				}
			\]
		\end{ex}

		\begin{defn}[Document Vector]
			Given a document collection \(\dc\) with \(M\) unique terms \(\terms = [\term_1, \term_2, \dotsc, \term_M ]\), each document \(\doc\) can be represented by an \(M\)-dimensional vector.
			\[
				\vec{\doc} =
				\left[
				\begin{array}{c}
					\tfidf(\term_1, \doc) \\
					\tfidf(\term_2, \doc) \\
					\vdots \\
					\tfidf(\term_M, \doc)
				\end{array}
				\right]
			\]
		\end{defn}

		\begin{ex}
			The documents in \cref{ex:documents} produce the following vectors.
			\[
				\vec{\doc_n} =
					\left[
						\begin{array}{l}
							\tfidf(\term_{1}, \doc_{n}) \\
							\tfidf(\term_{2}, \doc_{n}) \\
							\tfidf(\term_{3}, \doc_{n}) \\
							\tfidf(\term_{4}, \doc_{n}) \\
							\tfidf(\term_{5}, \doc_{n}) \\
							\tfidf(\term_{6}, \doc_{n}) \\
							\tfidf(\term_{7}, \doc_{n}) \\
							\tfidf(\term_{8}, \doc_{n}) \\
							\tfidf(\term_{9}, \doc_{n}) \\
							\tfidf(\term_{10}, \doc_{n}) \\
							\tfidf(\term_{11}, \doc_{n}) \\
							\tfidf(\term_{12}, \doc_{n}) \\
						\end{array}
					\right],
				\vec{\doc_1} =
					\left[
						\begin{array}{c}
							0.0000 \\
							0.3170 \\
							0.1170 \\
							0.0000 \\
							0.0000 \\
							0.3170 \\
							0.0000 \\
							0.1170 \\
							0.0000 \\
							0.6340 \\
							0.0000 \\
							0.0000 \\
						\end{array}
					\right],
				\vec{\doc_2} =
					\left[
						\begin{array}{c}
							0.2642 \\
							0.0000 \\
							0.0975 \\
							0.2642 \\
							0.2642 \\
							0.0000 \\
							0.0000 \\
							0.0975 \\
							0.0975 \\
							0.0000 \\
							0.0000 \\
							0.0000 \\
						\end{array}
					\right],
				\vec{\doc_3} =
					\left[
						\begin{array}{c}
							0.0000 \\
							0.0000 \\
							0.0000 \\
							0.0000 \\
							0.0000 \\
							0.0000 \\
							0.3962 \\
							0.0000 \\
							0.1462 \\
							0.0000 \\
							0.3962 \\
							0.3962 \\
						\end{array}
					\right]
			\]
		\end{ex}

		\begin{defn}[Search Query]
		\label{def:search-query}
			A search query \(\q\) is simply a document (\cref{def:document}).  The top-\(k\) answers to \(\q\) with respect to a collection \(\dc\) is defined as the \(k\) documents, \(\{\doc_1, \doc_2, \dotsc, d_k\}\) in \(\dc\), such that \(\{\vec{\doc_1}, \vec{\doc_2}, \dotsc, \vec{d_k}\}\) are the closest vectors to \(\vec{\q}\) using the Euclidean distance measure in \(\mathbb{R}^N\).
		\end{defn}

		\begin{ex}
			\label{ex:query}
			Given the search query \(\q = \{\text{math}, \text{lecture}, \text{was}, \text{great}\}\), compute the vector \(\vec{\q}\) within the document collection \(\dc\) (as defined in \cref{ex:documents}).
			\[
				\vec{\q} =
					\left[
						\begin{array}{c}
							0.0000 \\
							0.0000 \\
							0.0000 \\
							0.0000 \\
							0.0000 \\
							0.0000 \\
							0.2500 \\
							0.1038 \\
							0.1038 \\
							0.2500 \\
							0.0000 \\
							0.0000 \\
						\end{array}
					\right]
			\]
		\end{ex}

		We need a way of measuring the similarity between documents to determine the top-\(k\) documents for search query \(\q\).

		\begin{defn}[Cosine Similarity]
			Given two document vectors, \(\vec{\doc}_1\) and \(\vec{\doc}_2\), the cosine similarity is the dot product \(\vec{\doc}_1 \cdot \vec{\doc}_2\) normalized by the product of the Euclidean distance of \(\vec{\doc}_1\) and \(\vec{\doc}_2\) in \(\mathbb{R}^N\).	It is denoted as \(\similarity(\vec{\doc}_1, \vec{\doc}_2)\).
			\begin{align}
				\similarity(\vec{\doc}_1, \vec{\doc}_2) &= \frac{\vec{\doc}_1 \cdot \vec{\doc}_2}{\norm{\vec{\doc}_1} \cdot \norm{\vec{\doc}_2}} \\
				 &= \frac{\sum\limits_{i=1}^{N} \vec{\doc}_{1, i} \times \vec{\doc}_{2, i}}{\sqrt{\sum\limits_{i=1}^{N} (\vec{\doc}_{1, i})^2} \times \sqrt{\sum\limits_{i=1}^{N} (\vec{\doc}_{2, i})^2}}
			\end{align}
		\end{defn}

		Recall we may represent search queries as documents and thus document vectors.  Therefore we may compute the score of a document \(\doc\) for a search query \(\q\) as
		\[
			\similarity(\vec{\doc}, \vec{\q})
		\]

		\begin{ex}
			Given the document collection \(\dc\) (\cref{ex:documents}) and search query \(q\) (\cref{ex:query}), compute the similarity between \(\q\) and every document \(\doc \in \dc\).
			\begin{align}
				\similarity(\vec{\doc_1}, \vec{\q}) = 0.390890 \\
				\similarity(\vec{\doc_2}, \vec{\q}) = 0.061592 \\
				\similarity(\vec{\doc_3}, \vec{\q}) = 0.252789
			\end{align}
		\end{ex}

	\subsection{Extending the Document Model}
	\label{sec:extending-the-document-model}
		In the extended document model, documents have fields, denoted as \(\fields{\doc}\).  Each field has a value.
		\[
			\doc : \fields{\doc} \to \bag{\terms}
		\]

		\begin{ex}[Semi-Structured Document]
			We see that \(\doc_1\) is about MATH 360.  The document contents are semi-structured, containing both a course code and the subject ID.  By adding fields to the document, we are left with \cref{tbl:course-document}, which is similar in structure to \cref{tbl:hmr-properties-rel}.

			\begin{table}
				\centering

				\begin{tabular}{ll}
					\toprule
					Field & Value \\
					\midrule
					code & MATH 360 \\
					subject & MATH \\
					body & math 360 is a math class \\
					\bottomrule
				\end{tabular}

				\caption{Course document for MATH 360.}
				\label{tbl:course-document}
			\end{table}
		\end{ex}

	\subsection{Approximate String matching}
	\label{sec:n-gram}
		\begin{defn}[N-Gram]
			An \(n\)-gram is a contiguous sequence of substrings of string \(S\) of length \(n\).	 An algorithm for computing the \(n\)-gram of \(S\) is given in \cref{alg:n-gram}.
		\end{defn}

		% \char"24 - DOLLAR	 BILL Y'ALL

		\begin{algorithm}[H]
			\caption{\(\textsc{N-Gram}(S, n, s)\)}
			\label{alg:n-gram}

			\begin{singlespaced}
				\begin{algorithmic}[1]
					\REQUIRE \(S\) is a string, \(n \ge 1\), and \(s\) is a character
					\ENSURE the list of \(n\)-grams of \(S\)
					\medskip
					\STATE \(G \leftarrow []\)\label{alg:n-gram:G}
					\STATE \(p \leftarrow \textsc{Repeat}(s, n - 1)\)
					\STATE \(S \leftarrow \textsc{Pad}(S, p)\)
					\STATE \(S \leftarrow \textsc{Replace}(S, `\;', p)\)\label{alg:n-gram:S}

					\FOR{\(i=0\) \TO \(l - n + 1\)}
						\STATE append \(S[i, i + n]\) to \(G\)
					\ENDFOR

					\RETURN \(G\)
					\medskip
					\medskip
				\end{algorithmic}
			\end{singlespaced}
		\end{algorithm}

		%\todo{The medskips above should not be required, but are in single spaced mode.}

		Where \(l\) is the length of \(S\), \(\textsc{Repeat}(S, m)\) repeats the character \(s\) a total of \(m\) times, \(\textsc{Pad}(S, p)\) prefixes and postfixes \(S\) with \(p\), and \(\textsc{Replace}(S, s, p)\) replaces character \(s\) with \(p\) in string \(S\).

		\begin{ex}
		\label{ex:ngram}
			Given a string \(S = \text{``human''}\), compute the trigram of \(S\) using \cref{alg:n-gram}.
			\[
				G = \{
					\text{``\char"24\char"24h''},
					\text{``\char"24hu''},
					\text{``hum''},
					\text{``uma''},
					\text{``man''},
					\text{``an\char"24''},
					\text{``n\char"24\char"24''}
				\}
			\]
		\end{ex}

		We use \(n\)-grams to permit approximate string matching.

		\begin{ex}
		\label{ex:n-gram-comparison}
			Given a string \(S\) (\cref{ex:ngram}), let \(S' = \text{``humans''}\).  Compute the trigram of \(S'\) and compare it to \(S\).
			\[
				G' = \{
					\text{``\char"24\char"24h''},
					\text{``\char"24hu''},
					\text{``hum''},
					\text{``uma''},
					\text{``man''},
					\text{``ans''},
					\text{``ns\char"24''},
					\text{``s\char"24\char"24''}
				\}
			\]

			Comparing \(G\) to \(G'\) results in the matrix in \cref{fig:n-gram-misspelling-comparison}.  By using \(n\)-grams, we yield a similarity score of \(\sfrac{5}{10}\).

			\begin{figure}
				\[
					\bordermatrix{
						~ & G & G' \cr
						\term_{1} : \text{``ns\char"24''} & 0 & 1 \cr
						\term_{2} : \text{``n\char"24\char"24''} & 1 & 0 \cr
						\term_{3} : \text{``s\char"24\char"24''} & 0 & 1 \cr
						\term_{4} : \text{``ans''} & 0 & 1 \cr
						\term_{5} : \text{``man''} & 1 & 1 \cr
						\term_{6} : \text{``uma''} & 1 & 1 \cr
						\term_{7} : \text{``\char"24\char"24h''} & 1 & 1 \cr
						\term_{8} : \text{``hum''} & 1 & 1 \cr
						\term_{9} : \text{``\char"24hu''} & 1 & 1 \cr
						\term_{10} : \text{``an\char"24''} & 1 & 0 \cr
					}
				\]

				\caption{Comparison between \(n\)-grams of \(G\) and \(G'\).}
				\label{fig:n-gram-misspelling-comparison}
			\end{figure}
		\end{ex}

	\subsection{Benefits and Drawbacks of the Document Model}
	\label{sec:benefits-drawbacks-document-model}
		There are numerous reasons to use the document model.  The most significant reason is that it allows users without domain knowledge and working knowledge of a complex query language such as \gls{sql} to find information.  Another major advantage is that in the document model we do not need to design a database schema to store information.

		\begin{ex}[Simple Queries]
			Find all documents related to ``mathematics'' or ``lecture''.  The result of the query \(\q\) is
			\[
				\query(\text{``mathematics''}) \cup \query(\text{``lecture''}) \rightarrow \{\doc_2, \doc_3\}
			\]
		\end{ex}

		Users can also modify queries to require certain terms be present or not present.

		\begin{ex}[\texttt{AND} Query]
		\label{ex:and-query}
			Find all documents containing both ``mathematics'' and ``lecture''.  This query returns the following set of documents
			\[
				\query(\text{``mathematics''}) \cap \query(\text{``lecture''}) \rightarrow \{\doc_3\}
			\]

			as only \(\doc_3\) contains both terms.
		\end{ex}

		\begin{ex}[\texttt{NOT} Query]
			Find all documents containing ``mathematics'' but not ``lecture''.  This query returns different results than \cref{ex:and-query}.
			\[
				\query(\text{``mathematics''})\neg\query(\text{``lecture''}) \rightarrow \emptyset
			\]
		\end{ex}

		While none of the above queries required domain knowledge, it is possible to use the extended document model (\cref{sec:extending-the-document-model}) to search specific fields.  Doing so permits users to leverage their existing domain knowledge to achieve finer control over what documents are retrieved.

		\begin{ex}[Extended Query]
		\label{ex:extended-query}
			Find all documents with a subject of ``MATH'' that contain the term ``class''.
			\[
				\query(\text{``subject''}, \text{``MATH''}) \cap \query(\text{``class''}) \rightarrow \{\doc_1\}
			\]
		\end{ex}

		Not only does the document model provide a familiar interface to search for information with, it also ranks the results.  In the relational model a search for ``mathematics'' returns all named tuples that contain that term.  In the document model, documents are ranked against the query \(\q\) and the top-\(k\) documents are returned in descending order by score.

		The advantage is that users have the result of \(\q\) already ranked so only the most relevant documents may be explored.	 As the number of documents matching \(\q\) for a large corpus can be high, showing only the top-\(k\) relevant documents may save the user a substantial amount of time.

		The relational model does not permit approximate string matching.  By utilizing the document model with \(n\)-grams (\cref{sec:n-gram}), users who substitute, delete, or insert characters from the desired term may still receive results for their intended term (see \cref{ex:n-gram-comparison} for a demonstration of how \(n\)-grams overcome character insertion).

		Unfortunately the document model does not support the concept of foreign keys (\cref{def:foreign-keys}).  While information is easily accessible due to flexible search, each document is a discrete unit of information.  Aggregate queries are unsupported, as these units are not linked amongst one another.


\documentclass[letterpaper]{article}

\usepackage{fullpage}
\usepackage{nopageno}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usepackage[utf8]{inputenc}
\usepackage{aeguill}
\usepackage{setspace}

\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{trees}

\allowdisplaybreaks

\newcommand{\abs}[1]{\left\lvert #1 \right\rvert}

\begin{document}
\title{Notes}
\date{January 28, 2015}
\maketitle
\section*{continuing with trees, 2.3}
recall that a tree is a graph such that
\begin{enumerate}
\item
every edge is a bridge
\item
there are no cycles
\item
order $n$ and size $n-1$
\end{enumerate}

a natural question is to say ``how many non isomorphic trees of over $n$ are there?''

early application of graph theory is chemistry. molecules are trees

first few
\begin{enumerate}
\item
$n=1$ then we have $1$ tree
\item
$n=2$ then we have $1$ tree
\item
$n=3$ then we have $1$ non-isomorphic tree
\item
$n=4$ then we have

\tikz\path [graphs/.cd, nodes={shape=circle, draw, text=black,inner sep=1pt,outer sep=0pt}]
  graph [tree layout] { 1 -- 2; 1-- 3; 1--4 }
  [shift=(0:2)]
  graph [tree layout] { 1 -- {2 -- 3} -- 4 };
\end{enumerate}

for any $n$ the number of non-isomorphic trees on $n$-vertices is \emph{a complete mystery}

\subsection*{simpler question}
how many non-isomorphic {\bfseries labelled} trees exists on $n$-vertices?

a {\bfseries labelled graph} is a graph where each vertex is distinguished by a label.

\subsubsection*{example}
\tikz\path [graphs/.cd, nodes={shape=circle, draw, text=black,inner sep=1pt,outer sep=0pt}]
  graph [tree layout] { 1 -- 2;1 -- 3};
$\ne$
\tikz\path [graphs/.cd, nodes={shape=circle, draw, text=black,inner sep=1pt,outer sep=0pt}]
  graph [tree layout] { 2 -- 1; 2-- 3; };

two labeled graphs are different if their edge sets are different

\subsubsection*{example}
\tikz\path [graphs/.cd, nodes={shape=circle, draw, text=black,inner sep=1pt,outer sep=0pt}]
  graph [tree layout] { 1--5--4--3;5--6--2;6--4 }
  [shift=(0:2)]
  graph [tree layout] { 3--4--6--2;4--5--1;5--6 }
  [shift=(0:2)]
    graph [tree layout] { 2--5--4--1;5--6--3;4--6 }
  [shift=(0:1)];

1 and 2 are the same, 3 is different

the solution is given neatly by Prüfer codes

\subsubsection*{example}
\tikz\path [graphs/.cd, nodes={shape=circle, draw, text=black,inner sep=1pt,outer sep=0pt}]
  graph [tree layout] { 3--2--1;2--5;2--4--6 }
  [shift=(0:1)];
\begin{enumerate}
\item
record the neighbor of the leaf of least valued label
\item
erase leaf and repeat
\item
last node is not in the code
2
\tikz\path [graphs/.cd, nodes={shape=circle, draw, text=black,inner sep=1pt,outer sep=0pt}]
  graph [tree layout] { 3--2;2--5;2--4--6 }
  [shift=(0:1)];

22
\tikz\path [graphs/.cd, nodes={shape=circle, draw, text=black,inner sep=1pt,outer sep=0pt}]
  graph [tree layout] { 2--5;2--4--6 }
  [shift=(0:1)];

222
\tikz\path [graphs/.cd, nodes={shape=circle, draw, text=black,inner sep=1pt,outer sep=0pt}]
  graph [tree layout] { 2--4--6 }
  [shift=(0:1)];

2224
\tikz\path [graphs/.cd, nodes={shape=circle, draw, text=black,inner sep=1pt,outer sep=0pt}]
  graph [tree layout] { 4--6 }
  [shift=(0:1)];

2224

this process is reversible, but it's harder
\end{enumerate}

\tikz\path [graphs/.cd, nodes={shape=circle, draw, text=black,inner sep=1pt,outer sep=0pt}]
  graph [tree layout] {6--1--5--2--7;3--1;5--8--4}
  [shift=(0:1)];
prüfer code is $(1,8,1,5,2,5)$
\section*{spanning tree}
\subsection*{properties of prüfer codes}
\begin{enumerate}
\item
length is $n-2$
\item
$i$ appears at most $\deg(v_i)-1$ times
\item
at most $n-2$ \emph{different} entries
\end{enumerate}

{\bfseries Prüfer code} is a sequence of $n-2$ integers from $[n]$ where $[n]=\{1,\dots,n\}$

and so we have $n-2$ spaces with $n$ choices and so $n^{n-2}$ codes

\subsection*{Cayley's Tree theorem}
Labelled trees are in bijection with Prüfer codes. ie there are $n^{n-2}$ labeled trees on $n$ vertices (non-isomorphic)

\section*{now finally spanning trees}
{\bfseries spanning graph} of $G$ is a subgraph $H$ such that $V(G)=V(H)$.

{\bfseries weighted graph} a graph with numerical labels on the edges

question? how can we find a spanning tree of a weighted graph with least total weight?

\subsubsection*{greedy algorithm}
called Kruskal's algorithm

\begin{enumerate}
\item
choose any edge of least weight
\item
choose any remaining edge of next least weight  that doesn't make a cycle
\item
repeat
\end{enumerate}
\end{document}


\documentclass{article}
\usepackage{amsmath, amsfonts, graphicx}
\title{Synthesizing neural-network stabilizing controller for learned dynamics model}
\begin{document}
\section{Problem statement}
Assume that we are given dynamics model represented as neural networks
\begin{align}
	\text{discrete time } x[n+1] = f(x[n], u[n]) , u[n]\in\mathcal{U}\\
	\text{continuous time } \dot{x} = f(x) + G(x)u, u\in\mathcal{U}
\end{align}
where $f, G$ contain neural networks which we will describe later, our goal is to synthesize a controller $u=\pi(x)$, also represented by a neural network, and a Lyapunov function $V(x)$, represented by a third neural network, such that we can prove that the closed-loop system is Lyapunov stable. Namely we want to find a controller, such that the closed loop system is Lyapunov exponentially (or asymptotically) stable within a set $\mathcal{S}$, i.e., for the equilibrium state/control $x^*, u^*$, all the states start within $\mathcal{S}$ would eventually converge to equilibrium state.

We assume the set of admissible inputs $\mathcal{U}$ is a box in the input space, namely we have input bounds $u_{min}(i)\leq u(i)\leq u_{max}(i)$ for each dimension of the input.

Notice that we assume the continuous-time system is control affine. We will exploit this property later.

\section{Approach}
We consider the discrete-time and continuous-time system separately. Let's first consider the discrete-time case. In this project, all the neural networks are feed-forward neural network with leaky Relu units, hence the output of the network is a piecewise affine function of the input.
\subsection{Discrete time system}
First we assume that our forward dynamical system is represented by a neural network
\begin{align}
	x[n+1] = \phi_{dyn}(x[n], u[n]) - \phi_{dyn}(x^*, u^*) + x^* \label{eq:discrete_forward_dyn}
\end{align}
where $\phi_{dyn}$ is a feed-forward neural network with (leaky) ReLU activation units. Notice that by construction \eqref{eq:discrete_forward_dyn} guarantees that with $x[n]=x^*, u[n]=u^*$ the next state is still the equilibrium state $x^*$. This neural network $\phi_{dyn}$ is given and fixed.

The Lyapunov function for exponetial stability is
\begin{subequations}
\begin{align}
	V(x) > 0 \;\forall x\neq x^*, V(x^*) = 0\\
	V(x[n+1]) - V(x[n]) \le -\epsilon_2 V(x[n])\\
	x \rightarrow \infty \Rightarrow V(x)\rightarrow \infty
\end{align}
\end{subequations}

Since we will certify the Lyapunov condition through MILP, which cannot handle strict inequality constraint $V(x) > 0$, we consider the following necessary and sufficient condition
\begin{subequations}
\begin{align}
	V(x) \ge \epsilon_1 |R(x-x^*)|_1\\
	V(x[n+1]) - V(x[n]) \le -\epsilon_2V(x[n])
\end{align}
\label{eq:lyapunov_discrete}
\end{subequations}
where $R$ is a matrix with full column rank, $|R(x-x^*)|_1$ is the 1-norm of the vector $R(x-x^*)$.

We design our Lyapunov function as
\begin{align}
	V(x) = \phi_{V, \theta}(x) -\phi_{V, \theta}(x^*) + \lambda|R(x-x^*)|_1 \label{eq:lyapunov}
\end{align}
where $\phi_{V, \theta}$ is a feedforward neural network with (leaky) ReLU activation functions. $\lambda$ is a given positive constant (with $\lambda > \epsilon_1$). The reason to add the term  $\lambda|R(x-x^*)|_1$ to the Lyapunov function \eqref{eq:lyapunov}, is that it is very hard for the neural network $\phi_{V, \theta}$ to attain its minimum at $x^*$ (Since the neural network $\phi_{V, \theta}$ is a piecewise affine function of $x$. If it were to attain minimal at $x^*$, it implies that $x^*$ is the common vertex of all the neighbouring linear pieces, which is almost impossible to satisfy by gradient descent approach. See fig. \ref{fig:lyapunov_add_l1_3} as a visual explanation.) By adding the function $\lambda|R(x-x^*)|_1$ which has its global minimal at $x^*$, it is much easier to make the Lyapunov function to attain global minimal at $x^*$. Also by construction of \eqref{eq:lyapunov} we have $V(x^*) = 0$.
\begin{figure}
	\includegraphics[width=0.8\textwidth]{/home/hongkaidai/Dropbox/talks/pictures/neural_network_controller/lyapunov_add_l1_3.pdf}
	\caption{Adding the term $\lambda |x-x^*|_1$ helps the neural network to attain (local) minimal at $x^*$.}
	\label{fig:lyapunov_add_l1_3}
\end{figure}

Our controller is also represented by a neural network as
\begin{align}
	u[n] = saturate(\phi_{u, \eta}(x[n]) - \phi_{u, \eta}(x^*) + u^*)\label{eq:controller}
\end{align}
where $\phi_{u, \eta}$ is a feedforward neural network with (leaky) ReLU activation functions. The weights/biases of this network is denoted by $\eta$. $saturate$ is the saturation function that clamp the control within the input limits. Again by construction of \eqref{eq:controller}, the control action at the equilibrium state $x^*$ is $u^*$.

We could solve the following two optimization problem as MILP
\begin{align}
	\max_{x} \epsilon_1|R(x-x^*)|_1 - V(x)\\
	\max_{x[n]} V(x[n+1]) - V(x[n]) + \epsilon_2V(x[n])
\end{align}
When the maximal cost of either the function is larger than 0, we find a counter example that violates the Lyapunov condition \eqref{eq:lyapunov_discrete}. Our goal is to find the neural network for controller and Lyapunov function, such that the violation is 0. Namely we solve the following min-max problem.
\begin{align}
	\min_{\theta, \eta, R} \left(\max_{x}\epsilon_1|R(x-x^*)|_1 - V(x) + \max_{x[n]} V(x[n+1]) - V(x[n]) + \epsilon_2V(x[n])\right)
\end{align}
After solving the inner maximization problem, we then compute the gradient of the maximal cost w.r.t $\theta, \eta, R$, and then use gradient descent to minimize the loss.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Continuous-time system}
We assume that the continuous time system forward dynamics is
\begin{align}
	\dot{x} = \phi_f(x) + \phi_G(x)u - \phi_f(x^*) - \phi_G(x^*)u^*
\end{align}
where $\phi_f, \phi_G$ are given feedforward neural networks with leaky ReLU activation units. Without loss of generality we can assume that the bounds on the control input $u$ is
\begin{align}
	-\mathbf{1}\le u \le \mathbf{1}
\end{align}

Due to the control-affine property of the continuous-time system, we will show that we can search for a control Lyapunov function, instead of a controller and a Lyapunov function as in the discrete-time case. The control Lyapunov function satisfies
\begin{subequations}
\begin{align}
	V(x) \ge \epsilon_1|R(x-x^*)|_1\\
	\min_{-\mathbf{1}\le u\le \mathbf{1}} \dot{V}\le -\epsilon_2V(x)\label{eq:lyapunov_condition_Vdot_continuous}
\end{align}
\label{eq:lyapunov_condition_continuous}
\end{subequations}

Note that the left-hand side of the condition \eqref{eq:lyapunov_condition_Vdot_continuous} can be rewritten as
\begin{subequations}
\begin{align}
	&\min_{-\mathbf{1}\le u \le \mathbf{1}} \dot{V}\\
	=&\min_{-\mathbf{1}\le u \le\mathbf{1}}\frac{\partial V}{\partial x}(\phi_f(x) + \phi_G(x)u - \phi_f(x^*) - \phi_G(x^*)u^*)\\
	=&\frac{\partial V}{\partial x}(\phi_f(x) - \phi_f(x^*) - \phi_G(x^*)u^*) + \min_{-\mathbf{1}\le u \le \mathbf{1}} \frac{\partial V}{\partial x}\phi_G(x)u\label{eq:control_lyapunov3}\\
	=&\frac{\partial V}{\partial x}(\phi_f(x) - \phi_f(x^*) - \phi_G(x^*)u^*) - \left|\frac{\partial V}{\partial x}\phi_G(x)\right|_1\label{eq:control_lyapunov4}
\end{align}
\end{subequations}
From \eqref{eq:control_lyapunov3} to \eqref{eq:control_lyapunov4} we use the fact that $\min_{|x|_{\infty}\le 1} a^Tx = -|a|_1$, namely 1-norm is the \textit{dual norm} of $\infty$-norm.

We represent the control-Lyapunov function through a neural network as
\begin{align}
	V(x) = \phi_{V, \theta}(x) - \phi_{V, \theta}(x^*) + \lambda|R(x-x^*)|_1
\end{align}

And we solve the following two MILPs to either certify the control-Lyapunov condition, or find the counter-examples.
\begin{align}
	\max_{x} \epsilon_1|R(x-x^*)|_1 - V(x)\\
	\max_{x} \underbrace{\frac{\partial V}{\partial x}(\phi_f(x) - \phi_f(x^*) - \phi_G(x^*)u^*) - \left|\frac{\partial V}{\partial x}\phi_G(x)\right|_1}_{\min_{-\mathbf{1}\le u \le \mathbf{1}} \dot{V}} + \epsilon_2V(x)
\end{align}

We then compute the gradient of each MILP cost w.r.t $\theta, R$, and then use gradient descent on $\theta, R$ to minimize the loss.

\end{document}