\documentclass[acmsmall]{acmart}

\usepackage{algorithm2e}
\usepackage{amsthm}

\usepackage{todonotes}
\newcommand{\sotodo}{\todo[color=green]}
\newcommand{\sotodoinline}{\todo[color=green,inline=true]}

\AtBeginDocument{%
	\providecommand\BibTeX{{%
			\normalfont B\kern-0.5em{\scshape i\kern-0.25em b}\kern-0.8em\TeX}}}

\begin{document}

\title{PowerNumbers.jl: a fast approach to automatic asymptotics}

\author{Sheehan Olver}
\email{s.olver@imperial.ac.uk}
\affiliation{%
	\institution{Imperial College}
	\city{London}
	\state{England}
	\postcode{SW7 2AZ}
}

\author{Matthew Rees}
\email{matthew.rees.16@ucl.ac.uk}
\affiliation{%
	\institution{University College London}
	\city{London}
	\state{England}
	\postcode{WC1E 6BT}
}

\begin{abstract}
	We develop a scheme for quick arithmetic on asymptotic series, evaluated to just one or two terms. This generalizes the concept of dual numbers in automatic differentiation to support general algebraic powers, including negative powers to capture behaviour at infinity.  We give a full description of the algebraic rules for these newly defined {\it power numbers}, with justification of guaranteed equivalence to asymptotic algebra. Some example applications follow, including evaluation of complicated rational functions at infinity defined in terms of linear algebra, hypergeometric functions, and Stieltjes transforms over intervals for functions with power-like singularities and the endpoints.
\end{abstract}

\begin{CCSXML}
	<ccs2012>
	<concept>
	<concept_id>10002950.10003705.10003707</concept_id>
	<concept_desc>Mathematics of computing~Solvers</concept_desc>
	<concept_significance>500</concept_significance>
	</concept>
	</ccs2012>
\end{CCSXML}

\ccsdesc[500]{Mathematics of computing~Solvers}

\keywords{none}

\maketitle

\section{Introduction}

{\it Dual numbers} are a simple algebraic structure that underlies forward mode automatic-differentiation, with a convenient representation in terms of $2 \times 2$ matrices:

\begin{definition}
Let $K$ be a field. The set of matrices
$$\mathbf{D}^K = \left\{\begin{pmatrix}a & b \\ 0 & a \end{pmatrix} : a,b \in K\right\}$$
are called the set of \textbf{dual numbers}. The elements are typically denoted by
$$
a + b \epsilon \qquad \hbox{where} \qquad \epsilon = \begin{pmatrix} 0 & 1 \\ & 0 \end{pmatrix}.
$$
\end{definition}

The property that $\epsilon^2 = 0 $ suffices to guarantee that general analytic matrix functions applied to dual numbers encode the derivative at the point $a$, namely:
$$
f(a + b \epsilon) = f(a) + b f'(b) \epsilon
$$

Thus computations using dual numbers preserve the most important information  encoded in the Taylor Expansion.

In analogy to dual numbers, we seek a simple algebraic structure that encodes the leading-order information of Hahn-form asymptotic expansions. Asymptotic expansions are important computational tools in applied mathematics, that can reduce complicated functions to simple forms with specified accuracy. Many such expansions are of the Hahn form:
\begin{equation}
S(z) = \sum_{n=1}^{\infty}a_nz^{\alpha_n} \quad as \quad z \rightarrow 0
\end{equation}
where the $\alpha_n \in \mathbb{R}$ are strictly increasing. We we call the set of all such expansions $\mathbb{S}$.

By restricting these series to the terms most significant in the limit, we can define the set of \textbf{power numbers}. Specifically, we will be restricting to the first two terms with $\alpha \leq \beta$, so that we are essentially considering:
$$S(z) = az^\alpha + bz^\beta + o(z^\beta)$$

By giving algebraic rules that are consistent with their counterparts in $\mathbb{S}$, we will allow for fast manipulation without compromising on accuracy or losing important order information.

In \textbf{Section 2} we will rigorously describe the algorithms that govern the basic operations of addition, subtraction, multiplication and division on power numbers. These have been defined to correspond with what we expect from similar operations on $\mathbb{S}$, and we provide a theorem guaranteeing this symmetry. Furthermore, this guarantee extends to exponentiation and analytic functions of power numbers.

In \textbf{Section 3} we will give some of the possible applications of the construction, including the evaluation of rational polynomials at infinity and finding limiting expressions of hypergeometric functions(?). We also provide an implementation written in the Julia language, "PowerNumbers.jl".

\section{Formal Description}

\begin{definition}
Let $K$ be a field. The set of functions of $\epsilon \in [0,\infty)$,
$$\mathbf{PN}^K = \{a\epsilon^\alpha + b\epsilon^\beta : a,b \in K;\alpha,\beta\in\mathbb{R}\cup\{\infty\}; \alpha\leq\beta\}$$
is called the set of \textbf{power numbers}.
\end{definition}

In the case $\alpha=\beta$, we write only one term $(a+b)\epsilon^\beta$. Notationally, $\epsilon^0$ is omitted. While we do not have a one-to-one correspondence between $\mathbf{PN}^K$ and $\mathbb{S}$ (there is usually a choice between one or two terms), this is not a major concern. Power numbers are not considered to be equal unless the values $(a,b,\alpha,\beta)$ are all equal.

We enforce the equality $0\epsilon^\alpha + b\epsilon^\beta = b\epsilon^\beta$. However, in general, $a\epsilon^\alpha + 0\epsilon^\beta \neq a\epsilon^\alpha$.
By defining $(+,*)$ below, we acquire the double monoid $$\mathbb{PN}^K = (\mathbf{PN}^K,+,*)$$

\subsection{Basic Operations}
\begin{definition}
\textbf{Addition} $+:\mathbf{PN}^K \times \mathbf{PN}^K \rightarrow \mathbf{PN}^K$ is described by an algorithm.

\begin{algorithm}[H]
	\SetAlgoLined
	\KwData{$a\epsilon^\alpha + b\epsilon^\beta, c\epsilon^\gamma + d\epsilon^\delta \in \mathbf{PN}^K$; assume WLOG that $\beta \leq \delta$}
	\KwResult{$(a\epsilon^\alpha + b\epsilon^\beta) + (c\epsilon^\gamma + d\epsilon^\delta) = p\epsilon^\zeta + q\epsilon^\eta \in \mathbf{PN}^K$}
	\uIf{$\beta=\delta$}{
		\uIf{$\gamma=\beta$}{
			$p = a$, $q = b + c + d$, $\zeta = \alpha$, $\eta = \beta$
		}
		\uElseIf{$\alpha<\gamma<\beta$}{
			$p = a$, $q = c$, $\zeta = \alpha$, $\eta = \gamma$
		}
		\uElseIf{$\gamma=\alpha$}{
			$p = a + c$, $q = b + d$, $\zeta = \alpha$, $\eta = \beta$
		}
		\Else{
			$p = c$, $q = a$, $\zeta = \gamma$, $\eta = \alpha$
		}
	}
	\Else{
		\uIf{$\beta<\gamma$}{
			$p = a$, $q = b$, $\zeta = \alpha$, $\eta = \beta$
		}
		\uElseIf{$\gamma=\beta$}{
			$p = a$, $q = b + c$, $\zeta = \alpha$, $\eta = \beta$
		}
		\uElseIf{$\alpha<\gamma<\beta$}{
			$p = a$, $q = c$, $\zeta = \alpha$, $\eta = \gamma$
		}
		\uElseIf{$\gamma=\alpha$}{
			$p = a + c$, $q = b$, $\zeta = \alpha$, $\eta = \beta$
		}
		\Else{
			$p = c$, $q = a$, $\zeta = \gamma$, $\eta = \alpha$
		}
	}

	\caption{Summing Power Numbers}
\end{algorithm}

The additive identity for Power Numbers is $0\epsilon^\infty$.
\end{definition}

\begin{definition}
\textbf{Multiplication} $*:\mathbf{PN}^K \times \mathbf{PN}^K \rightarrow \mathbf{PN}^K$ can be most simply expressed as addition of Power Numbers:
\begin{displaymath}
(a\epsilon^\alpha + b\epsilon^\beta) * (c\epsilon^\gamma + d\epsilon^\delta)
= (ac\epsilon^{\alpha+\gamma} + ad\epsilon^{\alpha+\delta}) + (bc\epsilon^{\beta+\gamma}+ bd\epsilon^{\beta+\delta})
= p\epsilon^\zeta + q\epsilon^\eta \in \mathbf{PN}^K
\end{displaymath}

The multiplicative identity for Power Numbers is $1 + 0\epsilon^\infty$.
\end{definition}

While $\mathbb{PN}^K_\epsilon$ is a monoid under both addition and multiplication, we can define two operations that have some of the properties we expect for subtraction and division. \\

\begin{definition}
\textbf{Subtraction} $-:\mathbf{PN}^K \rightarrow \mathbf{PN}^K$ is defined as:
$$-(a\epsilon^\alpha + b\epsilon^\beta) = (-a)\epsilon^\alpha + (-b)\epsilon^\beta$$
Naturally, $-:\mathbf{PN}^K \times \mathbf{PN}^K \rightarrow \mathbf{PN}^K$ is defined as $A + (-(B))$, where $A, B \in \mathbf{PN}^K$.
\end{definition}

\begin{definition}
The \textbf{multiplicative pseudo-inverse} $inv:\mathbf{PN}^K \rightarrow \mathbf{PN}^K$ is slightly more complicated.

\begin{algorithm}[H]
	\SetAlgoLined
	\KwData{$a\epsilon^\alpha + b\epsilon^\beta \in \mathbf{PN}^K$}
	\KwResult{$$\frac{1}{a\epsilon^\alpha + b\epsilon^\beta} = p\epsilon^\zeta + q\epsilon^\eta \in \mathbf{PN}^K$$}
	\uIf{$\alpha=\infty$}{
		Not defined.
	}
	\uElseIf{$\alpha=\beta$}{
		$p = 0$, $q = \frac{1}{a+b}$, $\zeta = -\alpha$, $\eta = -\alpha$
	}
	\Else{
		$p = \frac{1}{a+b}$, $q = -\frac{b}{a^2}$, $\zeta = -\alpha$, $\eta = \beta-2\alpha$
	}

	\caption{Multiplicative Inversion}
\end{algorithm}

\textbf{Division} $\div:\mathbf{PN}^K \times \mathbf{PN}^K \rightarrow \mathbf{PN}^K$ is defined as $A \div B = A * (inv(B))$, where $A, B \in \mathbf{PN}^K$.
\end{definition}

This matches the Dual Numbers case, where $\alpha = 0$, $\beta = 1$.

There is a special case wherein the pseudo-negative and -inverse are the true negative and inverse, which motivates the following
\begin{proposition}
For $A = a\epsilon^\alpha + b\epsilon^\beta \in \mathbf{PN}^K$, we have that $A-A=0\epsilon^\infty$ and $A \div A = 1 + 0\epsilon^\infty$ if and only if $\beta = \infty$.
\end{proposition}

\subsection{Exponentiation, Analytic Functions \& Asymptotic Series}

The definition of $A^m$ for $A \in \mathbf{PN}^K$, $m \in \mathbb{Z}$ follows naturally from the above definitions of multiplication and division. \\
Since we will generally consider $\epsilon$ to be small, we can extend any analytic function $h:K \rightarrow K$ to $h:\mathbf{PN}^K_{\epsilon} \rightarrow \mathbf{PN}^K_{\epsilon}$ for $\alpha \geq 0$. This is via Taylor series:

$$h(a\epsilon^0+b\epsilon^\beta) = h(a+b\epsilon^\beta) = h(a) + b\epsilon^\beta h'(a) \quad$$
$$h(a\epsilon^\alpha+b\epsilon^\beta) = h(0) + a\epsilon^\alpha h'(0) \quad where \quad \alpha \neq 0$$

Now, consider an asymptotic series $S(z)$ that is also a Hahn series \textit{ie} $S \in \mathbb{S}$ as in (1). The primary significance of Power Numbers comes from the following
\begin{proposition}
	Let $f(z) = az^\alpha + bz^\beta + o(z^\beta)$,	$g(z) = cz^\gamma + dz^\delta + o(z^\delta)$ be series of the form in $(1)$.
	Define the associated Power Numbers, $F = a\epsilon^\alpha + b\epsilon^\beta$, $G = c\epsilon^\gamma + d\epsilon^\delta$.
	Then, where $\cdot$ is one of $(+$, $-$, $*$, $/)$ we have that
	$$f(z) \cdot g(z) = pz^\zeta + qz^\eta + o(z^\eta)$$
	$$\Leftrightarrow$$
	$$F \cdot G = p\epsilon^\zeta + q\epsilon^\eta$$
	Furthermore, for any analytic function $h:K \rightarrow K$ we have
	$$h(f(z)) = pz^\zeta + qz^\eta + o(z^\eta)$$
	$$\Leftrightarrow$$
	$$h(F) = p\epsilon^\zeta + q\epsilon^\eta$$
\end{proposition}

\section{Examples}

