\documentclass[10pt]{article}
\usepackage{cite}
\usepackage{a4}

\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{amsmath,amssymb,amsthm}

\usepackage{url}
\usepackage{pgf}
\usepackage{tikz}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% layout commands
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newtheorem{thm}{Theorem}[section]
\newtheorem{lem}[thm]{Lemma}

\newcommand{\prog}[3]{\langle #1, #2, #3\rangle}
\newcommand{\slp}[1]{\mathbb{#1}}
\newcommand{\slpterm}[3]{\slp{#1}_{#2} = #3}
\newcommand{\slpnonterm}[4]{\slp{#1}_{#2} = \slp{#1}_{#3} \cdot \slp{#1}_{#4}}
\newcommand{\substr}[2]{[#1\dots#2]}
\newcommand{\subslp}[3]{\slp{#1}[#2\dots#3]}

\newcommand{\problem}[3]{
            \noindent {\sc Problem:} \textbf{#1} \newline
            \noindent {\sc Input:} #2 \newline
            \noindent {\sc Output:} #3
}

\renewcommand{\emptyset}{
    \font\msbm = msbm10 at 12pt
    \mbox{\msbm \char 63}
}

\begin{document}

\title{Computing All Squares in Compressed Texts}
\author{Lesha Khvorost\thanks{The author acknowledges support from the Russian Foundation for Basic Research, grant
10-01-00793.}\\
Ural Federal University\\
jaamal@mail.ru}
\date{}
\maketitle

\begin{abstract}
We consider the problem of computing all squares in a string represented by a straight-line program (SLP). An
instance of the problem is an SLP $\slp{S}$ that derives some string $S$ and we seek a solution in the form of a table
that contains information about all squares in $S$ in a compressed form. We present an algorithm that solves the
problem in $O(|\slp{S}|^4\cdot \log^2{|S|})$ time and requires $O(|\slp{S}|\cdot \max\{ |\slp{S}|, \log{|S|}\})$ space,
where $|\slp{S}|$ (respectively $|S|$) is the size of the SLP $\slp{S}$ (respectively the length of the string $S$).
\end{abstract}

\section{Introduction}

Various compressed representations of strings are known: straight-line programs (SLPs),
collage-systems, string representations using antidictionaries, etc. Nowadays text compression
based on context-free grammars such as SLPs attracts much attention. The reason for this is not only that grammars
provide well-structured compression but also that the SLP-based compression is in a sense polynomially equivalent
to the compression achieved by the Lempel-Ziv algorithm that is widely used in practice. It means that, given a
string $S$, there is a polynomial relation between the size of an SLP that derives $S$ and the size of the
dictionary stored by the Lempel-Ziv algorithm \cite{18}.

While compressed representations save storage space, there is a price to pay: some classical problems on strings
become computationally hard when one deals with compressed data and measures algorithms' speed in terms of the
size of compressed representations. As examples we mention here the problems \textbf{Hamming distance} \cite{15} and
\textbf{Literal shuffle} \cite{4}. On the other hand, there exist problems that admit algorithms working rather well on
compressed representations: \textbf{Pattern matching} \cite{15}, \textbf{Longest common substring} \cite{16},
\textbf{Computing all palindromes} \cite{16}. This dichotomy gives rise to the following research direction: to
classify important string problems by their behavior with respect to compressed data.

The \textbf{Computing All Squares} (\textbf{CAS}) problem is a well-known problem on strings. It is of importance, for
example, in molecular biology. Up to recently it is was not known whether or not \textbf{CAS} admits an algorithm polynomial
in the size of a compressed representation of a given string.\footnote{A polynomial algorithm that solves \textbf{CAS}
for strings represented by Lempel-Ziv encodings was announced in~\cite{8}. This representation is slightly more general
than that by SLPs. However no details of the algorithm have ever been appeared.} In general, a string can have
exponentially many squares with respect to the size of its compressed representation. For example, the string $a^n$
has $\Theta(n^2)$ squares, while it is easy to build an SLP of size $O(\log n)$ that derives $a^n$. So we must
store information about squares in a compressed form. Also this implies that we cannot search for squares
consecutively by moving from one square to the ``next'' one.

\section{Preliminaries}

We consider strings of characters from a fixed finite alphabet $\Sigma$. The \emph{length} of a string $S$ is the number
of its characters and is denoted by $|S|$. The \emph{concatenation} of strings $S_1$ and $S_2$ is denoted by $S_1 \cdot
S_2$.  A \emph{position} in a string $S$ is a point between consecutive characters. We number positions from left to
right by $1,2,\dots,|S|-1$. It is convenient to consider also the position 0 preceding the text and the position $|S|$
following it. For a string $S$ and an integer $i$ where $0 \leq i \leq |S|$ we define $S[i]$ as the character between the positions
$i$ and $i+1$ of $S$. A \emph{substring} of $S$ starting at a position $\ell$  and ending at a position $r$ where $0\leq \ell < r \leq |S|$
is denoted by $S[\ell \dots r]$. We say that a substring $S[\ell \dots r]$ \emph{touches} a position $t$ if $\ell \leq t \leq r$.
A string is called a \emph{square} if it can be obtained
by concatenating two copies of some string. The position $|x|$ of a square $xx$ is called the \emph{center} of $xx$ and
$x$ is referred to as the \emph{root} of $xx$. A square $xx$ is called \emph{pure} if $x$ occurs exactly two times in
$xx$. A string $S$ is called \emph{p-periodic} if for a given integer $p$ and every position $i$ such that $0 \leq i
\leq |S| - p$ one has $S[i] = S[i + p]$. The integer $p$ is called the \emph{period} of $S$.

A \emph{straight-line program} (SLP) $\slp{S}$ is a sequence of assignments of the form:
$\slp{S}_1 = expr_1,\ \slp{S}_2 = expr_2, \dots, \slp{S}_n = expr_n,$ where $\slp{S}_i$ are \emph{rules} and $expr_i$
either is a symbol of $\Sigma$ (we call such rules \emph{terminal}), or $expr_i = \slp{S}_\ell\cdot \slp{S}_r \ (\ell, r < i)$
(we call such rules \emph{nonterminal}). Every SLP $\slp{S}$ generates exactly one string $S\in\Sigma^+$ and
we refer to $S$ as the \emph{text} generated by $\slp{S}$.

We adopt the following conventions in the paper: every SLP is denoted by a capital blackboard bold letter, for
example, $\slp{S}$. Every rule of this SLP is denoted by the same letter with indices, for example,
$\slp{S}_1,\slp{S}_2,\dots$. The text that is derived from a rule is denoted by the same indexed capital letter in
the standard font, for example, the text that is derived from $\slp{S}_i$ is denoted by $S_i$. The \emph{size} of an SLP $\slp{S}$ is
the number of its rules and is denoted by $|\slp{S}|$. The \emph{cut position} of a nonterminal rule
$\slp{S}_i = \slp{S}_\ell\cdot \slp{S}_r$ is the position $|S_\ell|$ in the text $S_i$.

\section{Basic operations}

In this section we present some basic operations over SLPs widely used in the paper. The operations are well-known and presented
here for the reader's convenience.

\vspace{5pt}

\problem{Subgrammar cutting (SubCut)}
{an SLP $\slp{S}$ that derives a text $S$, integers $\ell$ and $r$ such that $0 \leq \ell < r \leq |\slp{S}|$.}
{an SLP $\slp{S}\substr{\ell}{r}$ which derives the text $S\substr{\ell}{r}$.}

\noindent \textsc{Complexity:} There is an algorithm that solves \textbf{SubCut} using $O(|\slp{S}|)$ time and $O(|\slp{S}|)$ space.

\vspace{5pt}

\problem{Pattern matching (PM)}
{SLPs $\slp{S}$ and $\slp{T}$ that derives texts $S$ and $T$ respectively, $|S| \leq |T|$.}
{$O(|\slp{T}|)$ arithmetic progressions that describe the start positions of all occurrences of $S$ in $T$.}

\noindent \textsc{Complexity:} There is an algorithm \cite{15} that solves \textbf{PM} using $O(|\slp{T}|^2|\slp{S}|)$ time and
$O(|\slp{T}||\slp{S}|)$ space.

\vspace{5pt}

\problem{Substrings extending (SubsExt)}
{an SLP $\slp{S}$, integers $\ell_1, r_1, \ell_2$ and $r_2$ where $0 \leq \ell_1 < r_1 \leq |\slp{S}|, 0
\leq \ell_2 < r_2 \leq |\slp{S}|$ such that $S\substr{\ell_1}{r_1} = S\substr{\ell_2}{r_2}$.}
{integers $\ell_{ex}$ and $r_{ex}$ where $\ell_{ex}$ is the length of the longest common suffix of $S\substr{1}{r_1}$ and $S\substr{1}{r_2}$,
$r_{ex}$ is the length of the longest common prefix of $S\substr{\ell_1}{|S|}$ and $S\substr{\ell_2}{|S|}$.}

\noindent \textsc{Complexity:} There is an algorithm that solves \textbf{SubsExt} using $O(|\slp{S}|^3 \log|S|)$ time
and $O(|\slp{S}|^2)$ space.

\vspace{5pt}

\problem{Period termination}
{an SLP $\slp{S}$, an integer $p > 0$ and positions $\ell, r$ where $0 \leq \ell < r \leq |\slp{S}|$ such that $S\substr{\ell}{r}$ is $p$-periodic substring.}
{positions $t_L, t_R$ in the text $S$ where $p$-periodicity of $S\substr{\ell}{r}$ terminates from the left and from the right correspondingly.
It means that $S\substr{t_L}{r}$ is $p$-periodic while $S\substr{t_L+1}{r}$ is not $p$-periodic.}

\noindent \textsc{Complexity:} There exists an algorithm that solves \textbf{Period termination} using $O(|\slp{S}|^3 \log|S|)$ time
and $O(|\slp{S}|^2)$ space.

\section{Square-freeness checking algorithm}

\problem{Square-freeness}{an SLP $\slp{S}$ that derives a text $S$;}{ true/false, whether or not $S$ is square-free?}

\vspace{5pt}

\noindent \textbf{Squares location idea.} Suppose $xx$ is a square that occurs in $S$ and $c$ is the center of $xx$.
There is a unique rule $\slp{S}_j$ such that $xx$ fully occurs in $S_j$ and $xx$ touches the cut position of $\slp{S}_j$.
So for every integer $j \in \{1, 2, \dots, |\slp{S}|\}$ the algorithm looks for squares that touch the cut position of $\slp{S}_j$ only.

We use the following simple idea to obtain squares. If $B$ is a substring of $x$ then $B$ occurs at least two times in
$xx$. So the algorithm may fix some substring $B$ and look for other occurrences of $B$. Every pair of occurrences of
$B$ indicates that some squares are possible. Hence the algorithm has an additional step to check whether or
not a pair of substrings indeed corresponds to squares. We would like to note that such an algorithm is unable to find
all squares that touches the cut position of $\slp{S}_j$ directly. The algorithm partitions all squares into groups by length of
root. There is a unique integer $i_0$ such that $2^{i_0-1} \leq |x| < 2^{i_0}$. So for a fixed integer $j$ and every integer
$i \in \{1, \dots, \lceil \log_2 |S| \rceil\}$ the algorithm looks for squares $xx$ that touch the cut position of $\slp{S}_j$ and
$2^{i-1} \leq |x| < 2^i$.

\vspace{5pt}

\noindent \textbf{Local search idea.} Suppose $j$ and $i$ are fixed. Let $\gamma$ be the cut position of $\slp{S}_j$.
The algorithm partitions the $2^{i+1}$-neighborhood of $\gamma$ into 16 text blocks of equal length. Thus the length of each block is
equal to $2^{i-2}$. Let us enumerate the blocks from $B_1$ to $B_{16}$. For example, the left block that
touches $\gamma$ is $B_8$, the block left of $B_8$ is $B_7$ and the block right of $B_8$ is $B_9$. Depending
on the length of $S_j$ the neighborhood may contain less than 16 blocks or extreme blocks may have lengths less than
$2^{i-2}$. For squares whose centers locate at the blocks $B_{1}, B_{2}, \dots, B_{4}$ and $B_{13}, B_{14}, \dots, B_{16}$
we get a contradiction with restrictions on length of squares or with the condition that squares touch $\gamma$.
Therefore the algorithm looks for squares whose centers locate at blocks $B_{5}, B_{6}, \dots, B_{12}$ only.

Let $c$ be the center of a square $xx$ and lec $c$ belong to one of the eight central blocks $B_k$. Since $2^{i-1}
\leq |x|$, we conclude that $xx$ contains at least three consecutive blocks $B_{k-1}, B_{k}$ and $B_{k+1}$. All occurrences of $B_{k-1}$
in $B_{k+1}\cdot B_{k+2}$ can be obtained using the \textbf{PM} algorithm. So the algorithm takes every pair consisting of a block
$B_{k-1}$ and its occurrence in $B_{k+1}\cdot B_{k+2}$ and extends them to check whether or not they form any square.

\begin{lem}[{\rm\cite{2}}]
\label{lem:blocks}
Assume that the period of a string $B$ is $p$. If $B$ occurs only at positions $p_1 < p_2 < \dots < p_k$ of a text
$S$ and $p_k - p_1 \leq \frac{|B|}{2}$ then the $p_i$'s form an arithmetic progression with difference $p$.
\end{lem}

\noindent \textsc{Algorithm:} For every block $B_{k-1}$ the algorithm computes $\slp{B}_{k-1}$ and $\slp{B}_{k+1} \cdot \slp{B}_{k+2}$
using \textbf{SubCut}. Next it invokes \textbf{PM} with $\slp{B}_{k-1}$ and $\slp{B}_{k+1} \cdot \slp{B}_{k+2}$.
Lemma~\ref{lem:blocks} implies that the occurrences can be represented using at most four arithmetic
progressions. The algorithm compresses the occurrences into four arithmetic progressions. Finally it verifies whether or
not $B_{k-1}$ and a progression $\prog{a}{p}{t}$ of its occurrences form any square. There are the following cases:

\begin{itemize}
\item If $t = 0$ then there are no squares that satisfy the restrictions and the algorithm moves to the next block;

\item If $t = 1$ then the algorithm computes $\ell_{ex}$, $r_{ex}$ invoking \textbf{SubsExt} with $B_{k-1}$ and $S_j[a\dots
a + 2^{i-2}]$. If $\ell_{ex} + r_{ex} > a - (k-1)\cdot 2^{i-2}$ then there exists at least one square and the algorithm
returns $\bf{false}$. Otherwise there are no squares that satisfy the restrictions and it moves to the next block;

\item If $t \geq 2$ there exists at least one square and the algorithm returns $\bf{false}$.
\end{itemize}

\noindent \textsc{Complexity:} For each of the eight central blocks the algorithm invokes \textbf{SubCut} two times,
\textbf{PM} once and \textbf{SubsExt} at most four times. Hence the main step requires $O(|\slp{S}|^3 \cdot \log |S|)$ time
and $O(|\slp{S}|^2)$ space. The algorithm makes at most $|\slp{S}| \cdot \log |S|$ steps. Altogether we get the following theorem:

\begin{thm}
There is an algorithm that solves square-freeness problem using $O(|\slp{S}|^4\cdot \log^2{|S|})$ time and
$O(|\slp{S}|^2)$ space.
\end{thm}

\section{S-table and its properties}

The \emph{squares table} (shortly S-table) is a rectangular table $S(\slp{S})$ that holds information about all
squares in the text in a compressed form. The size of $S(\slp{S})$ is equal to $(\lfloor\log |S|\rfloor+1) \times
(|\slp{S}|+1)$. It is convenient to start numbering rows and columns of S-tables with 0. We denote the cell in the
$i$-th row and $j$-th column of table by $S(i,j)$. The cell $S(0,0)$ is always left blank. The cells $S(0,j)$ with
$j>0$ hold the rules of the SLP $\slp{S}$ ordered such that the lengths of the texts they derive increase. (If some
rules derive texts of the same length then the rules are listed in an arbitrary but fixed order). Thus, the first cells
of the 0-th row hold terminal rules followed by rules that derive texts of length~2, etc. The cells $S(i,0)$ with
$i>0$ hold segments $[2^{i-1}, 2^i - 1]$. Every cell $S(i, j)$ with $i,j>0$ holds information about families of squares.

There exist four types of families: an empty family (stored as $\emptyset$), a simple family of squares (stored using
4-tuple $\{|x|, p, c_\ell, c_r \}$ where $|x|$ is the length of root, $c_\ell$ is the position of the leftmost square,
$c_r$ is the center position of the rightmost square), a dynamic family of pure squares, a dynamic family of squares.
Both types of dynamic families contain squares with different root lengths and different centers. We are unable to master
an explicit compressed representation for the dynamic families that would be easy to handle with. This fact restricts the range of problems
that we can solve by usage of S-tables. But both dynamic families support the following properties:
\emph{total} (total number of squares that is contained in the family), \emph{reduction by length of root}
(for a fixed value of $|x|$, this property reduces a dynamic family to an array of simple families),
\emph{reduction by position} (for a fixed position $\ell$ of $S$ this property returns range of roots of squares that start from $\ell$).

Using information from an S-table it is easy to find information about all squares of fixed length, to compute total number of squares
that are contained in $S$, to find information about all squares that start from a fixed position, to find the longest square in $S$,
to check whether on not a text $S$ is square-free. At the same time there are problems that are not easy to decide via S-tables.
As an example one can take the following problem: for a given $\slp{S}$ that derives a text $S$ to construct an SLP that derives
the concatenation of all squares that are contained in $S$.
The example shows that an S-table accumulates a quantitative information about squares rather than information
appropriate for searching some fixed squares. It is a common feature of algorithms over SLPs that accumulate information
about all objects of a specified type. The similar situation appears in \textbf{Computing all palindromes}~\cite{16}.

\section{Computing all squares algorithm}

\problem{CAS}{an SLP $\slp{S}$ that derives a text $S$;}{a data structure (an S-table) that contains
information about all squares in $S$ in a compressed form.}

\noindent \textsc{Algorithm:} To solve the problem it remains to recognize all squares among a block $B_k$ and an
arithmetic progression $\prog{a}{p}{t}$ of its occurrences. Since $t$ can be exponentially large relative to
size of $\slp{S}$, the algorithm cannot consecutively check every occurrence of $B_k$.

Let $\alpha_L, \alpha_R$ be the output of \textbf{Period termination} for $\slp{S}_j, B_k =
S_j\substr{(k-1)\cdot2^{i-2}}{k\cdot 2^{i-2} -1}$. The positions $\alpha_L, \alpha_R$ are called \emph{defined} if they satisfy the
following inequalities: $(2k-1)\cdot2^{i-2} - (a + p\cdot t) \leq \alpha_L$, $\alpha_R < a + 2^{i-2}$.
Otherwise they are called \emph{undefined}. Since $2^{i} - 1$ is the greatest length of a root, the start
positions of pure squares cannot be further right than $(2k-1)\cdot2^{i-2} - (a + p\cdot t)$. Analogously let $\gamma_L, \gamma_R$ be the output of
\textbf{Period termination} for $\slp{S}_j, S_j\substr{a}{a + p\cdot t}$. The positions $\gamma_L, \gamma_R$ are called
\emph{defined} if they satisfy the following inequalities: $(k-1)2^{i-2} \leq \gamma_L$ and $\gamma_R < 2(a + p\cdot
t) - (k-1)2^{i-2}$. Otherwise they are called \emph{undefined}. The following lemmas present crucial relations between
$\alpha_L, \alpha_R, \gamma_L$ and $\gamma_R$.

\begin{lem}[{\rm\cite{2}}]
If one of $\alpha_R$ or $\gamma_L$ is defined then the other one is defined as well and $\alpha_R - \gamma_L \leq p$.
\end{lem}

\noindent \textbf{Case 1: both {\boldmath $\alpha_R$} and {\boldmath $\gamma_L$} are defined.}

\begin{lem}[{\rm\cite{2}}]
If both $\alpha_R, \gamma_L$ are defined then:

\begin{enumerate}
\item Squares that contain $B_k$ and are centered at positions $h$ such that $h \leq \gamma_L$ may exist only if
$\alpha_L$ is defined. These squares constitute a family of squares that corresponds to the difference
$|x| = a + t'\cdot p - (k-1)2^{i-2}$, provided that there exists some $t' \in \{0\dots t\}$ such that
$\gamma_L - \alpha_L = a + t'\cdot p - (k-1)2^{i-2}$.
\item Squares that contain $B_k$ and are centered at positions $h$ such that $\alpha_R < h$ may exist only if
$\gamma_R$ is defined. These squares constitute a family of squares that corresponds to the difference
$|x| = a + t''\cdot p - (k-1)2^{i-2}$, provided that there exists some $t'' \in \{0\dots t\}$ such that
$\gamma_L - \alpha_L = a + t''\cdot p - (k-1)2^{i-2}$.
\end{enumerate}
Notice that if $\alpha_R < \gamma_L$, then squares whose center $h$ satisfies $\alpha_R < h \leq\gamma_L$ may
exist only if both $\alpha_L$ and $\gamma_R$ are defined and $\gamma_R - \alpha_R = \gamma_L - \alpha_L$.
\label{lem:simple_squares}
\end{lem}

Using Lemma~\ref{lem:simple_squares} the algorithm finds simple families of squares.
If $\alpha_R < \gamma_L$ the algorithm may find at most three simple families of squares:
$\{\gamma_L - \alpha_L, p, k \cdot 2^{i-2}, \alpha_R\}, \{\gamma_R - \alpha_R, p, \alpha_R + 1, \gamma_L\},
\{\gamma_L - \alpha_L, p, \gamma_L + 1, \min\{a,(k+1) \cdot 2^{i-2}\}\}$. Otherwise it may find at most two simple
families of squares: $\{\gamma_L - \alpha_L, p, k \cdot 2^{i-2}, \gamma_L - 1\}, \{\gamma_R - \alpha_R, p, \alpha_R,
\min\{a,(k+1) \cdot 2^{i-2}\}\}$.

\begin{lem}[{\rm\cite{2}}]
If $\alpha_R, \gamma_L$ are defined and $\gamma_L < \alpha_R$, then there might be a family of squares
associated with each of the differences $|x| = a + p\cdot t' - (k-1)\cdot 2^{i-2}$ where $t' \in \{0\dots t\}$
with centers at positions $h$ such that $\gamma_L < h \leq \alpha_R$. The squares in each such family are all
pure squares and they are centered at positions $h$ such that $\max(\alpha_L + |x|, \gamma_L) < h \leq
\min(\alpha_R, \gamma_R-|x|)$. Notice that such a family is not empty only if $|x| < \min(\alpha_R-\alpha_L,
\gamma_R-\gamma_L)$.
\label{lem:dynamic_pure_squares}
\end{lem}

Using Lemma~\ref{lem:dynamic_pure_squares} the algorithm finds at most one dynamic family of pure squares and stores
it in the following way: $\{k, \prog{a}{p}{t}, \alpha_L, \alpha_R, \gamma_L, \gamma_R\}$.

\noindent \textbf{Case 2: both {\boldmath $\alpha_R$} and {\boldmath $\gamma_L$} are undefined.}
$S\substr{\alpha_L}{\gamma_R}$ is $p$-periodic and contains squares with different centers and different lengths of
roots. The algorithm accumulates all squares into a single dynamic family of squares that is stored in the following way: $\{k,
\prog{a}{p}{t}, \alpha_L, \gamma_R\}$.

\noindent \textsc{Complexity:} For each central block the algorithm computes four arithmetic progressions that describe all
occurrences of the block. So the algorithm invokes \textbf{SubCut} two times and \textbf{PM} once. For each arithmetic progression and the
block, the algorithm calculates $\alpha_L, \alpha_R, \gamma_L$ and $\gamma_R$. So the algorithm invokes
\textbf{Period termination} two times. After that it extracts families of squares and adds them to the S-table. Totally at
the main step the algorithm invokes \textbf{SubCut} at most 16 times, \textbf{PM} at most 8 times and \textbf{Period termination} at
most 64 times. The main step requires $O(|\slp{S}|^3 \cdot \log |S|)$ time and $O(|\slp{S}|^2)$ space. The algorithm
makes $|\slp{S}| \cdot \log |S|$ steps. Altogether we get the following theorem:

\begin{thm}
There is an algorithm that solves Computing all squares problem using $O(|\slp{S}|^4 \cdot \log^2{|S|})$ time and
$O(|\slp{S}| \cdot \max(|\slp{S}|, \log{|S|}))$ space.
\end{thm}

\section{Conclusion}

We have presented an algorithm that for a given SLP $\slp{S}$ deriving a text $S$ fills out a table containing
information about all squares that occur in $S$ in time $O(|\slp{S}|^4\cdot \log^2{|S|})$ using
$O(|\slp{S}|\cdot \max\{ |\slp{S}|, \log{|S|}\})$ space.

We emphasize main features of the algorithm. The algorithm presents a new technique for SLPs processing.
It is divided into independent steps in contrast to classical algorithms in this area which consecutively accumulate information.
As a result it can be parallelized. The algorithm is quite difficult for practical implementation.
It is not excluded that constants hidden in the "O" notation are actually very big. The present upper bound for the time complexity
is rather high and is not matched by any known lower bound. The question whether the upper bound can be lowered to cubic in $|\mathbb{S}|$
remains open.

{\footnotesize
\bibliography{cas_problem_rufidim}{}
\bibliographystyle{plain}
}

\end{document}


\documentclass[main_montangero.tex]{subfiles}
\begin{document}

\section{Time-dependent perturbation theory}

We have an atom or some quantum system, and we manipulate it by sending an EM pulse against it. How will it change?

Say we have some discrete energy levels, and a continuous spectrum after some threshold energy.
The photon can excite the ground state \(\ket{i} \) to some excited state \(\ket{f} \), or vice versa.
This can happen between discrete energies, or with the continuous energies.

We need to solve the time-dep Schrödinger equation:

\begin{equation}
    i \hbar \dv{}{t} \ket{\psi(t)} = \qty[H_0 + \lambda W(t)]\ket{\psi(t)}
\end{equation}

We work with the unperturbed eigenstates \(H_0 \ket{\varphi_n}  = E_n \ket{\varphi_n}  \), we assume \(\lambda \ll 1\),
and we say that the starting state at \(t=0\) is an unperturbed eigenstate.

What is the probability of getting a state \(f\)?

\begin{equation}
    \P _{if}(t) = \abs{\braket{\varphi_f}{\psi(t)} }^2
\end{equation}

we can get resonance.
For continuous states this changes a bit but we can generalize.

We expand

\begin{equation}
    \ket{\psi(t)} = \sum c_n(t) \ket{\varphi_n}   \qquad c_n(t) = \braket{\varphi_n}{\psi(t)}
\end{equation}

We can compute the matrix elements of the perturbation: \(\bra{\varphi_n} W(t) \ket{\varphi_k} = W_{nk}(t)\) and of the Hamiltonian:
\(\bra{\varphi_n} H_0 \ket{\varphi_k} = \delta_{nk} E_n\). Putting these in the Schrödinger equation, we get

\begin{equation}
    i \hbar \dv{}{t} c_n(t) = E_n c_n(t) + \sum _{k} \lambda W_{nk} c_k (t)
\end{equation}

We go in interaction picture: \(b_n(t) = c_n(t) \exp(+i E_n t / \hbar) \), which nullifies the unperturbed evolution.
Putting this inside the equation we get

\begin{equation}
    i \hbar \dot{b}_n(t) = \lambda \sum _k \exp(i \omega_{nk}) W_{nk} b_k (t)
\end{equation}

with \(\omega_{nk = (E_n - E_k) / \hbar}\). We can expand the \(b_n(t)\) in \(\lambda\):

\begin{equation}
    b_n(t) = \sum _{i}  b_n ^{(i)} \lambda^i (t)
\end{equation}

We know that for some \(i\) we must have \(b_n ^{(r)}(t=0) = \delta_{ni} \delta_{r0} \) and so:

\begin{align}
    i \hbar \dot{b}_n ^{(0)}(t) &= 0  \\
    i \hbar \dot{b}_n ^{(r)}(t) &= \sum _{k} \exp(i \omega_{nk} t) W_{nk} b_n ^{r-1}(t)
\end{align}

Then to order \(\lambda^1\):

\begin{equation}
    i \hbar \dot{b}_n (t) = \exp(i \omega_{nk}t) W_{nk}(t)
\end{equation}

\begin{equation}
    b_n^{(1)}(t) = \frac{1}{i \hbar} \int _{0}   ^{t} \dd{\tau} \exp(i \omega_{ni} \tau)W_{ni}(\tau)
\end{equation}

So \(\P _{if}(t) = \abs{c_f(t)}^2 = \abs{b_f(t)}^2\)

\begin{equation}
    \P _{if}(t) = \frac{\lambda^2}{\hbar^2} \abs{\int _{0}   ^{t} \dd{\tau} \exp(i \omega_{fi} \tau)W_{gi}(\tau)  }^2
\end{equation}

\paragraph{Example}

Let us take a \(W(t) = - W \sin(\omega t)\). Then,

\begin{equation}
    b_n^{(1)}(t) = - \frac{W_{ni}}{2 \hbar} \int _{0}   ^{t} \dd{\tau} \qty(\exp(i (\omega_{ni} - \omega) \tau) - \exp(-i (\omega_{ni} - \omega) \tau))
\end{equation}

So,

\begin{equation}
    \P _{if} (t, \omega) = \frac{\abs{W_{if}}^2 }{4 \hbar^2} F(t, \omega - \omega_{fi})
\end{equation}

where

\begin{equation}
    F(\omega, t) = \qty(\frac{\sin(\omega t/2)}{\omega/2})^2
\end{equation}

This is a sinc squared.

\subsection{Fermi's golden rule}

Is applying what we saw to continuous spectrums. Now we have some \(\braket{\alpha}{\alpha'} = \delta(\alpha - \alpha') \). We want to see what is the probability of the final state being in a neighbourhood of \(\alpha\).

We define the state density by \(\dd{\alpha} = \rho(\beta, E ) \dd{\beta} \dd{E} \) where the index \(\beta\) takes account of the degeneracy, which can be continuous.

\begin{equation}
    \delta \P \qty(\alpha_f, t) = \int   \dd{\alpha} \abs{\braket{\alpha}{\psi(t)} }^2
\end{equation}

where \(\alpha \in D_f\). So, we can change variable into

\begin{equation}
    \delta \P \qty(\alpha_f, t) = \int  \abs{\braket{\beta, E}{\psi(t)} }^2 \rho(E, \beta)  \dd{E} \dd{\beta}
\end{equation}

We can write

\begin{equation}
    \abs{\braket{\beta, E}{\psi(t)} }^2
    = \frac{1}{\hbar^2} \abs{\bra{\beta, E} W \ket{\varphi_i}  }^2 F\qty(t, \frac{E-E_i}{\hbar})
\end{equation}

where \(\lim_{t \rightarrow \infty} F\qty(t, \frac{E-E_i}{\hbar}) = 2 \pi i \hbar \delta(E - E_i)\).
Then

\begin{equation}
    \delta \P (\varphi_i, \alpha_f, t)
    = \delta \beta_f \frac{2 \pi i t}{\hbar} \abs{\bra{\beta_f, E_f = E_i} W \ket{\varphi_i}}^2
    \rho(\beta, E_f)
\end{equation}

Then

\begin{equation}
    W(\varphi_i, \alpha_f) = \dv{}{t} \fdv{\P}{\beta} = \frac{\pi}{2 \hbar}
    \abs{\bra{\beta_f, E_f = E_i + \hbar \omega} W \ket{\varphi_i}}^2
    \rho (\beta_f, E_f = E_i + \hbar \omega)
\end{equation}

The more states we have in the final configuration, the greater the probability. We are assuming here that the state density is constant in the degeneracy, and we can split the degeneracy and energy contributions.

\section{How to build a quantum computer}

Di Vincenzo criteria (2000): what is needed to have a true quantum computer.

\begin{enumerate}
    \item Scalability, well defined qubit;
    \item Reset: we must be able to reset the computer surely into a state \(\ket{0} \);
    \item Long coherence time wrt gate duration;
    \item Universal set of gates;
    \item Efficient readout;
\end{enumerate}

If our decoherence time is \(\tau_d\) and our gate time is \(\tau_g\), we need \(\tau_d / \tau_g \gtrsim 10^4\) in order to have time to do error correction.

Proposals:

\begin{itemize}
    \item Cauidiy: QED;
    \item Solid-state electron spins;
    \item Cold atoms;
    \item Trapped ions;
    \item Superconductive circuits.
\end{itemize}

\subsection{Trapped ions}

We can trap them with electric fields: Paul trap. We cannot do the intuitive thing, because of Gausses' law. We can work around it by making them time-dependent. Think of it like this: saddle which spins, an object in the saddle point will be stable if the rotation is fast enough.

We get three harmonic oscillators, with \(\omega_{x,y} \gg \omega_z\). The \(z\) direction is the 'quantum' degree of freedom: the state comes from the atom state \(i\) and the oscillator state \(n\).

Can we put many qubits in there? Our Hamiltonian will be

\begin{equation}
    H  = \sum  \frac{p_i^2}{2m} + \sum  \frac{1}{2} \omega_z^2 z_i^2
    + \sum_i \sum_{j<i} \frac{q^2}{4 \pi \varepsilon_0 \abs{r_i - r_j} }
\end{equation}

They repel each other, we can diagonalize this matrix and get the modes of oscillation.

So we have \(\ket{\alpha_1 \alpha_2 \dots \alpha_N n} \) where the \(n\) is the global oscillation. Our single qubit is \(\ket{\alpha_i n} \). We have the states \(\ket{g, 0} \), \(\ket{g, 1} \), \(\ket{g, 2} \) \dots and \(\ket{e, 0} \), \(\ket{e, 1} \) \dots

We can use a laser to excite the ground state into a vibrational excited state... we choose the right frequency to go from \(\ket{g, k} \) to \(\ket{e, k-1} \) which spontaneously decays into \(\ket{g, k-1} \). This is \emph{side-band cooling}. This can prepare a state with \(\P > 99.9\%\).

Cirac-Zoller Gate: allows us to entangle states with stuff like a CPHASE by interacting with the global vibration.

\subsection{Superconductive qubit}

Something like Bose-condensing electrons: Cooper pairs, with opposing momentums: so the entanglement is in the Fourier space, they are very delocalized in the position space.
Cooper pair currents are the superconductive currents.

We use Josephson juctions inside regular circuits:
two superconductors, separated by a thin insulant. Cooper pairs can tunnel through the insulant.

Charge qubit: quantum numbers inside the superconductor are \(n\), \(\varphi\), with \([n, \varphi] = i\).

\begin{equation}
    H = E_C \qty(n-n_g)^2 - E_J \cos \varphi
\end{equation}

with \(n_g - c_g V /(2e)\), and \(E_C = (2e)^2 / (2(C_J + C_g))\). We can write the eigenstates as \(\ket{n} \), and

\begin{equation}
    H = E_C \sum  (n-n_g) \dyad{n} - \frac{1}{2} E_J \sum \qty(\ketbra{n+1}{n} + \ketbra{n}{n+1}  )
\end{equation}

\end{document}


\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsmath,amsfonts}
\usepackage{hyperref}
\usepackage[a4paper, margin=.5in]{geometry}

%opening
\title{Documentation: A Unifying Mechanistic Model of Excitatory-Inhibitory Interactions in the Auditory Cortex}
\author{Youngmin Park}

\newcommand{\y}[1]{{\texttt{#1}}}

\begin{document}

\maketitle

%\begin{abstract}

%\end{abstract}

\section{Introduction}
The primary tools of this paper are Python (with numpy, scipy, matplotlib), XPPAUTO (XPPAUT or XPP for short) \cite{ermentrout2002simulating}, a Python binding for XPP, \y{Py\_XPPCALL} \cite{xppy}, and Brian2 \cite{Stimberg595710}. Please contact me with any questions about these programs: \y{ympark1988@gmail.com}\footnote{I used XPP extensively in my PhD, and XPP was created by and continues to be developed by Bard Ermentrout, my doctoral advisor. In addition, I am one of the primary contributors to \y{Py\_XPPCALL}.}.

I highly recommend using Ubuntu, since the most stable versions of XPP are currently on Ubuntu. At the time of writing, I have used Ubuntu 16.04 (Python 2.7.12, numpy 1.15.2, scipy 1.1.0, matplotlib 2.2.3), Ubuntu 18.04.2 (Python 2.7.15rc1, numpy 1.15.2, scipy 1.1.0, matplotlib 2.2.3), and XPPAUT version 8 to successfully run all code.

\section{Rate Models}

Rate models are implemented in XPPAUTO. In order to plot the simulations, I run XPPAUTO through the Python binding \y{Py\_XPPCALL}.

\subsection{XPPAUTO}
All rate models were implemented in \texttt{XPPAUTO} for convenience. XPP auto uses an almost plain-text file format for simulating ordinary differential equations (ODEs). There is extensive documentation on XPPAUTO on Bard's website:
\url{http://www.math.pitt.edu/~bard/xpp/help/xppodes.html}. The linked page provides full documentation of ode file syntax. XPPAUTO also comes with a graphical user interface (GUI), but this will not be needed for this documentation. The XPP .ode files for this project are located in the directory \url{https://github.com/geffenlab/park_geffen/rate_models/xpp}.

Let us take a look at one of the ode files, \texttt{cols3\_fs.ode}, which is the three-unit rate model for the forward suppression paradigm. The file begins with a function definition and a bunch of parameters:
\begin{verbatim}
f(x)=al*heav(x)*x*heav(1/al-x)+heav(x-1/al)
dan(x)=exp(-tl*x)
p al = 3

p wee=1.1,wep=2,wes=1
# either wpe=1.2, or wpp=1.8 to reduce SOM disinhibition activity.
p wpe=1.,wpp=2,wps=2

p wse=6,wss=0,wsp=0
p wee2=1,wpe2=1.25,wse2=.125
#p wee2=1,wpe2=1.5,wse2=.25

par tau1=1,tau2=1
par et=.7,pt=1,st=1
par pv_opto=0,som_opto=0

p q=1.3,tl=1
p taud1=150,taud2=10
p tde1=150,tde2=2
p lat=.65
\end{verbatim}
Comment lines are denoted by the pound symbol (\#). The function \texttt{f} is the firing-rate function, which corresponds to Equation (2) in the manuscript (There is a typo in the current manuscript. All simulations were run using the correct $f$ as shown in the code). The function is zero until $x=0$, at which point the function increases linearly with slope $\text{al}=3$ as a function of $x$, until the function reaches the value 1 (when $x=1/\text{al}$, at which point the function ``saturates'' at the value 1. The second function, which I named \y{dan}, is a decaying exponential with rate \y{tl}, which we will later use to modulate the square-wave inputs into the model. We use the same firing rate function for all models, which is a common choice in modeling studies \cite{natan2015complementary,yarden2017stimulus}.

The notation \texttt{p} and \texttt{par} both denote a line consisting of parameters. The first set of parameters:
\begin{verbatim}
p wee=1.1,wep=2,wes=1
p wpe=1.,wpp=2,wps=2

p wse=6,wss=0,wsp=0
p wee2=1,wpe2=1.25,wse2=.125
\end{verbatim}
are for recurrent excitation (\texttt{wee}), PV-to-Pyr inhibition (\texttt{wep}), SOM-to-Pyr inhibition (\texttt{wes}), Pyr-to-PV excitation (\texttt{wpe}), recurrent PV inhibition (\y{wpp}), SOM-to-PV inhibition (\y{wps}), Pyr-to-SOM excitation (\y{wse}), PV-to-SOM inhibition (\y{wsp}), and recurrent SOM inhibition (\y{wss}). The remaining terms are for the lateral weights. Lateral Pyr-to-Pyr (\y{wee2}), lateral Pyr-to-PV (\y{wpe2}), and lateral Pyr-to-SOM (\y{wse2}). These parameters appear in Equation 4, and in the unit equations on page 2.

The next set of parameters:
\begin{verbatim}
par tau1=1,tau2=1
par et=.7,pt=1,st=1
par pv_opto=0,som_opto=0
\end{verbatim}
Are time constants \y{tau1} and \y{tau2} are for the inhibitory populations, PV, and SOM, respectively. This rate model is written so that 1 time unit is 10ms, i.e., \y{tau1=1} means $\tau_1 = 10$ms \cite{tsodyks1997neural,natan2015complementary}. If needed, one could perform the necessary transformation in time so the model displays the correct time units by default.

The next line is for thresholds in the excitatory activity \y{et}, PV activity \y{pt}, and SOM activity \y{st}. These thresholds are minimum values beyond which the population activity can influence the postsynaptic population. Finally, the \y{pv\_opto} and \y{som\_opto} parameters set the level of optogenetic \textit{inactivation}, i.e., positive values of \y{pv\_opto} inactivation PVs, and likewise for \y{som\_opto}. In the paper I flip these numbers for readability.

Next up:
\begin{verbatim}
p q=1.3,tl=1
p taud1=150,taud2=10
p tde1=150,tde2=2
p lat=.65
\end{verbatim}
\y{q} is the strength of the thalamic inputs into the model. \y{tl} is the decay time constant in the exponential function \y{dan}. The depression time constants are \y{taud1} and \y{taud2}, again in the same units, so $\tau_{D_1} = 1500$ms and $\tau_{D_2} = 100$ms. We can ignore \y{tde1,tde2} as they are not used in the code. The parameter \y{lat} determines the strength of lateral thalamic inputs.

The next chunk of code sets up the stimuli in the forward suppression paradigm.
\begin{verbatim}
par t0=10,dur=5,isi=2

t1on=t0
t1off=t1on+dur
t2on=t1off+isi
t2off=t2on+dur

t3on=t2off+isi
t3off=t3on+dur
t4on=t3off+isi
t4off=t4on+dur

t5on=t4off+isi
t5off=t5on+dur
t6on=t5off+isi
t6off=t6on+dur

t7on=t6off+isi
t7off=t7on+dur
t8on=t7off+isi
t8off=t8on+dur

# thalamic inputs with fast timescale depression
gt1(x)=heav(x-t1on)*heav(t1off-x)*dan(x-t1on)
gt2(x)=heav(x-t2on)*heav(t2off-x)*dan(x-t2on)
gt3(x)=heav(x-t3on)*heav(t3off-x)*dan(x-t3on)
gt4(x)=heav(x-t4on)*heav(t4off-x)*dan(x-t4on)

gt5(x)=heav(x-t5on)*heav(t5off-x)*dan(x-t5on)
gt6(x)=heav(x-t6on)*heav(t6off-x)*dan(x-t6on)
gt7(x)=heav(x-t7on)*heav(t7off-x)*dan(x-t7on)
gt8(x)=heav(x-t8on)*heav(t8off-x)*dan(x-t8on)

par mode=1
i1(x)=g1*q*if(mode-1)then(0)else(gt1(x))
i2(x)=g2*q*if(mode-2)then(gt2(x))else(gt1(x)+gt2(x))
i3(x)=g3*q*if(mode-3)then(0)else(gt1(x))
\end{verbatim}

The functions \y{gt1(x)} through \y{gt8(x)} represent 8 different stimuli with stimulus onset times of \y{t1on} through \y{t8on}, stimulus offset times of \y{t1off} through \y{t8off}. The \y{heav} functions are heaviside functions, and the multiplication of two of them as written forms a square pulse. This pulse is then modulated by the exponential decay function \y{dan}. For the forward suppression paradigm, we only need two stimuli, so we only use \y{gt1} and \y{gt2} for the rest of the code.

The \y{mode} parameter sets which unit receives the first input. If \y{mode=1}, then
\begin{verbatim}
i1(x)=g1*q*gt1(x)
i2(x)=g2*q*gt2(x)
i3(x)=0
\end{verbatim}
For more information on how the if statement works in XPP, see the bottom of \url{http://www.math.pitt.edu/~bard/xpp/help/xppodes.html}. If the \y{mode} parameter is set to \y{mode=2}, then
\begin{verbatim}
i1(x)=0
i2(x)=g2*q*(gt1(x)+gt2(x))
i3(x)=0
\end{verbatim}
and so on. The terms \y{g1} and \y{g2} are variables and satisfy the equations
\begin{verbatim}
g1'=(1-g1)/taud1-i1(y)/taud2
g2'=(1-g2)/taud1-i2(y)/taud2
g3'=(1-g3)/taud1-i3(y)/taud2
\end{verbatim}
where \y{y'=1} and all \y{g1,g2,g3} are initialized with a value of 1. The trivial ODE \y{y'=1} is needed for technical reasons related to XPP that isn't too important for now. Anyway, as \y{i1(y)} activates, it ``depletes'' vesicles and forces \y{g1} to decay on a fast timescale. With no other activation of \y{i1}, \y{g1} recovers slowly back to its original value of 1.

Now we finally reach the actual model equations:
\begin{verbatim}
u1'=-u1+f(wee*u1-(wep-damp*(1-g1))*p1-(wes+samp*(1-g1))*s1-et+i1(y)+i2(y)*lat+wee2*u2/1.5)
p1'=(-p1+f(wpe*u1-wpp*p1-wps*s1-pt +i1(y)+i2(y)*lat +wpe2*u2-pv_opto ))/tau1
s1'=(-s1+f(wse*u1-wsp*p1-wss*s1-st                  +wse2*u2-som_opto ))/tau2

u2'=-u2+f(wee*u2-(wep-damp*(1-g2))*p2-(wes+samp*(1-g2))*s2-et+(i1(y)+i3(y))*lat+i2(y)+wee2*(u1+u3)/2)
p2'=(-p2+f(wpe*u2-wpp*p2-wps*s2-pt +(i1(y)+i3(y))*lat+i2(y)+wpe2*(u1+u3)/2 -pv_opto ))/tau1
s2'=(-s2+f(wse*u2-wsp*p2-wss*s2-st                         +wse2*(u1+u3)/2 -som_opto ))/tau2

u3'=-u3+f(wee*u3-(wep-damp*(1-g3))*p3-(wes+samp*(1-g3))*s3-et+i2(y)*lat+i3(y)+wee2*u2/1.5)
p3'=(-p3+f(wpe*u3-wpp*p3-wps*s3-pt +i2(y)*lat+i3(y) +wpe2*u2-pv_opto ))/tau1
s3'=(-s3+f(wse*u3-wsp*p3-wss*s3-st                  +wse2*u2-som_opto ))/tau2
\end{verbatim}
These are the implemented versions of the 3 unit equations from the manuscript:
Th first or left unit satisfies
\begin{align*}
 \tau_u u_1' &= -u_1 + f( w_{ee} u_1 - (w_{ep}(t)-aD_1)p_1 - (w_{es}(t)+b F_1)s_1 + qI_1(t) + w_{ee}^* u_2),\\
 \tau_p p_1' &= -p_1 + f( w_{pe} u_1 - w_{pp}p_1 - w_{ps}s_1 + I_\text{Opt,PV} + qI_1(t) + w_{pe}^* u_2),\\
 \tau_s s_1' &= -s_1 + f( w_{se} u_1 - w_{sp}p_1 - w_{ss}s_1 + I_\text{Opt,SOM} + w_{se}^* u_2),
\end{align*}
where $I_1 = i_1(t) +i_2(t)\alpha$. The second, or center unit, satisfies
\begin{align*}
 \tau_u u_2' &= -u_2 + f( w_{ee} u_2 - (w_{ep}(t)-aD_2)p_2 - (w_{es}(t)+b F_2)s_2 + qI_2(t) + w_{ee}^* (u_1+u_3)/2),\\
 \tau_p p_2' &= -p_2 + f( w_{pe} u_2 - w_{pp}p_2 - w_{ps}s_2 + I_\text{Opt,PV} + qI_2(t) + w_{pe}^* (u_1+u_3)/2),\\
 \tau_s s_2' &= -s_2 + f( w_{se} u_2 - w_{sp}p_2 - w_{ss}s_2 + I_\text{Opt,SOM} + w_{se}^* u_2),
\end{align*}
where $I_2(t) = (i_1(t)+i_3(t))\alpha +i_2(t)$. Finally, the third, or right unit, satisfies
\begin{align*}
 \tau_u u_3' &= -u_3 + f( w_{ee} u_3 - (w_{ep}(t)-aD_3)p_3 - (w_{es}(t)+b F_3)s_3 + qI_3(t) + w_{ee}^* u_2),\\
 \tau_p p_3' &= -p_3 + f( w_{pe} u_3 - w_{pp}p_3 - w_{ps}s_3 + I_\text{Opt,PV} + qI_3(t) + w_{pe}^* u_2),\\
 \tau_s s_3' &= -s_3 + f( w_{se} u_3 - w_{sp}p_3 - w_{ss}s_3 + I_\text{Opt,SOM} + w_{se}^* u_2),
\end{align*}
where $I_3 = i_2(t) +i_3(t)\alpha$.

The remaining lines are mostly related to using the XPPAUTO GUI. Choices that affect the integration are
\begin{verbatim}
@ bounds=100000
@ maxstor=1000000
@ total=200
@ dt=.1
\end{verbatim}
\y{bounds} is simply the maximum possible value of any solution before XPPAUTO gives up. \y{maxstor} sets the max storage XPPAUTO is allowed to take from memory. \y{total} is the total time of the simulation, and \y{dt} is the time step.

\subsection{Python Visualization}
These XPP ode files can be run using the XPP GUI, but we will run them using Python using \y{Py\_XPPCALL}. I have tried to include enough documentation to help users understand the basics \url{https://github.com/youngmp/Py_XPPCALL}. The Python files that run the XPP files are are located in \url{https://github.com/geffenlab/park_geffen/rate_models}. The Python file that reads the ODE file from the example above is called \y{cols3\_fs.py}. You should be able to run the python file directly.
\begin{verbatim}
$ python cols3_fs.py
\end{verbatim}
There will be lots of text that appears. These were mostly for debugging purposes and can be turned off by commenting the print statements in this file and in \y{xppcall.py}. After the script runs, a few plots should appear. I like to set up the plotting framework in the individual Python files before finalizing them in \y{generate\_figures.py}.

The \y{main} function calls all the relevant files. In brief, the loop runs three different stimulus patterns. The first stimulates the left unit then the center unit with 50ms tones and a 20ms inter-stimulus-interval. The second stimulates the center unit twice with the same time intervals. The third stimulates the right unit then the center unit with the same time intervals. This is a simple version of the forward suppression paradigm. These three sets of stimuli are repeated for PV inactivation and SOM inactivation. The max firing rate response is then recorded into a matrix and plotted.

Much of this logic appears in its final form in the function \y{r3\_s3\_fs\_full} in \y{generate\_figures.py}. The difference is that the function \y{r3\_s3\_fs\_full} includes the spiking model simulation and plots. I've tried to comment on most things... Let me know if you have any questions.

The remaining Python files for simulating the rate model, \y{cols3\_ssa} and \y{natan2015\_simple\_linear} have a similar construction. The \y{main} function in each script contains all the preliminary plotting/visualization code, which you can use to tinker and learn the ins and outs of the python/xpp interface. Seriously, play with those all you want.

The final version of the plotting functions appear in \y{generate\_figures.py}. Don't change those unless you're familiar with the code.

\section{Spiking Models}

\section{Generating Figures}

\y{generate\_figures.py} is the master plotting function for the paper. Most figure papers are generated here, except for a couple that were created in Inkscape. This file serves as a technical documentation of the code. In the main function, comments note which function belong to which figure.

\begin{thebibliography}{1}

\bibitem{ermentrout2002simulating}
Bard Ermentrout.
\newblock {\em Simulating, analyzing, and animating dynamical systems: a guide
  to XPPAUT for researchers and students}, volume~14.
\newblock Siam, 2002.

\bibitem{natan2015complementary}
Ryan~G Natan, John~J Briguglio, Laetitia Mwilambwe-Tshilobo, Sara~I Jones, Mark
  Aizenberg, Ethan~M Goldberg, and Maria~Neimark Geffen.
\newblock Complementary control of sensory adaptation by two types of cortical
  interneurons.
\newblock {\em Elife}, 4, 2015.

\bibitem{xppy}
Ilya Prokin and Youngmin Park.
\newblock Pyxppcall.
\newblock \url{https://github.com/iprokin/Py_XPPCALL}, 2017.

\bibitem{Stimberg595710}
Marcel Stimberg, Romain Brette, and Dan F.~M. Goodman.
\newblock Brian 2: an intuitive and efficient neural simulator.
\newblock {\em bioRxiv}, 2019.

\bibitem{tsodyks1997neural}
Misha~V Tsodyks and Henry Markram.
\newblock The neural code between neocortical pyramidal neurons depends on
  neurotransmitter release probability.
\newblock {\em Proceedings of the national academy of sciences},
  94(2):719--723, 1997.

\bibitem{yarden2017stimulus}
Tohar~S Yarden and Israel Nelken.
\newblock Stimulus-specific adaptation in a recurrent network model of primary
  auditory cortex.
\newblock {\em PLoS computational biology}, 13(3):e1005437, 2017.

\end{thebibliography}
\end{document}


%\documentclass[fleqn]{article}
%\usepackage{mathptmx}
%
%\DeclareMathSymbol{\Gamma}  {\mathalpha}{letters}{"00}
%\DeclareMathSymbol{\Delta}  {\mathalpha}{letters}{"01}
%\DeclareMathSymbol{\Theta}  {\mathalpha}{letters}{"02}
%\DeclareMathSymbol{\Lambda} {\mathalpha}{letters}{"03}
%\DeclareMathSymbol{\Xi}     {\mathalpha}{letters}{"04}
%\DeclareMathSymbol{\Pi}     {\mathalpha}{letters}{"05}
%\DeclareMathSymbol{\Sigma}  {\mathalpha}{letters}{"06}
%\DeclareMathSymbol{\Upsilon}{\mathalpha}{letters}{"07}
%\DeclareMathSymbol{\Phi}    {\mathalpha}{letters}{"08}
%\DeclareMathSymbol{\Psi}    {\mathalpha}{letters}{"09}
%\DeclareMathSymbol{\Omega}  {\mathalpha}{letters}{"0A}
%
%\DeclareMathSymbol{\alpha}  {\mathalpha}{letters}{"0B}
%\DeclareMathSymbol{\beta}   {\mathalpha}{letters}{"0C}
%\DeclareMathSymbol{\gamma}  {\mathalpha}{letters}{"0D}
%\DeclareMathSymbol{\delta}  {\mathalpha}{letters}{"0E}
%\DeclareMathSymbol{\epsilon}{\mathalpha}{letters}{"0F}
%\DeclareMathSymbol{\zeta}   {\mathalpha}{letters}{"10}
%\DeclareMathSymbol{\eta}    {\mathalpha}{letters}{"11}
%\DeclareMathSymbol{\theta}  {\mathalpha}{letters}{"12}
%\DeclareMathSymbol{\iota}   {\mathalpha}{letters}{"13}
%\DeclareMathSymbol{\kappa}  {\mathalpha}{letters}{"14}
%\DeclareMathSymbol{\lambda} {\mathalpha}{letters}{"15}
%\DeclareMathSymbol{\mu}     {\mathalpha}{letters}{"16}
%\DeclareMathSymbol{\nu}     {\mathalpha}{letters}{"17}
%\DeclareMathSymbol{\xi}     {\mathalpha}{letters}{"18}
%\DeclareMathSymbol{\pi}     {\mathalpha}{letters}{"19}
%\DeclareMathSymbol{\rho}    {\mathalpha}{letters}{"1A}
%\DeclareMathSymbol{\sigma}  {\mathalpha}{letters}{"1B}
%\DeclareMathSymbol{\tau}    {\mathalpha}{letters}{"1C}
%\DeclareMathSymbol{\upsilon}{\mathalpha}{letters}{"1D}
%\DeclareMathSymbol{\phi}    {\mathalpha}{letters}{"1E}
%\DeclareMathSymbol{\chi}    {\mathalpha}{letters}{"1F}
%\DeclareMathSymbol{\psi}    {\mathalpha}{letters}{"20}
%\DeclareMathSymbol{\omega}  {\mathalpha}{letters}{"21}
%\DeclareMathSymbol{\varepsilon} {\mathalpha}{letters}{"22}
%\DeclareMathSymbol{\vartheta}   {\mathalpha}{letters}{"23}
%\DeclareMathSymbol{\varpi}      {\mathalpha}{letters}{"24}
%\DeclareMathSymbol{\varrho}     {\mathalpha}{letters}{"25}
%\DeclareMathSymbol{\varsigma}   {\mathalpha}{letters}{"26}
%\DeclareMathSymbol{\varphi}     {\mathalpha}{letters}{"27}
%
%\DeclareMathSymbol{\dit}{\mathord}{letters}{`d}
%\DeclareMathSymbol{\dup}{\mathord}{operators}{`d}
%
%\nofiles
%
%\parindent 0pt
%\mathindent 1em

\def\test#1{#1}

\def\testnums{%
  \test 0 \test 1 \test 2 \test 3 \test 4 \test 5 \test 6 \test 7
  \test 8 \test 9 }
\def\testupperi{%
  \test A \test B \test C \test D \test E \test F \test G \test H
  \test I \test J \test K \test L \test M }
\def\testupperii{%
  \test N \test O \test P \test Q \test R \test S \test T \test U
  \test V \test W \test X \test Y \test Z }
\def\testupper{%
  \testupperi\testupperii}

\def\testloweri{%
  \test a \test b \test c \test d \test e \test f \test g \test h
  \test i \test j \test k \test l \test m }
\def\testlowerii{%
  \test n \test o \test p \test q \test r \test s \test t \test u
  \test v \test w \test x \test y \test z
  \test\imath \test\jmath }
\def\testlower{%
  \testloweri\testlowerii}

\def\testupgreeki{%
  \test A \test B \test\Gamma \test\Delta \test E \test Z \test H
  \test\Theta \test I \test K \test\Lambda \test M }
\def\testupgreekii{%
  \test N \test\Xi \test O \test\Pi \test P \test\Sigma \test T
  \test\Upsilon \test\Phi \test X \test\Psi \test\Omega
  \test\nabla }
\def\testupgreek{%
  \testupgreeki\testupgreekii}

\def\testlowgreeki{%
  \test\alpha \test\beta \test\gamma \test\delta \test\epsilon
  \test\zeta \test\eta \test\theta \test\iota \test\kappa \test\lambda
  \test\mu }
\def\testlowgreekii{%
  \test\nu \test\xi \test o \test\pi \test\rho \test\sigma \test\tau
  \test\upsilon \test\phi \test\chi \test\psi \test\omega }
\def\testlowgreekiii{%
  \test\varepsilon \test\vartheta \test\varpi \test\varrho
  \test\varsigma \test\varphi}
\def\testlowgreek{%
  \testlowgreeki\testlowgreekii\testlowgreekiii}

%\begin{document}

\section{Math Alphabets}

Math Italic (\texttt{\string\mathnormal})
\def\test#1{\mathnormal{#1},}
\begin{eqnarray*}
  && {\testnums}\\
  && {\testupper}\\
  && {\testlower}\\
  && {\testupgreek}\\
  && {\testlowgreek}
\end{eqnarray*}%

Math Roman (\texttt{\string\mathrm})
\def\test#1{\mathrm{#1},}
\begin{eqnarray*}
  && {\testnums}\\
  && {\testupper}\\
  && {\testlower}\\
  && {\testupgreek}
\end{eqnarray*}%

%Math Italic Bold
%\def\test#1{\mathbm{#1},}
%\begin{eqnarray*}
%  && {\testnums}\\
%  && {\testupper}\\
%  && {\testlower}\\
%  && {\testupgreek}\\
%  && {\testlowgreek}
%\end{eqnarray*}%

%Math Bold (\texttt{\string\mathbf})
%\def\test#1{\mathbf{#1},}
%\begin{eqnarray*}
%  && {\testnums}\\
%  && {\testupper}\\
%  && {\testlower}\\
%  && {\testupgreek}
%\end{eqnarray*}%

Caligraphic (\texttt{\string\mathcal})
\def\test#1{\mathcal{#1},}
\begin{eqnarray*}
  && {\testupper}
\end{eqnarray*}%

%Script (\texttt{\string\mathscr})
%\def\test#1{\mathscr{#1},}
%\begin{eqnarray*}
%  && {\testupper}
%\end{eqnarray*}%

%Fraktur (\texttt{\string\mathfrak})
%\def\test#1{\mathfrak{#1},}
%\begin{eqnarray*}
%  && {\testupper}\\
%  && {\testlower}
%\end{eqnarray*}%

%Blackboard Bold (\texttt{\string\mathbb})
%\def\test#1{\mathbb{#1},}
%\begin{eqnarray*}
%  && {\testupper}
%\end{eqnarray*}%

\clearpage
\section{Character Sidebearings}

\def\test#1{|#1|+}
\begin{eqnarray*}
  && {\testupperi}\\
  && {\testupperii}\\
  && {\testloweri}\\
  && {\testlowerii}\\
  && {\testupgreeki}\\
  && {\testupgreekii}\\
  && {\testlowgreeki}\\
  && {\testlowgreekii}\\
  && {\testlowgreekiii}
\end{eqnarray*}%
%
\def\test#1{|\mathrm{#1}|+}%
\begin{eqnarray*}
  && {\testupperi}\\
  && {\testupperii}\\
  && {\testloweri}\\
  && {\testlowerii}\\
  && {\testupgreeki}\\
  && {\testupgreekii}
\end{eqnarray*}%
%
%\def\test#1{|\mathbm{#1}|+}%
%\begin{eqnarray*}
%  && {\testupperi}\\
%  && {\testupperii}\\
%  && {\testloweri}\\
%  && {\testlowerii}\\
%  && {\testupgreeki}\\
%  && {\testupgreekii}\\
%  && {\testlowgreeki}\\
%  && {\testlowgreekii}\\
%  && {\testlowgreekiii}
%\end{eqnarray*}%
%%
%\def\test#1{|\mathbf{#1}|+}%
%\begin{eqnarray*}
%  && {\testupperi}\\
%  && {\testupperii}\\
%  && {\testloweri}\\
%  && {\testlowerii}\\
%  && {\testupgreeki}\\
%  && {\testupgreekii}
%\end{eqnarray*}%
%
\def\test#1{|\mathcal{#1}|+}%
\begin{eqnarray*}
  && {\testupperi}\\
  && {\testupperii}
\end{eqnarray*}%

\clearpage
\section{Superscript positioning}

\def\test#1{#1^{2}+}%
\begin{eqnarray*}
  && {\testupperi}\\
  && {\testupperii}\\
  && {\testloweri}\\
  && {\testlowerii}\\
  && {\testupgreeki}\\
  && {\testupgreekii}\\
  && {\testlowgreeki}\\
  && {\testlowgreekii}\\
  && {\testlowgreekiii}
\end{eqnarray*}%
%
\def\test#1{\mathrm{#1}^{2}+}%
\begin{eqnarray*}
  && {\testupperi}\\
  && {\testupperii}\\
  && {\testloweri}\\
  && {\testlowerii}\\
  && {\testupgreeki}\\
  && {\testupgreekii}
\end{eqnarray*}%
%
%\def\test#1{\mathbm{#1}^{2}+}%
%\begin{eqnarray*}
%  && {\testupperi}\\
%  && {\testupperii}\\
%  && {\testloweri}\\
%  && {\testlowerii}\\
%  && {\testupgreeki}\\
%  && {\testupgreekii}\\
%  && {\testlowgreeki}\\
%  && {\testlowgreekii}\\
%  && {\testlowgreekiii}
%\end{eqnarray*}%
%
%\def\test#1{\mathbf{#1}^{2}+}%
%\begin{eqnarray*}
%  && {\testupperi}\\
%  && {\testupperii}\\
%  && {\testloweri}\\
%  && {\testlowerii}\\
%  && {\testupgreeki}\\
%  && {\testupgreekii}
%\end{eqnarray*}
%
\def\test#1{\mathcal{#1}^{2}+}%
\begin{eqnarray*}
  && {\testupperi}\\
  && {\testupperii}
\end{eqnarray*}%

\clearpage
\section{Subscript positioning}

\def\test#1{\mathnormal{#1}_{i}+}%
\begin{eqnarray*}
  && {\testupperi}\\
  && {\testupperii}\\
  && {\testloweri}\\
  && {\testlowerii}\\
  && {\testupgreeki}\\
  && {\testupgreekii}\\
  && {\testlowgreeki}\\
  && {\testlowgreekii}\\
  && {\testlowgreekiii}
\end{eqnarray*}%
%
\def\test#1{\mathrm{#1}_{i}+}%
\begin{eqnarray*}
  && {\testupperi}\\
  && {\testupperii}\\
  && {\testloweri}\\
  && {\testlowerii}\\
  && {\testupgreeki}\\
  && {\testupgreekii}
\end{eqnarray*}%
%
%\def\test#1{\mathbm{#1}_{i}+}%
%\begin{eqnarray*}
%  && {\testupperi}\\
%  && {\testupperii}\\
%  && {\testloweri}\\
%  && {\testlowerii}\\
%  && {\testupgreeki}\\
%  && {\testupgreekii}\\
%  && {\testlowgreeki}\\
%  && {\testlowgreekii}\\
%  && {\testlowgreekiii}
%\end{eqnarray*}
%%
%\def\test#1{\mathbf{#1}_{i}+}%
%\begin{eqnarray*}
%  && {\testupperi}\\
%  && {\testupperii}\\
%  && {\testloweri}\\
%  && {\testlowerii}\\
%  && {\testupgreeki}\\
%  && {\testupgreekii}
%\end{eqnarray*}%
%
\def\test#1{\mathcal{#1}_{i}+}%
\begin{eqnarray*}
  && {\testupperi}\\
  && {\testupperii}
\end{eqnarray*}%

\clearpage
\section{Accent positioning}

\def\test#1{\hat{#1}+}%
\begin{eqnarray*}
  && {\testupperi}\\
  && {\testupperii}\\
  && {\testloweri}\\
  && {\testlowerii}\\
  && {\testupgreeki}\\
  && {\testupgreekii}\\
  && {\testlowgreeki}\\
  && {\testlowgreekii}\\
  && {\testlowgreekiii}
\end{eqnarray*}%
%
\def\test#1{\hat{\mathrm{#1}}+}%
\begin{eqnarray*}
  && {\testupperi}\\
  && {\testupperii}\\
  && {\testloweri}\\
  && {\testlowerii}\\
  && {\testupgreeki}\\
  && {\testupgreekii}
\end{eqnarray*}%
%
%\def\test#1{\hat{\mathbm{#1}}+}%
%\begin{eqnarray*}
%  && {\testupperi}\\
%  && {\testupperii}\\
%  && {\testloweri}\\
%  && {\testlowerii}\\
%  && {\testupgreeki}\\
%  && {\testupgreekii}\\
%  && {\testlowgreeki}\\
%  && {\testlowgreekii}\\
%  && {\testlowgreekiii}
%\end{eqnarray*}%
%%
%\def\test#1{\hat{\mathbf{#1}}+}%
%\begin{eqnarray*}
%  && {\testupperi}\\
%  && {\testupperii}\\
%  && {\testloweri}\\
%  && {\testlowerii}\\
%  && {\testupgreeki}\\
%  && {\testupgreekii}
%\end{eqnarray*}
%
\def\test#1{\hat{\mathcal{#1}}+}%
\begin{eqnarray*}
  && {\testupperi}\\
  && {\testupperii}
\end{eqnarray*}%

\clearpage
\section{Differentials}

\begin{eqnarray*}
\gdef\test#1{\dit #1+}%
  && {\testupperi}\\
  && {\testupperii}\\
  && {\testloweri}\\
  && {\testlowerii}\\
  && {\testupgreeki}\\
  && {\testupgreekii}\\
  && {\testlowgreeki}\\
  && {\testlowgreekii}\\
  && {\testlowgreekiii}\\
\gdef\test#1{\dit \mathrm{#1}+}%
  && {\testupgreeki}\\
  && {\testupgreekii}
\end{eqnarray*}%
%
\begin{eqnarray*}
\gdef\test#1{\dup #1+}%
  && {\testupperi}\\
  && {\testupperii}\\
  && {\testloweri}\\
  && {\testlowerii}\\
  && {\testupgreeki}\\
  && {\testupgreekii}\\
  && {\testlowgreeki}\\
  && {\testlowgreekii}\\
  && {\testlowgreekiii}\\
\gdef\test#1{\dup \mathrm{#1}+}%
  && {\testupgreeki}\\
  && {\testupgreekii}
\end{eqnarray*}%
%
\begin{eqnarray*}
\gdef\test#1{\partial #1+}%
  && {\testupperi}\\
  && {\testupperii}\\
  && {\testloweri}\\
  && {\testlowerii}\\
  && {\testupgreeki}\\
  && {\testupgreekii}\\
  && {\testlowgreeki}\\
  && {\testlowgreekii}\\
  && {\testlowgreekiii}\\
\gdef\test#1{\partial \mathrm{#1}+}%
  && {\testupgreeki}\\
  && {\testupgreekii}
\end{eqnarray*}%

\clearpage
\section{Slash kerning}

\def\test#1{1/#1+}
\begin{eqnarray*}
  && {\testupperi}\\
  && {\testupperii}\\
  && {\testloweri}\\
  && {\testlowerii}\\
  && {\testupgreeki}\\
  && {\testupgreekii}\\
  && {\testlowgreeki}\\
  && {\testlowgreekii}\\
  && {\testlowgreekiii}
\end{eqnarray*}

\def\test#1{#1/2+}
\begin{eqnarray*}
  && {\testupperi}\\
  && {\testupperii}\\
  && {\testloweri}\\
  && {\testlowerii}\\
  && {\testupgreeki}\\
  && {\testupgreekii}\\
  && {\testlowgreeki}\\
  && {\testlowgreekii}\\
  && {\testlowgreekiii}
\end{eqnarray*}

\clearpage
\section{Big operators}

\def\testop#1{#1_{i=1}^{n} x^{n} \quad}
\begin{displaymath}
  \testop\sum
  \testop\prod
  \testop\coprod
  \testop\int
  \testop\oint
\end{displaymath}
\begin{displaymath}
  \testop\bigotimes
  \testop\bigoplus
  \testop\bigodot
  \testop\bigwedge
  \testop\bigvee
  \testop\biguplus
  \testop\bigcup
  \testop\bigcap
  \testop\bigsqcup
% \testop\bigsqcap
\end{displaymath}

\section{Radicals}

\begin{displaymath}
  \sqrt{x+y} \qquad \sqrt{x^{2}+y^{2}} \qquad
  \sqrt{x_{i}^{2}+y_{j}^{2}} \qquad
  \sqrt{\left(\frac{\cos x}{2}\right)} \qquad
  \sqrt{\left(\frac{\sin x}{2}\right)}
\end{displaymath}

\begingroup
\delimitershortfall-1pt
\begin{displaymath}
  \sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{x+y}}}}}}}
\end{displaymath}
\endgroup % \delimitershortfall

\section{Over- and underbraces}

\begin{displaymath}
  \overbrace{x} \quad
  \overbrace{x+y} \quad
  \overbrace{x^{2}+y^{2}} \quad
  \overbrace{x_{i}^{2}+y_{j}^{2}} \quad
  \underbrace{x} \quad
  \underbrace{x+y} \quad
  \underbrace{x_{i}+y_{j}} \quad
  \underbrace{x_{i}^{2}+y_{j}^{2}} \quad
\end{displaymath}

\section{Normal and wide accents}

\begin{displaymath}
  \dot{x} \quad
  \ddot{x} \quad
  \vec{x} \quad
  \bar{x} \quad
  \overline{x} \quad
  \overline{xx} \quad
  \tilde{x} \quad
  \widetilde{x} \quad
  \widetilde{xx} \quad
  \widetilde{xxx} \quad
  \hat{x} \quad
  \widehat{x} \quad
  \widehat{xx} \quad
  \widehat{xxx} \quad
\end{displaymath}

\section{Long arrows}

\begin{displaymath}
  \leftarrow \mathrel{-} \rightarrow \quad
  \leftrightarrow \quad
  \longleftarrow  \quad
  \longrightarrow \quad
  \longleftrightarrow \quad
  \Leftarrow = \Rightarrow \quad
  \Leftrightarrow \quad
  \Longleftarrow  \quad
  \Longrightarrow \quad
  \Longleftrightarrow \quad
\end{displaymath}

\section{Left and right delimters}

\def\testdelim#1#2{ - #1 f #2 - }
\begin{displaymath}
  \testdelim()
  \testdelim[]
  \testdelim\lfloor\rfloor
  \testdelim\lceil\rceil
  \testdelim\langle\rangle
  \testdelim\{\}
\end{displaymath}

\def\testdelim#1#2{ - \left#1 f \right#2 - }
\begin{displaymath}
  \testdelim()
  \testdelim[]
  \testdelim\lfloor\rfloor
  \testdelim\lceil\rceil
  \testdelim\langle\rangle
  \testdelim\{\}
% \testdelim\lgroup\rgroup
% \testdelim\lmoustache\rmoustache
\end{displaymath}
\begin{displaymath}
  \testdelim)(
  \testdelim][
  \testdelim//
  \testdelim\backslash\backslash
  \testdelim/\backslash
  \testdelim\backslash/
\end{displaymath}

\clearpage
\section{Big-g-g delimters}

\def\testdelim#1#2{%
  - \left#1\left#1\left#1\left#1\left#1\left#1\left#1\left#1 -
  \right#2\right#2\right#2\right#2\right#2\right#2\right#2\right#2 -}

\begingroup
\delimitershortfall-1pt
\begin{displaymath}
  \testdelim\lfloor\rfloor
  \qquad
  \testdelim()
\end{displaymath}
\begin{displaymath}
  \testdelim\lceil\rceil
  \qquad
  \testdelim\{\}
\end{displaymath}
\begin{displaymath}
  \testdelim[]
  \qquad
  \testdelim\lgroup\rgroup
\end{displaymath}
\begin{displaymath}
  \testdelim\langle\rangle
  \qquad
  \testdelim\lmoustache\rmoustache
\end{displaymath}
\begin{displaymath}
  \testdelim\uparrow\downarrow \quad
  \testdelim\Uparrow\Downarrow \quad
\end{displaymath}
\endgroup % \delimitershortfall

%\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:


Now that we have defined the EF1 objective for filter optimization, we need to find efficient ways to (approximately) optimize it.  To this end, this section first describes two scalable and efficient greedy algorithms to optimize EF1.
%used to build filters setting for querying large data graphs in order to retrieve relevant UI elements that are of interest to the users.
Following this, we describe an optimization-based approach based on Mixed Integer Linear Programming (MILP) to benchmark the performance of the proposed greedy algorithms on moderate-sized problems.

\subsection{Greedy search}

% Should discuss greedy algorithm generically as having a metric and at each step
% a choice of k restrictions, which are each scored against the metric with the
% highest score chosen at each step.  Then each individual filter only has to
% specify what the choices are and how the choice restricts the set of emails
% selected.  What is a good succinct notation for this?

As discussed previously, we assume that three types of ``sub-filters'' are used to select a subset of relevant information in a VID: Keyword Filter, Time Filter, and Space Filter.  A Global Filter is generated by \emph{conjoining} these three sub-filters. In the following, we describe how to greedily build each of these sub-filters, and then how to combine them into a Global Filter.

\subsubsection{Greedy Keyword Filter algorithm}

Given a set of information elements, the greedy Keyword Filter algorithm aims to select a set of keywords to form a negation query in order to exclude a subset of elements containing these keywords for the purpose of maximizing the EF1-Score.

Formally, given a keyword query representation $Q_k$, the algorithm aims to select an
optimal subset of $k$ terms $T_{k}^{*}\subset E$ (where $|T_{k}^{*}|=k$ and $E$ is the initial set of elements) to build a negation keyword query $Q_k$, i.e. $Q_k=\{\neg t_{1}^{*},\dots \neg t_{k}^{*}\}$, in order to optimize the EF1-score. This is achieved by
building $T_{k}^{*}$ in a greedy manner by choosing the next optimal
term $t_{k}^{*}$ given the previous set of optimal term selections
$T_{k-1}^{*}=\{t_{1}^{*},\ldots,t_{k-1}^{*}\}$ (assuming $T_{0}^{*}=\emptyset$)
using the following selection criterion:
\begin{equation}
t_{k}^{*}=\argmax_{t_{k}\notin T_{k-1}^{*}}\hspace{-0.3mm}[EF1(E^{*} \textrm{ that satisfies } Q_k=\{\neg t_{1}^{*},\dots \neg t_{k}^{*}\})]
\end{equation}
where  $E^{*}$ is a subset of the initial element set $E$ that satisfy the negation keyword query $Q_k$. In order to reduce the keyword search space, we propose to use the top 100 terms ranked using Mutual Information to identify the keywords that are predictive of the ``supervised'' relevance measure.  We remark that other metrics like frequency would be more appropriate for unsupervised tasks.  We further remark that we have chosen to use a negation query as a means to effectively prune (or filter) the content as more terms are selected.
%mainly for technical reasons as a negation query generates queries with much fewer terms.
The best indexing strategy to support this greedy search is the inverted index data structure~\cite{Zobel2006}.

\subsubsection{Greedy Time Filter algorithm}

The idea behind the time-based greedy filter algorithm is as simple as finding a time window range $Q_t=[t_{start},t_{end}]$, which allows to select a subset of elements $E^{*}\subseteq E$ falling in that time window, with $E^{*}$ having the highest EF1-Score.

Formally, a list of elements $E=\{j_{t_1}\leq \dots \leq j_{t_n}\}$, where "$\leq$" specifies the timestamp order, we propose the following two time greedy algorithms:

\subfour{Naive greedy algorithm:} First, at each iteration of this algorithm, an early ranked element $j_{t_i}$ is removed, and then, the remaining set is assessed using EF1-Score. If the remaining set has a lower EF1-score value than the set of the previous iteration, the algorithm assigns $t_i$ to the lower time bound of the time query, i.e., $t_{start}=t_i$.
Next, the algorithm does the same set of operations, by removing at each iteration a lastly ranked element  $j_{t_i}$, and by stopping once the removal of  $j_{t_i}$ causes a decrease in the EF1-Score value. Then, the algorithm assigns $t_i$ to the upper time bound of the time query, i.e., $t_{end}=t_i$.
Lastly, the algorithm returns the  time query $Q_t=[t_{start},t_{end}]$, with obviously $EF1(E^{*} \textrm{ that satisfies } Q_t=[t_{start},t_{end}]) \geq EF1(E)$.

\subfour{Binary Partition Search algorithm:} Large datasets with sparse positive data (e.g., 0.5\% of alerts in a security graph) will cause the previous algorithm to take a large number of iterations to terminate since it greedily adjusts filter settings in a minimal way at each step.  A way to address this problem is to use binary partitioning search (BPS).  Hence, instead of removing a single element $j$ at each iteration, this algorithm operates by selecting between two distinct alternatives (binary partitions) at each iteration.

\begin{algorithm}[t]
%\scriptsize
\caption{Binary Partition Search (BPS) Algorithm}
\SetAlgoLined
\SetKwData{Left}{left}\SetKwData{This}{this}\SetKwData{Up}{up}
\SetKwFunction{Union}{Union}\SetKwFunction{FindCompress}{FindCompress}
\SetKwInOut{Input}{input}\SetKwInOut{Output}{output}
\Input{A set of ordered elements $E=\{j_{v_1} \dots j_{v_n}\}$ }
\Output{A timestamp $t$;}
\BlankLine
\label{alg:Dichotomy}

$v_{min}=v_1$; $v_{max}=v_n$;  $v_{mid}=\tfrac{v_1+v_n}{2}$;

\While {$v_{min}!=v_{mid}!=v_{max}$}{

\eIf{$[EF1(\{j_{v_{min}} \dots  j_{v_n}\}) \geq EF1(\{j_{v_{mid}} \dots  j_{v_n}\})]$}{
$v_{max}=v_{mid}$;
$v_{mid}=\tfrac{v_{min}+v_{mid}}{2}$;
}{
$v_{min}=v_{mid}$;
$v_{mid}=\tfrac{v_{min}+v_{max}}{2}$;
}
}
\Return  $v_{mid}$;

\label{alg:return}
\end{algorithm}

%Therefore, the algorithm first sets the values $t_{min}=t_1$, $t_{max}=t_n$,  and $t_{mid}=\tfrac{t_1+t_n}{2}$. Then, for each iteration, if $[EF1(\{d_{t_{min}}\leq \dots \leq d_{t_n}\}) \geq EF1(\{d_{t_{mid}}\leq \dots \leq d_{t_n}\})]$, the algorithm sets $t_{max}=t_{mid}$, $t_{mid}=\tfrac{t_{min}+t_{mid}}{2}$ and makes a new iteration, else, the algorithm sets $t_{min}=t_{mid}$,  $t_{mid}=\tfrac{t_{min}+t_{max}}{2}$  and makes a new iteration. The algorithm keeps iterating until $t_{min}=t_{mid}=t_{max}$, where it assigns $t_{mid}$ to the lower time bound of the time query, i.e., $t_{start}=t_{mid}$.

As an example of the BPS approach for the time sub-filter, the algorithm first sorts $E$ in increasing order of time stamp. Then, it applies the procedure described by Algorithm \ref{alg:Dichotomy}. This procedure will return  the lower time bound of the time query, i.e., $t_{start}=t_{mid}$.
Next, the algorithm sorts $E$ in decreasing order of time stamp, and then, it applies again the procedure described by Algorithm \ref{alg:Dichotomy} to get the upper time bound of the time query, i.e., $t_{end}=t_i$. Lastly, the algorithm returns the  time query $Q_t=[t_{start},t_{end}]$, such that $EF1(E^{*} \textrm{ that satisfies } Q_t=[t_{start},t_{end}]) \geq EF1(E)$. Note that this algorithm proceeds in a total of $log(n)$ iterations in the best case, and $2\times log(n)$ iterations in the worst case.

For both the naive and time-based greedy algorithms, we use the red-black tree as the indexing data structure \cite{Guibas1978}.

\subsubsection{Greedy Spatial Filter algorithm}

The aim of this algorithm is to return coordinates $Q_p=[(x_{min},y_{min},(x_{max},y_{max})]$ representing the EF1-Score maximizing bounding box represented by the lower and upper bound coordinates -- respectively $(x_{min},y_{min})$ and $(x_{max},y_{max})$. This 2D problem is similar to the previous one dimensional problem of finding the best time window. Therefore, the two greedy algorithms described above can be adapted for this problem by first applying each algorithm on the x-axis to determine $(x_{min},x_{max})$, then on the y-axis to determine $(y_{min},y_{max})$.
% We need to ditch some citations and this removes the complaint that we did not provide all details -- we claim it is obvious.  -Scott
%We omit the description of these two algorithms for lack of space, but a detailed description can be found in the technical report~\cite{Bouadjenek2018}.
We use the R-tree as a data structure for indexing multi-dimensional continuous data~\cite{Guttman1984}.

\subsubsection{Global Filter algorithm} To obtain a Global Filter combining all sub-filters, we propose a greedy algorithm, which at each iteration applies all sub-filters and chooses the one that most reduces EF1.  The selected filter is updated with its new setting and the iteration continues.  Iterations termination when no sub-filter can unilaterally improve EF1 and the final filter settings are returned as the Global Filter.
%to, theselects the best sub-filter to apply (according to its reduction in EF1-Score), then checks if that filter improves the EF1-Score. If so, the algorithm continues with the updated filter settings, otherwise, it terminates.
% Don't get the following -- seems non-essential, at least for submission.  -Scott
%Note that here, we use  $k=1$ for the keywords greedy algorithm.
%The algorithm will then determine a sequence of filters to apply on the initial set, and the final query is then built by combining these filters by types.

 %such as: $\{Keyword \to Time -> Position \to Time \to Keyword\} $

% I don't follow this example, but I think the algorithm is intuitive enough and we need space.  -Scott
% For example, let's suppose the algorithm determines the following filter sequence: $\{Q_{k_1}=\{ \neg natural\} \to Q_{t_1}=[50,2030] \to Q_{p_1}=[(10,60), (50,100)] \to Q_{t_2}=[60,1230] \to Q_{k_2}=\{ \neg fictive\}  \to  Q_{p_2}=[(10,60), (30,85)] \to  Q_{t_3}=[60,800] \}$.
% The final query is built by combining these filters by types as follows: $Q_k=Q_{k_1} \cup Q_{k_2}=\{\neg natural,\neg fictive\}$, $Q_t =Q_{t_1} \cap Q_{t_2} \cap Q_{t_3}=[60,800]$, and  $ Q_p=Q_{p_1}  \cap Q_{p_2}=[(10,60),$ $(30,85)]$, which gives $Q=[Q_k=\{\neg natural,\neg fictive\}\wedge Q_t=[60,800]\wedge Q_p = [(10,60),$ $(30,85)]]$.

Finally, we note that the Global Filter algorithm can use the Greedy Keyword Filter with the naive Greedy Time and Spatial Filters, which we refer to experimentally as {\bf Greedy}, or the Binary Partition Search variants, which we refer to experimentally as {\bf BPS}.
%time and position naive greedy algorithms to which we refer as Greedy Algorithm, or the keywords greedy algorithm with the time and position Binary Partition Search algorithms to which we refer as BPS Algorithm.

\subsection{Optimal MILP Solutions for Benchmarking}

Next, we propose an exact Mixed Integer Linear Progamming (MILP) optimization-based formulation to maximize EF1 and provide a benchmark for evaluating the two previous Global Filter algorithms (Greedy and BPS).  %Given the trivial solution of optimizing the expected precision (singleton) and expected recall (the whole collection), we consider in the following only the optimization of EF1.

\subsubsection{Fractional MILP Formulation} \hfill \\
We begin by reformulating the EF1 objective to prepare for further optimization steps by replacing the global sum of scores of all information elements with a constant $C = \sum_{j=1}^m S(j)$:
\begin{equation}
\begin{aligned}
    \emph{$EF1$} &= \dfrac{2\times \sum_{j=1}^m S(j)I(j)}{\sum_{j=1}^m I(j) + \sum_{j=1}^m S(j)} = \dfrac{2 \times \sum_{j=1}^m S(j)I(j)}{\sum_{j=1}^m I(j) + C}
\end{aligned}
\end{equation}

In order to obtain the EF1-optimal Global Filter, we let binary variables $\emph{I\textsubscript{filter}}($j$) \in \{0, 1\}$ indicate whether an information element $j$ is selected in each sub-filter and constrain that to be selected in the Global Filter (i.e., $I(j)=1$), $j$ must be selected in all filters (i.e., a conjunction).  This leads to the following fractional MILP formulation with sub-filter constraints to be defined later:
%intend to directly optimize the EF1 metric in terms of decision indication variables  for filter setting as follows:
% Don't use I(i)... confusing!
%\begin{align}
%\max_{\textit{filter vars}} \;\;
%& \dfrac{\sum_{j=1}^m S(i)I(j)}{\sum_{j=1}^m I(j) + C} \nonumber \\
%& \textrm{subject to constraints between {\it filter vars} and $I(j)$}
%\end{align}
\begin{equation}
\begin{aligned}
& \underset{I_{\mathit{filter}}(j)}{\text{maximize}}
& & \dfrac{\sum_{j=1}^m S(j)I(j)}{\sum_{j=1}^m I(j) + C} \\
& s.t
& & I(j) = \bigwedge I_{\mathit{filter}}(j) \\
\end{aligned} \label{eq:frac_milp}
\end{equation}

% Not necessary.  -Scott
%Note that our goal is to optimize the element set in terms of \emph{filters settings}. This means elements in the interface sharing the same property needs to be simultaneously added to the selected set. This is a unique property of our filter-based UI problem, which is different from the independent retrieval of each document in standard IR system.

\subsubsection{Transformation to a MILP} \hfill \\
While there are no direct solvers for fractional MILPs, we can transform~\eqref{eq:frac_milp} into a pure MILP form for which we have efficient and optimal solvers.  To do this, we use the Charnes-Cooper method \cite{Charnes1962} and Glover linearization method \cite{Glover1975} with big-M constraints, where auxiliary variables \emph{w(j)} and \emph{u} are  introduced\footnote{https://optimization.mccormick.northwestern.edu/index.php/Mixed-integer\_linear\_fractional\_programming\_(MILFP)}. Here, $w(j)$ is defined as $w(j)=I(j)\times u$ with $u$ defined as follows:
\begin{equation}
u = \dfrac{1}{\sum_{j=1}^m I(j) + C}
\end{equation}

Then, the EF1 optimization problem is able to be transformed into the following MILP problem:
\begin{equation}
\begin{aligned}
& \underset{w,u}{\text{maximize}}
& & \sum_{j=1}^m S(j)w(j) \\
& s.t
& & \sum_{j=1}^m w(j) + uC = 1 \\
& & & w(j) \leqslant u, \quad w(j) \leqslant M\times I(j)  \\
& & & w(j) \geqslant u - M\times [1-I(j)] \\
& & & u > 0,  \quad I(j) \in \{0, 1\}, \quad w(j) \geqslant 0 \label{eq:milp}
\end{aligned}
\end{equation}

\subsubsection{Constraints} \hfill \\
As our goal is to select elements through three sub-filters, we add three constraints to the above optimization.
\begin{enumerate}
\item {\bf Time Filter Constraint:} a two-element tuple ($t_{start}$, $t_{end}$) indicating respectively the start and the end of the time window.
\begin{equation}
\begin{aligned}
  I_{\mathit{time}}(j) &=
   \begin{cases}
     1, & \text{if $(t_{start} \leqslant t(j)) \land (t(j) \leqslant t_{end})$}  \\
     0, & \text{otherwise}
  \end{cases} \label{eq:cons1}
\end{aligned}
\end{equation}

\item {\bf Spatial Filter Constraint:} a four-element tuple ($x_{min}$, $y_{min}$, $x_{max}$, $y_{max}$) to create a bounding box filter in visualization interface.
\begin{equation}
\begin{aligned}
I_{\mathit{pos}}(j) & =\begin{cases}
1, & \text{if \ensuremath{(x_{min}\leqslant x(j))\land(x(j)\leqslant x_{max})\land}}\\
 & (y_{min}\leqslant y(j))\land(y(j)\leqslant y_{max})\\
0, & \text{otherwise}
\end{cases} \label{eq:cons2}
\end{aligned}
\end{equation}

\item {\bf Keyword Filter Constraint:} a boolean vector of terms $t^*_k$ with size $m$ - the size of the dictionary of the global collection.
\begin{equation}
  I_{\mathit{term}}(j) = \bigwedge_{t^*_k \in j} t^*_k \qquad \textnormal{for s = 1, 2, $\cdots$, m} \label{eq:cons3}
\end{equation}
All terms with $I_{\mathit{term}}=0$ are included in the negation query.

\item {\bf Global Filter Constraint:} for information element $j$ to be selected globally, it must be simultaneously selected by the three sub-filters.
%, all filter constraints have to be satisfied in this element. In others words, an AND operator is required between all the sub-filter constraints.
%{\bf TODO: mention how to apply to all filters... need to say an email j is selected if \emph{all} filters say it is selected, so an AND constraint.  \textcolor{red}{[PLEASE COMPLETE YIHAO]}.
%}
\begin{equation}
  I(j) = I_{\mathit{time}}(j) \land I_{\mathit{pos}}(j) \land I_{\mathit{keyword}}(j) \label{eq:cons4}
\end{equation}

\end{enumerate}
We refer to the above MILP formulation in~\eqref{eq:milp} with all filter constraints~\eqref{eq:cons1}--\eqref{eq:cons4} as the {\bf Optimal} Global Filter.

\subsection{Multiple Filter Selection Wrapper}

%The Global Filter selects a single best filter setting.  However, what if we want to display multiple possible Global Filters.
In practice a single Global Filter chosen by the previously described algorithms will narrow the user in on a single \textquotedblleft event\textquotedblright{}. However, there will likely be multiple anomalous events and so the user should have a choice of multiple filters.
Consider Figure~\ref{Fig:UseCase}: this actually shows three different spatial bounding boxes corresponding to three different events provided by Global Filters.
In this work, we provide an initial greedy approach for providing a ranked list of multiple filters (that is a wrapper approach working with any of the previously defined filtering algorithms -- Greedy, BPS, or Optimal); we leave it to future work to develop improved filtering and ranking methods for multiple filters.

The algorithm itself is quite simple.  After the first filter is produced, all selected elements by the filter have their scores $S(j)$ zeroed out.  The filtering algorithm is then run again, where it will inherently focus on a different content set.  This procedure is repeated until the desired number of filters is reached, or a metric / coverage score for high scoring content is reached.  The user should then be able to choose among the multiple filters in the VID.


% !TEX root = ../main.tex
% chktex-file 21
\section{Conclusion}%
\label{sec:conclusion}

In this paper we presented two approaches to speed up hyperparameter optimization: Fabolas and learning curve extrapolation.
Both use probabilistic models to estimate values of the error function.
Then four approaches to speed up training were presented: Bag of Little Bootstraps, subsample size selection for gradient descent, subsampling for logistic regression and \(k\)-means clustering for SVMs.\
All presented approaches are able to speedup training significantly on big datasets.
Since those approaches optimize different aspects of the training process, various combinations of them are possible to get further speedups.
We will now take a brief look at promising combinations.
\begin{itemize}
	\item Since Fabolas is a hyperparameter optimizer that does not make assumptions about the learner, it can be combined trivially with any of the training optimization approaches presented in Section~\ref{sec:params}.
	\item The learning curve extrapolation method described in Section~\ref{sec:hyperparams:earlyterm} requires a learner using gradient descent.
		It can thus be combined with the subsample size selection method described in Section~\ref{sec:params:samplesize}.
	\item Learning curve extrapolation could additionally be combined with Fabolas to guide the hyperparameter search.
		This would however require changing the covariance kernel of the cost model \(c\) since probes at suboptimal positions are likely to be terminated early, making them less costly and thus more attractive for the aquisition function \(a_F\).
		One possible approach to adapt the cost model is to add a dependence on the loss model \(f\).
		This would essentially merge the two Gaussian processes \(c\) and \(f\) into a single cost-loss model over the parameter space \(\Theta = \Lambda \times [0, 1] \times \{\mathrm{cost}, \mathrm{loss}\}\) where information about the loss of a configuration is indicative about its cost.
		Finding a suitable kernel function for this joint GP model could be a subject of further research.
	\item In principle Bag of Little Bootstraps (Section~\ref{sec:params:blb}) could be combined with the other three training optimization methods since it is a general purpose bagging method.
		However the combination of BLB with subsample size selection for gradient descent is problematic since the latter relies on dynamically adapting the sample size during training to reduce the expected variance.
		This approach does not work well if the training data is a resampled BLB bootstrap that does not allow for significant variance reduction since it only contains a small fraction of datapoints.
		The combinations of BLB with OSMAC and of BLB with WKM-SVM are more promising.
		In the case of OSMAC the sampling weights \(\pi_i\) could be used to sample the small bootstraps \(\check{X}\) (compare~\citet{Norazan2009}).
	\item Another promising combination is that of subsample size optimization (Section~\ref{sec:params:samplesize}) with OSMAC (Section~\ref{sec:params:osmac}).
		Since logistic regression is often implemented using gradient descent, both methods can be combined by simply using OSMAC to get the subsample for each SGD step.
\end{itemize}
Evaluating those combinations could be a subject of future work.


\chapter{datareset} \label{datareset}

\section{Introduction}

The \texttt{datareset} command assumes that certain parameters of the LPE determine which summands can be followed by which summands.
These parameters determine the control-flow of the LPE, and are therefore called \emph{control-flow parameters}.

\emph{Data parameters}, on the other hand, are used to store values, and often temporarily, which means that their values may not be used anymore after a particular state.
Data parameters can typically have different values after such a state, which causes them to add states to the state space for each of those values -- \emph{without} adding any new behavior!

The \texttt{datareset} command attempts to detect LPE control-flows and determine the parts of those control-flows where data parameters are no longer used.
At these locations, data parameters are set to a default value (`reset').
This removes states from the state space, potentially improving performance of subsequent computations.

\section{Formal background}

The control-flow analysis is based on work done in \cite{van2009state}.

\subsection{Parameter properties}

Each parameter $p$ of a summand $s$ has the following properties:
\begin{itemize}

\item Is the value of $p$ potentially altered by $s$?
If so, $p$ receives the label `changed'.

\item Does $p$ occur in the guard of $s$?
If so, $p$ receives the label `directly used'.

\item Has $p$ received the label `directly used' in $s$?
Or does $p$ occur in the assignment by $s$ to a parameter that has received the label as `changed'?
In either case, $p$ also receives the label `used'.

\item Must the value of $p$ have a specific (unique) value $v$ in order for $s$ to be enabled?
If so, $p$ receives the label `having a source' for $s$, with the source being $v$.

\item Does the value of $p$ have a specific (unique) value $v$ immediately after $s$ has been applied?
If so, $p$ receives the label `having a destination' for $s$, with the source being $v$.

\item Does $p$ have both a source and a destination for $s$?
Then $p$ is called a \emph{ruling parameter}, and $p$ is said to `rule' summand $s$.

\end{itemize}

\subsection{Control-flow graphs}

A parameter of the LPE is a \emph{control-flow parameter} of the LPE if for all summands it is either a ruling parameter or not \labeledas{changed} (or both).
A parameter that is not a control-flow parameter is defined as a \emph{data parameter}.

\vspace{1mm}

For each control-flow parameter $f$ of the LPE, a control-flow graph is constructed.
There is a state in the control-flow graph for each source and destination of $f$ (across all summands).
Two states $s_1$ and $s_2$ are connected by an edge $(s_1, i, s_2)$ if there is a summand $i$ where the source of $f$ is represented by one of the states and where the destination of $f$ is represented by the other state.
The direction of such an edge is from source state to destination state.

\subsection{Belongs-to function}

Here, the \emph{belongs-to} function is introduced.
The belongs-to function maps each data parameter $d$ to some set of control-flow parameters $b(d)$ as follows

\begin{align*}
b(d) = F \cap \bigcap\limits_{s \in S}^{} \text{ruling}(s)
\end{align*}

where

\begin{itemize}
\item $F$ is the set of all control-flow parameters;
\item $S$ is the set of all summands in which $d$ is \labeledaseither{changed}{used};
\item $\text{ruling}(s)$ is the set of all parameters that rule summand $s$.
\end{itemize}

\section{Algorithm}

The algorithm executes 3 phases: the preparatory phase, the iterative phase, and the deletion phase.

\subsection{Preparatory phase}

In this phase, the parameter properties, control-flow graphs and belongs-to function are computed.

\subsection{Iterative phase}

In the iterative phase, the so-called \emph{relevance relation} is computed.
This relation, with symbol $R$, relates a data parameter $d$, a control-flow parameter $f$, and a value $v$ if $d$ is `relevant' after a state in which $f$ has value $v$.
This is denoted $R(d, f, v)$.
Intuitively, this is a situation in which $d$ should \emph{not} be reset because its value may be needed in the future.

The computation of $R$ is a fixpoint algorithm: modifications are applied iteratively until $R$ no longer changes.
The initial value of $R$ is set to
\begin{align*}
R_0 = \bigcup\limits_{\substack{i \in S \\ d_k \in \text{directlyUsed}(i)}}^{} \;\{\; (d_k, d_j, \text{source}(i, d_j)) \;|\; d_j \in b(d_k) \;\}
\end{align*}

where

\begin{itemize}
\item $S$ is the set of all summands;
\item $\text{directlyUsed}(i)$ is a function that gives the set of all parameters that are \labeledas{directly used} in a summand $i$;
\item $\text{source}(i, f)$ is a function that gives the source value of a control-flow parameter $f$ for a summand $i$.
\end{itemize}

Each iteration can be split into two steps.
The first step checks for control-flow graphs in which a data parameter $d$ has already been \labeledas{relevant} whether this implies that $d$ is also relevant in preceding states (of the same control-flow graph).
If so, the appropriate triples are added to $R$:
\begin{align*}
R_{n}{'} = R_{n-1} \cup \bigcup\limits_{\substack{i \in S}}^{} \;\left\{\; (d_k, d_j, s) \;\middle|\; \substack{(d_l, d_j, t) \in R_{n-1} \\ d_j \in b(d_k) \\ d_k \in \text{vars}(v_i(d_l)) \\ (s, i, t) \in E_{d_j}} \;\right\}
\end{align*}

where

\begin{itemize}
\item $E_{f}$ is the set of edges that are part of the control-flow graph of control-flow parameter $f$.
\end{itemize}

The second step is similar, but data parameters that are found to be relevant are added as triples to $R$ in relation to \emph{another} control-flow parameter:
\begin{align*}
R_{n} = R_{n}{'} \cup \bigcup\limits_{\substack{i \in S}}^{} \;\left\{\; (d_k, d_j, \text{source}(i, d_j)) \;\middle|\; \substack{(d_l, d_p, t) \in R_{n}{'} \\ d_j \in b(d_k),\; d_j \notin b(d_l) \\ d_k \in \text{vars}(v_i(d_l)) \\ (r, i, t) \in E_{d_p}} \;\right\}
\end{align*}

\subsection{Deletion phase}

Finally, each summand of an LPE is subject to modification.
Modifications are made by making use of the belongs-to function $b$ and the relevance relation $R$.
Intuitively, we check whether the value of a parameter is `relevant' after a specific summand $i$ has been applied.
If so, the parameter should not be reset.
Otherwise, the parameter can be reset, for example to its initialization value:

\begin{align*}
v_{i}{'}(d_k) = \begin{cases}
v_{i}(d_k) & \text{if } \bigwedge\limits_{\substack{d_j \in \text{ruling}(i) \\ d_j \in b(d_k)}}^{} R(d_k, d_j, \text{dest}(i, d_j)) \\
v_{I}(d_k) & \text{otherwise}
\end{cases}
\end{align*}

where

\begin{itemize}
\item $\text{ruling}(s)$ is the set of all parameters that rule summand $s$;
\item $\text{source}(i, f)$ is a function that gives the destination value of a control-flow parameter $f$ for a summand $i$.
\end{itemize}

\section{Benchmark results}

The following durations were measured with a benchmark for several models:
\begin{itemize}
\item The average duration of \txs{} to make 500 steps in a model after it has been converted to LPE form;
\item The average duration of \txs{} to make 500 steps in a model after it has been converted to LPE form and after the \texttt{datareset} operation has been applied.
\end{itemize}

When plotting the second series of measurements against the first (see Figure~\ref{datareset-vs-lpe-only:fig}), it is easy to see that the impact is insignificant in most cases.

\begin{figure}[!ht]
\begin{center}
\includegraphics[width=0.7\linewidth]{charts/datareset-vs-lpe-only}
\caption{Benchmark results: datareset vs LPE transformation}
\label{datareset-vs-lpe-only:fig}
\end{center}
\end{figure}


% Copyright 2019 by Till Tantau
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Free Documentation License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.

\section{Three Dimensional Drawing Library}

\begin{tikzlibrary}{3d}
    This package provides some styles and options for drawing three dimensional
    shapes.
\end{tikzlibrary}

\subsection{Coordinate Systems}

\begin{coordinatesystem}{xyz cylindrical}
    The |xyz cylindrical| coordinate system allows to you specify a point in
    terms of cylindrical coordinates, sometimes also referred to as cylindrical
    polar coordinates or polar cylindrical coordinates. It is very similar to
    the |canvas polar| and |xy polar| coordinate systems with the difference
    that you provide an elevation over the $xy$-plane using the |z| key.
    %
    \begin{key}{/tikz/cs/angle=\meta{degrees} (initially 0)}
        The angle of the coordinate interpreted in the ellipse whose axes are
        the $x$-vector and the $y$-vector.
    \end{key}
    %
    \begin{key}{/tikz/cs/radius=\meta{number} (initially 0)}
        A factor by which the $x$-vector and $y$-vector are multiplied prior to
        forming the ellipse.
    \end{key}
    %
    \begin{key}{/tikz/cs/z=\meta{number} (initially 0)}
        Factor by which the $z$-vector is multiplied.
    \end{key}
    %
\begin{codeexample}[preamble={\usetikzlibrary{3d}}]
\begin{tikzpicture}[->]
  \draw (0,0,0) -- (xyz cylindrical cs:radius=1);
  \draw (0,0,0) -- (xyz cylindrical cs:radius=1,angle=90);
  \draw (0,0,0) -- (xyz cylindrical cs:z=1);
\end{tikzpicture}
\end{codeexample}
    %
\end{coordinatesystem}

\begin{coordinatesystem}{xyz spherical}
    The |xyz spherical| coordinate system allows you to specify a point in
    terms of spherical coordinates.
    %
    \begin{key}{/tikz/cs/radius=\meta{number} (initially 0)}
        Factor by which the $x$-, $y$-, and $z$-vector are multiplied.
    \end{key}
    %
    \begin{key}{/tikz/cs/latitude=\meta{degrees} (initially 0)}
        Angle of the coordinate between the $y$- and $z$-vector, measured from
        the $y$-vector.
    \end{key}
    %
    \begin{key}{/tikz/cs/longitude=\meta{degrees} (initially 0)}
        Angle of the coordinate between the $x$- and $y$-vector, measured from
        the $y$-vector.
    \end{key}
    %
    \begin{key}{/tikz/cs/angle=\meta{degrees} (initially 0)}
        Same as |longitude|.
    \end{key}
    %
\begin{codeexample}[preamble={\usetikzlibrary{3d}}]
\begin{tikzpicture}[->]
  \draw (0,0,0) -- (xyz spherical cs:radius=1);
  \draw (0,0,0) -- (xyz spherical cs:radius=1,latitude=90);
  \draw (0,0,0) -- (xyz spherical cs:radius=1,longitude=90);
\end{tikzpicture}
\end{codeexample}
    %
\end{coordinatesystem}

\subsection{Coordinate Planes}

Sometimes drawing with full three dimensional coordinates is not necessary and
it suffices to draw in two dimensions but in a different coordinate plane.  The
following options help you to switch to a different plane.

\subsubsection{Switching to an arbitrary plane}

\begin{key}{/tikz/plane origin=\meta{point} (initially {(0,0)})}
    Origin of the plane.
\end{key}

\begin{key}{/tikz/plane x=\meta{point} (initially {(1,0)})}
    Unit vector of the $x$-direction in the new plane.
\end{key}

\begin{key}{/tikz/plane y=\meta{point} (initially {(0,1)})}
    Unit vector of the $y$-direction in the new plane.
\end{key}

\begin{key}{/tikz/canvas is plane}
    Perform the transformation into the new canvas plane using the units above.
    Note that you have to set the units \emph{before} calling
    |canvas is plane|.
    %
\begin{codeexample}[preamble={\usetikzlibrary{3d}}]
\begin{tikzpicture}[
    ->,
    plane x={(0.707,-0.707)},
    plane y={(0.707,0.707)},
    canvas is plane,
]
    \draw (0,0) -- (1,0);
    \draw (0,0) -- (0,1);
\end{tikzpicture}
\end{codeexample}
    %
\end{key}

\subsubsection{Predefined planes}

\begin{key}{/tikz/canvas is xy plane at z=\meta{dimension}}
    A plane with
    %
    \begin{itemize}
        \item |plane origin={(0,0,|\meta{dimension}|)}|,
        \item |plane x={(1,0,|\meta{dimension}|)}|, and
        \item |plane y={(0,1,|\meta{dimension}|)}|.
    \end{itemize}
\end{key}

\begin{key}{/tikz/canvas is yx plane at z=\meta{dimension}}
    A plane with
    %
    \begin{itemize}
        \item |plane origin={(0,0,|\meta{dimension}|)}|,
        \item |plane x={(0,1,|\meta{dimension}|)}|, and
        \item |plane y={(1,0,|\meta{dimension}|)}|.
    \end{itemize}
\end{key}

\begin{key}{/tikz/canvas is xz plane at y=\meta{dimension}}
    A plane with
    %
    \begin{itemize}
        \item |plane origin={(0,|\meta{dimension}|,0)}|,
        \item |plane x={(1,|\meta{dimension}|,0)}|, and
        \item |plane y={(0,|\meta{dimension}|,1)}|.
    \end{itemize}
\end{key}

\begin{key}{/tikz/canvas is zx plane at y=\meta{dimension}}
    A plane with
    %
    \begin{itemize}
        \item |plane origin={(0,|\meta{dimension}|,0)}|,
        \item |plane x={(0,|\meta{dimension}|,1)}|, and
        \item |plane y={(1,|\meta{dimension}|,0)}|.
    \end{itemize}
\end{key}

\begin{key}{/tikz/canvas is yz plane at x=\meta{dimension}}
    A plane with
    %
    \begin{itemize}
        \item |plane origin={(|\meta{dimension}|,0,0)}|,
        \item |plane x={(|\meta{dimension}|,1,0)}|, and
        \item |plane y={(|\meta{dimension}|,0,1)}|.
    \end{itemize}
\end{key}

\begin{key}{/tikz/canvas is zy plane at x=\meta{dimension}}
    A plane with
    %
    \begin{itemize}
        \item |plane origin={(|\meta{dimension}|,0,0)}|,
        \item |plane x={(|\meta{dimension}|,0,1)}|, and
        \item |plane y={(|\meta{dimension}|,1,0)}|.
    \end{itemize}
\end{key}

\subsection{Examples}

\begin{codeexample}[preamble={\usetikzlibrary{3d}}]
\begin{tikzpicture}[z={(10:10mm)},x={(-45:5mm)}]
  \def\wave{
    \draw[fill,thick,fill opacity=.2]
     (0,0) sin (1,1) cos (2,0) sin (3,-1) cos (4,0)
           sin (5,1) cos (6,0) sin (7,-1) cos (8,0)
           sin (9,1) cos (10,0)sin (11,-1)cos (12,0);
    \foreach \shift in {0,4,8}
    {
      \begin{scope}[xshift=\shift cm,thin]
        \draw (.5,0)  -- (0.5,0 |- 45:1cm);
        \draw (1,0)   -- (1,1);
        \draw (1.5,0) -- (1.5,0 |- 45:1cm);
        \draw (2.5,0) -- (2.5,0 |- -45:1cm);
        \draw (3,0)   -- (3,-1);
        \draw (3.5,0) -- (3.5,0 |- -45:1cm);
      \end{scope}
    }
  }
  \begin{scope}[canvas is zy plane at x=0,fill=blue]
    \wave
    \node at (6,-1.5) [transform shape] {magnetic field};
  \end{scope}
  \begin{scope}[canvas is zx plane at y=0,fill=red]
    \draw[help lines] (0,-2) grid (12,2);
    \wave
    \node at (6,1.5) [rotate=180,xscale=-1,transform shape] {electric field};
  \end{scope}
\end{tikzpicture}
\end{codeexample}

\begin{codeexample}[preamble={\usetikzlibrary{3d}}]
\begin{tikzpicture}
  \begin{scope}[canvas is zy plane at x=0]
    \draw (0,0) circle (1cm);
    \draw (-1,0) -- (1,0) (0,-1) -- (0,1);
  \end{scope}

  \begin{scope}[canvas is zx plane at y=0]
    \draw (0,0) circle (1cm);
    \draw (-1,0) -- (1,0) (0,-1) -- (0,1);
  \end{scope}

  \begin{scope}[canvas is xy plane at z=0]
    \draw (0,0) circle (1cm);
    \draw (-1,0) -- (1,0) (0,-1) -- (0,1);
  \end{scope}
\end{tikzpicture}
\end{codeexample}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "pgfmanual-pdftex-version"
%%% End: