% This is a model template for the solutions in computational science. You can find a very useful documentation for LaTeX in Finnish at ftp://ftp.funet.fi/pub/TeX/CTAN/info/lshort/finnish/ or in English at ftp://ftp.funet.fi/pub/TeX/CTAN/info/lshort/english/. The section List of mathematical symbols in Chapter 3 is especially useful for the typesetting of mathematical formulas.

% Compile the document to PDF by command 'pdflatex model.tex' in the terminal. The command must be run twice for the references in the text to be correct.

\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
% This includes letters such as � and �
\usepackage[T1]{fontenc}
% Use here 'Finnish' for Finnish hyphenation. You may have to compile the code twice after the change.
\usepackage[english]{babel}
\usepackage{graphicx}
% Some math stuff
\usepackage{amsmath,amsfonts,amssymb,amsbsy,commath,booktabs,hyperref}
% This is just to include the urls
\usepackage{hyperref,subcaption,verbatim}
\usepackage[margin=2cm]{geometry}

\setlength{\parindent}{0mm}
\setlength{\parskip}{1.0\baselineskip}
\usepackage{minted}
\usemintedstyle{tango}

\usepackage{listings}
\usepackage{color}
\usepackage{pdfpages}
\DeclareMathAlphabet{\pazocal}{OMS}{zplm}{m}{n}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\lstset{frame=tb,
	language=Python,
	aboveskip=3mm,
	belowskip=3mm,
	showstringspaces=false,
	columns=flexible,
	basicstyle={\tiny\ttfamily},
	numbers=none,
	numberstyle=\tiny\color{gray},
	keywordstyle=\color{blue},
	commentstyle=\color{dkgreen},
	stringstyle=\color{mauve},
	breaklines=true,
	breakatwhitespace=true,
	tabsize=4
}

\begin{document}

\title{CS-E4830 Kernel Methods in Machine Learning \\ Assignment 3} % Replace the exercise round number
\author{Kunal Ghosh, 546247} % Replace with your name and student number
\maketitle
\section*{}
\includepdf[pages=-]{assignment3q1n2-1.pdf}
\section*{Solution to Question 3(a)}
Implementation of the \texttt{Stochastic Dual Coordinate Ascent for SVM} as described in the Assignment handout.
\inputminted[baselinestretch=1, fontsize=\small, breaklines=true, mathescape]{python}{../svmTrainDCGA2.py}
\section*{Solution to Question 3(b)}
In this section we have used the Quadratic program solver of the \texttt{CVXOPT} package in python, to solve
dual soft-margin SVM (equation 1 as given in the assignment handout).
\inputminted[baselinestretch=1, fontsize=\small, breaklines=true, mathescape]{python}{../svmTrainQP.py}
\section*{Solution to Question 3(c)}
We have used the following code snippet to run the \texttt{svmTrainDCGA} and \texttt{svmTrainQP} for different amounts
of training data. It also contains the code to time the execution, calculate prediction accuracy and generate plots.
The plot thus generated are discussed in the next section.
\inputminted[baselinestretch=1, fontsize=\small, breaklines=true, mathescape]{python}{../Assignment3.py}

\section*{Solution to Question 3(d)}
\begin{figure}[ht]
    \centering
    \includegraphics[width=1.1\linewidth]{figure_11.png}
    \label{fig:f11}
    \caption{\textit{This figure shows the test accuracy in percentage as a function of the training dataset size. It can be seen that the \texttt{Stochastic Dual Coordinate Ascent (DCGA)}
    algorithm performs better than the \texttt{Quadratic Program} solver for the full dataset but has lower accuracy for small dataset size. \textbf{Note!} that the same trend wasn't visible over multiple runs (see Figure 2 for another run). However, for the full dataset Stochastic DCGA did perform better than the QP version consistently.
    }}
\end{figure}
\begin{figure}[ht]
    \centering
    \includegraphics[width=1.1\linewidth]{figure_12.png}
    \label{fig:f12}
    \caption{\textit{This figure shows the test accuracy in percentage as a function of the training dataset size. This is an alternate run of the same code used to generate Figure 1, it is clear that \texttt{Stochastic DCGA} performs bettern than the \texttt{QP} for a given fixed dataset (all 3000 datapoints) but one cannot make the same conclusion for smaller dataset sizes as there is quite a lot of variance in the accuracy as is seen by comparing this figure with Figure 1.}}
\end{figure}

\begin{figure}[ht]
    \centering
    \includegraphics[width=1.1\linewidth]{runtime.png}
    \label{fig:f2}
    \caption{\textit{This figure shows the execution time (seconds), lower is better, as a function of the number of training datapoints. It is quite clear that the Quadratic programming \texttt{(QP)} solution is faster for small to moderate dataset sizes as compared to the \texttt{Stochastic DCGA} implementation. However as the dataset size increases \texttt{Stochastic DCGA} starts to run much quicker than the \texttt{QP}.}}
\end{figure}

\end{document}


\section{\texorpdfstring{Auction protocol, attacks, Security level}{Auction protocol, attacks, Security level}}
\vspace{5mm}
\large
\begin{observation}\label{random_bit}
	Any fixed bit XOR random bit (uncorrelated with fixed bit) produces random bit.
\end{observation}

\begin{example}
	\textbf{Bit commitment protocol}

	Toss a coin over a phone call. We assume, that 2 parties communicate over some line.
	They want to generate a random bit, however they do not trust each other.

	If Alice sends her random bit to Bob, he send his random bit back, then XOR should produce random bit. However, Bob could cheat, and choose bit according to his need.

	If Alice generates a random bit, then receives Bob's random bit, then she could cheat and change her bit in the process. Similarly for Bob.

	So, we need better approach. Alice generates random word of fixed length, let's say 128 bit. $x \in \{ 0, 1 \} ^{128}$. Alice sends $h(x)$. Bob sends his bit $b$.

	Then Alice send first bit of X as her random bit. Both do XOR the bits.
	The communication protocol ensures that Bob can verify Alice's random string by computing the hash.

	X should be long, otherwise Bob could brute force to check all possible hashes.
\end{example}

\begin{observation}
	In many communication protocols hash functions and RNG are more important than ciphers.
\end{observation}

\begin{example}
	Alice wants to participate in the auction. However, she could not do so in person. She chooses Bob as her representative.

	An auction is public and it has a broadcast which cannot be changed by active attacker. Since public will notice.

	Alice send 2 messages: Stop, Add <money>.

	In every protocol design we should consider:
	\begin{enumerate}
		\item Against whom we are defending
		\item For how long information should be secret.
	\end{enumerate}

	In the auction example, information should be secret for a couple of minutes maximum.
	However, Alices actions should be kept secret before Bob acts on the auction. Otherwise other participants could do a perfect counter move.

	A symmetric cipher will be used. We assume, that Alice and Bob met in person before the auction and exchanged the key.

	Problems of the Protocol and the possible solutions:
\begin{enumerate}

	\item If the messages use the same encryption key several times or, length of messages is the same, Eve could distinguish Stop from Add.
	Therefore, an \textbf{nonce} should be added to the message in order to make them the same length and prevent attacker from guessing using the length.

	\item The difference in length between Stop and Add. To fix this, we add padding to every message making all messages the same fixed length.

	\item Eve could sniff the message and check what were Bob's bid on the auction. Later she could use this message pretending to be Alice. Usually called \textbf{Replay attack}.
	In order to protect the communication from replay attack, Alice will add a sequence number of the current message (monotonically increasing sequence).
	If Bob receives the same sequence number twice, he considers it as an attack.

	\item In practice, Eve could disable communication line for some time and resend all messages later. Timestamp could be a solution, however it requires a sync clock between parties. Which is hard to do secretly.

	An alternative solution would be to add NOP (no operation) message and send messages periodically.

	\item Mallory can randomly change bit in the Cipher text. For many good ciphers, altering one bit in the cipher text will result in 1 bit change in plain text.
		To prevent that Alice should sign the message using \textbf{Message Authentication Code (MAC)}. The basic example of MAC creates $h(k_s | E_k(x))$, where $k_s$ is a new symmetric key.

		As a result, hash of the message no longer match if Mallory changed some bits.
	\item If Alice and Bob use same keys for the 2nd time, Eve could replay messages from the previous session. To prevent that they can use a different pair of keys every time.
		Alternatively, a \textbf{session Id} could be added to the message. Should be a secret, known only to Alice and Bob. We can also choose session id as nonce and make it a parameter of MAC.

	\end{enumerate}
\end{example}

\begin{observation}
	Every part of the protocol could be attacked. Protocol is as strong as the weakest part.
\end{observation}

\begin{definition}
	Authenticity - message was not changed during transfer.

	Secrecy - no one decoded the plain text of the message.

	In almost all cases, we want to combine both properties.
\end{definition}

\begin{observation}
	Changing the encryption key for every message is generally no a good idea for several reasons.
	First of all, an attacker has much smaller set of keys to check (entropy reduction).
	Secondly, many RNGs require some hear up. For example, an RNG has to precompute some values.

	We can however, derive key from single Master key. Later we will prove that adding \textbf{nonce} is as strong as changing the key.
	Frequent change of keys makes protocol harder to analyze.
\end{observation}

\subsection{Types of attacks}

\begin{example}
	Known cipher text - an attacker knows cipher text and wants to recover the plain text from it.
\end{example}

\begin{example}
	Known plain text - an attacker knows cipher text and plain text. He wants to recover the Key from them. Nonce are still not known, so only part of the plain text is known.
\end{example}

\begin{example}
	Chosen plain text - an attacker chooses plain text and gives it to the victim for encryption. Goal is again to recover the key.
\end{example}

\begin{example}
	Distinguishing attack - e.g. distinguish Stop and Add commands in the Auction protocol (by the length or other property of the cypher text).
\end{example}

How to measure strength of the cryptographic primitives. Quantification. For that we define security level (in bits).

\begin{definition}
	Security level - is the number of operations required to break some cryptographic primitive.
	Ideally we want security level to be equal to the key length. Meaning that the attacker has to check all possible keys in order to break the primitive.

	Security level of the good cryptographic primitive should be beyond computational power of any computer on the Earth.
\end{definition}

\begin{example}
	\textbf{Birthday attack} - the security level of the primitive is only half of the expected.

	For example, let's consider Challenge-response auth scheme.
	The nonce used in the protocol has $b$ bits. One could expect the security level to be $2^b$.
	Which is not true, because after $2^{b/2}$ the collision is likely to occur.

	This attack is related to the so called \textbf{Birthday paradox}.
	If there is 23 people in the room, the probability of the same birthday is actually $ \geq 1/2$.
\end{example}

\begin{theorem}[Birthday attack probability]
	The probability of the repetitions in nonces is $e^{-\frac{n^2}{2m}}$.
\end{theorem}
\begin{proof}
	Let's compute the probability that function $f:[n] \to [m]$ that generates nonces is injective. Where $n$ is number of runs, and $m$ is number of nonces.
	Then probability of A is \# of injective functions over all functions
	\[ Pr_f[A] = \frac{m(m-1)(m-2)...(m-n+1)}{m^n}\]
	Because we choose 1 nonce every run, number of available nonces decreases by 1 every run. Then we rewrite the sum as
	\[ \frac{m}{m} \frac{m-1}{m} ... \frac{m-n+1}{m} = 1 (1 - \frac{1}{m}) (1 - \frac{2}{m}) ...\]
	According to Calculus, for small x: $1 - x \approx e^{-x}$.
	\[ Pr_f[A] \approx 1 e^{-\frac{1}{m}} e^{-\frac{2}{m}} ... = e^{-\frac{1 + 2 + ... + n - 1}{m}} = e^{-\frac{n(n-1)}{2m}} \leq e^{-\frac{n^2}{2m}} \]

	For what values of $n$ and $m$ the probability is $\frac{1}{2}$?
	\[ e^{-\frac{n^2}{2m}} = 1/2 \Rightarrow \frac{n^2}{2m} = -2 \ln\left(\frac{1}{2}\right) \approx 1.39 \Rightarrow n^2 \approx m \]
\end{proof}

\begin{consequence}
	Number of runs required to reach $\frac{1}{2}$ probability of collisions is $\sqrt{m}$. Therefore sequrity level of Challenge-response auth scheme is $2^{b/2}$.
\end{consequence}

\begin{example}
	Assume Alice wants to send to Bob new private key, knowing his public asymmetric key $k_{pub}^B$. To do so, she encodes new key with Bobs key
	\[ E_{asym}(k_{new}, k_{pub}^B) \]

	Eve has access to $k_{pub}^B$, so she can guess a key $k_{g}$ and compare the results. By the assumption, $k_{new}$ is random, so guessing 1 key will not work.
	More sophisticated approach is to precompute a huge set of Keys.
	After some time, encrypted Key sent by Alice will be on of the precomputed one by Eve.
	As a result, Eve would have both encrypted and plain text of the Key.
\end{example}

\begin{theorem}[Success of precomputed table]
	Eve requires $tn \approx m$ (where $t$ is size of the precomputed table, $n$ is number of runs, $m$ is size of Keys set) to guess the key selected by Alice.
	In other words, key selected by Alice will be in the precomputed subset by Eve.
\end{theorem}
\begin{proof}
	Let's compute probability that random function $f$ generates a key that would not be in the precomputed subset.
	\[ P_f[f\ avoids \ subset] = (1 - \frac{t}{m})^n \]
	Since probability of $f$ hitting the subset is $\frac{t}{m}$ and key selection are independent.
	By the same approximation as in previous theorem
	\[ P_f[f\ avoids \ subset] = e^{-\frac{-tn}{m}} \Rightarrow tn \approx m \]

	For example $t = n = \sqrt{m}$. Again security level is $\leq$ 1/2 \# of bits.
