\XtoCBlock{Atan2}
\label{block:Atan2}
\begin{figure}[H]\includegraphics{Atan2}\end{figure}

\begin{XtoCtabular}{Inports}
y & \tabularnewline
\hline
x & \tabularnewline
\hline
\end{XtoCtabular}

\begin{XtoCtabular}{Outports}
Out & Result of atan2(y/x)\tabularnewline
\hline
\end{XtoCtabular}

\subsubsection*{Description:}
Computation of the angle between the inputs x and y.

% include optional documentation file
\InputIfFileExists{\XcHomePath/Library/Math/Doc/Atan2_Info.tex}{\vspace{1ex}}{}

\subsubsection*{Implementations:}
\begin{tabular}{l l}
\textbf{FiP8} & 8 Bit Fixed Point Implementation\tabularnewline
\textbf{FiP16} & 16 Bit Fixed Point Implementation\tabularnewline
\textbf{FiP32} & 32 Bit Fixed Point Implementation\tabularnewline
\textbf{Float32} & 32 Bit Floating Point Implementation\tabularnewline
\textbf{Float64} & 64 Bit Floating Point Implementation\tabularnewline
\end{tabular}

\XtoCImplementation{FiP8}
\index{Block ID!4880}
\nopagebreak[0]
% Implementation details
\begin{tabular}{l l}
\textbf{Name} & FiP8 \tabularnewline
\textbf{ID} & 4880 \tabularnewline
\textbf{Revision} & 1.0 \tabularnewline
\textbf{C filename} & Atan2\_FiP8.c \tabularnewline
\textbf{H filename} & Atan2\_FiP8.h \tabularnewline
\end{tabular}
\vspace{1ex}

8 Bit Fixed Point Implementation

% Implementation data structure
\XtoCDataStruct{Data Structure:}
\begin{lstlisting}
typedef struct {
     uint16        ID;
     int8          *y;
     int8          *x;
     int8          Out;
} ATAN2_FIP8;
\end{lstlisting}

\ifdefined \AddTestReports
\InputIfFileExists{\XcHomePath/Library/Math/Doc/Test_Atan2_FiP8.tex}{}{}
\fi
\XtoCImplementation{FiP16}
\index{Block ID!4881}
\nopagebreak[0]
% Implementation details
\begin{tabular}{l l}
\textbf{Name} & FiP16 \tabularnewline
\textbf{ID} & 4881 \tabularnewline
\textbf{Revision} & 1.0 \tabularnewline
\textbf{C filename} & Atan2\_FiP16.c \tabularnewline
\textbf{H filename} & Atan2\_FiP16.h \tabularnewline
\end{tabular}
\vspace{1ex}

16 Bit Fixed Point Implementation

% Implementation data structure
\XtoCDataStruct{Data Structure:}
\begin{lstlisting}
typedef struct {
     uint16        ID;
     int16         *y;
     int16         *x;
     int16         Out;
} ATAN2_FIP16;
\end{lstlisting}

\ifdefined \AddTestReports
\InputIfFileExists{\XcHomePath/Library/Math/Doc/Test_Atan2_FiP16.tex}{}{}
\fi
\XtoCImplementation{FiP32}
\index{Block ID!4882}
\nopagebreak[0]
% Implementation details
\begin{tabular}{l l}
\textbf{Name} & FiP32 \tabularnewline
\textbf{ID} & 4882 \tabularnewline
\textbf{Revision} & 1.0 \tabularnewline
\textbf{C filename} & Atan2\_FiP32.c \tabularnewline
\textbf{H filename} & Atan2\_FiP32.h \tabularnewline
\end{tabular}
\vspace{1ex}

32 Bit Fixed Point Implementation

% Implementation data structure
\XtoCDataStruct{Data Structure:}
\begin{lstlisting}
typedef struct {
     uint16        ID;
     int32         *y;
     int32         *x;
     int32         Out;
} ATAN2_FIP32;
\end{lstlisting}

\ifdefined \AddTestReports
\InputIfFileExists{\XcHomePath/Library/Math/Doc/Test_Atan2_FiP32.tex}{}{}
\fi
\XtoCImplementation{Float32}
\index{Block ID!4883}
\nopagebreak[0]
% Implementation details
\begin{tabular}{l l}
\textbf{Name} & Float32 \tabularnewline
\textbf{ID} & 4883 \tabularnewline
\textbf{Revision} & 0.1 \tabularnewline
\textbf{C filename} & Atan2\_Float32.c \tabularnewline
\textbf{H filename} & Atan2\_Float32.h \tabularnewline
\end{tabular}
\vspace{1ex}

32 Bit Floating Point Implementation

% Implementation data structure
\XtoCDataStruct{Data Structure:}
\begin{lstlisting}
typedef struct {
     uint16        ID;
     float32       *y;
     float32       *x;
     float32       Out;
} ATAN2_FLOAT32;
\end{lstlisting}

\ifdefined \AddTestReports
\InputIfFileExists{\XcHomePath/Library/Math/Doc/Test_Atan2_Float32.tex}{}{}
\fi
\XtoCImplementation{Float64}
\index{Block ID!4884}
\nopagebreak[0]
% Implementation details
\begin{tabular}{l l}
\textbf{Name} & Float64 \tabularnewline
\textbf{ID} & 4884 \tabularnewline
\textbf{Revision} & 0.1 \tabularnewline
\textbf{C filename} & Atan2\_Float64.c \tabularnewline
\textbf{H filename} & Atan2\_Float64.h \tabularnewline
\end{tabular}
\vspace{1ex}

64 Bit Floating Point Implementation

% Implementation data structure
\XtoCDataStruct{Data Structure:}
\begin{lstlisting}
typedef struct {
     uint16        ID;
     float64       *y;
     float64       *x;
     float64       Out;
} ATAN2_FLOAT64;
\end{lstlisting}

\ifdefined \AddTestReports
\InputIfFileExists{\XcHomePath/Library/Math/Doc/Test_Atan2_Float64.tex}{}{}
\fi


%% LaTeX Beamer presentation template (requires beamer package)
%% see http://bitbucket.org/rivanvx/beamer/wiki/Home
%% idea contributed by H. Turgut Uyar
%% template based on a template by Till Tantau
%% this template is still evolving - it might differ in future releases!

\documentclass{beamer}
\mode<presentation>
{
% 	\usetheme{Singapore}
	\usetheme{Dresden}
	\setbeamertemplate{footline}[frame number]
	\usefonttheme{serif}

	\setbeamertemplate{navigation symbols}{}
    \setbeamertemplate{caption}[numbered]
% 	\setbeamercovered{transparent}
}

\usepackage{cancel}
\usepackage{amsmath}
\usepackage{graphics}

\usepackage{ulem}
\normalem

\usepackage{wasysym}

\usepackage{mdframed}

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

\usepackage{multicol}

\title{Graph Decomposition (2)}
\subtitle{--- BFS and its Applications}

\author{Hengfeng Wei}
\institute{hengxin0912@gmail.com}

\date{\today}

% Delete this, if you do not want the table of contents to pop up at
% the beginning of each subsection:
\AtBeginSubsection[]
{
	\begin{frame}<beamer>
		\frametitle{Outline}
		\tableofcontents[currentsection]
	\end{frame}
}

\AtBeginSection[]
{
	\begin{frame}<beamer>
		\frametitle{Outline}
		\tableofcontents[currentsection]
	\end{frame}
}
% If you wish to uncover everything in a step-wise fashion, uncomment
% the following command:

%\beamerdefaultoverlayspecification{<+->}

\begin{document}

\begin{frame}
	\titlepage
\end{frame}

\begin{frame}
	\frametitle{Outline}
	\tableofcontents
% You might wish to add the option [pausesections]
\end{frame}

%%%%%%%%%%%%%%%%%%%
\section{BFS: Algorithm}

\begin{frame}{Problem}
  \begin{exampleblock}{Problem: Shortest Path}
    Given an undirected graph $G = (V, E)$ and a source vertex $s$,
    to compute distance $\delta(s, u)$ for each vertex $u$.
  \end{exampleblock}

  \[
    \delta(s,u) \equiv \# \textrm{ of edges of the shortest path between } s
    \textrm{ and } u.
  \]
\end{frame}
%%%%%%%%%%%%%
\begin{frame}{BFS on Undirected Graph}
  BFS with source vertex $s$:
  \begin{itemize}
    \item exploring every vertex $u$ reachable from $s$
    \item \textcolor{purple}{computing $\delta(s,u)$, for all reachable $u$}
  \end{itemize}

  \vspace{0.50cm}
  BFS as a framework:
  \begin{itemize}
    \item Prim's MST algorithm
    \item Dijkstra's SSSP algorithm
  \end{itemize}
\end{frame}
%%%%%%%%%%%%%
\begin{frame}{A Physical Algorithm of BFS (\textsf{Phys-BFS})}
  \begin{columns}
    \column{0.50\textwidth}
	  \begin{figure}[htp]
	    \centering
		\includegraphics[width = 0.85\textwidth]{figure/bfs-graph.pdf}
	  \end{figure}
    \column{0.50\textwidth}
	  \begin{figure}[htp]
	    \centering
		\includegraphics[width = 0.50\textwidth]{figure/bfs-physical-alg.pdf}
	  \end{figure}
  \end{columns}
\end{frame}
%%%%%%%%%%%%%
\begin{frame}{Edge Properties of \textsf{Phys-BFS}}
  \begin{mdframed}[linecolor = blue, leftmargin = 2cm, rightmargin = 2cm]
    \[ (u,v) \in E \Rightarrow d(u) \le d(v) \le d(u) + 1 \]
  \end{mdframed}
\end{frame}
%%%%%%%%%%%%%
\begin{frame}{A Parallel Algorithm of BFS (\texttt{Para-BFS})}
  \begin{description}
    \item[graph:] network of computers
    \item[vertex:] computer
    \item[edge:] network connections
  \end{description}

  \vspace{0.50cm}
  \begin{center}
  	To disseminate a computer virus from computer $s$.
  \end{center}