\subsection{Rational Polynomials at Infinity}
We can get a simple system for evaluating a rational polynomial in the infinite limit. For example, consider:
$$f(z) = \frac{z^3+z^2+1}{z^3+z}$$
Taking the Power Number $z = \epsilon^{-1}$. By the arithmetic described above we have:
$$\frac{(\epsilon^{-3})+(\epsilon^{-2})+1}{(\epsilon^{-3})+(\epsilon^{-1})} = \frac{\epsilon^{-3}}{\epsilon^{-3}}=\epsilon^0$$
Which gives the correct limit when considering $\epsilon \rightarrow 0$.

\section{Conclusion}

\end{document}


\documentclass[namecite, fleqn]{goose-article}

\title{Non-linear elasticity}

\author{Tom W.J.\ de Geus}

\hypersetup{pdfauthor={T.W.J. de Geus}}

\begin{document}

\maketitle

\begin{abstract}
This constitutive model encompasses a non-linear, but history independent,
relation between the Cauchy stress, $\bm{\sigma}$,
and the linear strain tensor, $\bm{\varepsilon}$, i.e.:
\begin{equation*}
    \bm{\sigma} = f \left( \bm{\varepsilon} \right)
\end{equation*}
The model is implemented in 3-D, hence it can directly be used for either 3-D or
2-D plane strain problems.
\end{abstract}

\section{Constitutive model}

The following strain-energy is defined:
\begin{equation}
    U ( \bm{\varepsilon} )
    = \frac{9}{2} K \varepsilon_\mathrm{m}
    + \frac{ \sigma_0 \, \varepsilon_0 }{ n+1 }
    \left( \frac{\varepsilon_\mathrm{eq}}{\mathrm{\varepsilon_0}} \right)^{n+1}
\end{equation}
where $K$ is the bulk modulus, $\varepsilon_0$ and $\sigma_0$ are a reference strain and
stress respectively, and $n$ is an exponent that sets the degree of non-linearity.
Finally $\varepsilon_\mathrm{m}$ and $\varepsilon_\mathrm{eq}$ are the hydrostatic and
equivalent strains (see \cref{sec:ap:strain}).

This leads to the following stress--strain relation:
\begin{equation}
\label{eq:stress}
    \bm{\sigma}
    = \frac{\partial U}{\partial \bm{\varepsilon}}
    = 3 K \varepsilon_\mathrm{m} \, \bm{I}
    + \frac{2}{3} \frac{\sigma_0}{\varepsilon_0^n} \,
    \varepsilon_\mathrm{eq}^{n-1} \, \bm{\varepsilon}_\mathrm{d}
\end{equation}
see \cref{sec:ap:nomenclature} for nomenclature.

\section{Consistent tangent}

The consistent tangent maps a variation in strain, $\delta \bm{\varepsilon}$,
to a variation in stress, $\delta \bm{\sigma}$, as follows
\begin{equation}
  \delta \bm{\sigma} = \mathbb{C} : \delta \bm{\varepsilon}
\end{equation}
The tangent, $\mathbb{C}$, thus corresponds to the derivative of \cref{eq:stress} w.r.t.\ strain.
For this, the chain rule is employed:
\begin{equation}
    \mathbb{C}
    = \frac{\partial}{\partial \bm{\varepsilon}}
    \bigg[\;
        3 K \varepsilon_\mathrm{m} \, \bm{I}
    \;\bigg]
    + \frac{\partial}{\partial \bm{\varepsilon}_\mathrm{d}}
    \bigg[\;
        \frac{2}{3} \frac{\sigma_0}{\varepsilon_0^n} \,
        \varepsilon_\mathrm{eq}^{n-1} \, \bm{\varepsilon}_\mathrm{d}
    \;\bigg]
    : \frac{\partial \bm{\varepsilon}_\mathrm{d}}{\partial \bm{\varepsilon}}
\end{equation}
Where:
\begin{itemize}

    \item the derivative of the volumetric part reads
    \begin{equation}
        \frac{\partial}{\partial \bm{\varepsilon}}
        \bigg[\;
            3 K \varepsilon_\mathrm{m} \, \bm{I}
        \;\bigg]
        = K \bm{I} \otimes \bm{I}
    \end{equation}

    \item the chain rule for the deviatoric part reads
    \begin{align}
        \frac{\partial}{\partial \bm{\varepsilon}_\mathrm{d}}
        \bigg[\;
            \varepsilon_\mathrm{eq}^{n-1} \, \bm{\varepsilon}_\mathrm{d}
        \;\bigg]
        &=
        \frac{
            \partial \big[ \varepsilon_\mathrm{eq}^{n-1} \big]
        }{
            \partial \bm{\varepsilon}_\mathrm{d}
        } \otimes \bm{\varepsilon}_\mathrm{d}
        + \varepsilon_\mathrm{eq}^{n-1}
        \frac{
            \partial \bm{\varepsilon}_\mathrm{d}
        }{
            \partial \bm{\varepsilon}_\mathrm{d}
        }
        \\
        &=
        \tfrac{2}{3} (n-1) \, \varepsilon_\mathrm{eq}^{n-3} \,
        \bm{\varepsilon}_\mathrm{d} \otimes \bm{\varepsilon}_\mathrm{d}
        + \varepsilon_\mathrm{eq}^{n-1} \, \mathbb{I}
    \end{align}

    \item and it has been used that
    \begin{align}
        \frac{\partial}{\partial \bm{\varepsilon}_\mathrm{d}}
        \bigg[\;
            \varepsilon_\mathrm{eq}^{n-1}
        \;\bigg]
        &= (n-1)\, \varepsilon_\mathrm{eq}^{n-2} \,
        \frac{2}{3} \frac{\bm{\varepsilon}_\mathrm{d}}{\varepsilon_\mathrm{eq}}
        \\
        &= \tfrac{2}{3} (n-1) \,
        \varepsilon_\mathrm{eq}^{n-3} \, \bm{\varepsilon}_\mathrm{d}
    \end{align}

\end{itemize}

Combining the above yields:
\begin{align}
    \mathbb{C}
    &= K \bm{I} \otimes \bm{I} +
    \frac{2}{3} \frac{\sigma_0}{\varepsilon_0^n}
    \bigg(
        \tfrac{2}{3} (n-1) \, \varepsilon_\mathrm{eq}^{n-3}
        \bm{\varepsilon}_\mathrm{d} \otimes \bm{\varepsilon}_\mathrm{d}
        + \varepsilon_\mathrm{eq}^{n-1} \mathbb{I}
    \bigg) : \mathbb{I}_\mathrm{d} \\
    &= K \bm{I} \otimes \bm{I} +
    \frac{2}{3} \frac{\sigma_0}{\varepsilon_0^n}
    \bigg(
        \tfrac{2}{3} (n-1) \, \varepsilon_\mathrm{eq}^{n-3}
        \bm{\varepsilon}_\mathrm{d} \otimes \bm{\varepsilon}_\mathrm{d}
        + \varepsilon_\mathrm{eq}^{n-1} \, \mathbb{I}_\mathrm{d}
    \bigg)
\end{align}

\section{Consistency check}

To check if the derived tangent $\mathbb{C}$ a \emph{consistency check} can be performed.
A (random) perturbation $\delta \bm{\varepsilon}$ is applied.
The residual is compared to that predicted by the tangent.
For the general case of linearisation, the following holds:
\begin{equation}
    \bm{\sigma}\big( \bm{\varepsilon}_\star + \delta \bm{\varepsilon} \big) =
    \bm{\sigma}\big( \bm{\varepsilon}_\star \big) +
    \mathbb{C} \big( \bm{\varepsilon}_\star \big) : \delta \bm{\varepsilon} +
    \mathcal{O}(\delta \bm{\varepsilon}^2)
\end{equation}
or
\begin{equation}
    \underbrace{
        \bm{\sigma}\big( \bm{\varepsilon}_\star + \delta \bm{\varepsilon} \big) -
        \bm{\sigma}\big( \bm{\varepsilon}_\star \big)
    }_{
        \displaystyle \delta \bm{\sigma}
    } -
    \mathbb{C} \big( \bm{\varepsilon}_\star \big) : \delta \bm{\varepsilon} =
    \mathcal{O}(\delta \bm{\varepsilon}^2)
\end{equation}
This allows the introduction of a relative error
\begin{equation}
  \eta =
  \Big|\Big|
        \delta \bm{\sigma} -
        \mathbb{C}(\bm{\varepsilon}_\star) : \delta \bm{\varepsilon}
  \Big|\Big|
  /
  \Big|\Big| \delta \bm{\sigma} \Big|\Big|
\end{equation}
This \emph{truncation error} thus scales as $\eta \sim || \delta \bm{\varepsilon} ||^2$
as depicted in \cref{fig:consistency:expected}.
As soon as the error becomes sufficiently small the numerical \emph{rounding error}
becomes more dominant, the scaling thereof is also included in \cref{fig:consistency:expected}.

\begin{figure}[htp]
    \centering
    \includegraphics[width=.5\textwidth]{figures/consistency}
    \caption{Expected behaviour of the consistency check, see \citet[p.~9]{Heath2002}.}
    \label{fig:consistency:expected}
\end{figure}

The measurement of $\eta$ and a function of $|| \delta \bm{\varepsilon} ||$, as depicted in
\cref{fig:consistency}, indeed matches the prediction in \cref{fig:consistency:expected}.

\begin{figure}[htp]
    \centering
    \includegraphics[width=.5\textwidth]{examples/consistency}
    \caption{Measured consistency check, cf.\ \cref{fig:consistency:expected}.}
    \label{fig:consistency}
\end{figure}

\bibliography{library}

\appendix
\vfill\newpage

\section{Nomenclature}
\label{sec:ap:nomenclature}

\paragraph{Tensor products}
\vspace*{.5eM}

\begin{itemize}

    \item Dyadic tensor product
    \begin{align}
        \mathbb{C} &= \bm{A} \otimes \bm{B} \\
        C_{ijkl} &= A_{ij} \, B_{kl}
    \end{align}

    \item Double tensor contraction
    \begin{align}
        C &= \bm{A} : \bm{B} \\
        &= A_{ij} \, B_{ji}
    \end{align}

\end{itemize}

\paragraph{Tensor decomposition}
\vspace*{.5eM}

\begin{itemize}

    \item Deviatoric part $\bm{A}_\mathrm{d}$ of an arbitrary tensor $\bm{A}$:
    \begin{equation}
        \mathrm{tr}\left( \bm{A}_\mathrm{d} \right) \equiv 0
    \end{equation}
    and thus
    \begin{equation}
        \bm{A}_\mathrm{d} = \bm{A} - \tfrac{1}{3} \mathrm{tr}\left( \bm{A} \right)
    \end{equation}

\end{itemize}

\paragraph{Fourth order unit tensors}
\vspace*{.5eM}

\begin{itemize}

    \item Unit tensor:
    \begin{equation}
        \bm{A} \equiv \mathbb{I} : \bm{A}
    \end{equation}
    and thus
    \begin{equation}
        \mathbb{I} = \delta_{il} \delta{jk}
    \end{equation}
    \item Right-transposition tensor:
    \begin{equation}
        \bm{A}^T \equiv \mathbb{I}^{RT} : \bm{A} = \bm{A} : \mathbb{I}^{RT}
    \end{equation}
    and thus
    \begin{equation}
        \mathbb{I}^{RT} = \delta_{ik} \delta_{jl}
    \end{equation}
    \item Symmetrisation tensor:
    \begin{equation}
        \mathrm{sym} \left( \bm{A} \right) \equiv \mathbb{I}_\mathrm{s} : \bm{A}
    \end{equation}
    whereby
    \begin{equation}
        \mathbb{I}_\mathrm{s} = \tfrac{1}{2} \left( \mathbb{I} + \mathbb{I}^{RT} \right)
    \end{equation}
    This follows from the following derivation:
    \begin{align}
        \mathrm{sym} \left( \bm{A} \right) &= \tfrac{1}{2} \left( \bm{A} + \bm{A}^T \right)
        \\
        &= \tfrac{1}{2} \left( \mathbb{I} : \bm{A} + \mathbb{I}^{RT} : \bm{A} \right)
        \\
        &= \tfrac{1}{2} \left( \mathbb{I} + \mathbb{I}^{RT} \right) : \bm{A}
        \\
        &= \mathbb{I}_\mathrm{s} : \bm{A}
    \end{align}

    \item Deviatoric and symmetric projection tensor
    \begin{equation}
        \mathrm{dev} \left( \mathrm{sym}
        \left( \bm{A} \right) \right) \equiv \mathbb{I}_\mathrm{d} : \bm{A}
    \end{equation}
    from which it follows that:
    \begin{equation}
        \mathbb{I}_\mathrm{d}
        = \mathbb{I}_\mathrm{s} - \tfrac{1}{3} \bm{I} \otimes \bm{I}
    \end{equation}

\end{itemize}

\section{Strain measures}
\label{sec:ap:strain}