\end{proof}


% This is part of the TFTB Reference Manual.
% Copyright (C) 1996 CNRS (France) and Rice University (US).
% See the file refguide.tex for copying conditions.

\markright{midpoint}
\section*{\hspace*{-1.6cm} midscomp}

\vspace*{-.4cm}
\hspace*{-1.6cm}\rule[0in]{16.5cm}{.02cm}
\vspace*{.2cm}

{\bf \large \sf Purpose}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
Mid-point construction used in the interference diagram.
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Synopsis}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
[ti,fi] = midpoint(t1,f1,t2,f2,K)
\end{verbatim}
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Description}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
        {\ty midscomp} gives the coordinates in the
        time-frequency plane of the interference-term corresponding to
        the points {\ty (t1,f1)} and {\ty (t2,f2)}, for a distribution in the
        affine class perfectly localized on power-law group-delays of
        the form $t_x(\nu)=t_0+c\ \nu^{K-1}.$ This function is mainly
        called by {\ty  plotsid}.\\

\hspace*{-.5cm}\begin{tabular*}{14cm}{p{1.5cm} p{11cm} c} Name &
Description \\ \hline {\ty t1} & time-coordinate of the first point\\ {\ty
f1} & frequency-coordinate of the first point ($>0$)\\ {\ty t2} &
time-coordinate of the second point\\ {\ty f2} & frequency-coordinate of
the second point ($>0$)\\ {\ty K} & power of the group-delay law. Example
of distributions satisfying this interference construction :\\ &
\hspace*{.5cm} {\ty K = 2} : Wigner-Ville distribution\\ &
\hspace*{.5cm} {\ty K = 1/2} : D-Flandrin distribution\\ & \hspace*{.5cm}
{\ty K = 0 } : Bertrand (unitary) distribution \\ & \hspace*{.5cm} {\ty K =
-1} : Unterberger (active) distribution\\ & \hspace*{.5cm} {\ty K =
Inf} : Margenau-Hill-Rihaczek distribution\\ \hline {\ty ti} &
time-coordinate (abscissa) of the interference-point\\ {\ty fi} &
frequency-coordinate (ordinate) of the interference-point\\ \hline
\end{tabular*}

\end{minipage}
\vspace*{1cm}

{\bf \large \sf Example}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
Here is the locus of the interference terms between two points, for {\ty K} going
from -15 to 15\,:
\begin{verbatim}
         t1=10; f1=0.45; t2=90; f2=0.05; hold on
         for K=-15:15,
          [ti(2*K+31),fi(2*K+31)]=midscomp(t1,f1,t2,f2,K);
         end
\end{verbatim}
\end{minipage}
%\newpage
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
         plot(ti,fi,'g*'); plot(t1,f1,'go'); plot(t2,f2,'go');
         line([t1,t2],[f1,f2]); hold off
         xlabel('Time'); ylabel('Normalized frequency');
\end{verbatim}
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf See Also}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
plotsid.
\end{verbatim}
\end{minipage}


\documentclass{article}

\usepackage[preprint]{./template/neurips_2020}

\usepackage[utf8]{inputenc} % allow utf-8 input
\usepackage[T1]{fontenc}    % use 8-bit T1 fonts
\usepackage{hyperref}       % hyperlinks
\usepackage{url}            % simple URL typesetting
\usepackage{booktabs}       % professional-quality tables
\usepackage{amsfonts}       % blackboard math symbols
\usepackage{nicefrac}       % compact symbols for 1/2, etc.
\usepackage{microtype}      % microtypography
\usepackage{amssymb}
\usepackage{physics}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{quantikz}

\title{Chapter 5: The Quantum Fourier Transform and its Applications}

\author{
  John Martinez \\
  \texttt{john.r.martinez14@gmail.com} \\
}

\begin{document}
\maketitle

\section{The Quantum Fourier Transform and its Applications}

%%% Section 5.1
\subsection{The Quantum Fourier Transform}
The \emph{discrete fourier transform} takes as input a vector of complex numbers
$x_0,\dots,X_{N-1}$ where the length $N$ of the vector is a fixed parameter. It
outputs the transformed data, a vector of complex numbers $y_{0},\dots,y_{N-1}$,
defined by
\begin{center}
  $y_{N} \equiv \frac{1}{\sqrt{N}}\displaystyle\sum_{j=0}^{N-1} x_{j}e^{2\pi i j k/N}$.
\end{center}

The \emph{quantum fourier transform} is exactly the same transformation,
although the conventional notation for the quantum fourier transform is
somewhat different. The quantum fourier transform on an orthonormal basis
$\ket{0},\dots,\ket{N-1}$ is defined to be linear operator with the followin
action on the basis states:
\begin{center}
  $\ket{j} \rightarrow
  \frac{1}{\sqrt{N}}\displaystyle\sum_{k=0}^{N-1}e^{2\pi i j k /N}\ket{k}$.
\end{center}

The action on an arbitrary state maybe written
\begin{center}
  $\displaystyle\sum_{j=0}^{N-1}x_j\ket{j} \rightarrow
  \displaystyle\sum_{k=0}^{N-1}y_k\ket{k}$.
\end{center}

It is not ovbious, but this transformation is a unitary transformation.
\end{document}


\appendix
\chapter{Appendix}

\section{\texorpdfstring{A brief note on the tree $H_{C,1}$}{A brief note on the tree HC1}}
\label{appx:x_plus_1_variant}
A special case of Collatz trees is the graph $H_{C,1}$ -- the $x+1$ variant of $H_C$. In this case, any sequence of successive nodes along the path from $v_n$ down to $v_1$ is strictly monotonically increasing. If we run reverse to the edge direction (towards the root), then of course the node sequence is strictly monotonically decreasing. Figure~\ref{fig:hc1} shows a portion of the graph $H_{C,1}$ starting at its root.

\begin{figure}[H]
	\includegraphics[width=1.00\textwidth]{figures/h_c1.png}
	\caption{Section of the graph $H_{C,1}$ starting at its root (without branches that reflect a subsequence containing the trivial cycle)}
	\label{fig:hc1}
\end{figure}

\section{\texorpdfstring{Algorithm for pruning binary trees $T_{\ge j}$}{Algorithm for pruning binary trees Tj}}
\label{appx:pruning}
Binary trees as introduced in chapter~\ref{ch:binary_tree} inspired by Kleinnijenhuis~\cite{Ref_Kleinnijenhuis_2020a}, \cite{Ref_Kleinnijenhuis_2020b} can iteratively be pruned (the way Kleinnijenhuis originally invented it). Listing~\ref{lst:pruning} implements this pruning algorithm.

\begin{listing}[H]
	\begin{minted}[bgcolor=bg, linenos, framesep=2mm, mathescape, breaklines, tabsize=2]{python}
def shrinkRoot(self):
	self.root = self.root.successors[0]
	self.root.predecessor = None

def prune(self):
	self.shrinkRoot()
	new_prunables = []
	for node in self.prunable_nodes:
		if node.predecessor is not None:
			if len(node.successors) > 1 and len(node.predecessor.successors) > 1:
				node.predecessor.successors[1].successors.append(node.successors[1])
				node.predecessor.successors[1].successors.reverse()
				node.successors[1].predecessor = node.predecessor.successors[1]
				node.successors[1].prunable = True
				new_prunables.append(node.successors[1])
			node.predecessor.successors.remove(node)
			node.predecessor = None
			self.labels.remove(node.label)
	self.prunable_nodes = new_prunables
	return self
	\end{minted}
	\caption{Python function for pruning a binary tree $T_{\ge j}$ \cite{Ref_Sultanow_Github}}
	\label{lst:pruning}
\end{listing}

Listing~\ref{lst:pruning} is only intended to illustrate the pruning algorithm in a simplifying manner. To run pruning on trees with arbitary large numbers, we refer to the professional Python API by Koch \cite{Ref_Koch_Github}.

\section{\texorpdfstring{The sum of reciprocated vertices depending only on $v_1$}{Sum of reciprocated vertices depending only on v1}}
\label{appx:sum_reciprocal_vertices}
One condition deduced from theorem~\ref{theo:1} is the product condition~\ref{eq:condition_max}, which specifies the validity of the cycle-alpha's upper limit. This condition requires the sum $\nicefrac{1}{kv_1}+\nicefrac{1}{kv_2}+\nicefrac{1}{kv_3}+\ldots$ to be limited. In order to formulate this sum independently from the successive vertices $v_2,v_3,\ldots$, we substitute these as follows:

\begin{flalign}
v_1&=v_1\notag\\
v_2&=\frac{kv_1+1}{2^{\alpha_1}}\notag\\
v_3&=\frac{k^2v_1+k+2^{\alpha_1}}{2^{\alpha_1+\alpha_2}}\notag\\
v_4&=\frac{k^3v_1+k^2+k\cdot2^{\alpha_1}+2^{\alpha_1+\alpha_2}}{2^{\alpha_1+\alpha_2+\alpha_3}}\label{eq:sum_v_4}\\
\vdots\notag\\
v_{n+1}&=\frac{k^nv_1+\sum_{j=1}^{n}k^{j-1}2^{\alpha_1+\ldots+\alpha_n-\sum_{l>n-j}\alpha_l}}{2^{\alpha_1+\ldots+\alpha_n}}\label{eq:sum_v_n_plus_1}
\end{flalign}

The sum of the reciprocated vertices can be expressed as a term that depends from $v_1$ and from the number of contracted edges, id est the number of dvisions by two, between two successive vertices $\alpha_1,\alpha_2,\alpha_3,\ldots$:
\begin{equation*}
\sum_{i=1}^{n+1}\frac{1}{kv_i}=\frac{1}{k}\left(\frac{1}{v_1}+\sum_{i=1}^{n}\frac{1}{v_{i+1}}\right)=\frac{1}{k}\left(\frac{1}{v_1}+\sum_{i=1}^{n}\frac{2^{\alpha_1+\ldots+\alpha_i}}{k^iv_1+\sum_{j=1}^{i}k^{j-1}2^{\alpha_1+\ldots+\alpha_n-\sum_{l>i-j}\alpha_l}}\right)
\end{equation*}

\section{\texorpdfstring{The product of reciprocated vertices incremented by one}{The product of reciprocated vertices incremented by one}}
\label{appx:product_formula_depending_v1}
In a similar way to deduce the sum of reciprocated vertices depending only on $v_1$ as performed in \ref{appx:sum_reciprocal_vertices}, we evolve the product formula depending only on $v_1$:
\begin{flalign}
\prod_{i=1}^{n+1}\left(1+\frac{1}{kv_i}\right)&=1+\frac{2^{\alpha_1+\ldots+\alpha_n}+k\cdot2^{\alpha_1+\ldots+\alpha_{n-1}}+\ldots+k^{n-1}\cdot2^{\alpha_1}+k^n}{k^{n+1}v_1}\label{eq:prod_sum_v_n_plus_1}\\
&=1+\frac{2^{\alpha_1+\ldots+\alpha_n}+k\cdot\sum_{j=1}^{i}k^{j-1}2^{\alpha_1+\ldots+\alpha_n-\sum_{l>i-j}\alpha_l}}{k^{n+1}v_1}\label{eq:prod_sum_v_n_plus_1_inserted}\\
&=1+\frac{2^{\alpha_1+\ldots+\alpha_n}+k\cdot\left(v_{n+1}\cdot2^{\alpha_1+\ldots+\alpha_n}-k^nv_1\right)}{k^{n+1}v_1}\notag\\
&=\frac{2^{\alpha_1+\ldots+\alpha_n}\left(1+kv_{n+1}\right)}{k^{n+1}v_1}\label{eq:prod_sum_v_n_plus_1_simplified}
\end{flalign}
We inserted the sum used in equation~\ref{eq:sum_v_n_plus_1} into the above-given equation~\ref{eq:prod_sum_v_n_plus_1} and then obtained equation~\ref{eq:prod_sum_v_n_plus_1_inserted}. Let us divide this product by the last factor and consider the product in the condition for cycle-alpha's upper limit, which iterates to $n$ instead of $n+1$:

\begin{equation}
\label{eq:prod_sum_v_n_simplified}
\prod_{i=1}^{n}\left(1+\frac{1}{kv_i}\right)=\frac{\prod_{i=1}^{n+1}\left(1+\frac{1}{kv_i}\right)}{\frac{kv_{n+1}+1}{kv_{n+1}}}=\frac{2^{\alpha_1+\ldots+\alpha_n}\cancel{\left(1+kv_{n+1}\right)}kv_{n+1}}{k^{n+1}v_1\cancel{\left(kv_{n+1}+1\right)}}=\frac{2^{\alpha_1+\ldots+\alpha_n}v_{n+1}}{k^nv_1}
\end{equation}

The above-shown equation~\ref{eq:prod_sum_v_n_simplified} becomes simplified, when we replaced the numerator by equation~\ref{eq:prod_sum_v_n_plus_1_simplified}. The question which sequence maximizes its last member $v_{n+1}$ ties into the question: Which sequence maximizes the product? The product formula~\ref{eq:prod_sum_v_n_simplified} does not depend from all vertices $v_1,v_2,\ldots,v_n$, it depends only from $2^\alpha=2^{\alpha_1+\ldots+\alpha_n}$, from the first vertex $v_1$ and the final one $v_{n+1}$.

