\documentclass[a4paper,11pt]{article}

\usepackage{fancyhdr}
\usepackage[dvips]{graphicx}  % for eps and MatLab Diagrams
\usepackage{amsmath}
\usepackage{psfrag,color}
\usepackage[framed]{/Applications/TeX/mcode}

\pagestyle{fancy}

\newcommand{\G}{\mcode{GRIDFIT}}

\begin{document} % Begin Document

% Title
\title{\textsc{Understanding Gridfit}}

% Authors and Contact Information
\author{\textbf{John R. D'Errico}\\
Email: woodchips@rochester.rr.com}

\maketitle

\section{Introduction}

\G{} is a surface modeling tool, fitting a surface of the form
$z(x,y)$ to scattered (or regular) data. As it is not an
interpolant, it allows the existence of replicates in your data with
no problems. What do I mean by an "interpolant"? An interpolant is a
code that is designed to always exactly predict all supplied data.
\mcode{GRIDDATA} and \mcode{INTERP1} are examples of interpolants.
\G{} is more accurately described as an approximant. It produces a
surface which represents the behavior of the supplied data as
closely as possible, allowing for noise in the data and for
replicate data.

A nice feature of \G{} is its ability to smoothly extrapolate beyond
the convex hull of your data, something that \mcode{GRIDDATA} cannot
do.\footnote{except by the slow, memory intensive 'v4' method}
Finally, \G{} does its extrapolation in a well behaved manner,
unlike how polynomial models (for example) might behave in
extrapolation.

\G{} also allows you to build a gridded surface directly from your
data, rather than interpolating a linear approximation to a surface
from a delaunay triangulation.

This document describes the ideas behind \G.

\section{The mechanical and philosophical underpinnings of GRIDFIT}

How does \G{} work? Imagine a thin, flexible plate, attached to your
data points by elastic bands. Each of these elastic bands draws the
plate towards its corresponding data point, stretching only in the
direction. If your data points were to fall on a simple planar
surface,
\begin{equation}
    z(x,y) = a_0 + a_1 x + a_2 y
 \end{equation}
then the result will be a least squares plane. This works because
the potential energy stored in a elastic band\footnote{perfect,
linearly elastic} will be proportional to the square of its
extension.

Now imagine that the data points do not fall on a plane, but arise
from some curved surface $z(x,y)$. While the plate itself is
flexible, I've allowed it some finite and non-zero bending rigidity
that the user can control. The bands connecting the plate to our
data points will pull the plate into a curved shape, while the
bending rigidity of the plate resists deformation. The relative
stiffness of the plate, as compared to the strength of the elastic
bands will cause some tradeoff between fitting the data as well as
possible and a smooth overall surface. It is this plate stiffness
that allows \G{} to smoothly extrapolate beyond the data itself. Of
course, extrapolation beyond the data will be well behaved, since
the plate will tend to become locally planar wherever there is no
data to deform it.

The tradeoff between stiffness of the plate and the effective spring
constant of the bands connecting the plate to the data allows the
user to choose anything between the extremes of a purely planar fit,
to a crinkly thing that can follow the bumps and hollows of noisy
data.

\section{The methodology of GRIDFIT}

First, lets look at how \G{} predicts a value at any location. Since
the \G{} surface is defined by values at a set of nodes forming a
rectangular lattice, any data point must lie in one of these
rectangular cells of the lattice. (One requirement of \G{} is that
the boundaries of its lattice must form a bounding box for all of
the data supplied in the $(x,y)$ plane.)

Given a point inside a single rectangular cell of the lattice, there
are three methods supplied in \G{} to impute a value at that point.
They are:
\begin{itemize}
  \item Nearest neighbor interpolation
  \item Triangle (linear) interpolation
  \item Bilinear (tensor product linear) interpolation
\end{itemize}

These methods were chosen to be simple to generate, as well as
because they are very "local" methods.  They require no more than a
few local values from the grid to predict the value at each point.
This local nature makes the sparse linear least squares problem as
efficient as possible, by keeping it maximally sparse.

If we think of the surface that will result from \G{} as essentially
a low order spline, it is easy to visualize the idea that
interpolation at any point inside the grid is just a linear
combination of the values at the grid nodes in the locality of the
given point. Thus, we can write the interpolation problem in general
as a linear algebra problem
\begin{equation}
    A\mathbf{x}=\mathbf{y}
\end{equation}
where the vector $\mathbf{x}$ is of length $nx\cdot{}ny$ ($nx$ is
the number of nodes in the $\mathbf{x}$ direction, and $ny$ is the
number of grid nodes in the $\mathbf{y}$ direction.) Thus $A$ has
$n$ rows, corresponding to each data point supplied by the user, and
$nx\cdot{}ny$ columns.

It will not be atypical to expect that there will be more columns
than rows in this "regression" problem. For example, a user with 100
data points might choose to build a gridded surface which has
$100\times100$ nodes. The resulting regression problem will be
massively underdetermined. As such, standard regression techniques,
involving a solution like \mcode{x = A$\backslash{}$y} or \mcode{x =
pinv(A)*y}, will be inappropriate.

The approach that \G{} takes, with its plate-like metaphor, is to
solve a regularized problem. At every node of the surface,
\G{}\footnote{using its default regularization method of 'gradient'}
attempts to force the (first) partial derivatives of the surface in
neighboring cells to be equal. In effect, this results in a second
set of linear equations of the form
\begin{equation}
    B\mathbf{x} = \mathbf{0}
\end{equation}
where the derivatives are approximated using finite differences of
the surface at neighboring nodes.

The main alternative regularization method in \G{} is the
'laplacian' method. It attempts to force the sum of second partial
derivatives to zero. This is actually closely related to the
gradient method, but with a subtly significant difference. Either
method will work nicely. (The 'springs' regularizer for \G{} is a
flawed one, at least in my humble opinion. I'd generally recommend
not using it without care. I've left it in the code because it has
interesting properties in extrapolation.)

The result of any of these regularizers is a system of the form
$B\mathbf{x}=\mathbf{0}$. Coupled with the interpolation system,
$A\mathbf{x}=\mathbf{y}$, we solve them in combination using a
simple trick. First, I scale $A$ and $B$ so that each matrix has a
unit 1-norm. This helps to eliminate scaling problems with the
derivatives. Then I solve for the vector $\mathbf{x}$ such that
\begin{equation}
    \|(A\mathbf{x}-\mathbf{y})\|^2 + \lambda\|B\mathbf{x}\|^2
\end{equation}
is minimized. Note that the parameter $\lambda$, nominally 1 as a
default in \G, allows the user to control the relative plate
"stiffness". As $\lambda$ approaches zero, the plate becomes as
flexible as possible. Stiffer plates are modeled by larger values,
$\lambda>1$.

\section{Tricks with tiles}

What can you do when a \G{} problem is simply too large even for the
sparse matrix abilities of MATLAB? This is where the tiling
abilities of \G{} become valuable. What do I mean by tiling? An
example might be appropriate.

\begin{lstlisting}
    >> nodes = linspace(0,1,100);
    >> tic,...
    zg=gridfit(rand(100,1),rand(100,1),rand(100,1),nodes,nodes);...
    toc
    Elapsed time is 5.814102 seconds.
    >>
    >> nodes = linspace(0,1,200);
    >> tic,...
    zg=gridfit(rand(100,1),rand(100,1),rand(100,1),nodes,nodes);...
    toc
    Elapsed time is 27.118945 seconds.
\end{lstlisting}
\bigskip

To solve for a $100\times100$ grid, \G{} must solve for 10000
unknown parameters using sparse linear algebra. A $200\times200$
grid is nominally 4 times as large, and it took my computer a bit
more than $4\times$ as long to solve. However, a $2000\times2000$
grid will probably take much more than 400 times as long to solve as
a $100\times100$ grid. It will likely take very much longer, if it
runs at all without exceeding the memory limits, due to the much
slower access times for virtual memory. For example, my computer
took 1020 seconds to solve a $500\times500$ problem.
\begin{lstlisting}
    >> nodes = linspace(0,1,500);
    >> tic,...
    zg=gridfit(rand(100,1),rand(100,1),rand(100,1),nodes,nodes);...
    toc
    Elapsed time is 1020.657834 seconds.
\end{lstlisting}
\bigskip

A trick that can work, IF you have enough data to adequately
populate each tile, is to break down the domain of a large grid into
smaller chunks, each of which can be quickly populated by \G. Then
\G{} composes each chunk into the whole surface. To smooth out any
artifacts at the edges of each tile, \G{} allows the tiles some
fractional overlap, interpolating between them in the overlapped
region. Thus the tiled solution below was accomplished with
essentially no dips into virtual memory, so it took far less time to
generate.

\begin{lstlisting}
    >> nodes = linspace(0,1,500);
    >> tic,...
    zg=gridfit(rand(100,1),rand(100,1),rand(100,1),nodes,nodes,...
    'tilesize',200,'overlap',0.2);...
    toc
    Elapsed time is 211.946853 seconds.
\end{lstlisting}
\bigskip

Note: these random surfaces were generated purely to show speed of
estimation. They are of course purely garbage. Also, only 100 data
points may well be inadequate to estimate a tiled surface. \G{} is
not strongly dependent on the number of data points for its speed,
so that number did not have an impact on the speed of estimation.

A picture:

\begin{lstlisting}
    % Users of gridfit who have really huge problems now have
    % an option. I'll generate a large amount of data,
    % and hope to model a fairly large grid - 800 x 800. This
    % would normally require gridfit to solve a system of
    % equations with 640,000 unknowns. It would probably be too
    % large of a problem for my computer, were I to use gridfit
    % on the full problem. Gridfit allows you to break the problem
    % into smaller tiles if you choose. In this case each tile
    % is 120x120, with a 25% (30 element) overlap between tiles.

    % Relax, this demo may take a couple of minutes to run!!!!

    n = 100000;
    x = rand(n,1);
    y = rand(n,1);
    z = x+y+sin((x.^2+y.^2)*10);

    xnodes = 0:.00125:1;
    ynodes = xnodes;

    [zg,xg,yg] = gridfit(x,y,z,xnodes,ynodes,'tilesize',...
    120,'overlap',0.25);

    surf(xg,yg,zg)
    shading interp
    colormap(jet(256))
    camlight right
    lighting phong
    title 'Tiled gridfit'
\end{lstlisting}

\begin{figure}
\centering
    \includegraphics[width=5in]{gfit.eps}
        \caption{An Example}
\end{figure}

\end{document}


% 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{ifestar2}
\section*{\hspace*{-1.6cm} ifestar2}

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

{\bf \large \sf Purpose}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
Instantaneous frequency estimation using AR2 modelisation.
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Synopsis}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
[fnorm,t2,ratio] = ifestar2(x)
[fnorm,t2,ratio] = ifestar2(x,t)
\end{verbatim}
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Description}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
        {\ty ifestar2} computes an estimation of the instantaneous
        frequency of the real signal {\ty x} at time instant(s) {\ty t}
        using an auto-regressive model of order 2. The result {\ty fnorm}
        lies between 0.0 and 0.5. This estimate is based only on the 4 last
        signal points, and has therefore an approximate delay of 2.5
        points. \\

\hspace*{-.5cm}\begin{tabular*}{14cm}{p{1.5cm} p{8.5cm} c}
Name & Description & Default value\\
\hline
        {\ty x}     & real signal to be analyzed\\
        {\ty t}     & time instants (must be greater than 4) &
         {\ty (4:length(x))}\\
\hline  {\ty fnorm} & output (normalized) instantaneous frequency\\
        {\ty t2}    & time instants coresponding to {\ty fnorm}. Since the
                algorithm do not systematically give a value, {\ty t2} is
                different from {\ty t} in general\\
        {\ty ratio} & proportion of instants where the algorithm yields
                an estimation\\
\hline
\end{tabular*}
\vspace*{.1cm}

This estimator is the causal version of the estimator called "4 points
Prony estimator" in article [1].
\end{minipage}
\vspace*{1cm}

{\bf \large \sf Example}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
Here is a comparison between the instantaneous frequency estimated by {\ty
ifestar2} and the exact instantaneous frequency law, obtained on a
sinusoidal frequency modulation :
\begin{verbatim}
         [x,if]=fmsin(100,0.1,0.4); x=real(x);
         [if2,t]=ifestar2(x);
         plot(t,if(t),t,if2);
\end{verbatim}
The estimation follows quite correctly the right law, but with a small bias
and with some weak oscillations.
\end{minipage}

%\newpage

{\bf \large \sf See Also}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
instfreq, kaytth, sgrpdlay.
\end{verbatim}
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Reference}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
[1] Prony "Instantaneous frequency estimation using linear prediction with
	comparisons to the dESAs", IEEE Signal Processing Letters, Vol 3,
	No 2, p 54-56, February 1996.
\end{minipage}


% !Tex program = pdflatex
% Chapter 5 - The quantum Fourier transform and its applications
\ifx\allfiles\undefined
\documentclass[en]{sol-man}
\begin{document}
\fi
\chapter{The quantum Fourier transform and its applications}

\section{The quantum Fourier transform}

\begin{exe}
    Give a direct proof that the linear transform defined by Equation (5.2)\footnote{\label{Equ-5.2}$\lvert j\rangle\longrightarrow\frac{1}{\sqrt{N}}\sum_{k=0}^{N-1}e^{2\pi ijk/N}\lvert k\rangle$.} is unitary.