\end{frame}
%%%%%%%%%%%%%
\begin{frame}{Color Properties in \texttt{Para-BFS}}
  \begin{displaymath}
	\textrm{states of computers} = \left\{ \begin{array}{ll}
	\texttt{WHITE} & \textrm{if healthy}\\
	\texttt{GRAY} & \textrm{if infected}
	\end{array} \right.
  \end{displaymath}
\end{frame}
%%%%%%%%%%%%%
\begin{frame}{A Parallel Algorithm of BFS (\texttt{Para-BFS})}
  \begin{algorithm}[H]
    \caption{A Parallel Algorithm of BFS (\texttt{Para-BFS}).}
    \begin{multicols}{2}[\columnsep = -50pt]
    \begin{algorithmic}[]
    {\footnotesize
      \Procedure{Para-BFS}{$G, s$}
		\ForAll{$u \in V$}
		  \State color[$u$] $\gets$ \texttt{WHITE}
		  \State d[$u$] $\gets$ $\infty$
		\EndFor

		\Statex
		\State color[$s$] $\gets$ \texttt{GRAY}
		\State d[$s$] $\gets$ $\infty$

		\Statex
		\State \textcolor{purple}{\textrm{Q} $\gets$ $\{ s \}$}
		\While{\textrm{Q} $\neq$ $\emptyset$}
		    \State \textcolor{purple}{$u$ $\gets$ Deq(\textrm{Q})}
			\ForAll{$(u,v) \in E$}
			  \If{color[$v$] = \texttt{WHITE}}
			    \State \textcolor{blue}{color[$v$] = \texttt{GRAY}}
			    \State d[$v$] = d[$u$] + 1
			    \State \textcolor{purple}{Enq(\textrm{Q}, $v$)}
			  \EndIf
			\EndFor
			\State \textcolor{blue}{color[$u$] $\gets$ \texttt{BLACK}}
		  \EndFor
		\EndWhile
      \EndProcedure
    }
    \end{algorithmic}
    \end{multicols}
  \end{algorithm}
\end{frame}
%%%%%%%%%%%%%
\begin{frame}{Color Properties of \texttt{Para-BFS}}
  States of vertices:
  \begin{description}
    \item [\texttt{WHILE:}] undiscovered
    \item [\texttt{GRAY:}]	discorvered but
    \item [\texttt{BLACK:}]	discorvered and all its neighbors has been
    discorvered
  \end{description}

  \begin{center}
  	\texttt{WHITE} $\Rightarrow$ \texttt{GRAY} $\Rightarrow$ \texttt{BLACK}
  \end{center}

  \begin{enumerate}
    \item $(u,v) \in E$, $u$ is \texttt{BLACK} $\Rightarrow$ $v$ is either
    \texttt{BLACK} or \texttt{GRAY}
    \item \texttt{GRAY} vertex may have adjacent \texttt{WHITE} vertices
    \\ $\Rightarrow$ ``frontier'' between discovered and undiscovered vertices
    \item Invariant: at any time, all \texttt{GRAY} vertices are in the Queue
  \end{enumerate}
\end{frame}
%%%%%%%%%%%%%
\begin{frame}{Correctness Proof \texttt{Para-BFS}}

\end{frame}
%%%%%%%%%%%%%
\begin{frame}{BFS Algorithm}

\end{frame}
%%%%%%%%%%%%%%%%%%%
\section{BFS: Properties}

%%%%%%%%%%%%%
\begin{frame}{Color Properties}

\end{frame}
%%%%%%%%%%%%%
\begin{frame}{Queue Properties}

\end{frame}
%%%%%%%%%%%%%
\begin{frame}{Correctness Proof}

\end{frame}
%%%%%%%%%%%%%
\begin{frame}{Edges Properties}

\end{frame}
% %%%%%%%%%%%%%%%%%
\section{BFS: Applications}

%%%%%%%%%%%%%
\begin{frame}{Testing Bipartiteness}

\end{frame}
%%%%%%%%%%%%%
\begin{frame}{}

\end{frame}
%%%%%%%%%%%%%
\begin{frame}{}

\end{frame}
%%%%%%%%%%%%%%%%%%
\section*{Summary}
\begin{frame}{}
  \begin{figure}[htp]
    \begin{center}
      \includegraphics[width=0.618\textwidth]{figure/thankyou.jpg}
    \end{center}
  \end{figure}
\end{frame}
%%%%%%%%%%
\end{document}


\documentclass{article}
\usepackage{parskip}
\usepackage{amsmath}
\usepackage{amssymb}

\setlength{\parindent}{0cm}

\begin{document}

\title{CPSC 413 Notes}
\author{Andrew Helwer}
\date{Winter 2011}
\maketitle

\section{Correctness of Iterative Algorithms}

\begin{itemize}
\item Precondition: a list of conditions that input must satisfy to be valid
\item Postcondition: a relationship between initial and final state
\item Assertion: a condition that holds during a certain point in execution
\item Loop Invariant: an assertion satisfied at the beginning of a loop
\item Loop Variant: an assertion satisfied at the end of the loop
\item Partial Correctness: precondition $\Rightarrow$ postcondition over the
algorithm
\item Bound Function $B$:
\begin{itemize}
\item Maps program variables to $\mathbb{Z}$
\item Loop variant implies $|B|$ has decreased by at least one
\item If $|B| \leq 0$ then the loop invariant is not satisfied
\end{itemize}
\item Termination: a bound function exists for every loop in the algorithm
\item Correctness: a partially correct algorithm that terminates
\end{itemize}

\section{Correctness of Recursive Algorithms}

\begin{itemize}
\item Recursive algorithms are guaranteed to terminate by their nature
\item Correctness of recursive algorithms is shown by structural induction
\item Structural Induction:
\begin{itemize}
\item Show correctness to hold for all base cases
\item Assume correctness for input of size $n$, then show for size $n+1$
\end{itemize}
\end{itemize}

\section{Asymptotic Notation}

\begin{itemize}
\item $f(n) \in O(g(n))$: $f$ bounded above by $g$
\item $f(n) \in \Omega (g(n))$: $f$ bounded below by $g$
\item $f(n) \in \Theta (g(n))$: $f$ bounded above and below by $g$
\end{itemize}

\section{Greedy Algorithms}

\begin{itemize}
\item Greedy Choice Property: optimal solution includes locally optimal choice
\item Greedy Algorithm: solves a problem exhibiting the greedy choice property
\item Greedy algorithms are the fastest-running solutions to a problem
\item Correctness of greedy algorithms is shown thus:
\begin{enumerate}
\item Prove correct output always exists given valid input
\item Describe how to solve trivial instances
\item Describe a greedy strategy involving a greedy choice
\item Prove some correct output includes the greedy choice
\item Prove adding the greedy choice to correct output gives correct output
\item Describe a recursive algorithm implementation
\item Prove correctness of the algorithm using structural induction
\end{enumerate}
\item Most early steps merely serve to simplify the structual induction step
\end{itemize}

\section{Divide-and-Conquer Algorithms}

\begin{itemize}
\item Problems are broken down into multiple problems of the same type
\item Once the problems become small enough, they are solved efficiently
\item The solved subproblems are then combined to form a total solution
\item Correctness of divide-and-conquer algorithms is shown thus:
\begin{enumerate}
\item Describe trivial instances and how to solve them
\item Describe how sub-instances should be formed from nontrivial ones
\item Describe how a solution is formed from sub-instance solutions
\item Describe a recursive algorithm implementation
\item Prove correctness of the algorithm using structural induction
\end{enumerate}
\end{itemize}

\section{The Master Theorem}

\begin{itemize}
\item The master theorem allows bounds to easily be placed on recurrences
\item The theorem is defined as follows:

Let $a \geq 1$ and $b > 1$ be constants, $f(n)$ be a function, and let $T(n)$
be defined over the nonnegative integers by the recurrence:

\begin{equation*}
T(n) = aT \left( \frac{n}{b} \right) +f(n)
\end{equation*}

Then $T(n)$ may be bounded asymptotically as follows based on $f(n)$:

\begin{itemize}
\item If $f(n) = O(n^{\log_ba-\epsilon})$ for some $\epsilon > 0$, then $T(n) =
\Theta(n^{\log_ba})$
\item If $f(n) = \Theta(n^{\log_ba})$ then $T(n) = \Theta(n^{\log_ba}\log n)$
\item $f(n) = \Omega(n^{\log_ba+\epsilon})$ for some $\epsilon > 0$ and
$af(n/b) \leq cf(n)$ for some constant $c<1$ and all sufficiently large $n$,
then $T(n) = \Theta(f(n))$
\end{itemize}

\end{itemize}

\end{document}


\chapter{Dump Data Format}\label{dump.chapter}

\noindent
For representing structured data in files, \CmdStan uses the dump format
introduced in \SPLUS and used in \R and \JAGS (and in \BUGS, but with
a different ordering).   A dump file is structured as a sequence of
variable definitions.  Each variable is defined in terms of its
dimensionality and its values.   There are three kinds of variable
declarations, one for scalars, one for sequences, and one for general
arrays.

\section{Creating Dump Files}

Dump files can be created from R using RStan.  The function is
\code{stan\_rdump} in package \code{rstan}.

Using R's native \code{dump()} function can produce dump files which
Stan cannot read in.  The underlying cause is that R supports
complicated data structures, some of which are not used in \CmdStan.
For example, R's \code{dump()} can write a numerical vector with names
for each element.

\section{Scalar Variables}

A simple scalar value can be thought of as having an empty list of
dimensions.  Its declaration in the dump format follows the \SPLUS
assignment syntax.  For example, the following would constitute a
valid dump file defining a single scalar variable \code{y} with value
17.2.
%
\begin{quote}
\begin{Verbatim}
y <- 17.2
\end{Verbatim}
\end{quote}
%
A scalar value is just a zero-dimensional array value.

\section{Sequence Variables}\label{sequence-variables.section}

One-dimensional arrays may be specified directly using the \SPLUS
sequence notation.  The following example defines an integer-value and
a real-valued sequence.
%
\begin{quote}
\begin{Verbatim}
n <- c(1,2,3)
y <- c(2.0,3.0,9.7)
\end{Verbatim}
\end{quote}
%
Arrays are provided without a declaration of dimensionality because
the reader just counts the number of entries to determine the size of
the array.

Sequence variables may alternatively be represented with \R's
colon-based notation.  For instance, the first example above could
equivalently be written as
%
\begin{quote}
\begin{Verbatim}
n <- 1:3
\end{Verbatim}
\end{quote}
%
The sequence denoted by \code{1:3} is of length 3, running from 1 to 3
inclusive.  The colon notation allows sequences going from high to
low, as in the first of the following examples, which is equivalent to
the second.
%
\begin{quote}
\begin{Verbatim}
n <- 2:-2
n <- c(2,1,0,-1,-2)
\end{Verbatim}
\end{quote}
%

As a special case, a sequence of zeros can also be
represented in the dump format by \code{integer(x)} and
\code{double(x)}, for type int and double, respectively.
Here \code{x} is a non-negative integer to specify the
length. If \code{x} is \code{0}, it can be ommitted. The
following are some examples.
%
\begin{quote}
\begin{Verbatim}
x1 <- integer()
x2 <- integer(0)
x3 <- integer(2)
y1 <- double()
y2 <- double(0)
y3 <- double(2)
\end{Verbatim}
\end{quote}
%

\section{Array Variables}\label{array-variables.section}

For more than one dimension, the dump format uses a dimensionality
specification.  For example,
%
\begin{quote}
\begin{verbatim}
y <- structure(c(1,2,3,4,5,6), .Dim = c(2,3))
\end{verbatim}
\end{quote}
%
This defines a $2 \times 3$ array.  Data is stored in column-major
order, meaning the values for \code{y} will be as follows.
%
\begin{quote}
\begin{Verbatim}
y[1,1] = 1     y[1,2] = 3     y[1,3] = 5
y[2,1] = 2     y[2,2] = 4     y[2,3] = 6
\end{Verbatim}
\end{quote}
%
The \code{structure} keyword just wraps a sequence of values and a
dimensionality declaration, which is itself just a sequence of
non-negative integer values.  The product of the dimensions must equal
the length of the array.

If the values happen to form a contiguous sequence of integers,
they may be written with colon notation.  Thus the example above is
equivalent to the following.
%
\begin{quote}
\begin{verbatim}
y <- structure(1:6, .Dim = c(2,3))
\end{verbatim}
\end{quote}
%
The same applies to the specification of dimensions, though it is
perhaps less likely to be used. In the above example,
c(2,3) could be written as \code{2:3}.

Arrays of more than two dimensions are written in a last-index major form.
For example,
%
\begin{quote}
\begin{verbatim}
z <- structure(1:24, .Dim = c(2,3,4))
\end{verbatim}
\end{quote}
%
produces a three-dimensional \code{int} (assignable to \code{real})
array \code{z} with values
%
\begin{quote}
\begin{verbatim}
z[1,1,1] =  1   z[1,2,1] =  3   z[1,3,1] =  5
z[2,1,1] =  2   z[2,2,1] =  4   z[2,3,1] =  6

z[1,1,2] =  7   z[1,2,2] =  9   z[1,3,2] = 11
z[2,1,2] =  8   z[2,2,2] = 10   z[2,3,2] = 12

z[1,1,3] = 13   z[1,2,3] = 15   z[1,3,3] = 17
z[2,1,3] = 14   z[2,2,3] = 16   z[2,3,3] = 18

z[1,1,4] = 19   z[1,2,4] = 21   z[1,3,4] = 23
z[2,1,4] = 20   z[2,2,4] = 22   z[2,3,4] = 24
\end{verbatim}
\end{quote}

The sequence of values inside \code{structure} can also be
\code{integer(x)} or \code{double(x)}. In particular, if one
or more dimensions is zero, \code{integer()} can be put inside
\code{structure}.  For instance, the following example is supported
by the dump format.

\begin{quote}
\begin{verbatim}
y <- structure(integer(), .Dim = c(2, 0))
\end{verbatim}
\end{quote}

\section{Matrix- and Vector-Valued Variables}

The dump format for matrices and vectors, including arrays of matrices
and vectors, is the same as that for arrays of the same shape.

\subsection{Vector Dump Format}

The following three declarations have the same dump format for their
data.
%
\begin{quote}
\begin{Verbatim}
real a[K];
vector[K] b;
row_vector[K] c;
\end{Verbatim}
\end{quote}

\subsection{Matrix Dump Format}

The following declarations have the same dump format.
%
\begin{quote}
\begin{Verbatim}
real a[M,N];
matrix[M,N] b;
\end{Verbatim}
\end{quote}

\subsection{Arrays of Vectors and Matrices}

The key to undertanding arrays is that the array indexing comes before
any of the container indexing.  That is, an array of vectors is just
that --- provide an index and get a vector.  See the chapter on array and matrix types in the user's guide section of the languag emanual for more information.

For the dump data format, the following declarations have the same
arrangement.
%
\begin{quote}
\begin{Verbatim}
real a[M,N];
matrix[M,N] b;
vector[N] c[M];
row_vector[N] d[M];
\end{Verbatim}
\end{quote}
%
Similarly, the following also have the same dump format.
%
\begin{quote}
\begin{Verbatim}
real a[P,M,N];
matrix[M,N] b[P];
vector[N] c[P,M];
row_vector[N] d[P,M];
\end{Verbatim}
\end{quote}

\section{Integer- and Real-Valued Variables}

There is no declaration in a dump file that distinguishes integer
versus continuous values.  If a value in a dump file's definition of a
variable contains a decimal point (e.g., \code{132.3}) or uses
scientific notation (e.g., \code{1.323e2}), Stan assumes that the
values are real.

For a single value, if there is no decimal point, it may be assigned
to an \code{int} or \code{real} variable in Stan.  An array value may
only be assigned to an \code{int} array if there is no decimal point
or scientific notation in any of the values.  This convention is
compatible with the way \R writes data.

The following dump file declares an integer value for \code{y}.
%
\begin{quote}
\begin{Verbatim}
y <- 2
\end{Verbatim}
\end{quote}
%
This definition can be used for a Stan variable \code{y} declared as
\code{real} or as \code{int}.  Assigning an integer value to a real
variable automatically promotes the integer value to a real value.

Integer values may optionally be followed by \code{L} or \code{l},
denoting long integer values.  The following example, where the type is
explicit, is equivalent to the above.
%
\begin{quote}
\begin{Verbatim}
y <- 2L
\end{Verbatim}
\end{quote}

The following dump file provides a real value for \code{y}.
%
\begin{quote}
\begin{Verbatim}
y <- 2.0
\end{Verbatim}
\end{quote}
%
Even though this is a round value, the occurrence of the decimal
point in the value, \code{2.0}, causes Stan to infer that \code{y} is
real valued.  This dump file may only be used for variables \code{y}
declared as real in Stan.

\subsection{Scientific Notation}

Numbers written in scientific notation may only be used for real
values in Stan.  R will write out the integer one million as
\code{1e+06}.

\subsection{Infinite and Not-a-Number Values}

Stan's reader supports infinite and not-a-number values for scalar
quantities (see the section of the reference manual section of the
language manaul for more information on Stan's numerical data types).
Both infinite and not-a-number values are supported by Stan's
dump-format readers.
%
\begin{center}
\begin{tabular}{r||c|c}
{\it Value} & {\it Preferred Form} & {\it Alternative Forms} \\ \hline \hline
positive infinity & \code{Inf} & \code{Infinity},
\code{infinity}
\\
negative infinity & \code{-Inf} & \code{-Infinity},
\code{-infinity}
\\
not a number & \code{NaN} &
\end{tabular}
\end{center}
%
These strings are not case sensitive, so \code{inf} may also be used
for positive infinity, or \code{NAN} for not-a-number.

\section{Quoted Variable Names}

In order to support \JAGS data files, variables may be double quoted.
For instance, the following definition is legal in a dump file.
%
\begin{quote}
\begin{Verbatim}
"y" <- c(1,2,3)
\end{Verbatim}
\end{quote}

\section{Line Breaks}

The line breaks in a dump file are required to be consistent with
the way \R reads in data.  Both of the following declarations are
legal.
%
\begin{quote}
\begin{Verbatim}
y <- 2
y <-
3
\end{Verbatim}
\end{quote}
%
Also following \R, breaking before the assignment arrow are not
allowed, so the following is invalid.
%
\begin{quote}
\begin{Verbatim}
y
<- 2  # Syntax Error
\end{Verbatim}
\end{quote}

Lines may also be broken in the middle of sequences declared
using the \code{c(...)} notation., as well as between the comma
following a sequence definition and the dimensionality declaration.
For example, the following declaration of a $2 \times 2 \times 3$
array is valid.
%
\begin{quote}
\begin{Verbatim}
y <-
structure(c(1,2,3,
4,5,6,7,8,9,10,11,
12), .Dim = c(2,2,
3))
\end{Verbatim}
\end{quote}
%
Because there are no decimal points in the values, the resulting dump
file may be used for three-dimensional array variables declared as
\code{int} or \code{real}.

\section{BNF Grammar for Dump Data}

A more precise definition of the dump data format is provided
by the following (mildly templated) Backus-Naur form grammar.

{\small
\begin{verbatim}
 definitions ::= definition+

 definition ::= name <- value optional_semicolon

 name ::= char*
        | ''' char* '''
        | '"' char* '"'

 value ::= value<int> | value<double>

 value<T> ::= T
            | seq<T>
            | zero_array<T>
            | 'structure' '(' seq<T> ',' ".Dim" '=' seq<int> ')'
            | 'structure' '(' zero_array<T> ',' ".Dim" '=' seq<int> ')'

 seq<int> ::= int ':' int
            | cseq<int>

 zero_array<int> ::= "integer" '(' <non-negative int>? ')'

 zero_array<real> ::= "double" '(' <non-negative int>? ')'

 seq<real> ::= cseq<real>

 cseq<T> ::= 'c' '(' vseq<T> ')'

 vseq<T> ::= T
           | T ',' vseq<T>
\end{verbatim}
}
\noindent
The template parameters \code{T} will be set to either \code{int} or
\code{real}.  Because Stan allows promotion of integer values to real
values, an integer sequence specification in the dump data format may
be assigned to either an integer- or real-based variable in Stan.


\usepackage{algpseudocode}
\lstset{language=Python,numbers=left,resetmargins=true,xleftmargin=8pt,basicstyle=\small,numberstyle=\scriptsize}
\usetikzlibrary{svg.path}
\excludecomment{solution}
\input{mixed/fig/tikzsettings}

\title{Numerical Linear Algebra}
\author{Guido Kanschat}
\date{\today}

\def\esp#1{V_{#1}}

\begin{document}
\maketitle
\tableofcontents
\chapter{Dense Algebraic Eigenvalue Problems}
\begin{intro}
  We refer to problems in linear algebra which allow storing the
  complete matrix as \define{dense linear algebra}. They are typically
  characterized by dimensions into the hundreds, possibly thousands,
  and any entry in the matrix may have a nonzero value. Such matrices
  are typically stored as a rectangular or quadratic array of numbers,
  and we can perform manipulations based on the matrix entry.

  In contrast, we will turn to \define{sparse linear algebra} in the
  later chapters, where dimensions got up to millions and billions
  ($10^9$). Since currently no computer on earth can store a matrix
  with $10^{18}$ entries, those matrices will be characterized by the
  fact that each row only contains very few nonzero entries, or that
  the matrix is not stored, but only available algorithmically in the
  form of a function performing the action $\vx\mapsto \mata
  \vx$. Thus, access to and manipulation of matrix entries is not
  possible, and we have to focus on methods only using the properties
  of the matrix as a linear mapping.
\end{intro}

\section{Mathematical background}
\subsection{Definition of Eigenvalue Problems}
\input{def-evp}
\section{Well-posedness of the EVP and bounds on eigenvalues}
\input{conditioning}

\section{Vector iterations}
\input{vector-iterations}

\section{Subspace iterations and the QR method}
\input{qr}

%\input{orthopoly}

\chapter{Solving Large Sparse Linear Systems}

\section{Motivation: discretization of partial differential equations}

\input{sparse}

\section{Basic iterative methods}
\input{iterations}

\section{Krylov-space methods}
\input{Krylov}

\chapter{Large Sparse Eigenvalue Problems}

\appendix
\chapter{Basics from Linear Algebra}
\section{Bases and matrices}
\subsection{Matrix notation for bases}
\input{bases}
\subsection{Similarity transformations}
\input{similarity}
\section{Inner products and orthogonality}
\input{inner}
\section{Projections}
\input{projections}

\chapter{Basics from Numerical Analysis}

\section{QR decomposition}
\input{qr-decomposition}

\section{Matrix norms and spectral radius}

\begin{Definition}{spectral-radius}
  The \define{spectral radius} of a matrix $\mata\in\Cnn$ is the
  maximal absolute value of its eigenvalues, that is
  \begin{gather}
    \rho(\mata) = \max_{\lambda\in\sigma(\mata)} \abs{\lambda}.
  \end{gather}
\end{Definition}

\begin{Lemma*}{spectral-radius}{Properties of the spectral radius}
  For any matrix norm $\norm\cdot$ and for any matrix $\mata\in\Cnn$ there holds
  \begin{gather}
    \rho(\mata) = \lim_{k\to\infty} \norm{\mata^k}^{1/k}.
  \end{gather}

  The sequence $\vx^{(k)} = A^k\vx$ converges to zero for all
  $\vx\in\C^n$ if and only if $\rho(\mata)<1$.

  For any $\epsilon>0$
  there is a matrix norm $\norm\cdot$ such that
  \begin{gather}
    \norm{\mata} \le (1+\epsilon) \rho(\mata) \qquad \forall \mata\in\Cnn.
  \end{gather}
\end{Lemma*}

\bibliographystyle{alpha}
\bibliography{all}
\printindex

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


\documentclass[oneside]{memoir}
\usepackage{fontspec}
\usepackage[hidelinks]{hyperref}
\usepackage{microtype}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{bm}
\usepackage{multirow}
\usepackage[euler-digits,euler-hat-accent]{eulervm}
\usepackage[bibstyle=numeric,backend=biber, sorting=none]{biblatex}
\addbibresource[datatype=bibtex]{../methods.bib}

\setlrmarginsandblock{3.5cm}{2.5cm}{*}
\setulmarginsandblock{2.5cm}{*}{1}
\checkandfixthelayout

\newcommand\ddfrac[2]{\frac{\displaystyle #1}{\displaystyle #2}}

\setmainfont{TeX Gyre Pagella}

\usepackage{xcolor}

\hypersetup{
    pdfauthor={Tomáš Raček},
    pdfpagemode=UseOutlines,
    colorlinks,
    linkcolor={red!50!black},
    citecolor={blue!50!black},
    urlcolor={blue!80!black}
}

\def\chapterheadstart{}

\begin{document}

\chapter*{Short description of the methods}
\addcontentsline{toc}{chapter}{Short description of the methods}
This section shortly describes the main idea of each method implemented in Atomic Charge Calculator~II. Methods here are ordered according to the publication date.

For clarity, we use unifying naming scheme (which may differ from the one used in the original publication); the symbols used throughout the chapter: $q_i$ stands for charge on $i$th atom, $Q$ is the total molecular charge, $N$ is the number of atoms in a molecule and $R_{i, j}$ represents the Euclidean distance between atoms $i$ and $j$.

\section*{DelRe}
\label{sec:methods_delre}
Methods of Del Re \cite{DelRe1958} starts with the definition of a linear system in the form:

\begin{equation}
\label{eq:delre_main}
\delta_i = \delta_i^0 + \sum_{j}\gamma_{i, j}\delta_j
\end{equation}

where $j$ iterates over atoms bonded to $i$. $\delta_i^0$ is an atom parameter, whereas $\gamma_{i, j}$ is a bond parameter. Solving for $\delta_i$ allow us to derive bond charges:

\begin{equation}
\label{eq:delre_bond}
q_{i, j} = \ddfrac{\delta_i - \delta_j}{2\epsilon_{i, j}}
\end{equation}

where $\epsilon_{i, j}$ is another bond parameter. Finally, charge for each atom is computed as the sum of all involved bond charges.

\section*{PEOE}
\label{sec:methods_peoe}

Partial equalization of orbital electronegativity \cite{Gasteiger1978, Gasteiger1980} is an iterative scheme in which the charges are moved along the bonds from the more electropositive atom to the more electronegative one. The amount of charge shifted is proportional to the difference of the electronegativities of the bonding partners. As effective electronegativity is defined here as a function of charge:

\begin{equation}
\label{eq:peoe_chi}
\chi_i^\alpha = A_i + B_iq^\alpha + C_i(q_i^\alpha)^2
\end{equation}

its value for each atom must be recomputed as it enters the next iteration.

The main idea of a charge transfer is expressed through the following equation:

\begin{equation}
\label{eq:peoe_main}
q_i^\alpha = \left(\sum_j \ddfrac{\chi_j^\alpha - \chi_i^\alpha}{D_i} + \sum_k \ddfrac{\chi_i^\alpha - \chi_k^\alpha}{D_k}\right)\cdot{\left(\ddfrac{1}{2}\right)}^\alpha
\end{equation}

where $j$ are atoms bonded to atom $i$ with higher electronegativity and $k$ are atoms bonded to atom $i$ with lower electronegativity.

Since the generated charges produce an electrostatic field which further hinders the charge transfer, the dampening factor $(1/2)^\alpha$ was introduced to account for that fact. Usually, six iterations of \ref{eq:peoe_chi} followed by \ref{eq:peoe_main} are necessary for charges to converge.

Finally, total atomic charge $q_i$ is the sum of charge transfers across all the iterations:

\begin{equation}
\label{eq:peoe_total}
q_i = \sum_\alpha q_i^\alpha
\end{equation}

\section*{Charge2}
\label{sec:methods_charge2}

Charge2 \cite{Abraham1982} is an iterative method in which charge increments from neighbor atoms are added to a central one.

\begin{align}
\label{eq:charge2_main}
q_i &= q_i(\alpha) + q_i(\beta) + q_i(\gamma)\\
q_i(\alpha) &= \sum_j \ddfrac{\chi_j - \chi_i}{a}\\
q_i(\beta) &= \sum_k \ddfrac{(\chi_k - \chi_H)P_i}{b}\\
q_i(\gamma) &= \sum_l \ddfrac{(\chi_l - \chi_H)P_i}{bc}\\
\end{align}

where
\begin{equation}
\label{eq:charge2_aux}
P_i = P_i^0\left(1 + \alpha(q_i^0 - q_i)\right)
\end{equation}

and $j, k$ and $l$ represents atoms one, two or three bonds apart from atom $i$, $a, b, c$ and $P^0$ are atom parameters, $q^0$ is a formal charge, $\chi_H$ is an electronegativity of hydrogen, and $\alpha$ is a common parameter.

\section*{EEM}
\label{sec:methods_eem}
Contrary to the partial electronegativity equalization methods like PEOE or MPEOE, full electronegativity equalization is fundamental to the Mortier's Electronegativity Equalization Method \cite{Mortier1986}.

According to the Sanderson's principle, the electronegativity of each atom gets equalized when atoms bond to form a molecule:

\begin{equation}
\label{eq:eem_sanderson}
\overline{\chi} = \chi_1 = \ldots = \chi_N
\end{equation}

The electronegativity of an atom in a molecule is expressed as:

\begin{equation}
\label{eq:eem_main}
\chi_i = A_i + B_iq_i + \sum_{i \neq j} \ddfrac{q_j}{R_{i, j}}
\end{equation}

where

\begin{align}
A_i &= \chi_i^0 + \Delta\chi_i\\
B_i &= 2(\eta_i^0 + \Delta\eta_i)
\end{align}

$\chi^0$ is an electronegativity of an isolated atom, $\eta^0$ is a hardness of an isolated atom. $\Delta$ symbols represent corrections for the molecular environment.

Finally, charge conservation principle holds:

\begin{equation}
\label{eq:eem_sum}
Q = \sum_i q_i
\end{equation}

Rewriting \ref{eq:eem_main} for every atom in molecule subject to \ref{eq:eem_sanderson} and \ref{eq:eem_sum} yields a system of $N + 1$ linear equations:

\begin{gather}
	\label{eq:eem_system}
	\begin{bmatrix}
		B_1			    & R_{1,2}^{-1}  & \cdots	& R_{1,N}^{-1}	& 1 \\
		R_{2,1}^{-1}	& B_2			& \cdots	& R_{2,N}^{-1}	& 1 \\
		\vdots			& \vdots		& \ddots	& \vdots		& \vdots \\
		R_{N,1}^{-1}	& R_{N,2}^{-1}	& \cdots	& B_N			& 1 \\
		1			    & 1		    	& \cdots	& 1		    	& 0 \\
	\end{bmatrix}
	\cdot
	\begin{bmatrix}
		q_1 \\
		q_2 \\
		\vdots \\
		q_N \\
		-\overline{\chi}
	\end{bmatrix}
	=
	\begin{bmatrix}
		-A_1\\
		-A_2\\
		\vdots \\
		-A_N\\
		Q\\
	\end{bmatrix}
\end{gather}

\section*{MPEOE}
\label{sec:methods_mpeoe}

Modified Partial Equalization of Orbital Electronegativity \cite{No1990} differs from original \hyperref[sec:methods_peoe]{PEOE} by expressing electronegativity as a linear function of charge, so that \ref{eq:peoe_chi} is modified to:

\begin{equation}
\label{eq:mpeoe_chi}
\chi_i^\alpha = A_i + B_iq^\alpha
\end{equation}

Other than that, the dampening factor $(1/2)^\alpha$ is a now considered as a bond type dependent parameter $f_{x, y}$ changing \ref{eq:peoe_main} to:

\begin{equation}
\label{eq:mpeoe_main}
q_i^\alpha = \sum_j \ddfrac{\chi_j^\alpha - \chi_i^\alpha}{D_i}f_{i, j}^\alpha + \sum_k \ddfrac{\chi_i^\alpha - \chi_k^\alpha}{D_k}f_{i, k}^\alpha
\end{equation}

\section*{QEq}
\label{sec:methods_qeq}

Charge Equilibration (QEq) \cite{Rappe1991} is similar to \hyperref[sec:methods_eem]{EEM}. However, originally, it was meant as an iterative scheme as parameter $J_{i, i}$ for hydrogen was defined to be charge-dependent.

\begin{equation}
\label{eq:qeq_main}
\chi_i = \chi_i^0 + J_{i, i}^0q_i + \sum_{i \neq j} J_{i,j}q_j
\end{equation}

In the original publication values for the Coulomb repulsion term $J_{i, j}$ were obtained using \textit{ab-initio} calculations. To simplify the process, several empirical terms were developed to substitute $J_{i, j}$ with some simple expression.

The system of linear equations is constructed and solved for $q$ similarly to the one in \hyperref[eq:eem_system]{EEM}.

\section*{ABEEM}
\label{sec:methods_abeem}

Atom-bond Electronegativity Equalization Method \cite{Yang1997} extends original \hyperref[sec:methods_eem]{EEM} to also include bond electronegativities into the equalization scheme. Electronegativity of atom $i$ is expressed as:

\begin{equation}
\label{eq:abeem_atom}
\chi_i = A_i + B_iq_i + C_i\sum_{i-j}q_{i-j} + k\sum_{i \neq j}\ddfrac{q_j}{R_{i, j}} + k \sum_{k-l \neq i-j}\ddfrac{q_{k-l}}{R_{i, k-l}}
\end{equation}

where $A, B$ and $C$ are atom parameters, $k$ is a common parameter, $q_{i-j}$ denotes the charge on the bond $i-j$. Distance to a bond is computed to its center proportional to the covalent radius of the constituent atoms. Electronegativity of a bond $i-j$ has the following form:

\begin{equation}
\label{eq:abeem_bond}
\chi_{i-j} = A_{i-j} + B_{i-j}q_{i-j} + C_{i-j, i}q_i + D_{i-j, j}q_j + k\sum_{k\neq i, j}\ddfrac{q_k}{R_{i-j, k}} + k\sum_{k-l\neq i-j}\ddfrac{q_{k-l}}{R_{i-j,k-l}}
\end{equation}

where $A, B, C$ and $D$ are bond parameters and $k$ is a common parameter.

Solving the system of linear equations provides us with the atomic and bond charges. The bond charges are then added onto the constituent atoms proportionally to their covalent radii yielding the final atomic charges.

\section*{GDAC}
\label{sec:methods_gdac}

Further modification of \hyperref[sec:methods_mpeoe]{MPEOE} method is coined Geometry-dependent Atomic Charges \cite{Cho2001}. GDAC modifies the dampening term $f_{x, y}$ to be geometry dependent:

\begin{equation}
f_{x, y} = 1 - \ddfrac{R_{x, y}}{R_x^{\text{vdw}} + R_y^{\text{vdw}}}
\end{equation}

where $R^{\text{vdw}}_x$ stands for the van der Waals radius of atom $x$.

\section*{MGC}
\label{sec:methods_mgc}

Molecular Graph Charge model (MGC) \cite{Oliferenko2000, Oliferenko2001} uses molecular graph representation of the molecule as it is inspired by electrical circuits and Kirchhoff's current laws. Therefore, no atomic coordinates are employed.

MGC constructs auxiliary matrix $\bm{S}$ in the following way:
\begin{equation}
\label{eq:mgc_main}
\bm{S} = -\bm{A} + \bm{D} + \bm{I}
\end{equation}

where $\bm{A}$ is a connectivity matrix, $\bm{D}$ represents diagonal degree matrix and $\bm{I}$ is a standard identity matrix.

Equalized electronegativities are obtained from those of isolated atoms as a solution to the system of linear equations.

\begin{equation}
\label{eq:mgc_system}
\bm{S\chi} = \bm{\chi}^0
\end{equation}

Finally, partial atomic charges are computed as a difference between equalized and standard electronegativities of respective atoms, divided by the average electronegativity $\chi_M$ (geometric average).

\begin{equation}
\label{eq:mgc_charges}
\bm{q} = \ddfrac{\bm{\chi} - \bm{\chi}^0}{\bm{\chi}_M}
\end{equation}

\section*{SFKEEM}
\label{sec:methods_sfkeem}

Selfconsistent Functional Kernel Equalized Electronegativity Method \cite{Chaves2006} develops on \hyperref[sec:methods_eem]{EEM}'s main idea. However, it incorporates different hardness matrix. The electronegativity equalization principle in SFKEEM is expressed as:

\begin{equation}
\label{eq:sfkeem_main}
\chi_i = A + 2B_iq_i + \sum_{i \neq j} 2\sqrt{B_iB_j}\mathrm{sech}(\sigma R_{i, j})
\end{equation}

where $A, B$ and $\sigma$ are empirical parameters and $\mathrm{sech}$ is a hyperbolic secant function.

\section*{KCM}
Kirchhoff Charge Model \cite{Yakovenko2008} builds a Laplacian matrix $\bm{L}$ as:

\begin{equation}
\label{eq:ksm_laplacian}
\bm{L} = \bm{B}^T \bm{W} \bm{B}
\end{equation}

where $\bm{B}$ is an incidence matrix and $\bm{W}$ is a diagonal "softness" matrix with elements $w_{i, i} = 1 / (\eta_i + \eta_j)$, where $\eta$ stands for hardness of an atom.

Atomic charges are derived using the following expression:

\begin{equation}
\label{eq:kcm_charges}
\bm{q} = (\bm{L}^{-1} - \bm{I})\bm{\chi^0}
\end{equation}

where $\bm{L}^{-1}$ is an inverse of $\bm{L}$ and $\bm{\chi^0}$ represents a vector of electronegativities of isolated atoms.

\section*{DENR}
\label{sec:methods_denr}

Dynamic electronegativity relaxation \cite{Shulga2008} is an iterative 2D scheme in which charges are derived using a Laplacian matrix:

\begin{equation}
\label{eq:denr_main}
\bm{q}^{(n + 1)} = (\bm{I} + c\Delta t\cdot \bm{B}_0)^{-1}\cdot(\bm{q}^{(n)} - c\Delta t\cdot \bm{a}_0)
\end{equation}

where $\bm{B}_0 = \bm{L}\bm{\eta}_0$ and $\bm{a}_0 = \bm{L}\bm{\chi}_0$. Note that $\bm{\eta}_0$ is a diagonal matrix of atomic hardnesses.

\section*{TSEF}
\label{sec:methods_tsef}

Topologically Symmetrical Energy Function \cite{Shulga2008} has electronegativity equalization principle as its base but changes off-diagonal term to include bond distance rather than Euclidean distance making TSEF conformationally independent.

\begin{equation}
\label{eq:tsef_main}
\phi_{i, j} = \alpha \cdot K\mathrm{(MDP_{i, j})}\cdot\ddfrac{1}{0.84\cdot\mathrm{MDP}_{i, j} + 0.46}
\end{equation}

where $\mathrm{MDP}$ stands for \textit{minimal distance path}, i.e., minimal number of bonds between two atoms, $K$ is parameter and $\alpha$ is a unit conversion factor.

\section*{SMP/QEq}
\label{sec:methods_smpqeq}

Self-Consistent Charge Equilibration Method \cite{Zhang2009} builds upon the idea of the original \hyperref[sec:methods_qeq]{QEq}, electronegativity of an atom is formalized as a function of charge, thus the whole scheme is a iterative one. The main equation follows:

\begin{equation}
\label{eq:smpqeq_main}
\chi_i(q_i) = A_i + 2\lambda(q_i)q_i + \sum_{i\neq j} J_{i, j}q_j
\end{equation}

where

\begin{equation}
\label{eq:smpqeq_lambda}
\lambda(q_i) = B_i + C_iq_i + D_i(q_i)^2
\end{equation}

and

\begin{equation}
\label{eq:smpqeq_J}
J_{i, j} = \left(\ddfrac{1}{(2\sqrt{B_iB_j})^3} + R_{i,j}^3\right)^{-1/3}
\end{equation}

where $A, B, C$ and $D$ are atom parameters.

\section*{VEEM}
\label{sec:methods_veem}

Valence electrons equilibration method \cite{Wu2011} calculates atomic charges based on the number of valence electrons of individual atoms and atomic groups.

First, equalized electronegativity is calculated for the whole molecule:

\begin{equation}
\label{eq:veem_en}
\chi_{ve} = \ddfrac{\sum_i \chi_iN_{ve, i}}{\sum_i N_{ve, i}}
\end{equation}

where $\chi_i$ is an electronegativity of isolated atom $i$; $N_{ve, i}$ stands for the number of valence electrons of atom $i$.

Finally, the partial atomic charge of atom $i$ is computed as:

\begin{equation}
\label{eq:veem_q}
q_i = N_{ve, i}\ddfrac{\chi_{ve} - \chi_i}{\chi_{ve}}
\end{equation}

\section*{EQeq}
\label{sec:methods_eqeq}
Extended charge equilibration method \cite{Wilmer2012} builds upon original \hyperref[sec:methods_qeq]{QEq} scheme, which is modified to take the following form (the simplest, non-periodic case without different charge centers):

\begin{equation}
\label{eq:eqeq_main}
\chi_i = \chi_i^0 + J_i^0q_i + \ddfrac{K}{2}\sum_{i \neq j} q_j\left(\ddfrac{1}{R_{i, j}} + O_{i, j}\right)
\end{equation}

where
\begin{eqnarray}
\label{eq:eqeq_default}
\chi_i^0 &=& \ddfrac{\mathrm{IP}_i + \mathrm{EA}_i}{2}\\
J_i^0 é &=& \mathrm{IP}_i - \mathrm{EA}_i
\end{eqnarray}

K is a constant; IP and EA stand for ionization potential and electron affinity, respectively, and

\begin{equation}
\label{eq:eqeq_overlap}
O_{i, j} = \exp{\left(-\ddfrac{J_{i, j}^2R_{i, j}^2}{K^2}\right)}\cdot\left(\ddfrac{J_{i, j}}{K} - \ddfrac{J_{i, j}^2R_{i, j}}{K^2} - \ddfrac{1}{R_{i, j}}\right)
\end{equation}

where $J_{i, j}$ is a geometric mean of $J_i^0$ and $J_j^0$.

\section*{EQeq+C}
Bond-order-corrected Extended Charge Equilibration Method \cite{MartinNoble2015} follows exactly the same procedure as \hyperref[sec:methods_eqeq]{EQeq}, however, after the computation is done, some corrections are added to the original charges, i.e.:

\begin{equation}
\label{eq:eqeqc_main}
q_i = q_i^0 + \sum_{j \neq i} T_{i, j}B_{i, j}
\end{equation}

where $q_i^0$ is original charge from EQeq, and

\begin{eqnarray}
\label{eq:eqeqc_terms}
T_{i, j} &=& D_i - D_j\\
B_{i, j} &=& \exp\left[-\alpha\left(R_{i, j} - r_i - r_j\right)\right]
\end{eqnarray}

where $D$ is an atom parameter, $\alpha$ is a common parameter and $r$ stands for covalent radius.

\section*{SQE}

Split-charge Equilibrium method \cite{Nistor2006} is based on the electronegativity equalization principle. However, unlike EEM or QEq, it does not perform equalization on a level of individual atoms but switches the problem to a bond domain by defining \textit{split-charges}, i.e., charges located on the bonds. The final atomic charge as a sum of the split-charges of the bonds that a particular atom forms. Formally, the atomic charge on atom $i$ is expressed as:

\begin{gather}
    \label{eq:sqe_sum}
    q_i = \sum_{j \in \mathrm{BA}(i)} p_{i, j}
\end{gather}

where $\mathrm{BA}(i)$ is a set of atoms bonded to atom $i$, and $p_{i, j}$ is a split-charge on the bond $i - j$.

SQE method written in the form of the system of linear equations is described by Equation~\ref{eq:sqe}:

\begin{gather}
    \label{eq:sqe}
    \left(THT^T + \mathrm{diag}(\kappa)\right)q_{sp} = T\chi
\end{gather}

where $q_{sp}$ is a vector of split-charges, $T$ is an incidence matrix describing the molecular topology, $\mathrm{diag}(\kappa)$ is a diagonal matrix with bond hardnesses, $\chi$ is a vector of atomic electronegativities, and $H$ is a hardness matrix that describes the interactions between the atoms.

To reconstruct the atomic charges $q$ from the split-charges, the following transformation is made:

\begin{gather}
    \label{eq:sqe_atomic}
    q = T^Tq_{sp}
\end{gather}

\section*{SQE+q0}
SQE+q0 \cite{Verstraelen2012}, an extension to SQE, adds formal charges to work as initial seeds for the computation of partial atomic charges which might help in molecules that contain charged functional groups. This change is expressed in Equations~\ref{eq:sqe_q0} and \ref{eq:sqe_q0_atomic}:

\begin{gather}
    \label{eq:sqe_q0}
    \left(THT^T + \mathrm{diag}(\kappa)\right)q_{sp} = T(\chi - Hq_0 + \eta * q_0)
\end{gather}

where $q_0$ is a vector of initial formal charges, $\eta$ is a vector of atomic hardnesses (i.e., $J_{i, i}$ terms), and $*$ is an element-wise product. Equation~\ref{eq:sqe_atomic} is then trivially modified to:

\begin{gather}
    \label{eq:sqe_q0_atomic}
    q = T^Tq_{sp} + q_0
\end{gather}

\section*{SQE+qp}
SQE+qp replaces the formal charges $q_0$ with the parameterized ones $q_p$. To preserve the total charge of the molecule, these parameterized values must be normalized first:

\begin{align}
    \label{eq:sqe_qp_norm}
    q_{pn} &= q_p - \frac{1}{N}\left(1^Tq_p - Q\right)\\
\end{align}

where $1^T$ is a row vector of ones and $N$ is the number of atoms in a molecule. The rest of the procedure is analogous as in SQE+q0:

\begin{align}
    \label{eq:sqe_qp}
    \left(THT^T + \mathrm{diag}(\kappa)\right)q_{sp} &= T(\chi - Hq_{pn} + \eta * q_{pn})\\
    q &= T^Tq_{sp} + q_{pn}
\end{align}

\chapter*{Complexity of implemented approaches}
\addcontentsline{toc}{chapter}{Complexity of implemented approaches}

The following table summarizes information about computational costs of the implemented methods, both in terms of time and memory complexity (expressed using ${\mathcal O}$ notation). The symbol $N$ denotes the number of atoms, $M$ is the number of bonds.
\bigskip

\begin{center}
\begin{tabular}{lll}
\toprule
Method & Time & Memory\\
\midrule
DelRe & $N^3$ & $N^2$\\
PEOE  & $N + M$ & $N$\\
Charge2 & $N + M$ & $1$\\
EEM* & $N^3$ & $N^2$\\
MPEOE & $N + M$& $N$\\
QEq* & $N^3$ & $N^2$ \\
ABEEM & $(N + M)^3$ & $(N + M)^2$\\
GDAC & $N + M$ & $N$\\
MGC & $N^3$& $N^2$\\
SFKEEM* & $N^3$& $N^2$\\
KCM & $N^3$ & $N^2$\\
DENR & $N^3$& $N^2$\\
TSEF & $N^3$ & $N^2$ \\
SMP/QEq* & $N^3$& $N^2$\\
VEEM & $N$& $1$ \\
EQeq* & $N^3$ & $N^2$ \\
EQeq+C* & $N^3$ & $N^2$\\
SQE & $M^3$ & $M^2$\\
SQE+q0 & $M^3$ & $M^2$\\
SQE+qp & $M^3$ & $M^2$\\
\bottomrule
\end{tabular}
\end{center}

\bigskip

Methods marked with * can utilize \textit{cutoff} and \textit{cover} complexity reductions described in the next section.

\chapter*{Assessment of charges quality}
\addcontentsline{toc}{chapter}{Assessment of charges quality}

In order to compare two sets of charges, two descriptors are commonly used. The first one is the Pearson's correlation coefficient ($R$); the second one is the root mean square deviation ($RMSD$). In the following sections, we denote the charge sets as $X = (x_1, \ldots, x_n)$ and $Y = (y_1, \ldots, y_n)$, $\overline{x}$ and $\overline{y}$ represent the arithmetic means of $x_i$ and $y_i$ values, respectively.

\section*{Pearson's correlation coefficient (R)}

Pearson's correlation coefficient describes the linear dependence between two sets of values. Its value ranges from -1 (negative correlation) through 0 (no correlation) to 1 (positive correlation). Sometimes, the value of squared Pearson's correlation coefficient ($R^2$) is used.

\begin{align*}
R(X, Y) = \ddfrac{\sum_{i = 1}^n (x_i - \overline{x})\cdot(y_i - \overline{y})}{\sqrt{\sum_{i = 1}^n(x_i - \overline{x})^2}\cdot\sqrt{\sum_{i = 1}^n(y_i - \overline{y})^2}}
\end{align*}

\section*{Root mean square deviation (RMSD)}

Root mean square deviation is another measure which can be used to compare two sets of values. When RMSD is zero, the sets are identical. Otherwise, a smaller value indicates higher similarity.

\begin{align*}
RMSD(X, Y) =\sqrt{ \ddfrac{1}{n}\sum_{i = 1}^n (x_i - y_i)^2}
\end{align*}

\chapter*{Cutoff and cover approaches}
\addcontentsline{toc}{chapter}{Cutoff and cover approaches}

Solving the electronegativity equalization system of linear equations can be troublesome for very large structures as it requires, in general, ${\mathcal O}(N^3)$ steps and ${\mathcal O}(N^2)$ memory. The original AtomicChargeCalculator (ACC) introduced two \textit{divide and conquer} complexity reduction algorithms to overcome this issue. Since ACC only supports EEM, these approaches were called \textit{EEM Cutoff} and \textit{EEM Cover}. ACC~II extended these approaches to other applicable methods, referencing them simply as \textit{cutoff} and \textit{cover}.

Here, we provide the description of these approaches as stated in the ACC publication's \cite{Ionescu2015} \href{https://static-content.springer.com/esm/art%3A10.1186%2Fs13321-015-0099-x/MediaObjects/13321_2015_99_MOESM1_ESM.pdf}{Additional file 1}:

\begin{quote}

\section*{EEM Cutoff}
For each atom in the molecule, ACC generates a fragment made up of all atoms within a cutoff
radius $R$ of the original atom. The values of the inter-atomic distances and EEM parameters are
obtained in the same way as when solving the full EEM matrix. The total fragment charge $Q_F$
is a quota of the total molecular charge $Q$, proportional to the number of atoms in the fragment
($N_F$), and irrespective of the nature of these atoms:

\begin{align*}
Q_F = \frac{Q\cdot N_F}{N}
\end{align*}

Then ACC solves the EEM matrix equation for this fragment, and returns the charge for the
atom used when generating the fragment. The same procedure is applied for all fragments,
obtaining a set of charges for all the atoms in the molecule. Then, each atomic charge $q_i$
is corrected by the addition of:

\begin{align*}
\frac{Q - \sum_{i = 1}^N q_i}{N}
\end{align*}

so that the sum of all atomic charges equals the total molecular charge $Q$.

\section*{EEM Cover}
The EEM Cover approach builds on the principles of EEM Cutoff to split the EEM matrix into
smaller matrices. However, EEM Cover generates fragments only for a subset of atoms in the
molecule. The procedure selects fragment-generating atoms so that: (i) no two such atoms are
connected to each other, and (ii) each atom in the molecule has at least one neighbor (within
two bonds) which was selected. This procedure ensures that each atom in the molecule will
eventually contribute to at least one fragment, and thus the entire volume of the molecule is
covered. ACC solves the EEM matrix equation for each fragment, and returns a list of charge
contributions for all atoms encountered in the calculations. The charge on each atom in the
molecule is then computed as the sum of its charge contributions from all fragments where the
atom is present. Further, each atomic charge $q_i$ is corrected by the addition of:

\begin{align*}
\frac{Q - \sum_{i = 1}^N q_i}{N}
\end{align*}

so that the sum of all atomic charges equals the total molecular charge $Q$.

\end{quote}

\section*{Time and space complexity}

For a given sphere radius $R$, these approaches effectively reduce the time and space complexity to ${\mathcal O}(R^6N + R^2N\log N)$ and ${\mathcal O}(R^4N + N\log N)$, respectively. \cite{Ionescu2015}

\section*{Accuracy}

Employing these approximative schemes may introduce some loss of accuracy when comparing with the original charges. Their assessment was made for original ACC for EEM \cite{Ionescu2015} or in \cite{Sehnal2015} where author states that "according to tests, the EEM Cover method produces results that are practically identical to solving the EEM matrix for the full original system". Here, we present a simplified\footnote{Only for $R = 12~$\AA\,, which is used in ACC II.} version of the table taken from \cite{Sehnal2015}:

\begin{table}[h]
\renewcommand{\thetable}{1}
\begin{center}
\begin{tabular}{lrrrr}
\toprule
\textbf{PDB ID} & \textbf{\# of atoms} & \textbf{Total charge} & \textbf{R} & \textbf{RMSD}\\
\midrule
\multirow{3}{*}{1lfg} & \multirow{3}{*}{5884}  &   0 & 0.9998 & 0.0080\\
                      &                        & -10 & 0.9999 & 0.0064\\
                      &                        &   8 & 0.9997 & 0.0094\\
\midrule
\multirow{3}{*}{35tu} & \multirow{3}{*}{13934} &   0 & 0.9999 & 0.0039\\
                      &                        & -10 & 1.0000 & 0.0034\\
                      &                        &   8 & 0.9999 & 0.0045\\
\midrule
\multirow{3}{*}{1tye} & \multirow{3}{*}{21013} &   0 & 1.0000 & 0.0033\\
                      &                        & -10 & 1.0000 & 0.0030\\
                      &                        &   8 & 0.9999 & 0.0037\\
\midrule
\multirow{3}{*}{1aoc} & \multirow{3}{*}{28708} &   0 & 1.0000 & 0.0021\\
                      &                        & -10 & 1.0000 & 0.0019\\
                      &                        &   8 & 1.0000 & 0.0024\\
\bottomrule
\end{tabular}
\caption{Comparison of Full EEM vs. EEM Cover method for computing partial atomic charges. Parameters EX-NPA\_6-31Gd\_gas \cite{Ionescu2013} were used in the computation.}
\end{center}
\end{table}

\chapter*{Notes on the implementation}
\addcontentsline{toc}{chapter}{Notes on the implementation}

Following section presents some notes and discusses differences in implementation of the some methods compared to the description used in original publication.

\section*{GDAC}
Only a subset of the parameters is used.

\section*{QEq}
The scheme is not iterative, expression for $J_{i,j}$ is taken from \cite{Louwen1998} as in \hyperref[sec:methods_smpqeq]{SMP/QEq}:

\begin{equation}
\label{eq:qeq_louwen}
J_{i, j} = \left(\ddfrac{1}{(2\sqrt{B_iB_j})^3} + R_{i,j}^3\right)^{-1/3}
\end{equation}

\section*{EQeq and EQeq+C}
Only the non-periodic case without non-zero charge centers is supported.

\section*{SQE, SQE+q0, SQE+qp}
The off-diagonal term in the hardness matrix has the form following form taken from \cite{Verstraelen2013}:

\begin{gather}
J_{i, j} = \frac{\mathrm{erf}{\left(R_{i, j}{({2w_i^2 + 2w_j^2})^{-1/2}}\right)}}{R_{i, j}}
\end{gather}

\chapter*{Applications of charges}
\addcontentsline{toc}{chapter}{Applications of charges}

Partial atomic charges, obtained by empirical charge calculation methods, can be applied for example in the following fields:

\begin{itemize}
\item descriptors for QSAR and QSPR modelling \cite{Svobodova2011, Varekova2013, Geidl2015, Dixon1993, Zhang2006, Gross2002, Ghafourian2000, Dudek2006, Karelson1996}
\item pharmacophore design \cite{Todeschini2008, Galvez1994, Stalke2011}
\item virtual screening \cite{Mannhold2006, Macdougall2007, Clement2000}
\item molecular docking \cite{Park2006, Nebgen2018, Rimac2017}
\item similarity searches \cite{Kearsley1996, Nikolova2003, Holliday2003}
\item conformers generation \cite{Vainio2007}
\item molecular dynamics \cite{Rappe1991, Chenoweth2008, Nejad2018, Lee2018}
\item study of mechanisms of chemical actions \cite{Ionescu2012, Rimac2017, Wheeler2019}
\end{itemize}

\printbibliography
\end{document}


\documentclass[bigger]{beamer}

\input{header-beam} % change to header-handout for handouts

% ====================
\title[Lecture 18]{Logic I F13 Lecture 18}
\date{November 19, 2013}
% ====================

\include{header}

\setlength{\fitchprfwidth}{14em}

\section{Review}

\subsec{Rules for $\forall$}{

\fitchctx{
\nline[$k.$]{\forall x\, A(x)}\\
\fpline{\quad A(b)}[\lalle{$k$}]
}

\bigskip

\fitchctx{
\boxedsubproof[$m$.]{c}{}{\ellipsesline \\ \nline[$n.$]{A(c)}}
\fpline{\quad \forall x\,A(x)}[\lalli{$m$--$n$}]}

\bit
\item c is special: c must not appear anywhere outside the subproof
\eit
}

\subsec{Rules for $\forall$}{
\setlength{\fitchctxwidth}{13em}

\fitchctx{
\boxedsubproof[$m$.]{c}{A(c)}{\ellipsesline \\ \nline[$n.$]{B(c)}}
\fpline{\quad \forall x(A(x) \to B(x))}[\lalli{$m$--$n$}]}

\bit
\item c is special: c must not appear anywhere outside the subproof
\eit
}

\subsec{Rules for $\exists$}{

\fitchctx{\pline[$m.$]{A(b)}\\
\fpline{\quad \exists x\, A(x)}[\lexii{$m$}]}

\bigskip

\fitchctx{
\pline[$k$.]{\exists x\, A(x)}\\
\boxedsubproof[$m$.]{c}{A(c)}{\ellipsesline \\ \nline[$n.$]{B}}
\fpline{\quad B}[\lexie{k}{$m$--$n$}]}
\bit
\item c is special: c must not appear anywhere outside the subproof
\eit
}

\subsec{Everybody Loves my Baby}{

\setlength{\fitchprfwidth}{13em}

\href{http://www.youtube.com/watch?v=cxS0FpTNir0}{Red Nichols (1935)}\\
\href{http://www.youtube.com/watch?v=TyD9rZ2bOq4}{Doris Day (1955)}\\
\href{http://www.youtube.com/watch?v=Y4wWpeGaNBw}{Brigitte Bardot (1968)}

\pause
\bigskip

\fitchprf{\tline{Everybody loves my baby}\\
\tline{My baby loves noone but me}}{
\tline{My baby is me}}

\bigskip

\fitchprf{\pline{\forall x\, Loves(x, b)}\\
\pline{\forall x(Loves(b, x) \lif x = i)}}{
\pline{b = i}}
}

\section{Strategies for Proofs with Quantifiers}

\setlength{\fitchprfwidth}{8em}

\subsec{Strategy for Proving Universal Sentences}{

\begin{tabular}{lp{10em}}
\fitchprf{\pline[1.]{}}{
\ellipsesline\\
\pline[5.]{\forall x\, A(x)}[?]
} &
Suppose we want to prove $\sf\forall x\, A(x)$.  For this, we have to use $\forall$Intro.
\end{tabular}
}

\subsec{Strategy for Proving Universal Sentences}{

\begin{tabular}{lp{10em}}
\fitchprf{\pline[1.]{}}{
\boxedsubproof[2.]{c}{}{
\ellipsesline\\
\pline[4.]{A(c)}
}
\pline[5.]{\forall x\, A(x)}[\lalli{2--4}]
} &
For $\forall$Intro, we need a subproof that ends with a substitution instance of
$\sf \forall x\, A(x)$ where we replace every free occurrence of x in
A(x) by a \emph{new} constant c. c has to be boxed in the assumption
line.  This always works, so do it earlier rather than later.
\end{tabular}
}

\subsec{Strategy for Proving Universal Sentences}{

\setlength{\fitchprfwidth}{10em}

\begin{tabular}{@{}l@{}p{10em}@{}}
\fitchprf{\pline[1.]{}}{
\boxedsubproof[2.]{c}{A(c)}{
\ellipsesline\\
\pline[4.]{B(c)}
}
\pline[5.]{\forall x(A(x) \to B(x))}[\lalli{2--4}]
} &
If the wff after the universal quantifier is a conditional, we can use the special form of $\forall$Intro instead of using a $\to$Intro inside the boxed subproof.
\end{tabular}
}

\setlength{\fitchprfwidth}{8em}

\subsec{Strategy for Proving Existential Sentences}{

\begin{tabular}{lp{10em}}
\fitchprf{\pline[1.]{}}{
\ellipsesline\\
\pline[3.]{\exists x\, A(x)}[?]
} &
We want to prove $\sf \exists x\, A(x)$.  For this, we can often use
$\exists$Intro.
\end{tabular}
}

\subsec{Strategy for Proving Existential Sentences}{

\begin{tabular}{lp{10em}}
\fitchprf{\pline[1.]{}}{
\ellipsesline\\
\pline[2.]{A(b)}\\
\pline[3.]{\exists x\, A(x)}[\lexii{2}]
} &
For $\exists$Intro, we need a substitution instance A(b) of A(x).
Any b will do. But save this strategy for last, just like $\lor$Intro. Often you can only  prove A(b) if
you're in a subproof, and b is a boxed constant in a surrounding subproof.
\end{tabular}
}

\subsec{Strategy for Proving Existential Sentences}{

\begin{tabular}{lp{10em}}
\fitchprf{\pline[1.]{}}{ \ellipsesline\\ \pline[2.]{A(b,
    b)}\\ \pline[3.]{\exists x\, A(x,
    b)}[\lexii{2}]\\ \pline[4.]{\exists x\, A(b,
    x)}[\lexii{2}]\\ \pline[5.]{\exists y\exists x\, A(y,
    x)}[\lexii{4}]\\
\pline[6.]{\exists x\, A(x, x)}[\lexii{2}]} & If b occurs more than once in your sentence,
you don't have to replace all occurrences of b by x.
\end{tabular}
}
\subsec{Strategy for Using Existential Premises}{

\begin{tabular}{lp{10em}}
\fitchprf{\pline[1.]{\exists x\, A(x)}}{
\ellipsesline\\
\pline[4.]{B}[?]
} &
Suppose you want to prove some sentence B, and you are ready to use
$\sf\exists x\, A(x)$. ($\sf\exists x\, A(x)$ might be something you've
proved, or is a premise, or an assumption of a subproof).
\end{tabular}
}

\subsec{Strategy for Using Existential Premises}{

\begin{tabular}{lp{10em}}
\fitchprf{\pline[1.]{\exists x\, A(x)}}{
\boxedsubproof[2.]{c}{A(c)}{
\ellipsesline\\
\pline[3.]{B}
}
\pline[4.]{B}[\lexie{1}{2--3}]
} &
To get B from $\sf\exists x\, A(x)$, set up a subproof where you assume
A(c). c has to be \emph{new} (in particular, it can't be in B), and boxed.
In the subproof, look for a proof of B.  This always works, so do it earlier rather than later.
\end{tabular}
}

\subsec{Strategy for Using Universal Premises}{

\begin{tabular}{lp{10em}}
\fitchprf{\pline[1.]{\forall x\, A(x)}}{
\ellipsesline} &
To use a universal sentence which you've proved, assumed, or is one of
your premises, use $\forall$Elim.
\end{tabular}
}

\subsec{Strategy for Using Universal Premises}{

\begin{tabular}{lp{10em}}
\fitchprf{\pline[1.]{\forall x\, A(x)}}{
\ellipsesline \\
\pline[$7$.]{A(b)}[\lalle{1}]
} &
To do that, you can write down any substitution instance of A(x), i.e.,
A(b) where b is any constant. You can always do that, but it's best to wait until you know which A(b) you need.
\end{tabular}
}

\subsec{Strategy for Using Universal Premises}{

\begin{tabular}{lp{10em}}
\fitchprf{\pline[1.]{\forall x\forall y\forall z\, A(x, y, z)}}{
\ellipsesline \\
\pline[$7$.]{A(a, b, c)}[\lalle{1}]
}&
\raggedright If there is more than one $\forall$ (they have to be
together ``in a block''), you can replace all variables at once. Here
we replaced x by a, y by b, and z by c.
\end{tabular}
}

\subsec{Strategy for Using Universal Premises}{

\begin{tabular}{lp{10em}}
\fitchprf{\pline[1.]{\forall x\forall y\forall z\, A(x, y, z)}}{
\ellipsesline \\
\pline[$7$.]{A(c, a, c)}[\lalle{1}]
} &
You don't have to keep the alphabetical order, and the constants don't
all have to be distinct.  E.g., here line 7 comes from line 1 by replacing
x by c, y by a, and z by c.
\end{tabular}
}

\subsec{Tips}{

\bit
\item Use the strategies for $\forall$Intro and $\exists$Elim as early as possible, and those for $\forall$Elim and $\exists$Intro as late as possible
\item \emph{When you have a choice between a rule with a subproof and one without, pick the one with the subproof.} (Except indirect proof for a sentence without $\lnot$!)
\item If you're thinking of looking for a proof of A(b) to get $\sf\exists x\, A(x)$ using $\exists$Intro, you can test if A(b) is a consequence of the premises and assumptions using \textbf{FO~Con}
\item If you type ``$\sf: x > b$'', click on $\sf\forall x\,A(x)$, select $\forall$Elim, and check the step, Fitch will fill in A(b) in the line
\item If you type ``$\sf: b > x$'', click on A(b), select $\exists$Intro, and check the step, Fitch will fill in $\sf\exists x\,A(x)$ in the line
\eit
}

\subsec{Examples}{

We'll now apply these strategies (and some strategies we remember from
propositional proofs) to give proofs of
\begin{align*}\sf
\sf\lnot \forall x\, A(x) & \sf\leftrightarrow \exists x\, \lnot A(x)\\
\sf\exists x (A(x) \to B) & \sf\leftrightarrow (\forall x\, A(x) \to B)\\
\end{align*}
\fitchprf{\pline{\forall x\exists y\, R(x, y)}\\
\pline{\forall x\forall y(R(x, y) \to R(y, x)}}{
\pline{\forall x\, R(x, x)}}

}
\end{document}


% Created 2022-01-04 Tue 01:03
% Intended LaTeX compiler: pdflatex
\documentclass[presentation]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{awesomebox}
\usepackage{booktabs}
\usepackage{placeins}
\usepackage{siunitx}
\usepackage{minted}
\usetheme[progressbar=frametitle]{metropolis}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{spot}
\usetikzlibrary{calc,patterns,decorations.pathmorphing,decorations.markings}
\usepgfplotslibrary{groupplots}
\newcommand{\gv}[1]{\ensuremath{\mbox{\boldmath$ #1 $}}}
\newcommand{\bv}[1]{\ensuremath{\mathbf{#1}}}
\newcommand{\norm}[1]{\left\lVert#1\right\rVert}
\newcommand{\abs}[1]{\left\lvert#1\right\rvert}
\newcommand{\bigqm}[1][1]{\text{\larger[#1]{\text{?}}}}
\newcommand{\order}[1]{\mathcal O \left( #1 \right)} % order of magnitude
\definecolor{scarlet}{rgb}{1.0, 0.13, 0.0}
\definecolor{shamrockgreen}{rgb}{0.0, 0.62, 0.38}
\definecolor{royalblue}{rgb}{0.25, 0.41, 0.88}
\definecolor{metropolisorange}{RGB}{235,129,27}
\definecolor{metropolisblue}{RGB}{35,55,59}
\usetheme{default}
\author{\emph{Tejaswin Parthasarathy}, Mattia Gazzola}
\date{\today}
\title{Elastica : Timesteppers}
\subtitle{ME447: Comp. Design \& Dyn. of Soft Syst}
\hypersetup{
 pdfauthor={\emph{Tejaswin Parthasarathy}, Mattia Gazzola},
 pdftitle={Elastica : Timesteppers},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 28.0.50 (Org mode 9.4.6)},
 pdflang={English}}
\begin{document}

\maketitle
\tikzset{>=latex}
\section{Time-marching algorithms}
\label{sec:org3fd3f69}
\begin{frame}[label={sec:orgc24348f}]{Motivation}
\begin{example}[Solve the following ODE]
\[ \frac{dx}{dt} = 2x \quad x(0) = 1 \]
\end{example}
\begin{block}{We can!}
\[ x(t) = e^{2t}\]
\end{block}
\end{frame}

\begin{frame}[label={sec:orgb6c16ac}]{Motivation}
\begin{example}[Solve the following ODE]
\[ \frac{dx}{dt} = \sin(\cos x^{\frac{4}{3}}) + 4\sin^2(t) \quad x(0) = 1 \]
\end{example}

\begin{block}<2->{We can!}
Use a time-marching algorithm that can solve the above equation, albeit numerically
\end{block}
\end{frame}
\begin{frame}[label={sec:org79f0b8c}]{Introduction}
\begin{itemize}
\item As seen in the last lecture, all our (temporal/spatial) rate of frame change
vectors (that effect rotations) are precisely of this form
\end{itemize}

More generally, we can solve problems of the form
\[ \frac{\partial \mathbf{u}}{\partial t} = \mathbf{F}(\mathbf{u}, t) \]
which is a partial differential equation (PDE), wherein \(\mathbf{F}\) is
any arbitrary function.
\end{frame}
\begin{frame}[label={sec:org38ebce7}]{Examples}
\begin{itemize}
\item Population dynamics (Lotka-Volterra)
\[ \begin{aligned}
	  y'_1 &= y_1 (\alpha_1 - \beta_1 y_2) \quad \text{(prey)} \\
	  y'_2 &= y_2 (-\alpha_2 + \beta_2 y_1) \quad \text{(predator)}
	  \end{aligned} \]
\item Chemical reactions (stiff)
\item Newton's equations of motion (our focus)
\[  m\ddot{\gv{x}} = F(\gv{x},t) \]
\end{itemize}
\note{:B\_note:
\begin{itemize}
\item Need not be time variable
\end{itemize}}
\end{frame}
\begin{frame}[label={sec:orgd98aec1}]{Introduction}
\begin{itemize}
\item We investigate three different (classes of) time-marching algorithms for
autonomous problems (?!):
\begin{itemize}
\item Euler's method (or Euler forward/backward)
\item Runge-Kutta-4/RK4 (multi-stage methods)
\item Position Verlet (symplectic, area preserving integrators)
\end{itemize}
\item We develop time marching methods that compute approximations to \(u(t)\)
at specfic time points, \(t^0, t^1, \cdots, t^n\).
\begin{itemize}
\item We only consider a uniform timestep size \(dt  \rightarrow t^n = n \cdot
       dt\).
\end{itemize}
\item Finally, we \emph{compare} these methods based on general and problem-specific properties\ldots{}
\end{itemize}
\end{frame}
\begin{frame}[label={sec:orgb10ecc6}]{Problem statement}
\begin{itemize}
\item Need function \(\gv{u} : [0, T] \to \mathbb{R}^n\) so that
\begin{itemize}
\item \(\gv{u}^{(k)}(t) = \gv{f}(t, \gv{u}, \gv{u}', \cdots , \gv{u}^{(k-1)})\) (\alert{explicit})
\end{itemize}
or
\begin{itemize}
\item \(\gv{f}(t, \gv{u}, \gv{u}', \cdots , \gv{u}^{(k-1)}, \gv{u}^{(k)}) = \gv{0}\) (\alert{implicit})
\end{itemize}
where we find a solution to a \(k\)-th order ordinary differential
equation. Typically \(k = 1,2\)
\item Meaningful only when accompanied by \(k\) initial conditions
\end{itemize}
\end{frame}
\begin{frame}[label={sec:orgfa4ae8a}]{Some properties of ODEs}
\begin{itemize}
\item \alert{Autonomous} ODE?
\begin{itemize}
\item \(\gv{f}\) does not explicitly depend on time \(t\)
\item An ODE can be made autonomous by introducing an extra variable (More on
this later)
\end{itemize}
\item \alert{Linear} ODE?
\begin{itemize}
\item \(\gv{f}(\gv{u}, t) =  \bv{A}(t)\gv{u} + \underbrace{\gv{b}}_{\text{forcing}}\)
\end{itemize}
\item \alert{Linear, homogenous} ODE?
\begin{itemize}
\item \(\gv{f}(\gv{u}, t) =  \bv{A}(t)\gv{u}\)
\end{itemize}
\item \alert{Constant-coefficient} ODE?
\begin{itemize}
\item \(\gv{f}(\gv{u}, t) =  \bv{A}\gv{u}\)
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}[label={sec:org2600374}]{Numerical methods: Euler's forward method}
\begin{columns}
\begin{column}{0.5\columnwidth}
\begin{itemize}
\item Simplest timestepping scheme
\item First-order approximation at time \(t_0\)
\begin{itemize}
\item Geometrical description
\item Taylor series expansion
\end{itemize}
\end{itemize}
\end{column}
\begin{column}{0.4\columnwidth}
\begin{center}
	\begin{tikzpicture}[
	declare function={func(\x)=sin(deg(pi*\x));},
	declare function={funcder(\x)=pi*cos(deg(pi*\x));}]
	\begin{axis}%
		[grid=none,
		axis x line=bottom,
		axis y line=left,
		domain=0.41:0.57,
		xmin=0.38,
		xmax=0.6,
		ymin=0.95,
		ymax=1.01,
		xlabel={$t$},
		ylabel={$u(t)$},
		ticks=none,
		height=1.12\textwidth,
		enlargelimits=false,
		]
		\addplot[smooth, very thick,
		color=metropolisorange]{func(x)};
		% For each in pgfplots is painful, see
		% https://tex.stackexchange.com/q/264168
		\addplot[thick, color=metropolisblue, mark=*] coordinates
		{ (0.41, {func(0.41)}) ({0.45}, {func(0.41) + funcder(0.41)*0.04}) };
		\foreach \a in {0.45, 0.49,..., 0.57}
        {\edef
		\temp{
		% \noexpand\addplot coordinates { (\x,0.96) (\x,1.02)};
		\noexpand\addplot[thick, color=metropolisblue, mark=*] coordinates
		{ (\a, {func(\a - 0.04) + funcder(\a - 0.04)*0.04}) ({\a + 0.04}, {func(\a) + funcder(\a)*0.04}) };
		}\temp
		}

		%\pgfplotsinvokeforeach {0.41, 0.45,..., 0.53}{
		% \addplot{\a*x^2};
		% \addplot coordinates { (#1, {func(#1)}) ({#1 + 0.04}, {func(#1) + funcder(#1)*0.04}) };
		% \addplot coordinates { (#1, #1) (#1 + 0.04, #1 + 0.04) };
		% \draw (axis cs:#1, func(#1)) -- (axis cs:{#1 + 0.04}, {func(#1) + funcder(#1)*0.04});
		% }
	\end{axis}
	\end{tikzpicture}
\end{center}
\end{column}
\end{columns}
\[ u(t_{0}+dt)=u(t_{0})+dt \cdot u'(t_{0})+{\frac {1}{2}}dt^{2} \cdot u''(t_{0})+O(dt^{3}). \]
\begin{itemize}
\item First order because local slope approximation is \(\order{dt}\)
\end{itemize}
\end{frame}

\begin{frame}[label={sec:org3b4075b}]{General explicit time stepping schemes}
\begin{itemize}
\item Explicit schemes approximate the next iterate \(t^{n+1}\) using:
\end{itemize}
\[ \gv{u}^{n+1} = \sum_{i=0}^{k} \alpha_i \gv{u}^{n-i} + \sum_{j=0}^{r} \beta_j \frac{\partial \gv{u}^{n-j}}{\partial t} \]
  which for \(k=1\) and \(r=0\) looks something along these lines:
\[ \gv{u}^{n+1} = \alpha_0 \gv{u}^{n} + \alpha_1 \gv{u}^{n-1} + \beta_0 \frac{\partial \gv{u}^{n}}{\partial t} \]
\begin{itemize}
\item Derivation of schemes other than Euler method follow a similar line of reasoning, while
details vary\footnote{By a \alert{lot}}
\end{itemize}
\end{frame}
\begin{frame}[label={sec:org4bc84f4}]{Some more time stepping schemes}
With \(\dot{x} = f(x)\),
\begin{block}{Euler forward}
\[ x^{n+1} = x^{n} + f(x^{n}) \cdot dt \]
\end{block}
\begin{block}{Euler backward}
\[ x^{n+1} = x^{n} + f(x^{n+1}) \cdot dt \]
\end{block}
\begin{columns}
\begin{column}{0.5\columnwidth}
\begin{block}{Midpoint method}
\begin{equation*}
\begin{aligned}
x^{*}&= x^{n} + f({x}^{n}) \cdot \frac{dt}{2} \\
x^{n+1} &= x^{n} + f({x}^{*}) \cdot dt \\
\end{aligned}
\end{equation*}
\end{block}
\end{column}
\begin{column}{0.4\columnwidth}
\begin{center}
	\begin{tikzpicture}[
	declare function={func(\x)=sin(deg(pi*\x));},
	declare function={funcder(\x)=pi*cos(deg(pi*\x));}]
	\begin{axis}%
		[grid=none,
		axis x line=bottom,
		axis y line=none,
		domain=0.43:0.49,
		xmin=0.41,
		xmax=0.51,
		ymin=0.975,
		ymax=1.005,
		ticks=none,
		height=1.1\textwidth,
		enlargelimits=false,
		clip=false]
		\addplot[smooth, very thick,
		color=metropolisorange]{func(x)} node[pos=0.1, above, anchor=south east]
		{{\scriptsize$x(t)$}};

		% Draw derivatve from yn to yn+1 first
		% 0.44 to 0.48
		\addplot[color=metropolisblue, mark=*] coordinates
		{ (0.44, {func(0.44)}) ({0.48},
		{func(0.44) + 0.04*funcder(0.46)-0.004}) }
		node[pos=0, below right, anchor=west]{{\scriptsize $y^n$}}
		node[below right, anchor=north west]{{\scriptsize $y^{n+1}$}}
		node[right, anchor=south west]{Estd.};

		% Draw actual derivatve line
		\addplot[color=royalblue, thick] coordinates
		{ (0.44, {func(0.46) - 0.02*funcder(0.46) }) (0.48,
		{func(0.46) + 0.02*funcder(0.46)}) } node[above]{Actual};

		% Draw connections to ground now
		\addplot[dashed] coordinates
		{ (0.44, {func(0.44)}) (0.44,\pgfkeysvalueof{/pgfplots/ymin})}
		node [above right, anchor=south west]{{\scriptsize$t^{n}$}};

		\addplot[dashed] coordinates
		{ (0.46, {func(0.46)}) (0.46,\pgfkeysvalueof{/pgfplots/ymin})}
		node [below, anchor=north]{{\scriptsize$t^{n} + \frac{dt}{2}$}};

		\addplot[dashed] coordinates
		{ (0.48, {func(0.48)}) (0.48,\pgfkeysvalueof{/pgfplots/ymin})}
		node [above right, anchor=south west]{{\scriptsize$t^{n+1}$}};

	\end{axis}
	\end{tikzpicture}
\end{center}
\end{column}
\end{columns}
\end{frame}
\begin{frame}[label={sec:org3bf8753}]{Some more time stepping schemes}
\begin{block}{Runge Kutta-4}
\begin{equation*}
\begin{aligned}
{k}_1 &= {f}({x}^{n}) \cdot dt \\
{k}_2 &= {f}({x}^{n} + 0.5 \cdot {k}_1)\cdot dt \\
{k}_3 &= {f}({x}^{n} + 0.5 \cdot {k}_2)\cdot dt \\
{k}_4 &= {f}({x}^{n} + {k}_3)\cdot dt \\
{x}^{n+1} &= {x}^{n} + \frac{{k}_1+2{k}_2+2{k}_3+{k}_4}{6}
\end{aligned}
\end{equation*}
\end{block}
\begin{block}{Position/Velocity Verlet}
\begin{itemize}
\item Later on we introduce these two schemes in the context of integrating
second order ODEs
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[label={sec:orgb1d9e79}]{Function evaluations}
\begin{itemize}
\item Our first attempt at comparing schemes is the number of functional
evaluations for one time step\ldots{}
\item Why? \(f(x)\) can be expensive to evaluate (e.g. calculating the effect
of the energy diffusion on millions to billions of
grid points in an astrophysical simulation)
\end{itemize}
\begin{table}[htbp]
\caption{\label{tab_sym_snake_params}Number of function evaluations for schemes}
\centering
\begin{tabular}{lr}
\toprule
Scheme & \(n[f(x)]\)\\
\midrule
Euler fwd & 1\\
Euler bwd & Solve!\\
Midpoint & 2\\
RK4 & 4\\
Verlet* & 1\\
\bottomrule
\end{tabular}
\end{table}
\end{frame}
\begin{frame}[label={sec:orge2303a5}]{Convergence/ Consistency}
Suppose the numerical solution at some time \(t\) is \(\gv{u}\), and the
exact solution at the same time is \(\tilde{\gv{u}}\).
\begin{definition}[Convergence]
A numerical method is said to be convergent if the numerical solution
approaches the exact solution as the step size \(dt\) goes to 0.
\end{definition}
The methods we just looked at are all convergent.
\begin{definition}[Consistency]
A numerical method is said to be consistent if the error, \(e_{dt}:=\lVert\tilde{\gv{u}}-\gv{u} \rVert\) is such that
\[ \lim_{dt \to 0} \frac{e_{dt}}{dt} = 0\]
\end{definition}
\end{frame}
\begin{frame}[label={sec:org2500b2a}]{Order of convergence}
\begin{definition}[Order of accuracy]
The numerical solution \(\gv{u}\) is said to be \(p^{\text{th}}\)-order
accurate if the error, \(e(dt):=\lVert\tilde{\gv{u}}-\gv{u} \rVert\)
is proportional to the step-size \(dt\), to the \(p^{\text{th}}\) power. That
is
\[ e(dt)=\lVert\tilde{\gv{u}}-\gv{u} \rVert\leq C(dt)^{p} \]
where the constant \(C\) is independent of \(dt\) and usually depends on
the solution \(\gv{u}\)
\end{definition}
 In the big O notation an \(p^{\text{th}}\)-order accurate numerical method
 is notated as
\[ \lVert\tilde{\gv{u}}-\gv{u} \rVert = \order{h^p}\]
\end{frame}
\begin{frame}[label={sec:org984fd76}]{Order of convergence : Importance}
\begin{columns}
\begin{column}{0.32\columnwidth}
\begin{block}{First order}
\begin{center}
\begin{tabular}{ll}
\toprule
\(dt\) & \(e(dt)\)\\
\midrule
\(10^{-1}\) & \(1\)\\
\(10^{-2}\) & \(10^{-1}\)\\
\(10^{-3}\) & \(10^{-2}\)\\
\(10^{-4}\) & \(10^{-3}\)\\
\(10^{-5}\) & \(\spot<2>{10^{-4}}\)\\
\bottomrule
\end{tabular}
\end{center}
\end{block}
\end{column}

\begin{column}{0.32\columnwidth}
\begin{block}{Second order}
\begin{center}
\begin{tabular}{ll}
\toprule
\(dt\) & \(e(dt)\)\\
\midrule
\(10^{-1}\) & \(1\)\\
\(10^{-2}\) & \(10^{-2}\)\\
\(10^{-3}\) & \(10^{-4}\)\\
\(10^{-4}\) & \(10^{-6}\)\\
\(10^{-5}\) & \(\spot<2>{10^{-8}}\)\\
\bottomrule
\end{tabular}
\end{center}
\end{block}
\end{column}
\begin{column}{0.32\columnwidth}
\begin{block}{Fourth order}
\begin{center}
\begin{tabular}{ll}
\toprule
\(dt\) & \(e(dt)\)\\
\midrule
\(10^{-1}\) & \(1\)\\
\(10^{-2}\) & \(10^{-4}\)\\
\(10^{-3}\) & \(10^{-8}\)\\
\(10^{-4}\) & \(10^{-12}\)\\
\(10^{-5}\) & \(\spot<2>{10^{-16}}\)\\
\bottomrule
\end{tabular}
\end{center}
\end{block}
\end{column}
\end{columns}
\begin{alertblock}<2->{Better returns for same timestep!}
\end{alertblock}
\end{frame}

\begin{frame}[label={sec:org9ae791f}]{Order of convergence : Implementation}
\begin{block}{Model problem definition}
Let's solve this problem, and test out methods for convergence:
\[ \frac{dy}{dt} = -y \quad,\quad  y(0) = 1 \]
which as we know has the analytical solution \(\tilde{y}(t) = e^{-t}\) \(\rightarrow\)
error known at every \(dt\)

Notice:
\begin{itemize}
\item We choose a simple problem to understand performance/convergence
\begin{itemize}
\item More complicated problems usually follow suit
\end{itemize}
\item We are solving an eigenvalue problem, just like the last lecture (rotations)
\end{itemize}

\alert{ACTIVITY}
\end{block}
\end{frame}
\begin{frame}[label={sec:org2bd73fe}]{Order of accuracy : Results}
% Need to run the notebook first
\begin{center}
	\begin{tikzpicture}
		\begin{loglogaxis}[
			enlargelimits=true,
			grid=both,
			grid style={line width=.1, draw=gray!20},
			major grid style={line width=.2,draw=gray!50},
			xlabel=$dt$,
			ylabel=$e(dt)$,
			height=1.0\textheight
			]
			% Euler backward/forward
			\addplot [mark=*, mark size=2.5, scarlet, very thick]
			table {code/data/euler_bwd_ooa.txt} node [midway, above left, anchor=south east]
			{Euler bwd/fwd};
			\addplot [metropolisblue, dashed, thick]
			table {code/data/euler_bwd_ooa_slopes.txt};

			% Midpoitn
			\addplot [mark=*, mark size=2.5, royalblue, very thick]
			table {code/data/midpoint_method_ooa.txt} node [pos=0.8, below , anchor=north west]
			{Midpoint};
			\addplot [metropolisblue, dashed, thick]
			table {code/data/midpoint_method_ooa_slopes.txt};

			% RK4
			\addplot [mark=*, mark size=2.5, metropolisorange, very thick]
			table {code/data/rk4_ooa.txt} node [midway, below right, anchor=north west]
			{RK4};
			\addplot [metropolisblue, dashed, thick]
			table {code/data/rk4_ooa_slopes.txt};

		\end{loglogaxis}
	\end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}[label={sec:orga06aaa8}]{Order of accuracy : Results}
\begin{table}[htbp]
\caption{\label{tab_sym_snake_params}Order of accuracy for different schemes}
\centering
\begin{tabular}{lr}
\toprule
Scheme & \(p\)\\
\midrule
Euler & 1\\
Midpoint & 2\\
RK4 & 4\\
Verlet* & ?\\
\bottomrule
\end{tabular}
\end{table}
\end{frame}
\begin{frame}[label={sec:org70a597c}]{Bottomline}
\begin{itemize}
\item Order of accuracy is a measure of ``goodness'' of algorithm
\item Higher-order accurate algorithms commit less error for a given \(h\).
\item But they are costly (in terms number of function evaluations, and
ultimately number of operations performed)
\end{itemize}
\end{frame}
\begin{frame}[label={sec:org492de25}]{Higher order ODEs}
We consider a harmonic oscillator, i.e. a linear spring-mass system:
\[ \ddot{x} + \omega^2 x = 0\]
\begin{columns}
\begin{column}{0.6\columnwidth}
\begin{itemize}
\item \(\omega^2 = \frac{k}{m} \equiv 1\)
\item Consider \(x(0) = 1, \dot{x}(0) = 0\) (need two ICs)
\item Solution is analytically known to be
\end{itemize}
\[ x(t) = \cos(t) \quad \dot{x}(t) = -\sin(t) \]
\begin{itemize}
\item Notice : solution is bounded
\end{itemize}
\end{column}
\begin{column}{0.4\columnwidth}
\tikzset{boxstyle/.style={draw,outer sep=0pt,thick}}
% https://tex.stackexchange.com/a/13952
\begin{center}
	\begin{tikzpicture}[scale=1]
	\tikzstyle{spring}=[thick,decorate,color=metropolisblue, decoration={zigzag,pre length=4,post length=4,segment length=10}]
	\tikzstyle{ground}=[fill,pattern=north east lines,draw=none,minimum width=8,minimum height=3]

	\node (M) [boxstyle, minimum width=50, minimum height=50, color=metropolisblue] {$m$};

	\node (ground) [ground,anchor=north,yshift=-10,minimum width=80] at (M.south) {};
	\draw (ground.north east) -- (ground.north west);
	\draw [thick] (M.south west) ++ (8pt,-5pt) circle (5pt) (M.south east) ++ (-8pt,-5pt) circle(5pt);
	% \draw [thick] (M.south west) ++ (8,-5) circle[radius=5pt];
	% \draw [thick]

	\node (wall) [ground, rotate=-90, minimum width=80, xshift=-10, yshift=-80] {};
	\draw (wall.north east) -- (wall.north west);

	% Syntax ($(A)!(C)!(B)$), specifies the projection of (C) on the line
	% from (A) to (B),
	% <name>.<number> syntax https://tex.stackexchange.com/a/426804
	\draw [spring] (M.180) -- ($(wall.north east)!(M.180)!(wall.north west)$)
	node [midway, above]{$kx$};

	% Draw x position beginning and end
	% Interesection of north east ground with horizontal wall and 10 pt line
	\draw [thick, color=metropolisblue] ($(wall.south west)!(ground.north west)!(wall.north west)$) --
	++ (0pt,-5pt) coordinate (c) -- ++(0pt,-5pt);
	\draw [thick, color=metropolisblue, ->] (c) -- ($(M.south west)!(c)!(M.north west)$)
	node[right]{$x$};

	\end{tikzpicture}
\end{center}
\end{column}
\end{columns}
\end{frame}

\begin{frame}[label={sec:org5b1d4e0}]{Conservation laws in typical higher order ODEs}
The system is \alert{Hamiltonian} as the energy is conserved:
\[ \underbrace{H(x(t), \dot{x}(t))}_{\text{Hamiltonian/total energy}} = \underbrace{x^2(t)}_{\text{Potential energy}} +
   \underbrace{\dot{x}^2(t)}_{\text{Kinetic energy}} \equiv 1\]

The solution can be represented in the time domain (left) or in the phase
portrait (right)
\begin{columns}
\begin{column}{0.5\columnwidth}
\begin{center}
	\begin{tikzpicture}
	\begin{axis}%
		[grid=both,
		minor tick num=4,
		grid style={line width=.1pt, draw=gray!10},
		major grid style={line width=.2pt,draw=gray!50},
		axis lines=middle,
		domain=0:7,
		x label style={at={(axis description cs:1.1,0.5)},anchor=east},
		y label style={at={(axis description cs:-0.1,.5)},rotate=90,anchor=south},
		xlabel={$t$},
		ylabel={$u(t)$},
		width=1.1\textwidth,
		title style={at={(0.5,0)},anchor=north,yshift=-0.1},
		title={Time domain},
		enlargelimits=true,
		% enlargelimits={abs=0.2}
		]
		\addplot[samples=200,smooth,metropolisorange,very thick]{cos(deg(x))}
		node[pos=0.1, right]{{\small $x(t)$}};
		\addplot[samples=200,smooth,metropolisblue,very thick]{-sin(deg(x))}
		node[pos=0.99, below left, anchor=north east]{{\small $\dot{x}(t)$}};
	\end{axis}
	\end{tikzpicture}
\end{center}
\end{column}
\begin{column}{0.5\columnwidth}
\begin{center}
	\begin{tikzpicture}
	\begin{axis}%
		[grid=both,
		minor tick num=4,
		grid style={line width=.1pt, draw=gray!10},
		major grid style={line width=.2pt,draw=gray!50},
		axis lines=middle,
		width=1.1\textwidth,
		xmin=-1.1, xmax=1.1, ymin=-1.1, ymax=1.1,
		axis equal,
		x label style={at={(axis description cs:0.98,0.5)},anchor=south east},
		y label style={at={(axis description cs:0.5,0.98)},anchor=west},
		xlabel={\textcolor{metropolisorange}{$x$}},
		ylabel={\textcolor{metropolisblue}{$\dot{x}$}},
		title style={at={(0.5,-0.2)},anchor=south},
		% title style={at={(0.5,0.0)},anchor=north},
		title={Phase portrait},
		enlargelimits=true,
		disabledatascaling
		]
		\draw[very thick, black] (axis cs: 0, 0) circle [radius=1];
		\draw[thick, black, ->] (axis cs:0, 0)--(axis cs:0.707106, 0.707106);
		\draw [thick,->] (0.4,0) arc (0:45:0.4) node [midway, right]{{\scriptsize $t$}};
	\end{axis}
	\end{tikzpicture}
\end{center}
\end{column}
\end{columns}
\end{frame}

\begin{frame}[label={sec:org8359662}]{Conversion to lower order ODE}
Let's convert the second order ODE to two first order ODEs, by considering \(y = \dot{x} \Rightarrow\)
\[ \begin{pmatrix} \dot{x} \\ \dot{y} \end{pmatrix} = \begin{bmatrix} 0 & 1\\-1
   & 0 \end{bmatrix} \begin{pmatrix} {x} \\ {y} \end{pmatrix} \]

\begin{itemize}
\item You can reuse the same schemes!
\item Alternatively, we can develop schemes for the second order equation
directly : \alert{Example using Taylor series}
\end{itemize}

\begin{block}{Position Verlet scheme}
\begin{itemize}
\item For integrating equations similar to \(\ddot{\gv{x}} = \gv{f}(\gv{x})\),
with \(\gv{y} = \dot{\gv{x}}\)
\end{itemize}
\begin{equation*}
\begin{aligned}
\gv{x}^* &= \gv{x}^n + 0.5\cdot dt \cdot \gv{y}^n \\
\gv{y}^{n+1} &= \gv{y}^n + dt \cdot \gv{f}\left( \gv{x}^*\right) \\
\gv{x}^{n+1} &= \gv{x}^* + 0.5\cdot dt \cdot \gv{y}^{n+1}
\end{aligned}
\end{equation*}
\end{block}
\note{:B\_note:
\begin{itemize}
\item Show derivation using
\end{itemize}
\[ f(x+h) = f(x) + f'(x)h + f''(x)h^2/2! + \cdots \quad
	f(x - h) = f(x) - f'(x) h + f''(x)h^2/2! + \cdots \]
and add them up}
\end{frame}
\begin{frame}[label={sec:org61a1641}]{More schemes}
Another example of a scheme for this model equation is
\begin{block}{Velocity Verlet algorithm}
\begin{equation*}
\begin{aligned}
\gv{y}^* &= \gv{y}^n + 0.5\cdot dt \cdot \gv{f}\left( \gv{x}^n\right) \\
\gv{x}^{n+1} &= \gv{x}^{n} + dt \cdot \gv{y}^{*} \\
\gv{y}^{n+1} &= \gv{y}^* + 0.5\cdot dt \cdot \gv{f}\left( \gv{x}^{n+1}\right) \\
\end{aligned}
\end{equation*}
\end{block}
\begin{block}{How do these schemes fare?}
\begin{itemize}
\item Position and Velocity Verlet have \(p = 2\) (second-order accurate) for
both position \(\gv{x}\) and velocity \(\gv{y}\)
\item But Position Verlet has only \alert{one} function evaluation!
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[label={sec:org4f35c6d}]{Energy-preserving/symplectic schemes}
\begin{block}{Why do we even care about these schemes?}
\begin{itemize}
\item Clearly, RK4 has higher order of convergence and it must be better?
\end{itemize}
\end{block}
\begin{block}{Answer}
\begin{itemize}
\item Position and Velocity Verlet schemes are symplectic (area-preserving) schemes
\item They preserve in the phase-portrait of a Hamiltonian system \(\Rightarrow\)
They always conserve energy by design!
\end{itemize}

\alert{ACTIVITY}
\end{block}
\end{frame}
\begin{frame}[label={sec:orga5817e9}]{Energy-preserving/symplectic schemes}
\begin{itemize}
\item The harmonic equation arises from Newton's fundamental laws of motion,
without dissipative forces \(\Rightarrow\) energy needs to be conserved
\item RK4 slowly dissipates energy \(\Rightarrow\) unphysical!
\item Euler forward increases energy without bounds, even when physics dictates
bounded solutions (counter examples?)
\item Verlet schemes conserve energy even for large \(dt\)
\begin{itemize}
\item There are still errors (it still has \(p = 2\) ) in the \emph{phase}
\item \alert{DEMO}
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}[label={sec:org0a9e1a4}]{But why is it energy preserving?\footnote{Peter Young, \href{https://young.physics.ucsc.edu/242/leapfrog.pdf}{Course Notes:Physics 115/242}}}
\begin{itemize}
\item Because it preserves the area in the \(x-p\) phase space (thus the area-preserving
property seen earlier)
\item Why area preservation? \(\Rightarrow\) \alert{Liouville's theorem}
\item On one application of the time-stepping scheme (map), an initial rectangle
with side lengths \(dx , dp\) gets stretched to a parallelogram with
sides \(dx^\prime, dp^\prime\)
\end{itemize}

\begin{columns}
\begin{column}{0.6\columnwidth}
New area \(dA^\prime = \det \bv{J} dA\), where \(\bv{J}\) is
\[ \bv{J} =	\begin{bmatrix}
	\frac{\partial x^\prime}{\partial x} & \frac{\partial x^\prime}{\partial p}  \\
	\frac{\partial p^\prime}{\partial x} & \frac{\partial p^\prime}{\partial p}  \\
	\end{bmatrix} \]

In the case of symplectic schemes, we require \(\det \bv{J} = 1\)
\end{column}
\begin{column}{0.4\columnwidth}
\begin{figure}[htbp]
\centering
\includegraphics[width=1.0\textwidth]{images/area_preserve.png}
\caption{Area preservation}
\end{figure}
\end{column}
\end{columns}
\end{frame}
\begin{frame}[label={sec:org18a66f7}]{Area preserviation of Verlet and Euler algorithms}
From a different perspective,
\(\begin{pmatrix} \delta x_1 \\ \delta v_1 \end{pmatrix} = \bv{J}\begin{pmatrix} \delta x_0
   \\ \delta v_0 \end{pmatrix}\)
where subscripts denote number of applications of the time-stepping scheme.
Break it down to
\(\bv{J} = \bv{C}\bv{B}\bv{A}\).
\begin{columns}
\begin{column}{0.5\columnwidth}
\begin{alertblock}{Euler forward}
\begin{equation*}
\begin{aligned}
\bv{A} &= \begin{bmatrix} 1 & h\\ hF^\prime(x_0) & 1 \end{bmatrix}\\
\bv{B} &= \begin{bmatrix} 1 & 0\\ 0 & 1 \end{bmatrix} \\
\bv{C} &= \begin{bmatrix} 1 & 0\\ 0 & 1 \end{bmatrix}
\end{aligned}
\end{equation*}
\end{alertblock}
\end{column}
\begin{column}{0.5\columnwidth}
\begin{alertblock}{Position verlet}
\begin{equation*}
\begin{aligned}
\bv{A} &= \begin{bmatrix} 1 & \tfrac{h}{2}\\ 0 & 1 \end{bmatrix}\\
\bv{B} &= \begin{bmatrix} 1 & 0 \\ hF^\prime(x_{1/2}) & 1 \end{bmatrix}\\
\bv{C} &= \begin{bmatrix} 1 & \tfrac{h}{2}\\ 0 & 1 \end{bmatrix}\\
\end{aligned}
\end{equation*}
\end{alertblock}
\end{column}
\end{columns}
Calculate \(\det \bv{J}\)\ldots{}
\end{frame}
\begin{frame}[label={sec:orga4bc767}]{Bottomline}
\begin{itemize}
\item The evolution of dynamics of the soft filament also relies on some form of energy
conservation (translational/rotational/bending/twist/shear/stretch) as the
governing equations are Newton's laws
\item We need symplectic algorithms for maintaining relevance to the physical world
\end{itemize}

\alert{Counterpoint} In reality, there is always dissipation (frictional forces,
 viscous forces, drag forces not included in either of the above, etc.)
\end{frame}
\begin{frame}[label={sec:org907601a}]{Why did forward Euler blow-up?}
\begin{itemize}
\item Because it was unstable\ldots{}related to the stability of a method (alternatively instability)
\end{itemize}
\begin{block}{Euler forward algorithm}
\begin{itemize}
\item Find out what happens to the numerical solution using forward Euler when applied to
\(\dot{y}(t) = \lambda y(t)\)
\item Why? Eigenvalue problem, easy to extend analysis to general matrices
\end{itemize}
\begin{equation*}
\begin{aligned} y_k & = y_{k-1} + h \lambda y_{k-1} \\ &= (1 + h
\lambda)y_{k-1} \\ &= (1 + h \lambda)^{k}y_{0}
\end{aligned}
\end{equation*}
\begin{itemize}
\item So stability \(\Leftrightarrow\) \(\abs{1 + h \lambda} \leq 1\)
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[label={sec:org91666ec}]{Why did forward Euler blow-up?}
\begin{itemize}
\item \(\abs{1 + h \lambda}\) is the \alert{amplification factor}
\item The condition on the amplification factor implies the existence of a
\alert{stability region} in the complex plane
\item \alert{DEMO}
\end{itemize}
% This file was created by matplotlib2tikz v0.7.4.
\begin{center}
\begin{tikzpicture}[scale=0.45]
	\begin{groupplot}[group style={group size=3 by 1}]
	\nextgroupplot[
	title={Euler forward},
	grid=both,
	grid style={line width=.1pt, draw=gray!10},
	major grid style={line width=.2pt,draw=gray!50},
	axis equal,
	xmin=-2.2, xmax=0.2,
	ymin=-1.2, ymax=1.2,
	minor tick num=4,
	xlabel={\(\displaystyle \mathrm{Re}\)},
	ylabel={\(\displaystyle \mathrm{Im}\)},
	enlargelimits=false,
	disabledatascaling
	]

	\draw[very thick, metropolisblue, fill=metropolisorange, fill opacity=0.3]
	(axis cs: -1, 0) circle [radius=1];
	% \addplot [very thick, metropolisorange, fill=metropolisorange, fill opacity=0.3]
	% table {code/data/euler_fwd_stability.txt};

	\nextgroupplot[
	title={Euler backward},
	grid=both,
	grid style={line width=.1pt, draw=gray!10},
	major grid style={line width=.2pt,draw=gray!50},
	axis equal,
	xmin=-0.2, xmax=2.2,
	ymin=-1.2, ymax=1.2,
	minor tick num=4,
	xlabel={\(\mathrm{Re}\)},
	enlargelimits=false,
	disabledatascaling
	]
	% \addplot [fill=metropolisorange, fill opacity=0.3]
	% table [row sep=\\]{
	% -0.5 -1.5\\
	% 2.5 -1.5\\
	% 2.5 1.5\\
	% -0.5 1.5\\
	% -0.5 -1.5};
	\filldraw[fill=metropolisorange, fill opacity=0.3]
	(axis cs: -1, -2) rectangle (3,2);
	\draw[very thick, metropolisblue, fill=white, fill opacity=0.6]
	(axis cs: 1, 0) circle [radius=1];
	% \addplot [thick, metropolisorange] table {code/data/euler_bwd_stability.txt};

	\nextgroupplot[
	title={Runge-Kutta--4},
	grid=both,
	grid style={line width=.1pt, draw=gray!10},
	major grid style={line width=.2pt,draw=gray!50},
	axis equal,
	xmin=-3, xmax=1,
	ymin=-3, ymax=3,
	minor tick num=4,
	xlabel={\(\mathrm{Re}\)},
	enlargelimits=false,
	disabledatascaling,
	]
	\addplot [thick, metropolisblue, fill=metropolisorange,
	fill opacity=0.3] table {code/data/rk4_stability.txt};
	\end{groupplot}
\end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}[label={sec:org31e4784}]{What about backward Euler?}
\begin{block}{Backward Euler algorithm}
\begin{itemize}
\item Find out what happens to the numerical solution using backward Euler when applied to
\(\dot{y}(t) = \lambda y(t)\)
\end{itemize}
\begin{equation*}
\begin{aligned} y_k & = y_{k-1} + h \lambda y_{k} \\
y_k (1 - h \lambda) &= y_{k-1} \\
y_k &= \frac{1}{(1 - h \lambda)}y_{k-1} \\
y_k &= \left( \frac{1}{1 - h \lambda} \right)^k y_{0}
\end{aligned}
\end{equation*}
\begin{itemize}
\item So stability \(\Leftrightarrow\) \(\abs{1 - h \lambda} \geq 1\)
\item Backward Euler can be stable even when the ODE is not!
\item \alert{DEMO}
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[label={sec:orgc276ff8}]{What about Verlet ?}
\begin{block}{Can it blow up?}
\end{block}
\begin{block}<2->{It can!}
\begin{itemize}
\item But only for non-hamiltonian systems
\item For hamiltonian systems, it always conserves a positive semi-definite
quantity and hence should not blow up
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[label={sec:org70d82df}]{Bottomline}
\begin{itemize}
\item Stability is another measure of how ``good'' a time-marching algorithm is
\item For \alert{explicit} schemes, main concern in time-step selection is usually
\alert{stability} (but also accuracy)
\item For \alert{implicit} schemes, \alert{accuracy} determines the time-step selection
\end{itemize}
\end{frame}
\begin{frame}[label={sec:org58363b5}]{Stiff ODEs}
\begin{block}{What are stiff problems?}
\begin{itemize}
\item Hard to define exactly
\item Usually when there are \alert{multiple time scales} in our problem
\end{itemize}
\end{block}
\begin{alertblock}{DEMO}
\begin{itemize}
\item In the above demo, stiffness results from the presence of a fast decay
component, but slow evolution of the total solution (slow--fast time scale)
\item In the case of a stable ODE system \(\dot{\gv{y}}(t) = \bv{J}_{f}(\gv{y}(t))\)
stiffness can arise if \(\bv{J}_f\) has eigenvalues of very different
magnitude (what is this called again?)
\end{itemize}
\end{alertblock}
\end{frame}
\begin{frame}[label={sec:org0e9270d}]{Stiff ODEs}
\begin{itemize}
\item Why not just \emph{small} or \emph{large} magnitude?
\begin{itemize}
\item Because discrepancy in time scales is the problem
\item If all time scales are similar, then we can deal with that one time
scale (non-dimensionalization of the problem helps)
\item If there are many, then some (usually fast ones) may be considered uninteresting.
\end{itemize}
\end{itemize}
\begin{block}{Explicit methods}
\begin{itemize}
\item What was the problem in applying explicit methods to stiff problems?
\begin{itemize}
\item Fastest time scale governs timestep \(\Rightarrow\) small timesteps
\(\Rightarrow\) inefficient.
\end{itemize}
\item \alert{Accuracy} (in terms of capturing the slow timescale) could be achieved
with large timesteps
\item \alert{Stability} demands a small time step
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[label={sec:org7de5eb0}]{Stiff ODEs}
\begin{block}{Implicit methods}
\begin{itemize}
\item Large time steps?
\begin{itemize}
\item Definitely.
\end{itemize}
\item \alert{Stability} is not a problem
\item \alert{Accuracy} suffers.
\end{itemize}
\end{block}
\begin{block}{So even here we have an issue.}
\end{block}
\begin{block}{Bottomline}
Stiff problems are hard to tackle (there are still ingenious ways to
partially offset the cost of solving a stiff problem)
\end{block}
\end{frame}

\begin{frame}[label={sec:org7b18dab}]{Summary}
\begin{table}[htbp]
\caption{\label{tab_sym_snake_params}Properties of different explicit schemes}
\centering
\begin{tabular}{lrrl}
\toprule
Scheme & \(p\) & \(n[f(x)]\) & Energy preserving?\\
\midrule
Euler & 1 & 1 & No\\
Midpoint & 2 & 2 & No\\
RK4 & 4 & 4 & No\\
Verlet & 2 & 1 & Yes\\
\bottomrule
\end{tabular}
\end{table}
\end{frame}
\begin{frame}[label={sec:org2e22f4e}]{Credits}
\begin{block}{A good chunk of the material in these slides are taken from Prof. Andreas Kloeckner's CS450 lectures}
\end{block}
\end{frame}
\end{document}