\section{Engel expansions maximize the node $v_{n+1}$}
A sequence $v_{n+1},v_n,\ldots,v_2,v_1$ describing a path in $H_{C,3}$ from $v_{n+1}$ down to $v_1$ allows at most one division by $2$ between two successive nodes. Dividing only once between two successive nodes, maximizes the $v_{n+1}$, but it is not obvious that this maximizes the product contained in condition~\ref{eq:condition_max}. Such a sequence forms the following ascending continued fraction (for the classical case $b=2,k=3$ see \cite[p.~11]{Ref_Laarhoven}):

\begin{equation}
	\label{eq:asc_continued_fraction}
	v_{n+1}=\cfrac{k\cfrac{k\cfrac{k\cfrac{kv_1+1}{b}+1}{b}+1}{b}+1}{b}\dotsb
	=\frac{k^nv_1+\sum_{i=0}^{n-1}k^ib^{n-1-i}}{b^n}
	=\frac{k^nv_1}{b^n}+\frac{k^n-b^n}{(k-b)b^n}
\end{equation}

\par\medskip
The sum of the products of the powers of $k$ and $b$, contained in equation \ref{eq:asc_continued_fraction}, can be simplified by converting this sum into the form $(k-b)
(b^{n-1}+kb^{n-2}+k^2b^{n-3}+\ldots+k^{n-3}b^2+k^{n-2}b+k^{n-1})=k^n-b^n$ as follows:
\begin{flalign*}
k^n-b^n=k^n\left(1-\frac{b^n}{k^n}\right)&=k^n\left(1-\frac{b}{k}\right)\left(1+\frac{b}{k}+\frac{b^2}{k^2}+\ldots+\frac{b^{n-3}}{k^{n-3}}+\frac{b^{n-2}}{k^{n-2}}+\frac{b^{n-1}}{k^{n-1}}\right)=\left(1-\frac{b}{k}\right)\sum_{i=0}^{n-1}k^n\left(\frac{b}{k}\right)^i\\
&=k\cdot\left(1-\frac{b}{k}\right)\cdot k^{n-1}\cdot\left(\frac{b^{n-1}}{k^{n-1}}+\frac{b^{n-2}}{k^{n-2}}+\frac{b^{n-3}}{k^{n-3}}+\ldots+\frac{b^2}{k^2}+\frac{b}{k}+1\right)\\
&=(k-b)
(b^{n-1}+kb^{n-2}+k^2b^{n-3}+\ldots+k^{n-3}b^2+k^{n-2}b+k^{n-1})
\end{flalign*}

\begin{example}
	A concrete example (where $b=2$ and $k=3$) for such a sequence is $v_1=31$, $v_2=47$, $v_3=71$, $v_4=107$, $v_5=161$. And, to follow that example, we can calculate the label of the vertex $v_5$ in a straightforward way:
	\[
	v_5=v_{n+1}=\frac{3^4(31+1)-2^4}{2^4}=161
	\]
	Besides, by choosing a vertex $v_1=2^{n+1}-1$, we are able to infinitely generate sequences each forming an ascending continued fraction. As per equation~\ref{eq:asc_continued_fraction} the last member in this sequence is the odd labeled vertex $v_{n+1}=3^n\cdot2-1$.
\end{example}

\begin{remark}
	Ascending variants of a continued fraction, such as used in equation~\ref{eq:asc_continued_fraction}, shall not be confused with continued fractions as treated for example in \cite{Ref_Moore}, \cite{Ref_Hensley}, \cite{Ref_Borwe_etal}. These ascending continued fractions correspond to the so-called "Engel Expansions" \cite{Ref_Kraaikamp_Wu}.
\end{remark}

\par\noindent
As illustrated below, we can formulate the ascending continued fractions in a generalized fashion, whereas the analogy to \ref{eq:asc_continued_fraction} is given by $b_1=b_2=b_3=b_4=b$ and $a_1=k^0$, $a_2=k^1$, $a_3=k^2$ and $a_4=k^3+k^4v_1$:
\[
\cfrac{a_1+\cfrac{a_2+\cfrac{a_3+\cfrac{a_4}{b_4}}{b_3}}{b_2}}{b_1}\dotsb=\frac{a_1}{b_1}+\frac{a_2}{b_1b_2}+\frac{a_3}{b_1b_2b_3}+\frac{a_4}{b_1b_2b_3b_4}+\cdots
\]

\par\medskip
The generalized equation~\ref{eq:asc_continued_fraction} may be used to compute any of the above-named ascending continued fraction that has $a_i=k^{i-1}$, $b_i=b$ for $i\in\mathbb{N}$ and $a_n=k^{n-1}+k^nv_1$.

\section{Include more divisions by two into an Engel expansion}
\label{sec:include_divisions_engel_expansion}
For calculating the largest possible $v_{n+1}$, we considered so far Engel expansions which contain only $n$ division by two within a Collatz sequence of $n+1$ memebers. In the following we include $m$ additional divisions by two and thus a total of $m+n$ divisions. We look at two corner cases:
\begin{itemize}
	\item the one where we do the additional $m$ divisions by $2$ at the end and
	\item the one where we do these additional divisions at the very beginning.
\end{itemize}

\par\noindent
\textbf{The first case} is our starting point to examine how the swapping a division by two affects the node $v_{n+1}$. For this, let us compare the Engel expansion where we devide by $2^m$ afterwards with one where we divide by $2$ in the penultimate step and by $2^{m-1}$ in last step. One can immediately recognize the following inequality with a mere look:
\[
\cfrac{1+\cfrac{3+\cfrac{3^2+\cfrac{3^3+3^4v_1}{2}}{2}}{2}}{2\cdot2^m}
<
\cfrac{1+\cfrac{3+\cfrac{3^2+\cfrac{3^3+3^4v_1}{2}}{2}}{2\cdot\textcolor{red}{\mathbf{2}}}}{2\cdot2^{m-1}}
\]

To put it simply, in the expansion on the right side of the above-shown inequality we perform one division by two a little bit earlier as we do it in the expansion on the left side of the expansion. Almost all summands of both expansions cancel out each other:

\[
\frac{1}{2\cdot2^m}+\cancel{\frac{3}{2^2\cdot2^m}+\frac{3^2}{2^3\cdot2^m}+\frac{3^3+3^4v_1}{2^4\cdot2^m}}
<
\frac{1}{2\cdot2^{m-1}}+\cancel{\frac{3}{2^2\cdot\textcolor{red}{\mathbf{2}}\cdot2^{m-1}}+\frac{3^2}{2^3\cdot\textcolor{red}{\mathbf{2}}\cdot2^{m-1}}\frac{3^3+3^4v_1}{2^4\cdot\textcolor{red}{\mathbf{2}}\cdot2^{m-1}}}
\]

\par\bigskip\noindent
\textbf{The second case} deals with Engel expansions where we perform that additional $m$ divisions by two as early as possible. The resulting value decreases, when we make a division by two later:
\[
\cfrac{1+\cfrac{3+\cfrac{3^2+\cfrac{3^3+3^4v_1}{2\cdot2^{m-1}}}{2\cdot\textcolor{red}{\mathbf{2}}}}{2}}{2}
<
\cfrac{1+\cfrac{3+\cfrac{3^2+\cfrac{3^3+3^4v_1}{2\cdot2^m}}{2}}{2}}{2}
\]

Also here almost all summands of both Engel expansions, they cancel each other out:

\[
\cancel{\frac{1}{2}+\frac{3}{2^2}}+\frac{3^2}{2^3\cdot\textcolor{red}{\mathbf{2}}}+\cancel{\frac{3^3+3^4v_1}{2^4\cdot\textcolor{red}{\mathbf{2}}\cdot2^{m-1}}}
<
\cancel{\frac{1}{2}+\frac{3}{2^2}}+\frac{3^2}{2^3}+\cancel{\frac{3^3+3^4v_1}{2^4\cdot2^m}}
\]

\par\medskip
While the first case minimizes the value of the node $v_{n+1}$, the second case maximizes it. The difference between the maximum and the minimum is given by the following equation:

\begin{flalign*}
	&\frac{3^{n-1}\left(\frac{3v_1+1}{2\cdot2^m}+1\right)-2^{n-1}}{2^{n-1}}-\frac{3^n\left(v_1+1\right)-2^n}{2^{n+m}}\\
	=&\frac{3^{n-1}\cdot\left(3v_1+1+2^{m+1}\right)-2^{n-1}\cdot2^{m+1}-3^n\left(v_1+1\right)+2^n}{2^{m+1}\cdot2^{n-1}}\\
	=&\frac{3^{n-1}+3^{n-1}\cdot2^{m+1}-2^{n+m}-3^n+2^n}{2^{n+m}}=\frac{3^{n-1}-3\cdot3^{n-1}+3^{n-1}\cdot2^{m+1}-2^{n+m}+2^n}{2^{n+m}}\\
	=&\frac{-2\cdot3^{n-1}+3^{n-1}\cdot2^{m+1}-2^{n+m}+2^n}{2^{n+m}}=\frac{\left(2\cdot3^{n-1}-2^n\right)\left(2^m-1\right)}{2^n\cdot2^m}\\
	=&\left(\frac{3^{n-1}}{2^{n-1}}-1\right)\left(1-\frac{1}{2^m}\right)
\end{flalign*}

This has the consequence that for a given sequence consisting of $n+1$ members, between which a total of $n+m$ divisions have taken place, the permutation of these divisions has a very limited effect on the node $v_{n+1}$ as described by theorem~\ref{theo:permutation}.

\par\medskip
\begin{theorem}
	\label{theo:permutation}
	Let $v_{n+1},v_n,\ldots,v_2,v_1$ be a sequence in which a total of $n+m$ divisions took place (a path in which a total of $n+m$ edges has been contracted). No matter how these divisions are permuted, i.e. performed sooner or later, the node $v_{n+1}$ can differ at most by the following product:
	\[
	\left(\frac{3^{n-1}}{2^{n-1}}-1\right)\left(1-\frac{1}{2^m}\right)
	\]
\end{theorem}