\end{exe}
\begin{pf}
    Set the matrix of the linear transform defined by Equation (5.2) as $\hat{U}$,
    \begin{align}
        \hat{U}\lvert j\rangle=\frac{1}{\sqrt{N}}\sum_{k=0}^{N-1}e^{2\pi ijk/N}\lvert k\rangle.
    \end{align}
    Since for any $\lvert j\rangle$
    \begin{align}
        \notag\langle j\rvert\hat{U}^{\dagger}\hat{U}\lvert j\rangle=&\left(\frac{1}{\sqrt{N}}\sum_{l=0}^{N-1}e^{2\pi ijl/N}\lvert l\rangle\right)^{\dagger}\left(\frac{1}{\sqrt{N}}\sum_{k=0}^{N-1}e^{2\pi ijk/N}\lvert k\rangle\right)=\frac{1}{N}\sum_{k,l=0}^{N-1}e^{2\pi ij(k-l)/N}\langle l\vert k\rangle=\frac{1}{N}\sum_{k,l=0}^{N-1}e^{2\pi ij(k-l)/N}\delta_{lk}\\
        =&\frac{1}{N}\sum_{k=0}^{N-1}1=1=\langle j\vert j\rangle,
    \end{align}
    \begin{align}
        \Longrightarrow\hat{U}^{\dagger}\hat{U}=I,
    \end{align}
    i.e., the linear transform defined by Equation (5.2)\footref{Equ-5.2} is unitary.
\end{pf}

\begin{exe}
    Explicitly compute the Fourier transform of the $n$ qubit state $\lvert 00\dots 0\rangle$.
\end{exe}
\begin{sol}
    The Fourier transform of the $n$ qubit state $\lvert 00\dots 0\rangle$ is
    \begin{align}
        \lvert 00\dots 0\rangle=\lvert 0\rangle\longrightarrow\frac{1}{2^{n/2}}\sum_{k=0}^{2^n-1}\lvert k\rangle=\frac{1}{2^{n/2}}=\left[\frac{1}{\sqrt{2}}(\lvert 0\rangle+\lvert 1\rangle)\right]^n.
    \end{align}
\end{sol}

\begin{exe}[Classical fast Fourier transform]
    Suppose we wish to perform a Fourier transform of a vector containing $2^n$ complex numbers on a classical computer. Verify that the straightforward method for performing the Fourier transform, based upon direct evaluation of Equation (5.1)\footnote{\label{Equ-5.1}$y_k\equiv\frac{1}{\sqrt{N}}\sum_{j=0}^{N-1}x_je^{2\pi ijk/N}$.} requires $\Theta(2^{2n})$ elementary arithmetic operations. Find a method for reducing this to $\Theta(n2^n)$ operations, based upon Equation (5.4)\footnote{\label{Equ-5.4}$\lvert j_1,\cdots,j_n\rangle\rightarrow\frac{\left(\lvert 0\rangle+e^{2\pi i0.j_n}\lvert 1\rangle\right)\left(\lvert 0\rangle+e^{2\pi i0.j_{n-1}j_n}\lvert 1\rangle\right)\cdots\left(\lvert 0\rangle+e^{2\pi i0.j_1j_2\cdots j_n}\lvert 1\rangle\right)}{2^{n/2}}$}.
\end{exe}
\begin{sol}
    According to Equation (5.1)\footref{Equ-5.1}, every element $y_k$ in vector $\lvert y\rangle$ takes $2^n$ elementary arithmetic operations. Since there are $2^n$ elements in vector $\lvert y\rangle$, the straightforward method for performing the Fourier transform, based upon direct evaluation of Equation (5.1)\footref{Equ-5.1} requires $\Theta(2^{2n})$ elementary arithmetic operations.

    Based upon Equation (5.1)\footref{Equ-5.1}. For every element $y_k$ in vector $\lvert y\rangle$,
    \begin{align}
        \notag y_k=&\frac{1}{2^{n/2}}\sum_{j=0}^{2^n-1}x_je^{2\pi ijk/N}=\sum_{j=0}^{2^{n-1}-1}x_{2j}e^{2\pi i(2j)k/N}+\sum_{j=0}^{2^{n-1}-1}x_{2j+1}e^{2\pi i(2j+1)k/N}\\
        =&\sum_{j=0}^{2^{n-1}-1}x_{2j}e^{2\pi i(2j)k/N}+e^{2\pi ik/N}\sum_{j=0}^{2^{n-1}-1}x_{2j+1}e^{2\pi i(2j)k/N}.
    \end{align}
    Define $T(\text{A})$ as the elementary arithmetic operation number needed (computational complexity) for algorithm A. Here,
    \begin{align}
        \notag&T(\text{Fourier transform of a vector containing $2^n$ complex numbers})\\
        \notag=&T(\text{calculate $\sum_{j=0}^{2^{n-1}-1}x_{2j}e^{2\pi i(2j)k/N}$ for }k=1,2,\cdots,2^n)+T(\text{calculate $\sum_{j=0}^{2^{n-1}-1}x_{2j+1}e^{2\pi i(2j)k/N}$ for }k=1,2,\cdots,2^n)+2^n\\
        \notag=&2T(\text{Fourier transform of a vector containing $2^{n-1}$ complex numbers})+2^n.
    \end{align}
    In this way,
    \begin{align}
        T(\text{Fourier transform of a vector containing $2^n$ complex numbers})=O(n2^n),
    \end{align}
    i.e., we reduce Fourier transform of a vector containing $2^n$ complex numbers to $\Theta(n2^n)$ elementary arithmetic operations.
\end{sol}

\begin{exe}
    Give a decomposition of the controlled-$R_k$ gate into single qubit and CNOT gates.
\end{exe}
\begin{sol}
    The $R_k$ gate can be written as
    \begin{align}
        R_k=\begin{bmatrix}
            1&0\\
            0&e^{2\pi i/2^k}
        \end{bmatrix}=e^{i\alpha}\begin{bmatrix}
            e^{i(\alpha-\beta/2-\delta/2)}\cos\frac{\gamma}{2}&-e^{i(\alpha-\beta/2+\delta/2)}\sin\frac{\gamma}{2}\\
            e^{i(\alpha+\beta/2-\delta/2)}\sin\frac{\gamma}{2}&e^{i(\alpha+\beta/2+\delta/2)}\cos\frac{\gamma}{2}
        \end{bmatrix},
    \end{align}
    i.e.,
    \begin{align}
        e^{i(\alpha-\beta/2-\delta/2)}\cos\frac{\gamma}{2}=&1,\\
        -e^{i(\alpha-\beta/2+\delta/2)}\sin\frac{\gamma}{2}=&0,\\
        e^{i(\alpha+\beta/2-\delta/2)}\sin\frac{\gamma}{2}=&0,\\
        e^{i(\alpha+\beta/2+\delta/2)}\cos\frac{\gamma}{2}=&e^{2\pi i/2^k},
    \end{align}
    so we can choose
    \begin{align}
        \alpha=&\pi/2^k,\\
        \beta=&2\pi/2^k,\\
        \delta=&0,\\
        \gamma=&0.
    \end{align}
    In this way, $R_k$ gate can be decomposed as
    \begin{align}
        R_k=e^{i\alpha}AXBXC,
    \end{align}
    where
    \begin{align}
        \alpha=&\frac{\pi}{2^k},\\
        A=&R_z(\beta)R_y(\gamma/2)=\begin{bmatrix}
            e^{-i\beta/2}&0\\
            0&e^{i\beta/2}
        \end{bmatrix}\begin{bmatrix}
            \cos\frac{\gamma}{4}&-\sin\frac{\gamma}{4}\\
            \sin\frac{\gamma}{4}&\cos\frac{\gamma}{4}
        \end{bmatrix}=\begin{bmatrix}
            e^{-i\beta/2}\cos\frac{\gamma}{4}&-e^{-i\beta/2}\cos\frac{\gamma}{4}\\
            e^{i\beta/2}\sin\frac{\gamma}{4}&e^{i\beta/2}\cos\frac{\gamma}{4}
        \end{bmatrix}=\begin{bmatrix}
            e^{-i\pi/2^k}&0\\
            0&e^{i\pi/2^k}
        \end{bmatrix},\\
        \notag B=&R_y(-\gamma/2)R_z(-(\delta+\beta)/2)=\begin{bmatrix}
            \cos\left(-\frac{\gamma}{4}\right)&-\sin\left(-\frac{\gamma}{4}\right)\\
            \sin\left(-\frac{\gamma}{4}\right)&\cos\left(-\frac{\gamma}{4}\right)
        \end{bmatrix}\begin{bmatrix}
            e^{i(\delta+\beta)/4}&0\\
            0&e^{-i(\delta+\beta)/4}
        \end{bmatrix}=\begin{bmatrix}
            e^{i(\delta+\beta)/4}\cos\frac{\gamma}{4}&e^{-i(\delta+\beta)/4}\sin\frac{\gamma}{4}\\
            -e^{i(\delta+\beta)/4}\sin\frac{\gamma}{4}&e^{-i(\delta+\beta)/4}\cos\frac{\gamma}{4}
        \end{bmatrix}\\
        =&\begin{bmatrix}
            e^{i\pi/2^{k+1}}&0\\
            0&e^{-i\pi/2^{k+1}}
        \end{bmatrix},\\
        C=&R_z((\delta-\beta)/2)=\begin{bmatrix}
            e^{-i(\delta-\beta)/4}&0\\
            0&e^{i(\delta-\beta)/4}
        \end{bmatrix}=\begin{bmatrix}
            e^{-i\pi/2^{k+1}}&0\\
            0&e^{i\pi/2^{k+1}}
        \end{bmatrix}.
    \end{align}
    Using the equation in Figure 4.6\footref{Fig-4.6-controlled-U-construction}, the decomposition of the controlled-$R_k$ gate into single qubit and CNOT gates is
    \begin{center}
        \begin{quantikz}
            \qw & \ctrl{1} & \qw\\
            \qw & \gate{R_k} & \qw
        \end{quantikz}$=$\begin{tikzcd}[ampersand replacement=\&]
            \qw \& \qw \& \ctrl{1} \& \qw \& \ctrl{1} \& \gate{\begin{bmatrix}1&0\\0&e^{i\alpha}\end{bmatrix}} \& \qw\\
            \qw \& \gate{C} \& \targ{} \& \gate{B} \& \targ{} \& \gate{A} \& \qw
        \end{tikzcd}
    \end{center}
\end{sol}

\ifx\allfiles\undefined
\end{document}
\fi


\documentclass[a4paper]{scrartcl}
\usepackage[margin=1.5cm]{geometry}

\usepackage{amsmath}
\usepackage{cleveref}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amsthm}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{booktabs}
\usepackage{csquotes}
\usepackage{algpseudocode}
\usepackage{pdflscape}

