\section{Laplacian Support Vector Machines}

\subsection{Motivation}

\begin{frame}
  \frametitle{Motivation}

  \structure{Example: two class ``clock'' data set}

  \begin{figure}
    \centering

    \subfloat[Large set of unlabeled samples (black squares) and only one labeled sample per class (blue circle, green diamond)]{
      \resizebox{.28\linewidth}{!}{
        \input{\texfigdir/clock1.pstex_t}
      }
    }
    \quad
    \subfloat[Result of a maximum margin supervised classification]{
      \resizebox{.28\linewidth}{!}{
        \input{\texfigdir/clock2.pstex_t}
      }
    }
    \quad
    \subfloat[Result of a semi-supervised classification with intrinsic norm from manifold regularization]{
      \resizebox{.28\linewidth}{!}{
        \input{\texfigdir/clock3.pstex_t}
      }
    }
  \end{figure}
\end{frame}

\subsection{Learning from Labeled and Unlabeled Data}

\begin{frame}
  \frametitle{Learning from labeled and unlabeled data}

  \structure{Training data: $\mathcal{S} = \mathcal{L} \cup \mathcal{U}$}

  \begin{itemize}
    \item labeled data: $\mathcal{L} = \{(\vec{x}_i, y_i), \quad i=1,\ldots,l\}$
    \item unlabeled data: $\mathcal{U} = \{\vec{x}_i, \quad i = \underbrace{l+1,\ldots,m}_u\}$
  \end{itemize}
  \pspread

  \structure{Graph Laplacian $\mat{L}$ associated with $\mathcal{S}$:}

  \begin{itemize}
    \item $\mat{L} = \mat{D} - \mat{W}$
    \item adjacency matrix $\mat{W}$
    \item diagonal matrix $\mat{D}$ with the degree of each node: $d_{ii} = \sum_{j=1}^m w_{ij}$
  \end{itemize}
  \pspread

  \structure{Kernel matrix $\mat{K}$:} $k_{ij} = k(\vec{x}_i, \vec{x}_j)$
  \pspread

  \structure{Decision boundary $f(\vec{x})$:} $\vec{f} = [f(\vec{x}_i),~i=1,\ldots,m]^T$
\end{frame}

\begin{frame}
  \frametitle{Learning from labeled and unlabeled data \cont}

  \structure{Regularization framework for function learning:}

  \begin{center}
    \tikz[baseline]{
      \node[fill=bl1!100, anchor=base, rounded corners=3pt, inner xsep=3mm] (d1) {
        \color{bl3}
        $\displaystyle
          f^* = \argmin_{f \in \mathcal{H}_k}
          \sum_{i=1}^l V(\vec{x}_i, y_i, f) +
          \gamma_\text{A} \, \|f\|^2_\text{A} +
          \gamma_\text{I} \, \|f\|^2_\text{I}
        $
      };
    }
  \end{center}
  \vspace{.25cm}

  \structure{Loss function $V(\vec{x}_i, y_i, f)$}

  \begin{itemize}
    \item Squared loss function \structure{$(y_i - f(\vec{x}_i))^2$} for Regularized Least Squares (RLS)
    \item Hinge loss function \structure{$\max [0, 1-y_i f(\vec{x}_i)]$} for SVM
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Learning from labeled and unlabeled data \cont}

  \structure{Regularization framework for function learning:}

  \begin{center}
    \tikz[baseline]{
      \node[fill=bl1!100, anchor=base, rounded corners=3pt, inner xsep=3mm] (d1) {
        \color{bl3}
        $\displaystyle
          f^* = \argmin_{f \in \mathcal{H}_k}
          \sum_{i=1}^l V(\vec{x}_i, y_i, f) +
          \gamma_\text{A} \, \|f\|^2_\text{A} +
          \gamma_\text{I} \, \|f\|^2_\text{I}
        $
      };
    }
  \end{center}
  \vspace{.15cm}

  \structure{Regularization terms}

  \begin{itemize}
    \item \emph{Ambient} norm $\|\cdot\|_\text{A}$:
      \begin{itemize}
        \item norm of the function $f$ in the Reproducing Kernel Hilbert Space (RKHS)
        \item enforces a smoothness condition on the possible solutions \\[.25cm]
      \end{itemize}
    \item \emph{Intrinsic} norm $\|\cdot\|_\text{I}$:
      \begin{itemize}
        \item norm of the function $f$ in the low dimensional manifold
        \item enforces a smoothness along the sampled $\mathcal{M}$
      \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Reproducing Kernel Hilbert Spaces (RKHS)}

  \structure{Hilbert space}

  \begin{itemize}
    \item \structure{abstract vector space} with any finite or \structure{infinite} number of dimensions
    \item possesses the structure of the \structure{inner product}
    \item allows the measurement of \structure{angles} and \structure{lengths}
    \item is \structure{complete} \\[.3cm]
  \end{itemize}
  \pause

  \structure{Reproducing Kernel Hilbert Space}

  \begin{itemize}
    \item Hilbert space of functions
    \item can be defined by kernels
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Manifolds}

  A \structure{manifold} is a topological space that on a small enough scale \\ resembles
  the Euclidean space. \\[.3cm]

  \begin{figure}
    \centering
    \subfloat{
      \includegraphics[height=3.3cm]{\pngdir/globe.\png} \pause
    }
    \qquad \qquad
    \subfloat{
      \alt<4->{
  \copyrightbox[b]{
      \makebox[.4\linewidth]{
        \href{https://commons.wikimedia.org/wiki/File:Waldseem%C3%BCller-Globus.jpg
        }
        {
        \includegraphics[height=3.3cm]{\jpgdir/waldseemueller-globus.\jpg}
        }
      }
  }{Martin Waldseemüller, Public domain, via Wikimedia Commons}
      }{\alt<3>{
        \includegraphics[height=3.3cm]{\pngdir/worlmap_notflat.\png}
      }{
        \includegraphics[height=3.3cm]{\pngdir/worldmap.\png}
      }}
    }
  \end{figure}
\end{frame}

\begin{frame}
  \frametitle{Manifold Learning}

  \structure{The Swiss Roll Problem}

\begin{figure}
    \subfloat{
          \includegraphics[height=4cm]{\pngdir/swiss_roll.\png}
      }
    \hspace{2cm} \pause
    \subfloat{
    \copyrightbox[b]{
      \makebox[.35\linewidth]{
          \includegraphics[height=3.2cm]{\pngdir/swiss_roll_solution.\png}
      }
    }{Algorithm: S. Marsland, “Machine Learning: An Algorithmic Perspective”, Chapter 10, 2009.}
    }
  \end{figure}
\end{frame}

\begin{frame}
  \frametitle{Learning from labeled and unlabeled data \cont}

  \structure{Intrinsic norm $\|\cdot\|_\text{I}$:}

  \begin{displaymath}
    \|f\|_\text{I}^2 =
    \sum_{i=1}^m \sum_{j=i}^m w_{ij} \big( f(\vec{x}_i) - f(\vec{x}_j) \big)^2 =
    \vec{f}^T \mat{L} \, \vec{f}
  \end{displaymath}
\end{frame}

\begin{frame}
  \frametitle{Learning from labeled and unlabeled data \cont}

  \structure{Representer Theorem} (Kimeldorf and Wahba, 1970): \\[.3cm]

  The solution $f^*$ of this optimization problem has the form:

  \begin{displaymath}
    f^*(\vec{x}) = \sum_{i=1}^m \beta_i^* \cdot k(\vec{x}_i, \vec{x}) + \beta_0^*
  \end{displaymath}
\end{frame}

\subsection{Laplacian Support Vector Machines}

\begin{frame}
  \frametitle{Laplacian Support Vector Machines}

  \structure{Constrained primal optimization problem based on the dual form:}

  \begin{center}
    \small
    \tikz[baseline]{
      \node[fill=bl1!100, anchor=base, rounded corners=3pt, inner xsep=3mm] (d1) {
        \color{bl3}
        $\displaystyle
          \begin{aligned}
            \min\limits_{\vec{\beta} \in \real^m, \vec{\xi}\in\real^l}
            & \qquad \sum_{i=1}^l \xi_i +
              \gamma_\text{A} \cdot \vec{\beta}^T \mat{K} \vec{\beta} +
              \gamma_\text{I} \cdot \vec{\beta}^T \mat{K} \mat{L} \mat{K} \vec{\beta} \\[.3cm]
            \mbox{subject to}
            & \qquad y_i \left( \sum_{j=1}^m \vec{\beta}_j k(\vec{x}_j, \vec{x}_i) + \beta_0\right) \geq 1 - \xi_i, \quad i = 1,\ldots,l \\[.3cm]
            & \qquad \xi_i \geq 0, \quad i = 1,\ldots,l
          \end{aligned}
        $
      };
    }
  \end{center}
\end{frame}

\begin{frame}
  \frametitle{Laplacian Support Vector Machines \cont}

  \structure{Lagrange function $L$:}

  \begin{eqnarray*}
    L(\vec\beta,\beta_0,\vec\xi,\vec\lambda,\vec\nu)
    &=& \sum_{i=1}^l \xi_i +
        \frac{1}{2} \vec{\beta}^T (2 \gamma_\text{A} \mat{K} + 2 \gamma_\text{I} \mat{K}\mat{L}\mat{K}) \vec{\beta} -{} \\
    & & {}- \sum_{i=1}^l \lambda_i \left(y_i \Bigg(\sum_{j=1}^m \beta_i k(\vec{x}_i,\vec{x}_j) + \beta_0\Bigg) - 1 + \xi_i\right) -{} \\
    & & {}- \sum_{i=1}^l \nu_i \xi_i
  \end{eqnarray*}
\end{frame}

\begin{frame}
  \frametitle{Laplacian Support Vector Machines \cont}

  \structure{KKT condition:} the gradient w.\,r.\,t. the primal variables $\vec{\beta}, \beta_0, \vec{\xi}$ has to vanish
  \pause

  \begin{itemize}
    \item Partial derivative w.\,r.\,t. $\beta_0$:
      \begin{displaymath}
        \frac{\partial L}{\partial \beta_0} \stackrel{!}{=} 0  \quad \Rightarrow \quad
        \sum_{i=1}^l \lambda_i y_i = 0
      \end{displaymath}
      \pause
    \item Partial derivative w.\,r.\,t. $\xi_i$:
      \begin{displaymath}
        \frac{\partial L}{\partial \xi_i} \stackrel{!}{=} 0  \quad \Rightarrow \quad
        1 - \lambda_i - \nu_i = 0 \quad \Rightarrow \quad
        0 \leq \lambda_i \leq 1
      \end{displaymath}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Laplacian Support Vector Machines \cont}

  \structure{Simplifying the Langrangian using the two identities above:}

  {\small
    \begin{eqnarray*}
      L(\vec{\beta}, \vec{\lambda})
      &=& \frac{1}{2} \vec{\beta}^T (2 \gamma_\text{A} \mat{K} + 2 \gamma_\text{I} \mat{K}\mat{L}\mat{K}) \vec{\beta} -{} \\
      & & {}- \sum_{i=1}^l \lambda_i \left(y_i \Bigg(\sum_{j=1}^m \beta_i k(\vec{x}_i,\vec{x}_j) + \beta_0\Bigg) - 1 \right) \\
      &=& \frac{1}{2} \vec{\beta}^T (2 \gamma_\text{A} \mat{K} + 2 \gamma_\text{I} \mat{K}\mat{L}\mat{K}) \vec{\beta} -
          \vec{\beta}^T \mat{K} \mat{J}_{\mathcal{L}}^T \mat{Y} \vec{\lambda} +
          \sum_{i=1}^l \lambda_i
    \end{eqnarray*}
  }

  with $\mat{J}_{\mathcal{L}} = [\mat{I}~\mat{0}] \in \real^{l\times m}$:
  \begin{itemize}
    \item identity matrix $\mat{I} \in \real^{l \times l}$
    \item rectangular matrix $\mat{0} \in \real^{l \times u}$ with all entries being 0 \\[.25cm]
  \end{itemize}
  and diagonal matrix $\mat{Y} \in \real^{l \times l}$ composed by the $l$ class labels $y_i$
\end{frame}

\begin{frame}
  \frametitle{Laplacian Support Vector Machines \cont}

  \structure{Partial derivative w.\,r.\,t. $\vec{\beta}$:}

  \begin{eqnarray*}
    \frac{\partial L}{\vec{\beta}} \stackrel{!}{=} \vec{0}
    &\Rightarrow&
    (2 \gamma_\text{A} \mat{K} + 2 \gamma_\text{I} \mat{K}\mat{L}\mat{K}) \vec{\beta} -
    \mat{K} \mat{J}_{\mathcal{L}}^T \mat{Y} \vec{\lambda} = \vec{0} \\[.25cm]
    &\Rightarrow&
    \vec{\beta} = (2 \gamma_\text{A} \mat{I} + 2 \gamma_\text{I} \mat{K}\mat{L})^{-1}
    \mat{J}_{\mathcal{L}}^T \mat{Y} \vec{\lambda}
  \end{eqnarray*}
  \pspread

  \structure{Note:} direct relationship between parameters $\vec{\beta}$ and Lagrange multipliers $\vec{\lambda}$
\end{frame}

\begin{frame}
  \frametitle{Laplacian Support Vector Machines \cont}

  \structure{Substituting back in the Langrange expression leads to the dual problem:}

  \begin{center}
    \small
    \tikz[baseline]{
      \node[fill=bl1!100, anchor=base, rounded corners=3pt, inner xsep=3mm] (d1) {
        \color{bl3}
        $\displaystyle
          \begin{aligned}
            \max\limits_{\vec{\lambda} \in \real^l}
            & \qquad \sum_{i=1}^l \lambda_i - \frac{1}{2} \vec{\lambda}^T \mat{Q} \vec{\lambda} \\[.3cm]
            \mbox{subject to}
            & \qquad 0 \leq \lambda_i \leq 1, \quad i = 1,\ldots,l \\
            & \qquad \sum_{i=1}^l \lambda_i y_i = 0
          \end{aligned}
        $
      };
    }
  \end{center}

  where

  {\small
  \begin{displaymath}
    \mat{Q} = \mat{Y} \mat{J}_{\mathcal{L}} \mat{K}
    (2 \gamma_\text{A} \mat{I} + 2 \gamma_\text{I} \mat{K}\mat{L})^{-1}
    \mat{J}_{\mathcal{L}}^T \mat{Y}
  \end{displaymath}
  }
