\section{Propane overview}
\label{sec:propane}

Policies for (distributed) control planes differ from data-plane
policies in a few important ways. First, control-plane
policies must account for all failures at compile time; there is
no controller at runtime, so the routers must be configured in advance to handle failures in a compliant manner.
%
In \sysname, we enable such specifications through a notion of {\em path preferences}, with the semantics that a less-preferred path is taken only when a higher-preference path is unavailable in the network.
%
Second, paths in a control-plane policy may be under-specified (e.g.,
"prefer customer" does not indicate a concrete path). The \sysname
compiler treats such under-specifications as constraints on the set of
allowed paths and automatically computes valid sets based on topology.
%

In the rest of this section, we define \sysname policies for the two
examples from the previous section, introducing key aspects of
the \sysname language along the way. The complete syntax of the
language as well as our strategy for compiling \sysname policies to
BGP is defined in the next section.

\subsection{Example 1: The Backbone}

%\sysname simplifies network configuration by allowing users to
%specify end-to-end forwarding paths and associating them with
%appropriate prefixes.  The \sysname compiler handles the task of
%generating low-level BGP configurations consistent with the user's
%specifications.  It automatically synthesizes
%import-export filters, local preferences, MED attributes, and community tags
%to ensure policy compliance under all possible failure scenarios.

\sysname allows operators to configure the network with the
abstraction that they have centralized control over routing.
Specifically, the operator simply provides a set of high-level constraints
that describe the
different routes traffic may (or may not) take and their relative preferences.
\sysname specifications are written modularly via a series
of declarations.
%These definitions allow users to express the three
%major elements of any \sysname specification:  \emph{prefixes},
%\emph{paths} and \emph{policies}.
For example, to begin specification of the backbone
network from the previous section, we first express the idea that for routes leading to our customer,
we prefer using \CD{R1} over \CD{R2} (policy P3 from Figure~\ref{fig:example1}):
%\begin{lstlisting}[mathescape=true]
%define ExitCust = $exit(R_1 \gg R_2)$
%\end{lstlisting}
\begin{code}
\Define ExitCust = \Exit(R1 \Prefer R2)
\end{code}
This  statement defines a set of \emph{ranked paths}, which includes
all paths (and only those paths) for which traffic exits our network
through either router \CD{R1} or \CD{R2}.  The paths that exit through \CD{R1}
are preferred (\Prefer) to those that exit through \CD{R2}.  As we
describe in the next section, the \Exit\ predicate, as well as other
path predicates used later in this section, is simply a shorthand for
a particular regular expression over paths that is expressible in our policy
language.

% DAVE CONVERSIONS
% C_{pfx} ===> PCust
% I_{pfx} ===> PInternal

To associate ranked paths with
one or more prefixes, we define a \sysname \emph{policy}.
Within a policy, statements with the form $t$\Path$p$
associate the prefixes defined by the predicate $t$ with the set of
ranked paths defined by the path expression $p$.  In general,
prefix predicates can be defined by arbitrary boolean combinations
(and, or, not) of concrete prefixes and community tags.  Here,
we assume we have already defined predicates \CD{PCust} for the
customer prefixes and \CD{PInt} for our internal prefixes.
In the following code, ranked paths are associated with
customer prefixes, internal prefixes,
and all other prefixes (\CD{true}).\footnote{Policy statements are processed in
order with earlier policy statements taking precedence over later
policy statements.  Hence, when the prefix predicate \True{} follows
statements involving \CD{PCust} and \CD{PInt}, it is interpreted as
\CD{\True{} \AND{} \NOT{}PCust \AND{} \NOT{}PInt}.}

%% \begin{lstlisting}[mathescape=true]
%% define Routing = {
%%     $\path{C_{pfx}}{ExitCust \gg end(Cust)}$
%%     $\path{I_{pfx}}{end(in)}$
%%     $\path{true}{ExitCust \gg exit(Peer) \gg exit(Prov)}$
%% }
%% \end{lstlisting}

\begin{code}
\Define Routing =
    \{PCust \Path ExitCust \Prefer \End(Cust)
     PInt  \Path \End(\In)
     \True  \Path ExitCust \Prefer \Exit(Peer) \Prefer \Exit(Prov)\}
\end{code}
\noindent
Line 2 of this policy
defines the paths that exit our network to customer prefixes---\CD{ExitCust} defines paths through \CD{R1} and \CD{R2}, and in the event
that connections to the customer through both routers fail,
a backup route (\CD{\End(Cust)}) is defined that admits traffic along
any path (including those through other networks) that ends at the customer.
Line 3 states that traffic for internal prefixes must end in our network, and is otherwise unconstrained.  The special keyword \In{} represents any location
inside the user's network and the keyword \Out{} represents any location
outside the user's network.
Line 4 applies to any other traffic, and allows any routes that leave through a peer with a preference for customers over peers over providers. To summarize our progress, the \CD{Routing} policy
implements P1, P3, and P4 from Figure~\ref{fig:example1}.

Since by default routing allows transit traffic (\EG, traffic entering from
\CD{Peer} and leaving through \CD{Prov}), we separately define a
policy to enforce P2 and P5 from Figure~\ref{fig:example1}, using conjunction (\AND),
disjunction (\OR) and negation (\NOT) of constraints.
%union (\Union), intersection (\Intersect), and negation (\NOT) to
%specify the appropriate sets of paths.
%
%% \begin{lstlisting}[mathescape=true]
%% define PP = Peer or Prov
%% define PPTransit = $enter(PP) \AND exit(PP)$
%% define CustTransit = $later(Cust) \AND later(Prov)$

%% define NoTransit = {
%%     $\path{true}{\neg PPTransit \AND \NOT CustTransit}$
%% }
%% \end{lstlisting}
\begin{code}
\Define PP = Peer \OR Prov
\Define PPTrans   = \Enter(PP) \AND \Exit(PP)
\Define CustTrans = \Later(Cust) \AND \Later(Prov)
\Define NoTrans   = \{\True \Path \NOT{}PPTrans \AND \NOT{}CustTrans\}
\end{code}
\noindent
The \CD{NoTrans} constraint above says that for any prefix, traffic should never both enter and exit the network from \CD{Peer} or \CD{Prov}. Similarly it prevents traffic from ever following paths that leave our network and later passing through both \CD{Prov} and \CD{Cust}.  To implement both \CD{Routing}
and \CD{NoTrans} simultaneously, we simply conjoin them: \CD{Routing \AND{} NoTrans}.
%
%% \begin{lstlisting}[mathescape=true]
%% Routing $\AND$  NoTransit
%% \end{lstlisting}
%
These constraints capture the entire policy. From them, \sysname will generate per-device import and export filters, local preferences,
MED attributes, and community tags to ensure that the policy is
implemented correctly under all failure cases.

\subsection{Example 2: The Data Center}

In our data center,
there are three main concerns:
(1) traffic for the prefix block allocated to each top-of-rack router must be able to reach that router,
(2) local services must not leak outside the data center, and
(3) aggregation must be performed on global prefixes to reduce churn
in the network.

\sysname allows us to specify each of these constraints modularly. The first constraint is about prefix ownership---namely, we only want traffic for certain prefixes to end up at a particular location. The following definition captures this intent.

%% \begin{lstlisting}[mathescape=true]
%% define Ownership = {
%%     $\path{p_{g1}}{end(A)}$
%%     $\path{p_{g2}}{end(B)}$
%%     $\path{p_{l1}}{end(E)}$
%%     $\path{p_{l2}}{end(F)}$
%% }
%% \end{lstlisting}

\begin{code}
\Define Ownership =
    \{PG1 \Path \End(A)
     PG2 \Path \End(B)
     PL1 \Path \End(E)
     PL2 \Path \End(F)
     \True \Path \End(\Out)\}
\end{code}
\noindent
In English: traffic for prefix \CD{PG1} is only allowed to follow paths that
end at router \CD{A}; traffic matching \CD{PG2},
but not \CD{PG1}, must
end at router \CD{B}; and so on.
Any traffic destined for a prefix that not a part of the data center should be allowed to leave the data center and end at some external location.
For the second constraint, we can define another policy:

%% \begin{lstlisting}[mathescape=true]
%% define Routing = {
%%     $\path{p_{g*}}{any}$
%%     $\path{p_{l*}}{\NOT enter(out)}$
%%     $\path{true}{exit(out)}$
%% }
%% \end{lstlisting}
\begin{code}
\Define Locality =
    \{PL1 \OR PL2 \Path \textbf{only}(\In)\}
\end{code}
\noindent
%The first line states that there is no restriction (\Any)
%on how traffic must traverse the network for global prefixes.
%aside from the default restriction
%that traffic must not pass through the user's network and then loop
%back on itself.
%This means traffic for
%\CD{PG*} may be sent either from other data center routers or
%from external ASes.
The locality constraint ensures that traffic for local
prefixes only ever follows paths that are internal to the network at each hop.
This constraint guarantees that the services remain reachable only to locations
inside the data center.