\section{The product in the condition for alpha's upper limit}
Let us take a closer look at the product contained in condition~\ref{eq:condition_max} for the case $k=3$ and use the ascending continued fractions for examining this product. The exciting question is, does this product have a limit value even in the case where we only contract a single edge between successive nodes? Setting accordingly the sequence, which maximizes $v_{n+1}$, into the product expressed by condition~\ref{eq:condition_max}, we obtain a product that is limited, or to be more specific, which in the worst case $v_1=1$ converges (for $n$ to infinity) towards $2$:
\begin{equation}
	\label{eq:product_simplification_k3}
	\prod_{i=1}^{n}\left(1+\frac{1}{3v_{i}}\right)
	=\prod_{i=1}^{n}\left(1+\frac{1}{3\frac{3^{i-1}(v_1+1)-2^{i-1}}{2^{i-1}}}\right)
	=\prod_{i=1}^{n}\frac{3^i(v_1+1)-2^i}{3^i(v_1+1)-3*2^{i-1}}
	=\frac{1}{v_1}-\frac{1}{v_1}\left(\frac{2}{3}\right)^n+1
\end{equation}

The above-illustrated last forming step, simplifies this product significantly into an expression waiving a product formulation. A detailed breakdown including all intermediate steps of this simplification is shown in the appendix~\ref{appx:product_simplification_k3}. The correctness of this simplification can be proven inductively too, which we detail in appendix~\ref{appx:proof_product_simplification_k3}. The most important and the most interesting aspect of this result is, that the above simplified term cannot exceed the value $2$, whatever you choose to insert into $n$ or into $v_1$:
\[
\frac{1}{v_1}-\frac{1}{v_1}\left(\frac{2}{3}\right)^{n+1}+1<2
\]

Since, as shown above, the product cannot exceed the value $2$, the logarithmic product expression in the condition~\ref{eq:condition_max} cannot exceed the value one and this condition becomes a consistently true statement:
\[
n\log_23-\lfloor n\log_23\rfloor<2-1
\]

Thus, for $k=3$ the condition~\ref{eq:condition_max} for alphas's upper limit is met for all sequences that maximize $v_{n+1}$.

\section{Include additional divisions into the product}
How does the product, contained in condition~\ref{eq:condition_max}, look like if we include the additional $m$ divisions into the Engel expansion as per section~\ref{sec:include_divisions_engel_expansion}? To answer this question, we consider the sequence $v_{n+1},v_n,v_{n-1},\ldots,v_2,v_1$ and we set $v_2=\frac{3v_1+1}{2\cdot2^{m}}$. Then reusing the continued fraction given by equation~\ref{eq:asc_continued_fraction}, we obtain:

{\setlength{\jot}{1.2em}
	\begin{flalign}
		\label{eq:asc_continued_fraction_m}
		v_{n+1}&=\cfrac{3\cfrac{3\cfrac{3\cfrac{3v_1+1}{2\cdot2^m}+1}{2}+1}{2}+1}{2}\dotsb
		=\cfrac{3\cfrac{3\cfrac{3v_2+1}{2}+1}{2}+1}{2}\dotsb
		=\frac{3^{n-1}(v_2+1)-2^{n-1}}{2^{n-1}}\\
		\notag
		&=\frac{3^{n-1}(\frac{3v_1+1}{2\cdot2^{m}}+1)-2^{n-1}}{2^{n-1}}=\frac{3^nv_1+3^{n-1}+3^{n-1}2^{m+1}}{2^{m+n}}-1
\end{flalign}}

\par\noindent
The product will be calculated by using equation~\ref{eq:product_simplification_k3}:
\begin{flalign}
	\label{eq:product_k3_m}
	\prod_{i=1}^{n}\left(1+\frac{1}{3v_{i}}\right)&=\left(1+\frac{1}{3v_1}\right)\cdot\prod_{i=2}^{n}\left(1+\frac{1}{3v_{i}}\right)\\
	\notag
	&=\left(1+\frac{1}{3v_1}\right)\cdot\prod_{i=1}^{n-1}\left(1+\frac{1}{3v_{i+1}}\right)=\left(1+\frac{1}{3v_1}\right)\cdot\left(\frac{1}{v_2}-\frac{1}{v_2}\left(\frac{2}{3}\right)^{n-1}+1\right)
\end{flalign}

\par\noindent
Finally substituting $v_2=\frac{3v_1+1}{2\cdot2^{m}}$ into equation~\ref{eq:product_k3_m} leads to the simplified formula of the product:
\begin{equation}
	\label{eq:product_simplification_k3_m}
	\prod_{i=1}^{n}\left(1+\frac{1}{3v_{i}}\right)=\left(1+\frac{1}{3v_1}\right)\cdot\frac{1-\left(\frac{2}{3}\right)^{n-1}+v_2}{v_2}=\frac{1+2^{m+1}}{3v_1}-\frac{2^m}{v_1}\left(\frac{2}{3}\right)^n+1
\end{equation}

\begin{example}
	An example provides the sequence $v_1=661$, $v_2=31$, $v_3=47$, $v_4=71$, $v_5=107$. When we input $v_1=661$ with $m=5$ and $n=4$ into equation~\ref{eq:asc_continued_fraction_m} we retrieve the label of the vertex $v_5$:
	\[
	v_5=v_{n+1}=\frac{3^4\cdot661+3^3+3^3\cdot2^6}{2^9}-1=107
	\]
	In this sequence five $(m=5)$ additional divisions by two took place in the first step using $v_1$:
	\[
	\frac{3\cdot661-1}{2\cdot2^5}=v_2=31
	\]
	Let us now verify the formula for the product by taking this particular example. To this end we input $v_1=661$ together with $m=5$ and $n=4$ into equation~\ref{eq:product_simplification_k3_m}:
	\[
	\left(1+\frac{1}{3\cdot661}\right)\left(1+\frac{1}{3\cdot31}\right)\left(1+\frac{1}{3\cdot47}\right)\left(1+\frac{1}{3\cdot71}\right)=\frac{1+2^{6}}{3\cdot661}-\frac{2^5}{661}\left(\frac{2}{3}\right)^4+1=1.023215853
	\]
\end{example}

\section{Condition for a limited growth of the Engel expansion}
\label{sec:condition_limited_growth}
Let us look now into the question of what condition must be met to prevent a greater growth than a decline in Collatz sequences. Specifically we consider an Engel expansion comprising $n+1$ sequence members that include $m$ additional divisions by two at the beginning. The last member $v_{n+1}$ in such a sequence can be calulated by formula~\ref{eq:asc_continued_fraction_m}. In order to restrict the growth of this sequence, we require that the last member has to be smaller than the first one. For this we define the condition $v_{n+1}<v_1$:

\[
\frac{3^nv_1+3^{n-1}+3^{n-1}2^{m+1}}{2^{m+n}}-1<v_1
\]

By transforming this inequality, which is thoroughly described in the appendix~\ref{appx:condition_limited_growth} step by step, we obtain the condition:

\begin{equation}
	\label{eq:condition_limited_growth}
	\frac{3^{n-1}\left(2^{m+1}-2\right)}{2^{m+n}-3^n}-1<v_1
\end{equation}

\section{Engel expansions maximize the product}
The question which sequence maximizes the target node $v_{n+1}$ ties into the question which sequence maximizes the product in the condition for cycle-alpha's upper limit given by equation~\ref{eq:condition_max}. The product formula that do not depend from all vertices $v_1,v_2,\ldots v_n$ has been evolved in appendix~\ref{appx:product_formula_depending_v1}. This formla depends only from $2^\alpha$, from the starting node $v_1$ and the target node $v_{n+1}$:

\[
\prod_{i=1}^{n}\left(1+\frac{1}{kv_i}\right)=\frac{2^{\alpha_1+\ldots+\alpha_n}v_{n+1}}{k^nv_1}
\]

In order to maximize this product, one needs to maximize the target node $v_{n+1}$, which exactly the Engel expansion does. Hence, for a given $v_1$, the Engel expansion is the worst case sequence maximizing the product in the condition for cycle-alpha's upper limit.


\documentclass[10pt]{article}

%\usepackage{showframe}
\usepackage[margin=2cm,a4paper]{geometry}
\usepackage{comment}
\usepackage{longtable}
\usepackage{enumitem}
\setlist{noitemsep, topsep=3pt}
\usepackage[colorlinks,urlcolor=blue,linkcolor=blue,citecolor=blue]{hyperref}
\usepackage{lscape}

\begin{document}

\section{3D Geometric Types}

3D geometric data types represent three-dimensional spatial objects.
Table~\ref{tab:geo3dtypes} shows the 3D geometric types available in PostgreSQL.

\begin{table}[htb]
\centering
\caption{3D Geometric Types}
\label{tab:geo3dtypes}
\begin{tabular}{|c|c|p{4.5cm}|l|}\hline
Name & Storage Size & Description & Representation \\\hline\hline
\verb+point3d+ & 24 bytes & Point on 3D space & \verb+(x,y,z)+ \\
\verb+lseg3d+ & 48 bytes & Finite 3D line segment & \verb+((x1,y1,z1),(x2,y2,z2))+ \\
\verb+line3d+ & 48 bytes & Infinite 3D line & \verb+((x1,y1,z1),(x2,y2,z2))+ \\
\verb+box3d+ & 48 bytes & Rectangular 3D box & \verb+((x1,y1,z1),(x2,y2,z2))+ \\
\verb+path3d+ & 64+24n bytes & Closed 3D path & \verb+((x1,y1,z1),...)+ \\
\verb+path3d+ & 64+24n bytes & Open 3D path & \verb+[(x1,y1,z1),...]+ \\
\verb+polygon3d+ & 56+24n bytes & 3D polygon & \verb+((x1,y1,z1),...)+ \\
sphere & 32 bytes & 3D sphere & \verb+<(x,y,z),r>+\\\hline
\end{tabular}
\end{table}

A rich set of functions and operators is available to perform various geometric operations such as scaling, translation, and determining intersections. They are explained in Section~\ref{sec:geo3dfcts}.

\subsection{3D Points}

3D points are the fundamental three-dimensional building block for 3D geometric types.
Values of type \verb+point3d+ are specified using either of the following syntaxes:
\begin{quote}
\begin{verbatim}
( x , y , z )
  x , y , z
\end{verbatim}
\end{quote}
where \verb+x+, \verb+y+, and \verb+z+ are the respective coordinates, as floating-point numbers.
3D points are output using the first syntax.

\begin{comment}
The specific methods for 3D points are as follows:
\begin{itemize}
\item \verb+point3D_x(point Point3D)+: a
\item \verb+point3D_y(point Point3D)+: b
\item \verb+point3D_z(point Point3D)+: c
\item \verb+point3D_horizontal(point1 Point3D, point2 Point3D)+: d
\item \verb+point3D_vertical(point1 Point3D, point2 Point3D)+: e
\item \verb+point3D_perpendicular(point1 Point3D, point2 Point3D)+: f
\item \verb+point3D_collinear(point1 Point3D, point2 Point3D, point3 Point3D)+: g
\end{itemize}
\end{comment}

\subsection{3D Line Segments}

3D line segments are represented by pairs of 3D points that are the endpoints of the segment.
Values of type \verb+lseg3d+ are specified using any of the following syntaxes:
\begin{quote}
\begin{verbatim}
[ ( x1 , y1 , z1 ) , ( x2 , y2 , z2 ) ]
( ( x1 , y1 , z1 ) , ( x2 , y2 , z2 ) )
  [ x1 , y1 , z1   ,   x2 , y2 , z2 ]
  ( x1 , y1 , z1   ,   x2 , y2 , z2 )
  ( x1 , y1 , z1 ) , ( x2 , y2 , z2 )
    x1 , y1 , z1   ,   x2 , y2 , z2 &
\end{verbatim}
\end{quote}
where \verb+(x1,y1,z1)+ and \verb+(x2,y2,z2)+ are the end points of the line segment.
3D line segments are output using the first syntax.

The two end points of a 3D line segment may be equal, in which case it represents a 3D point.

\subsection{3D Lines}

3D lines are specified by two of its points \verb+(x1,y1,z1)+ and \verb+(x2,y2,z2)+,
which must not be equal.
The direction vector of this line is \verb+(a,b,c) = (x2-x1,y2-y1,z2-z1)+ and
the parametric equations of the line are as follows
\begin{quote}
\begin{verbatim}
x = x1 + at
y = y1 + bt
z = z1 + ct
\end{verbatim}
\end{quote}
where \verb+t+ is any real number from $-\infty$ to $\infty$. Notice that the above equations also applies
for 3D line segments, but in that case \verb+t+ is any real number in $[0,1]$.

Values of type \verb+line3D+ are input and output in the following form:
\begin{quote}
\begin{verbatim}
[ ( x1 , y1 , z1 ) , ( x2 , y2 , z2 ) ]
( ( x1 , y1 , z1 ) , ( x2 , y2 , z2 ) )
  [ x1 , y1 , z1   ,   x2 , y2 , z2 ]
  ( x1 , y1 , z1   ,   x2 , y2 , z2 )
  ( x1 , y1 , z1 ) , ( x2 , y2 , z2 )
    x1 , y1 , z1   ,   x2 , y2 , z2 &
\end{verbatim}
\end{quote}
3D lines are output using the first syntax.

\subsection{3D Boxes}

3D boxes are represented by pairs of points that are opposite corners of the box.
Values of type \verb+box3d+ are specified using any of the following syntaxes:
\begin{quote}
\begin{verbatim}
( ( x1 , y1 , z1 ) , ( x2 , y2 , z2 ) )
  ( x1 , y1 , z1 ) , ( x2 , y2 , z2 )
    x1 , y1 , z1   ,   x2 , y2 , z2
\end{verbatim}
\end{quote}
where \verb+(x1,y1,z1)+ and \verb+(x2,y2,z2)+ are any two opposite corners of the box.
Boxes are output using the second syntax.
Any two opposite corners can be supplied on input, but the values will be reordered
as needed to store the upper right and lower left corners, in that order.

The two end points of a 3D box may be equal, in which case it represents a 3D point.
The two end points of a 3D box may have be equal in one of the axes (\verb+x+, \verb+y+,
or \verb+z+), in which case it represents a 2D rectangle perpendicular to the axis
with equal values.

\subsection{3D Paths}

3D paths are represented by lists of connected points. Paths can be open, where the first and last points in the list are considered not connected, or closed, where the first and last points are considered connected. Connected
paths are ``hollow", that is, they do not contain their interior.

Values of type \verb+path3d+ are specified using any of the following syntaxes:
\begin{quote}
\begin{verbatim}
[ ( x1 , y1 , z1 ) , ... , ( xn , yn , zn ) ]
( ( x1 , y1 , z1 ) , ... , ( xn , yn , zn ) )
  ( x1 , y1 , z1 ) , ... , ( xn , yn , zn )
  ( x1 , y1 , z1   , ... ,   xn , yn , zn )
    x1 , y1 , z1   , ... ,   xn , yn , zn
\end{verbatim}
\end{quote}
where the points are the end points of the line segments comprising the path.
Square brackets (\verb+[]+) indicate an open path, while parentheses (\verb+()+)
indicate a closed path. When the outermost parentheses are omitted, as in the third
through fifth syntaxes, a closed path is assumed.
Paths are output using the first or second syntax, as appropriate.

3d paths must contain at least one point. Furthermore, closed 3D paths must contain at least 3 non collinear points.

\subsection{3D Polygons}

3D polygons are represented by lists of points (the vertexes of the polygon).
Polygons are similar to closed paths, but unlike them, they include their interior.

Values of type \verb+polygon3d+ are specified using any of the following syntaxes:
\begin{quote}
\begin{verbatim}
( ( x1 , y1 , z1 ) , ... , ( xn , yn , zn ) )
  ( x1 , y1 , z1 ) , ... , ( xn , yn , zn )
  ( x1 , y1 , z1   , ... ,   xn , yn , zn )
    x1 , y1 , z1   , ... ,   xn , yn , zn
\end{verbatim}
\end{quote}
where the points are the end points of the line segments comprising the boundary of the polygon.
Polygons are output using the first syntax.

As for closed 3D paths, 3D polygons must contain at least 3 non collinear points.
3D polygons must be planar so that are ``well behaved" with respect to their operations,
such as determining whether a point is contained in the polygon. Although non planar polygons
are valid values of the type \verb+polygon3D+, many of the operations return a null value
for non planar polygons.

\subsection{Spheres}

Spheres are represented by a center point and radius. Values of type \verb+sphere+
are specified using any of the following syntaxes:
\begin{quote}
\begin{verbatim}
< ( x , y, z ) , r >
( ( x , y, z ) , r )
  ( x , y, z ) , r
    x , y, z   , r
\end{verbatim}
\end{quote}
where \verb+(x,y,z)+ is the center point and \verb+r+ is the radius of the sphere.
Circles are output using the first syntax.

\section{3D Geometric Functions and Operators}
\label{sec:geo3dfcts}

The geometric types \verb+point3d+, \verb+box3d+, \verb+lseg3d+,
\verb+line3d+, \verb+path3d+, \verb+polygon3d+, and \verb+sphere+
have a large set of native support functions and operators, shown in
Table~\ref{tab:geo3doperators}, Table~\ref{tab:geo3dfunctions}, and
Table~\ref{tab:geo3dconversion}.

The ``same as" operator, \verb+~=+, represents the usual notion
of equality for 3D geometries. In the case of
\verb+point3d+, \verb+box3d+, and \verb+sphere+ types the operator
test whether the values are identical, while for the other types
the operator tests whether the values represent the same set of
points in 3D space. For example, in the case of the \verb+line3d+
type the operator tests whether the two lines coincide, while
for the \verb+path3d+ type the operator tests whether the two
paths have the same set of vertices (but not necessarily in the same order).
These types also have an \verb+=+ operator that test identical values.
The other scalar comparison operators (\verb+<=+ and so on)
are rather arbitrary and they are used internally for indexing purposes,
they are not useful in the real world.

It is possible to access the three component numbers of a \verb+point3d+ as
though the point were an array with indexes 0, 1, and 2.
For example, if \verb+t.p+ is a \verb+point+ column then
\verb+SELECT p[0] FROM t+ retrieves the \verb+x+ coordinate and
\verb+UPDATE t SET p[1] = ...+ changes the \verb+y+ coordinate.
In the same way,  a value of type \verb+lseg3d+, \verb+line3d+ or \verb+box3d+
can be treated as an array of two point values.

The \verb+area+ function works for the types \verb+box3d+, \verb+path3d+,
\verb+polygon3d+, and \verb+sphere+.
In the case of the \verb+path3d+ data type, the \verb+area+ function only works
if the path is closed, is planar, and is not self-crossing.
For example, the function applied to the path
\begin{quote}
\begin{verbatim}
'((0,0,0),(0,1,1),(2,1,1),(2,2,2),(1,2,2),(1,0,0))'::path3d
\end{verbatim}
\end{quote}
will not work. However, the following the function applied to the visually identical path
\begin{quote}
\begin{verbatim}
'((0,0,0),(0,1,1),(1,1,1),(1,2,2),(2,2,2),(2,1,1),(1,1,1),(1,0,0),(0,0,0))'::path3d
\end{verbatim}
\end{quote}
will work. Similarly, the \verb+area+ function only works
if the polygon is planar and is not self-crossing.

The \verb+box3d+ and \verb+sphere+ functions computing the bounding box and
bounding sphere work for all types excepted \verb+line3d+.
When applied to a \verb+point3d+ they return an empty box or sphere
representing the point.

The \verb+center+ function and the corresponding operator \verb+@@+
work for all types excepted \verb+point3d+.

The \verb+coplanar+ function works for various combinations of
types \verb+point3d+, \verb+lseg3d+, \verb+line3d+, \verb+path3d+, and
\verb+polygon3d+ that involve more than three points. For example,
it works for \verb+point3d+ and \verb+polygon3d+ but neither for
\verb+point3d+ and \verb+lseg3d+ (since it is trivially true)
nor for \verb+point3d+ and \verb+point3d+ (since it does not make sense).

The \verb+length+ function works for the types \verb+lseg3d+,
\verb+path3d+, and \verb+polygon3d+.

The function \verb+points+ work for all types excepted \verb+point3d+.

The \verb+point3d+ functions
computing the center work for all types excepted \verb+line3d+.

\section{Indexing}

GiST and SP-GiST indexes can be created for table columns of some of the 3D geometry types.
The GiST index implements an R-tree for the types
\verb+point3d+, \verb+box3d+, and \verb+sphere+.
The SP-GiST index implements an Oct-tree for the type \verb+point3d+.
An example of creation of a GiST and an SP-GiST indexes is as follows:

\begin{quote}
\begin{verbatim}
CREATE TABLE geo3d_tbl(k int, p point3d, b box3d, s sphere);
CREATE INDEX geo3d_tbl_idx_p ON geo3d_tbl USING spgist (p);
CREATE INDEX geo3d_tbl_idx_b ON geo3d_tbl USING gist (b);
\end{verbatim}
\end{quote}

A GiST or SP-GiST index can accelerate queries involving the following operators:
\verb+<<+, \verb+&<+, \verb+&&+, \verb+&>+, \verb+>>+, \verb+~=+, \verb+@>+, \verb+<@+, \verb+&<|+,
\verb+<<|+, \verb+|>>+, \verb+|&>+, \verb+<->+, \verb+&</+, \verb+<</+, \verb+/>>+, and \verb+/&>+
(see Table~\ref{tab:geo3doperators} for more information).

In addition, B-tree indexes can be created for table columns of all 3D geometries.
For this index type, basically the only useful operation is equality.
There is a B-tree sort ordering defined for 3D geometry values, with corresponding
\verb+<+ and \verb+>+ operators, but the ordering is rather arbitrary and
not usually useful in the real world. B-tree support for 3D geometries
is primarily meant to allow sorting internally in queries,
rather than creation of actual indexes.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{landscape}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{center}
\begin{longtable}{|c|l|l|}%
\caption{3D Geometric Operators}
\label{tab:geo3doperators}
\\\hline
Operator & Description & Example \\\hline\hline
\endfirsthead

\multicolumn{3}{c}%
{\tablename\ \thetable{} 3D Geometric Types -- continued from previous page} \\
\hline
Operator & Description & Example \\\hline\hline
\endhead

\hline \multicolumn{3}{r}{Continued on next page} \\
\endfoot

\hline
\endlastfoot

\verb!+! & Translation &
	\verb!box3d '((0,0,0),(1,1,1))' + point3d '(2.0,0,0)'! \\
\verb+-+ & Translation &
	\verb+box3d '((0,0,0),(1,1,1))' - point3d '(2.0,0,0)'+ \\
\verb+*+ & Scaling &
	\verb+box3d '((0,0,0),(1,1,1))' * 2.0+ \\
\verb+/+ & Scaling &
	\verb+box3d '((0,0,0),(2,2,2))' / 2.0+ \\
\verb!+! & Concatenation &
	\verb!path3d '[(0,0,0),(1,1,1)]' + path3d '[(2,0,0),(2,2,2)]'! \\
\verb+#+ & Point or box of intersection &
	\verb+box3d '((1,-1,0),(-1,1,1))' # box3d '((1,1,1),(-2,-2,-2))'+ \\
\verb+#+ & Number of points in path or polygon &
	\verb+# path3d '((1,0,0),(0,1,1),(-1,0,2))'+ \\
\verb+@-@+ & Length of segment, path, or polygon &
	\verb+@-@ path3d '((0,0,0),(1,0,0))'+ \\
\verb+@@+ & Center &
	\verb+@@ sphere '((0,0,0),10)'+ \\
\verb+##+ & Closest point to first operand on second operand &
	\verb+point3d '(0,0,0)' ## lseg3d '((2,0,0),(0,2,1))'+ \\
\verb+<->+ & Distance between &
	\verb+sphere '((0,0,0),1)' <-> sphere '((5,0,1),1)'+ \\
\verb+&&+ & Overlaps? (One point in common makes this true) &
	\verb+box3d '((0,0,0),(1,1,1))' && box3d '((0,0,0),(2,2,2))'+ \\
\verb+<<+ & Is strictly left of? &
	\verb+sphere '((0,0,0),1)' << sphere '((5,0,0),1)'+ \\
\verb+>>+ & Is strictly right of? &
	\verb+sphere '((5,0,0),1)' >> sphere '((0,0,0),1)'+ \\
\verb+&<+ & Does not extend to the right of?  &
	\verb+box3d '((0,0,0),(1,1,1))' &< box3d '((0,0,0),(2,2,2))'+ \\
\verb+&>+ & Does not extend to the left of? &
	\verb+box3d '((0,0,0),(3,3,3))' &> box3d '((0,0,0),(2,2,2))'+ \\
\verb+<<|+ & Is strictly below? &
	\verb+box3d '((0,0,0),(3,3,3))' <<| box3d '((3,4,4),(5,5,5))'+ \\
\verb+|>>+ & Is strictly above? &
	\verb+box3d '((3,4,4),(5,5,5))' |>> box3d '((0,0,0),(3,3,3))'+ \\
\verb+&<|+ & Does not extend above? &
	\verb+box3d '((0,0,0),(1,1,1))' &<| box3d '((0,0,0),(2,2,2))'+ \\
\verb+|&>+ & Does not extend below? &
	\verb+box3d '((0,0,0),(3,3,3))' |&> box3d '((0,0,0),(2,2,2))'+ \\
\verb+<</+ & Is strictly in front? &
	\verb+box3d '((0,0,0),(3,3,3))' <<| box3d '((3,4,4),(5,5,5))'+ \\
\verb+/>>+ & Is strictly behind? &
	\verb+box3d '((3,4,4),(5,5,5))' |>> box3d '((0,0,0),(3,3,3))'+ \\
\verb+&</+ & Does not extend in front? &
	\verb+box3d '((0,0,0),(1,1,1))' &<| box3d '((0,0,0),(2,2,2))'+ \\
\verb+/&>+ & Does not extend below? &
	\verb+box3d '((0,0,0),(3,3,3))' |&> box3d '((0,0,0),(2,2,2))'+ \\
\verb+?#+ & Intersects? &
	\verb+lseg3d '((-1,0),(1,0))' ?# box3d '((-2,-2),(2,2))'+ \\
\verb+?-+ & Is horizontal? &
	\verb+?- lseg3d '((-1,0,0),(1,0,0))'+ \\
\verb+?-+ & Are horizontally aligned? &
	\verb+point3d '(1,0,0)' ?- point3d '(0,0,0)'+ \\
\verb+?|+ & Is vertical? &
	\verb+?| lseg3d '((-1,0,0),(1,0,1))'+ \\
\verb+?|+ & Are vertically aligned? &
	\verb+point3d '(0,1)' ?| point3d '(0,0,0)'+ \\
\verb+?/+ & Is perpendicular? &
	\verb+?/ lseg3d '((-1,0,1),(1,0,1))'+ \\
\verb+?/+ & Are perpendicularly aligned? &
	\verb+point3d '(0,1,1)' ?/ point3d '(0,0,1)'+ \\
\verb+?-|+ & Are orthogonal? &
	\verb+lseg3d '((0,0,0),(0,1,1))' ?-| lseg3d '((0,0,0),(1,0,1))'+ \\
\verb+?||+ & Are parallel? &
	\verb+lseg3d '((-1,0,0),(1,0,1))' ?|| lseg3d '((-1,2,2),(1,2,3))'+ \\
\verb+?-/+ & Are skew? &
	\verb+lseg3d '((-1,0,0),(1,0,1))' ?-/ lseg3d '((-1,2,2),(1,2,3))'+ \\
\verb+@>+ & Contains? &
	\verb+sphere '((0,0,0),2)' @> point3d '(1,1,1)'+ \\
\verb+<@+ & Contained in or on? &
	\verb+point3d '(1,1,1)' <@ sphere '((0,0,0),2)'+ \\
\verb+~=+ & Same as? &
	\verb+polygon3D '((0,0,0),(1,1,1))' ~= polygon3D '((1,1,1),(0,0,0))'+ \\
\end{longtable}
\end{center}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{center}
\begin{longtable}{|p{4cm}|l|l|l|}%
\caption{3D Geometric Functions}
\label{tab:geo3dfunctions}
\\\hline
Function & Return Type & Description & Example \\\hline\hline
\endfirsthead

\multicolumn{4}{c}%
{\tablename\ \thetable{} 3D Geometric Functions -- continued from previous page} \\
\hline
Function & Return Type & Description & Example \\\hline\hline
\endhead

\hline \multicolumn{4}{r}{Continued on next page} \\
\endfoot

\hline
\endlastfoot

\verb+area(object3d)+ & \verb+double precision+ & Area of object&
	\verb+area(sphere '((0,0,0),1)')+ \\
\verb+center(object3d)+ & \verb+point3d+ & Center of object&
	\verb+center(box3d '((0,0,0),(2,2,2))')+ \\
\verb+coincide(line3d,+
\newline\hspace*{0.5cm}\verb+line3d)+ & \verb+boolean+ & Are the same line? &
	\verb+coincide(line3d '((0,0,0),(1,1,1))', line3d '((2,2,2),(3,3,3))')+ \\
\verb+collinear(point3d,+
\newline\hspace*{0.5cm}\verb+point3d, point3d)+ & \verb+boolean+ & Are the points collinear? &
	\verb+collinear(point3d '0,0,0', point3d '1,1,1', point3d '2,2,2')+ \\
\verb+coplanar(object3d,+
\newline\hspace*{0.5cm}\verb+object3d)+ & \verb+boolean+ & Are the objects coplanar? &
	\verb+coplanar(point3d '1,1,1', polygon3d '0,0,0,2,2,2,2,0,0')+ \\
\verb+depth(box3d)+ & \verb+double precision+ & Perpendicular size of box &
	\verb+depth(box3d '((0,0,0),(1,2,3))')+ \\
\verb+diameter(sphere)+ & \verb+double precision+ & Diameter of sphere &
	\verb+diameter(sphere '((0,0,0),2.0)')+ \\
\verb+height(box3d)+ & \verb+double precision+ & Vertical size of box &
	\verb+height(box3d '((0,0,0),(1,2,3))')+ \\
\verb+high(box3d)+ & \verb+point3d+ & High corner of box &
	\verb+high(box3d '((0,0,0),(1,2,3))')+ \\
\verb+horizontal(point3d,+
\newline\hspace*{0.5cm}\verb+point3d)+ & \verb+boolean+ & Are the y values equal? &
	\verb+horizontal(point3d '(0,0,0)', point3d '(1,0,1)')+ \\
\verb+horizontal(lseg3d)+ & \verb+boolean+ & Are the y values equal? &
	\verb+horizontal(lseg3d '((0,0,0),(1,0,1))')+ \\
\verb+horizontal(line3d)+ & \verb+boolean+ & Are the y values equal? &
	\verb+horizontal(line3d '((0,0,0),(1,0,1))')+ \\
\verb+isclosed(path3d)+ & \verb+boolean+ & Is the path closed? &
	\verb+isclosed(path3d '((0,0,0),(1,1,1),(2,0))')+ \\
\verb+isopen(path3d)+ & \verb+boolean+ & Is the path open? &
	\verb+isopen(path3d '[(0,0,0),(1,1,1),(2,0,0)]')+ \\
\verb+isplanar(path3d)+ & \verb+boolean+ & Is the path planar? &
	\verb+isplanar(path3d '[(0,0,0),(1,1,1),(2,0,0)]')+ \\
\verb+isplanar(polygon3d)+ & \verb+boolean+ & Is the polygon planar? &
	\verb+isplanar(polygon3d '((0,0,0),(0,1,1),(1,1,1),(1,0,0))')+ \\
\verb+length(object3d)+ & \verb+double precision+ & Length of object &
	\verb+length(path3d '((-1,0,0),(1,0,1))')+ \\
\verb+low(box3d)+ & \verb+point3d+ & Low corner of box &
	\verb+low(box3d '((0,0,0),(1,2,3))')+ \\
\verb+npoints(path3d)+ & \verb+int+ & Number of points of path &
	\verb+npoints(path3d '[(0,0,0),(1,1,1),(2,0,0)]')+ \\
\verb+npoints(polygon3d)+ & \verb+int+ & Number of points of polygon &
	\verb+npoints(polygon3d '((0,0,0),(0,1,1),(1,1,1),(1,0,0))')+ \\
\verb+orthogonal(lseg3d,+
\newline\hspace*{0.5cm}\verb+lseg3d)+ & \verb+boolean+ & Are orthogonal? &
	\verb+orthogonal(lseg3d '((0,0,0),(1,1,1))', lseg3d '((0,1,1),(1,0,1))')+ \\
\verb+orthogonal(line3d,+
\newline\hspace*{0.5cm}\verb+line3d)+ & \verb+boolean+ & Are orthogonal? &
	\verb+orthogonal(line3d '((0,0,0),(1,1,1))', line3d '((0,1,1),(1,0,1))')+ \\
\verb+parallel(lseg3d,+
\newline\hspace*{0.5cm}\verb+lseg3d)+ & \verb+boolean+ & Are orthogonal? &
	\verb+parallel(lseg3d '((0,0,0),(1,1,1))', lseg3d '((2,1,1),(3,2,2))')+ \\
\verb+parallel(line3d,+
\newline\hspace*{0.5cm}\verb+line3d)+ & \verb+boolean+ & Are orthogonal? &
	\verb+parallel(line3d '((0,0,0),(1,1,1))', line3d '((0,1,1),(1,0,1))')+ \\
\verb+pclose(path3d)+ & \verb+path3d+ & Convert path to closed &
	\verb+pclose(path3d '[(0,0,1),(1,1,1),(2,0,0)]')+ \\
\verb+perpendicular(point3d,+
\newline\hspace*{0.5cm}\verb+point3d)+ & \verb+boolean+ & Are the z values equal? &
	\verb+perpendicular(point3d '(0,0,0)', point3d '(1,1,0)')+ \\
\verb+perpendicular(lseg3d)+ & \verb+boolean+ & Are the z values equal? &
	\verb+perpendicular(lseg3d '((0,0,0),(1,1,0))')+ \\
\verb+perpendicular(line3d)+ & \verb+boolean+ & Are the z values equal? &
	\verb+perpendicular(line3d '((0,0,0),(1,1,0))')+ \\
\verb+points(object3d)+ & \verb+point3D[]+ & Points of an object &
	\verb+points(polygon3d '((0,0,0),(0,1,1),(1,1,1),(1,0,0))')+ \\
\verb+popen(path3d)+ & \verb+path3d+ & Convert path to open &
	\verb+popen(path3d '((0,0,0),(1,1,1),(2,0,0))')+ \\
\verb+radius(sphere)+ & \verb+double precision+ & Radius of sphere &
	\verb+radius(sphere '((0,0,0),2.0)')+ \\
\verb+segments(path3d)+ & \verb+lseg3D[]+ & Array of edges of path &
	\verb+segments(path3d '[(0,0,0),(1,1,1),(2,0,0)]')+ \\
\verb+segments(polygon3d)+ & \verb+lseg3D[]+ & Array of edges of polygon &
	\verb+segments(polygon3d '((0,0,0),(0,1,1),(1,1,1),(1,0,0))')+ \\
\verb+skew(lseg3d, lseg3d)+ & \verb+boolean+ & Are skew? &
	\verb+skew(lseg3d '((0,0,0),(1,1,1))', lseg3d '((0,1,1),(1,0,2))')+ \\
\verb+skew(line3d, line3d)+ & \verb+boolean+ & Are skew? &
	\verb+skew(line3d '((0,0,0),(1,1,1))', line3d '((0,1,1),(1,0,2))')+ \\
\verb+vertical(point3d+
\newline\hspace*{0.5cm}\verb+point3d)+ & \verb+boolean+ & Are the x values equal? &
	\verb+vertical(point3d '(0,0,0)', point3d '(0,1,1)')+ \\
\verb+vertical(lseg3d)+ & \verb+boolean+ & Are the x values equal? &
	\verb+vertical(lseg3d '((0,0,0),(0,1,1))')+ \\
\verb+vertical(line3d)+ & \verb+boolean+ & Are the x values equal? &
	\verb+vertical(line3d '((0,0,0),(0,1,1))')+ \\
\verb+volume(box3d)+ & \verb+double precision+ & Volume of box &
	\verb+volume(box3d '((0,0,0),(1,1,1))')+ \\
\verb+volume(sphere)+ & \verb+double precision+ & Volume of sphere &
	\verb+volume(sphere '((0,0,0),1))')+ \\
\verb+width(box3d)+ & \verb+double precision+ & Horizontal size of box &
	\verb+width(box3d '((0,0,0),(1,2,3))')+ \\

\end{longtable}
\end{center}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{center}
\begin{longtable}{|p{5cm}|l|l|p{12cm}|}%
\caption{3D Geometric Type Conversion Functions}
\label{tab:geo3dconversion}
\\\hline
Function & Return Type & Description & Example \\\hline\hline
\endfirsthead

\multicolumn{4}{c}%
{\tablename\ \thetable{} 3D Geometric Type Conversion Functions -- continued from previous page} \\
\hline
Operator & Description & Example \\\hline\hline
\endhead

\hline \multicolumn{4}{r}{Continued on next page} \\
\endfoot

\hline
\endlastfoot

\verb+box3d(point3d, point3d)+ & \verb+box3d+ & points to box &
	\verb+box3d(point3d '(0,0,0)', point3d '(1,1,1)')+ \\
\verb+box3d(object3d)+ & \verb+box3d+ & bounding box &
	\verb+box3d(sphere '((0,0,0),2.0)')+ \\
\verb+box3d(box3d, box3d)+ & \verb+box3d+ & boxes to bounding box &
	\verb+box3d(box3d '((0,0,0),(1,1,1))', box3d '((3,3,3),(4,4,4))')+ \\
\verb+line3d(point3d, point3d)+ & \verb+line3d+ & points to line &
	\verb+line3d(point3d '(-1,0,0)', point3d '(1,0,1)')+ \\
\verb+lseg3d(box3d)+ & \verb+lseg3d+ & box diagonal to line segment &
	\verb+lseg3d(box3d '((-1,0,0),(1,0,1))')+ \\
\verb+lseg3d(point3d, point3d)+ & \verb+lseg3d+ & points to line segment &
	\verb+lseg3d(point3d '(-1,0,0)', point3d '(1,0,1)')+ \\
\verb+path3d(polygon3d)+ & \verb+path3d+ & polygon to path &
	\verb+path3d(polygon3d '((0,0,0),(1,1,1),(2,0,1))')+ \\
\verb+point3d(double precision,+\newline\hspace*{5mm}\verb+double precision,+
\newline\hspace*{5mm}\verb+double precision)+ & \verb+point+ & construct point &
	\verb+point3d(23.4, -44.5, 66.1)+ \\
\verb+point3d(object3d)+ & \verb+point3d+ & center of object &
	\verb+point3d(box3d '((-1,0,0),(1,0,1))')+ \\
\verb+polygon3d(path3d)+ & \verb+polygon3d+ & path to polygon &
	\verb+polygon3d(path3d '((0,0,0),(1,1,1),(2,0,0))')+ \\
\verb+sphere(point3d,+\newline\hspace*{5mm}\verb+double precision)+ &
	\verb+sphere+ & center and radius to sphere & \verb+sphere(point3d '(0,0,0)', 2.0)+ \\
\verb+sphere(object3d)+ & \verb+sphere+ & bounding sphere &
	\verb+sphere(lseg3d '((0,0,0),(1,1,1))')+ \\
\verb+sphere(sphere, sphere)+ & \verb+sphere+ & spheres to bounding sphere &
	\verb+sphere(sphere '((0,0,0),1)', sphere '((2,0,0),1)')+ \\

\end{longtable}
\end{center}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{landscape}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}