\end{frame}

\subsection{Lessons Learned}

\begin{frame}
  \frametitle{Lessons Learned}

  \structure{Laplacian SVM:}

  \begin{itemize}
    \item Ongoing research topic \\[.25cm]
    \item Extension of the Kernel SVM \\[.25cm]
    \item Additional regularization term \\[.25cm]
    \item Derivation of the dual problem
  \end{itemize}
\end{frame}

\input{nextTime.tex}

\subsection{Further Readings}

\begin{frame}
  \frametitle{Further Readings}

  \begin{itemize}
    \item Stefano Melacci, Mikhail Belkin: \\
      \structure{Laplacian Support Vector Machines Trained in the Primal}, \\
      Journal of Machine Learning Research, 12:1149-1184, 2011 \\[.25cm]
    \item Mikhail Belkin, Partha Niyogi, Vikas Sindhwani: \\
      \structure{Manifold Regularization: A Geometric Framework for Learning \\
      from Labeled and Unlabeled Examples}, \\
      Journal of Machine Learning Research, 7:2399-2434, 2006
  \end{itemize}
\end{frame}

% \subsection{Comprehensive Questions}
%
 %\begin{frame}
 %  \frametitle{Comprehensive Questions}
 %
 %  \begin{itemize}
 %    \item
 %  \end{itemize}
 %\end{frame}


\documentclass{beamer}

\usepackage{beamerthemevictor,comment,verbatim,graphicx,amssymb}

\input{tutmacs}
\input{slidemacs}
\input idxmacs

\begin{document}

\title{Line breaking}
\author{Victor Eijkhout}
\date{Notes for CS 594 -- Fall 2004}

\frame{\titlepage}

\section{Basic problem}

\frame[containsverbatim]{
  \frametitle{}
\begin{itemize}
\item Problem: break horizontal list into lines
\item $n$ words: $2^n$ possibilities
\item (much) better is possible
\item Aim: visually even `colour'
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{What is a paragraph?}
\begin{itemize}
\item Boxes: words, math, other solid objects
\item Glue: white space, possibly with stretch/shrink
\item Penalties: prevent or force breaks
\end{itemize}
}

\frame{
  \frametitle{Boxes}
\begin{itemize}
\item Words, formulas, actual boxes
\item described by height, width,depth
\item (hyphenation)
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Penalties}
\begin{itemize}
\item Inserted by user, macro, automatically
\item Example: \verb+\def~{\penalty10000 \ }+
\item Automatic: paragraph ends with \verb+\unskip\penalty10000\hfill+
\item also: penalties for two consecutive hyphenated lines; last full
  line hyphenated
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Glue}
\begin{itemize}
\item Denotations: \verb+\hskip 2cm plus 1cm minus .5cm+
\item Automatically inserted: \cs{leftskip}, \cs{abovedisplayskip}
\item Adding glue together: \n{2cm plus 1cm} and \n{2cm plus -1cm}
  total no stretch
\item Infinite glue: \cs{hfill} or \verb+\hskip 0pt plus 1fil+
  (\n{fill}, \n{filll})
\item infinite glue present: all other stretch/shrink ignored
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Line break locations}
\begin{itemize}
\item Foremost: at a glue, but only if preceeded by a non-discardable
\item At a hyphen or hyphenation location
\item At a penalty (this can override the above clauses)
\item (complicated rules for defining extent of a word)
\end{itemize}
}

\sectionframe{Examples}

\frame[containsverbatim]{
  \frametitle{Centered text}
\begin{examplewithcode}
\begin{minipage}{4cm}
\leftskip=0pt plus 1fil \rightskip=0pt plus 1fil
\parfillskip=0pt
This paragraph puts infinitely stretchable glue at
the left and right of each line.
The effect is that the lines will be centered.
\end{minipage}
\end{examplewithcode}
}

\frame[containsverbatim]{
  \frametitle{Centered last line}
\begin{examplewithcode}
\begin{minipage}{5cm}
\leftskip=0pt plus 1fil \rightskip=0pt plus -1fil
\parfillskip=0pt plus 2fil
This paragraph puts infinitely stretchable glue at
the left and right of each line, but the amounts cancel out.
The parfillskip on the last line changes that.
\end{minipage}
\end{examplewithcode}
}

\frame[containsverbatim]{
  \frametitle{Hanging punctuation}
\begin{itemize}
\item Put punctuation in the right margin
\item Make right margin look more straight/solid
\pgfimage{hang}
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{hanging punctuation code}
\begin{verbatim}
\newbox\pbox \newbox\cbox
\setbox\pbox\hbox{.} \wd\pbox=0pt
\setbox\cbox\hbox{,} \wd\cbox=0pt
\newdimen\csize \csize=\wd\cbox
\newdimen\psize \psize=\wd\pbox

\catcode`,=13 \catcode`.=13
\def,{\copy\cbox \penalty0 \hskip\csize\relax}
\def.{\copy\pbox \penalty0 \hskip\psize\relax}
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{Mathematical Reviews}
\begin{itemize}
\item Reviewer signature separated from review text
\item but if possible on the same line
\pgfimage{mr}
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{mathematical reviews code}
\begin{verbatim}
\def\signed#1{\unskip
  \penalty10000 \hskip 40pt plus 1fill
  \penalty0
  \hbox{}\penalty10000
                \hskip 0pt plus 1fill
  \hbox{#1}%
                \par
  }
\end{verbatim}
}

\sectionframe{\TeX's line breaking algorithm}

\subsection{Glue setting}

\frame[containsverbatim]{
  \frametitle{Glue ratio}
\begin{itemize}
\item Consider a line, and desired length
\item compare to natural width, stretch, shrink
\[ \rho=\begin{cases}
    0&\ell=L\\ (\ell-L)/X&\mbox{(stretch:) $\ell>L$ and $X>0$}\\
    (\ell-L)/Y&\mbox{(shrink:) $\ell<L$ and $Y>0$}\\
    \mathrm{undefined}&\mathrm{otherwise}
        \end{cases}
\]
\item note: negative for shrink
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Badness}
\begin{itemize}
\item Stretching/shrinking too far is `bad':
\[ b = \begin{cases}
          10\,000&\mbox{$\rho<-1$ or undefined}\\
          \min\left\{10\,000, 100|\rho|^3\right\}&\mbox{otherwise}
       \end{cases}
\]
\item Stretch beyond 100\% possible, shrink not.
\item Categories:
\begin{itemize}
\item[\textit{tight (3)}] if it has shrunk with $b\geq 13$
\item[\textit{decent (2)}] if $b\leq12$
\item[\textit{loose (1)}] if it has stretched with $100>b\geq 13$
\item[\textit{very loose (0)}] if it has stretched with $b\geq100$
\end{itemize}
Note $100\times(1/2)^3=12.5$
\item discourage `visually incompatible' lines
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Demerits}
\begin{itemize}
\item Add together badness and penalties
\item \cs{linepenalty}, \cs{doublehyphendemerits}, \cs{finalhyphendemerits}
\end{itemize}
}

\subsectionframe{Breaking strategies}

\frame[containsverbatim]{
  \frametitle{First fit}
\begin{itemize}
\item Wait until word crosses the margin, then
\item if the line can be shrunk: shrink
\item otherwise, if it can be stretched: stretch
\item otherwise, try hyphenating
\item otherwise, really stretch
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Best fit and total fit}
\begin{itemize}
\item (Best fit) Decide between stretch/shrink/hyphenate based on badness
\item (Total fit) Add all badnesses together; minimize over whole paragraph
\end{itemize}
}

\subsection{Implementation}

\frame{
  \frametitle{Program structure}
\begin{itemize}
\item Loop over all words, check if feasible breakpoint
\item<2-> Inner loop: over all previous words, check if feasible start
  of line
\item<3-> Optimization: cutoff on previous words
\item<4-> `active list'
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Main program}
\footnotesize
\begin{verbatim}
active = [0]; nwords = len(paragraph)
for w in range(1,nwords):
    # compute the cost of breaking after word w
    print "Recent word",w
    for a in active:
        line = paragraph[a:w+1]
        if w==nwords-1:
            ratio = 0 # last line will be set perfect
        else:
            ratio = compute_ratio(line)
            print "..line=",line,"; ratio=",ratio
        if ratio<-1:
            active.remove(a)
            print "active point",a,"removed"
        else:
            update_cost(a,w,ratio)
    report_cost(w)
    active.append(w)
    print
\end{verbatim}
}

\subsection{First fit, best fit}
\frame[containsverbatim]{
  \frametitle{Data structure}
\begin{verbatim}
def init_costs():
    global cost
    cost = len(paragraph)*[0]
    for i in range(len(paragraph)):
        cost[i] = {'cost':0, 'from':0}
    cost[0] = {'cost':10000, 'from':-1}
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{Cost function; first fit}
\begin{verbatim}
def update_cost(a,w,ratio):
    global cost
    if a>0 and cost[a-1]['cost']<10000:
        if ratio<=1 and ratio>=-1:
            to_here = abs(ratio)
        else: to_here = 10000
        if cost[w]['cost']==0 or to_here<cost[w]['cost']:
            cost[w]['cost'] = to_here
            cost[w]['from'] = a-1
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{First fit, dynamic version}
\begin{verbatim}
def update_cost(a,w,ratio):
    global cost
    if ratio<=1 and ratio>=-1:
        to_here = abs(ratio)
    else: to_here = 10000
    if a>0:
        from_there = cost[a-1]['cost']
        to_here = to_here+from_there
    else: from_there = 0
    if cost[w]['cost']==0 or to_here<cost[w]['cost']:
        cost[w]['cost'] = to_here
        cost[w]['from'] = a-1
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{example}
\begin{footnotesize}
\begin{verbatim}
You may  never  have  thought  of  it,  but  fonts  (better:   -0.111111111111
typefaces) usually have a mathematical  definition  somehow.   -0.666666666667
If   a   font   is   given   as   bitmap,   this   is  often   0.888888888889
a  result  originating  from  a  more  compact  description.   0.0
Imagine the situation that you have bitmaps at  300dpi,  and   -0.777777777778
you   buy   a  600dpi  printer.  It  wouldn't  look  pretty.   0.25
There   is   then   a   need   for  a  mathematical  way  of   0.555555555556
describing  arbitrary  shapes.  These  shapes  can  also  be   0.0
three-dimensional; in fact,  a~lot  of  the  mathematics  in   -0.285714285714
this  chapter  was  developed  by  a  car  manufacturer  for   0.0
modeling   car   body  shapes.  But  let  us  for  now  only   0.222222222222
look   in  two  dimensions,  which  means  that  the  curves   0.125
are  lines,  rather  than  planes.
\end{verbatim}
\end{footnotesize}
}

\frame[containsverbatim]{
  \frametitle{Best fit cost function}
\begin{verbatim}
def update_cost(a,w,ratio):
    global cost
    to_here = 100*abs(ratio)**2
    if a>0:
        from_there = cost[a-1]['cost']
        to_here = to_here+from_there
    else: from_there = 0
    if cost[w]['cost']==0 or to_here<cost[w]['cost']:
        cost[w]['cost'] = to_here; cost[w]['from'] = a-1
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{example}
\begin{footnotesize}
\begin{verbatim}
You may  never  have  thought  of  it,  but  fonts  (better:   -0.111111111111
typefaces) usually have a mathematical  definition  somehow.   -0.666666666667
If   a   font   is   given   as  bitmap,  this  is  often  a   0.5
result   originating   from   a  more  compact  description.   0.5
Imagine the situation that you have bitmaps at  300dpi,  and   -0.777777777778
you   buy   a  600dpi  printer.  It  wouldn't  look  pretty.   0.25
There   is   then   a   need   for  a  mathematical  way  of   0.555555555556
describing  arbitrary  shapes.  These  shapes  can  also  be   0.0
three-dimensional; in fact,  a~lot  of  the  mathematics  in   -0.285714285714
this  chapter  was  developed  by  a  car  manufacturer  for   0.0
modeling   car   body  shapes.  But  let  us  for  now  only   0.222222222222
look   in  two  dimensions,  which  means  that  the  curves   0.125
are  lines,  rather  than  planes.
\end{verbatim}
\end{footnotesize}
}

\subsection{Total fit}
\frame[containsverbatim]{
  \frametitle{Total fit, data structure}
\begin{verbatim}
def init_costs():
    global cost
    nul = [0,0,0]
    cost = len(paragraph)*[ 0 ]
    for i in range(len(paragraph)):
        cost[i] = nul[:]
        for j in range(3):
            cost[i][j] = {'cost':10000, 'from':-2}
    for j in range(3):
        cost[0][j] = {'cost':10000, 'from':-1}
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{cost function}
\begin{verbatim}
def update_cost(a,w,ratio):
    global cost
    type = stretch_type(ratio)
    to_here = 100*abs(ratio)**2
    if a>0:
        [from_there,from_type] = minimum_cost_and_type(a-1)
        to_here += from_there
    else: from_there = 0
    if cost[w][type]['cost']==0 or\
       to_here<cost[w][type]['cost']:
        cost[w][type]['cost'] = to_here;
        cost[w][type]['from'] = a-1
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{example}
\begin{footnotesize}
\begin{verbatim}
You may  never  have  thought  of  it,  but  fonts  (better:   -0.111111111111
typefaces)   usually   have   a   mathematical   definition    1.2
somehow. If a font is given  as  bitmap,  this  is  often  a   -0.454545454545
result   originating   from   a  more  compact  description.   0.5
Imagine   the   situation   that   you   have   bitmaps   at   1.0
300dpi, and you buy  a  600dpi  printer.  It  wouldn't  look   -0.333333333333
pretty. There is then a  need  for  a  mathematical  way  of   -0.4
describing  arbitrary  shapes.  These  shapes  can  also  be   0.0
three-dimensional; in fact,  a~lot  of  the  mathematics  in   -0.285714285714
this  chapter  was  developed  by  a  car  manufacturer  for   0.0
modeling   car   body  shapes.  But  let  us  for  now  only   0.222222222222
look   in  two  dimensions,  which  means  that  the  curves   0.125
are  lines,  rather  than  planes.
\end{verbatim}
\end{footnotesize}
}