As in the backbone example, we can logically conjoin these constraints
to specify the network-wide policy.
In addition to constraints on the shape of paths,
\sysname allows the operator to specify constraints on the BGP control plane.
For instance, a constraint on aggregation is included to ensure that
aggregation for global prefixes is performed from
locations inside (\In) the network to locations outside (\Out).
In this case, \CD{PG1} and \CD{PG2} will use the aggregate \CD{PG}
(which we assume is defined earlier using an appropriate prefix such
as \CD{74.3.28.0/24})
when advertised outside the datacenter.

\begin{code}
Ownership \AND{} Locality \AND{} \Agg(PG, \In \Link \Out)
\end{code}

As before, once \sysname compiles the policy, it is guaranteed to hold under all possible failure scenarios. \sysname also ensures safety against aggregation-induced black holes.
%It can efficiently determine a lower bound on the number of failures necessary to black hole traffic given the user's policy, topology, and placement of specific aggregates.

%%%%OLD TEXT BELOW
%
%\section{Propane}
%\label{sec:propane}
%
%\sysname simplifies network configuration by automatically generating low-level BGP configurations from a high-level specification of the network's routing policy.
%%
%The operator configures the network with the abstraction that he or she has centralized control over routing and uses a set of high-level constraints to describe the different routes traffic may (or may not) take and their relative preferences.
%%
%The \sysname compiler generates BGP configurations for each device in the network that operate in a completely distributed fashion and are correct by construction -- automatically synthesizing import/export filters, local preference and MED attributes, and community tags to ensure policy compliance under all possible failure scenarios.
%%
%We now demonstrate \sysname by showing how to configure the networks from the previous examples.
%
%\para{Example 1}
%
%We now show how to write the routing policy for the backbone network in Section~\ref{sec:motivation}. \sysname allows to address each of the requirements in turn. First we address requirements (P1), (P3), and (P4) that customers are preferred over peers over providers, and that a customer appear on the path for its prefix. The following \sysname code accomplishes these two objectives:
%
%\begin{lstlisting}[mathescape=true]
%define ExitCust = $exit(R_1 \gg R_2)$
%
%define Routing = {
%    $\path{C_{pfx}}{ExitCust \gg end(Cust) }$
%    $\path{I_{pfx}}{end(in)}$
%    $\path{true}{ExitCust \gg exit(Peer) \gg exit(Prov)}$
%}
%\end{lstlisting}
%
%The first definition defines paths that exit our network to the customer. The statement $exit(R_1 \gg R_2)$ restricts us to paths that leave our network through either router $R_1$ or $R_2$, with a preference for $R_1$.
%The first line of the \textsf{Routing} policy ensures that traffic destined for customer prefixes ($C_{pfx}$) either leaves the network directly to the customer (with a preference for leaving through $R_1$), or simply ends at the customer. Not leaving directly to the customer by going through \textit{Peer} or \textit{Prov} is less preferred than leaving directly, and thus serves as a backup route in a direct route is not available.
%
%The second line state that traffic for internal prefixes $I_{pfx}$ must end in our network, and is otherwise unconstrained. The last line applies to any other traffic, and allows for any routes that leave through a peer with a preference for customers over peers over providers. While this still allows transit traffic (e.g., traffic can enter from \textit{Peer} and leave through \textit{Prov}), we can restrict this behavior separately as follows:
%
%\begin{lstlisting}[mathescape=true]
%define PP = Peer or Prov
%define PPTransit = $enter(PP) \wedge exit(PP)$
%define CustTransit = $later(Cust) \wedge later(Prov)$
%
%define NoTransit = {
%    $\path{true}{\neg PPTransit \wedge \neg CustTransit}$
%}
%\end{lstlisting}
%
%The \textsf{NoTransit} constraint above ensures that requirements (P2) and (P5) are satisfied. In particular, it says that, for any prefix, traffice should never both enter and exit the network from \textit{Peer} or \textit{Prov}. Similarly it prevents traffic from ever following paths that leave our network and later go through both \textit{Prov} and \textit{Cust}.
%
%\begin{lstlisting}[mathescape=true]
%Routing $\wedge$  NoTransit
%\end{lstlisting}
%
%The final policy is simply the conjunction of the routing and no transit constraints. \sysname will generate per-device import and export filters, local preferences and MED attributes, and community tags to ensure that the policy is met under all failure scenarios.
%
%
%\para{Example 2}
%
%Consider again the datacenter from Section~\ref{sec:motivation}. In this example, there are primarily three main concerns: (1) traffic for the prefix block allocated to each top-of-rack router can reach that router, (2) local services do not leak outside the datacenter, and (3) aggregation is performed on global prefixes to reduce churn in the network.
%
%\sysname allows us to decompose and specify each of these constraints in a modular fashion. The first constraint is about prefix ownership -- namely, that we only want traffic for certain prefixes to end up at a particular locations. The following definition in propane captures this intent:
%
%\begin{lstlisting}[mathescape=true]
%define Ownership = {
%    $\path{p_{g1}}{end(A)}$
%    $\path{p_{g2}}{end(B)}$
%    $\path{p_{l1}}{end(E)}$
%    $\path{p_{l2}}{end(F)}$
%}
%\end{lstlisting}
%
%The constraints in the ownership task are read in a top-down fashion. Traffic for prefix $p_{g1}$ is only allowed to to follow paths that end at router A. Similarly, traffic not matching $p_{g1}$, but which matches $p_{g2}$ must end at router B and so on.
%%
%To capture the second constraint, we can define another task for the core routing policy:
%
%\begin{lstlisting}[mathescape=true]
%define Routing = {
%    $\path{p_{g*}}{any}$
%    $\path{p_{l*}}{\neg enter(out)}$
%    $\path{true}{exit(out)}$
%}
%\end{lstlisting}
%
%The first line states that there is no restriction on how traffic must traverse the network for global prefixes. This means traffic for $p_{g*}$ may be sent either from other routers in the datacenter, or from external ASes. The second line ensures that traffic for local prefixes never enters the network from an outside location. This  guarantees that the services remain reachable only to locations internal to the data center.
%
%Finally, we can combine these constraints logically to specify the network-wide policy:
%
%\begin{lstlisting}[mathescape=true]
%Ownership $\wedge$ Routing $\wedge$ agg($p_{agg}$, $in \rightarrow out$)
%\end{lstlisting}
%
%
%In addition to constraints on the shape of paths, \sysname allows the operator to specify constraints on the BGP control plane. Above, a constraint on aggregation is included to ensure that an aggregation for $p_{agg}$ is performed from locations inside the network to locations outside the network.
%
%Once \sysname compiles the policy, it is guaranteed to hold under all possible failure scenarios. Similarly, it can check for aggregate-induced black holes up to $k$ failures.
%
%
%
%


% !TEX program = XeLaTeX
\documentclass[11pt,a4paper]{report}
\usepackage{amsmath,braket}

%-------------Fonts---------------
%\usepackage{newpxtext}      % \rm for palatino and \sf for Helvetica
\usepackage{euler}           % mathfont
\usepackage{fontspec}
\setmainfont{Optima nova LT} % set from system fonts
%\setsansfont{Optima nova LT}
%---------------------------------

%\usepackage{mfirstuc,biblatex}
\usepackage{geometry}
\geometry{margin=1.25in}
% for normal ms word {left=1.25in,right=1.25in,top=1in,bottom=1in}
% for book or binding {inner=1in,outer=1.25}
%\fontsize{20pt}{2}
\linespread{}
\setlength{\parindent}{5ex}
\setlength{\parskip}{1.5ex}
\setlength{\fboxrule}{1.2pt}

\usepackage{fancyhdr}
%\fancypagestyle{plain}{
%    \fancyhf{}
%}

%--------------Body---------------
\begin{document}

\setcounter{chapter}{4}
\chapter{Approximation methods}
\section*{\S1 time-independent perturbation theory}

\thispagestyle{fancy}
\fancyhead{}
\fancyhead[L]{\it Introduction to Quantum Mechanics\\ University of Science and Technology of China}
\fancyhead[R]{\it Professor Yiheng Lin\\ May 21, 2019}

We start with the problem really close to a solved problem. then we can use the solution at hand to do approximation.

Recap Taylor - expansion:
\begin{equation}
   f(x)\simeq f(x_0)+f'(x_0)(x-x_0)+\cdots+\frac{f^n(x_0)}{n!}(x-x_0)^n+\cdots .
\end{equation}
If keep all the way to $(x-x_0)^2 \longrightarrow$ do a fit with a polynomial

For quantum mechanics:
\begin{equation}
    H=H_0+H' ,
\end{equation}
$H_0$ has a known solution for eigen energy $E^{(0)}_n$ , and eigenstates$\set{\ket{n^{(0)}}}$ :
\begin{equation}
    H_0\ket{n^{(0)}}=E^{(0)}_n\ket{n^{(0)}} ,