\section{Tested Neural Network Architectures}
\label{sec:appendices}

Architecture 1

\begin{table}[!htb]
\centering
\begin{tabular}{l l l l}
	\hline
	Layer & Shape & Activation & Additional Information\\
  	\hline
  	Fully connected & 30 & ReLU & L2 regularization factor = 0.2\\
  	Fully connected & 10 & ReLU & L2 regularization factor = 0.2\\
  	Fully connected & 1 & Linear & \\
  	\hline
\end{tabular}
\caption{Proposed Neural Network architecture 1}
\label{table:proposed_nn_1}
\end{table}

Architecture 2

\begin{table}[!htb]
\centering
\begin{tabular}{l l l l}
	\hline
	Layer & Shape & Activation & Additional Information\\
  	\hline
  	Fully connected & 50 & ReLU & L2 regularization factor = 0.2\\
  	Fully connected & 20 & ReLU & L2 regularization factor = 0.2\\
  	Fully connected & 1 & Linear & \\
  	\hline
\end{tabular}
\caption{Proposed Neural Network architecture 2}
\label{table:proposed_nn_2}
\end{table}

Architecture 3

\begin{table}[!htb]
\centering
\begin{tabular}{l l l l}
	\hline
	Layer & Shape & Activation & Additional Information\\
  	\hline
  	Fully connected & 100 & ReLU & L2 regularization factor = 0.2\\
  	Fully connected & 50 & ReLU & L2 regularization factor = 0.2\\
  	Fully connected & 1 & Linear & \\
  	\hline