\renewcommand{\vec}{\mathbf}
\renewcommand{\Re}{\operatorname{Re}}
\renewcommand{\Im}{\operatorname{Im}}
\newcommand{\dd}[1]{\,\mathrm{d}#1}
\newcommand{\ca}[1]{\accentset{\circ}{#1}}
\newcommand{\vp}[2]{\left<#1,#2\right>}
\newcommand{\abs}[1]{\left\lvert#1\right\rvert}
\newcommand{\diag}[1]{\mathrm{diag}\left(#1\right)}
\newcommand{\R}{\mathbb{R}}

\renewcommand{\epsilon}{\varepsilon}
\renewcommand{\theta}{\vartheta}

\begin{document}
\textbf{\Huge Warning: These are my notes. Don't expect this document to be self-contained and correct.}

\section{ADER-DG for linear acoustics}
The linearized acoustic equations are given as (see Finite Volume book by LeVeque)
\begin{equation}\label{eq:pde}
 \frac{\partial Q_p}{\partial t} + A_{pq}\frac{\partial Q_q}{\partial x} + B_{pq}\frac{\partial Q_q}{\partial y} = 0,
\end{equation}
where
\begin{equation}
 q = \begin{pmatrix}p \\ u \\ v\end{pmatrix}, \quad
 A = \begin{pmatrix}0 & K_0 & 0 \\ 1/\rho_0 & 0 & 0 \\ 0 & 0 & 0 \end{pmatrix}, \quad
 B = \begin{pmatrix}0 & 0 & K_0 \\ 0 & 0 & 0 \\ 1/\rho_0 & 0 & 0 \end{pmatrix}.
\end{equation}

The corresponding weak form is
\begin{equation}
 \int_{\Omega}\phi_k\frac{\partial Q_p}{\partial t}\dd{V} +
 \int_{\partial\Omega}\phi_k \left(n_xA_{pq} + n_yB_{pq}\right)Q_q\dd{S} -
 \int_{\Omega}\left(\frac{\partial \phi_k}{\partial x}A_{pq}Q_q + \frac{\partial \phi_k}{\partial y}B_{pq}Q_q\right)\dd{V} = 0,
\end{equation}
where $n=(n_x,n_y)$ is the outward unit surface normal.

We discretise the weak form with finite elements, which are axis aligned rectangles $\mathcal R^{(m)}$, and obtain
\begin{equation}
 \int_{\mathcal R^{(m)}}\phi_k\frac{\partial Q_p}{\partial t}\dd{V} +
 \int_{\partial\mathcal R^{(m)}}\phi_k \left(\left(n_xA_{pq} + n_yB_{pq}\right)Q_q\right)^*\dd{S} -
 \int_{\mathcal R^{(m)}}\left(\frac{\partial \phi_k}{\partial x}A_{pq} + \frac{\partial \phi_k}{\partial y}B_{pq}\right)Q_q\dd{V} = 0,
\end{equation}
where we a numerical flux (indicated with *).

Suppose we are given a grid of points
$P_{i,j} = \left(ih_x, jh_y\right)$, where $(i,j) \in [0,X]\times [0,Y]$ and $h_x,h_y > 0$.
Then a rectangle $R^{(m)}$ with $m=(i,j)$, where $i < X$ and $j < Y$, is given by the four points
$\left\{P_{i,j}, P_{i+1,j}, P_{i,j+1}, P_{i+1,j+1}\right\}$.

We approximate $Q$ with a modal basis, i.e.
\begin{equation}
 Q_p^h(x,y,t) = \hat{Q}_{lp}\left(t\right)\phi_l\left(\xi^{(m)}(x,y), \eta^{(m)}(x,y)\right),
\end{equation}
where
\begin{equation}
 \xi^{(m)}(x,y) = \frac{x-(P_m)_1}{h_x}, \quad
 \eta^{(m)}(x,y) = \frac{y-(P_m)_2}{h_y}.
\end{equation}

Then we obtain, using the substitution rule,
\begin{multline}
 |J|\frac{\partial \hat{Q}_{lp}}{\partial t}(t)\int_{0}^{1}\int_{0}^{1}\phi_k(\xi,\eta)\phi_l(\xi,\eta)\dd{\eta}\dd{\xi} \\
 + h_x\int_{0}^{1}\phi_k(\xi,1)\left(B_{pq}Q_q\right)^* \dd{\xi} - h_x\int_{0}^{1}\phi_k(\xi,0)\left(B_{pq}Q_q\right)^* \dd{\xi} \\
 + h_y\int_{0}^{1}\phi_k(1,\eta)\left(A_{pq}Q_q\right)^* \dd{\eta} - h_y\int_{0}^{1}\phi_k(0,\eta)\left(A_{pq}Q_q\right)^* \dd{\eta} \\
 - |J|\hat{Q}_{lp}(t)\int_{0}^{1}\int_{0}^{1}\left(\frac{1}{h_x}\frac{\partial \phi_k}{\partial \xi}(\xi,\eta)A_{pq} + \frac{1}{h_y}\frac{\partial \phi_k}{\partial \eta}(\xi,\eta)B_{pq}\right)\phi_l(\xi,\eta)\dd{\eta}\dd{\xi} = 0,
\end{multline}
where $|J|=h_xh_y$.

We turn now to the flux term. First, note that we may use rotational invariance:
\begin{equation}
 n_xA + n_yB = TAT^{-1},
\end{equation}
where
\begin{equation}
T(n_x,n_y)=\begin{pmatrix}
 1 & 0 & 0 \\
 0 & n_x & -n_y \\
 0 & n_y & n_x
\end{pmatrix}, \quad
T^{-1}(n_x,n_y)=\begin{pmatrix}
 1 & 0 & 0 \\
 0 & n_x & n_y \\
 0 & -n_y & n_x
\end{pmatrix},
\end{equation}
i.e. we only need to solve the Riemann problem in x-direction. In the homogeneous case we have
\begin{equation}
 A^{+} = \frac{1}{2}\begin{pmatrix}
 c & K_0 & 0 \\
 1/\rho_0 & c & 0 \\
 0 & 0 & 0
\end{pmatrix}, \quad
 A^{-} = \frac{1}{2}\begin{pmatrix}
 -c & K_0 & 0 \\
 1/\rho_0 & -c & 0 \\
 0 & 0 & 0
\end{pmatrix},
\end{equation}
where $c=\sqrt{K_0/\rho_0}$.

In the inhomogeneous case we have
\begin{equation}
 A^{+} = \begin{pmatrix}
 \frac{K_0^+c^-c^+}{K_0^-c^++K_0^+c^-} & \frac{K_0^-K_0^+c^+}{K_0^-c^++K_0^+c^-} & 0 \\
 \frac{K_0^+c^-}{\rho_0^+\left(K_0^-c^++K_0^+c^-\right)} & \frac{K_0^-K_0^+}{\rho_0^+\left(K_0^-c^++K_0^+c^-\right)} & 0 \\
 0 & 0 & 0
\end{pmatrix}, \quad
 A^{-} = \begin{pmatrix}
 -\frac{K_0^-c^-c^+}{K_0^-c^++K_0^+c^-} & \frac{K_0^-K_0^+c^-}{K_0^-c^++K_0^+c^-} & 0 \\
 \frac{K_0^-c^+}{\rho_0^-\left(K_0^-c^++K_0^+c^-\right)} & -\frac{K_0^-K_0^+}{\rho_0^-\left(K_0^-c^++K_0^+c^-\right)} & 0 \\
 0 & 0 & 0
\end{pmatrix}.
\end{equation}
Hence, the flux is given as
\begin{equation}
 TA^-T^{-1}Q^- + TA^+T^{-1}Q^+.
\end{equation}
For ease of notation, we define
\begin{equation}
 \mathcal{A}^{x,y,\pm} = T(x,y)A^\pm T(x,y)^{-1}.
\end{equation}

Note, that we need the transposed version as we multiply the matrix from the right. Hence,
with abuse of notation we define

\begin{equation}
 \mathcal{A}^{x,y,\pm} = T^{-T}\left(A^\pm\right)^T T^{T} = T\left(A^\pm\right)^T T^{-1},
\end{equation}
where we used that $T^{-1}=T^T$.

We are now able to obtain the complete semi-discrete scheme:
\begin{multline}
 |J|\frac{\partial \hat{Q}_{lp}}{\partial t}(t)\int_{0}^{1}\int_{0}^{1}\phi_k(\xi,\eta)\phi_l(\xi,\eta)\dd{\eta}\dd{\xi} \\
 + h_x\mathcal{A}_{pq}^{0,1,+}\hat{Q}_{lp}(t)\int_{0}^{1}\phi_k(\xi,1)\phi_l(\xi,1) \dd{\xi}
 + h_x\mathcal{A}_{pq}^{0,1,-}\hat{Q}^{(i,j+1)}_{lp}(t)\int_{0}^{1}\phi_k(\xi,1)\phi_l(\xi,0) \dd{\xi} \\
 + h_x\mathcal{A}_{pq}^{0,-1,+}\hat{Q}_{lp}(t)\int_{0}^{1}\phi_k(\xi,0)\phi_l(\xi,0) \dd{\xi}
 + h_x\mathcal{A}_{pq}^{0,-1,-}\hat{Q}^{(i,j-1)}_{lp}(t)\int_{0}^{1}\phi_k(\xi,0)\phi_l(\xi,1) \dd{\xi} \\
 + h_y\mathcal{A}_{pq}^{1,0,+}\hat{Q}_{lp}(t)\int_{0}^{1}\phi_k(1,\eta)\phi_l(1,\eta) \dd{\eta}
 + h_y\mathcal{A}_{pq}^{1,0,-}\hat{Q}^{(i+1,j)}_{lp}(t)\int_{0}^{1}\phi_k(1,\eta)\phi_l(0,\eta) \dd{\eta} \\
 + h_y\mathcal{A}_{pq}^{-1,0,+}\hat{Q}_{lp}(t)\int_{0}^{1}\phi_k(0,\eta)\phi_l(0,\eta) \dd{\eta}
 + h_y\mathcal{A}_{pq}^{-1,0,-}\hat{Q}^{(i-1,j)}_{lp}(t)\int_{0}^{1}\phi_k(0,\eta)\phi_l(1,\eta) \dd{\eta} \\
 - |J|\hat{Q}_{lp}(t)\int_{0}^{1}\int_{0}^{1}\left(\frac{1}{h_x}\frac{\partial \phi_k}{\partial \xi}(\xi,\eta)A_{pq} + \frac{1}{h_y}\frac{\partial \phi_k}{\partial \eta}(\xi,\eta)B_{pq}\right)\phi_l(\xi,\eta)\dd{\eta}\dd{\xi} = 0,
\end{multline}

To be precomputed:
\begin{align*}
 M_{kl} &= \int_{0}^{1}\int_{0}^{1}\phi_k(\xi,\eta)\phi_l(\xi,\eta)\dd{\eta}\dd{\xi} \\
 F_{kl}^{x,-,s} &= \int_{0}^{1}\phi_k(\xi,s)\phi_l(\xi,s) \dd{\xi} \\
 F_{kl}^{x,+,s} &= \int_{0}^{1}\phi_k(\xi,s)\phi_l(\xi,1-s) \dd{\xi} \\
 F_{kl}^{y,-,s} &= \int_{0}^{1}\phi_k(s,\eta)\phi_l(s,\eta) \dd{\eta} \\
 F_{kl}^{y,+,s} &= \int_{0}^{1}\phi_k(s,\eta)\phi_l(1-s,\eta) \dd{\eta} \\
 K_{kl}^\xi &= \int_{0}^{1}\int_{0}^{1}\frac{\partial\phi_k}{\partial\xi}(\xi,\eta)\phi_l(\xi,\eta)\dd{\eta}\dd{\xi} \\
 K_{kl}^\eta &= \int_{0}^{1}\int_{0}^{1}\frac{\partial\phi_k}{\partial\eta}(\xi,\eta)\phi_l(\xi,\eta)\dd{\eta}\dd{\xi}
\end{align*}

\subsection{$L^2$ projection}
It might become necessary to project a function on the basis functions, i.e. we require the integral
\begin{equation}
 |J|\int_{0}^{1}\int_{0}^{1}\phi_k(\xi,\eta)f\left(x(\xi,\eta),y(\xi,\eta)\right)\dd{\eta}\dd{\xi}
\end{equation}
which can be approximated with a quadrature rule $(\chi, \omega)$ (on $[-1,1]$).
\begin{multline}
 |J|\int_{0}^{1}\int_{0}^{1}\phi_k(\xi,\eta)f\left(x(\xi,\eta),y(\xi,\eta)\right)\dd{\eta}\dd{\xi} = \\
 \frac{|J|}{2}\int_{0}^{1}\sum_{j=0}^N\omega_j\phi_k\left(\xi,\frac{\chi_j+1}{2}\right)f\left(x\left(\xi,\frac{\chi_j+1}{2}\right),y\left(\xi,\frac{\chi_j+1}{2}\right)\right)\dd{\xi} =\\
 \frac{|J|}{4}\sum_{i=0}^N\sum_{j=0}^N\omega_i\omega_j\phi_k\left(\frac{\chi_i+1}{2},\frac{\chi_j+1}{2}\right)f\left(x\left(\frac{\chi_i+1}{2},\frac{\chi_j+1}{2}\right),y\left(\frac{\chi_i+1}{2},\frac{\chi_j+1}{2}\right)\right)\dd{\xi}
\end{multline}

\subsection{Convergence test}
We assume homogeneous material parameters and we assume that our solution is a plane wave of the form
\begin{equation}
 Q_p(x,y,t) = Q_p^0\sin\left(\omega t-k_xx-k_yy\right).
\end{equation}
Inserting the solution into \Cref{eq:pde} yields
\begin{equation}
 \left(\omega I_{pq} - k_xA_{pq} - k_yB_{pq}\right)Q_q^0\cos\left(\omega t-k_xx-k_yy\right) = 0,
\end{equation}
where $I$ is an identity matrix. So either the cosine term is zero or
\begin{equation}
 \left(\omega I_{pq} - k_xA_{pq} - k_yB_{pq}\right)Q_q^0 = 0
\end{equation}
must hold. This is equivalent to the following eigenvalue problem:
\begin{equation}
 \left(k_xA_{pq} + k_yB_{pq}\right)Q_q^0 = \omega Q_q^0
\end{equation}
So $\omega$ must be an eigenvalue and $Q_q^0$ must be an eigenvector of the matrix
\begin{equation}
 k_xA + k_yB = \lVert k\rVert T\left(\frac{k_x}{\lVert k\rVert},\frac{k_y}{\lVert k\rVert}\right)AT\left(\frac{k_x}{\lVert k\rVert},\frac{k_y}{\lVert k\rVert}\right)^{-1},
\end{equation}
where we used rotational invariance. Let $(\lambda_i,r_i)$ be eigenvalue and corresponding
eigenvector of $A$. Then
\begin{equation}
 Q^0 = T\left(\frac{k_x}{\lVert k\rVert},\frac{k_y}{\lVert k\rVert}\right)r_i, \text{ and } \omega = \lVert k\rVert\lambda_i
\end{equation}
solves the homogeneous problem with initial condition $Q_p(x,y,0) = Q_p^0\sin(-k_xx-k_yy)$.

For the convergence test we choose $r_i = (K_0, c, 0)^T, \omega = c, k_x=2\pi, k_y=2\pi$. Then
\begin{equation}
 T\left(\sqrt{2}/2,\sqrt{2}/2\right)r_i = \begin{pmatrix}
              1 & 0 & 0 \\
              0 & \sqrt{2}/2 & -\sqrt{2}/2 \\
              0 & \sqrt{2}/2 & \sqrt{2}/2 \\
             \end{pmatrix}
\begin{pmatrix}K_0\\ c \\ 0\end{pmatrix} =
\begin{pmatrix}K_0\\ \sqrt{2}c/2 \\ \sqrt{2}c/2\end{pmatrix}, \text{ and } \omega = 2\sqrt{2}\pi c.
\end{equation}
Due to periodic boundary conditions, after $1/2$ seconds the solution is equal to the initial condition.

\subsection{Source terms}
Assume we have a source term of the form
\begin{equation}
 S_p(t)\delta(x_s,y_s)
\end{equation}
then we need to add
\begin{equation}
 \frac{1}{|J|}M_{kq}^{-1}\phi_q\left(\xi(x_s,y_s),\eta(x_s,y_s) \right)\int_{t_n}^{t_n+\Delta t} S_p(t)\dd{t}
\end{equation}
to the cell that contains the source term.

\end{document}


%% ----------------------------------------------------------------
%% Appendix: Additional SANS derivations
%% ----------------------------------------------------------------
%!TeX root = subfile
\label{chapter:appendixC}
\markboth{Appendix C Additional SANS derivations}{Appendix C Additional SANS derivations}
\documentclass[../main.tex]{subfiles}
% ----------------------------------------------------------------
\begin{document}
\section{The spanwise-averaged vorticity transport equation}
\label{sec:savte}
The non-dimensional vorticity transport equation (VTE) can be written in its conservation form as
\begin{equation}
\partial_t\boldsymbol\omega+\nabla \cdot \pars{\vect{u}\otimes\boldsymbol{\omega}}=\nabla \cdot \pars{\boldsymbol{\omega}\otimes\vect{u}}+Re^{-1}\nabla^2\boldsymbol{\omega}.\label{eq:VTE}
\end{equation}
Decomposing and averaging similarly to \sref{sec:sans_math}, the following spanwise-averaged VTE can be obtained for the $\vect{e}_z$ component of \eref{eq:VTE},
\begin{gather}
\partial_t {\Omega}_z + U\partial_x{\Omega}_z + V\partial_y{\Omega}_z = Re^{-1}\pars{\partial_{xx}{\Omega}_z + \partial_{yy}{\Omega}_z}-\nonumber \\
-\partial_x\pars{\avg{u\p\omega_z\p}-\avg{\omega_x\p w\p}}-\partial_y\pars{\avg{v\p\omega_z\p}-\avg{\omega_y\p w\p}}+\nonumber \\
+{\Omega}_x\partial_xW+{\Omega}_y\partial_yW+{\Omega}_z\avg{\partial_z w\p}-W\avg{\partial_z\omega_z\p}+Re^{-1}\avg{\partial_{zz}\omega_z\p}, \label{eq:vort_e3_avg}
\end{gather}
where the spanwise-averaged divergence of the velocity and vorticity vector fields, respectively
\begin{gather}
\partial_x{U}+\partial_y{V}=-\avg{\partial_z{w\p}},\\
\partial_x{{\Omega}_x}+\partial_y{{\Omega}_y}=-\avg{\partial_z{\omega_z\p}},
\end{gather}
has been introduced to obtain the non-conservation form.

When spanwise periodicity is assumed, the third line in \eref{eq:vort_e3_avg} vanishes by its last three terms cancelling by definition (\eref{eq:periodic_assumption}), and the first two terms cancelling each other as follows,
\begin{gather}
{\Omega}_x\partial_xW+{\Omega}_y\partial_yW=\avg{\partial_y{w}-\partial_z{v}}\partial_x{W}+\avg{\partial_z{u}-\partial_z{w}}\partial_y{W}=\nonumber\\
=\avg{\partial_y{\pars{W+w\p}}-\partial_z{\pars{V+v\p}}}\partial_x{W}+\avg{\partial_z{\pars{U+u\p}}-\partial_z{\pars{W+v\p}}}\partial_y{W}=\nonumber\\
=\pars{\partial_y{W}-\cancelto{0}{\avg{\partial_z{v\p}}}}\partial_x{W}+\pars{\cancelto{0}{\avg{\partial_z{u\p}}}-\partial_x{W}}\partial_y{W}=0.
\end{gather}

The spanwise-averaged VTE is then simplified to
\begin{equation}
\partial_t {\Omega}_z + \vect{U}\cdot\nabla{\Omega}_z = Re^{-1}\nabla^2{\Omega}_z-\nabla\cdot\boldsymbol\zeta^R,
\end{equation}
where $\vect{U}=\pars{U,V}$ and $\boldsymbol \zeta^R = \pars{\avg{u\p\omega_z\p}-\avg{\omega_x\p w\p}, \avg{v\p\omega_z\p}-\avg{\omega_y\p w\p}}$.

\subsection{Cylindrical coordinates} \label{sec:cyl_coords}

In a cylindrical coordinates system, using $(\vect{e}_r,\vect{e}_\theta,\vect{e}_z)$ and
\begin{equation}
\nabla = \pars{\frac{1}{r}\pd{\pars{r\cdot}}{r}, \frac{1}{r}\pd{\pars{\cdot}}{\theta}, \pd{\pars{\cdot}}{z}},
\end{equation}
the $\vect{e}_z$ component of the non-dimensional VTE (\eref{eq:VTE}) can be written as
\begin{gather}
\pd{\omega_z}{t}+\frac{1}{r}\pd{\pars{r u_r\omega_z}}{r}+\frac{1}{r}\pd{\pars{u_\theta\omega_z}}{\theta}+\cancel{\pd{\pars{u_r\omega_z}}{z}}=\nonumber\\
=\frac{1}{r}\pd{\pars{r \omega_r u_z}}{r}+\frac{1}{r}\pd{\pars{\omega_\theta u_z}}{\theta}+\cancel{\pd{\pars{\omega_z u_r}}{z}}+\frac{1}{Re}\pars{\ddn{\omega_z}{r}{2}+\frac{1}{r}\pd{\omega_z}{r}+\frac{1}{r^2}\ddn{\omega_z}{\theta}{2}+\ddn{\omega_z}{z}{2}}.
\label{eq:VTE_cyl}
\end{gather}

A decomposition and average similarly to \sref{sec:sans_math} yields
\begin{gather}
\pd{{\Omega}_z}{t}+\frac{1}{r}\pd{\pars{r U_r{\Omega}_z+\avg{r u_r\p\omega_z\p}}}{r}+\frac{1}{r}\pd{\pars{U_\theta{\Omega}_z+\avg{u_\theta\p\omega_z\p}}}{\theta}=\nonumber\\
=\frac{1}{r}\pd{\pars{r {\Omega}_r U_z + \avg{r\omega_r\p u_z\p}}}{r}+\frac{1}{r}\pd{\pars{{\Omega}_\theta U_z+\avg{\omega_\theta\p u_z\p}}}{\theta}+\nonumber\\
+\frac{1}{Re}\pars{\ddn{{\Omega}_z}{r}{2}+\frac{1}{r}\pd{{\Omega}_z}{r}+\frac{1}{r^2}\ddn{{\Omega}_z}{\theta}{2}+\avg{\ddn{\omega_z\p}{z}{2}}}.
\label{eq:VTE_cyl_avg}
\end{gather}

The spanwise-averaged divergence of the velocity and vorticity fields can be respectively written in cylindrical coordinates as
\begin{gather}
\frac{1}{r}\pd{\pars{rU_r}}{r}+\frac{1}{r}\pd{U_\theta}{\theta}=-\avg{\pd{u_z\p}{z}},\label{eq:cont_avg_cyl}\\
\frac{1}{r}\pd{\pars{r{\Omega}_r}}{r}+\frac{1}{r}\pd{{\Omega}_\theta}{\theta}=-\avg{\pd{\omega_z\p}{z}}\label{eq:divort_avg_cyl},
\end{gather}
and introducing \eref{eq:cont_avg_cyl} and \eref{eq:divort_avg_cyl} into \eref{eq:VTE_cyl_avg} yields
\begin{gather}
\pd{{\Omega}_z}{t}+U_r\pd{{\Omega}_z}{r}+\frac{U_\theta}{r}\pd{{\Omega}_z}{\theta}={\Omega}_r\pd{U_z}{r}+\frac{{\Omega}_\theta}{r}\pd{U_z}{\theta}+\frac{1}{Re}\pars{\ddn{{\Omega}_z}{r}{2}+\frac{1}{r}\pd{{\Omega}_z}{r}+\frac{1}{r^2}\ddn{{\Omega}_z}{\theta}{2}}-\nonumber\\
-\frac{1}{r}\bracs{\pd{}{r}\pars{\avg{r u_r\p\omega_z\p}-\avg{r \omega_r\p u_z\p}}+
\pd{}{\theta}\pars{\avg{u_\theta\p\omega_z\p}-\avg{\omega_\theta\p u_z\p}}}+\nonumber\\
+{\Omega}_z\avg{\pd{u_z\p}{z}}-U_z\avg{\pd{\omega_z\p}{z}}+\frac{1}{Re}\avg{\ddn{\omega_z\p}{z}{2}}.
\end{gather}

\subsection{Relationship between SANS and spanwise-averaged VTE formulations}

Next, we show how the spanwise stresses in the SANS and the spanwise-averaged VTE frameworks are related.
First, we derive an expression for the vorticity-velocity terms of the spanwise-averaged VTE to write them as velocity-velocity terms.
Considering the following vector identity
\begin{equation}
\frac{1}{2} \nabla \left( \vect{a}\cdot\vect{a} \right) = (\vect{a} \cdot \nabla) \vect{a} + \vect{a} \times (\nabla \times \vect{a}),
\end{equation}
we can rewrite the vector identity in terms of fluctuating quantities
\begin{equation}
\frac{1}{2} \nabla \left( \vect{u}\p\cdot\vect{u}\p \right) = (\vect{u}\p \cdot \nabla) \vect{u}\p + \vect{u}\p \times \boldsymbol{\omega}\p,\label{eq:vect_identity_fluct}
\end{equation}
where the definition of the vorticity vector field, $\vect{\omega}=\nabla\times\vect{u}$, has been introduced.

Second, the conservation form of the convective forces can be expressed as
\begin{equation}
\nabla\cdot\pars{\vect u \otimes\vect u}=\pars{\vect u \cdot \nabla}\vect u + \vect u\pars{\nabla\cdot\vect u},
\end{equation}
and it can be rewritten for the fluctuating quantities as
\begin{gather}
\nabla\cdot\pars{\vect u\p \otimes\vect u\p}=\pars{\vect u\p \cdot \nabla}\vect u\p + \vect u\p\pars{\nabla\cdot\vect u\p},\\
\pars{\vect u\p \cdot \nabla}\vect u\p = \nabla\cdot\pars{\vect u\p \otimes\vect u\p}-\vect u\p\pars{\nabla\cdot\vect u\p}.\label{eq:conv_term_fluct}
\end{gather}

Third, decomposing the continuity equation for incompressible flows (\eref{eq:cont})
\begin{equation}
\partial_xU+\partial_xu\p+\partial_yV+\partial_yv\p+\partial_zw\p=0,\label{eq:continuity_decomp}
\end{equation}
and subtracting the averaged continuity equation (\eref{eq:cont_decomp_avg}) yields
\begin{equation}
\partial_xu\p+\partial_yv\p+\partial_zw\p-\avg{\partial_z w\p}=0,
\end{equation}
hence
\begin{equation}
\nabla\cdot\vect u\p =\avg{\partial_z w\p}.\label{eq:cont_fluct}
\end{equation}

We now introduce \eref{eq:cont_fluct} into \eref{eq:conv_term_fluct} obtaining
\begin{equation}
\pars{\vect u\p \cdot \nabla}\vect u\p = \nabla\cdot\pars{\vect u\p \otimes\vect u\p}-\vect u\p\avg{\partial_z w\p},\label{eq:conv_term_fluct2}
\end{equation}
and \eref{eq:conv_term_fluct2} is finally introduced into \eref{eq:vect_identity_fluct}
\begin{equation}
\frac{1}{2} \nabla \left( \vect{u}\p\cdot\vect{u}\p \right) = \nabla\cdot\pars{\vect u\p \otimes\vect u\p}-\vect u\p\avg{\partial_z w\p} + \vect{u}\p \times \boldsymbol{\omega}\p,
\end{equation}
which yields
\begin{equation}
\boldsymbol{\omega}\p\times\vect{u}\p = \nabla\cdot\pars{\vect u\p \otimes\vect u\p} - \frac{1}{2} \nabla \left( \vect{u}\p\cdot\vect{u}\p \right) - \vect u\p\avg{\partial_z w\p}.
\label{eq:vect_identity_fluct2}
\end{equation}

With this, a relation between the vorticity-velocity spanwise stresses and the velocity-velocity spanwise stresses is defined in the conservation form.
To derive a more explicit form of the spanwise-averaged stresses, let us average \eref{eq:vect_identity_fluct2} and retrieve its $\vect{e}_x$ and $\vect{e}_y$ components
\begin{gather}
\vect{e}_x: \quad \avg{\omega_y\p w\p}-\avg{\omega_z\p v\p} = \partial_x\avg{u\p u\p}+\partial_y\avg{v\p u\p}+\avg{\partial_z\pars{w\p u\p}}-\nonumber\\
-\partial_x\pars{\avg{u\p u\p}+\avg{v\p v\p}+\avg{w\p w\p}}/2,\label{eq:vec_ident_fluct_e1}\\
\vect{e}_y: \quad \avg{\omega_z\p u\p}-\avg{\omega_x\p w\p} = \partial_x\avg{u\p v\p}+\partial_y\avg{v\p v\p}+\avg{\partial_z\pars{w\p v\p}}-\nonumber\\
-\partial_y\pars{\avg{u\p u\p}+\avg{v\p v\p}+\avg{w\p w\p}}/2\label{eq:vec_ident_fluct_e2}.
\end{gather}
Note that the last term of \eref{eq:vect_identity_fluct2} vanishes because of \eref{eq:rule2}.

The spanwise stresses of the spanwise-averaged VTE (\eref{eq:vort_e3_avg}) can be expanded using Eqs. \ref{eq:vec_ident_fluct_e1} and \ref{eq:vec_ident_fluct_e2} as follows
\begin{gather}
-\partial_x\pars{\avg{u\p\omega_z\p}-\avg{\omega_x\p w\p}}-\partial_y\pars{\avg{v\p\omega_z\p}-\avg{\omega_y\p w\p}} = \nonumber\\
=-\partial_x\pars{\partial_x\avg{u\p v\p}+\partial_y\avg{v\p v\p}+\avg{\partial_z\pars{w\p v\p}} - \partial_y\pars{\avg{u\p u\p}+\avg{v\p v\p}+\avg{w\p w\p}}/2} +\nonumber\\
+\partial_y\pars{\partial_x\avg{u\p u\p}+\partial_y\avg{v\p u\p}+\avg{\partial_z\pars{w\p u\p}} - \partial_x\pars{\avg{u\p u\p}+\avg{v\p v\p}+\avg{w\p w\p}}/2}=\nonumber\\
=-\partial_{xx}\avg{u\p v\p}-\partial_{xy}\avg{v\p v\p}-\partial_x\avg{\partial_z\pars{w\p v\p}}+\partial_{xy}\pars{\avg{u\p u\p}+\avg{v\p v\p}+\avg{w\p w\p}}/2+\nonumber\\
+\partial_{xy}\avg{u\p u\p}+\partial_{yy}\avg{v\p u\p}+\partial_y\avg{\partial_z\pars{w\p u\p}}-\partial_{xy}\pars{\avg{u\p u\p}+\avg{v\p v\p}+\avg{w\p w\p}}/2=\nonumber\\
=\pars{\partial_{yy}-\partial_{xx}}\avg{u\p v\p}+\partial_{xy}\pars{\avg{u\p u\p}-\avg{v\p v\p}}+\partial_y\avg{\partial_z\pars{w\p u\p}}-\partial_x\avg{\partial_z\pars{w\p v\p}}\label{eq:vte_spanwise_stresses}.
\end{gather}

On the other hand, the SANS equations (Eqs. \ref{eq:z-avg_X} to \ref{eq:z-avg_Z}) have yield the following spanwise stresses
\begin{equation}
-\avg{\nabla\cdot\pars{\vect u\p\otimes\vect u\p}}=
-\begin{pmatrix}
    \partial_x\avg{u\p u\p}+\partial_y\avg{u\p v\p}+\avg{\partial_z\pars{w\p u\p}} \\
    \partial_x\avg{v\p u\p}+\partial_y\avg{v\p v\p}+\avg{\partial_z\pars{w\p v\p}} \\
    \partial_x\avg{w\p u\p}+\partial_y\avg{w\p v\p}+\avg{\partial_z\pars{w\p w\p}}
  \end{pmatrix}.
\end{equation}
Taking the curl of the momentum spanwise stresses
\begin{equation}
-\nabla\times\avg{\nabla\cdot\pars{\vect u\p\otimes\vect u\p}}=-
\begin{pmatrix}
    \vect{e}_x & \partial_x & \partial_x\avg{u\p u\p}+\partial_y\avg{u\p v\p}+\avg{\partial_z\pars{w\p u\p}} \\
    \vect{e}_y & \partial_y & \partial_x\avg{v\p u\p}+\partial_y\avg{v\p v\p}+\avg{\partial_z\pars{w\p v\p}} \\
    \vect{e}_z & \partial_z & \partial_x\avg{w\p u\p}+\partial_y\avg{w\p v\p}+\avg{\partial_z\pars{w\p w\p}}
  \end{pmatrix},
\end{equation}
recovers the VTE spanwise stresses as demonstrated for the $\vect{e}_z$ component
\begin{gather}
-\nabla\times\avg{\nabla\cdot\pars{\vect u\p\otimes\vect u\p}}\vect{e}_z=\nonumber\\
=-\partial_x\pars{\partial_x\avg{v\p u\p}+\partial_y\avg{v\p v\p}+\avg{\partial_z\pars{w\p v\p}}}
+\partial_y\pars{\partial_x\avg{u\p u\p}+\partial_y\avg{u\p v\p}+\avg{\partial_z\pars{w\p u\p}}}=\nonumber\\
=-\partial_{xx}\avg{v\p u\p}-\partial_{xy}\avg{v\p v\p}-\partial_x\avg{\partial_z\pars{w\p v\p}}+\partial_{xy}\avg{u\p u\p}+\partial_{yy}\avg{u\p v\p}+\partial_y\avg{\partial_z\pars{w\p u\p}}=\nonumber\\
=\pars{\partial_{yy}-\partial_{xx}}\avg{u\p v\p}+\partial_{xy}\pars{\avg{u\p u\p}-\avg{v\p v\p}}+\partial_y\avg{\partial_z\pars{w\p u\p}}-\partial_x\avg{\partial_z\pars{w\p v\p}},\label{eq:NS_spanwise_stresses}
\end{gather}
showing that the spanwise stresses in both formulations (Eqs. \ref{eq:vte_spanwise_stresses} and \ref{eq:NS_spanwise_stresses}) are equivalent.

\section{Analytical validation and quadrature error analysis}

The aim of this exercise is to analytically demonstrate the correct derivation of the SANS equations.
The velocity vector field needs to be solenoidal and periodic, in agreement with the assumption used for the derivation of the simplified SANS equations (\eref{eq:sans_simple}). For this, the Taylor--Green vortex case is considered in a $2\pi$-periodic box.
The pressure field is taken constant.
The components of its periodic solenoidal initial velocity vector field might be written as
\begin{gather}
u = \cos (x) \sin (y) \sin (z), \label{eq:tg-u} \\
v = \sin (x) \cos (y) \sin (z), \\
w = -2 \sin (x) \sin (y) \cos (z),\label{eq:tg-w}
\end{gather}
which is indeed a solenoidal vector field
\begin{gather}
\nabla \cdot \vect{u} = \pd{u}{x}+\pd{v}{y}+\pd{w}{z}=\nonumber\\
=- \sin (x) \sin (y) \sin (z)- \sin (x) \sin (y) \sin (z)+2\sin (x) \sin (y) \sin (z)=0.
\end{gather}

\subsection{No quadrature errors and spanwise periodicity}
\label{sec:sans_analytical}

The averaging operation in the spanwise direction $\avg{\cdot}$ is defined on the $z\in\left[a,b\right]$ interval as
\begin{equation}
\avg{q}\pars{x,y,t} = \frac{1}{b-a}\int^b_a q\pars{x,y,z,t}\,dz.
\end{equation}
Numerically, this carries a quadrature error which we will neglect for the moment.
The following condition needs to hold true assuming an error-free quadrature and spanwise periodicity,
\begin{equation}
\avg{\mathcal{S}\left(\vect{u}, p\right)} = \tilde{\mathcal{S}}\left(\vect{U}, P\right) + \nabla\cdot\boldsymbol\tau_{ij}^R, \label{eq:balance}
\end{equation}
where
\begin{align}
\mathcal{S}= \vect{u}\cdot\nabla \vect{u} + \nabla p - \nu \nabla^2 \vect{u}, \qquad &\vect{u}=\pars{u,v,w},\,\,\, \vect{x}=\pars{x,y,z},\\
 \tilde{\mathcal{S}}= \vect{U}\cdot\nabla \vect{U} + \nabla P - \nu \nabla^2 \vect{U}, \qquad &\vect{U} = \pars{U,V},\,\,\, \vect{x}=\pars{x,y}.
\end{align}

The derivation to validate the SANS equations will be performed for the $\vect{e}_x$ component of \eref{eq:balance}.
In a $L=b-a=2\pi$ domain and considering the velocity vector field defined in \eref{eq:tg-u} to \eref{eq:tg-w}, we obtain $\vect{U}=\pars{0,0,0}$, so $\vect{u}=\vect{u}\p$.

We begin calculating the LHS of \eref{eq:balance},
\begin{equation}
\avg{\mathcal{S}}\vect{e}_x=\avg{u\pd{u}{x}+v\pd{u}{y}+w\pd{u}{z}+\pd{p}{x}-\nu\pars{\ddn{u}{x}{2}+\ddn{u}{y}{2}+\ddn{u}{z}{2}}}.\label{eq:LHS}
\end{equation}
The required derivatives are
\begin{gather}
\begin{align}
\pd{u}{x}&=-\sin (x) \sin (y) \sin (z), \qquad \ddn{u}{x}{2}=-\cos (x) \sin (y) \sin (z)=-u,\\
\pd{u}{y}&= \cos (x) \cos (y) \sin (z), \qquad\,\,\,\,\, \ddn{u}{y}{2}=-\cos (x) \sin (y) \sin (z)=-u,\\
\pd{u}{z}&=\cos (x) \sin (y) \cos (z), \qquad\,\,\,\,\, \ddn{u}{z}{2}=-\cos (x) \sin (y) \sin (z)=-u.
\end{align}
\end{gather}

Substituting the derivatives into \eref{eq:LHS} yields
\begin{gather}
\avg{\mathcal{S}}\vect{e}_x=\avg{u\pd{u}{x}+v\pd{u}{y}+w\pd{u}{z}+\pd{p}{x}-\nu\pars{\ddn{u}{x}{2}+\ddn{u}{y}{2}+\ddn{u}{z}{2}}}= \nonumber \\
=\bigl\langle-\sin (x) \cos (x) \sin^2 (y) \sin^2 (z) + \sin (x) \cos (x) \cos^2 (y) \sin^2 (z)-\nonumber\\
\quad\;\;-2\sin (x) \cos (x) \sin^2 (y) \cos^2 (z) + 3\nu u \bigr\rangle = \nonumber\\
=-\frac{\pi}{2\pi}\sin (x) \cos (x) \sin^2 (y) + \frac{\pi}{2\pi} \sin (x) \cos (x) \cos^2 (y) -\frac{2\pi}{2\pi} \sin (x) \cos (x) \sin^2 (y) + 0=\nonumber\\
=\frac{1}{2}\sin(x) \cos(x) \pars{\cos^2(y)-3\sin^2(y)}.
\label{eq:b21_lhs}
\end{gather}

Next, we calculate the RHS of \eref{eq:balance}. Note that $\tilde{\mathcal{S}}=0$ because $\vect{U}=0$ and the pressure is constant.
Hence, only the spanwise stresses remain, analytically
\begin{gather}
\pars{\nabla\cdot\boldsymbol\tau_{ij}^R}\vect{e}_x = \pd{}{x}\avg{u\p u\p}+\pd{}{y}\avg{u\p v\p} = \pd{}{x}\avg{u u}+\pd{}{y}\avg{u v} = \nonumber \\
=\pd{}{x}\pars{\avg{\cos^2(x) \sin^2(y) \sin^2(z)}}+\pd{}{y}\pars{\avg{\sin(x)\cos(x)\sin(y)\cos(y)\sin^2(z)}}=\nonumber \\
=\pd{}{x}\pars{ \frac{\pi }{2\pi }\cos^2(x) \sin^2(y)}+\pd{}{y}\pars{\frac{\pi }{2\pi }\sin(x)\cos(x)\sin(y)\cos(y)}=\nonumber \\
=-\sin(x)\cos(x)\sin^2(y) +\frac{1}{2}\sin(x)\cos(x)\pars{\cos^2(x)-\sin^2(x)}=\nonumber \\
=\frac{1}{2} \sin(x) \cos(x) \pars{\cos^2(y)-3\sin^2(y)}.
\label{eq:b21_rhs}
\end{gather}

Finally, we can show that \eref{eq:balance} has the correct balance between both sides by bringing all the pieces together, i.e. using \eref{eq:b21_lhs} as LHS and \eref{eq:b21_rhs} as RHS,
\begin{equation}
\frac{1}{2} \sin(x) \cos(x) \pars{\cos^2(y)-3\sin^2(y)} = 0 + \frac{1}{2} \sin(x) \cos(x) \pars{\cos^2(y)-3\sin^2(y)}.
\end{equation}

\subsection{Quadrature errors without spanwise periodicity}
\label{sec:sans_quadrature_errors1}

When quadrature errors arising from the averaging operation are considered, we have to recall the second-line terms in \eref{eq:sans_full} that were neglected in the previous section.
The quadrature error depends on the function being averaged and the quadrature itself.
From here, we will consider the composite trapezoidal quadrature which has the following associated error on the integral interval $[a,b]$
\begin{equation}
\epsilon_T(\xi) = -\frac{(b-a)h^2}{12} f''(\xi), \qquad \xi \in [a,b],
\end{equation}
where $h$ is the subinterval (spacing) length defined as $h=(b-a)/N$, and $N$ is the number of subintervals for $N+1$ quadrature points.
Note that the convergence error is $\mathcal{O}\pars{h^2}$ and the presence of $f''$, which implies that the trapezoidal rule can perfectly calculate the quadrature of zero and first degree polynomial functions.

For the Taylor--Green vortex case, only first- and second-degree trigonometric functions are required. We can quantify the error associated to its quadrature using the definition above in the $2\pi$ interval as
\begin{gather}
I_1 =\int^{2\pi}_0 \sin(z) dz, \\
\epsilon_1\pars{\xi} := I_1 - I_{1,N} = \frac{\pi h^2}{6} \sin(\xi),
\end{gather}
and
\begin{gather}
I_2 =\int^{2\pi}_0 \sin^2(z) dz, \\
\epsilon_2\pars{\xi} := I_2 - I_{2,N} = -\frac{\pi h^2}{6} \bracs{2\pars{\cos^2(\xi)-\sin^2(\xi)}}.
\end{gather}
Next, we bound $\epsilon_1$ and $\epsilon_2$ with $|f''(\xi)|_{\max}$ within $[0,2\pi]$,
\begin{gather}
|\epsilon_1|_{\max} = \frac{\pi h^2}{6} \cdot 1, \\
|\epsilon_2|_{\max} = \frac{\pi h^2}{6} \cdot 2,
\end{gather}
hence
\begin{equation}
|\epsilon_2|_{\max}= 2|\epsilon_1|_{\max}.
\end{equation}
Defining $I_1^*$ and $I_2^*$ as $I^* = I/(2\pi)$, we obtain
\begin{gather}
I^*_1 = \frac{0+\epsilon}{2\pi}=\frac{\epsilon}{2\pi}, \\
I^*_2 = \frac{\pi+2\epsilon}{2\pi}=\frac{1}{2}+\frac{\epsilon}{\pi},
\end{gather}
where $\epsilon = \pi h^2/6$. Note that $I^*_1$ and $I^*_2$ using $\cos(z)$ instead of $\sin(z)$ are similarly calculated.

Considering quadrature errors, \eref{eq:balance} has to be corrected with the terms previously neglected because of spanwise periodicity,
\begin{equation}
\avg{\mathcal{S}\left(\vect{u}, p\right)} = \tilde{\mathcal{S}}\left(\vect{U}, P\right) + \nabla\cdot\boldsymbol\tau_{ij}^R+W\avg{\partial_z\vect{u}\p}-\nu\avg{\partial_{zz}\vect{u}\p}+\avg{\partial_z\pars{w\p\vect{u}\p}}.\label{eq:balance_all}
\end{equation}

We proceed by calculating again the new average and fluctuating quantities using the errors described above as follows
\begin{gather}
U = \frac{\epsilon}{2\pi}\cos(x)\sin(y),\qquad u\p=\cos(x)\sin(y)\pars{\sin(z)-\frac{\epsilon}{2\pi}},\\
V = \frac{\epsilon}{2\pi}\sin(x)\cos(y),\qquad v\p=\sin(x)\cos(y)\pars{\sin(z)-\frac{\epsilon}{2\pi}},\\
W = -\frac{\epsilon}{\pi}\sin(x)\sin(y),\,\,\,\,\, w\p=2\sin(x)\sin(y)\pars{\frac{\epsilon}{2\pi}-\cos(z)}.
\end{gather}
With this, the LHS of \eref{eq:balance_all} can be written as
\begin{gather}
\avg{\mathcal{S}}\vect{e}_x=\avg{u\pd{u}{x}+v\pd{u}{y}+w\pd{u}{z}+\pd{p}{x}-\nu\pars{\ddn{u}{x}{2}+\ddn{u}{y}{2}+\ddn{u}{z}{2}}}=\nonumber \\
=\bigl\langle-\sin (x) \cos (x) \sin^2 (y) \sin^2 (z) + \sin (x) \cos (x) \cos^2 (y) \sin^2 (z)-\nonumber\\
\quad\;\;-2\sin (x) \cos (x) \sin^2 (y) \cos^2 (z) + 3\nu u \bigr\rangle = \nonumber\\
=-\pars{\frac{1}{2}+\frac{\epsilon}{\pi}}\sin(x)\cos(x)\sin^2(y) +\pars{\frac{1}{2}+\frac{\epsilon}{\pi}}\sin(x)\cos(x)\cos^2(y) \nonumber \\
-2\pars{\frac{1}{2}+\frac{\epsilon}{\pi}}\sin(x)\cos(x)\sin^2(y)+3\nu\frac{\epsilon}{2\pi}\cos(x)\sin(y) = \nonumber\\
=\pars{\frac{1}{2}+\frac{\epsilon}{\pi}}\sin(x)\cos(x)\pars{\cos^2(y)-3\sin^2(y)}+3\nu\frac{\epsilon}{2\pi}\cos(x)\sin(y).\label{eq:S_avg}
\end{gather}

The spatial operator of the spanwise-averaged quantities $(\tilde{\mathcal{S}})$ considering quadrature errors can be written as
\begin{gather}
\tilde{\mathcal{S}}\,\vect{e}_x=U\pd{U}{x}+V\pd{U}{y}+\pd{P}{x}-\nu\pars{\ddn{U}{x}{2}+\ddn{U}{y}{2}}=\nonumber\\
=\frac{\epsilon}{2\pi}\cos(x)\sin(y)\pd{}{x}\pars{\frac{\epsilon}{2\pi}\cos(x)\sin(y)}+\frac{\epsilon}{2\pi}\sin(x)\cos(y)\pd{}{y}\pars{\frac{\epsilon}{2\pi}\cos(x)\sin(y)}+0-\nonumber\\
-\nu\bracs{\ddn{}{x}{2}\pars{\frac{\epsilon}{2\pi}\cos(x)\sin(y)}+\ddn{}{y}{2}\pars{\frac{\epsilon}{2\pi}\cos(x)\sin(y)}}=\nonumber\\
=-\pars{\frac{\epsilon}{2\pi}}^2\sin(x)\cos(x)\sin^2(x)+\pars{\frac{\epsilon}{2\pi}}^2\sin(x)\cos(x)\cos^2(y)+2\nu\frac{\epsilon}{2\pi}\cos(x)\sin(y)=\nonumber\\
=\pars{\frac{\epsilon}{2\pi}}^2\sin(x)\cos(x)\pars{\cos^2(y)-\sin^2(x)}+\nu\frac{\epsilon}{\pi}\cos(x)\sin(y).\label{eq:S_tilde}
\end{gather}

And $\pars{\nabla\cdot\boldsymbol\tau_{ij}^R}\vect{e}_x$ is calculated as follows
\begin{gather}
\pars{\nabla\cdot\boldsymbol\tau_{ij}^R}\vect{e}_x=\pd{}{x}\avg{u\p u\p}+\pd{}{y}\avg{u\p v\p} =\nonumber\\
=\pd{}{x}\pars{\avg{\cos^2(x) \sin^2(y) \pars{\sin(z)-\frac{\epsilon}{2\pi}}^2}}+\nonumber\\
+\pd{}{y}\pars{\avg{\sin(x)\cos(x)\sin(y)\cos(y)\pars{\sin(z)-\frac{\epsilon}{2\pi}}^2}}=\nonumber \\
=\pd{}{x}\pars{\cos^2(x) \sin^2(y)\avg{ \pars{\sin^2(z)+\pars{\frac{\epsilon}{2\pi}}^2-\frac{\epsilon}{\pi}\sin(z)}}}+\nonumber\\
+\pd{}{y}\pars{\sin(x)\cos(x)\sin(y)\cos(y)\avg{\pars{\sin^2(z)+\pars{\frac{\epsilon}{2\pi}}^2-\frac{\epsilon}{\pi}\sin(z)}}}=\nonumber \\
=\bracs{\frac{1}{2}+\frac{\epsilon}{\pi}+\pars{\frac{\epsilon}{2\pi}}^2-\frac{1}{2}\pars{\frac{\epsilon}{\pi}}^2}\Bigg[\pd{}{x}\pars{\cos^2(x) \sin^2(y)}+\nonumber\\
+\pd{}{y}\pars{\sin(x)\cos(x)\sin(y)\cos(y)}\Bigg]=\nonumber\\
=\bracs{\frac{1}{2}+\frac{\epsilon}{\pi}-\frac{1}{4}\pars{\frac{\epsilon}{\pi}}^2}\sin(x)\cos(x)\pars{\cos^2(y)-3\sin^2(y)}.\label{eq:divT}
\end{gather}

Next, we calculate the terms neglected in the previous section
\begin{gather}
W\avg{\partial_z\vect{u}\p}-\nu\avg{\partial_{zz}\vect{u}\p}+\avg{\partial_z\pars{w\p\vect{u}\p}}=\nonumber \\
=-\frac{\epsilon}{\pi}\sin(x)\sin(y)\avg{\pd{}{z}\pars{\cos(x)\sin(y)\pars{\sin(z)-\frac{\epsilon}{2\pi}}}}-\nonumber \\
-\nu\avg{\ddn{}{z}{2}\pars{\cos(x)\sin(y)\pars{\sin(z)-\frac{\epsilon}{2\pi}}}}+\nonumber \\
+\avg{\pd{}{z}\pars{\cos(x)\sin(y)\pars{\sin(z)-\frac{\epsilon}{2\pi}}2\sin(x)\sin(y)\pars{\frac{\epsilon}{2\pi}-\cos(z)}}}=\nonumber
\end{gather}
\begin{gather}
=-\frac{\epsilon}{\pi}\sin(x)\cos(x)\sin^2(y)\avg{\cos(z)}+\nu\cos(x)\sin(y)\avg{\sin(z)}+\nonumber\\
+2\sin(x)\cos(x)\sin^2(y)\avg{\pd{}{z}\pars{\frac{\epsilon}{2\pi}\sin(z)+\frac{\epsilon}{2\pi}\cos(z)-\sin(z)\cos(z)-\pars{\frac{\epsilon}{2\pi}}^2}}=\nonumber\\
=-\frac{1}{2}\pars{\frac{\epsilon}{\pi}}^2\sin(x)\cos(x)\sin^2(y)+\nu\frac{\epsilon}{2\pi}\cos(x)\sin(y)+\nonumber\\
+2\sin(x)\cos(x)\sin^2(y)\avg{\frac{\epsilon}{2\pi}\cos(z)-\frac{\epsilon}{2\pi}\sin(z)+\sin^2(z)-\cos^2(z)}=\nonumber\\
=-\frac{1}{2}\pars{\frac{\epsilon}{\pi}}^2\sin(x)\cos(x)\sin^2(y)+\nu\frac{\epsilon}{2\pi}\cos(x)\sin(y)+\nonumber\\
+2\sin(x)\cos(x)\sin^2(y)\cancelto{0}{\bracs{\pars{\frac{\epsilon}{2\pi}}^2-\pars{\frac{\epsilon}{2\pi}}^2+\frac{1}{2}+\frac{\epsilon}{\pi}-\frac{1}{2}-\frac{\epsilon}{\pi}}}=\nonumber\\
=-\frac{1}{2}\pars{\frac{\epsilon}{\pi}}^2\sin(x)\cos(x)\sin^2(y)+\nu\frac{\epsilon}{2\pi}\cos(x)\sin(y).\label{eq:extra}
\end{gather}
We note that $\avg{\partial_z\pars{w\p\vect{u}\p}}$ automatically cancels out.

Putting all the pieces together (Eqs. \ref{eq:S_avg}, \ref{eq:S_tilde}, \ref{eq:divT} \& \ref{eq:extra} into \eref{eq:balance_all}) we obtain the error between the balance of its LHS and RHS as follows
\begin{gather}
\mathcal{E} = \left|\avg{\mathcal{S}\left(\vect{u}, p\right)} - \tilde{\mathcal{S}}\left(\vect{U}, P\right) - \nabla\cdot\boldsymbol\tau_{ij}^R - W\avg{\partial_z\vect{u}\p}+\nu\avg{\partial_{zz}\vect{u}\p} - \avg{\partial_z\pars{w\p\vect{u}\p}}\right|=\nonumber\\
=\Bigg|\cancel{\pars{\frac{1}{2}+\frac{\epsilon}{\pi}}\sin(x)\cos(x)\pars{\cos^2(y)-3\sin^2(y)}}+\bcancel{3\nu\frac{\epsilon}{2\pi}\cos(x)\sin(y)} - \nonumber\\
- \pars{\frac{\epsilon}{2\pi}}^2\sin(x)\cos(x)\pars{\cos^2(y)-\sin^2(x)}-\bcancel{\nu\frac{\epsilon}{\pi}\cos(x)\sin(y)} - \nonumber \\
- \bracs{\cancel{\frac{1}{2}+\frac{\epsilon}{\pi}}-\frac{1}{4}\pars{\frac{\epsilon}{\pi}}^2}\sin(x)\cos(x)\pars{\cos^2(y)-3\sin^2(y)}+ \nonumber \\
+\frac{1}{2}\pars{\frac{\epsilon}{\pi}}^2\sin(x)\cos(x)\sin^2(y)-\bcancel{\nu\frac{\epsilon}{2\pi}\cos(x)\sin(y)}\Bigg| \nonumber=\\
=\Bigg|-\frac{1}{4}\pars{\frac{\epsilon}{\pi}}^2\sin(x)\cos(x)\pars{\cos^2(y)-\sin^2(x)}+\nonumber\\
+\frac{1}{4}\pars{\frac{\epsilon}{\pi}}^2\sin(x)\cos(x)\pars{\cos^2(y)-3\sin^2(y)}+\frac{1}{2}\pars{\frac{\epsilon}{\pi}}^2\sin(x)\cos(x)\sin^2(y)\Bigg| \nonumber= \\
=\Bigg|-\frac{1}{2}\pars{\frac{\epsilon}{\pi}}^2\sin(x)\cos(x)\sin^2(x)+\frac{1}{2}\pars{\frac{\epsilon}{\pi}}^2\sin(x)\cos(x)\sin^2(y)\Bigg|= 0.
\end{gather}

With this, we can conclude that the quadratic errors arising from the RHS of \eref{eq:balance_all} cancel out with each other.
Also, the linear errors arising on both LHS and RHS also cancel each other.

\subsection{Quadrature error with spanwise periodicity}
\label{sec:sans_quadrature_errors2}

The SANS derivation employed in \cref{chapter:sans} and \cref{chapter:zanspy} (\eref{eq:balance}) neglects errors arising from the quadratures of spanwise derivatives (second line of \eref{eq:sans_full}).
Because of this, \eref{eq:balance_all} is not balanced.
Here, we derive the error associated to this practice.
Inserting the previously derived terms into \eref{eq:balance} carries the following error
\begin{gather}
\mathcal{E} = \avg{\mathcal{S}\left(\vect{u}, p\right)} - \tilde{\mathcal{S}}\left(\vect{U}, P\right) - \nabla\cdot\boldsymbol\tau_{ij}^R=\nonumber\\
=\cancel{\pars{\frac{1}{2}+\frac{\epsilon}{\pi}}\sin(x)\cos(x)\pars{\cos^2(y)-3\sin^2(y)}}+3\nu\frac{\epsilon}{2\pi}\cos(x)\sin(y) - \nonumber\\
- \pars{\frac{\epsilon}{2\pi}}^2\sin(x)\cos(x)\pars{\cos^2(y)-\sin^2(x)}-{\nu\frac{\epsilon}{\pi}\cos(x)\sin(y)} - \nonumber \\
- \bracs{\cancel{\frac{1}{2}+\frac{\epsilon}{\pi}}-\frac{1}{4}\pars{\frac{\epsilon}{\pi}}^2}\sin(x)\cos(x)\pars{\cos^2(y)-3\sin^2(y)} \nonumber=\\
=\frac{\epsilon}{\pi}\nu\cos(x)\sin(y) - \frac{1}{4}\pars{\frac{\epsilon}{\pi}}^2\sin(x)\cos(x)\pars{\cos^2(y)-\sin^2(x)} + \nonumber \\
+\frac{1}{4}\pars{\frac{\epsilon}{\pi}}^2\sin(x)\cos(x)\pars{\cos^2(y)-3\sin^2(y)} \nonumber=\\
=\frac{\epsilon}{\pi}\nu\cos(x)\sin(y)-\frac{1}{2}\pars{\frac{\epsilon}{\pi}}^2\sin(x)\cos(x)\sin^2(y),
\end{gather}
which corresponds indeed to the derivation of \eref{eq:extra}
\begin{equation}
\mathcal{E}=|\avg{w}\avg{\partial_z\vect{u}\p}-\nu\avg{\partial_{zz}\vect{u}\p}|.
\end{equation}
It can be observed that, given $\epsilon$ is a small positive number, the viscous term has a larger contribution to the model error.

It is important to note that the midpoint and trapezoidal quadratures obtain a higher convergence rate when the integrand is periodic and smooth \citep{Weideman2002,Trefethen2014}, precisely from $\mathcal{O}(h^2)$ to $\mathcal{O}(e^{-1/h})$.
Applying the spanwise-periodicity constraint in our problem and considering incompressible flow (smooth functions), the quadrature error should converge faster than the spatial discretisation error of the governing equations $\pars{\mathcal{O}(h^2)}$.
% ----------------------------------------------------------------
\end{document}


\documentclass{article}
\usepackage[top=2cm,bottom=2cm,left=2cm,right=2cm]{geometry}
\usepackage{amsmath, amsthm}
\usepackage{tikz}
\usepackage{hyperref}

\usepackage{datetime}
\newdateformat{petsa}{\the\day\ \shortmonthname[\the\month] \the\year}

\theoremstyle{plain}
\newtheorem{definition}{Definition}[section]
\newtheorem{theorem}{Theorem}[section]
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{conjecture}[theorem]{Conjecture}

\theoremstyle{remark}
\newtheorem*{remark}{Remark}

\title{Description of \texttt{treedrawing}}
\author{poypoyan}
\date{\petsa\today}

\begin{document}

\maketitle

\begin{abstract}
This note contains an overview of my Github repo \texttt{treedrawing} \cite{treedrawing}, which is a naive algorithm that neatly draws rooted trees.
\end{abstract}

\section{Definitions}
We start by introducing some standard and non-standard terminologies:
\begin{definition}
A \textbf{rooted tree} is a triple $T=(V, E, r)$ where $(V, E)$ is a tree (a graph without cycles), and $r \in V$ is called the \textbf{root}.
\end{definition}
\begin{remark}
We adopt terminologies from computer science: we call $V$ the set of \textbf{nodes} (instead of vertices), and $E$ the set of \textbf{branches} (instead of edges).
\end{remark}
\begin{definition}
For nodes $x, y \in V$ connected by a branch, $x$ is the \textbf{parent} of $y$ and $y$ is a \textbf{child} of $x$ if $x$ has closer path to the root than $y$.
\end{definition}
\begin{definition}
A \textbf{leaf} is a node that does not have a child.
\end{definition}
\begin{definition}
A non-root node is \textbf{minor} iff it is the only child of its parent and it has exactly one child node. A node is \textbf{major} iff it is the root or it is not minor.
\end{definition}
\begin{definition}
The \textbf{sub-rooted tree} by a major node $x \in V$ is a rooted tree $T'=(V', E', x)$ consisting of $x$ itself (as root) and all of its the `descendant' nodes.
\end{definition}
\begin{remark}
(1) The sub-rooted tree by the root node is the whole rooted tree. (2) The sub-rooted tree by the leaf node is the trivial branch-less tree with itself as the only node.
\end{remark}
\begin{definition}\label{weight}
The \textbf{weight} of a major node is the number of major nodes of the sub-rooted tree by that node. The weight of a minor node is 0.
\end{definition}
\begin{remark}
The weight of every leaf node is 1.
\end{remark}

We show the converse of the last sentence in Definition \ref{weight}: all nodes of weight 0 are minor. This is equivalent to the statement that all major nodes have weights of at least 1. This is indeed the case because the major node itself is contained in the sub-rooted tree by itself. In conclusion, a node is minor iff its weight is 0.

To aid in understanding the concepts, an example rooted tree is provided in Figure \ref{rooted-tree}.

\begin{figure}[htbp]
	\centering
	\begin{tikzpicture}[nodes = {draw, circle}]
		\node [red] {8} [sibling distance = 1cm, level distance = 1cm]
		child {node {2}
			child {node {0}
				child {node {1}}
			}
		}
		child {node {4}
			child {node {3} [sibling distance = 2cm]
				child {node [xshift = 1cm] {1}}
				child {node {1}}
			}
		}
		child {node {1}};
	\end{tikzpicture}
	\caption{Example rooted tree with red-colored node as the root. The number in node indicates its weight.}
	\label{rooted-tree}
\end{figure}

\section{Algorithm Description}
The algorithm consists of three main functions that are executed in order: (1) \texttt{analyzeNode}, which records relevant information for each node recursively, (2) \texttt{setInitCoord}, which sets initial coordinates to each node recursively, and (3) \texttt{fixCoord}, which fixes coordinates of nodes so that there are no nodes with same location.

\subsection{Function \texttt{analyzeNode}}
The main input \footnote{Throughout this paper, we only mention the `important' inputs. Other inputs are instead mentioned in \texttt{example.py} comments.} of the algorithm is any data with a structure of rooted tree. Note that nodes in the input data must be \textit{labelled}. What the \texttt{analyzeNode} function mainly does is to perform tree traversal on the data and record the following information:

\begin{itemize}
	\item \texttt{connecNodes} is an array of arrays of natural numbers. An index \texttt{idx} in \texttt{connecNodes[idx]} corresponds to a node (with \texttt{idx = 0} being the root). Now \texttt{connecNodes[idx]} itself is the array of indices that correspond to the \textit{children} of the \texttt{idx} node. If the \texttt{idx} node is a leaf, then \texttt{connecNodes[idx]} is the empty array.
	\item \texttt{labelDict} is a function that aligns node \textit{labels} (strings) to indices for \texttt{connecNodes}, \texttt{weightNodes}, and \texttt{majorNodes}.
	\item \texttt{weightNodes} is an array of natural numbers. Like in \texttt{connecNodes}, an index \texttt{idx} in \texttt{weightNodes[idx]} corresponds to a node (with \texttt{idx = 0} being the root). Now \texttt{weightNodes[idx]} itself is the weight of the \texttt{idx} node.
	\item \texttt{majorNodes} is an array of objects. Like in \texttt{connecNodes}, an index \texttt{idx} in \texttt{majorNodes[idx]} corresponds to a node (with \texttt{idx = 0} being the root). Now for major nodes, \texttt{majorNodes[idx]} itself is the array of major nodes along the (unique) path from the root to the \texttt{idx} node (\texttt{idx} itself excluded). Since the root has no parent, \texttt{majorNodes[0]} is always the empty array. For minor nodes, \texttt{majorNodes[idx]} is the \textit{null} object.
\end{itemize}

\subsection{Function \texttt{setInitCoord}}
As the name says, this function sets initial coordinates for each node. These coordinates are stored in \texttt{coordNodes}. The root's coordinate is always set to $(0,0)$. The major nodes are always set to coordinates of upright square lattice (see Figures \ref{lattice} and \ref{setinitcoord}). We let \texttt{dist} the distance between consecutive horizontal/vertical points in the upright square lattice; it is hard-coded to $1.0$. Aside from the above outputs of \texttt{analyzeNode} except \texttt{labelDict}, the other relevant input is \texttt{outDir}, the direction of `growth' of rooted tree. For the following discussion, we set \texttt{outDir = `D'} (downward direction).

\begin{figure}[htbp]
	\centering
	\begin{tikzpicture}
		\foreach \i in {0,...,4}
		\foreach \j in {0,...,4}
		\node[circle, minimum width = 4pt, inner sep = 0pt, fill = red] at (\i * 0.75 cm, \j * 0.75 cm) {};
	\end{tikzpicture}
	\caption{Upright square lattice. The middlemost point would be the origin.}
	\label{lattice}
\end{figure}

For each major node $x$ with one child node, minor nodes are traversed until encountering another major node $y$. The coordinate for $y$ is set to one \texttt{dist} directly \textit{below} (since \texttt{outDir = `D'}) the coordinate of $x$. On the other hand, the coordinates of minor nodes between $x$ and $y$ are `squeezed' between the coordinates of $x$ and $y$ at equal distance to each other. Now $y$ either has no child or has multiple children. The former means unwinding the recursion. For the latter, $y$'s children are all one \texttt{dist} below $y$. Now the x-coordinate of the children depends on their weights: the child with highest weight is placed directly below $y$, then the child with next highest weight is placed one \texttt{dist} away from the closest child. The placement of children swings (e.g. center, left, right, left, $\ldots$) and goes \textit{away} from the child with highest weight. All of this is visualized in Figure \ref{setinitcoord}.

\begin{figure}[htbp]
	\centering
	\begin{tikzpicture}[nodes = {draw, circle, minimum width = 4pt, inner sep = 0pt}]
		\draw[-, thick = 2pt] (0 cm, 0 cm) -- (0 cm, -2 cm);
		\draw[-, thick = 2pt] (0 cm, -1 cm) -- (-1 cm, -2 cm);
		\draw[-, thick = 2pt] (0 cm, -1 cm) -- (1 cm, -2 cm);
		\draw[-, thick = 2pt] (0 cm, -1 cm) -- (-2 cm, -2 cm);
		\draw[-, thick = 2pt] (0 cm, -1 cm) -- (2 cm, -2 cm);
		\node[color = red, fill = red] at (0 cm, 0 cm) {};
		\node[fill = black] at (0 cm, -0.333 cm) {};
		\node[fill = black] at (0 cm, -0.667 cm) {};
		\node[color = red, fill = red] at (0 cm, -1 cm) {};
		\node[color = red, fill = red] at (0 cm, -2 cm) {};
		\node[color = red, fill = red] at (-1 cm, -2 cm) {};
		\node[color = red, fill = red] at (1 cm, -2 cm) {};
		\node[color = red, fill = red] at (-2 cm, -2 cm) {};
		\node[color = red, fill = red] at (2 cm, -2 cm) {};
		\node[draw = none] at (0.2 cm, 0.1 cm) {$x$};
		\node[draw = none] at (0.2 cm, -0.9 cm) {$y$};
		\node[draw = none] at (0 cm, -2.4 cm) {4};
		\node[draw = none] at (-1 cm, -2.4 cm) {3};
		\node[draw = none] at (1 cm, -2.4 cm) {2};
		\node[draw = none] at (-2 cm, -2.4 cm) {1};
		\node[draw = none] at (2 cm, -2.4 cm) {0};
	\end{tikzpicture}
	\caption{Initial coordinates; branches are drawn for clarity. Major nodes are red and minor nodes are black. Example weights of bottom nodes are also indicated.}
	\label{setinitcoord}
\end{figure}

The reasoning behind placement of children is aesthetics: the central path from root should have the most major nodes, and the sub-rooted trees that branch away from the central path should be minimal.

\subsection{Function \texttt{fixCoord}}
The problem with the previous function is that several major nodes will have the same coordinates. This is what \texttt{fixCoord} fixes. For each major node $x$, let $y$ be first index in \texttt{coordNodes} such that \texttt{coordNodes[x] = coordNodes[y]}. If $x \ne y$, then a duplicate coordinate is found.

Using \texttt{majorNodes}, the two major nodes that have the same parent and splits the path from root to $x$ and the path from root to $y$ are determined. The indices of that major node for $x$ and for $y$ are stored at \texttt{splitNodeA} and \texttt{splitNodeB} (let's say respectively). If for example \texttt{weight[splitNodeA] > weight[splitNodeB]}, then the whole sub-rooted tree by \texttt{splitNodeB} must move \textit{away} from the central path from root (and vice versa). This also means that the coordinate of $y$ must move. How many \texttt{dist} to move? The whole sub-rooted tree by \texttt{splitNodeB} must move away until the coordinate of $y$ is not equal to the coordinates of $x$ and all siblings of $x$.

After the movement, the next $x$ is checked. Looping through major nodes is continued until no duplicate coordinate is found, which means the function is done.

\section{Notes for Programmer}
The main output of the algorithm is the final coordinates of each node, \texttt{coordNodes}. On the other hand, the drawing of branches is actually not an output! This means that the drawing of branches must be done in a separate code/program, although I have the simple `straight line' branches (as seen in Figures \ref{setinitcoord} and \ref{interbranch}) in mind.

Since the coordinate of root node and \texttt{dist} are hard-coded to $(0,0)$ and $1.0$, respectively, one can `customize' the coordinates in the following manner: $$\mathbf{r} + s * \texttt{coordNodes[i]}$$
for all nodes $i$, where $\mathbf{r}$ is the actual root node coordinate and $s$ is the scalar.

Lastly, intersecting branches in a manner like in Figure \ref{interbranch} may occur.

\begin{figure}[htbp]
	\centering
	\begin{tikzpicture}[nodes = {draw, circle, minimum width = 4pt, inner sep = 0pt}]
		% sub-rooted tree 1
		\draw[-, thick = 2pt] (-2 cm, 0 cm) -- (-2 cm, -1 cm);
		\draw[-, thick = 2pt] (-2 cm, 0 cm) -- (-3 cm, -1 cm);
		\draw[-, thick = 2pt] (-2 cm, 0 cm) -- (-1 cm, -1 cm);
		\draw[-, thick = 2pt] (-2 cm, 0 cm) -- (3 cm, -1 cm);
		\node[color = red, fill = red] at (-2 cm, 0 cm) {};
		\node[color = red, fill = red] at (-2 cm, -1 cm) {};
		\node[color = red, fill = red] at (-3 cm, -1 cm) {};
		\node[color = red, fill = red] at (-1 cm, -1 cm) {};
		\node[color = red, fill = red] at (1 cm, -1 cm) {};
		% sub-rooted tree 2
		\draw[-, thick = 2pt] (1 cm, 0 cm) -- (0 cm, -1 cm);
		\draw[-, thick = 2pt] (1 cm, 0 cm) -- (1 cm, -1 cm);
		\draw[-, thick = 2pt] (1 cm, 0 cm) -- (2 cm, -1 cm);
		\node[color = red, fill = red] at (1 cm, 0 cm) {};
		\node[color = red, fill = red] at (0 cm, -1 cm) {};
		\node[color = red, fill = red] at (2 cm, -1 cm) {};
		\node[color = red, fill = red] at (3 cm, -1 cm) {};
	\end{tikzpicture}
	\caption{The right sub-rooted tree is sandwiched between two children of the left sub-rooted tree.}
	\label{interbranch}
\end{figure}

\bibliographystyle{plain}
\bibliography{treedrawing}
\end{document}


The global ledger state is stored locally by each node in a key value pair database. Any performant database such as RocksDb can be used to store the state data, however the data must be organised using a structure called a sparse Merkle tree. This structure allows the entire data state to be represented by a single hash, known as the root hash. Any changes to the data would result in a completely different root hash, therefore it can be used by nodes to verify that their dataset is the same as that of other nodes. Consequently, a node using an alternative storage organisation would be unable to interact with the network due to an inability to calculate the same root hash.\\

The sparse Merkle tree data structure allows for:

\begin{enumerate}
\item Persistent storage of the current and previous states of the ledger.
\item Efficient provision of proof that queried data exists, or importantly, does not exist in the ledger.\end{enumerate}

In addition to storing the global ledger state in a sparse Merkle tree, the smart contract KVM code and smart contract data are also stored using this data structure. This allows the root hash of both these data sets to be stored in the \textbf{storageRoot} and \textbf{codeHash} fields in the account state.  The use of the SMT for storing these data sets allows honest nodes to recognise and reject information that has been altered outside of the ledger cycle by malicious parties. \\

\subsubsection{Merkle Tree}

Each item of data being stored is represented as a leaf of the Merkle tree. The data in neighbouring leaves is hashed and concatenated and this value is used for the node one level above in the tree. Each node is stored in the database with its lookup key being the hash of its value. The nodes are themselves combined until a single hash has been obtained. This is the root hash and its value depends on every bit of data stored in the tree.\\

\begin{figure}[h]
  \centering
  \captionsetup{format=hang, font=footnotesize}
  \caption{A binary Merkle tree.}
\includegraphics[scale=0.8]{merkle-tree}
\end{figure}

A binary tree is one in which the nodes can have only two children. Note that not all Merkle trees are binary trees. For example the nodes in a Modified Merkle Patricia tree can have up to 16 children.\\

When a data entry changes, every node above it in the tree must be recalculated. The hash of the data held by a node is used as its key in the database, therefore every recalculated node will constitute a new entry in the underlying database, rather than an existing entry that needs to be updated. This means that previous states of the tree are not overwritten, and all old data can be accessed using previous root hashes.\\

\subsubsection{Merkle Proof}

A client with knowledge of the current root hash of the state tree does not need to retrieve the complete set of data to verify the inclusion of a specific entry. Knowledge of each sibling hash along the path down to the entry being authenticated is sufficient to reconstruct the root hash. A client who is able to reconstruct the root hash from this data can be confident that the entry is contained in the data set. For example, in the diagram above, the inclusion of entry $\beta$ can be verified with just $\beta$, $H(\kappa)$, and $H(\lambda)+H(\sigma)$. \\

Though it is simple to prove inclusion of an item using a Merkle tree, proving that a particular item is \emph{not} included can be harder. This is because the item could exist at any of the leaf nodes.\\

\subsubsection{Sparse Merkle Tree}

The Sparse Merkle tree is a binary tree. The position of an entry in the sparse merkle tree is determined by its hash. For the ledger state, we use the hash of the account address to determine the index of the leaf node to hold the account data. This account index is used to determine the path from the root hash to the data entry, telling us which branch to follow as we progress down the tree. The account index is not equivalent to a key in the underlying database.\\

In a sparse tree a default value is used for leaves that do not yet contain data.\\

\begin{figure}[h]
  \centering
  \captionsetup{format=hang, font=footnotesize}
 \caption{A sparse Merkle tree, containing a single entry. The default values are represented by $\phi$.}
\includegraphics[scale=0.8]{sparse-merkle-tree}
\end{figure}

Since a data entry in a sparse Merkle tree can only exist in a specific location, proofs of non-inclusion are easy to provide; the proof that a key is not included in the tree is simply a proof that the value for the key is default.\\

A naive implementation would require a tree with a leaf for each possible index. Using the hash of the address as the index gives $2^{256}$ possible indices, too many to generate a full tree. However, non default entries will be uncommon compared to default entries; most of the nodes will therefore have default values that can be determined based on the height of the node. These values do not need to be stored more than once. For a branch containing only one non default entry, the entry can be stored in topmost node for that branch, providing further storage savings.\\

\subsubsection{Choice of Sparse Merkle Tree over the Modified Merkle Patricia Tree}

In a Patricia tree, each node can contain up to 16 children. As the sparse Merkle tree is a binary tree, it is simpler both to implement and to traverse the path to a leaf.  \\

For the Patricia tree, the Merkle proof is composed of all the nodes in the path. Since each node has 16 children the amount of data is larger and not as straightforward to verify as in a binary tree. Using a sparse Merkle tree gives us Merkle proofs which are small and easy to use.\\


\section{Supplemental Basis Functions}
%\lstinputlisting[label=basis7s7p,caption=The 7s7p set of diffuse functions in the GAMESS format.,language={}]{parts/basis7s7p.txt}
%\lstinputlisting[label=basis7s7p,caption=The 3s1p set of diffuse functions in the GAMESS format.,language={}]{parts/basis3s1p.txt}
The exponents for the 7s7p and 3s1p diffuse Gaussian type orbitals are given below in the GAMESS format.
\subsection{7s7p}
\inputminted{md}{parts/basis7s7p.txt}
\subsection{3s1p}
\inputminted{md}{parts/basis3s1p.txt}
\subsection{3s1p3d}
\inputminted{md}{parts/basis3s1p3d.txt}
\newpage


\documentclass[12pt]{article}
\usepackage[margin=1.0in]{geometry}
\usepackage{graphicx}
\usepackage{ amssymb }
\usepackage{listings}
\usepackage[toc,page]{appendix}
\usepackage{wrapfig}
\usepackage{float}
\usepackage{indentfirst}
\usepackage{subcaption}
\usepackage{mwe}
\renewcommand{\thesubsection}{\thesection.\alph{subsection}}

\title{CTA200 2021 Assignment 2}
\author{Milica Ivetic \\ milica.ivetic@mail.utoronto.ca}
\date{}

\begin{document}

\maketitle

\section*{[1] Question 1}

For this question I wrote two functions, one for each of the approximation methods given in the question. I then plotted the error compared to the analytical derivative of $f(x) = sin(x)$ using the formula given in the question. The loglog plot for the two different approximation methods is given in Figure 1.

\begin{figure}[h]
\begin{center}
\includegraphics[scale=0.7]{Question1plot.pdf}
\caption{The loglog plot of error compared to the analytical derivative for both approximation methods. \label{fig1}}
\end{center}
\end{figure}

In the above plot we see that the first approximation method (blue line) has a changing slope, suggesting that the bigger the value for h gets, the less accurate the approximation is. For the second approximation method (orange line), the slope remains constant, suggesting that this method retains its accuracy for increasing h values. Also, for both methods, error increases as h increases.

\section*{[2] Question 2}

Three functions were needed for this question. The first function iterated the equation $z_{i + 1} = z_i^2 + c$ up to a certain arbitrary value. The chosen number of iterations was 30.

The second function plots an image in which the points in the complex plane that diverge are given a certain colour (blue) and bounded points are given another colour (orange), as seen in Figure 2a.

\begin{figure*}[h]
        \centering
        \begin{subfigure}[b]{0.49\textwidth}
            \centering
            \includegraphics[width=\textwidth]{divbound.pdf}
            \caption[]%
            {{\small Divergent and bounded points on the complex plane.}}
            \label{fig:mean and std of net14}
        \end{subfigure}
        \begin{subfigure}[b]{0.5\textwidth}
            \centering
            \includegraphics[width=\textwidth]{colourmap.pdf}
            \caption[]%
            {{\small Divergence of z on complex plane. The numbers on the colourbar represent the number of iterations.}}
        \end{subfigure}
        \caption[]%
        {\small Plots for Question 2.}
    \end{figure*}

The third function produces an image where the points are coloured by a colour scale that indicates the iteration number at which the given point diverged. As shown in Figure 2b, the smaller the number is on the colour bar, the faster it diverges. This is because the number represents the number of iterations before it diverges, hence why the outer parts of the plot are darker purple.

\section*{[3] Question 3}

For this question, the Scipy ODE integrator was used. Figure 3 shows the plots of solutions with varying $\gamma$ and $\beta$ values. Also, the initial conditions used were $I(0) = 1, S(0)=999, R(0) = 0$.

\begin{figure*}[h]
        \centering
        \begin{subfigure}[b]{0.49\textwidth}
            \centering
            \includegraphics[width=\textwidth]{gamma = 0.1 and beta = 1.pdf}
            \caption[]%
            {{\small Solutions for $\gamma = 0.1$ and $\beta = 1$.}}
            \label{fig:mean and std of net14}
        \end{subfigure}
        \begin{subfigure}[b]{0.5\textwidth}
            \centering
            \includegraphics[width=\textwidth]{gamma = 0.5 and beta = 0.5.pdf}
            \caption[]%
            {{\small Solutions for $\gamma = 0.5$ and $\beta = 0.5$.}}
        \end{subfigure}
        \begin{subfigure}[b]{0.5\textwidth}
            \centering
            \includegraphics[width=\textwidth]{gamma = 1 and beta = 0.5.pdf}
            \caption[]%
            {{\small Solutions for $\gamma = 1$ and $\beta = 0.5$.}}
        \end{subfigure}
        \caption[]%
        {\small Plots for Question 3.}
    \end{figure*}

\end{document}