\end{equation}
$H'$ is perturbational part of Hamiltonian, it can express as:
\begin{equation}
    H'=\lambda V ,
\end{equation}
where $\lambda$ is a number, and $\lambda\ll1$ . $V$ is another part of Hamiltonian.

Then we try to find $H\ket{n}=E_n\ket{n}$ . $E_n$ must be a function of $H$ ,which is a function of $\lambda$ , so as $\ket{n}$ . Therefore we can make a Taylor expansion of $E_n(\lambda)$ and $\ket{n(\lambda)}$ :
\begin{equation}
    \begin{aligned}
        E_n(\lambda)&=E_n^{(0)}+\lambda E^{(1)}_n+\lambda^2 E^{(2)}_n+\cdots ,
        \\
        \ket{N(\lambda)}&=\ket{n^{(0)}}+\lambda \ket{n^{(1)}}+\lambda^2 \ket{n^{(2)}}+\cdots ,
    \end{aligned}
\end{equation}
where $\lambda E^{(1)}_n$ is first order energy shift. $\lambda E^{(2)}_n$ is second order energy shift. Plug the expansion back to $H\ket{n}=E\ket{n}$ with $H=H_0+\lambda V$:
\begin{multline}
    (H_0+\lambda V)\cdot (\ket{n^{(0)}}+\lambda \ket{n^{(1)}}+\lambda^2\ket{n{(2)}}+\cdots)\\=(E^{(0)}_n+\lambda E^{(1)}+\lambda^2E^{(2)}+\cdots)\cdot (\ket{n^{(0)}}+\lambda \ket{n^{(1)}}+\lambda^2\ket{n^{(2)}}+\cdots) ,
\end{multline}
do not contain $\lambda$, Equation (1.6) $\Rightarrow H_0\ket{n^{(0)}}=E^{(0)}_n\ket{n^{(o)}}$, for $\lambda^1$ term we can get:
\begin{equation}
    H_0\ket{n^{(1)}}+V\ket{n^{(0)}}=E^{(0)}_n\ket{n^{(1)}}+E^{(1)}_n\ket{n^{(0)}} ,
\end{equation}
apply $\bra{n^{(0)}}$ to the left:
\begin{equation}
    \boxed{ \braket{n^{(0)}|V|n^{(0)}}=E^{(1)}_n , }
