% !TeX spellcheck = en_GB
% !TeX root = memoco-report.tex

\section{Tests and results}
\label{sec:results}
The described heuristic has been tested on instances generated as described in \cref{sec:datasetgen}, and its solution has been compared with the optimal solution obtained with the exact method. This has been done for instances of size up to 110, since the exact method employed too much time to solve bigger problems. In addition the same tests have been performed on some symmetric drilling problems from TSPLIB, namely \texttt{d198}, \texttt{d493} and \texttt{d657}, with respect to the optimal solutions listed at \cite{Tsplibsol}.
The parameters were set with the following values:
\begin{itemize}
	\setlength\itemsep{0.03em}
	\item K = 400 (so unbounded for size 10-110)
	\item I = 1000
	\item \texttt{max\_neighbours = 5}
	\item \texttt{backtracking\_threshold = 3}
	\item \texttt{intens\_min\_depth = 5}
	\item \texttt{intens\_n\_tours = 50}
\end{itemize}
Parameter \texttt{backtracking\_threshold} and \texttt{intens\_n\_tours} were set to a compromise between the best values found during calibration, and \texttt{intens\_min\_depth} was set to 5, since this value seemed to work best with size 90. Finally \texttt{max\_neighbours} was set to 5 because it resulted in a lower average error in both calibrations.

\subsection{Tests on synthetic dataset}
For each problem size to test, one instance was generated, and the heuristic was run on it first with 5 random restarts, then with 15 and 30. It was run 10 times for each different parameter, and the mean, standard deviation, minimum and maximum value were computed for both execution times and relative percentage error with respect to the optimal value. Tables \ref{tab:t10} - \ref{tab:t110} contains the results for some problems. Figure \ref{fig:path110} shows the heuristic solution for the bigger problem. Plots in figures \ref{fig:ploterr} and \ref{fig:plotime} summarise the results for tests with 15 restarts. In addition figure \ref{fig:logtime} compares the execution times of the two methods. Here the heuristic time is the arithmetic mean of the 10 executions with 15 restarts, while the exact method was run once for every problem. Finally, \cref{tab:cplextime} lists the average execution time of the exact method over 5 executions on different instances. Figure \ref{fig:timelinear} shows a comparison between the average execution times of the two methods (not scaled).

\input{results-tables}

\begin{figure}[H]
	\centering
	\includegraphics[width=12cm]{path_110}
	\caption{Heuristic solution of problem with size 110 (optimal)}
	\label{fig:path110}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=0.9\textwidth]{error_plot}
	\caption{Error statistics over 10 runs with 15 restarts}
	\label{fig:ploterr}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=0.9\textwidth]{time_plot}
	\caption{Execution time statistics over 10 runs with 15 restarts}
	\label{fig:plotime}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=.9\textwidth]{logtime_plot}
	\caption{Comparison of execution time of both methods ($\times$ 1000, logarithmic scale)}
	\label{fig:logtime}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=.9\textwidth]{timemeans}
	\caption[Mean execution time of both methods]{Mean execution time of both algorithms. The heuristic average is computed with 15 restarts and 10 iterations on the same instance, while the CPLEX time is averaged over 5 iterations on different instances}
	\label{fig:timelinear}
\end{figure}

\subsection{Tests on bigger instances}
Table \ref{tab:tsplibr} lists the results of testing the heuristic method with some bigger instances taken from TSPLIB. Extensive tests would require much time, so these results come from a single execution of the heuristic with the same parameters used for previous tests, but with 30 restarts instead of 15. For problem \texttt{d657} parameter \texttt{backtracking\_threshold} was lowered to 2 to reduce execution time. The produced solution paths are shown in figures \ref{fig:p198}, \ref{fig:p493} and \ref{fig:p657}.

\begin{table}[h]
	\caption{Results on TSPLIB problems}
	\label{tab:tsplibr}
	\centering
	\begin{tabular}[t]{cccccc}
		\rowcolor[HTML]{EFEFEF}
		\textbf{Problem} & \textbf{Size} & \textbf{Optimal value} & \textbf{Heuristic value} & \textbf{Relative error (\%)} & \textbf{Time (s)} \\
		d198    & 198 & 15780 & 15852.9 & 0.462  & 266   \\
		d493    & 493 & 35002 & 35428.1 & 1.217 & 3638	\\
		d657 *	& 657 & 48912 & 49892.1 & 2.004	&  2032 \\
		\multicolumn{6}{l}{\rule{0pt}{4ex}*\texttt{backtracking\_threshold = 2}}
	\end{tabular}
\end{table}

\begin{figure}[H]
	\centering
	\includegraphics[width=12cm]{path_198}
	\caption{Heuristic solution for d198}
	\label{fig:p198}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=12cm]{path_493}
	\caption{Heuristic solution for d493}
	\label{fig:p493}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=12cm]{path_657}
	\caption{Heuristic solution for d657}
	\label{fig:p657}
\end{figure}