\end{tabular}
\caption{Proposed Neural Network architecture 3}
\label{table:proposed_nn_3}
\end{table}

Architecture 4

\begin{table}[!htb]
\centering
\begin{tabular}{l l l l}
	\hline
	Layer & Shape & Activation & Additional Information\\
  	\hline
  	Fully connected & 250 & ReLU & L2 regularization factor = 0.2\\
  	Fully connected & 50 & ReLU & L2 regularization factor = 0.2\\
  	Fully connected & 1 & Linear & \\
  	\hline
\end{tabular}
\caption{Proposed Neural Network architecture 4}
\label{table:proposed_nn_4}
\end{table}

Architecture 5

\begin{table}[!htb]
\centering
\begin{tabular}{l l l l}
	\hline
	Layer & Shape & Activation & Additional Information\\
  	\hline
  	Fully connected & 20 & ReLU & L2 regularization factor = 0.2\\
  	Fully connected & 1 & Linear & \\
  	\hline
\end{tabular}
\caption{Proposed Neural Network architecture 5}
\label{table:proposed_nn_5}
\end{table}

\pagebreak

Architecture 6

\begin{table}[!htb]
\centering
\begin{tabular}{l l l l}
	\hline
	Layer & Shape & Activation & Additional Information\\
  	\hline
  	Fully connected & 10 & ReLU & L2 regularization factor = 0.2\\
  	Fully connected & 1 & Linear & \\
  	\hline
\end{tabular}
\caption{Proposed Neural Network architecture 6}
\label{table:proposed_nn_6}
\end{table}


\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}

%opening
\title{Calculation of the generalised Gini index denominator}
\author{Angelo Ortiz}

\begin{document}

\maketitle

\section{Preliminaries\protect\footnote{Taken from \cite{crt82}.}}
Let us consider $n$ agents and write $U_j$ for the utility (i.e., income) of the $j$th agent. Let us suppose that the utilities are in ascending order:
\begin{equation}
 U_1 \leq U_2 \leq \cdots \leq U_n;
\end{equation}
some of the $U_j$ can be negative, but the utility sum must remain positive.

We calculate the mean
$ \mu = \frac{\sum_{j=1}^n U_j}{n},$
and define the income share of the $j$th agent as
\begin{equation}
 u_j = \frac{U_j}{n\mu}.
\end{equation}

The Gini index can then be expressed as
\begin{equation}
 G = \frac{1}{n}\displaystyle\sum_{j=1}^n\displaystyle\sum_{i<j} (u_j - u_i).\label{eq:gini}
\end{equation}

Expanding \eqref{eq:gini}, we have
\begin{align}
 G &= \frac{1}{n}\displaystyle\sum_{j=1}^n u_j(n-1-2(n-j)) \nonumber \\
   &= 1 - \frac{1}{n}\displaystyle\sum_{j=1}^n u_j(1+2(n-j)) \nonumber \\
   &= 1 + \frac{2}{n}\displaystyle\sum_{j=1}^k j\cdot u_j - \frac{1}{n}\displaystyle\sum_{j=k+1}^n u_j(1+2(n-j)), \label{eq:expand}