\begin{itemize}

    \item Mean strain
    \begin{equation}
        \varepsilon_\mathrm{m}
        = \tfrac{1}{3} \, \mathrm{tr} ( \bm{\varepsilon} )
        = \tfrac{1}{3} \, \bm{\varepsilon} : \bm{I}
    \end{equation}
    \item Strain deviator
    \begin{equation}
        \bm{\varepsilon}_\mathrm{d}
        = \bm{\varepsilon} - \varepsilon_\mathrm{m} \, \bm{I}
        = \mathbb{I}_\mathrm{d} : \bm{\varepsilon}
    \end{equation}
    \item Equivalent strain
    \begin{equation}
        \varepsilon_\mathrm{eq}
        = \; \sqrt{
            \tfrac{2}{3} \, \bm{\varepsilon}_\mathrm{d} : \bm{\varepsilon}_\mathrm{d}
        }
    \end{equation}

\end{itemize}

\section{Variations}
\label{sec:ap:variations}

\begin{itemize}

    \item Strain deviator
    \begin{equation}
        \delta \bm{\varepsilon}_\mathrm{d}
        = \left( \mathbb{I}_\mathrm{s} - \tfrac{1}{3} \bm{I} \otimes \bm{I} \right) :
        \delta \bm{\varepsilon}
        = \mathbb{I}_\mathrm{d} : \delta \bm{\varepsilon}
    \end{equation}

    \item Mean equivalent strain
    \begin{equation}
        \delta \varepsilon_\mathrm{m}
        = \tfrac{1}{3} \bm{I} : \delta \bm{\varepsilon}
    \end{equation}

    \item Von Mises equivalent strain
    \begin{align}
        \delta \varepsilon_\mathrm{eq}
        &= \frac{1}{3} \frac{1}{\varepsilon_\mathrm{eq}}
        \left( \bm{\varepsilon}_\mathrm{d} : \delta \bm{\varepsilon}_\mathrm{d} +
        \delta \bm{\varepsilon}_\mathrm{d} : \bm{\varepsilon}_\mathrm{d} \right) \\
        &= \frac{2}{3} \frac{1}{\varepsilon_\mathrm{eq}}
        \left( \bm{\varepsilon}_\mathrm{d} : \delta \bm{\varepsilon}_\mathrm{d} \right) \\
        &= \frac{2}{3} \frac{\bm{\varepsilon}_\mathrm{d}}{\varepsilon_\mathrm{eq}} :
        \delta \bm{\varepsilon}_\mathrm{d}
    \end{align}

\end{itemize}

\end{document}


\section{The \rotatecopy algorithm}
\Label{sec:rotatecopy}

The \rotatecopy algorithm of the \cxx Standard Library \cite[\S 28.6.11]{cxx-17-draft} copies, in a particular way,
the elements of one sequence of length~$n$ into a separate sequence.
More precisely,

\begin{itemize}
\item the first~$m$ elements of the first sequence become the last~$m$ elements of the second sequence, and
\item the last~$n-m$ elements of the first sequence become the first~$n-m$ elements of the second sequence.
\end{itemize}

Figure~\ref{fig:rotatecopy} illustrates the effects of \rotatecopy
by highlighting how the initial and final segments of the array~\inl{a[0..n-1]} are mapped
to corresponding subranges of the array~\inl{b[0..n-1]}.

\begin{figure}[hbt]
\centering
\includegraphics[width=0.62\textwidth]{Figures/rotate_copy.pdf}
\caption{\Label{fig:rotatecopy} Effects of \rotatecopy}
\end{figure}

For our purposes we have modified the generic implementation
to that of a range of type \valuetype.
%
The signature now reads:

\begin{lstlisting}[style=acsl-block]

  void rotate_copy(const value_type* a, size_type m, size_type n, value_type* b);
\end{lstlisting}

%\clearpage

\subsection{Formal specification of \rotatecopy}

The specification of \rotatecopy is shown in the following listing.
Note that we require explicitly that both ranges do not overlap and that we are only
able to \emph{read} from the range~\inl{a[0..n-1]}.

\input{Listings/rotate_copy.h.tex}

\subsection{Implementation of \rotatecopy}

The following listing shows an implementation of the \rotatecopy function.
The implementation simply calls the function \copyi twice.

\input{Listings/rotate_copy.c.tex}

%\clearpage


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CS624: Analysis of Algorithms
% Copyright 2015 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/beacon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Question 8}

The \textsc{Quicksort} algorithm contains two recursive calls to itself.
After \textsc{Quicksort} calls \textsc{Partition}, it recursively sorts the left subarray and then it recursively sorts the right subarray.
The second recursive call in \textsc{Quicksort} is not really necessary; we can avoid it by using an iterative control structure.
This technique, called \textit{tail recursion}, is provided automatically by good compilers.
Consider the following version of quicksort, which simulates tail recursion.

\begin{algorithm}[H]
\caption{\textsc{Tail-Recursive-Quicksort($A$,$p$,$r$)}}
\begin{algorithmic}[1]
\While {$p < r$}
\State q = \textsc{Partition}($A$,$p$,$r$)
\State \textsc{Tail-Recursive-Quicksort($A$,$p$,$q-1$)}
\State $p$ = $q$ + 1
\EndWhile
\end{algorithmic}
\end{algorithm}

\begin{enumerate}[label=(\alph*)]
\item Argue that \textsc{Tail-Recursive-Quicksort}($A$,1,r) correctly sorts array $A$.

\item Describe a scenario in which \textsc{Tail-Recursive-Quicksort}'s stack depth is $\Theta(n)$ on an $n$-element input array.

\item Modify the code for \textsc{Tail-Recursive-Quicksort} so that the worst-case stack depth is $\Theta(\log n)$.
Maintain the $\mathcal{O}(n\log n)$ expected running time of the algorithm.
\end{enumerate}

\subsection*{Solution}

\begin{enumerate}[label=(\alph*)]
\item It is assumed that the original \textsc{Quicksort} algorithm sorts correctly.
Given that the original \textsc{Quicksort} makes use of the \textsc{Partition} algorithm itself, it is claimed that the statement $q = \textsc{Partition}(A,p,r)$ in second line of the algorithm works properly.
As the algorithm calls itself at each iteration, proof of correctness is given by induction on the length $r$ of the array to be sorted.

\begin{itemize}
\item[] \emph{Base Step}: When length of array $r = 1$, the array is already sorted and \textsc{Tail-Recursive-Quicksort} algorithm sorts correctly.
\item[] \emph{Induction Step}: Inductive hypothesis is formed as \textsc{Tail-Recursive-Quicksort} works fine for any array of length $r \leq r_0$.
Using this hypothesis, we show the algorithm is correct for $r = r_0 + 1$.
This can simply be proven by following statements of the program.
When $r = r_0 + 1$, the algorithm calls itself with arguments ($A$, $p$, $q-1$).
Since $q - 1 < r_0+1$ for any $q$, all recursive calls are to an algorithm, correctness of which is already proven by the inductive hypothesis.
\end{itemize}

\item The number of iterations of \textit{while} loop in \textsc{Tail-Recursive-Quicksort} determines the stack depth.
To make stack depth bound by $\Theta(n_0)$ for an $n_0$-element array, it suffices to make the algorithm to call itself for sorting an array ($n-1$)-element for any $n \leq n_0$.
This means argument $q-1$ of the \textsc{Tail-Recursive-Quicksort} algorithm should be decremented for each iteration.
This goal will be achieved, if we have an already-sorted array of length $n_0$.
In this case, \textsc{Partition}($A$,$p$,$r$) will always be $r$ for any $r \leq r_0$.

\item To maintain the expected runtime of the \textsc{Tail-Recursive-Quicksort} algorithm at $\Theta(n \log n)$, we cannot change arguments of the \textsc{Partition} and recursive \textsc{Tail-Recursive-Quicksort} calls.
Therefore the only way to change the worst-case scenario is to change the order of partitioned sub-arrays so that the small sub-arrays be sorted first.
This is made by insertion of a statement in between lines $2$ and $3$ that compares length of the sub arrays and gives the shortest one to the \textsc{Tail-Recursive-Quicksort} recursion.

\end{enumerate}


\documentclass{memoir}
\usepackage{notestemplate}

% \begin{figure}[ht]
%     \centering
%     \incfig{riemmans-theorem}
%     \caption{Riemmans theorem}
%     \label{fig:riemmans-theorem}
% \end{figure}

\begin{document}
% \section{}

Now we consider functions on the complex field. Namely, we are characterizing functions \(f:\C\to \C\). It is immediate that any function \(f\) can be parametrized by
\begin{align*}
	f:\C\to \C\\
	(x,y) \mapsto (u(x,y),v(x,y))
\end{align*}
for \(u,v \) real-valued functions. Sometimes we might write \(f = u + iv\) for shorthand of the above parametrization. This notation arises because
\begin{align*}
	f(x,y) = (u(x,y),v(x,y)) &\implies\\
	f &= (u(x,y),0) + (0,v(x,y)) \implies\\
	f &= u + iv.
\end{align*}
Now we construct the equivalent analytic structures on complex functions.
\begin{defn}[Complex Continuity]
	Let \(\Omega\subset \C\). A function \(f:\Omega\to \C\) is \textbf{continuous} if for all \(\varepsilon >0\), for all \(z \in \Omega\), there exists a \(\delta>0\) such that, for all \(w \in \Omega\),
	\begin{align*}
		|w-z| < \delta \implies \left| f(w)-f(z) \right| < \varepsilon
	\end{align*}
Equivalently, if \((z_n) \subset \Omega\) is an arbitrary sequence in \(\Omega\) converging to \(z \in \C\), then \(f(z_n) \to f(z)\).\\

We denote the space of complex continuous functions on \(\Omega\) by \(C(\Omega)\).
\end{defn}

\begin{anki}
TARGET DECK
Complex Qual::Complex Analysis

START
MathJaxCloze
Text: Let \(\Omega\subset \C\). A function \(f:\Omega\to \C\) is **continuous** if {{c2::for all \(\varepsilon >0\)}}, {{c2::for all \(z \in \Omega\)}}, {{c2::there exists a \(\delta>0\)}} such that, {{c2::for all \(w \in \Omega\)}},
{{c1::\(\begin{align*}
        	|w-z| < \delta \implies \left| f(w)-f(z) \right| < \varepsilon
        \end{align*}\)}}
Equivalently, if \((z_n) \subset \Omega\) is an arbitrary sequence in \(\Omega\) converging to \(z \in \C\), then {{c1::\(f(z_n) \to f(z)\)}}.
Extra: Of course, we can alternatively define continuity of a function at a point, then refer to a continuous function as one that's continuous at all points.
Tags: analysis complex_analysis complex_analyticity defn
<!--ID: 1624399037000-->
END
\end{anki}

Of course, we can alternatively define continuity of a function at a point, then refer to a continuous function as one that's continuous at all points.\\

The sum, difference, product, ratio, and composition of continuous functions is continuous. We leave the proof of this as an exercise to the reader.
\begin{defn}[Uniform Continuity]
	Let \(\Omega \subset \C\). A function \(f:\Omega\to \C\) is \textbf{uniformly continuous} if for all \(\varepsilon\), there exists a \(\delta>0\) such that for all \(z,w \in \Omega\),
	\begin{align*}
		|w-z| < \delta \implies \left| f(w)-f(z) \right| < \varepsilon
	\end{align*}
\end{defn}

\begin{anki}
START
MathJaxCloze
Text: Let \(\Omega \subset \C\). A function \(f:\Omega\to \C\) is **uniformly continuous** if {{c1::for all \(\varepsilon\)}}, {{c1::there exists a \(\delta>0\)}} such that {{c1::for all \(z,w \in \Omega\)}},
{{c2::\(\begin{align*}
        	|w-z| < \delta \implies \left| f(w)-f(z) \right| < \varepsilon
        \end{align*}\)}}
Extra: In other words, we choose \(\delta \) first before being given the point \(z \in \Omega\). On unbounded domains, this is a much stronger property than continuity.
Tags: analysis complex_analysis complex_analyticity defn
<!--ID: 1624399037047-->
END
\end{anki}

In other words, we choose \(\delta \) first before being given the point \(z \in \Omega\). On unbounded domains, this is a much stronger property than continuity. However, under many circumstances the notions are equivalent:
\begin{thm}
	On a compact set in \(\C\), every continuous function is uniformly continuous.
\end{thm}
\begin{anki}
START
MathJaxCloze
Text: On a compact set in \(\C\), every continuous function is {{c1::uniformly continuous}}.
Tags: analysis complex_analysis complex_analyticity
<!--ID: 1624399037095-->
END
\end{anki}

Many of the theorems here are equivalent to the real-valued version, and hence unless the proof differs, proofs will be omitted as they can be found in a standard real analysis textbook.\\

\begin{thm}
	The continuous image of a compact set in \(\C\) is compact.
\end{thm}
This is actually a corollary via point-set topology.

\begin{cor}
	A continuous real-valued function on a compact set has a maximum and a minimum.
\end{cor}
This doesn't seem very relevant immediately, as the range of a complex function is not well-ordered. However, by considering the modulus of complex functions, we can obtain a similar result.