\documentclass[11pt,a4paper,fleqn]{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[colorlinks=true, citecolor=blue, linkcolor=blue, filecolor=blue,urlcolor=blue]{hyperref}
\hypersetup{
     colorlinks   = true,
     citecolor    = gray
}
\usepackage{wrapfig}

\usepackage{caption}
\captionsetup{format=plain, indent=5pt, font=footnotesize, labelfont=bf}

\setkomafont{disposition}{\scshape\bfseries}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{bbm}
\usepackage{mathtools}
\usepackage{fontawesome}
% \usepackage{epsfig}
% \usepackage{grffile}
%\usepackage{times}
%\usepackage{babel}
\usepackage{tikz}
\usepackage{paralist}
\usepackage{color}
\usepackage[top=3cm, bottom=2.5cm, left=2.5cm, right=3cm]{geometry}
%\setlength{\mathindent}{1ex}

% PGF
\usepackage{pgfplots}
\usepackage{pgf}
\usepackage{siunitx}
\usepackage{xfrac}
\usepackage{calculator}
\usepackage{calculus}
\usepackage{eurosym}
\usepackage{booktabs}
%\sisetup{per-mode=fraction,%
%	fraction-function=\sfrac}

%\newcommand{\eur}[1]{\EUR{#1}\si{\per\kilo\meter}}
\pgfplotsset{
  compat=newest,
  every axis/.append style={small, minor tick num=3}
}

%\usepackage[backend=biber,style=alphabetic,url=false,doi=false]{biblatex}
%\addbibresource{sheet01_biber.bib}
% \addbibresource{/home/coroa/papers/refs.bib}

\newcommand{\id}{\mathbbm{1}}
\newcommand{\NN}{{\mathbbm{N}}}
\newcommand{\ZZ}{{\mathbbm{Z}}}
\newcommand{\RR}{{\mathbbm{R}}}
\newcommand{\CC}{{\mathbbm{C}}}
\renewcommand{\vec}[1]{{\boldsymbol{#1}}}

\renewcommand{\i}{\mathrm{i}}

\newcommand{\expect}[1]{\langle\,#1\,\rangle}
\newcommand{\e}[1]{\ensuremath{\,\mathrm{#1}}}

\renewcommand{\O}{\mc{O}}
\newcommand{\veps}{\varepsilon}
\newcommand{\ud}[1]{\textup{d}#1\,}

\newcommand{\unclear}[1]{\color{green}#1}
\newcommand{\problem}[1]{\color{red}#1}
\newcommand{\rd}[1]{\num[round-mode=places,round-precision=1]{#1}}

%\DeclareSIUnit{\euro}{\EUR}
\DeclareSIUnit{\dollar}{\$}
\newcommand{\eur}{\text{\EUR{}}}

\usepackage{palatino}
\usepackage{mathpazo}
\setlength\parindent{0pt}
\usepackage{xcolor}
\usepackage{framed}
\definecolor{shadecolor}{rgb}{.9,.9,.9}

%=====================================================================
%=====================================================================
\begin{document}

\begin{flushright}
 \textbf{Energy System Modelling }\\
 {\small Karlsruhe Institute of Technology}\\
 {\small Institute for Automation and Applied Informatics}\\
 {\small Summer Term 2020}\\
\end{flushright}

 \vspace{-0.5em}
 \hrulefill
 \vspace{0.3em}

\begin{center}
 \textbf{\textsc{\Large Tutorial III: Storage Optimisation}}\\
 \small Will be worked on in the exercise session on Friday, 19 June 2020.\\[1.5em]
\end{center}

\vspace{-0.5em}
\hrulefill
\vspace{0.8em}

%=============== ======================================================
\paragraph{Problem III.1 (analytical) -- storage optimisation without losses \faHome}~\\
%=====================================================================

\begin{wrapfigure}[11]{r}{0pt}
 \begin{tikzpicture}
  \begin{axis}[
    domain=0:14, no markers, samples=200
    % xlabel = $x$, ylabel = $f(x)$
   ]
   \addplot+[dashed] {0.3 * (1 + 0.9 * sin(deg(2*pi*x/7)))}; \label{figref:w}
   \addplot+[densely dotted] {0.12*(1 + sin(deg(2*pi*x))}; \label{figref:s}
   \addplot+[solid] {1}; \label{figref:l}
  \end{axis}
 \end{tikzpicture}
 \caption{daily and multi-week variations of wind and solar power generation
  \(g^{N}_{w}(t)\)
  \autoref{figref:w} and \(g^{S}_{s}(t)\)
  \autoref{figref:s}, and a constant load (all in per-unit) \(L(t)\)
  \autoref{figref:l}.}
 \label{fig:variations}
\end{wrapfigure}

Imagine a two-node Germany. The South can install solar panels with a capacity factor $c_s$ to cover its load $L^S$, while the North uses wind turbines that have a capacity factor $c_w$
to feed their load $L^N$. Figure \ref{fig:variations} shows approximations to the daily and multi-week variations of per-unit wind and solar power generation \(G^{N}_{w}(t)\) and \(G^{S}_{s}(t)\) and a constant load \(L^{N/S}(t)\):

\vspace{-0.5em}

\begin{align*}
 g_{w}^N(t) & = c_w(1+A_w \sin \omega_w t), \\
 g_{s}^S(t) & = c_s(1+A_s \sin \omega_s t), \\
 L^{N/S}(t) & = A_{l}^{N/S}.
\end{align*}

The capacity factors and constants are

\begin{align*}
 A_{l}^{N} & = 20 \si{\giga\watt}, & A_{l}^{S} & = 30 \si{\giga\watt},                                         \\
 c_w    & = 0.3,                & A_w     & = 0.9,                & \omega_w & = \frac{2\pi}{7 \text{d}}, \\
 c_s    & = 0.12,               & A_s     & = 1.0,                & \omega_s & = \frac{2\pi}{1 \text{d}}.
\end{align*}

For now, assume no power exchange between the regions and that the stores are lossless.

\begin{enumerate}[(a)]
 % (a)
 \item How much wind capacity $G^{N}_{w}$ must be installed in the North and solar capacity $G_s^S$ in the South so that on average generation matches demand?

       % (b)
 \item For a system to work, generation must match demand not on average but at each location and each point in time. Storages are one way of ensuring this constraint with variable renewable generation. What is the amount of store and dispatch power capacity $G_{st,store}=\max(-\Delta(t))$ and $G_{st,dispatch} = \max \Delta(t)$ the storage units must have in the North and in the South to account for the mismatch $\Delta(t)=L(t)-G\cdot g(t)$?
\newpage
       % (c)
 \item What is the amount of energy capacity $E_{st}$ one needs for either storage in the North and in the South? The energy capacity is given by

       \begin{equation*}
        E_{st} = \max_t e_{st}(t) = \max_t \int_{0}^{t} -\Delta(t') \;\mathrm{d}t'
       \end{equation*}

       % (d)
 \item Should they choose hydrogen or battery storage? And how much would it cost them with the prices in Table 1? Is the South or the North paying more for their energy? Disregard losses!

\end{enumerate}
Now we lift the restriction against transmission and allow the two regions to bridge their 500 km separation with a transmission line.

\begin{enumerate}[(e)]
	% (e)
	\item   Estimate the cost-optimal technology mix by assuming wind energy in the North is only stored in the North and solar energy in the South is likewise only stored in the South! What would happen if you dropped that assumption?
\end{enumerate}
\begin{enumerate}[(f)]
	 % (f)
	\item What do you imagine would change if you considered the storage losses given in Table 1 in your results (a)-(d)?
\end{enumerate}

\begin{table}[h]
 \centering
 \label{tab:prices}
 \begin{tabular}{@{}lrrrrr@{}}
  \toprule
                             & \eur / kW & \eur / kWkm & \eur / kWh & $\eta_{store}$ & $\eta_{dispatch}$ \\ \midrule
  Battery                    & 300         & & 200          & 0.9            & 0.9               \\
  Hydrogen                   & 750         & & 10           & 0.75           & 0.405              \\
  Solar                      & 600         & &              &                &                   \\
  Wind                       & 1200        & &              &                &                   \\
  Transmission line &          & 0.4 &              &                &                   \\ \bottomrule
 \end{tabular}
 \caption{Investment costs of renewable energy infrastructure.}
\end{table}

\newpage
%=============== ======================================================
\paragraph{Problem III.2 (programming) -- storage optimization with PyPSA and losses \faGroup}~\\
%=====================================================================

Python for Power System Analysis (PyPSA) is a free software toolbox for optimising modern power systems that include features such as variable wind and solar generation, storage units, etc\.. Use the toolbox to extend on your findings in Problem III.1.

To get started, you should have a look at the minimal LOPF example\footnote{\url{https://www.pypsa.org/examples/minimal_example_lopf.html}}, understand  the components documentation\footnote{\url{https://pypsa.org/doc/components.html}} of PyPSA as well as the underlying objective function and constraints in the LOPF documentation\footnote{\url{https://pypsa.org/doc/optimal_power_flow.html\#linear-optimal-power-flow}}.

\begin{enumerate}[(a)]
 \item Build a network in PyPSA with the two buses North and South and attach the load at each bus and attach the wind and solar generators with availability according to $$g^{N}_{w}(t) = c_w(1+A_w\sin \omega_w t)$$ $$g^{S}_{s}(t) = c_s(1+A_s\sin \omega_s t)$$ for a year and with \texttt{p\_nom\_extendable=True}.
 \item Attach extendable storage units at the North and the South. The storage units have to be modelled as a \texttt{H2-bus} (a bus with \texttt{carrier='H2'}) linked to the \texttt{AC-bus} North via a \texttt{Link} where \texttt{p\_nom\_extendable=True} with the \texttt{capital\_cost} of the power capacity and an also extendable \texttt{Store} with the \texttt{capital\_cost} of the energy capacity, for instance. The losses can be set on the links as \texttt{efficiency}.
 \item Run an investment optimization by calling the \texttt{lopf} function.
 \item How do your results \texttt{objective} and \texttt{{generators,stores,links}.p\_nom\_opt} compare with the results of III.1(d) in terms of the total system costs and the capacities of the generators, stores and links?
 \item Now again lift the restriction against transmission and allow North and South to bridge their 500 km separation by adding a transmission line to the network. How does the cost optimal technology mix change compared to III.1(f)?
 \item Replace the approximated availability time-series of the wind and the solar generators with more realistic data provided in \texttt{availability.csv} computed from reanalysis weather data and re-run the linear optimal power flow. Compare the results! Explain the differences by looking at the cumulative variations relative to the mean of the availability time-series!
\end{enumerate}
\end{document}


\documentclass[12pt,reqno]{article}

\usepackage[usenames]{color}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{amscd}

\usepackage{amsthm}
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{conjecture}[theorem]{Conjecture}

\theoremstyle{definition}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{example}[theorem]{Example}
\newtheorem{xca}[theorem]{Exercise}

\usepackage[colorlinks=true,
linkcolor=webgreen, filecolor=webbrown,
citecolor=webgreen]{hyperref}

\definecolor{webgreen}{rgb}{0,.5,0}
\definecolor{webbrown}{rgb}{.6,0,0}

\usepackage{color}

\usepackage{float}

\usepackage{graphics,amsmath,amssymb}
\usepackage{amsfonts}
\usepackage{latexsym}
\usepackage{epsf}

\setlength{\textwidth}{6.5in} \setlength{\oddsidemargin}{.1in}
\setlength{\evensidemargin}{.1in} \setlength{\topmargin}{-.5in}
\setlength{\textheight}{8.9in}

\newcommand{\seqnum}[1]{\href{http://oeis.org/#1}{\underline{#1}}}

\usepackage{gb4e}

\begin{document}

\begin{center}
\vskip 1cm{\LARGE\bf  On sequences with $\{-1,0,1\}$ Hankel transforms}  \vskip 1cm
\large
Paul Barry\\
School of Science\\
Waterford Institute of Technology\\
Ireland\\

\end{center}
\vskip .2 in

\begin{abstract} We study Hankel transforms of sequences, where the transform elements are members of the set $\{-1,0,1\}$. We relate these Hankel transforms to special continued fraction expansions. In particular, we posit a conjecture relating the distribution of non-zero terms in the Hankel transform to the distribution of powers of the variable in the defining continued fractions.
\end{abstract}

\section{Introduction} Given a sequence $a_n$, we denote by $h_n$ the general term of the sequence
with $h_n=|a_{i+j}|_{0 \le i,j \le n}$. The sequence $h_n$ is called the Hankel transform of $a_n$ \cite{Layman}.
For a given sequence $a_n$, it can be shown that the sequence
$$\sum_{k=0}^n \binom{n}{k} r^{n-k} a_k$$ will also have the same Hankel transform. Similarly, if
the sequence $a_n$ has generating function $f(x)$, where $f(0)\ne 0$,  then the sequence with generating function
$\frac{f(x)}{1-rxf(x)}$ will also have same Hankel transform. In both cases, $r \in \mathbb{R}$ is arbitrary.
Thus many sequences may have the same Hankel transform, and therefore the problem of inverting the Hankel transform is not an elementary one. In this note, we show, subject to a deep conjecture, that in one instance, the relationship between a sequence $h_n$ and its pre-image is more easily determined.

Although in the sequel we will exhibit Hankel transforms with ostensibly more than one pre-image, we introduce the notion of element multiplicity to confer a degree of uniqueness in the transform.

In the sequel, we will work with integer sequences. We will refer to some known sequences by their ``$Annnnnn$'' number in the On-Line Encyclopedia of Integer Sequences \cite{SL1}.

\section{A conjecture concerning special continued fractions}

\begin{conjecture} Consider a sequence of natural numbers  $p_n$, where $p_0=1$ and the sequence $b_n$ defined by
 \begin{equation}\label{b_seq} b_n=\sum_{k=0}^{\lfloor \frac{n}{2} \rfloor} p_{n-2k} - \frac{1+(-1)^n}{2} \end{equation} is non-negative and non-decreasing, with $b_n \in \mathbb{N}_0$,  $b_0=0,b_1 \ne 0$.   Then the sequence $a_n$ with generating function expressed as the continued fraction
$$\cfrac{1}{1\pm
\cfrac{x^{p_0}}{1\pm
\cfrac{x^{p_1}}{1\pm
\cfrac{x^{p_2}}{1\pm \cdots}}}}$$
has a Hankel transform consisting solely of the numbers $-1$, $0$, and $1$. Moreover, the non-zero terms occur at locations indexed by the sequence $b_n$.

Conversely, given a sequence of numbers $b_n \in N_0$,  $b_0=0,b_1 \ne 0, b_2, b_3,\ldots$ with $b_i \le b_{i+1}$, then the sequence whose generating function is given by the above continued fraction where the sequence $p_n$ is given by
\begin{displaymath}
\left(\begin{array}{c} p_0 \\ p_1 \\p_2\\ p_3 \\p_4 \\p_5 \\ \vdots \end{array}\right)=
\left(\begin{array}{ccccccc}1 & 0 & 0 & 0 & 0 & 0 & \ldots \\
                            0 & 1 & 0 & 0 & 0 & 0 & \ldots \\
                            0 & 0 & 1 & 0& 0 & 0 & \ldots \\
                            0 & -1 & 0 & 1 & 0 & 0 & \ldots \\
                            0 & 0 & -1&  0 & 1 & 0 & \ldots \\
                            0 & 0  & 0 & -1 & 0 & 1 &\ldots\\ \vdots
& \vdots &
\vdots & \vdots & \vdots & \vdots &
\ddots\end{array}\right)\left(\begin{array}{c} 1 \\ b_1 \\b_2\\ b_3 \\b_4 \\b_5 \\ \vdots \end{array}\right)\end{displaymath}
has a $\{-1,0,1\}$ Hankel transform whose non-zero elements are indexed by the sequence $b_n$.
\end{conjecture}

Equation (\ref{b_seq}) can be visualized in the following way.
\begin{displaymath}
\left(\begin{array}{c} b_0 \\ b_1 \\b_2\\ b_3 \\b_4 \\b_5 \\ \vdots \end{array}\right)=
\left(\begin{array}{ccccccc}0 & 0 & 0 & 0 & 0 & 0 & \ldots \\
                            0 & 1 & 0 & 0 & 0 & 0 & \ldots \\
                            0 & 0 & 1 & 0& 0 & 0 & \ldots \\
                            0 & 1 & 0 & 1 & 0 & 0 & \ldots \\
                            0 & 0 & 1&  0 & 1 & 0 & \ldots \\
                            0 & 1  & 0 & 1 & 0 & 1 &\ldots\\ \vdots
& \vdots &
\vdots & \vdots & \vdots & \vdots &
\ddots\end{array}\right)\left(\begin{array}{c} p_0 \\ p_1 \\p_2\\ p_3 \\p_4 \\p_5 \\ \vdots \end{array}\right),\end{displaymath} where column $k$ of the above matrix (except for column $0$) has generating function $\frac{x^k}{1-x^2}$. The sequence of numbers given by
$\sum_{k=0}^{\lfloor \frac{n}{2} \rfloor} p_{n-2k}$ corresponds to the degrees of the denominator polynomials in the partial continued fractions.

In the case that $b_i=b_{i+1}=\cdots =b_{i+r}$  we say that the corresponding non-zero term occurs with \emph{multiplicity} $r+1$.

We shall call the sequence of powers $\{p_n\}$ in the continued fraction the \emph{CF power sequence} and we shall call the indexing sequence $b_n$ the \emph{Hankel pattern sequence}. It specifies the pattern of occurrence of the non-zero terms in the Hankel transform. If we let $P(x)$ denote the generating function of the sequence $p_n$, and $B(x)$ denote the generating function of $b_n$, then we have the relations
\begin{equation} \label{rel_1} B(x)=\frac{1}{1-x^2}P(x)-\frac{1}{1-x^2},\end{equation} and
\begin{equation} \label{rel_2} P(x)=(1-x^2)B(x)-1.\end{equation}

Note that in the continued fraction above, the $\pm$ indicates that an arbitrary choice of ``$+$'' or ``$-$'' is possible at each stage. Note also that the conjecture is silent on the matter of the distribution of the minus signs in the transform.

\begin{example} The Jacobsthal numbers $J_n=\frac{2^n}{3}-\frac{(-1)^n}{3}$ \seqnum{A001045} give the sequence of numbers
$$0,1,1,3,5,11,21,43,\ldots.$$ We seek to find a sequence $a_n$ whose Hankel transform is composed of the numbers $-1$, $0$ and $1$, where the distribution of the non-zero terms is governed (or indexed) by the Jacobsthal numbers. Thus we want a Hankel transform of the form
$$\alpha,\beta,0,\gamma,0,\delta,0,0,0,0,0,\epsilon,0,0,0,0,0,0,0,0,0,\zeta,0,0,\ldots$$ where $\alpha, \beta,\ldots \in \{-1,1\}$.
As it is not immediately obvious how to deal with the duplicated $1$ ($J_1=J_2=1$), we shall ignore this for the moment and assume that the sequence is $$0,1,3,5,11,21,43,\ldots.$$
We look for the sequence of powers of $x$ to be used in the defining continued fraction.
\begin{displaymath}\left(\begin{array}{c} p_0 \\ p_1 \\p_2\\ p_3 \\p_4 \\p_5 \\ \vdots \end{array}\right)=
\left(\begin{array}{ccccccc}1 & 0 & 0 & 0 & 0 & 0 & \ldots \\
                            0 & 1 & 0 & 0 & 0 & 0 & \ldots \\
                            0 & 0 & 1 & 0& 0 & 0 & \ldots \\
                            0 & -1 & 0 & 1 & 0 & 0 & \ldots \\
                            0 & 0 & -1&  0 & 1 & 0 & \ldots \\
                            0 & 0  & 0 & -1 & 0 & 1 &\ldots\\ \vdots
& \vdots &
\vdots & \vdots & \vdots & \vdots &
\ddots\end{array}\right)\left(\begin{array}{c} 1 \\ 1 \\3\\ 5 \\11 \\21 \\ \vdots \end{array}\right)=
\left(\begin{array}{c} 1 \\ 1 \\3\\ 4 \\8 \\16 \\ \vdots \end{array}\right)\end{displaymath}

This leads us to consider the sequence $a_n$ with generating function
$$\cfrac{1}{1-
\cfrac{x}{1-
\cfrac{x}{1-
\cfrac{x^3}{1-
\cfrac{x^4}{1-
\cfrac{x^8}{1-
\cfrac{x^{16}}{1-\cdots}}}}}}}.$$
Thus $a_n$ begins
$$1, 1, 2, 4, 8, 17, 36, 76, 161, 342, 726, 1541, 3272, 6948, 14753,  \ldots$$ with the desired Hankel transform
$$1, 1, 0, -1, 0, 1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,\ldots.$$
Now let us see what happens when we retain both $J_1$ and $J_2$.
We look for the sequence of powers of $x$ to be used in the defining continued fraction.
\begin{displaymath}\left(\begin{array}{c} p_0 \\ p_1 \\p_2\\ p_3 \\p_4 \\p_5 \\ \vdots \end{array}\right)=
\left(\begin{array}{ccccccc}1 & 0 & 0 & 0 & 0 & 0 & \ldots \\
                            0 & 1 & 0 & 0 & 0 & 0 & \ldots \\
                            0 & 0 & 1 & 0& 0 & 0 & \ldots \\
                            0 & -1 & 0 & 1 & 0 & 0 & \ldots \\
                            0 & 0 & -1&  0 & 1 & 0 & \ldots \\
                            0 & 0  & 0 & -1 & 0 & 1 &\ldots\\ \vdots
& \vdots &
\vdots & \vdots & \vdots & \vdots &
\ddots\end{array}\right)\left(\begin{array}{c} 1 \\ 1 \\1\\ 3 \\5 \\11 \\ \vdots \end{array}\right)=
\left(\begin{array}{c} 1 \\ 1 \\1\\ 2 \\4 \\8 \\ \vdots \end{array}\right)\end{displaymath}

This leads us to consider the new sequence $a_n^*$ with generating function
$$\cfrac{1}{1-
\cfrac{x}{1-
\cfrac{x}{1-
\cfrac{x}{1-
\cfrac{x^2}{1-
\cfrac{x^4}{1-
\cfrac{x^8}{1-\cdots}}}}}}}.$$ We obtain the sequence that starts
$$1, 1, 2, 5, 13, 35, 95, 259, 707, 1932, 5281, 14438, 39475, 107933, 295115, 806922, 2206342, \ldots,$$ and which has Hankel transform
$$1, 1, 0, -1, 0, 1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,\ldots.$$
Thus both sequences $a_n$ and $a_n*$ have the same Hankel transform. However, the Hankel transform of $a_n$ corresponds to the
pattern sequence $0,1,3,5,11,\ldots$ while that of $a_n*$ has distribution $0,1,1,3,5,11,\ldots$. To distinguish the Hankel transforms, we could accompany each ``continued-fraction-derived'' $\{-1,0,1\}$ Hankel transform with its Hankel pattern sequence (or equivalently its CF power sequence). Using the Hankel pattern sequence for this purpose is equivalent to assigning multiplicities to the non-zero elements of the Hankel transform. Thus in the case of the Hankel transform of $a_n*$, we assign a multiplicity of two to the second $1$ in the Hankel transform, corresponding to the repetition of element $J_1=J_2$. To emphasize this, we sometimes write this Hankel transform as
$$1, 1_2, 0, -1, 0, 1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,\ldots, $$ where un-indexed numbers have multiplicity $1$.

\end{example}

In subsequent examples we allow for repeated elements in the Hankel pattern sequence. The next case is an extreme case, where all elements (bar the first) are duplicated.

\begin{example} The Catalan numbers $C_n=\frac{1}{n+1} \binom{2n}{n}$ \seqnum{A000108} with generating function $c(x)$ given by
$$c(x)=\frac{1-\sqrt{1-4x}}{2x}= \cfrac{1}{1-
\cfrac{x}{1-
\cfrac{x}{1-
\cfrac{x}{1-\cdots}}}}
$$ is well known to have Hankel transform $1,1,1,\ldots$ \cite{Kratt}; that is, each element $h_n=1$. The CF power sequence for $C_n$ is also $1,1,1,\ldots$. This means that the Hankel pattern for $C_n$ is the sequence $$0,1,1,2,2,3,3,4,4,\ldots$$ with general term $\lfloor \frac{n+1}{2} \rfloor$. Thus the first term $h_0=1$ has multiplicity $1$ while all the other terms $h_n=1$ have multiplicity $2$.
Using the index notation, we could thus write the Hankel transform of $C_n$ as
$$1,1_2,1_2,1_2,1_2,1_2,1_2,1_2,1_2,1_2,\ldots.$$
\end{example}

\begin{example} We consider the CF power sequence $\{p_n\}$ given by $1,1,2,2,2,2,2,\ldots$. This corresponds to the Hankel pattern
$$0,1,2,3,4,5,6,\ldots.$$ The sequence $a_n$ with generating function
$$\cfrac{1}{1-
\cfrac{x}{1-
\cfrac{x}{1-
\cfrac{x^2}{1-
\cfrac{x^2}{1-
\cfrac{x^2}{1-
 \cdots}}}}}}$$
 has Hankel transform $1,1,1,1,\ldots$ where each ``$1$'' has multiplicity $1$. Thus we could have written this Hankel transform as
 $$1_1,1_1,1_1,1_1,1_1,1_1,1_1,1_1,\ldots.$$

\noindent The sequence $a_n$ begins
 $$1, 1, 2, 4, 9, 20, 46, 105, 243, 560, 1299, 3006, \ldots,$$
\noindent The generating function of this sequence is
$$\cfrac{1}{1-\cfrac{x}{1-x c(x^2)}}.$$

 \end{example}

\begin{example} It is not the case that every $\{-1,0,1\}$ sequence is the Hankel transform of an integer sequence. For example, the sequence $$1,0,1,0,1,0,1,0,1,\ldots $$ is not the Hankel transform of any integer (or real) sequence.
\begin{proof} (Somos)
Assume that the sequence $\alpha, \beta, \gamma, \delta, \epsilon,\ldots$ has Hankel transform $1,0,1,0,\ldots$.
We have $h_0=\alpha$ and hence $\alpha=1$.

\noindent Then $h_1=\gamma-\beta^2=0$ and so $\gamma=\beta^2$, and so the sequence would start $1,\beta,\beta^2, \delta, \ldots$.

\noindent Then $h_2=\beta^6+2\beta^3 \delta-\delta^2$, and so we must have
$$\beta^6+2\beta^3 \delta-\delta^2=1.$$ The solution of this quadratic in $\delta$ is
$$ \delta=\beta^3-i \quad \text{or} \quad \delta=\beta^3+i.$$
\end{proof}

\end{example}

\begin{example} Consider the CF power sequence $p_n$ given by $1,1,3,3,3,3,3,\ldots$. This gives us the Hankel pattern sequence
$$0, 1, 3, 4, 6, 7, 9, 10, 12, 13, 15, \ldots.$$

The corresponding sequence begins
$$1, 1, 2, 4, 8, 17, 36, 76, 162, 345, 734, 1565, 3336, 7109, 15158, 32318, 68898, \ldots$$ with generating function  $$\cfrac{1}{1-\cfrac{x}{1-x c(x^3)}}.$$
\noindent Its Hankel transform is equal to the periodic sequence
$$1, 1, 0, -1, -1, 0, 1, 1, 0, -1, -1, 0, \ldots.$$
\noindent Here, the non-zero terms all have multiplicity one.
\end{example}
\section{A number theoretic example}
We let the Hankel  pattern sequence be the sequence $b_n=\binom{n+1}{2}$ of the triangular numbers. We find that the corresponding CF power sequence is the sequence $$1,1,3,5,7,9,11,\ldots$$ of extended odd numbers. The sequence generated by the continued fraction defined by the power sequence begins
$$ 1, 1, 2, 4, 8, 17, 36, 76, 161, 341, 723, 1533, 3250, 6891, 14611, 30980, 65688, 139281, \ldots $$ and has a Hankel $h_n$ transform
that begins
$$ 1, 1, 0, -1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, -1, 0, \ldots $$

In this case, the quantity
$$e_n=\sum_{k=0}^n (-1)^{n-k} h_n h_{n-k}$$ is of interest. This is the convolution of $h_n$ with $(-1)^n h_n$. The sequence
$e_{2n}$ begins $$1, -1, 2, 1, 0, 2, 1, 0, 0, 2, 1, 2, \ldots $$ and is related to the so-called ``eta quotients''.

If we now take the CF power sequence $p_n = \lfloor \frac{n+1}{2} \rfloor +0^n$, or $1,1,1,2,2,3,3,4,4,5,5,\ldots$, we obtain
the Hankel pattern sequence $$0, 1, 1, 3, 3, 6, 6, 10, 10, 15, 15, \ldots $$
The sequence $a_n$ now begins
$$1, 1, 2, 5, 13, 35, 95, 260, 713, 1959, 5386, 14815, 40759, 112151, 308609, 849240, 2337009, 6431246, \ldots $$ and has Hankel transform $$1, 1, 0, -1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,\ldots$$
\noindent Here, apart from the first term, the non-zero terms have multiplicity two.

\section{More variations on $1,3,5,\ldots$}
We start this section by noting that the sequence defined by the CF power sequence $1,3,5,7,\ldots$ is \seqnum{A143951}, which counts the  number of Dyck paths such that the area between the $x$-axis and the path is $n$ (Emeric Deutsch). The Hankel transform of this sequence begins
$$1, 0, 0, -1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -1,\ldots,$$ with Hankel pattern sequence
$$b_n=(n^2+3n+1+(-1)^n)/2,$$ which is essentially \seqnum{A176222}. The sequence $b_n$ thus begins
$$0, 3, 5, 10, 14, 21, 27, 36, 44, 55, 65,\ldots.$$

For the CF power sequence $1,1,3,3,5,5,7,7,\ldots$ we find that the sequence $b_n$, which begins
$$0, 1, 3, 4, 8, 9, 15, 16, 24, 25, 35,\ldots,$$ satisfies
$$b_n=(2n^2+6n+1+(2n-1)(-1)^n)/8.$$
The corresponding sequence $a_n$ begins
$$1, 1, 2, 4, 8, 17, 36, 76, 162, 345, 734, 1564, 3332, \ldots$$ with Hankel transform
$$ 1, 1, 0, -1, -1, 0, 0, 0, -1, -1, 0, 0, 0, 0, 0, 1, 1, 0, 0, \ldots.$$

For the CF power sequence $1,1,1,3,3,3,5,5,5,7,7,7,\ldots$  we find that the sequence $b_n$, which begins
$$0,1, 1, 3, 3, 5, 6, 8, 9, 12, 13, 16,\ldots,$$ has generating function
$$\frac{x(1+x^2-x^3)}{(1-x)^3 (1+2x+2x^2+x^3)}.$$ The corresponding sequence $a_n$ begins
$$ 1, 1, 2, 5, 13, 34, 90, 239, 635, 1689, 4494, 11958, 31823, 84692, 225396, \ldots$$ and has Hankel transform
$$1, 1, 0, 0, -1, 0, 0, 1, 0, -1, 0, 0, 1, 0, -1, 0,\ldots. $$

\noindent Some slight variations on this last example are also of interest. For instance, the CF power sequence
$$1, 2, 3, 3, 3, 5, 5, 5, 7, 7, 7, 9, 9, 9, 11, \ldots$$ corresponds to the Hankel pattern sequence $b_n$ which begins
$$0, 2, 3, 5, 6, 10, 11, 15, 18, 22, 25, 31, 34, 40, 45,\ldots,$$ with generating function
$$\frac{x(2+x-2x^3+x^4)}{(1-x)^3(1+2x+2x^2+x^3)}.$$ The Hankel transform of $a_n$ is then
$$1, 0, -1, -1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1,\ldots.$$

\section{A pattern avoiding example} The CF power sequence $$1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1,\ldots $$ corresponds to the Hankel pattern sequence $$0, 1, 1, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8, 9, 9, 10, 11, 11, 12, 13, 13, \ldots $$ which gives a $\{-1,0,1\}$ Hankel transform where odd indexed non-zero terms have multiplicity two, and the even-indexed non-zero terms have multiplicity one.
The corresponding sequence is \seqnum{A054391} \cite{Mansour_Shattuck}. It begins
$$1, 1, 2, 5, 14, 41, 123, 374, 1147, 3538, 10958, 34042, 105997, \ldots$$  and  has Hankel transform consisting of all $1$'s (with the multiplicities above). Thus we could write the Hankel transform as
$$1,1_2,1,1_2,1,1_2,1,\ldots.$$

We note that the generating function of this sequence is given by
$$\cfrac{1}{1-
\cfrac{x}{1-x u}},$$ where $u$ satisfies the equation
$$u=\cfrac{1}{1-\cfrac{x}{1-\cfrac{x}{1-x^2 u}}}.$$
Solving, we find that the generating function is equal to
$$\frac{1-3x-\sqrt{1-2x-3x^2}}{1-3x+2x^2+\sqrt{1-2x-3x^2}}, $$ confirming that this sequence is the same as \seqnum{A054391}.

\section{The Motzkin numbers} We consider the CF power sequence
$$ 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 2, \ldots.$$ This corresponds to the Hankel pattern sequence
$$0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10, 11, 12, 12, 13, 14, \ldots $$ so once again we obtain a sequence with an
all $1$'s Hankel transform, but with the multiplicities indicated. Thus we get the Hankel transform
$$1,1,1_2,1,1_2,1,1_2,1,\ldots.$$

The generating function $u=g(x)$ satisfies the equation
$$u=\cfrac{1}{1-\cfrac{x}{1-\cfrac{x}{1-x^2 u}}},$$ which solves to give
$$g(x)=\frac{1-x-\sqrt{1-2x-3x^2}}{2x^2},$$ which coincides with the g.f. of the Motzkin numbers \seqnum{A001006}.
\section{Euler pentagonal numbers}
In this section, we take the Euler pentagonal numbers \seqnum{A001318} \cite{Somos} as the basis of the CF power sequence. Thus we define
$$p_n=\frac{6n^2+6n+1}{16}-\frac{(2n+1)(-1)^n}{16}+0^n$$ which begins
$$1, 1, 2, 5, 7, 12, 15, 22, 26, 35, 40,\ldots.$$ The corresponding Hankel pattern sequence is given by
$$b_n = \frac{1}{2}\lfloor \frac{n+1}{2} \rfloor \lfloor \frac{n+2}{2} \rfloor \lfloor \frac{n+3}{2} \rfloor,$$ which is
\seqnum{A028724}. This begins
$$0, 1, 2, 6, 9, 18, 24, 40, 50, 75, 90,\ldots .$$

The resulting sequence begins
$$1, 1, 2, 4, 9, 20, 45, 101, 227, 511, 1150, 2589, 5828, 13120, 29536, 66492, 149690,\ldots,$$ and has Hankel transform

$$1, 1, 1, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, \ldots$$

Note that if in the continued fraction we take the pattern of signs $-,-,+,+,-,-,+,+,\ldots$ then the resulting sequence begins
$$1, 1, 2, 4, 7, 12, 21, 37, 65, 115, 204, 361, 638, 1128, 1994, 3524, 6230, \ldots$$ and has a Hankel transform that begins
$$1, 1, -1, 0, 0, 0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,\ldots $$

\section{A Fibonacci distribution}
It is of interest to find a sequence whose Hankel transform has a $\{-1,0,1\}$ distribution that follows the Fibonacci numbers \seqnum{A000045}. Corresponding to the CF power sequence
$$1,1,1,1,2,3,5,8,13,21,34,55,89,\ldots $$ we get the Hankel pattern sequence
$$0,1,1,2,3,5,8,13,21,34,55,89,\ldots.$$
The sequence defined by the corresponding continued fraction begins
$$1, 1, 2, 5, 14, 41, 123, 373, 1137, 3475, 10634, 32562, 99738, 305546, 936108, 2868084,\ldots$$
and it has a Hankel transform that starts
$$1, 1, 1, 1, 0, -1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, \ldots.$$

\section{A gap Hankel transform}
Consider the CF power sequence
$$1,3,4,2,2,2,2,2,\ldots.$$
The corresponding Hankel pattern sequence is
$$0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,\ldots.$$
Thus there will be a 'gap' at positions $1$ and $2$ (filled by zeros). In fact, the sequence $a_n$ corresponding to this CF power sequence begins
$$1, 1, 1, 1, 2, 3, 4, 6, 10, 15, 23, 36, 58, 90, 145, 230, 377, 601, 1000,\ldots$$
and has Hankel transform $h_n$ given by
$$1,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,\ldots.$$
\noindent The sequence $a_n$ has some interesting Hankel properties. The sequence $a_{n+1}$ has Hankel transform given by
$$1,0,-1,0,1,0,-1,0,1,0,\ldots.$$ \noindent The twice shifted sequence $a_{n+2}$ has Hankel transform
$$1, 1, -1, -1, -2, -2, -3, -3, -4, -4, \ldots,$$ while the three-times shifted sequence $a_{n+3}$ has Hankel transform
$$1, -1, -1, 1, 4, -1, -9, 1, 16, \ldots.$$ If we prepend $1,1$ to the sequence then the new sequence has a Hankel transform
of
$$1,0,0,0,0,1,1,2,2,3,3,\ldots.$$
Other sequences with ``gap'' Hankel transforms can be built using the template
$$1,r,r+1,2,2,2,2,2,2,2,\ldots$$ for the CF power sequence, since this maps to the pattern sequence
$$0, r, r + 1, r + 2, r + 3, r + 4, r + 5, r + 6, r + 7, r + 8, r + 9,\ldots.$$

\section{Conclusion} The foregoing shows that $\{-1,0,1\}$ Hankel transforms are objects worthy of study. The notion of multiplicity seems important, particularly as a way of further characterizing certain sequences that would otherwise have the ``same'' Hankel transforms. Thus the Catalan numbers $C_n$ have Hankel transform $1,1,1,1,\ldots$ but with pattern sequence $0,1,1,2,2,3,3,\ldots,$ while the Motzkin numbers $M_n$ also have Hankel transform $1,1,1,1,\ldots$ but with pattern sequence $0,1,2,2,3,4,4,\ldots$.

\section{Acknowledgement} The material in this note arose out of an exchange of emails with Michael Somos, who originally posed the question of the origins of $\{-1,0,1\}$ Hankel transforms. I am extremely grateful to him for the time spent on this exchange.

\begin{thebibliography}{13}

\bibitem{Kratt}
C. Krattenthaler, Advanced determinant calculus: A
complement, \emph{Lin. Alg. Appl.} {\bf 411} (2005)
68�-166.

\bibitem{Layman} J. W. Layman,
 The Hankel transform and some
    of
    its properties, \emph{J. Integer Seq.} {\bf
    4} (2001), \href{http://www.cs.uwaterloo.ca/journals/JIS/VOL4/LAYMAN/hankel.html}{Article 01.1.5}.

\bibitem{Mansour_Shattuck}
T. Mansour, M. Shattuck, Pattern avoiding partitions, sequence \seqnum{A054391} and the kernel method, \emph{Applications and Applied Mathematics} \textbf{6} (2011), 397--411.

\bibitem{SL1} N. J. A.~Sloane, \emph{The
On-Line Encyclopedia of Integer Sequences}. Published electronically
at \href{http://oeis.org}{http://oeis.org}, 2012.

\bibitem{Somos}
M. Somos, A Multisection of $q$-Series, http://cis.csuohio.edu/~somos/multiq.pdf

\end{thebibliography}

\bigskip \hrule \bigskip

\noindent 2010 {\it Mathematics Subject Classification}: Primary 11B83; Secondary 05A15, 11J70.
\\
\noindent \emph{Keywords:}
Permutation, integer sequence, Hankel determinant, Hankel
transform, continued fraction

 \bigskip \hrule \bigskip \noindent Concerns sequences

\seqnum{A000045},
\seqnum{A000108},
\seqnum{A001006},
\seqnum{A001045},
\seqnum{A001318},
\seqnum{A028724},
\seqnum{A054391},
\seqnum{A143951}.

\end{document}


\section{Number Printing}
\label{pgfmath-numberprinting}

{\emph{An extension by Christian Feuersänger}}

\medskip
\noindent
\pgfname\ supports number printing in different styles and rounds to arbitrary
precision.

\begin{command}{\pgfmathprintnumber\marg{x}}
    Generates pretty-printed output for the (real) number \meta{x}. The input
    number \meta{x} is parsed using |\pgfmathfloatparsenumber| which allows
    arbitrary precision.

    Numbers are typeset in math mode using the current set of number printing
    options, see below. Optional arguments can also be provided using
    |\pgfmathprintnumber[|\meta{options}|]|\meta{x}.
\end{command}

\begin{command}{\pgfmathprintnumberto\marg{x}\marg{macro}}
    Returns the resulting number into \meta{macro} instead of typesetting it
    directly.
\end{command}

\begin{key}{/pgf/number format/fixed}
    Configures |\pgfmathprintnumber| to round the number to a fixed number of
    digits after the period, discarding any trailing zeros.
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,fixed,precision=2}
\pgfmathprintnumber{4.568}\hspace{1em}
\pgfmathprintnumber{5e-04}\hspace{1em}
\pgfmathprintnumber{0.1}\hspace{1em}
\pgfmathprintnumber{24415.98123}\hspace{1em}
\pgfmathprintnumber{123456.12345}
\end{codeexample}

    See section~\ref{sec:number:styles} for how to change the appearance.
\end{key}

\begin{key}{/pgf/number format/fixed zerofill=\marg{boolean}  (default true)}
    Enables or disables zero filling for any number drawn in fixed point
    format.
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,fixed,fixed zerofill,precision=2}
\pgfmathprintnumber{4.568}\hspace{1em}
\pgfmathprintnumber{5e-04}\hspace{1em}
\pgfmathprintnumber{0.1}\hspace{1em}
\pgfmathprintnumber{24415.98123}\hspace{1em}
\pgfmathprintnumber{123456.12345}
\end{codeexample}
    %
    This key affects numbers drawn with |fixed| or |std| styles (the latter
    only if no scientific format is chosen).
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,std,fixed zerofill,precision=2}
\pgfmathprintnumber{4.568}\hspace{1em}
\pgfmathprintnumber{5e-05}\hspace{1em}
\pgfmathprintnumber{1}\hspace{1em}
\pgfmathprintnumber{123456.12345}
\end{codeexample}

    See section~\ref{sec:number:styles} for how to change the appearance.
\end{key}

\begin{key}{/pgf/number format/sci}
    Configures |\pgfmathprintnumber| to display numbers in scientific format,
    that means sign, mantissa and exponent (basis~$10$). The mantissa is
    rounded to the desired |precision| (or |sci precision|, see below).
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,sci,precision=2}
\pgfmathprintnumber{4.568}\hspace{1em}
\pgfmathprintnumber{5e-04}\hspace{1em}
\pgfmathprintnumber{0.1}\hspace{1em}
\pgfmathprintnumber{24415.98123}\hspace{1em}
\pgfmathprintnumber{123456.12345}
\end{codeexample}

    See section~\ref{sec:number:styles} for how to change the exponential
    display style.
\end{key}

\begin{key}{/pgf/number format/sci zerofill=\marg{boolean}  (default true)}
    Enables or disables zero filling for any number drawn in scientific format.
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,sci,sci zerofill,precision=2}
\pgfmathprintnumber{4.568}\hspace{1em}
\pgfmathprintnumber{5e-04}\hspace{1em}
\pgfmathprintnumber{0.1}\hspace{1em}
\pgfmathprintnumber{24415.98123}\hspace{1em}
\pgfmathprintnumber{123456.12345}
\end{codeexample}
    %
    As with |fixed zerofill|, this option does only affect numbers drawn in
    |sci| format (or |std| if the scientific format is chosen).

    See section~\ref{sec:number:styles} for how to change the exponential
    display style.
\end{key}

\begin{stylekey}{/pgf/number format/zerofill=\marg{boolean} (default true)}
    Sets both |fixed zerofill| and |sci zerofill| at once.
\end{stylekey}

\begin{keylist}{/pgf/number format/std,%
    /pgf/number format/std=\meta{lower e},
    /pgf/number format/std=\meta{lower e}:\meta{upper e}%
}
    Configures |\pgfmathprintnumber| to a standard algorithm. It chooses either
    |fixed| or |sci|, depending on the order of magnitude. Let $n=s \cdot m
    \cdot 10^e$ be the input number and $p$ the current precision. If $-p/2 \le
    e \le 4$, the number is displayed using |fixed| format. Otherwise, it is
    displayed using |sci| format.
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,std,precision=2}
\pgfmathprintnumber{4.568}\hspace{1em}
\pgfmathprintnumber{5e-04}\hspace{1em}
\pgfmathprintnumber{0.1}\hspace{1em}
\pgfmathprintnumber{24415.98123}\hspace{1em}
\pgfmathprintnumber{123456.12345}
\end{codeexample}
    %
    The parameters can be customized using the optional integer argument(s): if
    $\text{\meta{lower e}} \le e \le \text{\meta{upper e}}$, the number is
    displayed in |fixed| format, otherwise in |sci| format. Note that
    \meta{lower e} should be negative for useful results. The precision used
    for the scientific format can be adjusted with |sci precision| if
    necessary.
\end{keylist}

\begin{keylist}{/pgf/number format/relative*=\meta{exponent base 10}}
    Configures |\pgfmathprintnumber| to format numbers relative to an order of
    magnitude, $10^r$, where $r$ is an integer number.

    This key addresses different use-cases.

    \paragraph{First use-case:}

    provide a unified format for a \emph{sequence} of numbers. Consider the
    following test:
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/relative*={1}}
\pgfmathprintnumber{6.42e-16}\hspace{1em}
\pgfmathprintnumber{1.2}\hspace{1em}
\pgfmathprintnumber{6}\hspace{1em}
\pgfmathprintnumber{20.6}\hspace{1em}
\pgfmathprintnumber{87}
\end{codeexample}
    %
    \noindent With any other style, the |6.42e-16| would have been formatted as
    an isolated number. Here, it is rounded to |0| because when viewed relative
    to $10^1$ (the exponent $1$ is the argument for |relative|), it has no
    significant digits.
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/relative*={2}}
\pgfmathprintnumber{123.345}\hspace{1em}
\pgfmathprintnumber{0.0012}\hspace{1em}
\pgfmathprintnumber{0.0014}\hspace{1em}
\end{codeexample}
    %
    \noindent The example above applies the initial |precision=2| to |123.345|
    -- relative to $100$. Two significant digits of |123.345| relative to $100$
    are |123|. Note that the ``$2$ significant digits of |123.345|'' translates
    to ``round |1.2345| to $2$ digits'', which would yield |1.2300|. Similarly,
    the other two numbers are |0| compared to $100$ using the given
    |precision|.
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/relative*={-3}}
\pgfmathprintnumber{123.345}\hspace{1em}
\pgfmathprintnumber{0.0012}\hspace{1em}
\pgfmathprintnumber{0.0014}\hspace{1em}
\end{codeexample}

    \paragraph{Second use-case:}

    improve rounding in the presence of \emph{inaccurate} numbers. Let us
    suppose that some limited-precision arithmetics resulted in the result
    |123456999| (like the |fpu| of \pgfname). You know that its precision is
    about five or six significant digits. And you want to provide a fixed point
    output. In this case, the trailing digits |....999| are a numerical
    artifact due to the limited precision. Use |relative*=3,precision=0| to
    eliminate the artifacts:
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,relative*={3},precision=0}
\pgfmathprintnumber{123456999}\hspace{1em}
\pgfmathprintnumber{123456999.12}
\end{codeexample}
    %
    \noindent Here, |precision=0| means that we inspect |123456.999| and round
    that number to $0$ digits. Finally, we move the period back to its initial
    position. Adding |relative style=fixed| results in fixed point output
    format:
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,relative*={3},precision=0,relative style=fixed}
\pgfmathprintnumber{123456999}\hspace{1em}
\pgfmathprintnumber{123456999.12}
\end{codeexample}
    %
    \noindent Note that there is another alternative for this use-case which is
    discussed later: the |fixed relative| style.
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,fixed relative,precision=6}
\pgfmathprintnumber{123456999}\hspace{1em}
\pgfmathprintnumber{123456999.12}
\end{codeexample}

    You might wonder why there is an asterisk in the key's name. The short
    answer is: there is also a \declareandlabel{/pgf/number format/relative}
    number printer which does unexpected things. The key |relative*| repairs
    this. Existing code will still use the old behavior.

    Technically, the key works as follows: as already explained above,
    |relative*=3| key applied to |123456999.12| moves the period by three
    positions and analyzes |123456.99912|. Mathematically speaking, we are
    given a number $x = \pm m \cdot 10^e$ and we attempt to apply
    |relative*=|$r$. The method then rounds $x / 10^r$ to |precision| digits.
    Afterwards, it multiplies the result by $10^r$ and typesets it.
\end{keylist}

\begin{stylekey}{/pgf/number format/every relative}
    A style which configures how the |relative| method finally displays its
    results.

    The initial configuration is
    %
\begin{codeexample}[code only]
\pgfkeys{/pgf/number format/every relative/.style=std}
\end{codeexample}

    Note that rounding is turned off when the resulting style is being
    evaluated (since |relative| already rounded the number).

    Although supported, I discourage from using |fixed zerofill| or
    |sci zerofill| in this context -- it may lead to a suggestion of higher
    precision than is actually used (because |fixed zerofill| might simply add
    |.00| although there was a different information before |relative| rounded
    the result).
\end{stylekey}

\begin{key}{/pgf/number format/relative style=\marg{options}}
    The same as |every relative/.append style=|\marg{options}.
\end{key}

\begin{keylist}{/pgf/number format/fixed relative}
    Configures |\pgfmathprintnumber| to format numbers in a similar way to the
    |fixed| style, but the |precision| is interpreted relatively to the
    number's exponent.

    The motivation is to get the same rounding effect as for |sci|, but to
    display the number in the |fixed| style:
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,fixed relative,precision=3}
\pgfmathprintnumber{1000.0123}\hspace{1em}
\pgfmathprintnumber{100.0567}\hspace{1em}
\pgfmathprintnumber{0.000010003452}\hspace{1em}
\pgfmathprintnumber{0.010073452}\hspace{1em}
\pgfmathprintnumber{1.23567}\hspace{1em}
\pgfmathprintnumber{1003.75}\hspace{1em}
\pgfmathprintnumber{1006.75}\hspace{1em}
\end{codeexample}

    The effect of |fixed relative| is that the number is rounded to
    \emph{exactly} the first \meta{precision} non-zero digits, no matter how
    many leading zeros the number might have.

    Use |fixed relative| if you want |fixed| and if you know that only the
    first $n$ digits are correct. Use |sci| if you need a scientific display
    style and only the first $n$ digits are correct.

    Note that |fixed relative| ignores the |fixed zerofill| flag.

    See also the |relative*| key. Note that the |relative=|\marg{exponent} key
    explicitly moves the period to some designated position before it attempts
    to round the number. Afterwards, it ``rounds from the right'', i.e.\ it
    rounds to that explicitly chosen digit position. In contrast to that,
    |fixed relative| ``rounds from the left'': it takes the \emph{first}
    non-zero digit, temporarily places the period after this digit, and rounds
    that number. The rounding style |fixed| leaves the period where it is, and
    rounds everything behind that digit. The |sci| style is similar to
    |fixed relative|.
\end{keylist}

\begin{key}{/pgf/number format/int detect}
    Configures |\pgfmathprintnumber| to detect integers automatically. If the
    input number is an integer, no period is displayed at all. If not, the
    scientific format is chosen.
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,int detect,precision=2}
\pgfmathprintnumber{15}\hspace{1em}
\pgfmathprintnumber{20}\hspace{1em}
\pgfmathprintnumber{20.4}\hspace{1em}
\pgfmathprintnumber{0.01}\hspace{1em}
\pgfmathprintnumber{0}
\end{codeexample}
    %
\end{key}

\begin{command}{\pgfmathifisint\marg{number constant}\marg{true code}\marg{false code}}
    A command which does the same check as |int detect|, but it invokes
    \meta{true code} if the \meta{number constant} actually is an integer and
    the \meta{false code} if not.

    As a side-effect, |\pgfretval| will contain the parsed number, either in
    integer format or as parsed floating point number.

    The argument \meta{number constant} will be parsed with
    |\pgfmathfloatparsenumber|.
    %
\begin{codeexample}[]
15 \pgfmathifisint{15}{is an int: \pgfretval.}{is no int}\hspace{1em}
15.5 \pgfmathifisint{15.5}{is an int: \pgfretval.}{is no int}
\end{codeexample}
    %
\end{command}

\begin{key}{/pgf/number format/int trunc}
    Truncates every number to integers (discards any digit after the period).
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,int trunc}
\pgfmathprintnumber{4.568}\hspace{1em}
\pgfmathprintnumber{5e-04}\hspace{1em}
\pgfmathprintnumber{0.1}\hspace{1em}
\pgfmathprintnumber{24415.98123}\hspace{1em}
\pgfmathprintnumber{123456.12345}
\end{codeexample}
    %
\end{key}

\begin{key}{/pgf/number format/frac}
    Displays numbers as fractionals.
    %
\begin{codeexample}[width=3cm,preamble={\usetikzlibrary{fpu}}]
\pgfkeys{/pgf/number format/frac}
\pgfmathprintnumber{0.333333333333333}\hspace{1em}
\pgfmathprintnumber{0.5}\hspace{1em}
\pgfmathprintnumber{2.133333333333325e-01}\hspace{1em}
\pgfmathprintnumber{0.12}\hspace{1em}
\pgfmathprintnumber{2.666666666666646e-02}\hspace{1em}
\pgfmathprintnumber{-1.333333333333334e-02}\hspace{1em}
\pgfmathprintnumber{7.200000000000000e-01}\hspace{1em}
\pgfmathprintnumber{6.666666666666667e-02}\hspace{1em}
\pgfmathprintnumber{1.333333333333333e-01}\hspace{1em}
\pgfmathprintnumber{-1.333333333333333e-02}\hspace{1em}
\pgfmathprintnumber{3.3333333}\hspace{1em}
\pgfmathprintnumber{1.2345}\hspace{1em}
\pgfmathprintnumber{1}\hspace{1em}
\pgfmathprintnumber{-6}
\end{codeexample}

    \begin{key}{/pgf/number format/frac TeX=\marg{\textbackslash macro} (initially \texttt{\textbackslash frac})}
        Allows to use a different implementation for |\frac| inside of the
        |frac| display type.
    \end{key}

    \begin{key}{/pgf/number format/frac denom=\meta{int} (initially empty)}
        Allows to provide a custom denominator for |frac|.
        %
\begin{codeexample}[width=3cm,preamble={\usetikzlibrary{fpu}}]
\pgfkeys{/pgf/number format/.cd,frac, frac denom=10}
\pgfmathprintnumber{0.1}\hspace{1em}
\pgfmathprintnumber{0.5}\hspace{1em}
\pgfmathprintnumber{1.2}\hspace{1em}
\pgfmathprintnumber{-0.6}\hspace{1em}
\pgfmathprintnumber{-1.4}\hspace{1em}
\end{codeexample}
    \end{key}
    %
    \begin{key}{/pgf/number format/frac whole=\mchoice{true,false} (initially true)}
        Configures whether complete integer parts shall be placed in front of
        the fractional part. In this case, the fractional part will be less
        then $1$. Use |frac whole=false| to avoid whole number parts.
        %
\begin{codeexample}[width=3cm,preamble={\usetikzlibrary{fpu}}]
\pgfkeys{/pgf/number format/.cd,frac, frac whole=false}
\pgfmathprintnumber{20.1}\hspace{1em}
\pgfmathprintnumber{5.5}\hspace{1em}
\pgfmathprintnumber{1.2}\hspace{1em}
\pgfmathprintnumber{-5.6}\hspace{1em}
\pgfmathprintnumber{-1.4}\hspace{1em}
\end{codeexample}
    \end{key}
    %
    \begin{key}{/pgf/number format/frac shift=\marg{integer} (initially 4)}
        In case you experience problems because of stability problems, try
        experimenting with a different |frac shift|. Higher shift values $k$
        yield higher sensitivity to inaccurate data or inaccurate arithmetics.

        Technically, the following happens. If $r < 1$ is the fractional part
        of the mantissa, then a scale $i = 1/r \cdot 10^k$ is computed where
        $k$ is the shift; fractional parts of $i$ are neglected. The value
        $1/r$ is computed internally, its error is amplified.

        If you still experience stability problems, use |\usepackage{fp}| in
        your preamble. The |frac| style will then automatically employ the
        higher absolute precision of |fp| for the computation of $1/r$.
    \end{key}
\end{key}

\begin{key}{/pgf/number format/precision=\marg{number}}
    Sets the desired rounding precision for any display operation. For
    scientific format, this affects the mantissa.
\end{key}

\begin{key}{/pgf/number format/sci precision=\meta{number or empty} (initially empty)}
    Sets the desired rounding precision only for |sci| styles.

    Use |sci precision={}| to restore the initial configuration (which uses the
    argument provided to |precision| for all number styles).
\end{key}

\begin{key}{/pgf/number format/read comma as period=\mchoice{true,false} (initially false)}
    This is one of the few keys which allows to customize the number parser. If
    this switch is turned on, a comma is read just as a period.
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/read comma as period}
\pgfmathprintnumber{1234,56}
\end{codeexample}
    %
    This is typically undesired as it can cause side-effects with math parsing
    instructions. However, it is supported to format input numbers or input
    tables. Consider |use comma| to typeset the result with a comma as well.
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,
    read comma as period,
    use comma}
\pgfmathprintnumber{1234,56}
\end{codeexample}
    %
\end{key}

\subsection{Changing display styles}%
\label{sec:number:styles}

You can change the way how numbers are displayed. For example, if you use the
`\texttt{fixed}' style, the input number is rounded to the desired precision
and the current fixed point display style is used to typeset the number. The
same is applied to any other format: first, rounding routines are used to get
the correct digits, afterwards a display style generates proper \TeX-code.

\begin{key}{/pgf/number format/set decimal separator=\marg{text}}
    Assigns \marg{text} as decimal separator for any fixed point numbers
    (including the mantissa in sci format).

    Use |\pgfkeysgetvalue{/pgf/number format/set decimal separator}\value| to
    get the current separator into |\value|.
\end{key}

\begin{stylekey}{/pgf/number format/dec sep=\marg{text}}
    Just another name for |set decimal separator|.
\end{stylekey}

\begin{key}{/pgf/number format/set thousands separator=\marg{text}}
    Assigns \marg{text} as thousands separator for any fixed point numbers
    (including the mantissa in sci format).
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,
    fixed,
    fixed zerofill,
    precision=2,
    set thousands separator={}}
\pgfmathprintnumber{1234.56}
\end{codeexample}
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,
    fixed,
    fixed zerofill,
    precision=2,
    set thousands separator={}}
\pgfmathprintnumber{1234567890}
\end{codeexample}

\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,
    fixed,
    fixed zerofill,
    precision=2,
    set thousands separator={.}}
\pgfmathprintnumber{1234567890}
\end{codeexample}
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,
    fixed,
    fixed zerofill,
    precision=2,
    set thousands separator={,}}
\pgfmathprintnumber{1234567890}
\end{codeexample}
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,
    fixed,
    fixed zerofill,
    precision=2,
    set thousands separator={{{,}}}}
\pgfmathprintnumber{1234567890}
\end{codeexample}
    %
    The last example employs commas and disables the default comma-spacing.

    Use |\pgfkeysgetvalue{/pgf/number format/set thousands separator}\value| to
    get the current separator into |\value|.
\end{key}

\begin{stylekey}{/pgf/number format/1000 sep=\marg{text}}
    Just another name for |set thousands separator|.
\end{stylekey}

\begin{key}{/pgf/number format/1000 sep in fractionals=\marg{boolean} (initially false)}
    Configures whether the fractional part should also be grouped into groups
    of three digits.

    The value |true| will active the |1000 sep| for both, integer and
    fractional parts. The value |false| will active |1000 sep| only for the
    integer part.
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,
    fixed,
    precision=999,
    set thousands separator={\,},
    1000 sep in fractionals,
    }
\pgfmathprintnumber{1234.1234567}
\end{codeexample}
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,
    fixed,fixed zerofill,
    precision=9,
    set thousands separator={\,},
    1000 sep in fractionals,
    }
\pgfmathprintnumber{1234.1234567}
\end{codeexample}
    %
\end{key}

\begin{key}{/pgf/number format/min exponent for 1000 sep=\marg{number} (initially 0)}
    Defines the smallest exponent in scientific notation which is required to
    draw thousand separators. The exponent is the number of digits minus one,
    so $\meta{number}=4$ will use thousand separators starting with $1e4 =
    10000$.
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,
    int detect,
    1000 sep={\,},
    min exponent for 1000 sep=0}
\pgfmathprintnumber{5000}; \pgfmathprintnumber{1000000}
\end{codeexample}

\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,
    int detect,
    1000 sep={\,},
    min exponent for 1000 sep=4}
\pgfmathprintnumber{1000}; \pgfmathprintnumber{5000}
\end{codeexample}
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,
    int detect,
    1000 sep={\,},
    min exponent for 1000 sep=4}
\pgfmathprintnumber{10000}; \pgfmathprintnumber{1000000}
\end{codeexample}
    %
    \noindent A value of |0| disables this feature (negative values are
    ignored).
\end{key}

\begin{key}{/pgf/number format/use period}
    A predefined style which installs periods ``\texttt{.}'' as decimal
    separators and commas ``\texttt{,}'' as thousands separators. This style is
    the default.
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,fixed,precision=2,use period}
\pgfmathprintnumber{12.3456}
\end{codeexample}
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,fixed,precision=2,use period}
\pgfmathprintnumber{1234.56}
\end{codeexample}
    %
\end{key}

\begin{key}{/pgf/number format/use comma}
    A predefined style which installs commas ``\texttt{,}'' as decimal
    separators and periods ``\texttt{.}'' as thousands separators.
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,fixed,precision=2,use comma}
\pgfmathprintnumber{12.3456}
\end{codeexample}
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,fixed,precision=2,use comma}
\pgfmathprintnumber{1234.56}
\end{codeexample}
    %
\end{key}

\begin{key}{/pgf/number format/skip 0.=\marg{boolean} (initially false)}
    Configures whether numbers like $0.1$ shall be typeset as $.1$ or not.
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,
    fixed,
    fixed zerofill,precision=2,
    skip 0.}
\pgfmathprintnumber{0.56}
\end{codeexample}
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,
    fixed,
    fixed zerofill,precision=2,
    skip 0.=false}
\pgfmathprintnumber{0.56}
\end{codeexample}
    %
\end{key}

\begin{key}{/pgf/number format/showpos=\marg{boolean} (initially false)}
    Enables or disables the display of plus signs for non-negative numbers.
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/showpos}
\pgfmathprintnumber{12.345}
\end{codeexample}

\begin{codeexample}[]
\pgfkeys{/pgf/number format/showpos=false}
\pgfmathprintnumber{12.345}
\end{codeexample}

\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,showpos,sci}
\pgfmathprintnumber{12.345}
\end{codeexample}
    %
\end{key}

\begin{stylekey}{/pgf/number format/print sign=\marg{boolean}}
    A style which is simply an alias for |showpos=|\marg{boolean}.
\end{stylekey}

\begin{key}{/pgf/number format/sci 10e}
    Uses $m \cdot 10^e$ for any number displayed in scientific format.
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,sci,sci 10e}
\pgfmathprintnumber{12.345}
\end{codeexample}
    %
\end{key}

\begin{key}{/pgf/number format/sci 10\textasciicircum e}
    The same as `|sci 10e|'.
\end{key}

\begin{key}{/pgf/number format/sci e}
    Uses the `$1e{+}0$' format which is generated by common scientific tools
    for any number displayed in scientific format.
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,sci,sci e}
\pgfmathprintnumber{12.345}
\end{codeexample}
    %
\end{key}

\begin{key}{/pgf/number format/sci E}
    The same with an uppercase `\texttt{E}'.
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,sci,sci E}
\pgfmathprintnumber{12.345}
\end{codeexample}
    %
\end{key}

\begin{key}{/pgf/number format/sci subscript}
    Typesets the exponent as subscript for any number displayed in scientific
    format. This style requires very little space.
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,sci,sci subscript}
\pgfmathprintnumber{12.345}
\end{codeexample}
    %
\end{key}

\begin{key}{/pgf/number format/sci superscript}
    Typesets the exponent as superscript for any number displayed in scientific
    format. This style requires very little space.
    %
\begin{codeexample}[]
\pgfkeys{/pgf/number format/.cd,sci,sci superscript}
\pgfmathprintnumber{12.345}
\end{codeexample}
    %
\end{key}

\begin{key}{/pgf/number format/sci generic=\marg{keys}}
    Allows to define an own number style for the scientific format. Here,
    \meta{keys} can be one of the following choices (omit the long key prefix):

    \begin{key}{/pgf/number format/sci generic/mantissa sep=\marg{text} (initially empty)}
        Provides the separator between a mantissa and the exponent. It might be
        |\cdot|, for example,
    \end{key}
    %
    \begin{key}{/pgf/number format/sci generic/exponent=\marg{text} (initially empty)}
        Provides text to format the exponent. The actual exponent is available
        as argument |#1| (see below).
    \end{key}
    %
\begin{codeexample}[]
\pgfkeys{
    /pgf/number format/.cd,
    sci,
    sci generic={mantissa sep=\times,exponent={10^{#1}}}}
\pgfmathprintnumber{12.345};
\pgfmathprintnumber{0.00012345}
\end{codeexample}
    %
    The \meta{keys} can depend on three parameters, namely on |#1| which is the
    exponent, |#2| containing the flags entity of the floating point number and
    |#3| is the (unprocessed and unformatted) mantissa.

    Note that |sci generic| is \emph{not} suitable to modify the appearance of
    fixed point numbers, nor can it be used to format the mantissa (which is
    typeset like fixed point numbers). Use |dec sep|, |1000 sep| and
    |print sign| to customize the mantissa.
\end{key}

\begin{key}{/pgf/number format/retain unit mantissa=\mchoice{true,false} (initially true)}
    Allows to omit a unit mantissa.
    %
\begin{codeexample}[]
\pgfkeys{
    /pgf/number format/.cd,
    sci, retain unit mantissa=false}
\pgfmathprintnumber{10.5};
\pgfmathprintnumber{10};
\pgfmathprintnumber{1010};
\pgfmathprintnumber[precision=1]{-1010};
\end{codeexample}
    %
    The feature is applied after rounding to the desired precision: if the
    remaining mantissa is equal to~$1$, it will be omitted. It applies to all
    styles involving the scientific format (including |std|).
\end{key}

\begin{key}{/pgf/number format/\protect\atmarktext dec sep mark=\marg{text}}
    Will be placed right before the place where a decimal separator belongs to.
    However, \marg{text} will be inserted even if there is no decimal
    separator. It is intended as place-holder for auxiliary routines to find
    alignment positions.

    This key should never be used to change the decimal separator!
    Use |dec sep| instead.
\end{key}

\begin{key}{/pgf/number format/\protect\atmarktext sci exponent mark=\marg{text}}
    Will be placed right before exponents in scientific notation. It is
    intended as place-holder for auxiliary routines to find alignment
    positions.

    This key should never be used to change the exponent!
\end{key}

\begin{key}{/pgf/number format/assume math mode=\marg{boolean} (default true)}
    Set this to |true| if you don't want any checks for math mode. The initial
    setting checks whether math mode is active using |\pgfutilensuremath| for
    each final number.

    Use |assume math mode=true| if you know that math mode is active. In that
    case, the final number is typeset as-is, no further checking is performed.
\end{key}

\begin{stylekey}{/pgf/number format/verbatim}
    A style which configures the number printer to produce verbatim text
    output, i.e., it doesn't contain \TeX\ macros.
    %
\begin{codeexample}[preamble={\usetikzlibrary{fpu}}]
\pgfkeys{
    /pgf/fpu,
    /pgf/number format/.cd,
    sci,
    verbatim}
\pgfmathprintnumber{12.345};
\pgfmathprintnumber{0.00012345};
\pgfmathparse{exp(15)}
\pgfmathprintnumber{\pgfmathresult}
\end{codeexample}
    %
    The style resets |1000 sep|, |dec sep|, |print sign|, |skip 0.| and sets
    |assume math mode|. Furthermore, it installs a |sci generic| format for
    verbatim output of scientific numbers.

    However, it will still respect |precision|, |fixed zerofill|,
    |sci zerofill| and the overall styles |fixed|, |sci|, |int detect| (and
    their variants). It might be useful if you intend to write output files.
\end{stylekey}

%--------------------------------------------------
% \subsubsection{Defining own display styles}
% You can define own display styles, although this may require some insight into \TeX-programming. Here are two examples:
% \begin{enumerate}
%     \item A new fixed point display style: The following code defines a new style named `\texttt{my own fixed point style}' which uses $1{\cdot}00$ instead of $1.00$.
% \begin{lstlisting}
% \def\myfixedpointstyleimpl#1.#2\relax{%
%     #1{\cdot}#2%
% }%
% \def\myfixedpointstyle#1{%
%     \pgfutilensuremath{%
%     \ifpgfmathfloatroundhasperiod
%         \expandafter\myfixedpointstyleimpl#1\relax
%     \else
%         #1%
%     \fi
%     }%
% }
% \pgfkeys{/my own fixed point style/.code={%
%     \let\pgfmathprintnumber@fixed@style=\myfixedpointstyle}
% }%
% \end{lstlisting}
%     You only need to overwrite the macro \lstinline!\pgfmathprintnumber@fixed@style!. This macro takes one argument (the result of any numerical computations). The \TeX-boolean \lstinline!\ifpgfmathfloatroundhasperiod! is true if and only if the input number contains a period.
%
%     \item An example for a new scientific display style:
% \begin{lstlisting}
% % #1:
% %         0 == '0' (the number is +- 0.0),
% %         1 == '+',
% %         2 == '-',
% %         3 == 'not a number'
% %         4 == '+ infinity'
% %         5 == '- infinity'
% % #2: the mantissa
% % #3: the exponent
% \def\myscistyle#1#2e#3\relax{%
%     ...
% }
% \pgfkeys{/my own sci style/.code={%
%     \let\pgfmathfloatrounddisplaystyle=\myscistyle},
% }%
% \end{lstlisting}
% \end{enumerate}
%--------------------------------------------------


\documentclass[a4paper, 11pt]{article}
\usepackage[margin=2cm]{geometry}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{multirow}
\usepackage{amsmath,amsfonts,amssymb,amsthm}

\setlength{\parindent}{0em}

\title{\textbf{COMP90056 Assignment A Report}}
\author{Tingsheng (Tinson) Lai (731319)}
\date{2019}

\begin{document}
    \maketitle
    \section{Introduction}
        This report is a summary of the implementation and experimentation taken in the second assignment. It majorly focuses on two techniques, $\ell_{0}$ sampling and sparse recovery, which are also mutually interrelated. The prior, $\ell_{0}$ sampler, is an essential sampler used in a stream with biased distribution to assign even probability to each item presented in the stream. Another important use case is when the stream is neither an insertion stream nor even a strict turnstile\footnote{In the remaining part of the report, unless otherwise indicated, a turnstile stream is always strict in any circumstances.} stream but a general stream. Sparse vector recovery is the fundamental technique embedded in the sampler to handle complex models of streaming data. It can achieve a high success rate for recovering a sparse vector with a minor amount of memory.
    \section{Theory \& Implementation}
        \subsection{Sparse Recovery}
            \subsubsection{1-Sparse Recovery}
                Starting from 1-sparse recovery, this is a data structure used to recover a 1-sparse vector with only three integers. A $k$-sparse vector, by definition, is a vector containing exactly $k$ element(s) in the vector with non-zero frequency. The traditional technique requires a hash map to summarise the frequency for each item to determine if a given stream forms a 1-sparse vector. It becomes infeasible in a space-hungry setting to process streams with large varieties of items, and this technique is extremely slow as well. Instead, we only need to record three numbers, which are:
                \begin{itemize}
                    \item $w_{1} = \sum^{n}_{i = 1} f_{i} = F_{1}$, where $f_{i}$ is the sum of all updates retrieved from the stream for item $i$
                    \item $w_{2} = \sum^{n}_{i = 1} i \cdot f_{i}$
                    \item $w_{3} = \left( \sum^{n}_{i = 1} q^{i} \cdot f_{i} \right) \textit{ mod } p$, where $p$ is a large prime number and $q$ is a random number between $\left[ 2,p \right]$\footnote{This differs to what the note says but setting $q$ to 0 or 1 is pointless, thus the implementation deliberately ignore these two candidates.}
                \end{itemize}
                The update process is fast as it only needs integer computations. It also saves space for the process as the structure needs merely three integers. One of the major functionalities of this structure is to identify and distinguish cases when the sparsity $s$ of the stream are:
                \begin{itemize}
                    \item $s = 0 \Rightarrow w_{1} = 0 \wedge w_{2} = 0 \wedge w_{3} = 0$, but it is not always true for the reverse. However, the exact non-zero integer solution to simultaneously solve these three equations is very rare, especially when $q$ is extensively large. Hence it is safe to claim that the reverse is correct for most of the time. This claim is proven later with the experiments. An implicit fact here is that $w_{1} = 0 \wedge \left( w_{2} \neq 0 \vee w_{3} \neq 0 \right) \Rightarrow s \geq 2$. This fact is critical to handle general streams.
                    \item $s = 1 \Rightarrow w_{2} \textit{ mod } w_{1} = 0 \wedge w_{3} = w_{1} \cdot q^{w_{2} / w_{1}} \textit{ mod } p$, similarly, does not always hold for the reverse. Letting $q$ be a prime number can mitigate the collision of numbers caused by modulus operator. Technically, however, randomly selecting a prime number in a range involves multiple complicated computations and verifications, and the complexity grows polynomially. In practice, a combination of large $q$ and $p$ will be sufficient to handle most of the circumstances, and experiments also support this at later stages. Meanwhile, based on the fact that $i \in \mathbb{Z}^{+}$, $w_{1}$ and $w_{2}$ must have the same sign even if the incoming data is from a general stream.
                    \item $s \geq 2$, when the structure does not meet the conditions listed previously.
                \end{itemize}
                Given the assumption that all arithmetic operations can be completed in constant time\footnote{May not be applicable to multiplication, division and modulus. An explicit improvement for the modulus computation of mersenne prime is to simplify it into a constant number of bit operations, which is achieved in the implementation.}, the computation complexity of $_{1}$ and $w_{2}$ is constant. For $w_{3}$, a recursive divide and conquer approach to compute the exponentiation can reduce the theoretical complexity to $O \left( \log{n} \right)$ but recursions incur unnecessary overhead. An elegant workaround is to introduce an accumulator and square itself each time until the exponent reaches the nearest power of 2 smaller than the index, then repeat this process for the remaining exponent. Both of these techniques achieve a theoretical upper bound of computation complexity $O \left( \log{n} \right)$.

                Modulus operator has two properties heavily used in the implementation to avoid problems caused by integer overflow in low-level languages, which are:
                \begin{itemize}
                    \item $\left( a + b \right) \textit{ mod } p = \left( a \textit{ mod } p + b \textit{ mod } p \right) \textit{ mod } p$
                    \item $\left( a \cdot b \right) \textit{ mod } p = \left( a \textit{ mod } p \cdot b \textit{ mod } p \right) \textit{ mod } p$
                \end{itemize}
            \subsubsection{$k$-Sparse Recovery}
                Building on top of the 1-sparse recovery, multiple (precisely, $k$) 1-sparse recovery structures are incorporated into a single structure using a technique similar to the count-min sketch as discussed in the previous assignment. The $\delta$ parameter controls the expected maximum error rate in the output. The yielded results in experiments prove this parameter to be a precise estimation of the error rate. The theoretical bound of complexity is $\Omega \left( k \cdot \log{\delta} \cdot \log{n} \right)$\footnote{The computation time of hash function was not considered in this complexity, so it is a lower bound}.
        \subsection{$\ell_{0}$ Sampler}
            Using a $k$-wise independent hash family to approximate the $\epsilon$-min hash family is a simple but appropriate workaround as described in \cite{INDYK200184}. The simplicity of insertion streams reduces the complexity of implementation of the sampler. The uniformity of $k$-wise independent hash function guarantees the sample from the stream is uniformly distributed.

            In a more flexible streaming model where negative updates exist, simple techniques for the insertion stream is not suitable as it does not support subtraction. The previously defined $s$-sparse structure came to the rescue. Multiple levels of $s$-sparse structures are combined with a hash function to incorporate both uniformity and error tolerance into the sampler to handle strict turnstile streams or even general streams. The hash function is expected to distribute the items sampled from the stream onto different levels based on the returned hash value. Causing an $\ell_{0}$ sampler to fail requires at least fulfilling the $s$-sparse structure at the highest level. The sampler can still accommodate data from streams with higher sparsity than expected.
    \section{Experiment}
        \subsection{$\ell_{0}$ Sampler}
            The experiment used a uniform distribution alongside with a binomial distribution to test if the sampler assigned equal priority to each item during sampling. From the graphs below, it is clear that the $\ell_{0}$ sampler did not follow the original distribution to select items. The binomial distribution was simulating a heavy hitter for the items centred around the mean success rate of the distribution. The selection of items was not entirely uniform due to collisions incurred by hashing. Based on the observation that the sampler did not favour the items with higher weights in the centre of the second graph, it is fair to claim that the $\ell_{0}$ sampler works as it was initially designed.
            \begin{center}
                \includegraphics[scale=0.5]{graphs/a/1-Uniform.jpg}
                \includegraphics[scale=0.5]{graphs/a/1-Binomial.jpg}
            \end{center}
            For $\ell_{0}$ sampler performance in a turnstile or general stream, as the stream generated has an extremly large range, $\left[ 1,2^{16}-1 \right]$ but the total stream cannot be too long\footnote{The execution time is too long even for one update.}, the graph generated is trivial to see. However, an interesting observation from the results is that there is no failure during sampling even the sparsity $s$ grows to approximately $2k$ in the sampler.
        \subsection{Sparse Recovery}
            \subsubsection{1-Sparse Recovery}
                A sketch of execution result for 1-sparse structure under different sparsity in different streams with different sizes of unique items. Repeating this process did not cause oscillations in results, so randomly picking a output result is still representative.
                \begin{table}[h]
                    \centering
                    \begin{tabular}{|c|c|c|c|c|c|c|c|c|}
                    \hline
                                                     &                    & $s = 0$ & $s = 1$ & $s = 2$ & $s = 3$ & $s = 4$ & $s = 5$ & $s = 6$ \\ \hline
                    \multirow{2}{*}{$unique = 500$}  & \textbf{Turnstile} & 100     & 100     & 100     & 100     & 100     & 100     & 100     \\ \cline{2-9}
                                                     & \textbf{General}   & 100     & 99      & 100     & 100     & 100     & 99      & 99      \\ \hline
                    \multirow{2}{*}{$unique = 5000$} & \textbf{Turnstile} & 100     & 99      & 99      & 100     & 100     & 100     & 100     \\ \cline{2-9}
                                                     & \textbf{General}   & 100     & 99      & 100     & 99      & 100     & 100     & 99      \\ \hline
                    \end{tabular}
                \end{table}
            \subsubsection{$k$-Sparse Recovery}
                Similar to the previous table, the results did not vary too far in a sequence of consecutive executions. This is the lowest output picked from the results. It is a little bit confusing that the its ability to handle general streams is even better than strict turnstile stream. One of the possible justification is that a strict turnstile stream will always end up with non-negative frequencies for all numbers. Since a bigger number is likely to be factorised into more numbers which increases the probability of collisions, the difficulty of distinguishing between $k$-sparse and $(k+1)$-sparse grows significantly.
                \begin{table}[h]
                    \centering
                    \begin{tabular}{|c|c|c|c|c|c|c|c|c|}
                    \hline
                                                     &                    & $s = 0$ & $s = 1$ & $s = 2$ & $s = 3$ & $s = 4$ & $s = 5$ & $s = 6$ \\ \hline
                    \multirow{2}{*}{$unique = 500$}  & \textbf{Turnstile} & 100     & 98      & 94      & 70      & 90      & 95      & 100     \\ \cline{2-9}
                                                     & \textbf{General}   & 100     & 99      & 95      & 88      & 92      & 99      & 99      \\ \hline
                    \multirow{2}{*}{$unique = 5000$} & \textbf{Turnstile} & 100     & 99      & 92      & 68      & 75      & 94      & 99      \\ \cline{2-9}
                                                     & \textbf{General}   & 99      & 100     & 94      & 90      & 92      & 97      & 100     \\ \hline
                    \end{tabular}
                \end{table}
    \section{Future Improvement}
        It worths to see how pairwise independence improved the $\ell_{0}$ sampling metioned in \cite{Cormode:2014:UFA:2631033.2631080}. The initial design of the $\ell_{0}$ sampler uses an $\epsilon$-min-wise independent hash family, but the parameter $\epsilon$ was not considered in the generation of $k$-wise independent hash functions.
    \bibliographystyle{ieeetr}
    \bibliography{main}
\end{document}


\documentclass[9pt,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage{fourier}
\usepackage{latexsym}
\usepackage{amsmath}
\usepackage{ragged2e}
\usepackage{relsize}
\usepackage{multicol}
%\usepackage{xr}

\usepackage{siunitx}

%%%%%%%%%%%%%%%%
%
%        TRIANGLE OF POWER
%
% https://tex.stackexchange.com/questions/307833/how-to-represent-the-triangle-of-power-in-latex
%
%%%%%%%%%%%%%%%

\newcommand{\dotriangle}[1]{%
  \raisebox{-.7ex}{$\vcenter{#1\kern.2ex\hbox{$\triangle$}\kern.2ex}$}%
}

\newcommand{\tripow}[3]{% Syntax: \tripow{#1}{#2}{#3} gives you #1 ^ {#2} = #3
   \mathop{% We want it to an operator
      \mathchoice% We want different functionality in text and display mode
         {% DISPLAY MODE
            \vphantom{\dotriangle\LARGE}% \vphantom off-set: places the bottom entries.
            \rule[-1.4ex]{0.1em}{0pt}% Syntax: [<vetical drop #1>]{<left margin>}{<Should be 0>}
            _{\scriptstyle #1}% style of #1 entry
            {\overset{\scriptstyle #2}% style of #2 entry
            {\dotriangle\LARGE}}% Size of the displayed operator - should match the \vphantom off-set.
            \rule[-1.4ex]{0em}{0pt}% Syntax: [<vetical drop #3>]{<Should be 0>}{<Should be 0>}
            _{\scriptstyle #3}% style of #3 entry
            \rule[0ex]{0.1em}{0pt}% Syntax: [<Should be 0>]{<right margin>}{<Should be 0>}
          }%
        {% TEXT MODE
            \vphantom{\dotriangle\normalsize}%
            \rule[-1.05ex]{-0.7ex}{0pt}%
            _{#1}%
            \overset{#2}%
            {\dotriangle\normalsize}% size in text mode
            \rule[-1.05ex]{0pt}{0pt}%
            _{#3}%
            \rule[0ex]{-0.2em}{0pt}%
          }%
        {% SCRIPT MODE
            \vphantom{\dotriangle\normalsize}%
            \rule[-1.05ex]{-0.8ex}{0pt}%
            _{\scriptstyle #1}%
            {\overset{\scriptstyle #2}%
            {\dotriangle\normalsize}}% size in script mode
            \rule[-1.05ex]{0pt}{0pt}%
            _{\scriptstyle #3}%
            \rule[0ex]{-0.3em}{0pt}%
         }%
        {}% SCRIPTSCRIPT MODE
   }%
}

%Check if we are compiling under latex or pdflatex
\usepackage{ifpdf}
\ifpdf
  \usepackage[pdftex]{graphicx}
\else
  \usepackage[dvips]{graphicx}
\fi

\usepackage{pdfpages}

% Set the margins
  \setlength{\marginparwidth}{0pt}
  \setlength{\marginparsep}{0pt}
  \setlength{\oddsidemargin}{0in}
  \setlength{\evensidemargin}{0in}
  \setlength{\textwidth}{6.5in}
  \setlength{\topmargin}{0in}
  \setlength{\headheight}{0.125in}
  \setlength{\headsep}{0.25in}
  \setlength{\textheight}{8.75in}
  \setlength{\footskip}{0.35in}
%  \setlength{\headwidth}{\textwidth}
  \raggedbottom

% This package sets the page header style
\usepackage{calc}
\usepackage{fancyhdr}
  \fancyhf{}
  \pagestyle{fancy}
  \fancyheadoffset[LE,RO]{\marginparsep+\marginparwidth}
  \fancyhead[LE,RO]{\bfseries\thepage}
  \fancyhead[LO]{\bfseries\rightmark}
  \fancyhead[RE]{\bfseries\leftmark}
  \fancyfoot[LO,RE]{\scriptsize{Duplication or distribution in any form, electronic or otherwise, is strictly prohibited.}}
  \renewcommand{\chaptermark}[1]{\markboth{Solutions}{\chaptername \ \thechapter \ \ #1}}

\usepackage[labelfont=bf,labelsep=none]{caption}
 \DeclareCaptionFormat{suggested}{{\small #1#2 #3}}
 \captionsetup{format=suggested}

% This package makes hyperlinks for the index and bookmarks for the pdf
\usepackage{xr-hyper}
\usepackage[bookmarksnumbered,pdfpagelabels=true,plainpages=false,pdfstartview=FitH,colorlinks=true,linkcolor=black,citecolor=black,urlcolor=blue]{hyperref}
\externaldocument{main}

\renewcommand\theequation{\arabic{equation}}

\allowdisplaybreaks

\usepackage{tikz}

\begin{document}
 \frontmatter

 \chapter*{Solutions for \emph{Elementary Mathematical Analysis}}

 You'll need to create a new file for the solution of each problem in the
 solutions subdirectory.  Also, if you rename your main file,
 change TextbookExample in this document (two places) to your new name.

 \cleardoublepage

 \addcontentsline{toc}{chapter}{Table of Contents}
 \setcounter{tocdepth}{1}
 \tableofcontents

 \mainmatter
 \pagestyle{fancy}

%\setcounter{chapter}{-1}

\chapter{Solutions}
\renewcommand{\theenumi}{\alph{enumi}}
\begin{multicols}{2}
\input{main.sln}
\end{multicols}

\end{document}