\end{align}
where $k$ is defined in such a way so that $\sum_{j=1}^k u_j = 0$.

The generalised Gini index is thus defined as
\begin{equation}
 G^\star = \frac{G}{1 + \frac{2}{n}\displaystyle\sum_{j=1}^k j\cdot u_j}.\label{eq:gen}
\end{equation}

The rationale behind this denominator is that, although all of the three terms in \eqref{eq:expand} are positive, the third one is subtracted from the other two, whose sum is greater than or equal to 1.

\section{When such a $k$ does not exist}
\subsection{CTR}
In this case, Chen, Tsaur and Rhai (hereafter CTR) changed\footnote{Actually, Berreby and Silber noticed and righted a mistake in \cite{bs85}. CTR later confirmed this in \cite{crt85}.} the denominator of $G^\star$ as follows:
\begin{equation}
 1 + \frac{2}{n}\displaystyle\sum_{j=1}^k j\cdot u_j + \frac{1}{n}\displaystyle\sum_{j=1}^k u_j\left[\frac{\sum_{i=1}^k u_i}{u_{k+1}} - (1+2k)\right],
\end{equation}
when there exists no such k so that $\sum_{j=1}^k u_j = 0$, but rather $\sum_{j=1}^k u_j < 0$ and $\sum_{j=1}^{k+1} u_j > 0$.

\subsection{My formula}
It is apparent in \eqref{eq:gen} that the weighted sum is calculated over the sequence of incomes $(u_j)_{j=1}^k$ s.t. their sum is nil.
Since $\sum_{j=1}^k u_j < 0$ and $\sum_{j=1}^{k+1} u_j > 0$, there exists $0 < b < u_{k+1}$ s.t.
\begin{equation}
 u_{k+1} = b - \sum_{j=1}^{k} u_j \label{eq:kp1}.
\end{equation}

In my opinion, it suffices to divide the income of the $(k+1)$th agent as in \eqref{eq:kp1}, \textit{simulating} an additional agent such that, when taken together with the first $k$ agents, their income sum is exactly zero.

Hence, considering \eqref{eq:kp1} and re-expanding \eqref{eq:gini}, we now have
\begin{align}
 G &= 1 - \frac{1}{n}\displaystyle\sum_{j=1}^n u_j(1+2(n-j)) \nonumber \\
   &= 1 - \frac{1}{n}\displaystyle\sum_{j=1}^k u_j(1+2(n-j)) - \frac{1}{n}\left[b - \sum_{j=1}^k u_j\right](1+2(n-k-1)) \nonumber \\
   & - \frac{1}{n}\displaystyle\sum_{j=k+2}^n u_j(1+2(n-j)) \nonumber \\
   &= 1 + \frac{2}{n}\displaystyle\sum_{j=1}^k u_j(j-k-1) - \frac{b}{n}(2(n-k)-1) - \frac{1}{n}\displaystyle\sum_{j=k+2}^n u_j(1+2(n-j)) \label{eq:pers}
\end{align}

Here again, all of the four terms in \eqref{eq:pers} are positive. Using the same reasoning as before, the normalisation coefficient for the generalised Gini index should be limited to the first two terms, which yields
\begin{equation}
 G^\star = \frac{G}{1 + \frac{2}{n}\displaystyle\sum_{j=1}^k u_j(j-k-1)}.
\end{equation}

\begin{thebibliography}{9}

\bibitem{bs85}
Z.M. Berreby, Jacques Silber. \textit{The Gini coefficient and negative income: A comment}. Oxford Economic Papers, vol. 37, pp. 525-526, 1985.

\bibitem{crt82}
Chau-Nan Chen, Tien-Wang Tsaur, Tong-Shieng. \textit{The Gini coefficient and negative income}. Oxford Economic Papers, vol. 34, pp. 473-478, 1982.

\bibitem{crt85}
Chau-Nan Chen, Tien-Wang Tsaur, Tong-Shieng. \textit{The Gini coefficient and negative income: A reply}. Oxford Economic Papers, vol. 37, pp. 527-528, 1985.

\end{thebibliography}

\end{document}


%% SECTION HEADER /////////////////////////////////////////////////////////////////////////////////////
\section{2D Spectral Modelling}
\label{sec:2Dmodel}

%% SECTION CONTENT ////////////////////////////////////////////////////////////////////////////////////

According to the first-order shear deformation theory~\cite{reissner1945effect, mindlin1951influence}, the displacement field is expressed as:
\begin{eqnarray}
	\left \{ \begin{array}{c}
		\textbf{u}^e(\xi,\eta) \\
		\textbf{v}^e(\xi,\eta) \\
		\textbf{w}^e(\xi,\eta)
	\end{array} \right\} =
	\left \{ \begin{array}{c}
		\textbf{u}_0^e(\xi,\eta) + z\boldsymbol{\varphi}_x^e(\xi,\eta)\\
		\textbf{v}_0^e(\xi,\eta) + z\boldsymbol{\varphi}_y^e(\xi,\eta)\\
		\textbf{w}_0^e(\xi,\eta) \\
	\end{array} \right\},
\end{eqnarray}
where \(\textbf{u}_0^e\), \(\textbf{v}_0^e\) and \(\textbf{w}_0^e\) are nodal displacements, \(\boldsymbol{\varphi}_x^e\), \(\boldsymbol{\varphi}_y^e\) are the rotations of the normal to the mid-plane with respect to the axes \textit{x} and \textit{y}, respectively.
\begin{eqnarray}
	\left \{\begin{array}{c}
		\textbf{u}_0^e(\xi,\eta) \\
		\textbf{v}_0^e(\xi,\eta) \\
		\textbf{w}_0^e(\xi,\eta) \\
		\boldsymbol{\varphi}_x^e(\xi,\eta) \\
		\boldsymbol{\varphi}_y^e(\xi,\eta)
	\end{array} \right\}
	= \textbf{N}^e(\xi,\eta)\widehat{\textbf{d}}^e
	= \sum_{n=1}^q\sum_{m=1}^p\textbf{N}_m^e(\xi)\textbf{N}_n^e(\eta)
	\left \{ \begin{array}{c}
		\widehat{\textbf{u}}_0^e \\
		\widehat{\textbf{v}}_0^e \\
		\widehat{\textbf{w}}_0^e \\
		\widehat{\boldsymbol{\varphi}}_x^e \\
		\widehat{\boldsymbol{\varphi}}_y^e
	\end{array} \right \}.
\end{eqnarray}

The nodal bending strain--displacement relations are given in the form:
\begin{eqnarray}
	\boldsymbol{\epsilon}_b^e =
	\textbf{B}_b^e\widehat{\textbf{d}}^e =
	\left [
	\begin{array}{ccccc}
		\frac{\partial N^e}{\partial x} & 0 & 0 & 0 & 0\\
		0 & \frac{\partial N^e}{\partial y} & 0 & 0 & 0\\
		\frac{\partial N^e}{\partial y} & \frac{\partial N^e}{\partial x} & 0 & 0 & 0\\
		0 & 0 & 0 & -\frac{\partial N^e}{\partial x} & 0\\
		0 & 0 & 0 & 0 & -\frac{\partial N^e}{\partial y}\\
		0 & 0 & 0 & -\frac{\partial N^e}{\partial y} & -\frac{\partial N^e}{\partial x}
	\end{array} \right]
	\left \{ \begin{array}{c}
		\widehat{\textbf{u}}_0^e \\
		\widehat{\textbf{v}}_0^e \\
		\widehat{\textbf{w}}_0^e \\
		\widehat{\boldsymbol{\varphi}}_x^e \\
		\widehat{\boldsymbol{\varphi}}_y^e
	\end{array} \right\}.
\end{eqnarray}

The nodal shear strain--displacement relations are given in the form:
\begin{eqnarray}
	\boldsymbol{\epsilon}_s^e =
	\textbf{B}_s^e\widehat{\textbf{d}}^e =
	\left [
	\begin{array}{ccccc}
		0 & 0 & \frac{\partial N^e}{\partial y} & -1 & 0\\
		0 & 0 & \frac{\partial N^e}{\partial y} & 0 & -1
	\end{array} \right]
	\left \{ \begin{array}{c}
		\widehat{\textbf{u}}_0^e \\
		\widehat{\textbf{v}}_0^e \\
		\widehat{\textbf{w}}_0^e \\
		\boldsymbol{\varphi}_x^e \\
		\boldsymbol{\varphi}_y^e
	\end{array} \right\}.
\end{eqnarray}


% !TEX root = ./main.tex
\section{Introduction}

\begin{enumerate}
    \item Evolution was first invented by physicists.
    \item What are the forces of evolution.
    \item What is population genetics.
    \item What is Kimura's diffusion theory.
    \item What is the benefit of having mathematical models of evolution.
\end{enumerate}

\subsection{Evolutionary dynamics}

\noindent
The concept of evolution is commonly associated with biology, but one may argue
that evolution on a broader sense has been a topic of physics for a much longer.
From Galileo's pioneering studies of the movement of objects on earth, to
Kepler's insight into planetary movements, physicists have been intrigue by the
time evolution of natural phenomena for centuries. But it was not until Newton's
Principia that calculus gave us a formal language to understand dynamical
processes. The mathematical theory for evolutionary dynamics is no exception.
Our objective in this paper is to derive the differential equations for the time
trajectories of dynamical variables having to do with the genetic composition of
a population.

Since evolution is a population-level rather than a single-organism phenomena,
the relevant quantities we must keep track of in a mathematical theory of
evolution are relative abundances (frequencies) at this population level.
Specifically, a lot of the classic population genetics theory is cast in the
language of allele frequencies. An allele can be thought as a version of a gene.
In Mendel's original work he cross-pollinated various pea plants that presented
different physical characteristics (phenotypes) associated with different
alleles. If a pea plant carried a particular allele, the seeds presented a
wrinkled surface. A different allele made seeds had a smooth surface.
Equivalently, there were different alleles for the other phenotypes such as
flower coloring or plant height.

The equivalent of the hydrogen atom in population genetics is the so-called
one-locus two-allele model in which a single gene (locus for its location on the
genome) presents only two possible versions (alleles). This minimal setup will
allow us to write down the dynamics of the relative frequency of both alleles as
a population evolves under different evolutionary forces. We imagine a
population with $N$ organisms that carries two possible alleles, $A$ and $a$,
for a particular gene of interest. The number of organisms carrying allele $A$
or $a$ are given by $n$ and $(N - n)$, respectively, where $N$ is the total
population size. The population size can in principle take any value between
zero and infinity. But even if a population in which one of the two alleles went
extinct (all organisms in the population carrying the same allele) were to keep
growing ($N$ changing with time), we would not say that such a population is
evolving anymore. The population could still be growing, but nothing about their
characteristics (in this simplified model where we only track a single gene)
would change over time. In this scenario we neglect the possibility of the
extinct allele re-emerging via mutation, but the point still remains. This is
why the quantity we care about is not the absolute number of organisms carrying
a specific allele, but their relative frequency. Furthermore, by tracking the
allele frequency we remove one degree of freedom since knowing the frequency of
one allele tells us the frequency of the other in this one-locus two-allele
scenario. Without loss of generality, we define the frequency of allele $A$,
$x$, as
\begin{equation}
    x \equiv \frac{n}{N}.
\end{equation}
Our main objective will then be to write down dynamical equations for how this
allele frequency evolves over time. To write these dynamics we will need to work
in the regime where $N$ is very large. This will allow us to approximate $x$ as
a continuous variable.

\subsection{Deterministic vs. stochastic evolutionary forces, and diffusion
theory}

Throughout this work we will deal with three evolutionary forces: natural
selection, mutation, and genetic drift. Mathematically, these are force-fields
in the very real sense that there is a force associated with every point in our
abstract space of allele frequencies; just as there is a gravitational force
associated with every position nearby an object with mass. Following up with
this gravity analogy, two of the forces--selection and mutation--are
deterministic forces, meaning that given certain initial conditions for the
allele composition of a population, the dynamics always reach the same end point
if only these forces are included. For example, if allele $A$ confers higher
fitness, as long as $x(t = 0) > 0$, allele $A$ will always take over the
population if selection is the only force present.

Genetic drift, the process through which allele frequency changes due to random
sampling of alleles, is by definition a stochastic force. Analogous to the
stochastic forces that kick around a Brownian particle undergoing a random walk,
the very nature of these random kicks can only be described probabilistically as
every displacement will be different. Mathematically, this implies that we can
either write down a stochastic differential equation, or, rather than writing
down an equation to describe how the allele frequency $x$ changes over time, we
can write down the dynamics for how the probability distribution of $P(x, t)$ of
observing a certain frequency x at time t changes over time.

Diffusion theory, the formulation of evolutionary dynamics we focus on this
paper, brings together all forces under the same theoretical umbrella. By
describing the time evolution of the probability distribution, we can write down
a partial differential equation analogous to the diffusion-advection equation in
real space. As we will see later on, the major mathematical difference between
traditional diffusion and allele-frequency diffusion is the fact that the
stochastic forces depend on the allele frequency. Said differently, the
diffusion coefficient in the abstract space of allele frequencies is not a
constant, but rather a function of the position in this space. Although
seemingly subtle, this complication makes the derivation and analysis of the
evolutionary dynamics significantly more difficult. To gain intuition into this
effect we will derive the partial differential equation (known as the
Fokker-Planck equation in the physics literature) from a discrete master
equation approach.