\begin{defn}[Maximum and Minimum of Complex Functions]
	Let \(f:\C\to \C\) be a complex-valued function. We say that \(f\) attains a \textbf{maximum} at \(z_0 \in \Omega \) if
	\begin{align*}
		\left| f(z) \right| \leq \left| f(z_0) \right|
	\end{align*}for all \(z \in \Omega \).\\

	If instead
	\begin{align*}
		\left| f(z) \right| \geq \left| f(z_0) \right|
	\end{align*} for all \(z\in \Omega \) then we say \(z_0\) is a \textbf{minimum} for \(f\).
\end{defn}
Notice that if \(f\) is continuous, then \(z\mapsto \left| f(z) \right| \) is continuous (by the triangle inequality).

\begin{anki}
START
MathJaxCloze
Text: Let \(f:\C\to \C\) be a complex-valued function. We say that \(f\) attains a **maximum** at \(z_0 \in \Omega \) if
{{c1::\(\begin{align*}
        	\left| f(z) \right| \leq \left| f(z_0) \right|
        \end{align*}\)}}
for all \(z \in \Omega \).

If instead
{{c1::\(\begin{align*}
      \left| f(z) \right| \geq \left| f(z_0) \right|
      \end{align*}\)}}
for all \(z\in \Omega \) then we say \(z_0\) is a **minimum** for \(f\).
Extra: Notice that if \(f\) is continuous, then \(z\mapsto \left| f(z) \right| \) is continuous (by the triangle inequality).
Tags: analysis complex_analysis complex_analyticity defn
<!--ID: 1624399037133-->
END
\end{anki}

\begin{cor}
	A continuous (real or complex-valued) function on a compact set \(\Omega\) is bounded and attains a maximum and minimum on \(\Omega\).
\end{cor}

\begin{anki}
START
MathJaxCloze
Text: A continuous (real or complex-valued) function on a compact set \(\Omega \) is {{c1::bounded}} and {{c1::attains a maximum and minimum on \(\Omega\)}}.
Tags: analysis complex_analysis complex_analyticity
<!--ID: 1624399037173-->
END
\end{anki}

\subsection{Curves in \(\C\)}
\label{sub:curves_in_c}

\begin{defn}[Continuous Curve]
	Let \(\Omega\subset \C\). A \textbf{continuous curve} in \(\Omega\) is a continuous function \(\gamma :[t_0,t_1]\to \Omega\) where \([t_0,t_1]\subset \R\).
\end{defn}

\begin{anki}
% Up to 5 consequences
START
Definition
Name: Continuous Curve in \(\C\)
Premise 1: \(\Omega\subset \C\), \([t_0,t_1] \subset \R\)
Consequence 1: \(\gamma \) is a continuous curve if \(\gamma:[t_0,t_1]\to \Omega\) is a continous function
Tags: analysis complex_analysis complex_analyticity defn
<!--ID: 1624399037216-->
END
\end{anki}

Because every complex function can be parametrized by \(F(x,y) = (u(x,y),v(x,y))\), it follows that we can write a curve \(\gamma \) by
\begin{align*}
	\gamma (t) = (x(t),y(t))
\end{align*}
for real-valued curves \(x,y\). This will prove useful later when we introduce complex differentiation.

\begin{defn}[Path-Connected]
	Let \(\Omega\subset \C\). Then \(\Omega\) is path-connected if every two points in  \(\Omega\) can be joined by a continuous curve in \(\Omega\).
\end{defn}

\begin{anki}
% Up to 5 consequences
START
Definition
Name: Path-Connected Set in \(\C\)
Premise 1: \(\Omega\subset \C\)
Consequence 1: \(\Omega\) is path-connected if \(\forall z_0,z_1 \in \Omega\), \(\exists \gamma:[t_0,t_1]\to \Omega\) with \(\gamma(t_i)=z_i\)
Tags: analysis complex_analysis complex_topology defn
<!--ID: 1624399037262-->
END
\end{anki}

\begin{thm}
	Let \(\Omega\subset \C\) be open, Then the following are equivalent:
	\begin{itemize}
		\item \(\Omega\) is connected
		\item Every two points in \(\Omega\) can be joined by a broken line consisting of (finitely many) horizontal and vertical line segments within \(\Omega\)
		\item \(\Omega\) is path-connected
	\end{itemize}
\end{thm}

\begin{anki}
% Up to 4 premises
% Up to 5 equivalences
START
Equivalence
Name: Connectedness of open \(\Omega\subset \C\)
Premise 1:  \(\Omega\subset \C\) open set
Equivalence 1: \(\Omega\) is connected
Equivalence 2: Every \(z_0,z_1 \in \Omega\) is joined by line consisting of (finitely many) horizontal and vertical line segments in \(\Omega\)
Equivalence 3: \(\Omega\) is path-connected
Tags: analysis complex_analysis complex_topology
<!--ID: 1624399037300-->
END
\end{anki}

\end{document}


\subsection{part b}
Now design PID controller with following methods.
\newpage
\begin{itemize}
    \item ziegler nichols
    $$
    G_c =   \dfrac{4.756 s^2 + 6.423 s + 2.27}{0.1904 s^2 + 2.76 s}
    $$
    \begin{figure}[H]
        \caption{step responde with ziegler nichols PID controller}
        \centering
        \includegraphics[width=10cm]{../Figure/Q1/b/zn.png}
    \end{figure}
    \item refined ziegler nichols
    $$
    G_c =     \dfrac{3.099 s + 2.27}{1.629 s}, \qquad H =
    \dfrac{1.051 s^2 + 1.698 s + 1}{0.09418 s^2 + 1.434 s + 1}
    $$
    \begin{figure}[H]
        \caption{step responde with refined ziegler nichols PID controller}
        \centering
        \includegraphics[width=10cm]{../Figure/Q1/b/rzn.png}
    \end{figure}
    \item modified ziegler nichols
    $$
    r_1 = 1.0, \qquad p_b = 5:5:35
    $$
    \begin{figure}[H]
        \caption{step responde with modified ziegler nichols PID controller}
        \centering
        \includegraphics[width=10cm]{../Figure/Q1/b/mzn.png}
    \end{figure}
    \item Cohen Coon
    $$
    G_c =   \dfrac{3.223 s^2 + 7.463 s + 3.189}{ 0.09188 s^2 + 2.3 s}
    $$
    \begin{figure}[H]
        \caption{step responde with Cohen Coon PID controller}
        \centering
        \includegraphics[width=10cm]{../Figure/Q1/b/cc.png}
    \end{figure}
    \newpage
    \item Cohen Coon revisited
    $$
    G_c =   \dfrac{3.374 s^2 + 7.744 s + 3.202}{0.09579 s^2 + 2.378 s}
    $$
    \begin{figure}[H]
        \caption{step responde with Cohen Coon revisited PID controller}
        \centering
        \includegraphics[width=10cm]{../Figure/Q1/b/ccr.png}
    \end{figure}
    \item Astrom Hagglund
    $$
    G_c =   \dfrac{0.9211 s^2 + 1.794 s + 1.385}{0.06048 s^2 + 1.247 s}
    $$
    \begin{figure}[H]
        \caption{step responde with Astrom Hagglund PID controller}
        \centering
        \includegraphics[width=10cm]{../Figure/Q1/b/ah.png}
    \end{figure}
    \item Frequency based Astrom Hagglund
    $$
    G_c =   \dfrac{1.025 s^2 + 1.666 s + 1.355}{0.0688 s^2 + 1.171 s}
    $$
    \begin{figure}[H]
        \caption{step responde with Frequency based Astrom Hagglund PID controller}
        \centering
        \includegraphics[width=10cm]{../Figure/Q1/b/fah.png}
    \end{figure}
    \item CHR set point $0\%$ overshoot
    $$
    G_c =   \dfrac{0.8439 s^2 + 1.19 s + 1.135}{0.06758 s^2 + 0.9794 s}
    $$
    \begin{figure}[H]
        \caption{step responde with CHR set point $0\%$ overshoot PID controller}
        \centering
        \includegraphics[width=10cm]{../Figure/Q1/b/chr0.png}
    \end{figure}
    \item CHR set point $20\%$ overshoot
    $$
    G_c =   \dfrac{1.758 s^2 + 2.581 s + 1.797}{0.08893 s^2 + 1.371 s}
    $$
    \begin{figure}[H]
        \caption{step responde with CHR set point $20\%$ overshoot PID controller}
        \centering
        \includegraphics[width=10cm]{../Figure/Q1/b/chr20.png}
    \end{figure}
    \item WJC
    $$
    G_c =   \dfrac{9.298 s^2 + 21.39 s + 12.51}{0.4048 s^2 + 10 s}
    $$
    \begin{figure}[H]
        \caption{step responde with WJC PID controller}
        \centering
        \includegraphics[width=10cm]{../Figure/Q1/b/wjc.png}
    \end{figure}
    \item optimum set point PID ISTE
    $$
    G_c =   \dfrac{1.993 s^2 + 3.738 s + 2.496}{0.07258 s^2 + 1.447 s}
    $$
    \begin{figure}[H]
        \caption{step responde with optimum PID controller}
        \centering
        \includegraphics[width=10cm]{../Figure/Q1/b/optpid.png}
    \end{figure}
    \item optimum set point PI-D ISTE
    $$
    G_c =   \dfrac{4.219 s + 2.41}{1.751 s} \qquad H = \frac{0.9836 s^2 + 4.328 s + 2.41}{0.191 s^2 + 4.328 s + 2.41}
    $$
    \begin{figure}[H]
        \caption{step responde with optimum PI-D controller}
        \centering
        \includegraphics[width=10cm]{../Figure/Q1/b/optpi-d.png}
    \end{figure}
\end{itemize}
\subsection{conclusion}
ziegler nichols is very slow but refined ziegler nichols is better ans faster. modified ziegler nichols is very slow.
Cohen coon is slow and has hight overshoot but cohen coon revisited is faster.
Astromm hagglund is very good in doesn't have overshoot and system is fast but Frequency is a little slow. CHR is fast but overshoot method work strange. Zero overshoot has overshoot but 20\% overshoot has lower overshoot.
WJC work fast and have very small overshoot.
Optimum PID work well but PI-D is a little slow.
We don't what the system is so we can't select best controller it depends on our plant.