\end{document}


\section{Prior Work and Motivation}

\begin{frame}{What is an Impact Model?}
    \begin{itemize}
        \item Predicts behavior of an object after contact with another object (e.g. Cassie's Foot with Ground) \\
    \end{itemize}
    \newline
    \tikzstyle{startstop} = [rectangle, rounded corners, minimum width=3.5cm, minimum height=1.75cm, text centered, text width = 4cm, draw=black]
    \tikzstyle{arrow} = [ultra thick,->,>=stealth]
    \begin{tikzpicture}[node distance=2cm]

        \node (in) [startstop, label=above:{\textbf{Input}}, xshift = 2cm, yshift = 10cm] { $v^-$ \\(pre-impact velocity, position, and orientation) \\ };
        \node (out) [startstop, label=above:{\textbf{Output}}, xshift = 8cm, yshift = 10 cm] {$v^+$ \\(post-impact velocity, position, and orientation)};
        \node(props)  [startstop, label=above:{\textbf{System Properties}}, width=6cm, xshift = 5cm, yshift = 7.5cm] { $\mu$ (frictional coefficient)\\ $\epsilon$ (coefficient of restitution)};
        \draw [arrow] (in) -- (out) node[midway,above] {\textit{Model}};

    \end{tikzpicture}

\end{frame}

\begin{frame}
\frametitle{Prior Work (Dr.\ Nima Fazeli)}

\begin{itemize}
    \item Paper's Goal - Compare and evaluate impact models \\
    \begin{itemize}
    \item Tuned system properties to enhance the performance of the models
    \end{itemize}
    \item Data - Planar impacts from 3D printed ellipses \\
\end{itemize}

\begin{figure}[h!]
    \centering
    \begin{subfigure}[b]{0.5\linewidth}
        \includegraphics[scale=0.4]{figures/nimaFigSetup.jpg}
        \caption{Experimental Setup\cite{nima1}}
        \label{fig:cIRB}
    \end{subfigure}
    \quad
    \begin{subfigure}[b]{0.4\linewidth}
       \includegraphics[scale=0.5]{figures/nimaFigDrop.jpg}
        \caption{Dropping Ellipse\cite{nima1}\cite{nima2}}
        \label{fig:nimaDrop}
    \end{subfigure}
\end{figure}

\end{frame}

\setlength{\belowcaptionskip}{-10pt}
\begin{frame}{Prior Work  (Dr.\ Nima Fazeli)}
    Key Takeaways \\
    \begin{itemize}
        \item None of the predictive models are very good
        \item Even post-hoc models like IRB have significant error
    \end{itemize}
 \begin{figure}
        \centering
        \includegraphics[scale=0.35]{figures/nimaFigModels.jpg}
        \caption{frequency of Errors Across Different impact Models\cite{nima2}}
        \label{fig:nimaModels}
\end{figure}
\end{frame}

\subsection{Research Goals}
\begin{frame}{Research Goals}
\begin{itemize}
    \item Understand a few impact models
    \item Experiment with interesting data sets and compare to work done by Dr. Fazeli
    \item Look for issues and shortcomings from models
    \item Test different ways to improve models
\end{itemize}
\end{frame}


\subsection{Ill conditioning}

Skips over actual max if not "smooth" enough.


\section{The Type and Effect System}

\begin{frame}
\frametitle{The Type and Effect System}
\framesubtitle{Type Environments}
\begin{align}
    \Gamma; \Delta_1 \vdash e :: T \Delta_2
\end{align}
where
\begin{itemize}
    \item $\Gamma$ represents the type assumptions (is the type environment)
    \item $e$ is an expression of type $T$
    \item $\Delta_1, \Delta_2$ the lock status before and after the execution of expression $e$
\end{itemize}
\begin{align}
    \tag{Type Environment}
    \Gamma = \{x_1:T_1, x_2:T_2, ..., x_n:T_n\}
\end{align}
\end{frame}

\begin{frame}
\frametitle{The Type and Effect System}
\framesubtitle{Lock Environments}
\begin{align}
    \tag{Lock Environment}
    \Delta = \{a_1:n_1, a_2:n_2, ..., a_k:n_k\}
\end{align}
where
\begin{itemize}
    \item \textbf{$a_i$} represents a variable $x$ or a lock reference $r$
    \item \textbf{$n_i$} represents the lock's status which can be 0 for free locks or $n \ge 0$ which represents the number of times the lock is taken by the thread
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{The Type and Effect System}
\framesubtitle{Notation}
\begin{align}
    \tag{Lock Environment Look-up}
    \Delta(a_i) = n_i
\end{align}
\begin{align}
    \tag{Type Environment Look-up}
    \Gamma(x_i) = T_i
\end{align}
\begin{align}
    \tag{Extending Lock Environment}
    \Delta, a_{k+1}:n_{k+1}
\end{align}
\begin{align}
    \tag{Extending Type Environment}
    \Gamma, x_{k+1}:T_{k+1}
\end{align}
\end{frame}

\begin{frame}
\frametitle{The Type and Effect System}
\framesubtitle{Notation}
\begin{align}
    \tag{Domain of Lock Environment}
    dom(\Delta) = \{a_1, a_2, ..., a_k\}
\end{align}
\begin{align}
    \tag{Domain of Type Environment}
    dom(\Gamma) = \{x_1, x_2, ..., x_n\}
\end{align}
\begin{align}
    \sigma; \Gamma; \Delta_1 \vdash e :: T \Delta_2 ~\text{where} ~\sigma~ \text{is the heap}
\end{align}
\end{frame}

\begin{frame}
\frametitle{The Type and Effect System}
\framesubtitle{Restriction}
The system disallows assignment to lock fields; imposes a single assignment policy for lock fields, which is assured in that we allow such assignments by the class constructors, only.
\end{frame}

\begin{frame}
\frametitle{The Type and Effect System}
\framesubtitle{Sum of Lock Environments}
\begin{definition}
Given two lock environments $\Delta_1, \Delta_2$, the sum of $\Delta_1 + \Delta_2 = \Delta$
\end{definition}
where
\begin{align}
    \Delta \vdash l:n_1+n_2 ~\text{if}~
    \Delta_1 \vdash l:n_1,
    \Delta_2 \vdash l:n_2
\end{align}
\begin{align}
    \Delta \vdash l:n ~\text{if}~
    \Delta_1 \nvdash l,
    \Delta_2 \vdash l:n
\end{align}
\begin{align}
    \Delta \vdash l:n ~\text{if}~
    \Delta_1 \vdash l:n,
    \Delta_2 \nvdash l
\end{align}

\begin{align*}
    \Delta_1 \ge \Delta_2 ~\text{iff}~ dom(\Delta_1) \supseteq dom(\Delta_2) \wedge \forall l \in dom(\Delta_2), n_1 \ge n_2, \\
    ~\text{where}~ \Delta_1 \vdash l:n_1,
    \Delta_2 \vdash l:n_2
\end{align*}
\end{frame}

\begin{frame}
\frametitle{The Type and Effect System}
\framesubtitle{Difference of Lock Environments}

\begin{prooftree}
\AxiomC{$\Delta_1 \vdash l:n_1$}
\AxiomC{$\Delta_2 \vdash l:n_2$}
\BinaryInfC{$\Delta_1-\Delta_2 \vdash l:n_1-n_2 $}
\end{prooftree}

\begin{prooftree}
\AxiomC{$\Delta + l$}
\AxiomC{$l \in dom(\Delta)$}
\AxiomC{$\Delta(l) = n$}
\TrinaryInfC{$\Delta \vdash l:n + 1 $}
\end{prooftree}

\begin{prooftree}
\AxiomC{$\Delta - l$}
\AxiomC{$l \in dom(\Delta)$}
\AxiomC{$\Delta(l) = n$}
\TrinaryInfC{$\Delta \vdash l:n - 1 $}
\end{prooftree}

\end{frame}

\begin{frame}
\frametitle{The Type and Effect System}
\framesubtitle{Aliases Problem}

The lock environments express resources (the current lock balance) and if $x$ and $y$ happen to be aliases, the resources must be combined.

\begin{align}
    x, y ~\text{not aliases}~ \rightarrow
    \Delta' = \Delta [l1/x] [l2/y] = \{l1:1, l2:1\}
\end{align}
\begin{align}
    x, y ~\text{aliases}~ \rightarrow
    \Delta' = \Delta [l/x] [l/y] = \{l:(1+1)\}
\end{align}
\end{frame}

\begin{frame}
\frametitle{The Type and Effect System}
\framesubtitle{Substitution}

\begin{definition}
Given a lock environment $\Delta$ the \textbf{substitution} of a variable x by a lock reference l in $\Delta$ is written as $\Delta[l/x]$. Let $\Delta' = \Delta [l/x]$
\end{definition}
\begin{prooftree}
\AxiomC{$\Delta' = \Delta'', l:n_l, x: n_x$}
\RightLabel{\textsc{COMP}}
\UnaryInfC{$\Delta' = \Delta'', l:(n_l + n_x)$}
\end{prooftree}

\begin{prooftree}
\AxiomC{$\Delta' = \Delta'', x:n$}
\AxiomC{$l \not \in dom(\Delta'')$}
\RightLabel{\textsc{REPL}}
\BinaryInfC{$\Delta' = \Delta'', l:n$}
\end{prooftree}
\end{frame}

\begin{frame}

\frametitle{The Type and Effect System}
\framesubtitle{Well-Formed Heaps}

$\sigma \vdash ok$ means the heap is well-formed, if no binding occurs more than once, and all references mentioned in the instance states are allocated in $\sigma$.

\end{frame}

\begin{frame}
\frametitle{The Type and Effect System}
\framesubtitle{Projection}

\begin{definition}
Assume a heap $\sigma$ with $\sigma \vdash ok$ and a thread $p$. The \textbf{projection} of $\sigma$ onto $p$, written $\sigma \downarrow_p$ is defined as:
\end{definition}
\begin{align*}
\bullet \downarrow_p & = \bullet \\
(\sigma, l:0) & = \sigma \downarrow_p l:0 \\
(\sigma, l:p(n))  & = \sigma \downarrow_p l:n \\
(\sigma, l:p'(n))  & = \sigma \downarrow_p l:0,  ~\text{if}~ p'\not = p \\
(\sigma, 0:C(\vec{v}))  & = \sigma \downarrow_p
\end{align*}
\end{frame}

\begin{frame}
\frametitle{The Type and Effect System}
\framesubtitle{Local Thread}
\begin{figure}[h]
    \includegraphics[width=10cm]{figures/local_system.png}
    \label{fig::local::system}
\end{figure}
\end{frame}

\begin{frame}
\frametitle{The Type and Effect System}
\framesubtitle{Global}
\begin{figure}[h]
    \includegraphics[width=10cm]{figures/global_system.png}
    \label{fig::global::system}
\end{figure}
\end{frame}

Methodologies Software Processes/Assignments/Safe Locking for Multi-Threaded Java Presentation/slides/system.tex


% Sample University of Calgary Thesis
% This file contains CHAPTER TWO

\chapter{Important Results}

\section{The first result}

\begin{thm}[Residue Theorem]
Let $f$ be analytic in the region $G$ except for the isolated
singularities $a_1,a_2,\dots,a_m$. If $\gamma$ is a closed
rectifiable curve in $G$ which does not pass through any of the
points $a_k$ and if $\gamma\approx 0$ in $G$, then
\[
  \frac{1}{2\pi i}\int_\gamma\! f = \sum_{k=1}^m
  n(\gamma;a_k)\mathop{\mathrm{Res}}(f;a_k)\,.
\]
\end{thm}

\blindtext\footnote{\blindtext}

\begin{figure}
  \[ \circ \to \circ \]
  \caption{Circles and arrows}
\end{figure}

\section{The second result}

\subsection{The first half}

\blindtext\pagenote{\blindtext}

\subsection{The second half}

\blindtext


\section{Appendix 1: Sloan actuators structure}

\begin{figure}[h]
\begin{center}
	\begin{subfigure}{0.22\textwidth}
	\includegraphics[width=\textwidth]{circles/initialisation}
	\caption{Initialisation.}
	\label{fig:appendix:sloan_structure:initialization}
	\end{subfigure}
	\begin{subfigure}{0.54\textwidth}
	\includegraphics[width=\textwidth]{circles/layer_1}
	\caption{First actuator's layer.}
	\label{fig:appendix:sloan_structure:layer_1}
	\end{subfigure}

	\begin{subfigure}{0.70\textwidth}
	\includegraphics[width=\textwidth]{circles/layer_2}
	\caption{Second actuator's layer.}
	\label{fig:appendix:sloan_structure:layer_2}
	\end{subfigure}
	\caption{Actuators structure explanation: the initialisation creates an actuator at position (0, 0), layer initialisation then creates an actuator on the right, and each addition function creates an actuator close to the last actuator created. $i^{\textrm{th}}$ layer is built by initialising the layer, repeating addition functions 1-5 $i$ times, and finishing by repeating addition function 6 $(i-1)$ times.}
	\label{fig:appendix:sloan_structure}
\end{center}
\end{figure}

\begin{figure}[h]
\begin{center}
	\scriptsize{
	\begin{python}
position.append((0, 0))

def initialisation(num_circle):
    position.append((2*R*num_circle, 0))
    return

def addition_1():
    position.append(tuple((map(operator.add, position[(len(position)-1)], (-R, R * 3 ** 0.5)))))
    return

def addition_2():
    position.append(tuple((map(operator.add, position[(len(position)-1)], (-2*R, 0)))))
    return

def addition_3():
    position.append(tuple((map(operator.add, position[(len(position)-1)], (-R, -R * 3 ** 0.5)))))
    return

def addition_4():
    position.append(tuple((map(operator.add, position[(len(position)-1)], (R, - R * 3 ** 0.5)))))
    return

def addition_5():
    position.append(tuple((map(operator.add, position[(len(position)-1)], (2*R, 0)))))
    return

def addition_6():
    position.append(tuple((map(operator.add, position[(len(position)-1)], (R, R * 3 ** 0.5)))))
    return

def repeat_function(num_circle, function):
    for _ in range(num_circle):function()
    return

def layer_prod(layer):
    initialisation(layer)
    repeat_function(layer, addition_1)
    repeat_function(layer, addition_2)
    repeat_function(layer, addition_3)
    repeat_function(layer, addition_4)
    repeat_function(layer, addition_5)
    repeat_function((layer - 1), addition_6)
    return
	\end{python}
	}
	\caption{Code generating the hexagonal structure shown Figure \ref{fig:sloan:arrangement}. The first line creates the actuator at position (0, 0) (see Figure \ref{fig:appendix:sloan_structure:initialization}). Effects of functions initialisation and addition 1 to 6 are shown on Figure \ref{fig:appendix:sloan_structure:layer_1}. Upper layers (see Figure \ref{fig:appendix:sloan_structure:layer_2}) are built using functions repeat_function and layer_prod.}
	\label{fig:appendix:sloan_structure:code}
\end{center}
\end{figure}


\chapter{Conclusions and Future Work}

\section{Conclusions}

This project aimed to contribute:
\begin{enumerate}[$\mathcal{C}$1.]
  \item The first articulation of the proposed architecture, including the context needed to understand it. It does, by way of this report, particular the second and third chapters.
  \item A Python implementation of the new architecture and algorithm. This was created in order to meet the last contribution. The code is publicly available on Github.
  \item A suite of graded evaluations/tests that explore how the architecture and source separation algorithm work in practice. The project explores how the architecture and algorithm work in practice. However more questions are raised as to why it seems to break down, performing less strikingly in the larger more challenging cases.
\end{enumerate}

The main takeaway from the project is that the ORBM works in smaller problems, and the implications and applications if it does work in larger cases would be huge, so there is a motivation to continue this work going forward.

\section{Future Work}

Frean and Marsland should pursure exactly why the ORBM is not performing better than the RBM on the MNIST dataset. I would suggest ensuring both models have a similar range of weights, to ensure one does not over power the other. Or another approach would be to revert back to using a single model, except the model would be trained on all MNIST digits (0-9). Then a composite input would be shown to this model in the ORBM architecture and the reconstructions should separate the sources of that input. This would alleviate issues of weights having different ranges.


\section{First Ispc performance test}

This are the results for the sponza scene.

Time measurements are done on the new raytracer that uses Ispc. It saves the distance of aabb intersections and stops early if ray already hit something closer. It currently only supports bvh with multiple split axis for the children.

"Time triangle intersections" is the time all triangle intersections took. "Time rest" means the total time all rays took to render the image minus the time the triangle intersections took. Both times are in seconds. To get more consistent data multi threading was not used. Both triangle intersection and aabb intersections are done with Ispc. Each memory block is padded to 32 bit (both triangles and aabb). Im sure the triangle intersection code can still be optimized.

The program was executed with windows priority "high" but results are not really consistent.

First set of plots is with avx (8 lanes) and the second set is with sse(4 lanes)

\iftrue
\begin{figure}[!htb]
	\begin{minipage}[t]{0.4\textwidth}
		\plot{Time triangle intersections}{triangleIntersectionSum}{Data/sponzaPad8TableWithSpace.txt}
	\end{minipage}\hfil \hfil
	\begin{minipage}[t]{0.4\textwidth}
		\plotr{Time rest}{rayTimeWithoutTri}{Data/sponzaPad8TableWithSpace.txt}
	\end{minipage}

	\caption{Overview over the sponza ispc performance tests. (avx2)}
\end{figure}
\newpage
\fi

\pgfplotsset{
	every axis/.append style={colorbar = false}
}

\begin{figure}[!htb]
	\begin{minipage}[t]{0.4\textwidth}
		\begin{tikzpicture}
		\begin{axis}
		[
		%view={90}{0} for x, view={0}{0} for y restriction
		view={0}{0},
		xlabel = \leafs,
		ylabel = \branchf,
		zlabel = Time triangle intersections,
		cycle list name=exotic,
		legend style={at={(0.99,1.5)}, anchor = north east},
		xtick={2,3,4,5,6,7,8,9,10,11,12,13,14,15,16},
		xticklabels={,,,$5$,,,,,$10$,,,,,$15$,},
		]
		\addplot3+[thick, restrict y to domain={2: 2}, mark=none]table[x = leafSize, y = branchFactor, z = triangleIntersectionSum, col sep=comma]{Data/sponzaPad8Table.txt};
		\addplot3+[thick, restrict y to domain={4: 4}, mark=none]table[x = leafSize, y = branchFactor, z = triangleIntersectionSum, col sep=comma]{Data/sponzaPad8Table.txt};
		\addplot3+[thick, restrict y to domain={8: 8}, mark=none]table[x = leafSize, y = branchFactor, z = triangleIntersectionSum, col sep=comma]{Data/sponzaPad8Table.txt};
		\addplot3+[thick, restrict y to domain={16: 16}, mark=none]table[x = leafSize, y = branchFactor, z = triangleIntersectionSum, col sep=comma]{Data/sponzaPad8Table.txt};

		\legend{N2, N4, N8, N16}
		\end{axis}
		\end{tikzpicture}
	\end{minipage}\hfil \hfil
	\begin{minipage}[t]{0.4\textwidth}
		%TODO move label to the right side
		\begin{tikzpicture}
		\begin{axis}
		[
		%view={90}{0} for x, view={0}{0} for y restriction
		view={0}{0},
		xlabel = \leafs,
		ylabel = \branchf,
		zlabel = Time rest,
		cycle list name=exotic,
		legend style={at={(0.99,1.5)}, anchor = north east},
		xtick={2,3,4,5,6,7,8,9,10,11,12,13,14,15,16},
		xticklabels={,,,$5$,,,,,$10$,,,,,$15$,},
		]
		\addplot3+[thick, restrict y to domain={2: 2}, mark=none]table[x = leafSize, y = branchFactor, z = rayTimeWithoutTri, col sep=comma]{Data/sponzaPad8Table.txt};
		\addplot3+[thick, restrict y to domain={4: 4}, mark=none]table[x = leafSize, y = branchFactor, z = rayTimeWithoutTri, col sep=comma]{Data/sponzaPad8Table.txt};
		\addplot3+[thick, restrict y to domain={8: 8}, mark=none]table[x = leafSize, y = branchFactor, z = rayTimeWithoutTri, col sep=comma]{Data/sponzaPad8Table.txt};
		\addplot3+[thick, restrict y to domain={16: 16}, mark=none]table[x = leafSize, y = branchFactor, z = rayTimeWithoutTri, col sep=comma]{Data/sponzaPad8Table.txt};

		\legend{N2, N4, N8, N16}
		\end{axis}
		\end{tikzpicture}
	\end{minipage}

	\caption{Comparison of N2, N4, N8, N16 for sponza performance tests. (avx2)}
\end{figure}

\begin{figure}[!htb]
	\begin{minipage}[t]{0.4\textwidth}
		\begin{tikzpicture}
		\begin{axis}
		[
		%view={90}{0} for x, view={0}{0} for y restriction
		view={90}{00},
		xlabel = \leafs,
		ylabel = \branchf,
		zlabel = time triangle intersections,
		legend style={at={(0.99,1.5)}, anchor = north east},
		cycle list name=exotic,
		ytick={2,3,4,5,6,7,8,9,10,11,12,13,14,15,16},
		yticklabels={,,,$5$,,,,,$10$,,,,,$15$,},
		]
		\addplot3+[thick, restrict x to domain={4:4}, unbounded coords=discard, mark=none]table[x = leafSize, y = branchFactor, z = triangleIntersectionSum, col sep=comma]{Data/sponzaPad8Table.txt};
		\addplot3+[thick, restrict x to domain={8:8}, unbounded coords=discard, mark=none]table[x = leafSize, y = branchFactor, z = triangleIntersectionSum, col sep=comma]{Data/sponzaPad8Table.txt};
		\addplot3+[thick, restrict x to domain={12:12}, unbounded coords=discard, mark=none]table[x = leafSize, y = branchFactor, z = triangleIntersectionSum, col sep=comma]{Data/sponzaPad8Table.txt};
		\addplot3+[thick, restrict x to domain={16:16}, unbounded coords=discard, mark=none]table[x = leafSize, y = branchFactor, z = triangleIntersectionSum, col sep=comma]{Data/sponzaPad8Table.txt};

		\legend{L4,L8,L12,L16}
		\end{axis}
		\end{tikzpicture}
	\end{minipage}\hfil \hfil
	\begin{minipage}[t]{0.4\textwidth}
		%TODO move label to the right side
		\begin{tikzpicture}
		\begin{axis}
		[
		%view={90}{0} for x, view={0}{0} for y restriction
		view={90}{0},
		xlabel = \leafs,
		ylabel = \branchf,
		zlabel = time rest,
		cycle list name=exotic,
		ytick={2,3,4,5,6,7,8,9,10,11,12,13,14,15,16},
		yticklabels={,,,$5$,,,,,$10$,,,,,$15$,},
		]
		\addplot3+[thick, restrict x to domain={4:4}, unbounded coords=discard, mark=none]table[x = leafSize, y = branchFactor, z = rayTimeWithoutTri, col sep=comma]{Data/sponzaPad8Table.txt};
		\addplot3+[thick, restrict x to domain={8:8}, unbounded coords=discard, mark=none]table[x = leafSize, y = branchFactor, z = rayTimeWithoutTri, col sep=comma]{Data/sponzaPad8Table.txt};
		\addplot3+[thick, restrict x to domain={12:12}, unbounded coords=discard, mark=none]table[x = leafSize, y = branchFactor, z = rayTimeWithoutTri, col sep=comma]{Data/sponzaPad8Table.txt};
		\addplot3+[thick, restrict x to domain={16:16}, unbounded coords=discard, mark=none]table[x = leafSize, y = branchFactor, z = rayTimeWithoutTri, col sep=comma]{Data/sponzaPad8Table.txt};

		\legend{L4,L8,L12,L16}
		\end{axis}
		\end{tikzpicture}
	\end{minipage}
	\caption{Comparison of L4, L8, L12, L16 for sponza performance tests. (avx2) (Im sure the spikes on L4 are not consistent there. The run before padding had no anomalies like that?)}
\end{figure}

\pgfplotsset{
	every axis/.append style={colorbar = true}
}
\iftrue
\begin{figure}[!htb]
	\begin{minipage}[t]{0.4\textwidth}
		\plot{Time triangle intersections}{triangleIntersectionSum}{Data/sponzaSSETableWithSpace.txt}
	\end{minipage}\hfil \hfil
	\begin{minipage}[t]{0.4\textwidth}
		\plotr{Time rest}{rayTimeWithoutTri}{Data/sponzaSSETableWithSpace.txt}
	\end{minipage}

	\caption{Overview over the sponza ispc performance tests. (sse4)}
\end{figure}
\newpage
\fi

\pgfplotsset{
	every axis/.append style={colorbar = false}
}

\begin{figure}[!htb]
	\begin{minipage}[t]{0.4\textwidth}
		\begin{tikzpicture}
		\begin{axis}
		[
		%view={90}{0} for x, view={0}{0} for y restriction
		view={0}{0},
		xlabel = \leafs,
		ylabel = \branchf,
		zlabel = Time triangle intersections,
		cycle list name=exotic,
		legend style={at={(0.99,1.5)}, anchor = north east},
		xtick={2,3,4,5,6,7,8,9,10,11,12,13,14,15,16},
		xticklabels={,,,$5$,,,,,$10$,,,,,$15$,},
		]
		\addplot3+[thick, restrict y to domain={2: 2}, mark=none]table[x = leafSize, y = branchFactor, z = triangleIntersectionSum, col sep=comma]{Data/sponzaSSETable.txt};
		\addplot3+[thick, restrict y to domain={4: 4}, mark=none]table[x = leafSize, y = branchFactor, z = triangleIntersectionSum, col sep=comma]{Data/sponzaSSETable.txt};
		\addplot3+[thick, restrict y to domain={8: 8}, mark=none]table[x = leafSize, y = branchFactor, z = triangleIntersectionSum, col sep=comma]{Data/sponzaSSETable.txt};
		\addplot3+[thick, restrict y to domain={16: 16}, mark=none]table[x = leafSize, y = branchFactor, z = triangleIntersectionSum, col sep=comma]{Data/sponzaSSETable.txt};

		\legend{N2, N4, N8, N16}
		\end{axis}
		\end{tikzpicture}
	\end{minipage}\hfil \hfil
	\begin{minipage}[t]{0.4\textwidth}
		%TODO move label to the right side
		\begin{tikzpicture}
		\begin{axis}
		[
		%view={90}{0} for x, view={0}{0} for y restriction
		view={0}{0},
		xlabel = \leafs,
		ylabel = \branchf,
		zlabel = Time rest,
		cycle list name=exotic,
		legend style={at={(0.99,1.5)}, anchor = north east},
		xtick={2,3,4,5,6,7,8,9,10,11,12,13,14,15,16},
		xticklabels={,,,$5$,,,,,$10$,,,,,$15$,},
		]
		\addplot3+[thick, restrict y to domain={2: 2}, mark=none]table[x = leafSize, y = branchFactor, z = rayTimeWithoutTri, col sep=comma]{Data/sponzaSSETable.txt};
		\addplot3+[thick, restrict y to domain={4: 4}, mark=none]table[x = leafSize, y = branchFactor, z = rayTimeWithoutTri, col sep=comma]{Data/sponzaSSETable.txt};
		\addplot3+[thick, restrict y to domain={8: 8}, mark=none]table[x = leafSize, y = branchFactor, z = rayTimeWithoutTri, col sep=comma]{Data/sponzaSSETable.txt};
		\addplot3+[thick, restrict y to domain={16: 16}, mark=none]table[x = leafSize, y = branchFactor, z = rayTimeWithoutTri, col sep=comma]{Data/sponzaSSETable.txt};

		\legend{N2, N4, N8, N16}
		\end{axis}
		\end{tikzpicture}
	\end{minipage}

	\caption{Comparison of N2, N4, N8, N16 for sponza performance tests. (sse4)}
\end{figure}

\begin{figure}[!htb]
	\begin{minipage}[t]{0.4\textwidth}
		\begin{tikzpicture}
		\begin{axis}
		[
		%view={90}{0} for x, view={0}{0} for y restriction
		view={90}{00},
		xlabel = \leafs,
		ylabel = \branchf,
		zlabel = time triangle intersections,
		legend style={at={(0.99,1.5)}, anchor = north east},
		cycle list name=exotic,
		ytick={2,3,4,5,6,7,8,9,10,11,12,13,14,15,16},
		yticklabels={,,,$5$,,,,,$10$,,,,,$15$,},
		]
		\addplot3+[thick, restrict x to domain={4:4}, unbounded coords=discard, mark=none]table[x = leafSize, y = branchFactor, z = triangleIntersectionSum, col sep=comma]{Data/sponzaSSETable.txt};
		\addplot3+[thick, restrict x to domain={8:8}, unbounded coords=discard, mark=none]table[x = leafSize, y = branchFactor, z = triangleIntersectionSum, col sep=comma]{Data/sponzaSSETable.txt};
		\addplot3+[thick, restrict x to domain={12:12}, unbounded coords=discard, mark=none]table[x = leafSize, y = branchFactor, z = triangleIntersectionSum, col sep=comma]{Data/sponzaSSETable.txt};
		\addplot3+[thick, restrict x to domain={16:16}, unbounded coords=discard, mark=none]table[x = leafSize, y = branchFactor, z = triangleIntersectionSum, col sep=comma]{Data/sponzaSSETable.txt};

		\legend{L4,L8,L12,L16}
		\end{axis}
		\end{tikzpicture}
	\end{minipage}\hfil \hfil
	\begin{minipage}[t]{0.4\textwidth}
		%TODO move label to the right side
		\begin{tikzpicture}
		\begin{axis}
		[
		%view={90}{0} for x, view={0}{0} for y restriction
		view={90}{0},
		xlabel = \leafs,
		ylabel = \branchf,
		zlabel = time rest,
		cycle list name=exotic,
		ytick={2,3,4,5,6,7,8,9,10,11,12,13,14,15,16},
		yticklabels={,,,$5$,,,,,$10$,,,,,$15$,},
		]
		\addplot3+[thick, restrict x to domain={4:4}, unbounded coords=discard, mark=none]table[x = leafSize, y = branchFactor, z = rayTimeWithoutTri, col sep=comma]{Data/sponzaSSETable.txt};
		\addplot3+[thick, restrict x to domain={8:8}, unbounded coords=discard, mark=none]table[x = leafSize, y = branchFactor, z = rayTimeWithoutTri, col sep=comma]{Data/sponzaSSETable.txt};
		\addplot3+[thick, restrict x to domain={12:12}, unbounded coords=discard, mark=none]table[x = leafSize, y = branchFactor, z = rayTimeWithoutTri, col sep=comma]{Data/sponzaSSETable.txt};
		\addplot3+[thick, restrict x to domain={16:16}, unbounded coords=discard, mark=none]table[x = leafSize, y = branchFactor, z = rayTimeWithoutTri, col sep=comma]{Data/sponzaSSETable.txt};

		\legend{L4,L8,L12,L16}
		\end{axis}
		\end{tikzpicture}
	\end{minipage}
	\caption{Comparison of L4, L8, L12, L16 for sponza performance tests. (sse4)}
\end{figure}


\section{Power to the People}

It is well accepted that large data systems benefit significantly
from careful optimization of data movement. Optimizing compilers and query plan
rewriters aim to do this automatically.
\Q\ takes a fundamentally different approach. It is our belief that the
choreography of computations can be left to the database programmer {\bf if}
they have
(i) some understanding of the underyling system architecture and (ii)
relatively simple knobs to influence the run time system.

In many analytical tasks, one repeatedly performs very similar operations on slowly
changing data. In these cases,
it is not onerous to maintain (and periodically refresh) summary statistics that
can significantly speed up more complex operations. For example, \Q's sort
routine gets linear speed up on up to 8 cores, when provided with
a rough distribution of the keys.
Note that the fidelity demanded of these summary statistics is often low ---
they need to be only as good as the use to which they are put.

\subsection{Building indexes --- the social graph}
\label{social_graph}

At LinkedIn, we used \Q\ to analyze the social graph, given its importance in
extracting insights.
Assume that a social graph is presented as a
a table, {\tt E}, with 2 columns, {\tt from} and {\tt to}. \Q\ supports the
creation of auxiliary data structures to enable fast access but, in keeping with
our minimalist philosophy, does not provide them out of the box. We describe the
pre-processing performed to provide efficient access.
\be
\item {\tt f, t = Q.sort2(E.from, E.to, "ascending")}, sorts {\tt E}
with {\tt from} as primary key and {\tt to} as
secondary key
\item {\tt V.m = Q.unique(f)} creates a Vector of
member IDs, sorted ascending.
\item For each member in {\tt V.m}, we determine the contiguous edges out of it
  by
  \be
\item {\tt V.lb = Q.join(f, V.m, min\_index)}
\item {\tt V.ub = Q.join(f, V.m, max\_index)}
  \ee
Now, we can assert that member
{\tt m = V.m[i]} is connected to members {\tt t[V.lb[i] .. V.ub[i]]}
\ee


\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{braket}%量子力学符号
\usepackage{geometry}
\usepackage{enumerate}
\usepackage{natbib}
\usepackage{float}%稳定图片位置
\usepackage{graphicx,subfig}%画图
\usepackage{caption}
\usepackage[english]{babel}
\usepackage{indentfirst}%缩进
\usepackage{enumerate}%加序号
\usepackage{multirow}%合并行
\usepackage{hyperref}
\hypersetup{hypertex=true, colorlinks=true, linkcolor=black, anchorcolor=black, citecolor=black}
\title{\Large \textbf{VP390 Problem Set 4}\\
\author{\textbf{Pan, Chongdan ID:516370910121}\\
}
}
\begin{document}
\maketitle
\section{Problem 1}
\begin{enumerate}[(a)]
    \item $\hat{\pi_x}f(x)=\hbar\frac{\partial e^{-x^2}}{\partial x}=-2x\hbar e^{-x^2}$
    \\$\hat{M}_{\frac{1}{2}}f=e^{-(\frac{1}{2}x)^2}=e^{-\frac{x^2}{4}}$
    \item Assume the conjugates exist and donate as $\hat{\pi_x}^\dagger,\hat{M_c}^\dagger$
    \\$\braket{\Psi,\hat{\pi_x}\Phi}=\int_{-\infty}^\infty\Psi^*\hbar\frac{\partial\Phi}{\partial x}\mathrm{d}x$
    \\$=[\hbar\Psi^*\Phi]^\infty_{-\infty}-\int_{-\infty}^\infty\hbar\frac{\partial\Psi^*}{\partial x}\Phi\mathrm{d}x=-\braket{\hat{\pi_x}\Psi,\Phi}$
    \\So $\hat{\pi_x}^\dagger=-\hat{\pi_x}$, and it's not Hermitian
    \\\\$\braket{\Psi,\hat{M_c}\Phi}=\int_{-\infty}^\infty\Psi^*(x)\Phi(cx)\mathrm{d}x$
    \\Let $y=cx$, then $\braket{\Psi,\hat{M_c}\Phi}=\int_{-\infty}^\infty\frac{\Psi^*(\frac{y}{c})\Phi(y)}{c}\mathrm{d}y$
    \\So $\hat{M_c}^\dagger=\frac{\hat{M_{1/c}}}{c}$, and it's not Hermitian
\end{enumerate}
\section{Problem 2}
\begin{enumerate}[(a)]
    \item $\braket{\Psi,(\alpha\hat{A})\Phi}=\alpha\braket{\hat{A}^\dagger\Psi,\Phi}=\braket{(\alpha^*\hat{A}^\dagger)\Psi,\Phi}$
    \\So $(\alpha\hat{A})^\dagger=\alpha^*\hat{A}^\dagger$
    \item $\braket{\Psi,\hat{A}\Phi}=\braket{\hat{A}^\dagger\Psi,\Phi}=\braket{\Phi,\hat{A}^\dagger\Psi}=\braket{(\hat{A}^\dagger)^\dagger\Phi,\Psi}=\braket{\Psi,(\hat{A}^\dagger)^\dagger\Phi}$
    \\So $(\hat{A}^\dagger)^\dagger=\hat{A}$
    \item $\braket{(\hat{A}\hat{B})^\dagger\Psi,\Phi}=\braket{\Psi,(\hat{A}\hat{B})\Phi}=\braket{\Psi,\hat{A}(\hat{B}\Phi)}=\braket{\hat{A}^\dagger\Psi,\hat{B}\Phi}=\braket{\hat{B}^\dagger\hat{A}^\dagger\Psi,\Phi}$
    \\So $(\hat{A}\hat{B})^\dagger=\hat{B}^\dagger\hat{A}^\dagger$
\end{enumerate}
\section{Problem 3}
\begin{enumerate}[(a)]
    \item $E_1=\frac{\hbar^2\pi^2}{2mL^2}=\frac{(1.05\times10^{-34})^2\times3.14^2}{2\times1.67\times10^{-27}(10^{-15})^2}\approx3.25\times10^{-11}J=203.4$MeV
    \item $E_1=h\nu=h\frac{c}{\lambda}$
    \\$\lambda_1=h\frac{c}{E_1}=6.63\times10^{-34}\frac{3\times10^8}{3.3\times10^{-11}}\approx6.12\times10^{-15}$m
    \item $\bigtriangleup E=(2^2-1)E_1=3E_1$
    \\$\lambda_2=\frac{\lambda_1}{3}\approx2.04\times10^{-15}$m
    \item $\bigtriangleup E=(3^2-2^2)E_1=5E_1$
    \\$\lambda_3=\frac{\lambda_1}{5}\approx1.22\times10^{-15}$m
    \item $\bigtriangleup E=(3^2-1)E_1=8E_1$
    \\$\lambda_4=\frac{\lambda_1}{8}\approx7.65\times10^{-16}$m
\end{enumerate}
\section{Problem 4}
\begin{enumerate}[(a)]
    \item $\braket{\Psi,\Psi}=\braket{\sqrt{\frac{1}{3}}\psi_3,\sqrt{\frac{1}{3}}\psi_3}+\braket{\sqrt{\frac{1}{3}}\psi_3,\sqrt{\frac{2}{3}}\psi_5}+\braket{\sqrt{\frac{2}{3}}\psi_5,\sqrt{\frac{1}{3}}\psi_3},\braket{\sqrt{\frac{2}{3}}\psi_5+\sqrt{\frac{2}{3}}\psi_5}$
    \\$=\frac{1}{3}\braket{\psi_3,\psi_3}+0+0+\frac{2}{3}\braket{\psi_5,\psi_5}=\frac{1}{3}+\frac{2}{3}=1$
    \\So it's normalized
    \item $\Psi(x,0)=\sqrt{\frac{2}{3a}}\sin\frac{3\pi}{a}x+\sqrt{\frac{4}{3a}}\sin\frac{5\pi}{a}x$
    \\$\Psi(x,0)\Psi(x,0)^*=(\sqrt{\frac{2}{3a}}\sin\frac{3\pi}{a}x+\sqrt{\frac{4}{3a}}\sin\frac{5\pi}{a}x)^2$
    \\$=\frac{2}{3a}\sin^2\frac{3\pi}{a}x+\frac{2\sqrt{2}}{3a}\sin\frac{3\pi}{a}x\sin\frac{5\pi}{a}x+\frac{4}{3a}\sin^2\frac{5\pi}{a}x$
    \\$\int_0^{a/2}\Psi(x,0)^2\mathrm{d}x=\frac{4}{15}$
    \\So the probability is $\frac{1}{2}$
    \item The possible outcome of energy is $E_3=\frac{9\hbar^2\pi^2}{2ma^2},E_5=\frac{25\hbar^2\pi^2}{2ma^2}$
    \\The probability to get $E_3$ is $\frac{1}{3}$, for $E_5$ is $\frac{2}{3}$
    \item $E=\frac{1}{3}E_3+\frac{2}{3}E_5=\frac{59\hbar^2\pi^2}{6ma^2}$
    \item $\Psi(x,t)=\sqrt{\frac{1}{3}}e^{\frac{i}{\hbar}E_3t}\psi_3(x)+\sqrt{\frac{2}{3}}e^{\frac{i}{\hbar}E_5t}\psi_5(x)$
\end{enumerate}
\section{Problem 5}
\begin{enumerate}[(a)]
    \item $\Psi(x,0)=\Phi(x)=\sum_nc_n\psi_n(x)$, where
    \\$\Phi(x)=\begin{cases}
        \sqrt{\frac{2}{a}}\sin\frac{\pi}{a}x &0\leq x\leq a\\
        0 &\mathrm{otherwise}
    \end{cases}$
    \\$c_m=\braket{\psi_m,\Phi}$, where
    \\$\psi_m(x)=\begin{cases}
        \sqrt{\frac{2}{2a}}\sin\frac{m\pi}{2a}x &0\leq x\leq 2a\\
        0 &\mathrm{otherwise}
    \end{cases}$
    \\$c_m=\int_{-\infty}^{\infty}\psi_m^*\Phi\mathrm{d}x=\frac{\sqrt{2}}{a}\int_0^a\sin\frac{\pi}{a}x\sin\frac{m\pi}{2a}x\mathrm{d}x$
    \\Therefore $c_1=\frac{4\sqrt{2}}{3\pi},c_2=\frac{\sqrt{2}}{2},c_3=\frac{4\sqrt{2}}{5\pi},c_4=0,c_5=\frac{-4\sqrt{2}}{21\pi}$
    \\When $0=t_1<t_2<t_3<t_4$, we can the plot:
    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.5]{P1.png}
        \caption{Plot for $t=t_1=0$}
        \label{P1}
    \end{figure}
    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.5]{P2.png}
        \caption{Plot for $t=t_2$}
        \label{P2}
    \end{figure}
    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.5]{P3.png}
        \caption{Plot for $t=t_3$}
        \label{P3}
    \end{figure}
    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.5]{P4.png}
        \caption{Plot for $t=t_4$}
        \label{P4}
    \end{figure}
    \item When $t<0,E=\frac{\hbar^2\pi^2}{2ma^2}$
    \\When $t\geq0,E_2=\frac{\hbar^2\pi^2}{2m(2a)^2}n^2=E$
    \\So the probability is $c_2^2=0.5$
\end{enumerate}
\section{Problem 6}
\begin{enumerate}[(b)]
    \item Assume $\psi$ is the solution for $-\frac{\hbar^2}{2m}\frac{\mathrm{d}^2\psi(x)}{\mathrm{d}x^2}+V(x)\psi(x)=E\psi(x)$
    \\Then $-\frac{\hbar^2}{2m}\frac{\mathrm{d}^2\psi(-x)}{\mathrm{d}x^2}+V(-x)\psi(-x)=E\psi(-x)$
    \\Since $V(x)=V(-x)$
    \\$-\frac{\hbar^2}{2m}\frac{\mathrm{d}^2\psi(-x)}{\mathrm{d}x^2}+V(x)\psi(-x)=E\psi(-x)$
    \\$\psi(-x)$ also satisfies this stationery equation.
    \\Therefore $\psi(x)\pm\psi(-x)$ satisfies this equation as well.
\end{enumerate}
\end{document}


% !TEX root = main.tex
% !TEX spellcheck = en-US
\section{Definitions and lemmas for multi-round SRS-based protocols}
\label{sec:se_definitions}

%\chaya{move the USE definition to this section?}

Yhe protocols we consider have an SRS, more than three messages, require more than
just two transcripts for standard model extraction and are not special
sound. As a first step to prove non-malleability of such protocols, we need to consider a special
soundness property adapted to the forking soundness definition in the case of multi-round SRS-based protocols. We also need to consider a more general forking lemma and to introduce unique response property compatible with
multi-round SRS-based protocols.

\subsection{Generalised forking lemma.}
%\label{sec:forking_lemma}
First of all, although dubbed ``general'', \cref{lem:forking_lemma} is not
general enough for our purpose as it is useful only for protocols where witness
can be extracted from just two transcripts. To be able to extract a witness
from, say, an execution of $\plonkprot$ we need to obtain at least
$(3 \numberofconstrains + 1)$ valid proofs, and $(\multconstr + \linconstr + 1)$ for $\sonicprot$. Here we
propose a generalisation of the general forking lemma that given probability of
producing an accepting transcript, $\accProb$, lower-bounds the probability of
generating a \emph{tree of accepting transcripts} $\tree$, which allows to
extract a witness.

\begin{definition}[Tree of accepting transcripts, cf.~{\cite{EC:BCCGP16}}]
	\label{def:tree_of_accepting_transcripts}
	Consider a $(2\mu + 1)$-message interactive proof system $\ps$. A $(n_1,
  \ldots, n_\mu)$-tree of accepting transcripts is a tree where each node on
  depth $i$, for $i \in \range{1}{\mu + 1}$, is an $i$-th prover's message in an
  accepting transcript; edges between the nodes are labeled with verifier's
  challenges, such that no two edges on the same depth have the same
  label; and each node on depth $i$ has $n_{i} - 1$ siblings and $n_{i +
    1}$ children. The tree consists of $N = \prod_{i = 1}^\mu n_i$
  branches, where $N$ is the number of accepting transcripts. We require $N = \poly$.
\end{definition}

\begin{lemma}[General forking lemma II]
	\label{lem:generalised_forking_lemma}
	Fix $q \in \ZZ$ and set $H$ of size $h \geq m$. Let $\zdv$ be a $\ppt$
  algorithm that on input $y, h_1, \ldots, h_q$ returns $(i, s)$ where $i \in
  \range{0}{q}$ and $s$ is called a side output. Denote by $\ig$ a randomised
  instance generator. We denote by $\accProb$ the probability
	\[
		\condprob{i \neq 0}{ y \gets \ig;\ h_1, \ldots, h_q \sample H;\ (i, s)
		\gets \zdv(y, h_1, \ldots, h_q)}\,.
	\]
	Let $\genforking_{\zdv}^{m}$ denote the algorithm described in
  \cref{fig:genforking_lemma} then the probability $\frkProb := \condprob{b =
    1}{y \gets \ig;\ h_1, \ldots, h_{q} \sample H;\ (b, \vec{s}) \gets
    \genforking_{\zdv}^{m}(y, h_1, \ldots, h_q)}$ is at least
	\[
		\frac{\accProb^m}{q^{m - 1}} - \accProb \cdot \left(1 -
    \frac{h!}{(h - m)! \cdot h^{m}}\right).
	\]

	\begin{figure}[t]
		\centering
		\fbox{
		\procedure{$\genforking_{\zdv}^{m} (y,h_1^{1}, \ldots, h_{q}^{1})$}
		{
		\rho \sample \RND{\zdv}\\
		(i, s_1) \gets \zdv(y, h_1^{1}, \ldots, h_{q}^{1}; \rho)\\
    i_1 \gets i\\
		\pcif i = 0\ \pcreturn (0, \bot)\\
		\pcfor j \in \range{2}{m}\\
		\pcind h_{1}^{j}, \ldots, h_{i - 1}^{j} \gets h_{1}^{j - 1}, \ldots,
		h_{i - 1}^{j - 1}\\
		\pcind h_{i}^{j}, \ldots, h_{q}^{j} \sample H\\
		\pcind (i_j, s_j) \gets \zdv(y, h_1^{j}, \ldots, h_{i - 1}^{j}, h_{i}^{j},
		\ldots, h_{q}^{j}; \rho)\\
		\pcind \pcif i_j = 0 \lor i_j \neq i\ \pcreturn (0, \bot)\\
    \pcif \exists (j, j') \in \range{1}{m}^2, j \neq j' : (h_{i}^{j} = h_{i}^{j'})\
	\pcreturn (0, \bot)\\
		\pcelse \pcreturn (1, \vec{s})
	}}
	\caption{Generalised forking algorithm $\genforking_{\zdv}^{m}$}
	\label{fig:genforking_lemma}
\end{figure}
\end{lemma}
The proof goes similarly to \cite[Lemma 1]{CCS:BelNev06} with some modifications required
by the fact that the protocol has more than 3 rounds and the number of
transcripts required is larger. Due to page limit, the proof is presented in
\cref{sec:forking_proof}.

To highlight importance of the generalised forking lemma we describe how we use
it in our forking simulation-extractability proof.  Let $\proofsystem$ be a
forking sound proof system where for an instance $\inp$ the
corresponding witness can be extracted from an
$(1, \ldots, 1, n_k, 1, \ldots, 1)$-tree of accepting transcripts.  Let $\advse$
be the simulation-extractability adversary that outputs an accepting proof with
probability at least $\accProb$. (Although we use the same $\accProb$ to denote
probability of $\zdv$ outputing a non-zero $i$ and the probability of $\advse$
outputing an accepting proof, we claim that these probabilities are exactly the
same by the way we define $\zdv$.)  Let $\advse$ produce an accepting
proof $\zkproof_{\advse}$ for instance $\inp_{\advse}$; $r$ be $\advse$'s
randomness; $Q$ the list of queries submitted by $\advse$ along with simulator
$\simulator$'s answers; and $Q_\ro$ be the list of all random oracle
queries made by $\advse$.  All of these are given to the extractor $\ext$ that
internally runs the forking algorithm $\genforking_\zdv^{n_k}$.  Algorithm $\zdv$
takes $(\srs, \advse,
%\inp_\advse,
%\zkproof_\advse,
Q, r)$ as input $y$ and $Q_\ro$ as input $h_1^1, \ldots,
h_q^1$.
(For the sake of completeness, we allow $\genforking_\zdv^{n_k}$ to
pick $h^1_{l + 1}, \ldots, h^1_q$ responses if $Q_\ro$ has only $l < q$
elements.)

Next, $\zdv$ internally runs $\advse(\srs; r)$ and responds to its random
oracle and simulator queries by using $Q_\ro$ and $Q$. Note that $\advse$ makes
the same queries as it did before it output $(\inp_{\advse}, \zkproof_{\advse})$
as it is run on the same random tape and with the same answers from the
simulator and random oracle. Once $\advse$ outputs
$\zkproof_{\advse}$, algorithm $\zdv$ outputs $(i, \zkproof_{\advse})$, where
$i$ is the index of a random oracle query submitted by $\advse$ to receive the challenge after the
$k$-th message from the prover---a message where the tree of transcripts
branches.
Then, after the first run of $\advse$ is done, the extractor runs $\zdv$ again,
but this time it provides fresh random oracle responses $h^2_i, \ldots,
h^2_q$. Note that this is equivalent to rewinding $\advse$ to a point just
before $\advse$ is about to ask its $i$-th random oracle
query. The probability that the adversary produces an accepting transcript with the
fresh random oracle responses is at least $\accProb$. This continues until the
required number of transcripts is obtained.

We note that in the original forking lemma, the forking algorithm $\forking$,
cf.~\cref{fig:forking_lemma}, gets only as input $y$ and elements $h^1_1, \ldots,
h^1_q$ are randomly picked from $H$ internally by $\forking$. However, assuming
that $h^1_1, \ldots, h^1_q$ are random oracle responses, and thus random, makes
the change only notational.

We also note that the general forking lemma proposed in
\cref{lem:generalised_forking_lemma} works for protocols with an extractor that can obtain the
witness from a $(1, \ldots, 1, n_k, 1, \ldots, 1)$-tree of accepting
transcripts. This limitation however does not affect the main result of this
paper, i.e.~showing that both $\plonk$ and $\sonic$ are forking simulation extractable.

\subsection{Unique-response protocols}
Another technical hurdle is the assumption of unique response property
of the transformed sigma protocol required by Faust et al. The original
Fischlin's formulation, although suitable for applications presented in
\cite{C:Fischlin05,INDOCRYPT:FKMV12}, does not suffice in our case. First,
the property assumes that the protocol has three messages, with the second being
the challenge from the verifier. That is not the case we consider here. Second,
it is not entirely clear how to generalize the property. Should one require that
after the first challenge from the verifier, the prover's responses are fixed?
That does not work since the prover needs to answer differently on different
verifier's challenges, as otherwise the protocol could have fewer
rounds. Another problem is that the protocol could consist of
a round other than the first one where the prover message is randomized.
Unique response cannot hold in this case. Finally, the protocols we
consider here are not in the standard model, but use an SRS
what also complicates things considerably.

We walk around these obstacles by providing a generalised notion of the unique
response property. More precisely, we say that a $(2\mu + 1)$-message protocol
has \emph{unique responses from $i$}, and call it an $\ur{i}$-protocol, if it
follows the definition below:

\begin{definition}[$\ur{i}$-protocol]
\label{def:wiur}
Let $\proofsystem$ be a $(2\mu + 1)$-message public coin proof system
$\ps = (\kgen, \prover, \verifier, \simulator)$. Let $\proofsystem_\fs$ be
$\proofsystem$ after the Fiat--Shamir transform and $\ro$ the random
oracle. Denote by $a_1, \ldots, a_{\mu}, a_{\mu + 1}$ protocol messages output
by the prover, We say that $\proofsystem$ has \emph{unique responses from $i$
  on} if for any $\ppt$ adversary $\adv$:
\[
	\prob{
		\begin{aligned}
		&	\inp, \vec{a} = (a_1, \ldots, a_{\mu + 1}), \vec{a'} = (a'_1, \ldots,
    a'_{\mu + 1})
		\gets \adv^\ro(\srs), \\
    & \vec{a} \neq \vec{a'}, a_1, \ldots, a_{i} = a'_1,
    \ldots, a'_{i}, \\
		& \verifier^\ro_\fs (\srs, \inp, \vec{a}) =
		\verifier^\ro_\fs(\srs, \inp, \vec{a'}) = 1
		\end{aligned}
		\ \left|\
	\vphantom{\begin{aligned}
	&	\vec{a} = (a_0, b_0, \ldots, a_j, b_j, a_\mu), \vec{a'} = (a'_0, b'_0, \ldots, a'_j,
	b'_j a'_\mu) \gets \adv(\srs), \vec{a} \neq \vec{a'}, \\
	& b_k = b'_k, k \in \range{1, \mu - 1},\\ a_l = a'_l, l \in
\range{1}{j}, j > i
	\end{aligned}}
\srs \gets \kgen_\fs(\REL) \right.
}
\]
is upper-bounded by some negligible function $\negl$.
% Let $\proofsystem$ be a $(2\mu + 1)$-message public coin proof system
% $\ps = (\kgen, \prover, \verifier, \simulator)$ and let $r$ be verifier's
% randomness which determines its challenges $r_1, \ldots, r_\mu$. Denote by
% $\vec{a} = a_1, \ldots, a_{\mu}, a_{\mu + 1}$ protocol messages output by the prover and by $r_1, \ldots, r_\mu$ the challenges of the verifier, We
% say that $\proofsystem$ has \emph{unique responses from $i$ on} if for any
% $\ppt$ adversary $\adv$:
% \[
% 	\prob{
% 		\begin{aligned}
%     & \vec{a} \neq \vec{a'}, a_1, \ldots, a_{i} = a'_1,
%     \ldots, a'_{i}, \\
% 		& \verifier (\srs, \inp, \vec{a}; r) =
% 		\verifier(\srs, \inp, \vec{a'}; r) = 1
% 		\end{aligned}
% 		\ \left|\
% 	\vphantom{\begin{aligned}
% 	&	\vec{a} = (a_0, b_0, \ldots, a_j, b_j, a_\mu), \vec{a'} = (a'_0, b'_0, \ldots, a'_j,
% 	b'_j a'_\mu) \gets \adv(\srs), \vec{a} \neq \vec{a'}, \\
% 	& b_k = b'_k, k \in \range{1, \mu - 1},\\ a_l = a'_l, l \in
% \range{1}{j}, j > i
% 	\end{aligned}}
%   \begin{aligned}
% &\srs \gets \kgen(\REL) \\
% &\inp, \vec{a}, \vec{a'} \gets \adv(\srs) \\
% & r_1, \ldots, r_i \gets H^i\\
% &    r_{i+1}, \ldots r_\mu \gets H^{\mu-i}, r'_{i+1}, \ldots r'_\mu \gets H^{\mu-i}
% \end{aligned}
%      \right.
% } \leq \negl.
% \]
\end{definition}
Intuitively, a protocol is $\ur{i}$ if it is infeasible for a $\ppt$ adversary
to produce a pair of acceptable and different proofs $\zkproof$, $\zkproof'$
that are the same on  first $i$ messages.
% after $i$-th prover's message, all
% $\prover$'s further messages are determined by the witness it knows, the
% messages it already send and received and the future challenges from the
% verifier.
We note that the definition above is also meaningful for protocols without an SRS. Intuitively in that case $\srs$ is the empty string.

\iffalse
\subsection{Forking soundness}
Note that the special soundness property (as usually defined) holds for
all---even computationally unbounded---adversaries. Unfortunately, since a
simulation trapdoors for $\plonkprot$ and $\sonicprot$ exist, the protocols
cannot be special sound in that regard. This is because an unbounded adversary
could recover the trapdoor and build a number of simulated proofs for a fake
statement. Hence, we provide a weaker, yet sufficient, definition of
\emph{forking soundness}. More precisely, we state that an
adversary that is able to answer correctly multiple challenges either knows the
witness or can be used to break some computational assumption.

\begin{definition}[Forking soundness]
  Let $\proofsystem = (\kgen, \prover, \verifier, \simulator)$ be an
  $(2 \mu + 1)$-message proof system for a relation $\REL$. We say that
  $\proofsystem$ is $(\epsss(\secpar), (n_1, \ldots, n_\mu))$-\emph{forking sound}
  if there exists an extractor $\extt$ that given an $(n_1, \ldots, n_\mu)$-tree
  of acceptable transcripts $\tree$ and instance $\inp$ output by some $\ppt$ adversary $\adv(\REL,
  \srs)$, for $\srs \sample \kgen(\REL)$, outputs $\wit$ such that $\REL(\inp,
  \wit) = 1$ with probability at least $1 - \epsss$.
\end{definition}

Since we do not utilise the classical special soundness (that holds for all,
even unbounded, adversaries) all references to that property should be
understood as references to its computational version.
\fi

\subsection{Forking soundness}
Note that the special soundness property (as usually defined) holds for
all---even computationally unbounded---adversaries. Unfortunately, since a
simulation trapdoors for $\plonkprot$ and $\sonicprot$ exist, the protocols
cannot be special sound in that regard. This is because an unbounded adversary
can recover the trapdoor and build a number of simulated proofs for a fake
statement. Hence, we provide a weaker, yet sufficient, definition of
\emph{forking soundness}. More precisely, we state that an
adversary that is able to answer correctly multiple challenges either knows the
witness or can be used to break some computational assumption.
\chaya{a notion of rewinding-based knowledge soundness has been used to mean exactly the above in prior works, like BBF19. we should clarify if forking soundness different from rewinding-based knowledge soundness? seems to me like the diference is just that here it is tailored for the NI version.}
\chaya{I now see that the diff is the access to the simulator that the adversary gets. might be helpful to clarify this, either here or in a tech overview section. I am not sure why this needs to be defined this way by giving access to the simulator.}
However, differently from the standard definition of special soundness, we do
not require from the extractor to be able to extract the witness from \emph{any}
tree of acceptable transcripts. We require that the tree be produced honestly,
that is, all challenges are picked randomly---exactly as an honest verifier would pick.
Intuitively, the tree is as it would be generated by a $\genforking$
algorithm from the generalized forking lemma.

% \begin{definition}[Forking soundness]
%   % Let $\proofsystem = (\kgen, \prover, \verifier, \simulator)$ be an
%   % $(2 \mu + 1)$-message proof system for a relation $\REL$. Let $\tree$ be an
%   % $(n_1, \ldots, n_\mu)$-tree of acceptable transcripts output by a $\ppt$ tree
%   % building algorithm $\tdv$ which plays the role of the verifier $\ps.\verifier$
%   % against $\ppt$ adversary $\adv$ which it interacts with.
% %
%   % We say that $\proofsystem$ is
%   % $(\epsss, (n_1, \ldots, n_\mu))$-\emph{computationally special sound} if there
%   % exists an extractor $\extt$ that given an $ (n_1, \ldots, n_\mu)$-tree of
%   % acceptable transcripts $\tree$ for an instance $\inp \in \LANG_\REL$ output by
%   % a $\ppt$ tree building adversary $\tdv$ some $\ppt$ adversary
%   % $\adv(\srs)$, for $\srs \sample \kgen(\REL)$, outputs $\wit$ such that
%   % $\REL(\inp, \wit) = 1$ with probability at least $1 - \epsss$.
%   Let $\proofsystem = (\kgen, \prover, \verifier, \simulator)$ be an
%   $(2 \mu + 1)$-message proof system for a relation $\REL$. Let $\tree$ be an
%   $(n_1, \ldots, n_\mu)$-tree of transcripts for an in valid instance
%   $\inp$. We say that $\ps$ is $(\epsss, (n_1, \ldots, n_\mu))$-forking sound if there is an extractor $\extt$ that given $\tree$ extracts $\wit$
%   such that $\REL(\inp, \wit) = 1$ with probability at least $1 - \epsss.$
% \end{definition}

% \begin{definition}[$k$-round forking soundness]
%   % Let $\proofsystem = (\kgen, \prover, \verifier, \simulator)$ be an
%   % $(2 \mu + 1)$-message proof system for a relation $\REL$. Let $\tree$ be an
%   % $(n_1, \ldots, n_\mu)$-tree of acceptable transcripts output by a $\ppt$ tree
%   % building algorithm $\tdv$ which plays the role of the verifier $\ps.\verifier$
%   % against $\ppt$ adversary $\adv$ which it interacts with.
% %
%   % We say that $\proofsystem$ is
%   % $(\epsss, (n_1, \ldots, n_\mu))$-\emph{computationally special sound} if there
%   % exists an extractor $\extt$ that given an $ (n_1, \ldots, n_\mu)$-tree of
%   % acceptable transcripts $\tree$ for an instance $\inp \in \LANG_\REL$ output by
%   % a $\ppt$ tree building adversary $\tdv$ some $\ppt$ adversary
%   % $\adv(\srs)$, for $\srs \sample \kgen(\REL)$, outputs $\wit$ such that
%   % $\REL(\inp, \wit) = 1$ with probability at least $1 - \epsss$.
%   Let $\proofsystem = (\kgen, \prover, \verifier, \simulator)$ be an
%   $(2 \mu + 1)$-message proof system for a relation $\REL$. Let $\tree$ be the
%   algorithm below that rewinds $\advse^{\simulator_\fs,
%           \ro} (\srs; r)$ to produce a $(1, n_k, 1)$-tree of
%   transcripts such that none of the challenges in round $k$ were used in
%   simulated proofs. We say that $\ps$ is $(\epsss, ((1, n_k, 1)))$-forking
%   special sound if there is an extractor $\extt$ that given a tree produced by
%   $\tree$ extracts $\wit$ such that $\REL(\inp, \wit) = 1$ with probability at
%   least $1 - \epsss.$
% \end{definition}

% Since we do not utilise the classical special soundness (that holds for all,
% even unbounded, adversaries) all references to that property should be
% understood as references to its computational version.

\begin{definition}[$(\eps(\secpar), k,n)$-forking soundness]
  Let $\proofsystem = (\kgen, \prover, \verifier, \simulator)$ be an
  $(2 \mu + 1)$-message proof system for a relation $\REL$.

For any $\ppt$ adversary $\advse^{\simulator_\fs,
  \ro} (\srs; r)$ we consider the procedure $\zdv$ that provided the transcript $(\srs, \adv, r, Q, Q_{H})$ and $h_1, \ldots, h_q$ runs $\adv$ by providing it with random oracle queries
and simulated proofs. While $Q_{H}$ is consistent with $h_1, \ldots, h_q$, it replays the proofs of $Q$.
%
$\zdv$ returns the index $i$ of the
  random oracle query made for challenge $k$ and the proof $\adv$ returns.

  Consider the algorithm $\genforking_{\zdv}^{n}$
  that rewinds $\zdv$ to produce a $(1,\dots, n,\dots, 1)$-tree of
  transcripts such that none of the $n$ challenges in round $k$ were used in
  simulated proofs.

  We say that $\ps$ is $(\eps(\secpar), k,n)$-forking sound if
  for any PPT adversary the probability that
  \begin{align*}
    \Pr\left[
    \REL(\inp, \wit) = 0
    \,\Biggl|\,
    \begin{aligned}
       & \srs \sample \kgen(\REL),
        r \sample \RND{\advse},
         (\inp_{\advse}, \zkproof_{\advse}) \gets \advse^{\simulator_\fs,\ro} (\srs; r), \\
       &    (1, \tree) \gets \genforking_{\zdv}^{m}((\srs,\adv,r,Q, Q_{H}),Q_{H}),
           \wit \gets \extt(\tree)
    \end{aligned}
    \right] \leq \eps(\secpar).
  \end{align*}
   List $Q$ contains all $(\inp, \zkproof)$ pairs where
  $\inp$ is an instance provided to the simulator by the adversary and
  $\zkproof$ is the simulator's answer. List $Q_\ro$ contains all $\advse$'s
  queries to $\ro$ and $\ro$'s answers.
\end{definition}

\hamid{next definition should be commented!}
\begin{definition}[$(\eps(\secpar), k,n)$-forking soundness]
  Let $\proofsystem = (\kgen, \prover, \verifier, \simulator)$ be an
  $(2 \mu + 1)$-message proof system for a relation $\REL$.

  Let $\tdv$, called tree creator, be the
  algorithm below that rewinds the $\ppt$ adversary $\advse^{\simulator_\fs,
          \ro} (\srs; r)$ to produce a $(1,\dots, n,\dots, 1)$-tree of
  transcripts such that none of the $n$ challenges in round $k$ were used in
  simulated proofs.

  $\tdv$ has oracle access to $\adv$ and provides it with (oracle) access to
  random oracle $\ro$ and simulator $\simulator_\fs$ -- more precisely $\tdv$
  has an internal procedure $\bdv$ that provided $\srs$ and random oracle
  queries' responses $h_1, \ldots, h_q$ gives $\adv$ access to the random oracle
  and simulates proof for it. In the end, $\bdv$ returns the index $i$ of the
  random oracle query made for challenge $k$, the set $Q$ of simulator random
  oracle indexes, the instance $\inp$, and the proof $\adv$ returns. Eventually,
  $\tdv$ returns a $(1, \ldots, n, \dots, 1)$ tree of acceptable
  transcripts~$\tree$.

  \begin{figure}
    \centering
 		\fbox{ \procedure{$\tdv(\adv, \srs \sample \kgen(\REL))$}
      {h_1^{1}, \ldots,
      h_{q}^1 \sample H \\
      (i, Q, \inp, \zkproof_1) \gets \bdv(\adv, \srs, h_1^{1}, \ldots, h_{q}^{1})\\
        % i_1 \gets i\\
        \pcif i\in Q \lor \verifier(\srs, \inp, \zkproof_1) = 0\ \pcreturn (0, \bot)\\
        \pcfor j \in \range{2}{m}\\
        \pcind h_{1}^{j}, \ldots, h_{i - 1}^{j} \gets h_{1}^{j - 1}, \ldots,
        h_{i - 1}^{j - 1}\\
        \pcind h_{i}^{j}, \ldots, h_{q}^{j} \sample H\\
        \pcind (i_j, Q_j, \inp_j, \zkproof_j) \gets \bdv(\adv, \srs, h_1^{j}, \ldots, h_{i -
          1}^{j}, h_{i}^{j},
        \ldots, h_{q}^{j})\\
        \pcind \pcif i \neq i_j \lor i_j \in Q_j \lor \inp \neq \inp_j \lor \verifier(\srs, \inp_j, \zkproof_i) = 0\ \pcreturn (0, \bot)\\
        %\pcind \pcif i_j = 0 \lor i_j \neq i\ \pcreturn (0, \bot)\\
        % \pcif \exists (j, j') \in \range{1}{m}^2, j \neq j' : (h_{i}^{j} =
        % h_{i}^{j'})\
        %
        % pcreturn (0, \bot)\\
        \pcelse \pcreturn (1, \tree = (\inp, \pmb{\pi}))}
    }
  \end{figure}
%
  We say that $\ps$ is $(\eps(\secpar), k,n)$-forking sound if
  for any PPT adversary the probability that
  \[
    \Pr\left[
    %  \begin{aligned}
       % & \forall_{\zkproof \in \tree} \verifier(\srs, \inp, \zkproof) = 1, \\
         \wit \gets \extt(\tree),
         \REL(\inp, \wit) = 0
    %  \end{aligned}
      \,\left|\,
        %\begin{aligned}
           \srs \sample \kgen(\REL),
           (1, \tree) \gets \tdv(\adv, \srs)
     %   \end{aligned}
      \right.\right] \leq \eps(\secpar).
      \]
\end{definition}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright (c) 2003-2018 by The University of Queensland
% http://www.uq.edu.au
%
% Primary Business: Queensland, Australia
% Licensed under the Apache License, version 2.0
% http://www.apache.org/licenses/LICENSE-2.0
%
% Development until 2012 by Earth Systems Science Computational Center (ESSCC)
% Development 2012-2013 by School of Earth Sciences
% Development from 2014 by Centre for Geoscience Computing (GeoComp)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

The acoustic wave equation governs the propagation of pressure waves. Wave
types that obey this law tend to travel in liquids or gases where shear waves
or longitudinal style wave motion is not possible. An obvious example is sound
waves.

The acoustic wave equation is defined as;
\begin{equation}
 \nabla ^2 p - \frac{1}{c^2} \frac{\partial ^2 p}{\partial t^2} = 0
\label{eqn:acswave}
\end{equation}
where $p$ is the pressure, $t$ is the time and $c$ is the wave velocity. In this
chapter the acoustic wave equation is demonstrated. Important steps include the
translation of the Laplacian $\nabla^2$ to the \esc general form, the stiff
equation stability criterion and solving for the displacement or acceleration solution.

\section{The Laplacian in \esc}
The Laplacian operator which can be written as $\Delta$ or $\nabla^2$,  is
calculated via the divergence of the gradient of the object, which in this
example is the scalar $p$. Thus we can write;
\begin{equation}
 \nabla^2 p = \nabla \cdot \nabla p =
	\sum_{i}^n
	\frac{\partial^2 p}{\partial x^2_{i}}
 \label{eqn:laplacian}
\end{equation}
For the two dimensional case in Cartesian coordinates \autoref{eqn:laplacian}
becomes;
\begin{equation}
 \nabla^2 p = \frac{\partial^2 p}{\partial x^2}
		   + \frac{\partial^2 p}{\partial y^2}
\end{equation}

In \esc the Laplacian is calculated using the divergence representation and the
intrinsic functions \textit{grad()} and \textit{trace()}. The function
\textit{grad{}} will return the spatial gradients of an object.
For a rank 0 solution, this is of the form;
\begin{equation}
 \nabla p = \left[
	   \frac{\partial p}{\partial x _{0}},
	   \frac{\partial p}{\partial x _{1}}
                  \right]
\label{eqn:grad}
\end{equation}
Larger ranked solution objects will return gradient tensors. For example, a
pressure field which acts in the directions $p _{0}$ and $p
_{1}$ would return;
\begin{equation}
  \nabla p = \begin{bmatrix}
	   \frac{\partial p _{0}}{\partial x _{0}} &
		\frac{\partial p _{1}}{\partial x _{0}} \\
	  \frac{\partial p _{0}}{\partial x _{1}} &
		\frac{\partial p _{1}}{\partial x _{1}}
                  \end{bmatrix}
\label{eqn:gradrank1}
\end{equation}

\autoref{eqn:grad} corresponds to the Linear PDE general form value
$X$. Notice however, that the general form contains the term $X
_{i,j}$\footnote{This is the first derivative in the $j^{th}$
direction for the $i^{th}$ component of the solution.},
hence for a rank 0 object there is no need to do more then calculate the
gradient and submit it to the solver. In the case of the rank 1 or greater
object, it is also necessary to calculate the trace. This is the sum of the
diagonal in \autoref{eqn:gradrank1}.

Thus when solving for equations containing the Laplacian one of two things must
be completed. If the object \verb!p! is less than rank 1 the gradient is
calculated via;
\begin{python}
gradient=grad(p)
\end{python}
and if the object is greater then or equal to a rank 1 tensor, the trace of
the gradient is calculated.
\begin{python}
 gradient=trace(grad(p))
\end{python}
These values can then be submitted to the PDE solver via the general form term
$X$. The Laplacian is then computed in the solution process by taking the
divergence of $X$.

Note, if you are unsure about the rank of your tensor, the \textit{getRank}
command will return the rank of the PDE object.
\begin{python}
 rank = p.getRank()
\end{python}

\section{Numerical Solution Stability} \label{sec:nsstab}
Unfortunately, the wave equation belongs to a class of equations called
\textbf{stiff} PDEs. These types of equations can be difficult to solve
numerically as they tend to oscillate about the exact solution, which can
eventually lead to a catastrophic failure. To counter this problem, explicitly
stable schemes like the backwards Euler method, and correct parameterisation of
the problem are required.

There are two variables which must be considered for
stability when numerically trying to solve the wave equation. For linear media,
the two variables are related via;
\begin{equation} \label{eqn:freqvel}
f=\frac{v}{\lambda}
\end{equation}
The velocity $v$ that a wave travels in a medium is an important variable. For
stability the analytical wave must not propagate faster then the numerical wave
is able to, and in general, needs to be much slower then the numerical wave.
For example, a line 100m long is discretised into 1m intervals or 101 nodes. If
a wave enters with a propagation velocity of 100m/s then the travel time for
the wave between each node will be 0.01 seconds. The time step, must therefore
be significantly less than this. Of the order $10E-4$ would be appropriate.
This stability criterion is known as the Courant\textendash
Friedrichs\textendash Lewy condition given by
\begin{equation}
dt=f\cdot \frac{dx}{v}
\end{equation}
where $dx$ is the mesh size and $f$ is a safety factor. To obtain a time step of
$10E-4$, a safety factor of $f=0.1$ was used.

The wave frequency content also plays a part in numerical stability. The
Nyquist-sampling theorem states that a signals bandwidth content will be
accurately represented when an equispaced sampling rate $f _{n}$ is
equal to or greater then twice the maximum frequency of the signal
$f_{s}$, or;
\begin{equation} \label{eqn:samptheorem}
 f_{n} \geqslant f_{s}
\end{equation}
For example, a 50Hz signal will require a sampling rate greater then 100Hz or
one sample every 0.01 seconds. The wave equation relies on a spatial frequency,
thus the sampling theorem in this case applies to the solution mesh spacing.
This relationship confirms that the frequency content of the input signal
directly affects the time discretisation of the problem.

To accurately model the wave equation with high resolutions and velocities
means that very fine spatial and time discretisation is necessary for most
problems.  This requirement makes the wave equation arduous to
solve numerically due to the large number of time iterations required in each
solution. Models with very high velocities and frequencies will be the worst
affected by this problem.

\section{Displacement Solution}
\sslist{example07a.py}

We begin the solution to this PDE with the centred difference formula for the
second derivative;
\begin{equation}
 f''(x) \approx \frac{f(x+h - 2f(x) + f(x-h)}{h^2}
\label{eqn:centdiff}
\end{equation}
substituting \autoref{eqn:centdiff} for $\frac{\partial ^2 p }{\partial t ^2}$
in \autoref{eqn:acswave};
\begin{equation}
 \nabla ^2 p - \frac{1}{c^2h^2} \left[p_{(t+1)} - 2p_{(t)} +
p_{(t-1)} \right]
= 0
\label{eqn:waveu}
\end{equation}
Rearranging for $p_{(t+1)}$;
\begin{equation}
 p_{(t+1)} = c^2 h^2 \nabla ^2 p_{(t)} +2p_{(t)} -
p_{(t-1)}
\end{equation}
this can be compared with the general form of the \modLPDE module and it
becomes clear that $D=1$, $X_{i,j}=-c^2 h^2 \nabla ^2 p_{(t)}$ and
$Y=2p_{(t)} - p_{(t-1)}$.

The solution script is similar to others that we have created in previous
chapters. The general steps are;
\begin{enumerate}
 \item The necessary libraries must be imported.
 \item The domain needs to be defined.
 \item The time iteration and control parameters need to be defined.
 \item The PDE is initialised with source and boundary conditions.
 \item The time loop is started and the PDE is solved at consecutive time steps.
 \item All or select solutions are saved to file for visualisation later on.
\end{enumerate}

Parts of the script which warrant more attention are the definition of the
source, visualising the source, the solution time loop and the VTK data export.

\subsection{Pressure Sources}
As the pressure is a scalar, one need only define the pressure for two
time steps prior to the start of the solution loop. Two known solutions are
required because the wave equation contains a double partial derivative with
respect to time. This is often a good opportunity to introduce a source to the
solution. This model has the source located at it's centre. The source should
be smooth and cover a number of samples to satisfy the frequency stability
criterion. Small sources will generate high frequency signals. Here, when using
a rectangular domain, the source is defined by a cosine function.
\begin{python}
U0=0.01 # amplitude of point source
xc=[500,500] #location of point source
# define small radius around point xc
src_radius = 30
# for first two time steps
u=U0*(cos(length(x-xc)*3.1415/src_radius)+1)*\
	whereNegative(length(x-xc)-src_radius)
u_m1=u
\end{python}

\subsection{Visualising the Source}
There are two options for visualising the source. The first is to export the
initial conditions of the model to VTK, which can be interpreted as a scalar
surface in \mayavi. The second is to take a cross section of the model which
will require the \textit{Locator} function.
First \verb!Locator! must be imported;
\begin{python}
 from esys.escript.pdetools import Locator
\end{python}
The function can then be used on the domain to locate the nearest domain node
to the point or points of interest.

It is now necessary to build a list of $(x,y)$ locations that specify where are
model slice will go. This is easily implemented with a loop;
\begin{python}
cut_loc=[]
src_cut=[]
for i in range(ndx/2-ndx/10,ndx/2+ndx/10):
    cut_loc.append(xstep*i)
    src_cut.append([xstep*i,xc[1]])
\end{python}
We then submit the output to \verb!Locator! and finally return the appropriate
values using the \verb!getValue! function.
\begin{python}
src=Locator(mydomain,src_cut)
src_cut=src.getValue(u)
\end{python}
It is then a trivial task to plot and save the output using \mpl
(\autoref{fig:cxsource}).
\begin{python}
pl.plot(cut_loc,src_cut)
pl.axis([xc[0]-src_radius*3,xc[0]+src_radius*3,0.,2*U0])
pl.savefig(os.path.join(savepath,"source_line.png"))
\end{python}
\begin{figure}[h]
 \centering
	\includegraphics[width=6in]{figures/sourceline.png}
 \caption{Cross section of the source function.}
 \label{fig:cxsource}
\end{figure}

\subsection{Point Monitoring}
In the more general case where the solution mesh is irregular or specific
locations need to be monitored, it is simple enough to use the \textit{Locator}
function.
\begin{python}
 rec=Locator(mydomain,[250.,250.])
\end{python}
 When the solution \verb u  is updated we can extract the value at that point
via;
\begin{python}
 u_rec=rec.getValue(u)
\end{python}
For consecutive time steps one can record the values from \verb!u_rec! in an
array initialised as \verb!u_rec0=[]! with;
\begin{python}
  u_rec0.append(rec.getValue(u))
\end{python}

It can be useful to monitor the value at a single or multiple individual points
in the model during the modelling process. This is done using
the \verb!Locator! function.

\section{Acceleration Solution}
\sslist{example07b.py}

An alternative method to the displacement solution, is to solve for the
acceleration $\frac{\partial ^2 p}{\partial t^2}$ directly. The displacement can
then be derived from the acceleration after a solution has been calculated
The acceleration is given by a modified form of \autoref{eqn:waveu};
\begin{equation}
  \nabla ^2 p - \frac{1}{c^2} a = 0
\label{eqn:wavea}
\end{equation}
and can be solved directly with $Y=0$ and $X=-c^2 \nabla ^2 p_{(t)}$.
After each iteration the displacement is re-evaluated via;
\begin{equation}
 p_{(t+1)}=2p_{(t)} - p_{(t-1)} + h^2a
\end{equation}

\subsection{Lumping}
For \esc, the acceleration solution is preferred as it allows the use of matrix
lumping. Lumping or mass lumping as it is sometimes known, is the process of
aggressively approximating the density elements of a mass matrix into the main
diagonal. The use of Lumping is motivated by the simplicity of diagonal matrix
 inversion. As a result, Lumping can significantly reduce the computational
requirements of a problem. Care should be taken however, as this
function can only be used when the $A$, $B$ and $C$ coefficients of the
general form are zero.

More information about the lumping implementation used in \esc and its accuracy
can be found in the user guide.

To turn lumping on in \esc one can use the command;
\begin{python}
 mypde.getSolverOptions().setSolverMethod(SolverOptions.HRZ_LUMPING)
\end{python}
It is also possible to check if lumping is set using;
\begin{python}
  print(mypde.isUsingLumping())
\end{python}

\section{Stability Investigation}
It is now prudent to investigate the stability limitations of this problem.
First, we let the frequency content of the source be very small. If we define
the source as a cosine input, then the wavlength of the input is equal to the
radius of the source. Let this value be 5 meters. Now, if the maximum velocity
of the model is $c=380.0ms^{-1}$, then the source
frequency is $f_{r} = \frac{380.0}{5} = 76.0 Hz$. This is a worst case
scenario with a small source and the models maximum velocity.

Furthermore, we know from \autoref{sec:nsstab}, that the spatial sampling
frequency must be at least twice this value to ensure stability. If we assume
the model mesh is a square equispaced grid,
then the sampling interval is the side length divided by the number of samples,
given by $\Delta x = \frac{1000.0m}{400} = 2.5m$ and the maximum sampling
frequency capable at this interval is
$f_{s}=\frac{380.0ms^{-1}}{2.5m}=152Hz$ this is just equal to the
required rate satisfying \autoref{eqn:samptheorem}.

\autoref{fig:ex07sampth} depicts three examples where the grid has been
undersampled, sampled correctly, and over sampled. The grids used had
200, 400 and 800 nodes per side respectively. Obviously, the oversampled grid
retains the best resolution of the modelled wave.

The time step required for each of these examples is simply calculated from
the propagation requirement. For a maximum velocity of $380.0ms^{-1}$,
\begin{subequations}
 \begin{equation}
  \Delta t \leq \frac{1000.0m}{200} \frac{1}{380.0} = 0.013s
 \end{equation}
 \begin{equation}
  \Delta t \leq \frac{1000.0m}{400} \frac{1}{380.0} = 0.0065s
 \end{equation}
 \begin{equation}
  \Delta t \leq \frac{1000.0m}{800} \frac{1}{380.0} = 0.0032s
 \end{equation}
\end{subequations}
Observe that for each doubling of the number of nodes in the mesh, we halve
the time step. To illustrate the impact this has, consider our model. If the
source is placed at the center, it is $500m$ from the nearest boundary. With a
velocity of $380.0ms^{-1}$ it will take $\approx1.3s$ for the wavefront to
reach that boundary. In each case, this equates to $100$,  $200$ and $400$ time
steps. This is again, only a best case scenario, for true stability these time
values may need to be halved and possibly halved again.

\begin{figure}[ht]
\centering
\subfigure[Undersampled Example]{
\includegraphics[width=0.45\textwidth,trim=0cm 6cm 5cm 6cm,clip]{figures/ex07usamp.png}
\label{fig:ex07usamp}
}
\subfigure[Just sampled Example]{
\includegraphics[width=0.45\textwidth,trim=0cm 6cm 5cm 6cm,clip]{figures/ex07jsamp.png}
\label{fig:ex07jsamp}
}
\subfigure[Over sampled Example]{
\includegraphics[width=0.45\textwidth,trim=0cm 6cm 5cm 6cm,clip]{figures/ex07nsamp.png}
\label{fig:ex07nsamp}
}
\caption{Sampling Theorem example for stability investigation}
\label{fig:ex07sampth}
\end{figure}