\subsection{Numerical Approximation and Analysis}\label{sec:sample-approx}

We saw in the earlier section that the source of error for the set based approach involved mis-estimation of the support of contour events in the parameter space. ￼

The stability results in section demonstrates the sensitivity of the sample based approach to errors in specifications of the initial, observed, and the push forward densities.
The latter requires construction through approximation, and is the primary contribution of errors in the updated density.
From the perspective of modelers, there is no correct choice of initial, only ones which are unsuitable for satisfying the predictability assumption.
That means that the effort is invested in characterizing the predicted distribution will have the greatest impact on the accuracy of the updated density.

There is no one method which is suitable for arbitrary choices of dimension and distribution.
However the literature on density estimation points to kernel density estimation as one of the most popular and practical methods to implement.
A kernel in this context refers to a choice of a distribution which is used to assign probabilities to samples.
Any new point can then be assigned probability by evaluating its likelihood against each distribution and averaging all of them.
It has a strong dependence on the number of samples used and the dimension which the approximation is taking place.

We demonstrate that the sources of error involved in the sample based approach lead to a trade off wherein the contours are defined on top of correct support sets but there may be inconsistency in the probabilities along the contour's direction, something that the specification of an ansatz (after the sets are constructed), prevents from occurring.

In some sense it becomes a problem of deciding what is more important to the specific use case.
Is it more of a problem to assign positive probability to points which should have been identified as not possible, or to assign incorrect probabilities to points belonging to the same equivalence class?


%#! latexmk -pv IAB_DYNAMICO_kernels
\section{\src{comp_pvort}}

\subsection{Description}

Kernel \src{comp_pvort} is taken from the original subroutine
\src{compute_pvort} in \DYNAMICO.
%
This subroutine is originally defined in module \src{caldyn_gcm_mod}.
%
This module defines subroutine \src{caldyn}, which is the main
subroutines for dynamics part of the model, and several sub-subroutines
for various terms in the governing equation, such as potential
vorticity, geopotential, etc.
%
This subroutine calculates potential vorticity.

\subsection{Discretization and code}

\autoref{l:definition_comp_pvort} shows the definition part of this subroutine
and \autoref{f:pad_comp_pvort} shows the PAD of
this.
%
Note that sources shown here are modified in the process of
kernelization from the original distributed version.

\begin{LstF90}[%
caption={Definition part of \src{compute_pvort}},%
label={l:definition_comp_pvort}%
]
SUBROUTINE compute_pvort(ps,u,theta_rhodz, rhodz,theta,qu,qv)
  USE icosa
  USE disvert_mod, ONLY : mass_dak, mass_dbk, caldyn_eta, eta_mass
  USE exner_mod
  USE trace
  USE omp_para
  IMPLICIT NONE
  REAL(rstd),INTENT(IN)  :: u(iim*3*jjm,llm)
  REAL(rstd),INTENT(IN)  :: ps(iim*jjm)
  REAL(rstd),INTENT(IN)  :: theta_rhodz(iim*jjm,llm)
  REAL(rstd),INTENT(INOUT) :: rhodz(iim*jjm,llm)
  REAL(rstd),INTENT(INOUT) :: theta(iim*jjm,llm)
  REAL(rstd),INTENT(INOUT) :: qu(iim*3*jjm,llm)
  REAL(rstd),INTENT(INOUT) :: qv(iim*2*jjm,llm)

  INTEGER :: i,j,ij,l
  REAL(rstd) :: etav,hv, m
\end{LstF90}
%
Here \src{u}, \src{ps}, \src{theta_rhodz} are velocity on the edge, surface pressure,
and mass-weighted potential temperature, respectively.
Output arrays \src{rhodz}, \src{theta}, \src{qu}, \src{qv} are mass, potential temperature,
potential vorticity on the edge, and potential vorticity on the vertex, respectively.
%
These arrays except \src{ps} have two dimensions, first one is for
horizontal index and second one is for vertical index.
%
Note that \DYNAMICO adopts C-grid in horizontal, number of horizontal
grid point for scalar, or number of control volume, in one domain is
\src{iim*jjm}, but \src{u} and \src{qu} are defined on the edge of
control volume, the size of the first dimension of them are
\src{iim*3*jjm}.  Also \src{qv} is defined on the vertex of control
volume, the size of the first dimension of \src{qv} is \src{iim*2*jjm}.

\begin{figure}[htbp]
 \centering
 \includegraphics[scale=.4]{figs/pvort.pdf}
 \caption{PAD of \src{compute_pvort}}\label{f:pad_comp_pvort}%
\end{figure}

The first section of this subroutine calculates \src{theta} in the
double loop of \src{ij} and \src{l}.
%
Note that in this kernel package, \src{caldyn_eta} is set as
\src{eta_mass}, that means that vertical coordinate $\eta$ uses
Lagrangian vertical coordinate, rather than mass coordinate.
%
In the second section, \src{qv} at two points, \src{qv(ij+z_up,l)} and
\src{qv(ij+z_down,l)}, are calculated in the first double loop, then
\src{qu} at three pounts, \src{qu(ij+u_right,l)}, \src{qu(ij+u_lup,l)}
and \src{qu(ij+l_down,l)} are calculated.

\clearpage

\subsection{Input data and result}

Input data file is prepared and you can download from official server using
\file{data/download.sh} script.
%
This data file is created by original \DYNAMICO\footnotemark with
Held-Suarez case parameter set included in the original source archive.
%
\footnotetext{with slight modification by AICS.}
%
Max/min/sum of input/output data of the kernel subroutine are output as
a log.
%
Below is an example of \src{$IAB_SYS=Ubuntu-gnu-ompi} case.

\begin{LstLog}
 [KERNEL] comp_pvort
 *** Start  initialize
                iim, jjm, llm:    23    25    19
             ij_begin, ij_end:    48   528
     ij_begin_ext, ij_end_ext:    24   552
             ll_begin, ll_end:     1    19
        t_right, t_rup, t_lup:     1    23    22
     t_left, t_ldown, t_rdown:    -1   -23   -22
        u_right, u_rup, u_lup:     0  1173   575
     u_left, u_ldown, u_rdown:    -1  1150   553
           z_rup, z_up, z_lup:   598     0   597
     z_ldown, z_down, z_rdown:   -23   575   -22
                   caldyn_eta:     1
                            g:     9.80000000
 +check[Av              ] max=  4.1228713627140027E+11,min=  0.0000000000000000E+00,sum=  3.2428753277257527E+13
 +check[de              ] max=  4.5171816240714993E+06,min=  0.0000000000000000E+00,sum=  4.7785815753077912E+08
 +check[Riv2            ] max=  3.8193271158709069E-01,min=  0.0000000000000000E+00,sum=  9.6499999999999977E+02
 +check[fv              ] max=  8.2383275804860789E-05,min= -6.6879410680009186E-05,sum=  4.1115175112148659E-03
 +check[mass_dak        ] max=  3.9864758943842335E+03,min= -4.2200064724847380E+03,sum=  1.1368683772161603E-13
 +check[mass_dbk        ] max=  1.6280745944237918E-01,min=  0.0000000000000000E+00,sum=  1.0000000000000000E+00
 *** Finish initialize
 *** Start kernel
 ### check point iteration:        1000
 ### Input ###
 +check[u               ] max=  4.1278968179782127E-01,min= -4.1278968179782127E-01,sum=  1.6791131703073393E+01
 +check[ps              ] max=  1.0000000000000000E+05,min=  1.0000000000000000E+05,sum=  5.7500000000000000E+07
 +check[theta_rhodz     ] max=  3.9393370687019045E+05,min=  0.0000000000000000E+00,sum=  1.8099621340626464E+09
 +check[theta_prev      ] max=  8.0139914420291746E+02,min=  0.0000000000000000E+00,sum=  3.8582633571973117E+06
 +check[rhodz_prev      ] max=  1.2306877011993038E+03,min=  0.0000000000000000E+00,sum=  5.3979591836733194E+06
 +check[qu_prev         ] max=  1.0339537867296609E-06,min= -8.4408169682701225E-07,sum=  3.9419811615778674E-04
 +check[qv_prev         ] max=  1.0397552030841796E-06,min= -8.4408169685381862E-07,sum=  2.6984926372303133E-04
 ### Output ###
 +check[theta           ] max=  8.0139914420291746E+02,min=  0.0000000000000000E+00,sum=  3.8582633571973117E+06
 +check[rhodz           ] max=  1.2306877011993038E+03,min=  0.0000000000000000E+00,sum=  5.3979591836733194E+06
 +check[qu              ] max=  1.0626772908333491E-06,min= -8.5290650439776975E-07,sum=  3.9864842567446446E-04
 +check[qv              ] max=  1.0855993800007362E-06,min= -8.9078811385791910E-07,sum=  2.7461310165802981E-04
 ### final iteration:        1000
 ### Validation : grid-by-grid diff ###
 +check[theta           ] max=  0.0000000000000000E+00,min=  0.0000000000000000E+00,sum=  0.0000000000000000E+00
 +check[rhodz           ] max=  0.0000000000000000E+00,min=  0.0000000000000000E+00,sum=  0.0000000000000000E+00
 +check[qu              ] max=  0.0000000000000000E+00,min=  0.0000000000000000E+00,sum=  0.0000000000000000E+00
 +check[qv              ] max=  0.0000000000000000E+00,min=  0.0000000000000000E+00,sum=  0.0000000000000000E+00
 *** Finish kernel
\end{LstLog}

Check the lines below \src{``Validation : grid-by-grid diff''} line,
that shows difference between calculated output array and
pre-calculated reference array.
These should be zero or enough small to be acceptable.
%
There are sample output log files in \file{reference/}
in each kernel program directory, for reference purpose.

\subsection{Sample of performance result}

Here's an example of the performance result part of the log output.
Below is an example executed with the machine environment described in \autoref{s:measuring_env}.
%
Note that in this program kernel part is iterated 1000 times.

\begin{LstLog}
 *** Computational Time Report
 *** ID=001 : MAIN_comp_pvort                  T=     0.248 N=   1000
\end{LstLog}


%
% STAT 100: Chance and Data Analysis - A Course Overview
% Section: Measurement of Data
%
% Author: Jeffrey Leung
%

\section{Measurement of Data}
	\label{sec:measurement-of-data}
\subsection{Introduction}
	\label{subsec:measurement-of-data:introduction}
\begin{easylist}

	& \emph{Measurement:} Collection of quantitative data

	& \emph{Unit of measurement:} System of set values to quantify data
		&& Comparisons between measurements must use the same units

	& \emph{Instrument:} Tool to measure a quantitative characteristic of an individual
		&& E.g. Ruler, scale

\end{easylist}
\subsection{Imprecision of Measurements}
	\label{subsec:measurement-of-data:imprecision-of-measurements}
\begin{easylist}

	& \emph{Uncertainty (measurement):} Possible error in a measurement due to imprecision of an instrument
	& \emph{Bias (measurement):} Difference between the measured value and the true value of a quantitative characteristic
	& Uncertainty and bias can be reduced by using instruments with higher precision

	& \emph{Random error:} Variations between repeated measurements on the same individual
		&& E.g. Surveying 100 people out of a population of 1000; recording the weight of a live, energetic pig
		&& Can be reduced by averaging multiple repeated measurements
		&& The less the random error, the more reliable the measurement

	& \emph{Variance:} Unreliability of a measurement (calculated from multiple measurements
		&& Calculation:
		\begin{displaymath}
			Variance =
			\frac
			{
				\sum\limits_{i=0}^{n}
				(x_{i} - \bar{x})^{2}
			}
			{
				n - 1
			}
		\end{displaymath}
		\Deactivate
		\begin{center}
			\begin{tabular}{ l r @{ = } l }
				where & $n$ & number of measurements \\
				& $x_{i}$ & measurement number $i$ \\
				& $\bar{x}$ & average of measurements
			\end{tabular}
		\end{center}
		\Activate

		&& E.g. Find the variance of the measurements 220 lbs, 224 lbs, 217 lbs, and 227 lbs.

		\Deactivate
		\begin{center}
			\begin{tabular}{ r @{ = } l }
				$n$ & 4 \\
				$\bar{x}$ & $\frac{220 + 224 + 217 + 227}{4} = \frac{888}{4} = 222$
			\end{tabular}
		\end{center}
		\Activate

		\Deactivate
		\begin{IEEEeqnarray}{ r C l }
			Variance
			& = & \frac
			{
				\sum\limits_{i=0}^{n}
				(x_{i} - \bar{x})^{2}
			}
			{
				n - 1
			} \\
			& = & \frac
			{
				(220-222)^{2} + (224-222)^{2} + (217-222)^{2} + (227-222)^{2}
			}
			{
				4-1
			} \\
			& = & \frac
			{
				4 + 4 + 25 + 25
			}
			{
				3
			} \\
			& = & \frac{58}{3}
		\end{IEEEeqnarray}
		\Activate

\end{easylist}
\subsection{Relevancy of a Measurement}
	\label{subsec:measurement-of-data:relevancy-of-a-measurement}
\begin{easylist}

	& A valid measurement should be a relevant representation of the property to be studied, and not some other property

	& Rates are a standard measure of comparison, while counts are not
		&& E.g. Out of 50 people, the morning class had 20 attendees. Out of 100 people, the evening class had 30 attendees. \smallskip \\
		Comparing the number/count of subjects, the evening class (30) had greater attendance than the morning class (20). \\
		Comparing the rates of subjects, the morning class ($\frac{20}{50} = 0.4$) had greater attendance than the evening class ($\frac{30}{100} = 0.3$).

\end{easylist}
\clearpage