\end{equation}
which is {\bf first order pertubrbation for $E_n$}, plug in to $E_n=E^{(0)}_n+\lambda E^{(1)}_n $, we can get:
\begin{equation}
    \begin{aligned}
        E_n= E^{(0)}_n+ \braket{n^{(0)}|\lambda V|n^{(0)}} \to \boxed {E_n=E^{(0)}+\braket{n^{(0)}|H'|n^{(0)}} . }
    \end{aligned}
\end{equation}

For $H=H_0+\lambda V$ , we can express it in the energy representation in $\set{\ket{n^{(0)}}}$:
\[ H_0 =
\begin{pmatrix}
     E^{(0)}_0& 0         & 0         & \ldots    & 0         \\
     0        & E^{(0)}_1 & 0         & \ldots    & 0         \\
     0        & 0         & E^{(0)}_2 & \ldots    & 0         \\
     \vdots   & \vdots    & \vdots    & \ddots    & \vdots    \\
     0        & 0         & 0         & \ldots    & E^{(0)}_n \\
\end{pmatrix} . \]
$\lambda V$ in the same basis can express as:
\[ \lambda V =
\begin{pmatrix}
    \lambda V_{11} & \lambda V_{12} & \ldots & \lambda V_{1n} \\
    \lambda V_{21} & \lambda V_{22} & \ldots & \lambda V_{2n} \\
    \vdots         & \vdots         & \ddots & \vdots         \\
    \lambda V_{n1} & \lambda V_{n2} & \ldots & \lambda V_{nn} \\
\end{pmatrix} . \]
For $n=0$, $E_0\simeq E^{(0)}_0+\lambda V_{11}$. First order perturbation is just n row, n colunm for matrix $H_0+\lambda V$ in $\set{\ket{n^{(0)}}}$ basis.

Now we try to find first order pertubrbation for state. From Equation (1.7) and (1.8):
\begin{equation*}
    \begin{aligned}
        H_0\ket{n^{(1)}}+V\ket{n^{(0)}}&=E^{(0)}_n\ket{n^{(1)}}+E^{(1)}_n\ket{n^{(0)}} , \\
        \braket{n^{(0)}|V|n^{(0)}}&=E^{(1)}_n ,
    \end{aligned}
\end{equation*}
we try to solve $\ket{n^{(1)}}$, we can apply $\bra{k^{(0)}}$ to left, with $k\neq n$, then $\braket{k^{(0)}|n^{(0)}}=0$. we also require $E^{(0)}\neq E^{(0)}$. We can get
\begin{equation}
    \begin{aligned}
    \braket{k^{(0)}|H_0|n^{(1)}}+\braket{k^{(0)}|V|n^{(0)}}&=E^{(0)}_n\braket{k^{(0)}|n^{(1)}}+E^{(1)}_n\braket{k^{(0)}|n^{(0)}} , \\
    E^{(0)}_k\braket{ k^{(0)} | n^{(1)} }+\braket{k^{(0)}|V|n^{(0)}}&=E^{(0)}_n\braket{k^{(0)} | n^{(1)}} .
    \end{aligned}
\end{equation}
The term $\braket{k^{(0)} | n^{(1)}}$ is a inner product between an unknown state $\ket{n^{(1)}}$ and a state in the known basis, it called ``amplitude''.

We want to solve the state $\ket{n^{(1)}}$ , from the {\bf Superposition Principle}, we know that $\ket{n^{(1)}}=\sum_k{C_k \ket{k^{(0)}}}$ , since $\set{\ket{k^{(0)}}}$ form a basis. If we can solve every $C_k$ , the state $\ket{n^{(1)}}$ is known. We can easily observe that $\braket{k^{(0)} | n^{(1)}} \to \sum_k{ C_k \braket{ k^{(0)} | n^{(0)} } } \to \sum_k{ C_k \delta_{k,n} } \to C_k$ , so all we need is to solve $\braket{ k^{(0)} | n^{(1)} }$ .
\newpage From Equation (5.10) we can solve $\braket{ k^{(0)} | n^{(1)} }$ , which qual to $C_k$ :
\begin{equation}
    \boxed{ C_k = \frac{\braket{k^{(0)}|V|n^{(0)}}}{E^{(0)}_n-E^{(0)}_k} , }
\end{equation}
notice that $k\neq n$ . Therefore, we can get the {\bf first order perturbation for state $\ket{n}$ }
\begin{equation}
    \boxed{ \ket{n^{(1)}} = \sum_{k\neq m} \frac{ \braket{ k^{(0)}|V | n^{(0)} } }{ E^{(0)}_n - E^{(0)}_k } \ket{ k^{(0)}}, }
\end{equation}
plug it and $H'=\lambda V$ back to $ \ket{n} = \ket{n^{(0)}} + \lambda \ket{n^{(1)}} $ we get
\begin{equation}
    \boxed{ \ket{n} = \ket{n^{(0)}} + \sum_{k\neq m} \frac{ \braket{ k^{(0)}|H'| n^{(0)} } }{ E^{(0)}_n - E^{(0)}_k } \ket{ k^{(0)}} , }
\end{equation}
where $\braket{k^{(0)}|H'|n^{(0)}}$ can be the matrix element in the $k$ row and $n$ colum of matrix $H'$ in the basis of $\set{\ket{n^{(0)}}}$ :
\[ H_0+H' \xrightarrow{matirx \hspace{1ex} in \hspace{1ex} \set{\ket{n^{(0)}}}}
\begin{pmatrix}
    &  &  &  \\
    & E^{(0)}_{ka} & H^{(0)}_{kn} & \\
    &  & E^{(0)}_{bn} & \\
    &  & & \\
\end{pmatrix} . \]
\subsection*{Example： Spin - $\dfrac{1}{2}$ system}
We have
\begin{equation*}
    H_0=\Omega \sigma_z \to \begin{pmatrix}
     \Omega & 0 \\
     0 & \Omega \\
    \end{pmatrix} ; \hspace{2em}
    H'=\lambda \sigma_x \to \begin{pmatrix}
        0 & \lambda \\
        \lambda & 0 \\
       \end{pmatrix} ,
\end{equation*}
The Hamiltonian is
\begin{equation*}
    H=H_0+H' \to \begin{pmatrix}
        \Omega & \lambda \\
        \lambda & \Omega \\
    \end{pmatrix} , and \hspace{1em} \Omega \gg \lambda
\end{equation*}
we can solve eigenenergy
\begin{equation*}
    E_1=\Omega + \frac{\lambda^2}{2\Omega} , \hspace{1em} E_2=-\Omega - \frac{\lambda^2}{2\Omega} ,
\end{equation*}
and the eigenstates
\begin{equation*}
    \ket{\psi}_1= \begin{pmatrix}
        1-\frac{\lambda^2}{2\Omega^2} \\
        \frac{\lambda}{2\Omega} \\
    \end{pmatrix} ,
\end{equation*}
\begin{equation*}
    \ket{\psi}_2= \begin{pmatrix}
        -\frac{\lambda}{2\Omega} \\
        1-\frac{\lambda^2}{2\Omega^2} \\
    \end{pmatrix} .
\end{equation*}
\end{document}

Lecture notes/20190521 Ch5 approximation methods.tex


\par
\section{Driver programs for the {\tt Chv object}}
\label{section:Chv:drivers}
\par
%=======================================================================
\begin{enumerate}
%-----------------------------------------------------------------------
\item
\begin{verbatim}
test_addChevron msglvl msgFile nD nU type symflag seed alphareal alphaimag
\end{verbatim}
This driver program tests the {\tt Chv\_addChevron} method.
Use the script file {\tt do\_addChevron} for testing.
When the output file is loaded into matlab,
the last line to the screen is the error of the assembly.
\par
\begin{itemize}
\item
The {\tt msglvl} parameter determines the amount of output.
Use {\tt msglvl = 1} for just timing output.
\item
The {\tt msgFile} parameter determines the message file --- if {\tt
msgFile} is {\tt stdout}, then the message file is {\it stdout},
otherwise a file is opened with {\it append} status to receive any
output data.
\item
The {\tt nD} parameter is the number of rows and columns
in the (1,1) block.
\item
The {\tt nU} parameter is the number of columns
in the (1,2) block.
\item
The {\tt type} parameter denotes the type of entries ---
{\tt SPOOLES\_REAL} or {\tt SPOOLES\_COMPLEX}
\item
The {\tt symflag} parameter is the symmetry flag ---
{\tt SPOOLES\_SYMMETRIC}, {\tt SPOOLES\_HERMITIAN}
or {\tt SPOOLES\_NONSYMMETRIC}.
\item
The {\tt seed} parameter is a random number seed.
\item
The {\tt alphareal} and {\tt alphaimag} parameters form a complex
number that is a scaling parameter.
Normally {\tt alpha} is (1.0,0.0), when we are just loading matrix
entries into a front.
However, when we factor $A + \alpha B$, the entries of $B$ will
be loaded with {\tt alpha} set equal to $\alpha[0:1]$.
\end{itemize}
%-----------------------------------------------------------------------
\item
\begin{verbatim}
test_assmbChv msglvl msgFile nDJ nUJ nDI nUI type symflag seed
\end{verbatim}
This driver program tests the {\tt Chv\_assembleChv} method.
It assembles a chevron $T_I$ into $T_J$, as is done during the
assembly of postponed rows and columns during the factorization
when pivoting is enabled.
Use the script file {\tt do\_assmbChv} for testing.
When the output file is loaded into matlab,
the last line to the screen is the error of the assembly.
\par
\begin{itemize}
\item
The {\tt msglvl} parameter determines the amount of output.
Use {\tt msglvl = 1} for just timing output.
\item
The {\tt msgFile} parameter determines the message file --- if {\tt
msgFile} is {\tt stdout}, then the message file is {\it stdout},
otherwise a file is opened with {\it append} status to receive any
output data.
\item
The {\tt nDJ} parameter is the number of rows and columns
in the (1,1) block of $T_J$.
\item
The {\tt nUJ} parameter is the number of columns
in the (1,2) block of $T_J$.
\item
The {\tt nDI} parameter is the number of rows and columns
in the (1,1) block of $T_I$.
\item
The {\tt nUI} parameter is the number of columns
in the (1,2) block of $T_I$.
\item
The {\tt type} parameter denotes the type of entries ---
{\tt SPOOLES\_REAL} or {\tt SPOOLES\_COMPLEX}
\item
The {\tt symflag} parameter is the symmetry flag ---
{\tt SPOOLES\_SYMMETRIC}, {\tt SPOOLES\_HERMITIAN}
or {\tt SPOOLES\_NONSYMMETRIC}.
\item
The {\tt seed} parameter is a random number seed.
\end{itemize}
%-----------------------------------------------------------------------
\item
\begin{verbatim}
test_copyEntriesToVector msglvl msgFile nD nU type symflag
                         pivotingflag storeflag seed
\end{verbatim}
This driver program tests the {\tt Chv\_copyEntriesToVector} method
which is used when after a front has been factored to store the
entries into dense $L$ and $U$ submatrices.
Use the script file {\tt do\_copyEntriesToVector} for testing.
When the output file is loaded into matlab,
the last line to the screen is a matrix that contains two entries.
If the program executes correctly, these two entries should be
exactly zero.
\par
\begin{itemize}
\item
The {\tt msglvl} parameter determines the amount of output.
Use {\tt msglvl = 1} for just timing output.
\item
The {\tt msgFile} parameter determines the message file --- if {\tt
msgFile} is {\tt stdout}, then the message file is {\it stdout},
otherwise a file is opened with {\it append} status to receive any
output data.
\item
The {\tt nD} parameter is the number of rows and columns
in the (1,1) block.
\item
The {\tt nU} parameter is the number of columns
in the (1,2) block.
\item
The {\tt type} parameter denotes the type of entries ---
{\tt SPOOLES\_REAL} or {\tt SPOOLES\_COMPLEX}
\item
The {\tt symflag} parameter is the symmetry flag ---
{\tt SPOOLES\_SYMMETRIC}, {\tt SPOOLES\_HERMITIAN}
or {\tt SPOOLES\_NONSYMMETRIC}.
\item
The {\tt pivotingflag} parameter is the pivoting flag ---
{\tt SPOOLES\_NO\_PIVOTING} for no pivoting,
{\tt SPOOLES\_PIVOTING} for pivoting.
\item
The {\tt storeflag} parameter is the storage flag,
to store by rows, use {\tt SPOOLES\_BY\_ROWS},
to store by columns, use {\tt SPOOLES\_BY\_COLUMNS}.
\item
The {\tt seed} parameter is a random number seed.
\end{itemize}
%-----------------------------------------------------------------------
\item
\begin{verbatim}
test_copyBigEntriesToVector msglvl msgFile nD nU type symflag
                         pivotingflag storeflag seed droptol
\end{verbatim}
This driver program tests the {\tt Chv\_copyBigEntriesToVector} method
which is used when after a front has been factored to store the
entries into sparse $L$ and $U$ submatrices.
Use the script file {\tt do\_copyBigEntriesToVector} for testing.
When the output file is loaded into matlab,
the last line to the screen is a matrix that contains three entries.
The first two are the maximum magnitudes of the entries that were
not copied (two different ways), and the third is the drop tolerance.
If the program executes correctly, the third term is larger than
the first two.
\par
\begin{itemize}
\item
The {\tt msglvl} parameter determines the amount of output.
Use {\tt msglvl = 1} for just timing output.
\item
The {\tt msgFile} parameter determines the message file --- if {\tt
msgFile} is {\tt stdout}, then the message file is {\it stdout},
otherwise a file is opened with {\it append} status to receive any
output data.
\item
The {\tt nD} parameter is the number of rows and columns
in the (1,1) block.
\item
The {\tt nU} parameter is the number of columns
in the (1,2) block.
\item
The {\tt type} parameter denotes the type of entries ---
{\tt SPOOLES\_REAL} or {\tt SPOOLES\_COMPLEX}
\item
The {\tt symflag} parameter is the symmetry flag ---
{\tt SPOOLES\_SYMMETRIC}, {\tt SPOOLES\_HERMITIAN}
or {\tt SPOOLES\_NONSYMMETRIC}.
\item
The {\tt pivotingflag} parameter is the pivoting flag ---
{\tt SPOOLES\_NO\_PIVOTING} for no pivoting,
{\tt SPOOLES\_PIVOTING} for pivoting.
\item
The {\tt storeflag} parameter is the storage flag,
to store by rows, use {\tt SPOOLES\_BY\_ROWS},
to store by columns, use {\tt SPOOLES\_BY\_COLUMNS}.
\item
The {\tt seed} parameter is a random number seed.
\item
The {\tt droptol} parameter is a drop tolerance parameters,
entries whose magnitude is smaller than {\tt droptol} are not
copied.
\end{itemize}
%-----------------------------------------------------------------------
\item
\begin{verbatim}
test_factor msglvl msgFile nD nU type symflag pivotingflag seed tau
\end{verbatim}
This driver program tests the {\tt Chv\_factor} method.
Use the script file {\tt do\_factor} for testing.
When the output file is loaded into matlab,
the last line to the screen is a matrix that contains three entries.
The first entry is the error in the factorization.
The second and third entries are the maximum magnitudes of the
entries in $L$ and $U$, respectively.
\par
\begin{itemize}
\item
The {\tt msglvl} parameter determines the amount of output.
Use {\tt msglvl = 1} for just timing output.
\item
The {\tt msgFile} parameter determines the message file --- if {\tt
msgFile} is {\tt stdout}, then the message file is {\it stdout},
otherwise a file is opened with {\it append} status to receive any
output data.
\item
The {\tt nD} parameter is the number of rows and columns
in the (1,1) block.
\item
The {\tt nU} parameter is the number of columns
in the (1,2) block.
\item
The {\tt type} parameter denotes the type of entries ---
{\tt SPOOLES\_REAL} or {\tt SPOOLES\_COMPLEX}
\item
The {\tt symflag} parameter is the symmetry flag ---
{\tt SPOOLES\_SYMMETRIC}, {\tt SPOOLES\_HERMITIAN}
or {\tt SPOOLES\_NONSYMMETRIC}.
\item
The {\tt pivotingflag} parameter is the pivoting flag ---
{\tt SPOOLES\_NO\_PIVOTING} for no pivoting,
{\tt SPOOLES\_PIVOTING} for pivoting.
\item
The {\tt seed} parameter is a random number seed.
\item
The {\tt tau} parameter is used when pivoting is enabled.
All entries in $L$ and $U$ will have magnitudes less than {\tt tau}.
\end{itemize}
%-----------------------------------------------------------------------
\item
\begin{verbatim}
test_findPivot msglvl msgFile nD nU type symflag seed tau
\end{verbatim}
This driver program tests the {\tt Chv\_findPivot} method.
Use the script file {\tt do\_findPivot} for testing.
When the output file is loaded into matlab,
look on the screen for the variables
{\tt maxerrupd} (the error in the factor and update),
{\tt ubound} (the maximum magnitude of the entries in $U$),
and if nonsymmetric
{\tt lbound} (the maximum magnitude of the entries in $L$).
\par
\begin{itemize}
\item
The {\tt msglvl} parameter determines the amount of output.
Use {\tt msglvl = 1} for just timing output.
\item
The {\tt msgFile} parameter determines the message file --- if {\tt
msgFile} is {\tt stdout}, then the message file is {\it stdout},
otherwise a file is opened with {\it append} status to receive any
output data.
\item
The {\tt nD} parameter is the number of rows and columns
in the (1,1) block.
\item
The {\tt nU} parameter is the number of columns
in the (1,2) block.
\item
The {\tt type} parameter denotes the type of entries ---
{\tt SPOOLES\_REAL} or {\tt SPOOLES\_COMPLEX}
\item
The {\tt symflag} parameter is the symmetry flag ---
{\tt SPOOLES\_SYMMETRIC}, {\tt SPOOLES\_HERMITIAN}
or {\tt SPOOLES\_NONSYMMETRIC}.
\item
The {\tt seed} parameter is a random number seed.
\item
The {\tt tau} parameter is used when pivoting is enabled.
All entries in $L$ and $U$ will have magnitudes less than {\tt tau}.
\end{itemize}
%-----------------------------------------------------------------------
\item
\begin{verbatim}
test_maxabs msglvl msgFile nD nU type symflag seed
\end{verbatim}
This driver program tests the
{\tt Chv\_maxabsInRow()},
{\tt Chv\_maxabsInRow11()},
{\tt Chv\_maxabsInColumn()},
{\tt Chv\_maxabsInColumn11()} and
{\tt Chv\_maxabsInDiagonal11()}
methods.
Use the script file {\tt do\_maxabs} for testing.
When the output file is loaded into matlab,
look on the screen for the variables
{\tt rowerror}, {\tt colerror},
{\tt rowerror11}, {\tt colerror11}
and {\tt diag11error}.
All should be zero.
\par
\begin{itemize}
\item
The {\tt msglvl} parameter determines the amount of output.
Use {\tt msglvl = 1} for just timing output.
\item
The {\tt msgFile} parameter determines the message file --- if {\tt
msgFile} is {\tt stdout}, then the message file is {\it stdout},
otherwise a file is opened with {\it append} status to receive any
output data.
\item
The {\tt nD} parameter is the number of rows and columns
in the (1,1) block.
\item
The {\tt nU} parameter is the number of columns
in the (1,2) block.
\item
The {\tt type} parameter denotes the type of entries ---
{\tt SPOOLES\_REAL} or {\tt SPOOLES\_COMPLEX}
\item
The {\tt symflag} parameter is the symmetry flag ---
{\tt SPOOLES\_SYMMETRIC}, {\tt SPOOLES\_HERMITIAN}
or {\tt SPOOLES\_NONSYMMETRIC}.
\item
The {\tt pivotingflag} parameter is the pivoting flag ---
{\tt SPOOLES\_NO\_PIVOTING} for no pivoting,
{\tt SPOOLES\_PIVOTING} for pivoting.
\item
The {\tt seed} parameter is a random number seed.
\end{itemize}
%-----------------------------------------------------------------------
\item
\begin{verbatim}
test_r1upd msglvl msgFile nD nU type symflag seed
\end{verbatim}
This driver program tests the {\tt Chv\_r1upd()} method.
Use the script file {\tt do\_r1upd} for testing.
When the output file is loaded into matlab,
the last line is the error of the update.
\par
\begin{itemize}
\item
The {\tt msglvl} parameter determines the amount of output.
Use {\tt msglvl = 1} for just timing output.
\item
The {\tt msgFile} parameter determines the message file --- if {\tt
msgFile} is {\tt stdout}, then the message file is {\it stdout},
otherwise a file is opened with {\it append} status to receive any
output data.
\item
The {\tt nD} parameter is the number of rows and columns
in the (1,1) block.
\item
The {\tt nU} parameter is the number of columns
in the (1,2) block.
\item
The {\tt type} parameter denotes the type of entries ---
{\tt SPOOLES\_REAL} or {\tt SPOOLES\_COMPLEX}
\item
The {\tt symflag} parameter is the symmetry flag ---
{\tt SPOOLES\_SYMMETRIC}, {\tt SPOOLES\_HERMITIAN}
or {\tt SPOOLES\_NONSYMMETRIC}.
\item
The {\tt seed} parameter is a random number seed.
\end{itemize}
%-----------------------------------------------------------------------
\item
\begin{verbatim}
test_r2upd msglvl msgFile nD nU type symflag seed
\end{verbatim}
This driver program tests the {\tt Chv\_r2upd()} method.
Use the script file {\tt do\_r2upd} for testing.
When the output file is loaded into matlab,
the last line is the error of the update.
\par
\begin{itemize}
\item
The {\tt msglvl} parameter determines the amount of output.
Use {\tt msglvl = 1} for just timing output.
\item
The {\tt msgFile} parameter determines the message file --- if {\tt
msgFile} is {\tt stdout}, then the message file is {\it stdout},
otherwise a file is opened with {\it append} status to receive any
output data.
\item
The {\tt nD} parameter is the number of rows and columns
in the (1,1) block.
\item
The {\tt nU} parameter is the number of columns
in the (1,2) block.
\item
The {\tt type} parameter denotes the type of entries ---
{\tt SPOOLES\_REAL} or {\tt SPOOLES\_COMPLEX}
\item
The {\tt symflag} parameter is the symmetry flag ---
{\tt SPOOLES\_SYMMETRIC}, {\tt SPOOLES\_HERMITIAN}
or {\tt SPOOLES\_NONSYMMETRIC}.
\item
The {\tt seed} parameter is a random number seed.
\end{itemize}
%-----------------------------------------------------------------------
\item
\begin{verbatim}
test_swap msglvl msgFile nD nU type symflag seed
\end{verbatim}
This driver program tests three methods:
{\tt Chv\_swapRowsAndColumns()},
{\tt Chv\_swapRows()} and
{\tt Chv\_swapColumns()}.
Use the script file {\tt do\_swap} for testing.
When the output file is loaded into matlab,
look for the {\tt maxerrrowswap1}, {\tt maxerrcolswap1},
{\tt maxerrswap}, {\tt maxerrsymswap1} and {\tt maxerrsymswap2} values.
All should be zero.
\par
\begin{itemize}
\item
The {\tt msglvl} parameter determines the amount of output.
Use {\tt msglvl = 1} for just timing output.
\item
The {\tt msgFile} parameter determines the message file --- if {\tt
msgFile} is {\tt stdout}, then the message file is {\it stdout},
otherwise a file is opened with {\it append} status to receive any
output data.
\item
The {\tt nD} parameter is the number of rows and columns
in the (1,1) block.
\item
The {\tt nU} parameter is the number of columns
in the (1,2) block.
\item
The {\tt type} parameter denotes the type of entries ---
{\tt SPOOLES\_REAL} or {\tt SPOOLES\_COMPLEX}
\item
The {\tt symflag} parameter is the symmetry flag ---
{\tt SPOOLES\_SYMMETRIC}, {\tt SPOOLES\_HERMITIAN}
or {\tt SPOOLES\_NONSYMMETRIC}.
\item
The {\tt seed} parameter is a random number seed.
\end{itemize}
%-----------------------------------------------------------------------
\item
\begin{verbatim}
test_update msglvl msgFile type symflag sparsityflag
            ncolT ncolU nrowD nentU offset seed
\end{verbatim}
This driver program tests the
{\tt Chv\_updateH()},
{\tt Chv\_updateS()} and
{\tt Chv\_updateN()} methods.
The {\tt Chv} object $T$ is updated by $-U^TDU$, $-U^HDU$ or $-LDU$,
depending on whether $T$ is symmetric, hermitian or nonsymmetric.
Use the script file {\tt do\_update} for testing.
When the output file is loaded into matlab,
the last line is the error in the update which should be zero.
\par
\begin{itemize}
\item
The {\tt msglvl} parameter determines the amount of output.
Use {\tt msglvl = 1} for just timing output.
\item
The {\tt msgFile} parameter determines the message file --- if {\tt
msgFile} is {\tt stdout}, then the message file is {\it stdout},
otherwise a file is opened with {\it append} status to receive any
output data.
\item
The {\tt type} parameter denotes the type of entries ---
{\tt SPOOLES\_REAL} or {\tt SPOOLES\_COMPLEX}
\item
The {\tt symflag} parameter is the symmetry flag ---
{\tt SPOOLES\_SYMMETRIC}, {\tt SPOOLES\_HERMITIAN}
or {\tt SPOOLES\_NONSYMMETRIC}.
\item
The {\tt sparsityflag} parameter should be zero for dense $U$ and $L$,
or 1 for sparse $U$ and $L$.
\item
The {\tt ncolT} parameter is the number of columns
in the (1,1) and (1,2) blocks of $T$.
\item
The {\tt nDT} parameter is the number of rows and columns
in the (1,1) block of $T$.
\item
The {\tt ncolU} parameter is the number of columns in $U$.
\item
The {\tt nrowD} parameter is the number of rows and columns in $D$.
\item
The {\tt nentU} parameter is the number entries in $U$, ignored if
{\tt sparsityflag = 0}.
\item
The {\tt offset} parameter is the offset of first index in $T$ from
the last index in $D$.
\item
The {\tt seed} parameter is a random number seed.
\end{itemize}
%-----------------------------------------------------------------------
\end{enumerate}


\chapter{Disease enrichment results}

This appendix contains the results for disease enrichment between the weighted and unweighted graphs for the diseases Schizophrenia and Alzheimer's.
These can be found in tables \ref{tab:wdisease} and \ref{tab:uwdisease} for the weighted and unweighted results of the disease enrichment test, respectively.

\begin{table}
    \centering
    \begin{tabular}{l c c c c}
        \small
        Community & Size & disease & p-value & p_lower (\%) \\
        \hline
        44 & 35 & schizophrenia & $3.45\e{-3}$ & 0.6 \\
        45 & 73 & schizophrenia & $3.90\e{-3}$ & 0.4 \\
        48 & 17 & Alzheimer's_disease & $7.57\e{-3}$ & 0.2 \\
        33 & 64 & Alzheimer's_disease & $8.39\e{-3}$ & 0.7 \\
        33 & 64 & schizophrenia & $2.33\e{-2}$ & 2.2 \\
        14 & 18 & schizophrenia & $4.19\e{-2}$ & 3.2 \\
        2 & 48 & schizophrenia & $4.67\e{-2}$ & 4.9 \\
        15 & 8 & Alzheimer's_disease & $4.98\e{-2}$ & 5.2 \\
    \end{tabular}
    \caption{Disease enrichment results for the weighted communities detected in the active zone network.}
    \label{tab:wdisease}
\end{table}

\begin{table}
    \centering
    \begin{tabular}{l c c c c }
        \small
        Community & Size & disease & p-value & p_lower (\%) \\
        \hline
        29 & 88 & schizophrenia & $1.69\e{-3}$ & 0.2 \\
        2 & 99 & schizophrenia & $4.32\e{-3}$ & 0.5 \\
        30 & 26 & Alzheimer's_disease & $5.37\e{-3}$ & 0.7 \\
        61 & 24 & schizophrenia & $1.96\e{-2}$ & 1.5 \\
        63 & 23 & Alzheimer's_disease & $3.50\e{-2}$ & 4.3 \\
        61 & 24 & Alzheimer's_disease & $4.25\e{-2}$ & 4.5 \\
        63 & 23 & schizophrenia & $4.62\e{-2}$ & 4.3 \\
    \end{tabular}
    \caption{Disease enrichment results for the unweighted communities detected in the active zone network.}
    \label{tab:uwdisease}
\end{table}

%\section{Crossover membership test}
%%describing the test used to check which communities are most similar.
%
%This test was used to determine which of one group of communities was most similar to another community.
%Both communities can be expressed as a set of protein identifiers.
%Combining these sets we are left with a total set of all identifiers in either community.
%
%By iterating over this set and testing each identifier for membership in either set we obtain an array of boolean truth values.
%If the sets are identical, all of these will be true.
%These truth values can then be compared individually to find the number of shared members between each group.
%


\section{Benchmarks}\label{sec:benchmarks}

\subsection{tau}

The \pkg{tau}~\citep{tau} package offers, among other things, a framework for
constructing n-grams from a text, via its \code{textcnt()} function.

In \pkg{ngram}, the use of \code{get.phrasetable(ngram(x, n=3))} roughly corresponds
to \code{textcnt(x, n=3, split=" ", method="string")} in \pkg{tau}.  Although
\code{get.phrasetable()} returns proportions in addition to counts, and in the
form of a more costly dataframe compared to \code{tau}'s vector of counts, we
are still able to achieve very good performance.
\begin{lstlisting}[language=rr]
library(rbenchmark)
library(tau)
library(ngram)

x <- ngram::rcorpus(50000)

reps <- 15
cols <- c("test", "replications", "elapsed", "relative")

benchmark(tau=textcnt(x, n=3, split=" ", method="string"), ngram=get.phrasetable(ngram(x, n=3)), replications=reps, columns=cols)
\end{lstlisting}
Evaluating this script gives:
\begin{lstlisting}[language=rout]
   test replications elapsed relative
2 ngram           15   0.958    1.000
1   tau           15 137.994  144.044
\end{lstlisting}

In fact, a good portion of the time in the \pkg{ngram} runs here is in converting the
internal \proglang{C} data structure over to an \proglang{R} one.  The original
purpose of the \pkg{ngram} package was merely amusement, babbling n-grams.
If we just compare the run times for this, the difference is striking:
\begin{lstlisting}[language=rr]
library(tau)
library(ngram)

x <- ngram::rcorpus(100000)

tautime <- system.time(pt1 <- textcnt(x, n=3, split=" ", method="string"))[3]
ngtime <- system.time(pt2 <- ngram(x, n=3))[3]

cat("tau: ", tautime, "\n")
cat("ngram: ", ngtime, "\n")
cat("tau/ngram: ", tautime/ngtime, "\n")
\end{lstlisting}
If we evaluate this, we see:
\begin{lstlisting}[language=rout]
tau:  36.576
ngram:  0.048
tau/ngram:  762
\end{lstlisting}
Here, \pkg{ngram} is primed for babbling, in that it has already stored all
``next words'', while \pkg{tau} only contains what we call the phrasetable of
3-grams.

\subsection{RWeka}

The \pkg{ngram} package has a separate tokenizer to produce returns similar to
those in the \pkg{RWeka} package.  However, \pkg{ngram} is significantly faster:

\begin{lstlisting}[language=rr]
library(memuse)
library(ngram)
library(RWeka)

x = ngram::rcorpus(nwords=1e6, alphabet="a")
memuse(x)
## 4.292 MiB

system.time(ngram_asweka(x, min=2, max=2))
##    user  system elapsed
##   0.216   0.044   0.261

system.time(NGramTokenizer(x, Weka_control(min=2, max=2)))
##    user  system elapsed
## 500.228   0.528 500.056
\end{lstlisting}


% $Header$
% $Author: fager $
% $Date: 2009-04-28 20:51:04 +0200 (ti, 28 apr 2009) $
% $Revision: 99 $
% $Log$
% Revision 1.3  2005/02/22 07:31:00  fager
% Corrections from M.Kelly implemented
%
% Revision 1.2  2004/10/21 18:59:06  fager
% Version logging added. Comments from KA implemented.
%
\section{Model representation using netlists}\label{sec:mna}
So far, the description has been focused on handling measured
S-parameter data originating from TouchStone files etc. However,
in many applications it is also desired to handle the S-parameters
of an ideal electrical circuit diagram. This is typically the case
in modelling applications, where the parameters of a model are
determined by minimizing the difference between the S-parameters
generated by the model and the measured ones.

\subsection{Netlist format and importing}
The circuit shown in \figref{Circuit} will hereafter serve as an
example.

\begin{figure}[htbf]
    \centering
  \includegraphics[scale=0.85]{Figures/Circuit.eps}
  \caption{Example circuit.}\label{fig:Circuit}
\end{figure}

Electrical circuits are entered into Milou by means of a
\emph{netlist} file, similar to the ones used in Spice. The
expressions shown in \tabref{Netlist} are recognized by the MuWave
netlist parser:

\begin{table}[htbf]
    \centering
  \caption{Valid netlist expressions.}\label{tab:Netlist}
    \begin{minipage}{\textwidth}
    \centering
    \begin{small}
    \begin{tabular}{l|l}
    % after \\: \hline or \cline{col1-col2} \cline{col3-col4} ...
    Component & Netlist expression\\
    \hline
    Resistance & \verb"R value"\footnote{A \emph{value} can be either a numeric value or a parameter name.}\verb" node1 node2"\\
    Conductance & \verb"G value node1 node2"\\
    Capacitance & \verb"C value node1 node2"\\
    Inductance & \verb"L value node1 node2"\\
    General admittance & \verb"X expression"\footnote{An \emph{expression} can include an arbitrary algebraic expression. $s$ should be used instead of $j\omega$. Other names that are not recognized functions will be considered user parameters.} \verb" node1 node2"\\
    Transconductance& \verb"VCCS value node1+ node2+ node1- node2-"\\
    Transcond. with $\tau$& \verb"VCCSD value node1+ node2+ node1- node2-"\\
    Gyrator & \verb"GY value node1+ node2+ node1- node2-"\\
    General transcond. & \verb"X2 expression node1+ node2+ node1- node2-"\\
    Port & \verb"P portname node1 node 2"\\
    Comment & \verb"% Comment text" \\
    \hline
    \end{tabular}
    \end{small}
    \end{minipage}
\end{table}

The netlist corresponding to the circuit in \figref{Circuit}
becomes (\verb"circuit.net"):
\begin{small}
\begin{verbatim}
    % Example circuit
    R R1 1 0
    C C1 1 2
    X s*A/(A+s*B) 2 0
    L L1 2 3
    VCCS gm 1 3 0 0
    P P1 1 0
    P P2 3 0
\end{verbatim}
\end{small}

The netlist can now be imported into Milou by using the
\verb"read_netlist" function,
\begin{small}
\begin{verbatim}
    >> model = read_netlist(mna,'test/circuit.net')
    Admittance matrix:
        '+1/(R1)+s*C1'    '-s*C1'                 []
        '-s*C1'           [1x26 char]    '-1/(s*L1)'
        '+gm'             '-1/(s*L1)'    '+1/(s*L1)'

    Parameters:
        'R1'    'C1'    'A'    'B'    'L1'    'gm'

    Parameter types:
        'R'    'C'    'X'    'X'    'L'    'VCCS'

    Frequencies
\end{verbatim}
\end{small}

The output variable \verb"model" is now an \verb"mna" object. mna
stands for \emph{modified nodal admittance}, which is the type of
matrix normally used to represent circuits in simulators. MuWave
currently uses the ordinary nodal admittance matrix for
representing circuits\footnote{The modified nodal admittance
matrix uses a different representation of components that may
present infinite conductance, e.g. inductances at low frequency,
and thus provides better numerical stability.}. It is, by the way,
shown in the first part of the displayed \verb"mna" object
information displayed above.

The next thing displayed is the list of parameters. These are the
unique parameter names found in the netlist. The order in which
the parameters appear is important when, at a later stage, their
values should be assigned. The parameter names can also be
accessed by the \verb"params" command,
\begin{small}
\begin{verbatim}
    >> params(model)
    ans =
        'R1'    'C1'    'A'    'B'    'L1'    'gm'
\end{verbatim}
\end{small}

Next in the results displayed is the types associated with the parameters.
This is not applicable for parameters defined in \verb"X" or
\verb"X2" elements.

Finally, there is a list of partial derivatives that are used for
calculating sensitivities. This feature is, however, not treated
in this manual text.

\subsection{Evaluation of the model X-parameters}
Before it is possible to assign numbers to the model parameter and
evaluate its X-parameters, one needs to specify the frequencies
used for consequent calculations. The reason that the frequencies
are entered separately is to speed up the X-parameter evaluations
later. The frequencies are assigned to the \verb"mna"-object using
the \verb"freq" command,
\begin{small}
\begin{verbatim}
    >> model = freq(model,msp.freq);
\end{verbatim}
\end{small}
where the same frequencies as in the previous measurement examples
have been used.

It is now possible to evaluate the model for any set of parameters
using a call to either the \verb"calc" or the \verb"reduce"
functions. The model parameter values should be arranged in a
vector with the order being the same as described above.
\begin{small}
\begin{verbatim}
    >> R1 = 100;C1 = 1e-12;A = 0.7e-12;B = 2e-12;
    >> L1 = 1e-9; gm = 1e-3;
    >> model_params = [R1,C1,A,B,L1,gm];
\end{verbatim}
\end{small}

Once the parameters have been defined it is possible to calculate
either the full $N$-by-$N$ matrix, where $N$ is the number of
nodes, or a reduced matrix describing the circuit as observed at
the ports that were defined in the netlist.

The first case is evaluated by using the \verb"calc" function,
\begin{small}
\begin{verbatim}
    >> calc(model,model_params)
    xparam-object
    	 type:	Y
    	 reference:	50
    	 ports:	3
    	 elements:	101
    	 freq:	1E+009 -> 5E+010
\end{verbatim}
\end{small}
The resulting object is, for our three-node example a 3-by-3
Y-type \verb"xparam" object.

The function \verb"reduce" is used in a similar fashion to
calculate the port-reduced Z-parameters,
\begin{small}
\begin{verbatim}
    >> xp_model = reduce(model,model_params)
    xparam-object
    	 type:	Z
    	 reference:	50
    	 ports:	2
    	 elements:	101
    	 freq:	1E+009 -> 5E+010
\end{verbatim}
\end{small}
Note that the resulting object is a 2-by-2 Z-type \verb"xparam"
object, which can be handled in exactly the same manner as the
measurements were used before.


\subsection{Quantisation of states}

Shrink the number of states.


\documentclass[a4paper]{article}
\usepackage[margin=1in]{geometry}

\begin{document}
\section{List of runs}

\subsection{Glossary}
\begin{itemize}
    \item W : Only waves runs
    \item WR : Only waves runs with rotation
    \item WV : Wave-vortex runs
    \item WVR : Wave-vortex runs with rotation
    \item $t_{stat}$ : Time when the run is approximately stationary
    \item $t_{max}$ : Time until which the run was simulated
    \item $\frac{<\bf \Omega_0 >}{{(P k_f^2)}^{2/3}}$ : Initial enstrophy - forcing power ratio
\end{itemize}

\subsection{Tables}
\begin{tabular}{lrrrrrrrr}
\toprule
{} &   $n$ &  $c$ &  $\nu_8$ &  $\epsilon$ &  $\frac{k_{diss}}{k_f}$ &   $F_f$ &  $t_{stat}$ &  $t_{\max}$ \\
\midrule
W1  &   960 &   10 & 1.56e-10 &        1.04 &                    28.8 &   0.111 &        4.45 &        49.9 \\
W2  &  1920 &   10 & 9.68e-13 &        1.06 &                    57.7 &   0.112 &        4.66 &        49.8 \\
W3  &  3840 &   10 &    6e-15 &         1.1 &                     115 &   0.113 &        5.22 &        49.9 \\
W4  &  7680 &   10 & 3.72e-17 &        1.18 &                     231 &   0.116 &        6.12 &        16.9 \\
W5  &   960 &   20 & 1.56e-10 &           1 &                    28.8 &   0.055 &        4.88 &        49.9 \\
W6  &  1920 &   20 & 9.69e-13 &        1.02 &                    57.7 &  0.0553 &        5.61 &         120 \\
W7  &  3840 &   20 & 6.01e-15 &        1.08 &                     115 &  0.0563 &        6.92 &         120 \\
W8  &  7680 &   20 & 3.72e-17 &        1.21 &                     231 &  0.0585 &        8.52 &         120 \\
W9  &   960 &   40 & 1.56e-10 &       0.996 &                    28.8 &  0.0274 &        6.02 &        49.8 \\
W10 &  1920 &   40 & 9.68e-13 &       0.998 &                    57.7 &  0.0274 &        7.12 &        49.8 \\
W11 &  3840 &   40 &    6e-15 &        1.06 &                     115 &   0.028 &        8.72 &        49.8 \\
W12 &   960 &  100 & 1.56e-10 &       0.999 &                    28.8 &   0.011 &        8.72 &        49.8 \\
W13 &  1920 &  100 & 9.69e-13 &        1.01 &                    57.7 &   0.011 &        10.2 &         120 \\
W14 &  3840 &  100 & 6.01e-15 &        1.06 &                     115 &  0.0112 &        13.9 &         120 \\
W15 &  7680 &  100 & 3.72e-17 &        1.22 &                     231 &  0.0117 &        17.2 &        95.8 \\
W16 &   960 &  400 & 1.56e-10 &        1.14 &                    28.8 & 0.00287 &        15.9 &         120 \\
W17 &  1920 &  400 & 9.69e-13 &        1.15 &                    57.7 & 0.00288 &        21.3 &         120 \\
W18 &  3840 &  400 & 6.01e-15 &        1.12 &                     115 & 0.00286 &        25.5 &         119 \\
W19 &   960 &  700 & 1.56e-10 &        1.27 &                    28.8 &  0.0017 &        19.1 &        49.8 \\
W20 &  1920 &  700 & 9.68e-13 &        1.05 &                    57.7 &  0.0016 &        22.2 &        49.8 \\
\bottomrule
\end{tabular}
\begin{tabular}{lrrrrrrrrrrr}
\toprule
{} &   $n$ &  $c$ &  $\nu_8$ &  $f$ &  $\epsilon$ &  $\frac{k_{diss}}{k_f}$ &  $F_f$ &  $Ro_f$ &  $Bu$ &  $t_{stat}$ &  $t_{\max}$ \\
\midrule
WR1 &  1920 &   20 & 9.68e-13 & 21.3 &       0.838 &                    57.7 & 0.0518 &  0.0366 &   0.5 &          11 &        99.9 \\
WR2 &  1920 &   20 & 9.68e-13 & 15.1 &       0.894 &                    57.7 & 0.0529 &  0.0529 &     1 &        7.92 &        99.8 \\
WR3 &  1920 &   20 & 9.68e-13 & 10.7 &       0.938 &                    57.7 & 0.0538 &  0.0761 &     2 &        6.62 &        99.8 \\
WR4 &  1920 &   20 & 9.68e-13 & 7.54 &        0.98 &                    57.7 & 0.0546 &   0.109 &     4 &        6.12 &        99.8 \\
\bottomrule
\end{tabular}
\begin{tabular}{lrrrrrrrrr}
\toprule
{} &   $n$ &  $c$ &  $\nu_8$ &  $\epsilon$ &  $\frac{k_{diss}}{k_f}$ &   $F_f$ &  $\frac{<\bf \Omega_0 >}{{(P k_f^2)}^{2/3}}$ &  $t_{stat}$ &  $t_{\max}$ \\
\midrule
WV1  &  1920 &   20 & 1.11e-12 &        1.02 &                    56.6 &  0.0553 &                                         0.01 &        5.71 &        49.8 \\
WV2  &  1920 &   20 & 1.11e-12 &        1.02 &                    56.6 &  0.0554 &                                          0.1 &        5.71 &        99.9 \\
WV3  &  1920 &   20 & 1.11e-12 &           1 &                    56.6 &   0.055 &                                            1 &        5.41 &        49.9 \\
WV4  &  1920 &   20 & 1.11e-12 &        1.11 &                    56.6 &  0.0568 &                                           10 &        3.62 &        99.8 \\
WV5  &  1920 &  100 & 1.11e-12 &        1.02 &                    56.6 &  0.0111 &                                         0.01 &        11.1 &        99.9 \\
WV6  &  1920 &  100 & 1.11e-12 &        1.02 &                    56.6 &  0.0111 &                                          0.1 &        11.1 &        99.9 \\
WV7  &  1920 &  100 & 1.11e-12 &        1.01 &                    56.6 &   0.011 &                                            1 &        10.7 &        49.9 \\
WV8  &  1920 &  100 & 1.11e-12 &        1.07 &                    56.6 &  0.0112 &                                           10 &        8.72 &        99.9 \\
WV9  &  1920 &  100 & 1.11e-12 &        2.22 &                    56.6 &  0.0143 &                                          100 &         1.5 &        49.9 \\
WV10 &  1920 &  400 & 1.11e-12 &        1.03 &                    56.6 & 0.00277 &                                         0.01 &        20.7 &        49.8 \\
WV11 &  1920 &  400 & 1.11e-12 &        1.08 &                    56.6 & 0.00282 &                                          0.1 &        19.3 &        99.9 \\
WV12 &  1920 &  400 & 1.11e-12 &        1.04 &                    56.6 & 0.00278 &                                            1 &        18.4 &        49.8 \\
WV13 &  1920 &  400 & 1.11e-12 &        1.17 &                    56.6 &  0.0029 &                                           10 &        18.3 &        99.9 \\
WV14 &  1920 &  400 & 1.11e-12 &        1.89 &                    56.6 &  0.0034 &                                          100 &           5 &        49.8 \\
\bottomrule
\end{tabular}
\begin{tabular}{lrrrrrrrrrrrr}
\toprule
{} &   $n$ &  $c$ &  $\nu_8$ &  $f$ &  $\epsilon$ &  $\frac{k_{diss}}{k_f}$ &  $F_f$ &  $Ro_f$ &  $Bu$ &  $\frac{<\bf \Omega_0 >}{{(P k_f^2)}^{2/3}}$ &  $t_{stat}$ &  $t_{\max}$ \\
\midrule
WVR1  &  1920 &   20 & 1.11e-12 & 7.54 &        1.03 &                    56.6 & 0.0555 &   0.111 &     4 &                                         0.01 &           8 &        99.9 \\
WVR2  &  1920 &   20 & 1.11e-12 & 3.37 &        1.03 &                    56.6 & 0.0555 &   0.248 &    20 &                                            1 &        6.52 &        99.8 \\
WVR3  &  1920 &  100 & 1.11e-12 & 75.4 &       0.932 &                    56.6 & 0.0107 &  0.0107 &     1 &                                         0.01 &        22.6 &        99.8 \\
WVR4  &  1920 &  100 & 1.11e-12 & 75.4 &       0.915 &                    56.6 & 0.0107 &  0.0107 &     1 &                                          0.1 &        22.9 &        99.8 \\
WVR5  &  1920 &  100 & 1.11e-12 & 75.4 &        1.14 &                    56.6 & 0.0115 &  0.0115 &     1 &                                            1 &        22.7 &        99.8 \\
WVR6  &  1920 &  100 & 1.11e-12 & 53.3 &       0.991 &                    56.6 &  0.011 &  0.0155 &     2 &                                         0.01 &        19.2 &        99.8 \\
WVR7  &  1920 &  100 & 1.11e-12 & 53.3 &       0.998 &                    56.6 &  0.011 &  0.0155 &     2 &                                          0.1 &        19.3 &        99.8 \\
WVR8  &  1920 &  100 & 1.11e-12 & 53.3 &        1.06 &                    56.6 & 0.0112 &  0.0159 &     2 &                                            1 &        19.4 &        99.8 \\
WVR9  &  1920 &  100 & 1.11e-12 & 37.7 &        1.04 &                    56.6 & 0.0111 &  0.0222 &     4 &                                         0.01 &        16.8 &        99.9 \\
WVR10 &  1920 &  100 & 1.11e-12 & 37.7 &        1.04 &                    56.6 & 0.0111 &  0.0223 &     4 &                                          0.1 &        16.6 &        99.9 \\
WVR11 &  1920 &  100 & 1.11e-12 & 37.7 &        1.04 &                    56.6 & 0.0111 &  0.0223 &     4 &                                            1 &        16.3 &        99.9 \\
WVR12 &  1920 &  100 & 1.11e-12 & 37.7 &        1.25 &                    56.6 & 0.0118 &  0.0237 &     4 &                                           10 &        12.7 &        99.9 \\
WVR13 &  1920 &  100 & 1.11e-12 & 37.7 &        1.66 &                    56.6 &  0.013 &   0.026 &     4 &                                          100 &         1.6 &        99.9 \\
\bottomrule
\end{tabular}
\end{document}


\section{Medium Armor Training}\label{perk:mediumArmorTraining}
\textbf{Cost:} 500CP\\
\textbf{Requirements:} Cloth Armor Training I\\
\textbf{Passive, Repeatable, Source(500 Gold)}\\
You are trained with medium armor.\\
You add your level to Dodge and Evade checks while equipped with armor with the "Medium" descriptor.\\
Additionally, your Agility can not exceed 9 while wearing this type of armor.\\
\\
Level Progression:\\
\\
\rowcolors{2}{lightgray}{white}
\begin{tabular}{l | l | l | l | l}
	Level & CP Cost & Gold Cost & Effect & Maximum Agility
	\\
	II
	& 1,000CP
	& 1,000 Gold
	& +1d4 Evade, +2 Dodge
	& 10
	\\
	III
	& 2,500CP
	& 2,500 Gold
	& +2d4 Evade, +4 Dodge
	& 11
	\\
	IV
	& 5,000CP
	& 5,000 Gold
	& +3d4 Evade, +6 Dodge
	& 12
	\\
\end{tabular}