\section{Distributions}
\label{sec:distributions}
\newcommand{\distname}[1]{\textbf{#1}}
\newcommand{\distattrib}[1]{\textit{#1}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% If you are confused by the input of this document, please make sure you see
% these defined commands first. There is no point writing the same thing over
% and over and over and over and over again, so these will help us reduce typos,
% by just editing a template sentence.
\newcommand{\nameDescription}{\xmlAttr{name},
  \xmlDesc{required string attribute}, user-defined name of this distribution.
  %
  \nb As with other objects, this identifier can be used to reference this
  specific entity from other input blocks in the XML.}
\newcommand{\specBlock}[2]{The specifications of this distribution must be
  defined within #1 \xmlNode{#2} XML block.}
\newcommand{\attrIntro}{This XML node accepts one attribute:}
\newcommand{\attrsIntro}{This XML node accepts the following attributes:}
\newcommand{\subnodeIntro}{This distribution can be initialized with the
  following child node:}
\newcommand{\subnodesIntro}{This distribution can be initialized with the
  following children nodes:}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%\maljdan{Do we want to provide the equations of each distribution?}
%\alfoa{TBH I do not think so. It is not a theory manual... I put few equations in the ROM section just to explain better the meaning of some parameters.}

RAVEN provides support for several probability distributions.
%
Currently, the user can choose among several 1-dimensional distributions and
$N$-dimensional ones, either custom or multidimensional normal.

The user will specify the probability distributions, that need to be used during
the simulation, within the \xmlNode{Distributions} XML block:
\begin{lstlisting}[style=XML]
<Simulation>
   ...
  <Distributions>
    <!-- All the necessary distributions will be listed here -->
  </Distributions>
  ...
</Simulation>
\end{lstlisting}

In the next two sub-sections, the input requirements for all of the
distributions are reported.

%%%%%% 1-Dimensional Probability distributions
\subsection{1-Dimensional Probability Distributions}
\label{subsec:1dDist}
This sub-section is organized in two different parts: 1) continuous 1-D
distributions and 2) discrete 1-D distributions.
%
These two paragraphs cover all the requirements for using the different
distribution entities.
%
%%%%%% paragraph 1-Dimensional Continuous Distributions.
\subsubsection{1-Dimensional Continuous Distributions}
\label{subsubsec:1DContinuous}
In this paragraph all the 1-D distributions currently available in RAVEN are
reported.

Firstly, all the probability distributions functions in the code can be
truncated by using the following keywords:
\begin{lstlisting}[style=XML]
<Distributions>
   ...
   <aDistributionType>
      ...
      <lowerBound>aFloatValue</lowerBound>
      <upperBound>aFloatValue</upperBound>
      ...
   </aDistributionType>
</Distributions>
\end{lstlisting}
Each distribution has a pre-defined, default support (domain) based on its
definition, however these domains can be shifted/stretched using the appropriate
\xmlNode{low} and \xmlNode{high} parameters where applicable, and/or truncated
using the nodes in the example above, namely \xmlNode{lowerBound} and
\xmlNode{upperBound}.
For example, the Normal distribution domain is $[-\infty,+\infty]$, and thus
cannot be shifted or stretched, as it is already unbounded, but can be
truncated.
%
RAVEN currently provides support for 13 1-Dimensional distributions.
%
In the following paragraphs, all the input requirements are reported and
commented.

%%%%%% Beta
\paragraph{Beta Distribution}
\label{Beta}
The \distname{Beta} distribution is parameterized by two positive shape parameters, denoted by
$\alpha$ and $\beta$, that appear as exponents of the random variable. Its default support
(domain) is $x \in [0, 1]$.
%
The distribution domain can be changed, specifying new boundaries, to fit the user's needs.
%
The user can specify a \distname{Beta} distribution in two ways.  The standard
is to provide the parameters \xmlNode{low}, \xmlNode{high}, \xmlNode{alpha},
and \xmlNode{beta}.  Alternatively, to approximate a normal
distribution that falls to 0 at the endpoints, the user may provide
the parameters \xmlNode{low}, \xmlNode{high}, and \xmlNode{peakFactor}. The
peak factor is a value between 0 and 1 that determines the peakedness of the
distribution.  At 0 it is dome-like ($\alpha=\beta=4$) and at 1 it is very
strongly peaked around the mean ($\alpha=\beta=100$).  A reasonable approximation
to a Gaussian normal is a peak factor of 0.5.

\specBlock{a}{Beta}
%
\attrIntro
\vspace{-5mm}
\begin{itemize}
  \itemsep0em
  \item \nameDescription
\end{itemize}
\vspace{-5mm}
\subnodesIntro
\begin{itemize}
  \item Standard initialization:
    \begin{itemize}
    \item \xmlNode{alpha}, \xmlDesc{float, conditional required parameter}, first shape
     parameter.  If specified, \xmlNode{beta} must also be inputted and
     \xmlNode{peakFactor} can not be specified.
%
     \item \xmlNode{beta}, \xmlDesc{float, conditional required parameter}, second shape
      parameter.  If specified, \xmlNode{alpha} must also be inputted and
      \xmlNode{peakFactor} can not be specified.
%
       \item \xmlNode{low}, \xmlDesc{float, optional parameter}, lower domain
       boundary. \default{0.0}
 %
       \item \xmlNode{high}, \xmlDesc{float, optional parameter}, upper domain,
         boundary. \default{1.0}
      \end{itemize}
 %
     \item Alternative initialization:
     \begin{itemize}
        \item \xmlNode{peakFactor}, \xmlDesc{float, optional parameter}, alternative
         to specifying \xmlNode{alpha} and \xmlNode{beta}.  Acceptable values range from
        0 to 1.
%
       \item \xmlNode{low}, \xmlDesc{float, optional parameter}, lower domain
       boundary. \default{0.0}
 %
       \item \xmlNode{high}, \xmlDesc{float, optional parameter}, upper domain,
         boundary. \default{1.0}
      \end{itemize}
  %
\end{itemize}

Example:
\begin{lstlisting}[style=XML]
<Distributions>
  ...
  <Beta name='aUserDefinedName'>
     <low>aFloatValue</low>
     <high>aFloatValue</high>
     <alpha>aFloatValue</alpha>
     <beta>aFloatValue</beta>
  </Beta>
  <Beta name='aUserDefinedName2'>
     <low>aFloatValue</low>
     <high>aFloatValue</high>
     <peakFactor>aFloatValue</peakFactor>
  </Beta>
  ...
</Distributions>
\end{lstlisting}

%%%%%% Exponential
\paragraph{Exponential Distribution}
\label{Exponential}
The \distname{Exponential} distribution has a default support of
$x \in [0, +\infty)$.

\specBlock{an}{Exponential}
%
\attrIntro
\vspace{-5mm}
\begin{itemize}
  \itemsep0em
  \item \nameDescription
\end{itemize}
\vspace{-5mm}
\subnodeIntro
\begin{itemize}
  \item \xmlNode{lambda}, \xmlDesc{float, required parameter}, rate parameter.
  \item \xmlNode{low}, \xmlDesc{float, optional parameter}, lower domain
     boundary. \default{0.0}
  %
\end{itemize}

Example:
\begin{lstlisting}[style=XML]
<Distributions>
  ...
  <Exponential name='aUserDefinedName'>
    <lambda>aFloatValue</lambda>
    <low>aFloatValue</low>
  </Exponential>
  ...
</Distributions>
\end{lstlisting}

%%%%%% Gamma
\paragraph{Gamma Distribution}
\label{Gamma}
The \distname{Gamma} distribution is a two-parameter family of continuous
probability distributions.
%
The common exponential distribution and $\chi$-squared distribution are special
cases of the gamma distribution.
%
Its default support is $x \in [0,+\infty]$.

\specBlock{a}{Gamma}
%
\attrIntro
\vspace{-5mm}
\begin{itemize}
  \itemsep0em
  \item \nameDescription
\end{itemize}
\vspace{-5mm}
\subnodesIntro
\begin{itemize}
  \item \xmlNode{alpha}, \xmlDesc{float, required parameter}, shape parameter.
  %
  \item \xmlNode{beta}, \xmlDesc{float, optional parameter}, 1/scale or the
  inverse scale parameter. \default{1.0}
  \item \xmlNode{low}, \xmlDesc{float, optional parameter}, lower domain
  boundary. \default{0.0}
  %
\end{itemize}

Example:
\begin{lstlisting}[style=XML]
<Distributions>
  ...
  <Gamma name='aUserDefinedName'>
    <alpha>aFloatValue</alpha>
    <beta>aFloatValue</beta>
    <low>aFloatValue</low>
  </Gamma>
  ...
</Distributions>
\end{lstlisting}

%%%%%% Laplace
\paragraph{Laplace Distribution}
\label{Laplace}
The \distname{Laplace} distribution is a two-parameter continuous
probability distribution.  It is the distribution of the differences
between two independent random variables with identical exponential
distributions.
%
Its default support is $x \in (-\infty,+\infty)$.

\specBlock{a}{Laplace}
%
\attrIntro
\vspace{-5mm}
\begin{itemize}
  \itemsep0em
  \item \nameDescription
\end{itemize}
\vspace{-5mm}
\subnodesIntro
\begin{itemize}
\item \xmlNode{location}, \xmlDesc{float, required parameter},
  determines the location or shift of the distribution.
\item \xmlNode{scale}, \xmlDesc{float, required parameter}, must be
  greater than 0, and determines how spread out the distribution is.
\end{itemize}

Example:
\begin{lstlisting}[style=XML]
<Distributions>
  ...
  <Laplace name='aUserDefinedName'>
    <location>aFloatValue</location>
    <scale>aFloatValue</scale>
  </Laplace>
  ...
</Distributions>
\end{lstlisting}

%%%%%% Logistic
\paragraph{Logistic Distribution}
\label{Logistic}
The \distname{Logistic} distribution is similar to the
normal distribution with a CDF that is an instance of a logistic function ($Cdf(x) = \frac{1}{1+e^{-\frac{(x-location)}{scale})}}$).
%
It resembles the normal distribution in shape but has heavier tails (higher
kurtosis).
%
Its default support is $x \in [-\infty,+\infty]$.

\specBlock{a}{Logistic}
%
\attrIntro
\vspace{-5mm}
\begin{itemize}
  \itemsep0em
  \item \nameDescription
\end{itemize}
\vspace{-5mm}
\subnodesIntro
\begin{itemize}
  \item \xmlNode{location}, \xmlDesc{float, required parameter}, the
  distribution
  mean.
  %
  \item \xmlNode{scale}, \xmlDesc{float, required parameter}, scale parameter
  that
  is proportional to the standard deviation ($\sigma ^{2}=\frac{1}{3}\pi^{2}scale^{2} $).
  %
\end{itemize}

Example:
\begin{lstlisting}[style=XML]
<Distributions>
  ...
  <Logistic name='aUserDefinedName'>
    <location>aFloatValue</location>
    <scale>aFloatValue</scale>
  </Logistic>
  ...
</Distributions>
\end{lstlisting}

%%%%%% LogNormal
\paragraph{LogNormal Distribution}
\label{LogNormal}
The \distname{LogNormal} distribution is a distribution with the
logarithm of the random variable being normally distributed.
%
Its default support is $x \in [0, +\infty]$.

\specBlock{a}{LogNormal}
%
\attrIntro
\vspace{-5mm}
\begin{itemize}
  \itemsep0em
  \item \nameDescription
\end{itemize}
\vspace{-5mm}
\subnodesIntro
\begin{itemize}
  \item \xmlNode{mean}, \xmlDesc{float, required parameter}, the log of the distribution
  mean or expected value.
  %
  \item \xmlNode{sigma}, \xmlDesc{float, required parameter}, standard
  deviation.
  \item \xmlNode{low}, \xmlDesc{float, optional parameter}, lower domain
  boundary. \default{0.0}
  %
\end{itemize}
\nb The \xmlNode{mean} and \xmlNode{sigma} listed above are NOT the mean and standard deviation of the
distribution; they are the mean and standard deviation of the log of the distribution.  Using the following
notation:
\begin{itemize}
  \item $\mu_\ell$: the $\mu$ parameter of the lognormal distribution, which RAVEN expects in the
    \xmlNode{mean} node;
  \item $\sigma_\ell$: the $\sigma$ parameter of the lognormal distribution, which RAVEN expects in the
    \xmlNode{sigma} node;
  \item $M$: the user-desired mean value of the distribution;
  \item $S$: the user-desired standard deviation of the distribution;
\end{itemize}
a conversion is defined to translate from mean $M$ and standard deviation $S$ into the parameters RAVEN
expects:
\begin{equation}
  \mu_\ell = \log\left(\frac{M}{\sqrt{1+\frac{S^2}{M^2}}}\right),
\end{equation}
\begin{equation}
  \sigma_\ell = \sqrt{\log{1+\frac{S^2}{M^2} }}.
\end{equation}

Example:
\begin{lstlisting}[style=XML]
<Distributions>
  ...
  <LogNormal name='aUserDefinedName'>
    <mean>aFloatValue</mean>
    <sigma>aFloatValue</sigma>
    <low>aFloatValue</low>
  </LogNormal>
  ...
</Distributions>
\end{lstlisting}

%%%%%% LogUniform
\paragraph{LogUniform Distribution}
\label{LogNormal}
The \distname{LogNormal} distribution is a distribution associated to
a variable $y=h(x)=e^{x}$ where variable x is uniform distributed.
This distribution supports not only the case  $y=h(x)=e^{x}$ (natural case) but also
the case where $y=h(x)=10^{x}$ (decimal case).
%

Its default support is $x \in [h(lowerBound),h(upperBound)]$.

\specBlock{a}{LogUniform}
%
\attrIntro
\vspace{-5mm}
\begin{itemize}
  \itemsep0em
  \item \nameDescription
\end{itemize}
\vspace{-5mm}
\subnodesIntro
\begin{itemize}
  \item \xmlNode{lowerBound}, \xmlDesc{float, required parameter}, domain lower boundary.
  %
  \item \xmlNode{upperBound}, \xmlDesc{float, required parameter}, domain upper boundary.
  %
  \item \xmlNode{base}, \xmlDesc{string, required parameter}, case type (decimal or natural).
  %
\end{itemize}

Example:
\begin{lstlisting}[style=XML]
<Distributions>
  ...
  <LogUniform name="x_dist">
    <upperBound>1.0</upperBound>
    <lowerBound>3.0</lowerBound>
    <base>natural</base>
  </LogUniform>
  ...
</Distributions>
\end{lstlisting}

%%%%%% Normal
\paragraph{Normal Distribution}
\label{Normal}
The \distname{Normal} distribution is an extremely
useful continuous distribution.
%
Its utility is due to the central limit theorem, which states that, under mild
conditions, the mean of many random variables independently drawn from the same
distribution is distributed approximately normally, irrespective of the form of
the original distribution.
%
Its default support is $x \in [-\infty, +\infty]$.

\specBlock{a}{Normal}
%
\attrIntro
\vspace{-5mm}
\begin{itemize}
  \itemsep0em
  \item \nameDescription
\end{itemize}
\vspace{-5mm}
\subnodesIntro
\begin{itemize}
  \item \xmlNode{mean}, \xmlDesc{float, required parameter}, the distribution
  mean
  or expected value.
  %
  \item \xmlNode{sigma}, \xmlDesc{float, required parameter}, the standard
  deviation.
  %
\end{itemize}

Example:
\begin{lstlisting}[style=XML]
<Distributions>
  ...
  <Normal name='aUserDefinedName'>
    <mean>aFloatValue</mean>
    <sigma>aFloatValue</sigma>
  </Normal>
  ...
</Distributions>
\end{lstlisting}

%%%%%% Triangular
\paragraph{Triangular Distribution}
\label{Triangular}
The \distname{Triangular} distribution is a continuous distribution that has a
triangular shape for its PDF.
%
%It is often used where the distribution is only vaguely known.
%
Like the uniform distribution, upper and lower limits are ``known,'' but a
``best guess,'' of the mode or center point is also added.
%
It has been recommended as a ``proxy'' for the beta distribution.
%
Its default support is $x \in [min,max]$.

\specBlock{a}{Triangular}
%
\attrIntro
\vspace{-5mm}
\begin{itemize}
  \itemsep0em
  \item \nameDescription
\end{itemize}
\vspace{-5mm}
\subnodesIntro
\begin{itemize}
  \item \xmlNode{apex}, \xmlDesc{float, required parameter}, peak location
  %``best guess'', also called, peak factor.
  %
  \item \xmlNode{min}, \xmlDesc{float, required parameter}, domain lower
  boundary.
  %
  \item \xmlNode{max}, \xmlDesc{float, required parameter}, domain upper
  boundary.
  %
\end{itemize}

Example:
\begin{lstlisting}[style=XML]
<Distributions>
  ...
  <Triangular name='aUserDefinedName'>
    <apex>aFloatValue</apex>
    <min>aFloatValue</min>
    <max>aFloatValue</max>
  </Triangular>
  ...
</Distributions>
\end{lstlisting}

%%%%%% Uniform
\paragraph{Uniform Distribution}
\label{Uniform}
The \distname{Uniform} distribution is a continuous distribution with a
rectangular-shaped PDF.
%
It is often used where the distribution is only vaguely known, but upper and
lower limits are known.
%
Its default support is $x \in [lower,upper]$.

\specBlock{a}{Uniform}
%
\attrIntro
\vspace{-5mm}
\begin{itemize}
  \itemsep0em
  \item \nameDescription
\end{itemize}
\vspace{-5mm}
\subnodesIntro
\begin{itemize}
  \item \xmlNode{lowerBound}, \xmlDesc{float, required parameter}, domain lower
  boundary.
  %
  \item \xmlNode{upperBound}, \xmlDesc{float, required parameter}, domain upper
  boundary.
  %
\end{itemize}
\nb Since the Uniform distribution is a rectangular-shaped PDF, the truncation does not have any effect;
 this is the reason why the children nodes are the ones generally used for truncated distributions.
Example:
\begin{lstlisting}[style=XML]
<Distributions>
  ...
  <Uniform name='aUserDefinedName'>
    <lowerBound>aFloatValue</lowerBound>
    <upperBound>aFloatValue</upperBound>
  </Uniform>
  ...
</Distributions>
\end{lstlisting}

%%%%%% Weibull
\paragraph{Weibull Distribution}
\label{Weibull}
The \distname{Weibull} distribution is a continuous distribution that is often
used in the field of failure analysis; in particular, it can mimic distributions
where the failure rate varies over time.
%
If the failure rate is:
\vspace{-5mm}
\begin{itemize}
  \itemsep0em
  \item constant over time, then $k = 1$, suggests that items are failing from
  random events;
  \item decreases over time, then $k < 1$, suggesting ``infant mortality'';
  \item increases over time, then $k > 1$, suggesting ``wear out'' - more likely
  to fail as time goes by.
  %
\end{itemize}
\vspace{-5mm}
Its default support is $x \in [0, +\infty)$.

\specBlock{a}{Weibull}
%
\attrIntro
\vspace{-5mm}
\begin{itemize}
  \itemsep0em
  \item \nameDescription
\end{itemize}
\vspace{-5mm}
\subnodesIntro
\begin{itemize}
  \item \xmlNode{k}, \xmlDesc{float, required parameter}, shape parameter.
  %
  \item \xmlNode{lambda}, \xmlDesc{float, required parameter}, scale parameter.
  \item \xmlNode{low}, \xmlDesc{float, optional parameter}, lower domain
  boundary. \default{0.0}
  %
\end{itemize}

Example:
\begin{lstlisting}[style=XML]
<Distributions>
  ...
  <Weibull name='aUserDefinedName'>
    <lambda>aFloatValue</lambda>
    <k>aFloatValue</k>
    <low>aFloatValue</low>
  </Weibull>
  ...
</Distributions>
\end{lstlisting}

%%%%%% Custom1D
\paragraph{Custom1D Distribution}
\label{Custom1D}
The \distname{Custom1D} distribution is a custom continuous distribution that can be initialized from a dataObject
generated by RAVEN.
This distribution cannot be initialized from a dataObject directly but through a .csv file.
This file must contain the values of either cdf or pdf of the random variable sampled along the range of the desired
random variable.
In the distribution block of the RAVEN input file, the user needs to specify which file (including its working directory)
needs to be used to initialize the distribution. In addition, the user is required to specify which type (cdf or pdf) or values
are contained in the file and also the IDs of both the random variable and cdf/pdf.
Thus the csv file contains a set of points that samples the function $pdf(x)$ or $cdf(x)$ for several values of the stochastic variable $x$.
The user needs to specify which variable IDs correspond to $x$ and $pdf(x)$ (or $cdf(x)$).
The distribution create a fourth order spline interpolation from the provided input points.
%
Note that the support of this distribution is set between the minimum and maximum values of the random variable which are
specified in the distribution input file.

Refer to the test example ($tests/framework/test_distributionCustom1D.xml$) for more clarification.

\specBlock{a}{Custom1D}
%
\attrIntro
\vspace{-5mm}
\begin{itemize}
  \itemsep0em
  \item \nameDescription
\end{itemize}
\vspace{-5mm}
\subnodesIntro
\begin{itemize}
  \item \xmlNode{dataFilename}, \xmlDesc{string, required parameter}, file name to be used to initialize the distribution.
  \item \xmlNode{workingDir}, \xmlDesc{string, optional parameter}, relative working directory that contains the input file.
  \item \xmlNode{functionType}, \xmlDesc{string, required parameter}, type of initialization values specified in the input file (pdf or cdf).
  \item \xmlNode{variableID}, \xmlDesc{string, required parameter}, ID of the variable contained in the input file.
  \item \xmlNode{functionID}, \xmlDesc{string, required parameter}, ID of the function associated to the variableID contained in the input file.
\end{itemize}

Example:
\begin{lstlisting}[style=XML]
<Distributions>
  ...
    <Custom1D name="pdf_custom">
      <dataFilename>PointSetFile2_dump.csv</dataFilename>
      <functionID>pdf_values</functionID>
      <variableID>x</variableID>
      <functionType>pdf</functionType>
      <workingDir>custom1D/</workingDir>
    </Custom1D>
    <Custom1D name="cdf_custom">
      <dataFilename>PointSetFile3_dump.csv</dataFilename>
      <functionID>cdf_values</functionID>
      <variableID>x</variableID>
      <functionType>cdf</functionType>
      <workingDir>custom1D/</workingDir>
    </Custom1D>
  ...
</Distributions>
\end{lstlisting}

The example above initializes two distributions from two .csv files.
For example, the first distribution retrieves the pdf values, located in the column with label $pdf_values$, for several locations of the variable located in the column
with label $x$ in the file $PointSetFile2_dump.csv$.

%%%%%% paragraph 1-Dimensional Discrete Distributions.
\subsubsection{1-Dimensional Discrete Distributions.}
\label{subsubsec:1DDiscrete}
RAVEN currently supports 3 discrete distributions.
%
In the following paragraphs, the input requirements are reported.

%%%%%% Bernoulli
\paragraph{Bernoulli Distribution}
\label{Bernoulli}
The \distname{Bernoulli} distribution is a discrete distribution of the outcome
of a single trial with only two results, 0 (failure) or 1 (success), with a
probability of success \distattrib{p}.
%
It is the simplest building block on which other discrete distributions of
sequences of independent Bernoulli trials can be based.
%
Basically, it is the binomial distribution (k = 1, \distattrib{p}) with only
one trial.
%
Its default support is $k \in {0, 1}$.

\specBlock{a}{Bernoulli}
%
\attrIntro
\vspace{-5mm}
\begin{itemize}
  \itemsep0em
  \item \nameDescription
\end{itemize}
\vspace{-5mm}
\subnodeIntro
\begin{itemize}
  \item \xmlNode{p}, \xmlDesc{float, required parameter}, probability of
  success.
  %
 \end{itemize}
Example:
\begin{lstlisting}[style=XML]
<Distributions>
  ...
  <Bernoulli name='aUserDefinedName'>
    <p>aFloatValue</p>
  </Bernoulli>
  ...
</Distributions>
\end{lstlisting}

%%%%%% Binomial
\paragraph{Binomial Distribution}
\label{Binomial}
The \distname{Binomial} distribution is the discrete probability distribution of
the number of successes in a sequence of \distattrib{n} independent yes/no
experiments, each of which yields success with probability \distattrib{p}.
%
Its default support is $k \in {0, 1, 2, ..., n}$.

\specBlock{a}{Binomial}
%
\attrIntro
\vspace{-5mm}
\begin{itemize}
  \itemsep0em
  \item \nameDescription
\end{itemize}
\vspace{-5mm}
\subnodesIntro
\begin{itemize}
  \item \xmlNode{p}, \xmlDesc{float, required parameter}, probability of
  success.
  %
  \item \xmlNode{n}, \xmlDesc{integer, required parameter}, number of
  experiments.
  %
\end{itemize}

Example:
\begin{lstlisting}[style=XML]
<Distributions>
  ...
  <Binomial name='aUserDefinedName'>
    <n>aIntegerValue</n>
    <p>aFloatValue</p>
  </Binomial>
  ...
</Distributions>
\end{lstlisting}

%%%%%% Geometric
\paragraph{Geometric Distribution}
\label{Geometric}
The \distname{Geometric} distribution is a one-parameter discrete probability distribution.
%
The distribution uses the probability $p$ that trial will be
successful.  The geometric distribution gives the probability of
observing $k$ trials before the first success.
%
Its support is $k \in {0, 1, 2, ..., n}$.

\specBlock{a}{Geometric}

\attrIntro
\vspace{-5mm}
\begin{itemize}
  \itemsep0em
\item \nameDescription
\end{itemize}
\vspace{-5mm}
\subnodesIntro
\begin{itemize}
\item \xmlNode{p}, \xmlDesc{float, required parameter}, the success fraction for the trials.
\end{itemize}

Example:
\begin{lstlisting}[style=XML]
<Distributions>
  ...
  <Geometric name='aUserDefinedName'>
    <p>aFloatValue</p>
  </Geometric>
  ...
</Distributions>
\end{lstlisting}

%%%%%% Poisson
\paragraph{Poisson Distribution}
\label{Poisson}
The \distname{Poisson} distribution is a discrete probability distribution that
expresses the probability of a given number of events occurring in a fixed
interval of time and/or space if these events occur with a known average rate
and independently of the time since the last event.
%
Its default support is $k \in {1, 2, 3, 4, ...}$.

\specBlock{a}{Poisson}
%
\attrIntro
\vspace{-5mm}
\begin{itemize}
  \itemsep0em
  \item \nameDescription
\end{itemize}
\vspace{-5mm}
\subnodeIntro
\begin{itemize}
  \item \xmlNode{mu}, \xmlDesc{float, required parameter}, mean rate of
  events/time.
  %
\end{itemize}

Example:
\begin{lstlisting}[style=XML]
<Distributions>
  ...
  <Poisson name='aUserDefinedName'>
    <mu>aFloatValue</mu>
  </Poisson>
  ...
</Distributions>
\end{lstlisting}

%%%%%% Categorical
\paragraph{Categorical Distribution}
\label{Categorical}
The \distname{Categorical} distribution is a discrete distribution that describes the result of a random variable that can have $K$ possible outcomes.
The probability of each outcome is separately specified.
The possible outcomes must be only numerical values (either integer or float numbers). No string can be assigned to any outcome.
%
There is not necessarily an underlying ordering of these outcomes, but labels are assigned in describing the distribution (in the range $1$ to $K$).
%
\specBlock{a}{Categorical}
%
\attrIntro
\vspace{-5mm}
\begin{itemize}
  \itemsep0em
  \item \nameDescription
\end{itemize}
\vspace{-5mm}
\subnodeIntro
\begin{itemize}
  \item \xmlNode{state}, \xmlDesc{float, required parameter}, probability for outcome 1
  \begin{itemize}
          \item \xmlAttr{outcome}, \xmlDesc{float, required parameter}, outcome value.
  \end{itemize}
  \item \xmlNode{state}, \xmlDesc{float, required parameter}, probability for outcome 2
  \begin{itemize}
          \item \xmlAttr{outcome}, \xmlDesc{float, required parameter}, outcome value.
  \end{itemize}
  \item ...
  \item \xmlNode{state}, \xmlDesc{float, required parameter}, probability for outcome K
  \begin{itemize}
          \item \xmlAttr{outcome}, \xmlDesc{float, required parameter}, outcome value.
  \end{itemize}
 \end{itemize}
Example:
\begin{lstlisting}[style=XML]
<Distributions>
  ...
    <Categorical name='testCategorical'>
        <state outcome="10">0.1</state>
        <state outcome="20">0.2</state>
        <state outcome="50">0.15</state>
        <state outcome="60">0.4</state>
        <state outcome="90">0.15</state>
    </Categorical>
  ...
</Distributions>
\end{lstlisting}

\paragraph{Uniform Discrete Distribution}
\label{subsec:UniformDiscrete}

The \textbf{UniformDiscrete} distribution is a discrete distribution which describes a random variable
that can have $N$ values having equal probability value.
This distribution allows the user to choose two kinds of sampling strategies: with or without replacement.
In case the ``without replacement'' strategy is used, the distribution samples from the set of specified $N$ values
reduced by the previously sampled values.
After, the sampler has generated values for all variables, the distribution is
resetted (i.e., the set of values that can be sampled is returned to $N$).
In case the ``with replacement'' strategy is used, the distribution samples always from the complete set of specified $N$ values.

\specBlock{a}{Uniform Discrete}
%
\attrIntro
\vspace{-5mm}
\begin{itemize}
  \itemsep0em
  \item \nameDescription
\end{itemize}
\vspace{-5mm}
\subnodeIntro
\begin{itemize}
  \item \xmlNode{lowerBound}, \xmlDesc{float, required parameter}, lower bound.
  \item \xmlNode{upperBound}, \xmlDesc{float, required parameter}, upper bound.
  \item \xmlNode{nPoints},    \xmlDesc{integer, optional parameter}, number of points between lower and upper bound
  \item \xmlNode{strategy},   \xmlDesc{string,  required parameter}, type of sampling strategy
  (withReplacement or withoutReplacement).
  %
\end{itemize}

Example:
\begin{lstlisting}[style=XML]
<Distributions>
  ...
    <UniformDiscrete name="UD_dist">
      <lowerBound>3</lowerBound>
      <upperBound>8</upperBound>
      <strategy>orderedWithReplacement</strategy>
    </UniformDiscrete>
  ...
</Distributions>
\end{lstlisting}

\paragraph{Markov Categorical Distribution}
\label{subsec:markovCategorical}

The \textbf{MarkovCategorical} distribution is a specific discrete categorical distribution describes
a random variable that can have $K$ possible outcomes, based on the steady state probabilities provided by
Markov model.
%
\begin{itemize}
  \item \xmlNode{transition}, \xmlDesc{float, optional field}, the transition matrix of given Markov model.
  \item \xmlNode{dataFile}, \xmlDesc{string, optional xml node}. The path for the given data file, i.e. the transition matrix.
    In this node, the following attribute should be specified:
    \begin{itemize}
      \item \xmlAttr{fileType}, \xmlDesc{string, optional field}, the type of given data file, default is `csv'.
    \end{itemize}
  \nb Either \xmlNode{transition} or \xmlNode{dataFile} is required to provide the transition matrix.
  \item \xmlNode{workingDir}, \xmlDesc{string, optional field}, the path of working directory
  \item \xmlNode{state}, \xmlDesc{required xml node}. The output from this state indicates
    the probability for outcome 1.
    In this node, the following attribute should be specified:
    \begin{itemize}
      \item \xmlAttr{outcome}, \xmlDesc{float, required field}, outcome value.
      \item \xmlAttr{index}, \xmlDesc{integer, required field}, the index of steady state probabilities corresponding to the transition matrix.
    \end{itemize}
  \item \xmlNode{state}, \xmlDesc{required xml node}. The output from this state indicates
    the probability for outcome 2.
    In this node, the following attribute should be specified:
    \begin{itemize}
      \item \xmlAttr{outcome}, \xmlDesc{float, required field}, outcome value.
      \item \xmlAttr{index}, \xmlDesc{integer, required field}, the index of steady state probabilities corresponding to the transition matrix.
    \end{itemize}
  \item ...
  \item \xmlNode{state}, \xmlDesc{required xml node}. The output from this state indicates
    the probability for outcome K.
    In this node, the following attribute should be specified:
    \begin{itemize}
      \item \xmlAttr{outcome}, \xmlDesc{float, required field}, outcome value.
      \item \xmlAttr{index}, \xmlDesc{integer, required field}, the index of steady state probabilities corresponding to the transition matrix.
    \end{itemize}

\end{itemize}

\textbf{Example:}

\begin{lstlisting}[style=XML]
<Simulation>
 ...
  <Distributions>
    ...
    <MarkovCategorical name="x_dist">
        <!--dataFile fileType='csv'>transitionFile</dataFile-->
        <transition>
            -1.1   0.8   0.7
            0.8    -1.4  0.2
            0.3    0.6   -0.9
        </transition>
        <state outcome='1' index='1'/>
        <state outcome='2' index='2'/>
        <state outcome='4' index='3'/>
    </MarkovCategorical>
    ...
  </Distributions>
 ...
</Simulation>
\end{lstlisting}

%%%%%% N-Dimensional Probability distributions
\subsection{N-Dimensional Probability Distributions}
\label{subsec:NdDist}
The group of $N$-Dimensional distributions allow the user to model stochastic dependencies between parameters. Thus instead of using $N$ distributions for $N$ parameters, the user can define a single distribution lying in a $N$-Dimensional space.
The following $N$-Dimensional Probability Distributions are available within RAVEN:
\begin{itemize}
\item MultivariateNormal: Multivariate normal distribution (see Section~\ref{MultivariateNormal})
\item NDInverseWeight: ND Inverse Weight interpolation distribution (see Section~\ref{NDInverseWeight})
\item NDCartesianSpline: ND spline interpolation distribution (see Section~\ref{NDCartesianSpline})
\end{itemize}
For NDInverseWeight and NDCartesianSpline distributions, the user provides the sampled values of either CDF or PDF of the distribution. The sampled values can be scattered distributed (for NDInverseWeight) or over a Cartesian grid (for NDCartesianSpline).

The user could specify, for each $N$-Dimensional distribution, the parameters of the random number generator function:
\begin{itemize}
\item \xmlNode{initialGridDisc}, \xmlDesc{positive integer, optional field}, user-defined initial grid discretization. This parameter specifies the number of discretizations that need to be performed, initially, for each Dimension in
order to find N-Dimensional coordinate that corresponds to the CDF represented by a random number (0-1);
\item \xmlNode{tolerance}, \xmlDesc{float, optional field}, user-defined tolerance in order to find the N-D coordinates corresponding to a random number. This tolerance is expressed in terms of CDF.
\end{itemize}
in the \xmlNode{samplerInit} block defined in sampler block \xmlNode{samplerInit} (see Section~\ref{sec:Samplers}).

\subsubsection{MultivariateNormal Distribution}
\label{MultivariateNormal}
the multivariate normal distribution or multivariate Gaussian distribution, is a generalization of the one-dimensional (univariate) normal distribution to higher dimensions.
The multivariate normal distribution is often used to describe, at least approximately, any set of (possibly) correlated real-valued random variables each of which clusters around a mean value.
The multivariate normal distribution of a $k$-dimensional random vector $\mathbf{x} = [x_1, x_2, …, x_k]$  can be written in the following notation:
$ \mathbf{x}\ \sim\ \mathcal{N}(\boldsymbol\mu,\, \boldsymbol\Sigma)$
with with $k$-dimensional mean vector

$\boldsymbol\mu= [E[x_1], E[x_2], …, E[x_k]]$

and $k \times k$ covariance matrix

$\boldsymbol\Sigma = [Cov[x_i,x_j]] , i=1,2,\ldots,k ; j=1,2,\ldots,k$

The probability distribution function for this distribution is the following:

$
f_{\mathbf x}(x_1,\ldots,x_k) =
\frac{1}{\sqrt{(2\pi)^k|\boldsymbol\Sigma|}}
\exp\left(-\frac{1}{2}({\mathbf x}-{\boldsymbol\mu})^\mathrm{T}{\boldsymbol\Sigma}^{-1}({\mathbf x}-{\boldsymbol\mu})
\right),
$

The specifications of this distribution must be defined within the xml block \xmlNode{MultivariateNormal}.
This XML node needs to contain the attributes:
\vspace{-5mm}
\begin{itemize}
\itemsep0em
\item \xmlAttr{name}, \xmlDesc{required string attribute}, user-defined identifier of this multivariate normal distribution.
%
\nb As with other objects, this is the name that can be used to refer to this specific entity from other input XML blocks.
\item \xmlAttr{method}, \xmlDesc{required string attribute}, defines which method is used to generate the multivariate normal distribution.
The only allowable methods are \xmlString{spline} and \xmlString{pca}.
%
\end{itemize}
\vspace{-5mm}

In RAVEN the MultivariateNormal distribution can be initialized through the following keywords:
\begin{itemize}
  \item \xmlNode{mu}, list of mean values of each dimension
  \item \xmlNode{covariance}, list of element values in the covariance matrix. There are two types of \xmlNode{covariance}, based on the \xmlAttr{type}:
  \begin{itemize}
    \item \xmlAttr{type}, \xmlDesc{string, optional field}, specifies the type of covariance, the default \xmlAttr{type} is \xmlString{abs}. Possible values for \xmlAttr{type} are \xmlString{abs} and \xmlString{rel}. \nb \xmlString{abs} indicates the covariance is a normal covariance matrix, while \xmlString{rel} indicates the covariance is a relative covariance matrix. In addition, method \xmlString{pca} can be combined with both types, and method \xmlString{spline} only accept the type \xmlString{abs}
  \end{itemize}
  \item \xmlNode{transformation}, \xmlDesc{XML node, optional field}, option to enable input parameter transformation using principal component analysis (PCA) approach. If this node is provided, PCA will be used to compute the principal components of input covariance matrix. The subnode \xmlNode{rank} is used to indicate the number of principal components that will be used for the input transformation. The content will specify one attribute:
  \begin{itemize}
    \item \xmlNode{rank}, \xmlDesc{positive integer, required field}, user-defined dimensionality reduction.
  \end{itemize}
\end{itemize}

Example:
\begin{lstlisting}[style=XML]
<Distributions>
  ...
    <MultivariateNormal name='MultivariateNormal_test' method='spline'>
        <mu>0.0 60.0</mu>
        <covariance>
	1.0 0.7
	0.7 1.0
        </covariance>
    </MultivariateNormal>
    <MultivariateNormal name='MultivariateNormal_abs' method='pca'>
        <mu>0.0 60.0</mu>
        <covariance type='abs'>
	1.0 0.7
	0.7 1.0
        </covariance>
    </MultivariateNormal>
    <MultivariateNormal name='MultivariateNormal_rel' method='pca'>
        <mu>0.0 60.0</mu>
        <covariance type='rel'>
	1.0 0.7
	0.7 1.0
        </covariance>
    </MultivariateNormal>
  ...
</Distributions>
\end{lstlisting}

In the following, we defined a distribution with a transformation node using PCA method. The number of principal components is
defined in \xmlNode{rank}. In this distribution, PCA is employed to restructure the multivariate normal distribution. In addition,
the size of uncorrelated variables is also determined by \xmlNode{rank}.

\begin{lstlisting}[style=XML]
<Distributions>
  ...
    <MultivariateNormal name='MultivariateNormal_test' method='pca'>
        <mu>0.0 10.0 20.0</mu>
        <covariance type="abs">
           1.0   0.7   -0.2
           0.7   1.0   0.4
           -0.2  0.4   1.0
        </covariance>
        <transformation>
          <rank>2</rank>
        </transformation>
    </MultivariateNormal>
  ...
</Distributions>
\end{lstlisting}

\subsubsection{NDInverseWeight Distribution}
\label{NDInverseWeight}
The NDInverseWeight distribution creates a $N$-Dimensional distribution given a set of points
scattered distributed. These points sample the PDF of the original distribution.
Distribution values (PDF or CDF) are calculated using the inverse weight
interpolation scheme.

\specBlock{a}{NDInverseWeight}
%
\attrsIntro
\vspace{-5mm}
\begin{itemize}
\itemsep0em
\item \nameDescription
\end{itemize}
\vspace{-5mm}

In RAVEN the NDInverseWeight distribution can be initialized through the following nodes:
\begin{itemize}
\item \xmlNode{p}, \xmlDesc{float, required parameter}, power parameter. Greater values of p assign greater influence to values closest to the interpolated point.
\item \xmlNode{data\textunderscore filename}, \xmlDesc{string, required parameter},  name of the data file containing scattered values (file type '.txt').
\begin{itemize}
\item \xmlAttr{type}, \xmlDesc{required string attribute},  indicates if the data in indicated file is PDF or CDF.
\end{itemize}
\item \xmlNode{working\textunderscore dir}, \xmlDesc{string, required parameter}, folder location of the data file
\end{itemize}

Example:
\begin{lstlisting}[style=XML]
<Distributions>
  ...
  <NDInverseWeight name='...'>
        <p>...</p>
        <dataFilename type='...'>...</dataFilename>
        <workingDir>...</workingDir>
  </NDInverseWeight>
  ...
</Distributions>
\end{lstlisting}

Each data entry contained in data\textunderscore filename is listed row by row and must be listed as follows:
\begin{itemize}
\item number of dimensions
\item number of sampled points
\item ND coordinate of each sampled point
\item value of each sampled point
\end{itemize}

As an example, the following shows the data entries contained in data\textunderscore filename for a 3-dimensional data set that contained two sampled CDF values: ([0.0,0.0,0.0], 0.1) and ([1.0, 1.0,0.0], 0.8)

Example scattered data file:
\begin{lstlisting}
3
2
0.0
0.0
0.0
1.0
1.0
0.0
0.1
0.8
\end{lstlisting}

\subsubsection{NDCartesianSpline Distribution}
\label{NDCartesianSpline}

The NDCartesianSpline distribution creates a $N$-Dimensional distribution given a set of points
regularly distributed on a Cartesian grid. These points sample the PDF of the original distribution.
Distribution values (PDF or CDF) are calculated using the ND spline
interpolation scheme.

\specBlock{a}{NDCartesianSpline}
%
\attrsIntro
\vspace{-5mm}
\begin{itemize}
\itemsep0em
\item \nameDescription
\end{itemize}
\vspace{-5mm}

In RAVEN the NDCartesianSpline distribution can be initialized through the following nodes:
\begin{itemize}
\item \xmlNode{data\textunderscore filename}, \xmlDesc{string, required parameter},  name of the data file containing scattered values (file type '.txt').
\begin{itemize}
\item \xmlAttr{type}, \xmlDesc{required string attribute}, indicates if the data in indicated file is PDF or CDF.
\end{itemize}
\item \xmlNode{working\textunderscore dir}, \xmlDesc{string, required parameter}, folder location of the data file
\end{itemize}

Example:
\begin{lstlisting}[style=XML]
<Distributions>
  ...
  <NDCartesianSpline name='...'>
        <dataFilename type='...'>...</dataFilename>
        <workingDir></workingDir>
  </NDCartesianSpline>
  ...
</Distributions>
\end{lstlisting}

Each data entry contained in data \textunderscore filename is listed row by row and must be listed as follows:
\begin{itemize}
\item number of dimensions
\item number of discretization for each dimension
\item discretization values for each dimension
\item value of each sampled point
\end{itemize}

As an example, the following shows the data entries contained in data \textunderscore filename for a 2-dimensional CDF data set on the following grid $(x,y)$:
\begin{itemize}
\item first dimension (x): -0.5, 0.5
\item first dimension (y): 1.0 2.0 3.0
\end{itemize}

Example scattered data file:
\begin{lstlisting}
2
2
3
-0.5
0.5
1.0
2.0
3.0
CDF value of (-0.5,1.0)
CDF value of (+0.5,1.0)
CDF value of (-0.5,2.0)
CDF value of (+0.5,2.0)
CDF value of (-0.5,3.0)
CDF value of (+0.5,3.0)
\end{lstlisting}


\subsection{Influence of the topology}

\begin{figure}
  \includegraphics[width=\columnwidth]{figure-topology-influence.pdf}
  %
  \caption{%
  %
  \textbf{Influence of topology on the self organization.}
  %
  The same initial set of 1024 neurons has been equiped with 2-nearest neighbors, 3 nearest neighbors and 4-nearest neighbors induced topology (panels \textbf{A}, \textbf{B} and \textbf{C} respectively) and trained on 25,000 random RGB colors. This lead to qualitatively different self-organization as shown on panels \textbf{D}, \textbf{E} and \textbf{F} respectively, with major discontinuities in the 2-nearest neighbors case. ).
  %
  }
  %
  \label{fig:topology-influence}

\end{figure}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% UMB-CS110-2015S: Introduction to Computing
% Copyright 2015 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/UMB-CS110-2015S
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def \topDirectory {.}
\def \texDirectory {\topDirectory/src/main/tex}

\documentclass[12pt,letterpaper,twoside]{article}
\usepackage{\texDirectory/template/style/directives}
\usepackage{\texDirectory/template/style/assignment}
\input{\texDirectory/template/config}

\begin{document}

\doc{title}{Midterm Exam}
\doc{points}{20}

\prepare{header}

\section*{Question 1}

The following code snippets either do not compile or do not run as expected. There are five \textbf{distinct} problems in each code snippet. You are expected to find and fix all errors so that given command-line arguments will lead to expected output as indicated.
\begin{enumerate}[label=\textbf{(\alph*)}]
\item Execution Script \hfill Expected Output\\
\texttt{java HelloWorld President} \hfill \texttt{Hello President!}
\begin{lstlisting}
public class HelloWorld {
	public static void main(String args) {
		System.out.printn("Hello" + args[0]!)
	}
}
\end{lstlisting}

\item Execution Script \hfill Expected Output\\
\texttt{java Divisible 15 4} \hfill \texttt{15 not divisible by 4}

\begin{lstlisting}
public class Divisible.java {
	public static void main(String args) {
		int b;
		int a = args[0];
		b = args[1];
		if (a % b = 0) {
			System.out.println(a + " divisible by " + b);
		}
		elseif {
			System.out.print(a + " not divisible by " + b);
		}
	}
}
\end{lstlisting}

\item Execution Script \hfill Expected Output\\
\texttt{java Quadratic 1 -2 -4} \hfill \texttt{-1.236 and 3.237}

\begin{lstlisting}
public class Quadratic {
	public static void main(String[] args) {
		int a = Integer.parseInt(args[0]);
		int b = Integer.parseInt(args[1]);
		int c = Integer.parseInt(args[2]);
		double discriminant = b^2 - 4*a*c;
		if (double discriminant > 0) {
			double 1sol = (- b + sqrt(discriminant))/(2*a);
			double 2sol = (- b - sqrt(discriminant))/(2*a);
			System.out.printf("%f and %f\n", sol1, sol2);
		}
	}
}
\end{lstlisting}

\item Execution Script \hfill Expected Output\\
\texttt{java IdentityMatrix 2} \hfill \texttt{1 0}\\ \raggedleft \texttt{0 1}

\begin{lstlisting}
public class IdentityMatrix {
	public static void main(String[] args) {
		int i, j;
		int row = Integer.parseInt(args[0]);
		int[][] matrix = int[row][row];
		for (i = 1; i < row; i++) {
			matrix[i,i] = 1;
		}
		for (i = 1; i < row; i++) {
			for (j = 1; j < row; j++) {
				System.out.print(matrix[i,j] + " ");
			}
		}
	}
}
\end{lstlisting}
\newpage
\item Execution Script \hfill Expected Output\\
\texttt{java Factorial 5} \hfill \texttt{5! is 120}

\begin{lstlisting}
public class Factorial {
	public static void main(String[] args) {
		long product = 1;
		long a = Long.parseLong(args[0]);
		for (i = a: i >= 0: i--) {
			product *= a;
		}
		System.out.println(a! + " is " + product );
	}
}
\end{lstlisting}

\end{enumerate}

\section*{Question 2}

Determine the output of the following code snippet and support your answer by explaining how the program works. No assumption about user input can be made except that the input is an integer.

\begin{enumerate}[label=\textbf{(\alph*)}]
\item Execution Script \hfill \texttt{java CoolArray}

\begin{lstlisting}
import java.util.Scanner;
public class CoolArray {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter array size: ");
		int size = input.nextInt();
		input.close();
		double array[] = new double[2*size];
		for (int i = 0; i < size; i++) {
			int num = (int) (Math.random() * 10);
			array[i] = num;
			array[2*size - i - 1] = 10 - num;
		}
		double sum = 0;
		for (int i = 0; i < 2*size; i++) {
			sum += array[i];
		}
		System.out.println(sum/size/2);
		System.out.println();
	}
}

\end{lstlisting}

\end{enumerate}

\section*{Question 3}

Write a program \texttt{GPAcalculator.java} that asks a student for \texttt{N} grades - where \texttt{N} is given by student - and number of credits corresponding to each and prints his GPA based on entered information. You are expected to use Class Scanner to get input from user.

\prepare{footer}

\end{document}


\lab{Applications}{Optimization}{Transportation}
\label{lab:Transportation}
\objective{.}

\section*{Transportation}

The following data is from the Seattle Transportation Traffic Management Division.

The graph represents a section of one way roads in Seattle, with the exception of Third street going both east and west.

We can model the flow of traffic using a system of equations. For example, if the rate of flow into an intersection is $x$ and $20$ and the flow out of the same intersection is $y$ and $45$, then the flow through the intersection can be represented by $x-y = 25$.

Create the system of equations that models these streets.

Now solve the system using linalg.lsts(). %Part 1a
\begin{lstlisting}[style=python]
\end{lstlisting}

Was this the kind of answer you expected? How can you have a negative number of cars? Since this is a closed system, the number of cars leaving is the same entering, we'll get infinitely many answers.
This explains the negative entries of x.
We can get further information by measuring the number of cars passing through each independent street in this system. Find the rank of the matrix to know how many streets we should be looking for and identify which streets we need to place counters on.

The counters revealed the following data.

\begin{table}
 $x_4$ =& 2303\\
 $x_9$ =& 5302\\
 $x_{10}$ =& 13412\\
 $x_{15}$ =& 3052\\
 $x_{16}$ =& 4270\\
 $x_{17}$ =& 1243\\
\end{table}

Solve the system with these parameters.

So we've determined the amount of traffic on each road. Interpreting, we have that $9969$ cars travel east from Pike to Union along Fifth Street.

We can now identify the intersections with significant traffic in one direction and less traffic in the other.

Intersection A has lots of cars travelling east. $18144$ cars approach from the west and and $9972$ leave from the east. Comparatively few cars travel north, with $2303$ cars approaching the intersection from the south. This indicates intersection A would a good candidate for a light. Intersection E would also be a good choice.

Which intersections would you recommend putting lights in first?

%Part 2
Now we want to find the fastest path through from A to L. This can be represented by a a Minimum Cost Linear Program for this network flow.
We'll define $c_i$ to be the cost of travelling along road $x_i$. The cost will represent the amount of traffic, the numbers you found in Part 1.

$A$, an $m\times n$ matrix, is called the arc-node incidence matrix where $m$ is the number of nodes and $n$ is the number of arcs. In our case, there are $12$ intersections and $17$ streets. $A$ is determined by the formula
\begin{displaymath}
   f(x) = \left\{
     \begin{array}{lr}
       1 & : arc j starts at node i\\
       -1 & : arc j ends at node i\\
       0 & : else
     \end{array}
   \right.
\end{displaymath}

Notice that each column must sum to $0$.
Arc 1, or $x_1$, starts at node, intersection, $A$ and ends at node $B$. So the first column of $A$ is $[1,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]^T$.

$b$ is the external supply to the system. So $b$ is the same as in part 1, but be sure to remove the independent numbers we found in part 1. Unlike other optimization problems, $Ax=b$.

look at network flow python packages?


\subsection{Comparison of Accuracy vs Order}

It would be interesting to investigate whether the accuracy changes depending on
the transfer function's order $n$.

For each  order $n$ 100 random PTn transfer functions $G_n(s)$ were generated in
the form of:

\begin{equation}
    G(s) = \prod_{k=1}^n \frac{1}{s+T_k}
    \label{eq:random_ptn}
\end{equation}

where $n$ was a random integer in  the  range of $[2..8]$ and $T_k$ was a random
number in the range of $[1..9]$.

The step response of each $G_n(s)$  was fed into each method. The step responses
of each resulting transfer  function  was  then  compared  to the original input
function's step  response  and the root mean square error (RMSE) was calculated.
The  100 errors were then averaged, resulting in a final error  value  for  each
method, for each order.

\begin{figure}
    \includegraphics[width=\linewidth]{images/error_order}
    \caption{The mean square error (MSE) of each method to a randomly generated step response, in function of filter order}
    \label{fig:error_order}
\end{figure}
\begin{figure}
    \includegraphics[width=\linewidth]{images/sani_interpolation_vs_lookup}
    \caption{Interpolation formulae proposed by L. Sani compared to ``brute force'' calculating the individual t10, t50, t90 parameters and creating lookup curves}
    \label{fig:interpolation_vs_lookup}
\end{figure}

The data portrayed in figure \ref{fig:error_order} shows these errors.

\subsubsection*{Conclusions}

As expected, the fitted methods yield  the  most  accurate results. The Sani fit
appears to be better than the Hudzovic fit.

The  most  accurate  non-fitted  method  is  Hudzovic's   transfer  function  in
combination with Sani's characterisation (orange curve).

An interesting observation is that the method proposed by L. Sani\cite{ref:sani}
using the $t_{10}$, $t_{50}$,  $t_{90}$ characterisation (purple curve) performs
worse and worse the higher the order, whereas all  other  methods perform better
and  better.  The exact reason as to why this happens is a consequence of  using
the    interpolation     formulae     proposed     by    L.    Sani    (equation
\ref{eq:sani_interpolation}) rather than using lookup curves.

To  see  if  this  is indeed the cause, the MATLAB code was adapted such that L.
Sani's  method  for determining $r$, $T$, $n$ based on the input data  $t_{10}$,
$t_{50}$, $t_{90}$ used  a  lookup  curve  instead  of  using  the interpolation
formulae. The same simulation was performed again with the modified code.

The result can be seen in figure \ref{fig:interpolation_vs_lookup}.  The  purple
line is the same  purple  curve  from  figure  \ref{fig:error_order}. The dashed
purple line shows the result of the modification.

It appears that by using lookup curves the resulting  transfer  function is less
accurate than if interpolation formulae were used.