\documentclass[main.tex]{subfiles}
\begin{document}

\subsection{The stress-energy tensor}

\marginpar{Wednesday\\ 2021-11-24}

A massive particle is characterized by a four-momentum \(pp^{\mu } = m u^{\mu }\), where \(u^{\mu } = \dv*{\xi^{\mu }}{\tau }\), and \(\xi^{\mu }\) are the flat spacetime coordinates in a LIF parametrizing the particle's trajectory.

This can be written as
%
\begin{align}
p^{\mu } = mc \dv{\xi^{0}}{\tau } \qty[1, \dv{\xi^{i}}{\xi^{0}}] = \qty[mc \gamma , m \gamma v^{i}]
\,.
\end{align}

Here, we are using \(\gamma = (1 - v^2 / c^2)^{-1/2}\), so that \(E = \gamma m c^2\).

\paragraph{The stress-energy tensor}

Suppose we have a collection of \(n\) particles, with their locations \(\xi^{i} _n (t)\).

We define the 00 component of the stress-energy tensor, or \emph{energy density}, as
%
\begin{align}
T^{00}  = \sum _{n} c p^{0}_n
\delta^3 (\vec{\xi} - \vec{\xi}_n (t))
\,,
\end{align}
%
the \emph{momentum density} \(T^{0i} / c\) through
%
\begin{align}
T^{0i} = \sum _{n } c p^{i}_n \delta^3 (\vec{\xi} - \vec{\xi}_n (t))
\,,
\end{align}
%
and the \emph{momentum flux} as
%
\begin{align}
T^{ij} = \sum _{n} p^{i}_n \dv{\xi^{j}}{\tau } \delta^3 (\vec{\xi } - \vec{\xi}_n (t))
\,.
\end{align}

In more generality, we can define
%
\begin{align}
T^{\alpha \beta } = \sum _{n} p^{\alpha }_{n} \dv{\xi^{\beta }_n}{\tau } \delta^3 (\vec{\xi} - \vec{\xi}_n(t))
\,.
\end{align}

Since
%
\begin{align}
p_n^\alpha = \frac{E_n}{c^2} \dv{\xi^{\alpha }_n}{t}
\,,
\end{align}
%
we can write this as
%
\begin{align}
T^{\alpha \beta } = c^2 \sum _{n} \frac{p_n^\alpha p_n^\beta }{E_n} \delta^3(\vec{\xi} - \vec{\xi}_n(t))
\,,
\end{align}
%
which tells us that this is a manifestly symmetric object.

In integral form, it reads
%
\begin{align}
T^{\alpha \beta } = c \sum _{n} \int p_n^\alpha \dv{\xi^{\beta }}{\tau } \delta^{4} (\xi^{\mu } - \xi^\mu _n (\tau )) \dd{\tau _n}
\,.
\end{align}

The 3D delta function is defined through
%
\begin{align}
\int \dd[3]{\xi } \rho (\vec{\xi }) \delta^3 (\vec{\xi} - \vec{\xi}_n) = \rho (\vec{\xi}_n)
\,,
\end{align}
%
which tells us that the dimensions of a \([\delta^3]\) are \([\text{length}^{-3}]\); therefore the dimensions of \(T^{00}\) are indeed those of an energy density.

In the non-relativistic limit, \(v^{i} \ll c\), the time component of each four-momentum is approximately \(p^{0}_n \approx m_n c\); so in this first approximation we find
%
\begin{align}
T^{00} \approx \sum _{n} m_n c^2 \delta^3(\vec{\xi} - \vec{\xi}_n (t)) = \underbrace{\sum _{n} m_n c^2 \delta^3(\vec{\xi} - \vec{\xi}_n (t))}_{\rho } c^2
\,.
\end{align}

The spatial components of the three-momentum, \(p^{i}\), have dimensions of \([\text{energy} / \text{velocity}]\).

Therefore, the components \([cT^{0i}] = [E / t S]\) have dimensions of the energy per unit time per unit area.
This is therefore referred to as \emph{energy flow} across a surface orthogonal to \(x^{i}\).

Similarly, the \(T^{ij}\) have the dimensions of the flux of the \(i\)-th component of the momentum flowing across the unit surface orthogonal to axis \(\xi^{j}\).

One can show that this object does indeed transform like a tensor, and its most general expression, in a non-flat frame \(x^{\mu }\) reads
%
\begin{align}
T^{\alpha \beta } = c \sum _{n} \int \frac{ \dd{\tau _n}}{\sqrt{-g}} p^{\alpha }_n \dv{x^{\beta }_n}{\tau _n} \delta^{4} (x^{\mu } x^{\mu }_n (\tau ))
\,.
\end{align}

In the flat case, \(\sqrt{-g} = 1\).
The invariant volume element is written as \(\sqrt{-g} \dd[4]{x}\).

The \(\delta \)-function has its own transformation rule:
%
\begin{align}
\frac{ \delta^{4} (x^{\mu } - x^{\mu }_n)}{\sqrt{-g}} =
\frac{ \delta^{4} (x^{\mu \prime} - x^{\mu \prime}_n)}{\sqrt{-g'}}
\,.
\end{align}

If we have a scalar field with a Lagrangian \(\mathscr{L} = - \partial^{\mu } \phi \partial_{\mu } \phi / 2 + V(\phi )\), we can associate a stress-energy tensor to it by
%
\begin{align}
T_{\mu \nu } = \partial_{\mu } \phi \partial_{\nu } \phi
- g_{\mu \nu } \mathscr{L}
\,.
\end{align}

If we have a perfect fluid with energy density \(\epsilon \) and pressure \(P\), it will have a diagonal stress-energy tensor in its own rest frame:
%
\begin{align}
T_{\mu \nu } = (\epsilon + P) u_\mu u_\nu + P g_{\mu \nu }
\,.
\end{align}

For the electromagnetic field we get
%
\begin{align}
T_{\mu \nu } = C \qty(g^{\alpha \beta } F_{\alpha (\mu } F_{\nu ) \beta } - \frac{1}{2} F_{\alpha \beta } F^{\alpha \beta } g_{\mu \nu })
\,,
\end{align}
%
where the electromagnetic tensor is written in terms of the four-potential \(A^{\mu }\):
%
\begin{align}
F_{\alpha \beta } = 2 \nabla_{[\alpha } A_{\beta ]}
\,.
\end{align}

We now seek a conservation law of some sort.
Moving back to flat spacetime, we can compute its three-divergence:
%
\begin{align}
\pdv{T^{\alpha i}}{\xi^{i}} &=
\sum _{n} p^{\alpha }_{n} (t) \dv{\xi^{i}_n(t)}{t} \pdv{}{\xi^{i}} \delta^3 (\vec{\xi} - \vec{\xi}_n(t))  \\
&= - \sum _{n} p^{\alpha }_{n} (t) \dv{\xi^{i}_n(t)}{t} \pdv{}{\xi_n^{i}} \delta^3 (\vec{\xi} - \vec{\xi}_n(t))   \\
&= - \sum _{n} p^{\alpha }_{n} (t) \dv{}{t} \delta^3 (\vec{\xi} - \vec{\xi}_n(t))
\,.
\end{align}

On the other hand, by taking the derivative of the zeroth component we find
%
\begin{align}
\pdv{T^{\alpha 0}}{\xi^{0}}
&= \sum _{n} \qty[
    \dv{p^{\alpha }_n}{t} (t) \delta^3(\vec{\xi} - \vec{\xi}_n(t))
    + p^{\alpha }_n (t) \dv{}{t} \delta^3(\vec{\xi} - \vec{\xi}_n(t))
]
\,,
\end{align}
%
but the derivative of the four-momentum can be referred back to the quadri-force, which vanishes for an isolated system:
%
\begin{align}
\dv{p^{\alpha }_n}{t } = \dv{p^{\alpha }_n}{\tau } \dv{\tau }{t} = F^{\alpha }_n \dv{\tau }{t}
\,,
\end{align}
%
therefore we get
%
\begin{align}
\pdv{T^{\alpha i}}{\xi^{i}} = - \pdv{T^{\alpha 0}}{\xi^{0}}
\,,
\end{align}
%
which means that \(T^{\alpha \mu }{}_{, \mu } = 0\): in flat spacetime, the divergence of the stress-energy tensor vanishes.

This is associated with a conservation law: for example, we can integrate \(0 = T^{0 \mu }{}_{, \mu }\) on a volume at fixed time (a \(\xi^{0} = \text{const}\) hypersurface),
%
\begin{align}
\pdv{}{\xi^{0}} \int_V T^{00} \dd[3]{x} = - \int_V \pdv{T^{0i}}{\xi^{i}} \dd[3]{x} = - \int _{\partial V} T^{0i} n_i \dd{S}
\,.
\end{align}

If the system is isolated, the term on the boundary \(\partial V\) vanishes, which means that the total energy \(E = \int T^{00} \dd[3]{x}\) is conserved.
Similarly, we can find conservation laws for \(P^{i} = \int T^{0i} \dd[3]{x}\).

We have so far only found this result for flat spacetime, or in a LIF, but we can now introduce the \textbf{principle of general covariance}:

\begin{quotation}
    ``A physical law is true if:
    \begin{enumerate}
        \item with no gravity, it reduces to the laws of special relativity;
        \item it is generally covariant, meaning that it can be expressed in a tensorial form.''
    \end{enumerate}
\end{quotation}

What we can then do is generalize \(T^{\alpha \beta }{}_{, \beta } = 0\) to \(T^{\alpha \beta }{}_{; \beta } = 0\).

Be careful about this: it is \emph{not a conservation law}!
If one does all the calculations, it comes out to
%
\begin{align}
\pdv{}{x^{\mu }} \qty(\sqrt{-g} T^{\mu \nu }) = - \sqrt{-g} \Gamma^{\nu }_{\lambda \mu } T^{\lambda \mu }
\,.
\end{align}

The reasons this is not a conservation law are the gravitational field and gravitational waves.

If we include the Landau-Lifschitz pseudo-tensor \(\tau_{\mu \nu }\), then \emph{that} is conserved.

\subsection{The Einstein Equations}

Newtonian gravity is described by Poisson's equation: \(\nabla^2 \phi = 4 \pi G \rho \).
We want to write an equation which reproduces this in the non-relativistic limit.

Let us try to take some of these limits: the weak, and stationary field limits.
The low-velocity limit means that \(\dv*{x^{i}}{\tau } \ll c\), which also means that
%
\begin{align} \label{eq:low-velocity-limit}
\dv{x^{i}}{\tau } \ll c \dv{t}{\tau }
\,.
\end{align}

Further, we take the \emph{weak field limit}: we assume that \(g_{\mu \nu } = \eta_{\mu \nu } + h_{\mu \nu }\), where the components of the perturbation \(h_{\mu \nu }\) all have magnitudes much smaller than 1.
We will then work to first order in this as well.

We can do this in the geodesic equation
%
\begin{align}
\dv[2]{x^{\mu }}{\tau } + \Gamma^{\mu }_{\alpha \beta } \dv{x^{\alpha }}{\tau } \dv{x^{\beta }}{\tau } = 0
\,.
\end{align}

Since the largest component of the four-velocity is the zeroth one \eqref{eq:low-velocity-limit} the most relevant contribution will be given by the \(\Gamma^{\mu }_{00} u^{0} u^{0}\) term.

Those Christoffel symbols read
%
\begin{align}
\Gamma^{\mu }_{00} = \frac{1}{2} \eta^{\mu \nu } \qty(2 h_{\nu 0, 0} - h_{00, \nu })
\,.
\end{align}
%
By the stationary approximation, the time derivatives vanish; this yields \(\Gamma^{\mu }_{00} = - h_{00}{}^{, \mu }\).

The geodesic equation is therefore
%
\begin{align}
\dv[2]{x^{\mu }}{\tau } = \frac{1}{2} h_{00}{}^{, \mu } \qty(\dv{x^{0}}{\tau })^2
\,,
\end{align}
%
but the \(\mu = 0\) equation just reads \(\dv*[2]{x^{0}}{\tau } = 0\), so we can write \(x^{0} = ct = \tau \) with an affine transformation.

The spatial equations then read:
%
\begin{align}
\frac{1}{c^2} \dv[2]{x^{i}}{t} = \frac{1}{2} \partial^{i} h_{00}
\,.
\end{align}

This looks very similar to Newton's law
%
\begin{align}
\dv[2]{x^{i}}{t} = - \partial^{i} \phi
\,,
\end{align}
%
so long as we identify \(\phi = - c^2 h_{00} /2\), or \(h_{00} = -2 \phi / c^2\) (up to an additive constant).

In the Newtonian case, the field reads \(\phi = - GM / r\): this yields
%
\begin{align}
g_{00} = \eta_{00} + h_{00} = - 1 + \frac{2GM}{c^2 r}
\,,
\end{align}
%
again up to an additive constant, which we can however neglect if we ask that the field vanishes at infinity.

We might be therefore tempted to write something like
%
\begin{align} \label{eq:poisson-like-equation}
\nabla^2 g_{00} = \frac{8 \pi G}{c^{4}} T_{00}
\,,
\end{align}
%
however this is not covariant (not even under Lorentz transformations).

The way to make this covariant is to write
%
\begin{align}
G_{\mu \nu } = \frac{8 \pi G}{c^{4}} T_{\mu \nu }
\,,
\end{align}
%
where the Einstein tensor \(G_{\mu \nu }\) is made up of differential operators acting on the metric.

The guidelines to arrive at this equation are:
\begin{enumerate}
    \item the expression must be tensorial;
    \item the dimensions of \(G_{\mu \nu }\) must be those of inverse length, so we can only have terms like \(\partial g \partial g\), or \(\partial \partial g\), unless we want to include a new characteristic length scale --- therefore, we want to write \(G_{\mu \nu }\) in terms of the Riemann tensor;
    \item \(G_{\mu \nu }\) must be symmetric;
    \item \(G_{\mu \nu }\) must satisfy \(G^{\mu \nu }{}_{; \nu } = 0\), since the stress-energy tensor does;
    \item the expression reduces to equation \eqref{eq:poisson-like-equation}.
\end{enumerate}

Only two quantities can be constructed in this way from the Riemann tensor: the Ricci tensor \(R_{\mu \nu } = R^{\alpha }_{\mu \alpha \nu }\) and the term \(R g_{\mu \nu }\), where \(R = g^{\mu \nu } R_{\mu \nu }\).

With this, we can say that \(G_{\mu \nu } = c_1 R_{\mu \nu } + c_2 g_{\mu \nu } R\).

\end{document}


\section{Systems of linear equations}


%  -----------------------------------------------------------------------------
%  Author         : Bimalka Piyaruwan Thalagala
%  GitHub         : https://github.com/bimalka98
%  Date Created   : 11/8/2021
%  Last Modified  :
%  -----------------------------------------------------------------------------

\documentclass[a4paper,11pt]{article}%,twocolumn
\input{settings/packages}
\input{settings/page}
\input{settings/macros}
\usepackage[ framed, numbered]{matlab-prettifier}%framed,%
\usepackage{listings}
\usepackage{pythonhighlight}
\usepackage{pdfpages}

\begin{document}

\input{content/title_page}

\pagebreak
%%-----------------------------------------------------------------------
\includepdf[pages=-, width=\textwidth]{code/answers.pdf}

\section*{Question 4}
\subsection*{(a) The sequence of signals (waveforms) corresponding to the binary sequence}

Please note that when plotting the below figure following values were assumed for the parameters.\\

\begin{tabular}{l l}
	Bit interval ($T_b$) & 1 $s$ \\
	Symbol interval  (T) &$2 \times T_b = $  2 $s$
\end{tabular}

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.35]{figures/fig4a}
\end{figure}

\lstinputlisting[basicstyle = \mlttfamily\scriptsize , style = Matlab-editor]{code/codea.m}

\pagebreak
\subsection*{(b) The Quaternary MSK phase trajectory}
Please note that when plotting the below figure following values were assumed for the parameters.\\

\begin{tabular}{l l}
	Bit interval ($T_b$) & 1 $s$ \\
	Symbol interval  (T) &$2 \times T_b = $  2 $s$
\end{tabular}

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.35]{figures/fig4b}
\end{figure}

\lstinputlisting[basicstyle = \mlttfamily\scriptsize , style = Matlab-editor]{code/codeb.m}

\pagebreak
\subsection*{(c) The Quaternary MSK waveform corresponding to the binary sequence}
Please note that when plotting the below figure following values were assumed for the parameters.\\

\begin{tabular}{l l}
	Bit interval ($T_b$) & 1 $s$ \\
	Symbol interval  (T) &$2 \times T_b = $  2 $s$
\end{tabular}

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.35]{figures/fig4c}
\end{figure}

\lstinputlisting[basicstyle = \mlttfamily\scriptsize , style = Matlab-editor]{code/codec.m}
%---------------------------------------------------------------------------
\end{document}


\documentclass[t]{beamer}
\usetheme{Copenhagen}
\setbeamertemplate{headline}{} % remove toc from headers
\beamertemplatenavigationsymbolsempty

\usepackage{amsmath, tikz, pgfplots, tcolorbox, xcolor, array}
\pgfplotsset{compat = 1.16}

\title{Operations with Functions}
\author{}
\date{}

\AtBeginSection[]
{
  \begin{frame}
    \frametitle{Objectives}
    \tableofcontents[currentsection]
  \end{frame}
}

\begin{document}

\begin{frame}
\maketitle
\end{frame}

\section{Write the sum, difference, product, and quotient of two functions}

\begin{frame}{Sum, Difference, Product, and Quotient of Functions}
We can add, subtract, multiply, and divide functions just like we can with real numbers.	\newline\\
\begin{center}
\setlength{\extrarowheight}{10pt}
\begin{tabular}{c|c}
 \textbf{Sum}    &  $(f+g)(x) = f(x) + g(x)$    \\[6pt]
 \hline
 \textbf{Difference}   &    $(f-g)(x)=f(x)-g(x)$    \\[6pt]
 \hline
 \textbf{Product}   &   $(fg)(x)=f(x) \cdot g(x)$   \\[6pt]
 \hline
 \textbf{Quotient}  &   $\left(\dfrac{f}{g}\right)\left(x\right) = \dfrac{f(x)}{g(x)}$, \quad $g(x) \neq 0$   \\
\end{tabular}
\end{center}
\end{frame}

\begin{frame}{Sum, Difference, Product, and Quotient of Functions}
So if $f(x) = x+2$ and $g(x) = x^2 - 4$, then
\begin{align*}
    \onslide<2->{(f+g)(x) &= {\color{red}f(x)} + {\color{blue}g(x)} }\\[8pt]
    \onslide<3->{&=  {\color{red}x+2} + {\color{blue}x^2 - 4}  } \\[8pt]
    \onslide<4->{&=x^2 +x - 2}
\end{align*}
\end{frame}

\begin{frame}{Example 1}
Find each of the following if $f(x) = x^2 - 3$ and $g(x) = 4x+5$ \newline\\
(a) \quad $(f + g)(x)$
\begin{align*}
\onslide<2->{(f+g)(x) &=  f(x) + g(x)} \\[10pt]
\onslide<3->{&= x^2-3 + 4x + 5} \\[10pt]
\onslide<4->{&= x^2 + 4x + 2}
\end{align*}
\end{frame}

\begin{frame}{Example 1 \quad $f(x) = x^2-3$ \quad $g(x) = 4x+5$}
(b) \quad $(f - g)(x)$
\begin{align*}
\onslide<2->{(f-g)(x) &=  f(x) - g(x)} \\[10pt]
\onslide<3->{&= x^2-3 - (4x + 5)} \\[10pt]
\onslide<4->{&= x^2-3-4x-5} \\[10pt]
\onslide<5->{&= x^2-4x-8}
\end{align*}
\end{frame}

\begin{frame}{Example 1 \quad $f(x) = x^2-3$ \quad $g(x) = 4x+5$}
(c) \quad $(g - f)(x)$
\begin{align*}
\onslide<2->{(g-f)(x) &=  g(x) - f(x)} \\[10pt]
\onslide<3->{&= 4x+5 - (x^2-3)} \\[10pt]
\onslide<4->{&= 4x+5-x^2+3} \\[10pt]
\onslide<5->{&= -x^2+4x+8}
\end{align*}
\end{frame}

\begin{frame}{Example 1 \quad $f(x) = x^2-3$ \quad $g(x) = 4x+5$}
(d) \quad $(fg)(x)$
\begin{align*}
\onslide<2->{(fg)(x) &=  f(x) \cdot g(x)} \\[10pt]
\onslide<3->{&= (x^2-3)(4x+5)}
\end{align*}
\begin{center}
\onslide<4->{
\begin{tikzpicture}
\draw[black] (0,0) grid (2,2);
\node at (0,1.5) [left] {$x^2$};
\node at (0,0.5) [left] {$-3$};
\node at (0.5,2) [above] {$4x$};
\node at (1.5,2) [above] {$5$};
\onslide<5->{\node at (0.5,1.5) {$4x^3$};}
\onslide<6->{\node at (1.5,1.5) {$5x^2$};}
\onslide<7->{\node at (0.5,0.5) {$-12x$};}
\onslide<8->{\node at (1.5,0.5) {$-15$};}
\end{tikzpicture}
}
\onslide<9->{\[4x^3+5x^2-12x-15\]}
\end{center}
\end{frame}

\begin{frame}{Example 1 \quad $f(x) = x^2-3$ \quad $g(x) = 4x+5$}
(e) \quad $\left(\dfrac{f}{g}\right)(x)$
\begin{align*}
\onslide<2->{\left(\frac{f}{g}\right)(x) &=  \frac{f(x)}{g(x)}} \\[10pt]
\onslide<3->{&= \frac{x^2-3}{4x+5}}
\end{align*}
\end{frame}

\begin{frame}{Example 1 \quad $f(x) = x^2-3$ \quad $g(x) = 4x+5$}
(f) \quad $\left(\dfrac{g}{f}\right)(x)$
\begin{align*}
\onslide<2->{\left(\frac{g}{f}\right)(x) &=  \frac{g(x)}{f(x)}} \\[10pt]
\onslide<3->{&= \frac{4x+5}{x^2-3}}
\end{align*}
\end{frame}

\section{Evaluate the sum, difference, product, and quotient of two functions at a value}

\begin{frame}{Methods}
If $f(x) = x+2$ and $g(x) = x^2-4$, then
\begin{align*}
\onslide<2->{(f+g)(3) &= f(3) + g(3)} \\[6pt]
\onslide<3->{&= (3+2) + (3^2-4)} \\[6pt]
\onslide<4->{&= 10}
\end{align*}
\end{frame}

\begin{frame}{Alternate Method}
If $f(x) = x+2$ and $g(x) = x^2-4$, then
\begin{align*}
\onslide<2->{(f+g)(x) &= f(x) + g(x)} \\[6pt]
\onslide<3->{&= x+2 + x^2-4} \\[6pt]
\onslide<4->{&= x^2 + x - 2} \\[6pt]
\onslide<5->{(f+g)(3) &= 3^2 + 3 - 2} \\[6pt]
\onslide<6->{&= 10}
\end{align*}
\end{frame}

\begin{frame}{Example 2}
Evaluate each of the following if $f(x) = x^2 - 3$ and $g(x) = 4x + 5$	\newline\\
(a) \quad $(f + g)(3)$
\begin{align*}
\onslide<2->{(f+g)(3) &= f(3) + g(3)} \\[6pt]
\onslide<3->{&= 6 + 17} \\[6pt]
\onslide<4->{&= 23}
\end{align*}
\end{frame}

\begin{frame}{Example 2 \quad $f(x) = x^2 - 3$ and $g(x) = 4x + 5$}
(b) \quad $(f - g)(0)$
\begin{align*}
\onslide<2->{(f-g)(0) &= f(0) - g(0)} \\[6pt]
\onslide<3->{&= -3 - 5} \\[6pt]
\onslide<4->{&= -8}
\end{align*}
\end{frame}

\begin{frame}{Example 2 \quad $f(x) = x^2 - 3$ and $g(x) = 4x + 5$}
(c) \quad $(fg)(2)$
\begin{align*}
\onslide<2->{(fg)(2) &= f(2) \cdot g(2)} \\[6pt]
\onslide<3->{&= 1(13)} \\[6pt]
\onslide<4->{&= 13}
\end{align*}
\end{frame}

\begin{frame}{Example 2 \quad $f(x) = x^2 - 3$ and $g(x) = 4x + 5$}
(d) \quad $(gg)(1)$
\begin{align*}
\onslide<2->{(gg)(1) &= g(1) \cdot g(1)} \\[6pt]
\onslide<3->{&= 9(9)} \\[6pt]
\onslide<4->{&= 81}
\end{align*}
\end{frame}

\begin{frame}{Example 2 \quad $f(x) = x^2 - 3$ and $g(x) = 4x + 5$}
(e) \quad $\left(\dfrac{f}{g}\right)(1)$
\begin{align*}
\onslide<2->{\left(\frac{f}{g}\right)(1) &= \frac{f(1)}{g(1)}} \\[10pt]
\onslide<3->{&= \frac{-2}{9}}
\end{align*}
\end{frame}

\begin{frame}{Example 2 \quad $f(x) = x^2 - 3$ and $g(x) = 4x + 5$}
(f) \quad $\left(\dfrac{g}{f}\right)(8)$
\begin{align*}
\onslide<2->{\left(\frac{g}{f}\right)(8) &= \frac{g(8)}{f(8)}} \\[10pt]
\onslide<3->{&= \frac{37}{61}}
\end{align*}
\end{frame}

\end{document}


    \filetitle{!switch...!case...!otherwise...!end}{Switch among several cases based on expression}{modellang/switch}

	\paragraph{Syntax with an otherwise
clause}\label{syntax-with-an-otherwise-clause}

\begin{verbatim}
!switch Expr
    !case Balue1
        Block1
    !case Balue2
        Block2
    ...
    !otherwise
        OtherwiseBlock
!end
\end{verbatim}

\paragraph{Syntax without an otherwise
clause}\label{syntax-without-an-otherwise-clause}

\begin{verbatim}
!switch Expr
    !case Value1
        Block1
    !case Value2
        Block2
    ...
!end
\end{verbatim}

\paragraph{Description}\label{description}

The \texttt{!switch...!case...!otherwise...!end} command works the same
way as its counterpart in the Matlab programming language.

Use the \texttt{!switch...!case...!end} command to create a larger
number of branches of the model code. Which block of code is actually
read in and which blocks are discarded depends on which value in the
\texttt{!case} clauses matches the value of the \texttt{!switch}
expression. This works exactly as the \texttt{switch...case...end}
command in Matlab. The expression after the \texttt{!switch} part of the
command must must be a valid Matlab expression, and can refer to the
model parameters, or to other fields included in the parameter database
passed in when you run the \href{model/model}{\texttt{model}} function;
see \href{model/model}{the option \texttt{'assign='}}.

If the expression fails to be matched by any value in the \texttt{!case}
clauses, the branch in the \texttt{!otherwise} clause is used. If it is
a \texttt{!switch} command without the \texttt{!otherwise} clause, the
whole command is discarded. The Matlab function \texttt{isequal} is used
to match the \texttt{!switch} expression with the \texttt{!case} values.

\paragraph{Example}\label{example}

\begin{verbatim}
!switch policy_regime

    !case 'IT'
        r = rho*r{-1} + (1-rho)*kappa*pie{4} + epsilon;

    !case 'Managed_exchange_rate'
        s = s{-1} + epsilon;

    !case 'Constant_money_growth'
        m-m{-1} = m{-1}-m{-2} + epsilon;

!end
\end{verbatim}

When reading the model file in, create a parameter database, include at
least a field named \texttt{policy\_regime} in it, and use the option
\texttt{'assign='} to pass the database in. Note that you do not need to
declare \texttt{policy\_regime} as a parameter in the model file.

\begin{verbatim}
P = struct();
P.policy_regime = 'Managed_exchange_rate';
...
m = model('my.model','assign',P);
\end{verbatim}

In this case, the managed exchange rate policy rule,
\texttt{s = s\{-1\} + epsilon;} is read in and the rest of the
\texttt{!switch} command is discarded. To use another branch of the
\texttt{!switch} command you need to re-read the model file again with a
different value assigned to the \texttt{policy\_regime} field of the
input database.


\documentclass{article}
\usepackage[margin = .7in]{geometry}
\usepackage[dvipdfmx]{graphicx}
\usepackage{listings}
\usepackage{amsmath}
\usepackage{bm}
\lstset{%
  language={python},
  basicstyle={\small},%
  identifierstyle={\small},%
  commentstyle={\small\itshape},%
  keywordstyle={\small\bfseries},%
  ndkeywordstyle={\small},%
  stringstyle={\small\ttfamily},
  frame={tb},
  breaklines=true,
  columns=[l]{fullflexible},%
  numbers=left,%
  xrightmargin=0zw,%
  xleftmargin=3zw,%
  numberstyle={\scriptsize},%
  stepnumber=1,
  numbersep=1zw,%
  lineskip=-0.5ex%
}

\begin{document}
\title{STAT6011/7611/6111/3317 \\
COMPUTATIONAL STATISTICS (2016 Fall) \\
Assignment 3}
\author{Kei Ikegami (u3535947)}
\maketitle

\section{}
	\subsection{}
		\begin{align*}
			p({\bf y} | \sigma^2) &= \idotsint \Pi_{i = 1}^{n} \frac{1}{\sqrt{2\pi}} \exp\left( -\frac{(y_i - \theta_i)^2}{2}\right)\frac{1}{\sqrt{2\pi}\sigma} \exp\left( -\frac{\theta_i^2}{2\sigma^2}\right)\mathrm{d}\theta_1 \dots \mathrm{d}\theta_n \\
			&= \idotsint \Pi_{i = 1}^{n} \frac{1}{\sqrt{2\pi}\sqrt{\sigma^2 + 1}} \exp\left( -\frac{y_i}{2} + \frac{\sigma^2 y_i^2}{2(\sigma^2 + 1)}\right) \frac{\sqrt{\sigma^2 + 1}}{\sqrt{2\pi}\sigma} \exp\left( \frac{\sigma^2 + 1}{\sigma^2} \left(\theta_i -\frac{\sigma^2}{\sigma^2 + 1}y_i \right)\right)\mathrm{d}\theta_1 \dots \mathrm{d}\theta_n \\
			&= \Pi_{i = 1}^{n} \frac{1}{\sqrt{2\pi}\sqrt{\sigma^2 + 1}} \exp\left( -\frac{y_i}{2} + \frac{\sigma^2 y_i^2}{2(\sigma^2 + 1)}\right) \\
			&= \left(\frac{1}{\sqrt{2\pi}\sqrt{\sigma^2 + 1}}\right)^{n} \exp \left( -\frac{\sum_{i=1}^{n} y_i}{2} + \frac{\sigma^2 \sum_{i=1}^{n} y_i^2}{2(\sigma^2 + 1)}\right)
		\end{align*}
	\subsection{}
		\begin{align*}
			p(\sigma^2 | {\bf y}) \propto \left(\frac{1}{\sqrt{2\pi}\sqrt{\sigma^2 + 1}}\right)^{n} \exp \left( -\frac{\sum_{i=1}^{n} y_i}{2} + \frac{\sigma^2 \sum_{i=1}^{n} y_i^2}{2(\sigma^2 + 1)}\right) \frac{1}{\sigma^2}
		\end{align*}
		\lstinputlisting[caption=1-b]{1-b.py}
		\begin{center}
		\includegraphics[width = 12cm]{1-b.png}
		\end{center}
	\subsection{}
	Let $\Theta = (\theta_1, \theta_2, \dots , \theta_n)$, and let $\Theta_{-j} = (\theta_1, \dots, \theta_{j-1}, \theta_{j+1}, \dots, \theta_n)$.
		\begin{align*}
			p(\sigma^2 | {\bf y}, \Theta) &\propto \frac{1}{\sigma^2(\sqrt{2\pi \sigma^2})^n} \exp\left( -\frac{\sum_i^n \theta_i^2}{2\sigma^2}\right) \\
			p(\theta_j | {\bf y}, \Theta_{-j}, \sigma^2) &\propto N \left( \frac{\sigma^2 y_j}{\sigma^2 + 1}, \frac{\sigma^2}{\sigma^2 + 1}\right) \forall j \in \left\{1, 2, 3, \dots, n \right\} \\
		\end{align*}
		\lstinputlisting[caption=1-c]{1-c.py}
		Trace plot. $\sigma^2$ and $\theta_1$.
		\par
		\includegraphics[width = 9cm]{1-c-sigma.png}
		\includegraphics[width = 9cm]{1-c-theta.png}

\section{}
	\subsection{}
		\begin{align*}
		p({\bf y} | \sigma^2) &= \idotsint \Pi_{i = 1}^{n} \left(\Pi_{j=1}^{J} \frac{1}{\sqrt{2\pi}} \exp\left( -\frac{(y_{ij} - \theta_i)^2}{2}\right) \right)\frac{1}{\sqrt{2\pi}\sigma} \exp\left( -\frac{\theta_i^2}{2\sigma^2}\right)\mathrm{d}\theta_1 \dots \mathrm{d}\theta_n \\
		&= \idotsint \Pi_{i = 1}^{n} \exp\left( -\frac{\sum_{j=1}^{J} y_{ij}^2}{2} + \frac{\sigma^2 (\sum_{j=1}^{J} y_{ij})^2}{2(\sigma^2 + 1)} \right) \frac{1}{\sqrt{2\pi}^J} \frac{1}{\sqrt{J\sigma^2 + 1}} \\ &\frac{\sqrt{J\sigma^2 + 1}}{\sqrt{2\pi}\sigma} \exp\left(-\frac{J\sigma^2 + 1}{2\sigma^2} \left( \theta_i - \frac{\sigma^2}{J\sigma^2 + 1} \sum_{j=1}^{J} y_{ij} \right)^2\right)\mathrm{d}\theta_1 \dots \mathrm{d}\theta_n \\
		&= \Pi_{i=1}^{n} \frac{1}{\sqrt{2\pi}^J} \frac{1}{\sqrt{J\sigma^2 + 1}} \exp\left( -\frac{\sum_{j=1}^{J} y_{ij}^2}{2} + \frac{\sigma^2 (\sum_{j=1}^{J} y_{ij})^2}{2(\sigma^2 + 1)} \right)\\
		&= \frac{1}{\sqrt{2\pi}^{nJ}} \frac{1}{\sqrt{J\sigma^2 + 1}^{n}} \exp\left( -\frac{\sum_{i=1}^{n} \sum_{j=1}^{J} y_{ij}^2}{2} + \frac{\sigma^2 \sum_{i=1}^{n} \left( \sum_{j=1}^{J} y_{ij}\right)^2}{2(J\sigma^2+ 1)}\right)\\
		\end{align*}
	\subsection{}
		We prove the marginal likelihood is bounded away from 0 around the neighborhood of $\sigma^2 = 0$. In such a case, the usual prior, like Jaffrey's prior, which has infinite mass on the neighborhood, leads to an improper posterior distribution, because the multiplication of prior and the likelihood results in infinite among the neighbor hood.
		\par
		First, we calculate the first derivative of the marginal likelihood function with respect to $\sigma^2$, and will show that in the support of $\sigma^2$ the function is monotone decreasing function when $n \neq 1$ and $J \neq 1$. Let L be the part of marginal likelihood function concerning with $\sigma^2$.
		\begin{align}
			L(\sigma^2) &= (J\sigma^2 + 1)^{-\frac{n}{2}} \exp \left( \frac{\sigma^2}{J\sigma^2 + 1}\right) \nonumber \\
			\frac{\partial L}{\partial \sigma^2} &= \exp \left( \frac{\sigma^2}{J\sigma^2 + 1} \right) (J \sigma^2 + 1)^{-\frac{n}{2}-2}\left( 1 - \frac{nJ^2}{2}\sigma^2 - \frac{nJ}{2}\right)
		\end{align}
		Since $\sigma^2 > 0$, $J\sigma^2 +1$ can not be equal to 0. Thus,
		\begin{align*}
			&\frac{\partial L(\sigma^2)}{\partial \sigma^2} = 0
			\Leftrightarrow 1- \frac{nJ^2}{2}\sigma^2 - \frac{nJ}{2} = 0
			\Leftrightarrow \sigma^2 = \frac{2 - nJ}{nJ^2}
		\end{align*}
		Since $n > 0, J >0$,
		\begin{align*}
			\begin{cases}
			\frac{2 - nJ}{nJ^2} > 0 \Rightarrow 2 > nJ \\
			\frac{2 - nJ}{nJ^2} \le 0 \Rightarrow 2 \le nJ
			\end{cases}
		\end{align*}
		In other words, the function has it extreme value in its support when both of n and J is equal to 1, and does not have otherwise.
		In the former case,
		\begin{align*}
			\frac{\partial L(0)}{\partial \sigma^2} = 1 - \frac{nJ}{2} = \frac{1}{2}
		\end{align*}
		the fact that we can calculate the first derivative at $\sigma^2 = 0$ implies that the function is continuous at the point. Then the marginal likelihood function is increasing function until it reaches its extreme value. Thus there is some neighborhood around $\sigma^2 = 0$ among which the function value is lower bounded by 1, that is bigger than 0. So we are done with this case.
		\par
		In the latter case,
		\begin{align*}
			\frac{\partial L(0)}{\partial \sigma^2} = 1 - \frac{nJ}{2} \leq 0
		\end{align*}
		By (1), we know the first derivative is negative when $\sigma^2 > 0$, and so the function is decreasing in this case. As the same as the previous case, the function is continuous as $\sigma^2 = 0$ since the first derivative exists at $\sigma^2 = 0$. Due to the definition of continuity of function, we can take some neighborhood around $\sigma = 0$ where the value generated by the function is included in $[1 - \epsilon, 1]$  for any $\epsilon > 0$. 1 is the limit of the marginal likelihood function as $\sigma^2 \to 0$. This means we can take the neighborhood around $\sigma^2 = 0$ whose value is lower bounded by some positive number.
		By the above two part, we prove the function is lower bounded away from 0. So we are done.
	\subsection{}
	By problem 2-a, we can derive the below posterior distribution.
		\begin{align*}
		p(\sigma^2 | {\bf y}) &\propto \frac{1}{\sigma^2} \frac{1}{\sqrt{J\sigma^2 + 1}^n} \exp \left( \frac{\sigma^2 \sum_{i=1}^{n} \left( \sum_{j=1}^{J} y_{ij}\right)^2}{2(J\sigma^2+ 1)}\right)
		\end{align*}
		\lstinputlisting[caption=2-c]{2-c.py}
		\begin{center}
		\includegraphics[width = 12cm]{2-c.png}
		\end{center}
	\subsection{}
	Use the previous notations. And by the same way we get the below posterior distributions.
		\begin{align*}
			p(\sigma^2 | \Theta, {\bf y}) &\propto \frac{1}{\sigma^2 \sqrt{2\pi\sigma^2}^n} \exp \left( -\frac{\sum_{i=1}^{n} \theta_{i}^{2}}{2\sigma^2}\right)\\
			p(\theta_i | \Theta_{-i}, {\bf y}, \sigma^2) &\propto N\left( \frac{\sigma^2 \sum_{j=1}^{J} y_{ij}}{1 + J\sigma^2}, \frac{\sigma^2}{1 + J\sigma^2}\right) \forall i \in \left\{ 1,2,3, \dots, n \right\} \\
		\end{align*}
		\lstinputlisting[caption=2-d]{2-d.py}
		\par
		Trace plot. $\sigma^2$ and $\theta_1$.
		\par
		\includegraphics[width = 9cm]{2-d-sigma.png}
		\includegraphics[width = 9cm]{2-d-theta.png}

\section{}
	\subsection{}
	Let $\hat{\beta} = ({\bf X}^T{\bf X})^{-1}{\bf X}^T{\bf y}$ and $\hat{\epsilon} = {\bf y}-\hat{{\bf y}}$.
		\begin{align*}
		p(\beta, \sigma^2 | {\bf X}, {\bf y}) &\propto \frac{1}{\sigma^2} p({\bf y} | {\bf X}, \beta, \sigma^2) \\
		&\propto (\sigma^2)^{-(\frac{n}{2} +1)} \exp\left( \frac{1}{2\sigma^2} ({\bf y} - {\bf X}\beta)^T ({\bf y} - {\bf X}\beta)\right)\\
		&= (\sigma^2)^{-(\frac{n}{2} +1)} \exp\left( \frac{1}{2\sigma^2} (\hat{\epsilon}^T\hat{\epsilon} + (\beta - \hat{\beta})^T ({\bf X}^T{\bf X})(\beta - \hat{\beta}) )\right)\\
		\end{align*}
	\subsection{}
	Let $\theta = \frac{2}{\hat{\epsilon}^T\hat{\epsilon} + (\beta - \hat{\beta})^T ({\bf X}^T{\bf X})(\beta - \hat{\beta}) )}$
		\begin{align*}
		p(\beta | {\bf X}, {\bf y}) &= \int \left( \frac{1}{\sigma^2}\right)^{\frac{n}{2} + 1} \exp \left( \frac{1}{\sigma^2 \theta}\right) \mathrm{d} \sigma^2
		= \int |\sigma^{-4}| \left(\frac{1}{\sigma^2} \right)^{\frac{n}{2} + 1} \exp \left( \frac{1}{\sigma^2 \theta}\right) \mathrm{d} \sigma^{-2}\\
		&= \int \left(\frac{1}{\sigma^2} \right)^{\frac{n}{2} -1} \exp \left( \frac{1}{\sigma^2 \theta}\right) \mathrm{d} \sigma^{-2}
		= \Gamma \left(\frac{n}{2} \right) \theta^{\frac{n}{2}} \int \frac{1}{\Gamma(\frac{n}{2})} \left(\frac{1}{\theta}\right)^{\frac{n}{2}} \left( \frac{1}{\sigma^2}\right)^{\frac{n}{2} -1} \exp \left( \frac{1}{\sigma^2 \theta}\right) \mathrm{d} \sigma^{-2}\\
		&= \Gamma \left(\frac{n}{2} \right) \left(\frac{1}{\theta} \right)^{-\frac{n}{2}}
		\propto \left(\frac{1}{\theta} \right)^{-\frac{n}{2}}\\
		&= \left( \frac{\hat{\epsilon}^T\hat{\epsilon}}{2} \frac{\hat{\epsilon}^T\hat{\epsilon} + (\beta - \hat{\beta})^T ({\bf X}^T{\bf X})(\beta - \hat{\beta}))}{\hat{\epsilon}^T\hat{\epsilon}}\right)^{-\frac{n}{2}}
		\propto \left( 1 + \frac{(\beta - \hat{\beta})^T ({\bf X}^T{\bf X})(\beta - \hat{\beta}))}{\hat{\epsilon}^T\hat{\epsilon}}\right)^{-\frac{n}{2}}\\
		\end{align*}
		\par
		This means the posterior distribution of $\beta$ is $t_{n-k} (\hat{\beta} , S_{\epsilon}^2({\bf X}^T{\bf X})^{-1})$, where $S_{\epsilon}^2 = \frac{\hat{\epsilon}^T\hat{\epsilon}}{n-k}$.
		\par
		Next we calculate the posterior distribution of $\sigma^2$.
		\begin{align*}
			p(\sigma^2 | {\bf X}, {\bf y}) &= \int (\sigma^2)^{-(\frac{n}{2} + 1)} \exp\left( -\frac{\hat{\epsilon}^T\hat{\epsilon} + (\beta - \hat{\beta})^T ({\bf X}^T{\bf X})(\beta - \hat{\beta})}{2\sigma^2}\right) \mathrm{d} \beta\\
			&= (2\pi)^{\frac{n}{2}} |\left(\frac{{\bf X}^T{\bf X}}{\sigma^2}\right)^{-1}| (\sigma^2)^{-(\frac{n}{2} + 1)} \exp\left( -\frac{\hat{\epsilon}^T\hat{\epsilon}}{2\sigma^2}\right) \\ &\int \frac{1}{(2\pi)^{\frac{n}{2}} |\left(\frac{{\bf X}^T{\bf X}}{\sigma^2}\right)^{-1}|} \exp\left( -\frac{1}{2} \left( (\beta - \hat{\beta})^T ((\frac{{\bf X}^T {\bf X}}{\sigma^2})^{-1})^{-1} (\beta - \hat{\beta})\right)\right) \mathrm{d} \beta \\
			&= \frac{(2\pi)^{\frac{n}{2}}}{|{\bf X}^T{\bf X}|}(\sigma^2)^{-\frac{n}{2}} \exp\left( -\frac{\hat{\epsilon}^T\hat{\epsilon}}{2\sigma^2}\right)\\
			&\propto (\sigma^2)^{-\frac{n}{2}} \exp\left( -\frac{\hat{\epsilon}^T\hat{\epsilon}}{2\sigma^2}\right)\\
		\end{align*}
		The last term tells us that the posterior distribution of $\sigma^2$ is inverse gamma distribution whose parameters are $(\frac{n}{2}, \frac{\hat{\epsilon}^T\hat{\epsilon}}{2})$.
		Thus, the posterior mean of $\beta$ is $\hat{\beta}$ and one of $\sigma^2$ is $\frac{\frac{\hat{\epsilon}^T\hat{\epsilon}}{2}}{\frac{n}{2} -1} = \frac{\hat{\epsilon}^T\hat{\epsilon}}{n-2}$.

\end{document}


\documentclass{article}
\usepackage{hyperref}
\usepackage{mathtools}
% \usepackage{mhchem}
\begin{document}
\title{Model G with fluid dynamics implemented in Tensorflow 2}
\author{Lumi Pakkanen}
\date{March 2020}
\maketitle
\section{Kinetic Reaction Equations}
The kinetic scheme of Model G reads
\begin{align}
A &\xrightleftharpoons[k_{-1}]{k_1} G \\
G &\xrightleftharpoons[k_{-2}]{k_2} X \\
B+X &\xrightleftharpoons[k_{-3}]{k_3} Y+Z \\
2X+Y &\xrightleftharpoons[k_{-4}]{k_4} 3X \\
X &\xrightleftharpoons[k_{-5}]{k_5} \Omega
\end{align}
This is represented by to following differential equations:
\begin{align}
\frac{\mathrm d G}{\mathrm d t} &= R_G(G, X, Y) = -(k_{-1} + k_2) G + k_{-2} X + k_1 A \label{eq:reaction} \\
\begin{split}
\frac{\mathrm d X}{\mathrm d t} &= R_X(G, X, Y) = k_2 G - (k_{-2} + k_3 B + k_5) X \\
                                & + k_{-3}Z Y - k_{-4}X^3 + k_4 X^2 Y + k_5 \Omega
\end{split}\\
\frac{\mathrm d Y}{\mathrm d t} &= R_Y(G, X, Y) = k_3 B X - k_{-3} Z Y + k_{-4} X^3 - k_4 X^2 Y
\end{align}
Because the equation is polynomial in G, X and Y we can easily solve it by developing the solution into a series
\begin{align*}
G(t) &= \sum_{n=0}^{\infty}a_n t^n \\
X(t) &= \sum_{n=0}^{\infty}b_n t^n \\
Y(t) &= \sum_{n=0}^{\infty}c_n t^n \\
\end{align*}
The time derivates become
\begin{align*}
G'(t) &= \sum_{n=1}^{\infty}n a_n t^{n-1} \\
X'(t) &= \sum_{n=1}^{\infty}n b_n t^{n-1} \\
Y'(t) &= \sum_{n=1}^{\infty}n c_n t^{n-1} \\
\end{align*}
Plugging this back into ~\ref{eq:reaction} we can recursively solve $a_{n+1}$, $b_{n+1}$ and $c_{n+1}$ in terms of $a_{n}$, $b_{n}$ and $c_{n}$. When integrating the system numerically we limit ourselves to $n\leq4$ and start from a known position
\begin{equation}
G(0) = a_0, X(0) = b_0, Y(0) = c_0
\end{equation}
and calculate a short time $\Delta t$ ahead
\begin{align*}
G(\Delta t) &= \sum_{n=0}^{4}a_n \Delta t^n \\
X(\Delta t) &= \sum_{n=0}^{4}b_n \Delta t^n \\
Y(\Delta t) &= \sum_{n=0}^{4}c_n \Delta t^n
\end{align*}
These new values for G, X and Y will be used as the $a_0$, $b_0$ and $c_0$ for the next time step.
Further numerical considerations include solving the steady state equations $R_J(G_0, X_0, Y_0)=0, \forall J \in \{G, X, Y\}$ and working with so called field variables $\varphi_J = J - J_0, J \in \{G, X, Y\}$. This makes sure that we're always adding small numbers together when integrating. The original equation has low numerical precission due to how floating point numbers work. Another issue to consider is the non-dimensionalization of the equations to make sure that all calculations happen in a numerically stable regime. The resulting differential equations are still polynomial in $\varphi_G$, $\varphi_X$ and $\varphi_Y$ so it's possible to recursively solve for another set of coefficients $a'_{n}$, $b'_{n}$ and $c'_{n}$ that produce a numerically stable and accurate step integrator $J(t) \mapsto J(t+\Delta t), J \in \{G, X, Y\}$

\section{Diffusion Equations}
The previous equations can be considered to be the 0-dimensional version of Model G. We now add a spatial component with diffusion.
\begin{align*}
\frac{\partial G}{\partial t} &= D_G \nabla^2 G + R_G(G, X, Y) \\
\frac{\partial X}{\partial t} &= D_X \nabla^2 X + R_X(G, X, Y) \\
\frac{\partial Y}{\partial t} &= D_Y \nabla^2 Y + R_Y(G, X, Y) \\
\end{align*}
When integrating the system numerically we alternate between solving the diffusion equation and the reaction equation for short time steps $\Delta t$. This is simpler than trying to solve the whole equation at once. The diffusion part has an exact solution.
\begin{align}
G(t, \mathbf{x}) &= \exp(-D_G |\overline{\omega}|^2 t + \mathrm i \overline{\omega} \cdot \mathbf{x}) \label{eq:diffusion_exact} \\
X(t, \mathbf{x}) &= \exp(-D_X |\overline{\omega}|^2 t + \mathrm i \overline{\omega} \cdot \mathbf{x}) \\
Y(t, \mathbf{x}) &= \exp(-D_Y |\overline{\omega}|^2 t + \mathrm i \overline{\omega} \cdot \mathbf{x})
\end{align}
Which describes a decaying complex plane wave advancing in the direction of $\overline{\omega}$. This is recognized as a component of a Fourier decomposition. If we're working with periodic boundary conditions we can use a Fast Fourier Transform to transform the numerical solution into a simple component-wise multiplication by the decay tensor.
\begin{equation}
\mathbf \Gamma_{J,\overline{\omega}} = \exp \left (-D_J \Delta t |\overline{\omega}|^2 \right), J \in {G, X, Y}
\end{equation}
\section{Fluid Dynamics}
We can augment the system further by introducing fluid dynamics.
\begin{align}
\frac{\partial G}{\partial t} + (\mathbf u \cdot \nabla) G &= D_G \nabla^2 G + R_G(G, X, Y) - G \nabla \cdot \mathbf u \label{eq:fluid_model_G}\\
\frac{\partial X}{\partial t} + (\mathbf u \cdot \nabla) X &= D_X \nabla^2 X + R_X(G, X, Y) - X \nabla \cdot \mathbf u \\
\frac{\partial Y}{\partial t} + (\mathbf u \cdot \nabla) Y &= D_Y \nabla^2 Y + R_Y(G, X, Y) - Y \nabla \cdot \mathbf u \\
\end{align}
It is assumed that the concentrations of $A$,$B$,$Z$ and $\Omega$ are held constant so that they follow along with the fluid, but do not cause additional density gradients that would couple back to the fluid flow.
Here $\mathbf u$ is a velocity field that obeys the Navier-Stokes equations for a compressible fluid
\begin{equation}
\frac{\partial \mathbf u}{\partial t} + (\mathbf u \cdot \nabla) \mathbf u = -c_s^2 \nabla \ln \rho + \nu \left (\nabla^2 \mathbf u + \frac{1}{3}\nabla(\nabla \cdot \mathbf u) + 2 \mathbf S \cdot \nabla \ln \rho \right ) \label{eq:navier-stokes}
\end{equation}
where $-c_s^2\nabla \ln \rho$ is the pressure term in the isothermal case and $c_s$ is the constant speed of "sound" in ether. $\nu$ is is the kinematic viscosity coefficient, which is assumed constant and $\mathbf S$ is the traceless rate-of-strain tensor:
\begin{equation}
S_{ij} = \frac{1}{2}\left ( \frac{\partial u_i}{\partial x_j} + \frac{\partial u_j}{\partial x_j} - \frac{2}{3}\delta_{ij}\nabla \cdot \mathbf u \right )
\end{equation}
The total density $\rho$ is a sum of the individual concentration densities
\begin{equation}
\rho = \rho_0 + \alpha_G G + \alpha_X X + \alpha_Y Y
\end{equation}
We note that the internal viscosity and shear equation
\begin{equation}
\frac{\partial \mathbf u}{\partial t} = \nu \left (\nabla^2 \mathbf u + \frac{1}{3}\nabla(\nabla \cdot \mathbf u) \right )
\end{equation}
Has the solution (with some abuse of notation)
\begin{equation}
u_j(t, \mathbf{x}) = \exp \left (-\nu \left (|\overline \omega|^2 + \frac{1}{3} \overline \omega_j (\overline 1 \cdot \overline \omega) \right) + \mathrm i \overline \omega_j \cdot \mathbf x \right)
\end{equation}
giving rise to the Fourier domain decay tensor
\begin{equation}
\mathbf \Theta_{j, \overline \omega} = \exp \left (-\nu \Delta t \left(|\overline{\omega}|^2 + \frac{1}{3} \overline \omega_j (\overline 1 \cdot \overline \omega) \right) \right) \label{eq:viscosity_shear_exact}
\end{equation}
for component-wise multiplication.
Because we're already working in the Fourier domain due to utilizing ~\ref{eq:diffusion_exact} and ~\ref{eq:viscosity_shear_exact} instead of using a local stencil we can calculate partial derivatives globally. For example partial differentation with respect to the $x$ direction becomes.
\begin{equation}
\frac{\partial G}{\partial x} = \mathcal F^{-1} \left(\mathrm i \omega_x \mathcal F (G)\right)
\end{equation}
Where the $\omega_x$ is a tensor corresponding to the wave numbers in the $x$ direction and the multiplication is done componentwise. It's good to note that many FFT libraries abuse the fact the frequencies wrap around so care must be taken when constructing $\omega_x$ to make sure that frequencies above the Nyquist bound are treated as negative frequencies.
The remaining parts of ~\ref{eq:fluid_model_G} and ~\ref{eq:navier-stokes} are integrated in terms of these very accurate global spatial derivatives. Unfortunately it still gives us only $O(\Delta t)$ accuracy in the $t$ direction.
\section{Algorithm}
Putting it all together we first derive the centered polynomial reaction integrator using a computer algebra system and further compile it with all constants curried in
\begin{equation}
I_{R}(\Delta t, A, B, Z, \Omega, k_1, k_{-1} ...)(\varphi_G, \varphi_X, \varphi_Y)|_{t=t_0} \mapsto \left ( \varphi_G, \varphi_X, \varphi_Y \right)|_{t=t_0+\Delta t}
\end{equation}
Working in a spatial volume $(L_x, L_y, L_z)$ divided into an equidistant grid of $(N_x, N_y, N_z)$ points we then construct the decay tensor for diffusion in the Fourier domain.
\begin{align}
\omega &\leftarrow meshgrid(range(-N_j/2, N_j/2) \cdot 2 \pi / L_j \  \mathrm{for} \ j \in (x, y, z)) \\
\Gamma_J &\leftarrow \exp \left(-\Delta t D_J (\omega_x^2 + \omega_y^2 + \omega_z^2) \right), J \in (G, X, Y)
\end{align}
The reaction diffusion integration over one time step $\Delta t$ now becomes
\begin{align}
\varphi_G, \varphi_X, \varphi_Y &\leftarrow I_R(\varphi_G, \varphi_X, \varphi_Y) \\
J &\leftarrow \mathcal F^{-1} \left(\Gamma_J \mathcal F(J)\right), J \in (G, X, Y)
\end{align}
TODO: Fluid dynamics algorithm
\end{document}


\documentclass[a4paper,12pt]{paper}
%\documentclass{jfm}
\usepackage{amsmath}
\usepackage{braket}
\usepackage[margin=1in]{geometry}
\usepackage[nonumberlist]{glossaries}
\usepackage{glossary-mcols}

\newcommand{\pder}[2][]{\frac{\partial#1}{\partial#2}}

\oddsidemargin=0pt
% \topmargin=0pt

%opening
\title{Helmholtz decomposition and spectral energy budget for shallow
water equations}
\author{\small Ashwin Vishnu Mohanan}
\date{\tiny \today}

\makeglossaries
%definitions
\newglossaryentry{zeta}{name=$\zeta$, description={absolute vorticity}}
\newglossaryentry{kappa}{name=$\kappa$, description={mag. of wavenumber}}
\newglossaryentry{sigma}{name=$\sigma$,
description={freq. of IG waves, $\sqrt{f^2+(c\kappa)^2}$}}
\newglossaryentry{J}{name=$\mathbf{J}$, description={mass
flux, $h\mathbf{u}$}}
\newglossaryentry{M}{name=$\mathbf{M}$, description={displaced mass flux,
$\eta\mathbf{u}$}}
\newglossaryentry{W}{name=$\mathbf{W}$, description={derived variable vector}}
\newglossaryentry{U}{name=$\mathbf{U}$, description={primitive variable vector}}
\newglossaryentry{B}{name=$\mathbf{B}$, description={normal mode vector,
dimension:
$LT^{-1}$}}
\newglossaryentry{N}{name=$\mathbf{N}$, description={normal mode vector,
dimension:
$T^{-1}$}}
\newglossaryentry{Xn}{name=$X_n$, description={normalized eigenvector matrix}}
\newglossaryentry{P}{name=$P$, description={transformation matrix from
$\mathbf{W}$ to $\mathbf{U}$}}
\newglossaryentry{Q}{name=$Q$, description={transformation matrix from
$\mathbf{U}$ to $\mathbf{B}$}}
\glossarystyle{mcolindex}
\glsaddall

\begin{document}

\maketitle

\printglossaries

\section{Governing Equations}

The governing equations for a shallow layer of fluid are:
\begin{align}
  \partial_t \mathbf u
      = &- (\mathbf{u}.\nabla) \mathbf{u} - c^2 \nabla h - f\mathbf{e_z} \times
\mathbf u \label{dtu0} \\
  \partial_t h
      =& - \nabla. (h \mathbf u) \label{dth}
\end{align}
Equation \eqref{dtu0} may also be written in the \textit{rotational form} as:
\begin{equation}
 \label{dtu}
 \partial_t \mathbf u
 = - \nabla |\mathbf u|^2/2 - c^2 \nabla h - \zeta \times \mathbf u
\end{equation}
where, \gls{zeta}
represents absolute vorticity, i.e. the sum of vorticity and system rotation.
Furthermore, equations \eqref{dtu0} and \eqref{dth} can be combined to form an
equation for the \textit{total mass flux}, \gls{J} $= h\mathbf{u}$:
\begin{equation}
  \label{dtJ}
 \partial_t \mathbf J = -(\mathbf{u}.\nabla)\mathbf{J} - \nabla(c^2h^2)/2 -
\zeta \times \mathbf J - (\nabla. \mathbf J)\mathbf u
\end{equation}
Similarly, an equation for the \textit{displaced mass flux}, \gls{M} $ =
\eta\mathbf{u}$ is:
\begin{equation}
  \label{dtM}
 \partial_t \mathbf M = -(\mathbf{u}.\nabla)\mathbf{M} - \nabla(c^2\eta^2)/2 -
\zeta \times \mathbf M - (\nabla.\mathbf{u} + \nabla.\mathbf{M})\mathbf u
\end{equation}
For a divergence free flow, a Poisson equation for \emph{h} can be formulated.
Taking divergence of \eqref{dtu},
yields the Poisson equation:
\begin{equation}
  \label{poisson}
  \nabla^2 h = \frac{1}{c^2} \left[ \nabla.(\zeta \times \mathbf u )
	      - \nabla^2 \frac{|u|^2}{2} \right]
\end{equation}
Applying fourier transform, the spectral counterpart for \eqref{poisson} in
tensor notation is:
\begin{equation}
\label{poisson_fft}
  -\kappa^2 \hat{h} = \frac{1}{c^2} \left[ ik_i (\widehat{\epsilon_{ijk} \zeta_j
u_k})
	      + \kappa^2 \frac{\widehat{u_i u_i}}{2} \right]
\end{equation}

\section{Helmholtz Decomposition}
\subsection{For velocity field, u}
The Helmholtz decomposition theorem, or the fundamental theorem of vector
calculus,
states that any well-behaved vector field can be decomposed into
the sum of a longitudinal (diverging, non-curling, irrotational) vector field
and
a transverse (solenoidal, curling, rotational, non-diverging) vector field.
This allows us to express the velocity field as:
\begin{align}
\label{helm_u}
\mathbf u & = -\nabla \times (e_z \Psi) + \nabla \Phi\\
	& =  -\nabla \times \Psi_z + \nabla \Phi
\end{align}

For the sake of clarity,
we shall denote the rotational (vortical) and divergent (wave) parts of the
velocity with the suffix \emph{r} and \emph{d} respectively. Thus,
\begin{align}
\mathbf u_r & = -\nabla \times \Psi_z ; &\mathbf u_d =   \nabla \Phi
\end{align}
And therefore, $\mathbf u  = \mathbf u_r + \mathbf u_d$.

To find the projection operators for the divergent part, taking divergence of
\eqref{helm_u} gives $\nabla .\mathbf{u} = \nabla^2 \Phi
$.
This transforms in the spectral plane as, $ik_j \hat{u}_j = -\kappa^2
\hat{\Phi}$, implying:
\begin{align}
\mathbf{\hat{u}}_d =& ik_i \hat{\Phi} = \frac{k_i k_j}{\kappa^2} \hat{u}_j\\
\mathbf{\hat{u}}_r =& \mathbf{\hat{u}} - \mathbf{\hat{u}}_d = \left( \delta_{ij}
- \frac{k_i k_j}{\kappa^2} \right) \hat{u}_j
\end{align}

Thus, for two-dimensions the decomposed velocity are represented as follows,

\begin{align}
\mathbf{\hat{u}}_d^x =& \frac{k_x (k_x \hat{u}_x + k_y \hat{u}_y)}{\kappa^2} \\
\mathbf{\hat{u}}_r =& \hat{u}_{x} - \frac{k_x  (k_x \hat{u}_x + k_y \hat{u}_y)}{\kappa^2}
\end{align}

\subsection{For fluid depth, h}
To obtain a similar decomposition, $h = h_r + h_d$, one should use the Poisson
equation \eqref{poisson_fft}.
Since Poisson equation requires a divergence free flow, the LHS of
\eqref{poisson_fft} would correspond to the rotational part of the flow in the
transformed plane, i.e. $\hat{h}_r$. Henceforth, the divergent part, $\hat{h}_d$
can be obtained by subtracting $\hat{h}_r$ from $\hat{h}$.

\section{Normal mode decomposition}

\subsection{Following Bartello 1995}
In order to isolate oscillating fast modes that display no nonlinearity, one
can adopt a linearization approach followed by a normal mode decomposition.
As a result of linearization, from \eqref{dtu} and \eqref{dth}:
  \begin{align}
    \partial_t \mathbf u =& - c^2 \nabla \eta - f\mathbf{e_z} \times \mathbf u
  \label{dtu_l} \\
    \partial_t \eta =& - \nabla.  \mathbf u \label{dth_l}
  \end{align}
Taking curl and divergence of the linearized equation for momentum
\eqref{dtu_l} gives the following evolution equations with change of variables:
  \begin{align}
    \partial_t \zeta =& - f \delta
  \label{dtcurl_l} \\
    \partial_t \delta =&  f \zeta - c^2 \nabla^2 \eta \label{dtdiv_l} \\
    \partial_t \eta =& - \delta \label{dteta_l}
  \end{align}
where $\zeta$ and $\delta$ are relative vorticity and divergence as functions
of $\mathbf{r}$ and $t$ respectively. Representing the dependent flow
quantities in terms of Fourier modes:
  \begin{align}
  \begin{pmatrix}
      u \\ v \\ \eta \\ \zeta \\ \delta
  \end{pmatrix} (\mathbf{r},t)
  = \int
  \begin{pmatrix}
      \hat{u} \\ \hat{v} \\ \hat{\eta} \\ \hat{\zeta} \\ \hat{\delta}
  \end{pmatrix} e^{i(\mathbf k . \mathbf{r} - \omega t)} \mathbf{dk} d\omega
  \end{align}
allows to rewrite the system of equations \eqref{dtcurl_l} to \eqref{dteta_l}
as an eigenvalue problem:
\begin{align}
 i\omega
   \begin{Bmatrix}
       \hat{\zeta} \\ \hat{\delta} \\c\kappa\hat{\eta}
  \end{Bmatrix}
  = i
  \begin{bmatrix}
     0 & if & 0 \\
   -if &  0 & -ic\kappa \\
     0 & ic\kappa & 0
  \end{bmatrix}
  \begin{Bmatrix}
       \hat{\zeta} \\ \hat{\delta} \\c\kappa\hat{\eta}
  \end{Bmatrix}
\end{align}
where, $\kappa = |\mathbf{k}|$.

Let us define $A$ as the Hermitian matrix operating on the vector
$\mathbf{W} = \{\hat{\zeta}, \hat{\delta} ,c\kappa \hat{\eta} \}^T $.which
yields the
familiar dispersion relation
for the slow geostrophic mode
and fast Poincar\'e wave modes:
\begin{equation}
 \omega^{(0)} = 0;\quad \omega^{(\pm)}=\pm \sigma
\end{equation}
where, $\sigma = \sqrt{f^2 + c^2\kappa^2 }$. These are the eigenvalues of
the matrix operator \emph{A}. Since \emph{A} is Hermitian, the corresponding
eigenvectors are orthogonal and these are normalized as follows
\begin{align}
  \mathbf X^{(0)}_n =
    \frac{1}{\sigma}
    \begin{Bmatrix}
       -c\kappa \\ 0 \\ f
     \end{Bmatrix}; \quad
  \mathbf X^{(\pm)}_n =
    \frac{1}{\sqrt{2} \sigma}
    \begin{Bmatrix}
       f \\ \mp i\sigma \\ c\kappa
     \end{Bmatrix}
\end{align}
Let $X_n$ be the eigenvector matrix, which follows the
property $X_n \bar{X}_n^{T}=I$. It can, then, be applied to diagonalize the
system of
equations as follows:
\begin{align}
 \partial_t \mathbf{W} =& [A] \mathbf{W}\\
 \partial_t (\bar{X}_n^T \mathbf{W}) =& \bar{X}_n^T[A]X_n
\bar{X}_n^T\mathbf{W}\\
			    =& [\Lambda] (\bar{X}_n^T\mathbf{W})
\end{align}
Thus, the alternate diagonalized system of equations for the normal modes are
given by:
\begin{align}
 \partial_t
\mathbf{N}
  = &
  \begin{bmatrix}
     0 & 0 & 0 \\
     0 & -i\sigma & 0 \\
     0 & 0 & i\sigma
  \end{bmatrix}
  \mathbf{N}\\
\text{where},
   \mathbf{N} = \bar{X}_n^T \mathbf{W}
   = &\frac{1}{\sqrt{2}\sigma}
   \begin{Bmatrix}
       -\sqrt{2}c\kappa(\hat{\zeta} -f \hat{\eta}) \\
        f\hat{\zeta} + c^2\kappa^2\hat{\eta} - i\sigma\hat{\delta} \\
       f\hat{\zeta} + c^2\kappa^2\hat{\eta} + i\sigma\hat{\delta}
   \end{Bmatrix}
\end{align}

The first mode represents \emph{linearised potential vorticity} which is
conserved in this framework. The remaining two are the \emph{linearised
ageostrophic or wave modes}. In the absence of system rotation, i.e. $f=0$,
the normal modes are simply,
\begin{equation}
\label{nmode}
 \mathbf{N} =
 \begin{Bmatrix}
  \hat \zeta \\
  \frac{1}{\sqrt{2}} (c\kappa \hat{\eta} - i\hat{\delta})\\
  \frac{1}{\sqrt{2}} (c\kappa \hat{\eta} + i\hat{\delta})
 \end{Bmatrix}
\end{equation}

%---------------------------------------------%
\subsection{Following Farge \& Sadourny 1989}
Instead of finding the normal modes for the vorticity, divergence and
displacement field of the flow, we shall make use of the Helmholtz
decomposition described in \eqref{helm_u}. The shallow water equations then
transform to:
 \begin{align}
    \partial_t \psi =&  f \phi
  \label{dtpsi_l} \\
    \partial_t \phi =&  -f \psi - c^2 \eta \label{dtphi_l} \\
    \partial_t \eta =& - \nabla^2 \phi \label{dteta_l2}
  \end{align}
where $\psi$ and $\psi$ are stream function and velocity potential as functions
of $\mathbf{r}$ and $t$ respectively. By substituting the dependent
variables with the respective Fourier transform, this reduces to the eigenvalue
problem:
\begin{align}
 i\omega
   \begin{Bmatrix}
       \hat{\psi} \\ \hat{\phi} \\ \hat{\eta}
  \end{Bmatrix}
  = i
  \begin{bmatrix}
     0 & if & 0 \\
   -if &  0 & ic^2 \\
     0 & -i\kappa^2 & 0
  \end{bmatrix}
  \begin{Bmatrix}
       \hat{\psi} \\ \hat{\phi} \\ \hat{\eta}
  \end{Bmatrix}
\end{align}
the square matrix is not Hermitian and this would result in complex
eigenvalues. By adopting the following change of variables:
\begin{equation}
 \hat{\psi} \to \kappa^2\hat \psi = \hat{\zeta} ; \quad
 \hat{\phi} \to -\kappa^2\hat \phi = \hat{\delta}; \quad
 \hat{\eta} \to c\kappa\hat \eta
\end{equation}
it falls back to the previous eigenvalue problem as demonstrated in the
previous section. In other words, we can use the same eigenvector matrix, $X_n$
to find the normal modes of:

$$\mathbf{H} = \{\hat \psi,\; \hat \phi,\;  \eta  \}^T$$
which is closely related to:
$$\mathbf{W}
= \{\kappa^2\hat \psi,\; -\kappa^2\hat
\phi,\; c\kappa\hat \eta  \}^T
= \{\hat \zeta;\; \hat \delta;\;  c\kappa \hat \eta \}^T $$

%---------------------------------------------%
\subsection{Normal mode inversion to primitive variables}
To begin with, we shall form a new vector $\mathbf{B} = \mathbf{N}/\kappa$
which will have the same dimensions as velocity.
It has been shown before that the normal modes can be represented by $\mathbf{N}
= \bar{X}_n^T \mathbf{W}$. Now,  we shall form a new vector $\mathbf{B} =
\mathbf{N}/\kappa$ which will have the same dimensions as velocity. Thus,
\begin{align}
   \mathbf{B}
   = &\frac{1}{\sqrt{2}\sigma}
    \begin{Bmatrix} \sqrt{2} c\left(-
    \kappa^{2} \hat \psi +  f\hat\eta \right)\\
    \kappa \left(c^{2} \eta + f \psi + i \phi \sigma\right)\\
    \kappa \left(c^{2} \eta + f \psi - i \phi \sigma\right)
    \end{Bmatrix}
\end{align}

The $\mathbf{B}$ vector can also be related to the primitive variable vector,
$\mathbf{U} = \{\hat{u},\hat{v},c\hat{\eta}\}^T$ using transformation matrices
as follows:
\begin{align}
 \mathbf{B} =&\frac{1}{\kappa}[\bar{X}_n]^T W\\
	    =&\frac{1}{\kappa}[\bar{X}_n]^T [P] \mathbf{U}
\end{align}
where,
\begin{equation}P=
\begin{bmatrix}- i k_{y} &  i k_{x} & 0\\ i k_{x} &  i
k_{y} &
0\\0 & 0 & \kappa\end{bmatrix}
\end{equation}
It is also straightforward to show that the magnitude of the normal modes
equals the total energy as:
\begin{align}
 \mathbf{\bar B}^T\mathbf{B}
 =&\frac{1}{\kappa^2} \mathbf{\bar U}^T
     [\bar P]^T[{X}_n]  [\bar{X}_n]^T [P] \mathbf{U} \\
 =& \mathbf{\bar U}^T\mathbf{U}
\end{align}
implying, $$2(E_K+E_P)=\sum_i B^{(i)}\bar{B}^{(i)}=\kappa^{2} \psi
\overline{\psi} +\kappa^{2} \phi \overline{\phi} + c^{2}
\eta \overline{\eta}
= u\bar{u} + v\bar{v} + c^2\eta\bar{\eta}$$
This allows us to represent the primitive variables in terms of normal modes as
$$\mathbf{U} = [Q]\mathbf{B}$$
where, the inversion matrix is
\begin{align}
 Q
 = & \kappa [P]^{-1}[\bar{X}_n]^{T^{-1}}\\
 =& \frac{1}{\sqrt{2}\sigma\kappa}
 \begin{bmatrix}
      -i\sqrt{2}c\kappa  k_{y} &
      \left(i f k_{y} +
	  k_{x}   \sigma\right) &
      \left(i f k_{y} - k_{x} \sigma\right)\\
      i \sqrt{2}c\kappa k_{x} &
      \left(-  i f k_{x} +  k_{y}   \sigma\right) &
      - \left(i f  k_{x} + k_{y} \sigma\right)\\
      \sqrt{2}\kappa f &
       c\kappa^{2} &
      c\kappa^{2}
\end{bmatrix}
\end{align}
In tensor notation,
\begin{align}
 \hat{u}_l =& \epsilon_{lm3} ik_m\left[ -\frac{c}{\sigma} B^{(0)} +
	      \frac{f}{\sqrt{2}\sigma\kappa} (B^{(+)} + B^{(-)})
	      \right]
	      + k_l \frac{1}{\sqrt{2}\kappa}  (B^{(+)} - B^{(-)})\\
c\hat{\eta} =& \frac{f}{\sigma} B^{(0)} + \frac{c\kappa}{\sqrt{2}\sigma}
(B^{(+)} + B^{(-)})
\end{align}

\section{Spectral energy budget}
\subsection{Kinetic energy}
%---------------------------------------------%
\subsubsection{Expression for mean kinetic energy}
Kinetic energy being cubic for shallow water equations, it can be split into
quadratic and non-quadratic parts as follows:
\begin{align*}
  E_K(\mathbf{r},t)
    =& h\mathbf{u}.\mathbf{u}\\
    =& (1+\eta)\mathbf{u}.\mathbf{u}\\
    =& \mathbf{u}.\mathbf{u} + \eta\mathbf{u}.\mathbf{u}\\
    =& E_K^Q + E_K^{NQ}
\end{align*}

%---------------------------------------------%
\subsubsection{K.E. budget}
Let the rate of change of kinetic energy, without any approximations, be
written as:
\begin{equation}\label{dtKE}
 \pder{t}E_K(\mathbf{k},t) = T_K + C_K
\end{equation}
where, $T_K$ and $C_K$ represents the transfer and conversion spectral
functions respectively. Equation
\eqref{dtKE} splits into:
\begin{align*}
 \pder{t}E_K^Q + \pder{t}E_K^{NQ}
 =&  (T_K^{Q} + C_K^{Q}) + (T_K^{NQ} + C_K^{NQ})
%  =& \frac{1}{2}\partial_t(\mathbf{\hat u}.\widehat{h\mathbf{u}}^*)\\
%  =& \frac{1}{2}\partial_t(\mathbf{\hat u}.\hat{\mathbf{J}}^*)
\end{align*}
%-------------------------------------------------%
\paragraph{Quadratic K.E. budget:}
Consider the rate of change of quadratic kinetic energy,
\begin{equation}\label{dtKE2}
 \pder{t}E_K^{Q}(\mathbf{k},t) = T_K^{Q} + C_K^{Q}
\end{equation}
Following \eqref{dtu0}, equation \eqref{dtKE2} expands into:
\begin{align*}
 \partial_t E_K^{Q}(\mathbf{k},t)
 =& \frac{1}{2}\partial_t(\mathbf{\hat u}.\mathbf{\hat u^*})\\
 =& \frac{1}{2}\left[ \mathbf{\hat u} .\pder[\mathbf{\hat u^*}]{t}
		      + \mathbf{\hat u^*}. \pder[\mathbf{\hat u}]{t}\right]\\
 =& \frac{1}{2}\left[ -\hat{u}_i (\widehat{ u_j\partial_j u_i })^*
    - c^2 \hat{u}_i (ik_i \hat{\eta})^*
    - \hat{u}_i (\epsilon_{i3k} f \hat{u}_k)^*
    - ... \text{conjugate terms}
    \right]\\
 =& -\text{Re}\left[ \hat{u}_i (\widehat{ u_j\partial_j u_i })^*
    + c^2 \hat{u}_i (ik_i \hat{\eta})^*
    + \hat{u}_i (\epsilon_{i3k} f \hat{u}_k)^* \right]
\end{align*}
% since, $u_i (\epsilon_{ijk} \zeta_j u_k) = 0$
% \footnote{ \textbf{Note}
% $\epsilon_{ijk} \zeta_j u_k
% = \epsilon_{jki}  (\epsilon_{jlm} \partial_l u_m +   f_j )u_k$}
% =& (\delta_{kl}\delta_{im} - \delta_{km} \delta_{il})\partial_l u_m +
% \epsilon_{ijk} f_j u_k\\
% =& u_k\pder{x_k}u_i - \pder{x_i}u_ku_k + \epsilon_{ijk} f_j u_k
we have,
\begin{align}
 T_K^{Q}= &-\text{Re}\left[\hat{u}_i (\widehat{ u_j\partial_j u_i })^*
	   \right]\\
 C_K^{Q}= &-\text{Re}\left[   c^2 \hat{u}_i (ik_i \hat{\eta})^*  \right]\\
 	= &-\text{Re}\left[   c^2 \hat{u}_i^* ik_i \hat{\eta} \right]
\end{align}

%-------------------------------------------------%
\paragraph{Non-quadratic K.E. budget:}
\begin{align*}
  \pder{t}E_K^{NQ}(\mathbf{k},t) =& T_K^{NQ} + C_K^{NQ}
\end{align*}
Following equations \eqref{dtu0} and \eqref{dtM} for rate of change of velocity
$\mathbf{u}$ and the displaced mass flux, $\mathbf M = \eta \mathbf{u}$
respectively,
 \begin{align*}
 \partial_t E_K^{NQ}(\mathbf{k},t)
 =& \frac{1}{2}\partial_t(\mathbf{\hat M}.\mathbf{\hat u^*})\\
 =& \frac{1}{2}\left[ \mathbf{\hat M} .\pder[\mathbf{\hat u^*}]{t}
		      + \mathbf{\hat u^*}. \pder[\mathbf{\hat M}]{t}\right]\\
 =& \frac{1}{2}\left[-\hat{M}_i(\widehat{ u_j\partial_j u_i })^*
		      - c^2 \hat{M}_i (ik_i \hat{\eta})^*
		      - \hat{M}_i (\epsilon_{i3k} f \hat{u}_k)^* \right]\\
  &+\frac{1}{2}\left[ - \hat{u}_i^* \widehat{(u_j\partial_j M_i) }
		      - c^2\hat{u}_i^*ik_i\widehat{\eta\eta}/2
		      - \hat{u}_i^* (\epsilon_{i3k} f \hat{M}_k)
		      - \hat{u_i}^* \widehat{(u_i \partial_j({u}_j+
M_j))}
			\right]
\end{align*}
Hence,
\begin{align}
 T_K^{NQ}= & -\frac{1}{2}\left[\hat{M}_i(\widehat{ u_j\partial_j u_i })^*
			      + \hat{u}_i^* \widehat{(u_j\partial_j M_i) }
			      + \hat{u_i}^* \widehat{(u_i \partial_j{M}_j)}
			      \right] \\
 T_{K,K}^{NQ,Q}= & -\frac{1}{2}\left[ \hat{u_i}^* \widehat{(u_i
\partial_j{u}_j)}
			\right] \\
 T_{K,P}^{NQ,Q}= & -\frac{1}{2}\left[c^2 \hat{M}_i (ik_i \hat{\eta})^*
	      + c^2\hat{u}_i^*ik_i\widehat{\eta\eta} /2
	      \right]
\end{align}

\subsubsection{Helmholtz decomposition of kinetic energy}

Furthermore, using Helmholtz decomposition to substitute for $\mathbf u$ in
the expression
for \emph{mean kinetic energy},

\begin{align}
\braket{ E_K}
& = \frac{1}{2} \braket{ h\mathbf u.\mathbf u} \\
& = \frac{1}{2} \braket{h(\mathbf{u_r} + \mathbf{u_d} ).(\mathbf{u_r} +
\mathbf{u_d} )}\\
& = \frac{1}{2}\braket{ h |\mathbf{u_r} | ^2}
    -\braket{ \mathbf{u_r}.h\mathbf{u_d}}
    +  \frac{1}{2} \braket{ h | \mathbf{u_d}| ^2}
\end{align}
where, $h = 1 + \eta$ and, $\eta$ is the surface displacement of the shallow
water layer. In the limit for small displacements i.e., $\eta \to 0$ or $ h \to
1$, invoking orthogonality,

\begin{equation}
\lim_{h \to 1} \braket{E_K} = \braket{E_K^{Q}}
=  \frac{1}{2}\braket{|\mathbf{u_r}  | ^2 + | \mathbf{u_d} | ^2}
\end{equation}
The spectral equivalent of the above expression will be:
\begin{equation}
\braket{E_K^{Q} }
=  \frac{1}{2}\braket{ \mathbf{\hat{u}_r}.\mathbf{\hat{u}_r}^* +
\mathbf{\hat{u}_d}.\mathbf{\hat{u}_d}^* }
\end{equation}

%---------------------------------------------------------%
\subsection{Available potential energy}
\subsubsection{Expression for mean available potential energy}
\begin{align}
\braket{ E_P}
& = \frac{c^2}{2} \braket{ \eta^2}
\end{align}
The spectral equivalent of which is:
\begin{align}
\braket{ E_P}
& = \frac{c^2}{2} \braket{ \hat{\eta}\hat{\eta}^*}
\end{align}

\subsubsection{A.P.E. budget}
Similar to equation \eqref{dtKE} we can form,
\begin{equation}\label{dtPE}
 \pder{t}E_P(\mathbf{k},t) = T_P+ C_P
\end{equation}
where, $T_P$ and $C_P$ represents the transfer and conversion spectral
functions respectively.

Following \eqref{dth}, equation \eqref{dtPE} expands as:
\begin{align*}
 \pder{t}E_P(\mathbf{k},t)
 =& \frac{c^2}{2}\partial_t({\hat \eta}{\hat \eta^*})\\
 =& \frac{c^2}{2}\left[ \hat{\eta} .\pder[\hat{\eta}^*]{t}+ \hat
\eta^*.\pder[\hat \eta]{t}\right]\\
 =& \frac{c^2}{2}\left[ - \hat{\eta} (ik_i\hat{ u_i } + ik_i\widehat{
      \eta u_i })^*
	    - \hat{\eta}^* (ik_i\hat{ u_i } + ik_i\widehat{
      \eta u_i })  \right]\\
      =& -\text{Re}\left[c^2\hat{\eta} (ik_i\hat{ u_i } + ik_i\widehat{
      \eta u_i })^* \right]
\end{align*}
Thus,
\begin{align}
T_P= & -\text{Re}\left[c^2\hat{\eta} (ik_i\widehat{
      \eta u_i })^*  \right]\\
C_P=&  -\text{Re}\left[c^2\hat{\eta} (ik_i\hat{u_i })^*\right]\\
   =&  +\text{Re}\left[c^2\hat{\eta} ik_i\hat{u_i }^*\right]
\end{align}
thus, $C^Q_K = -C_P$ which, in turn, makes the assertion that
equivalent conversion occurs between quadratic K.E. and A.P.E.
%
% \begin{align*}
% 2\times T_K(\mathbf{k},t)
% & = \partial_t (\mathbf u.h\mathbf u)\\
% &= \partial_t \mathbf u .h\mathbf u  +|\mathbf u|^2 \partial_t h + h\mathbf u
% .\partial_t \mathbf u \\
% & =   2 \left( -  \frac{\nabla|\mathbf u|^2}{2} - c^2 \nabla h - \zeta_a \times \mathbf u	\right).h\mathbf u - \left( |\mathbf u|^2 \nabla.(h \mathbf u )\right)\\
% & =   \left( -  h\frac{\nabla|\mathbf u|^3}{3} - c^2 \frac{\nabla h^2}{2} . \mathbf u \right)- \left( |\mathbf u|^2 \nabla.(h \mathbf u )\right) \\
% \end{align*}
%
%

\end{document}


\documentclass[a4paper]{article}

\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{amsmath,amsthm,amsfonts,amssymb,euscript,stmaryrd,graphicx,appendix,enumitem,yhmath,mathrsfs,superbox}
\usepackage{fullpage}
\usepackage{color,psfrag}
\usepackage[normalem]{ ulem }
\usepackage{soul}

\definecolor{ACORRIGER}{rgb}{0.9,0,0}

\def\myqed {{% set up
\parfillskip=0pt % so \par doesnt push \square to left
\widowpenalty=10000 % so we dont break the page before \square
\displaywidowpenalty=10000 % ditto
%\finalhyphendem*erits=0 % TeXbook exercise 14.32
%
% horizontal
\leavevmode % \nobreak means lines not pages
\unskip % remove previous space or glue
\nobreak % don't break lines
\hfil % ragged right if we spill over
\penalty50 % discouragement to do so
\hskip.2em % ensure some space
\null % anchor following \hfill
\hfill % push \square to right
$\square$% % the end-of-proof mark
%
% vertical
\par}} % build paragraph

\newcommand{\footnoteremember}[2]{
 \footnote{#2}
 \newcounter{#1}
 \setcounter{#1}{\value{footnote}}
}
\newcommand{\footnoterecall}[1]{
 \footnotemark[\value{#1}]
}
\def\andcr{%
 \end{tabular}%
 \\
 \begin{tabular}[t]{c}}

\begin{document}

\title{1D PDE solver}

\section{Introduction}

The price of a European option satitisfies the \textit{Black Scholes equation}
\begin{gather*}
0 = \frac{\partial f}{\partial t}(s, t) + \frac{1}{2}\sigma^2 s^2 \frac{\partial^2 f}{\partial s^2}(s, t)
+ r s \frac{\partial f}{\partial s}(s, t) - rf(s, t)
\end{gather*}
with the final condition $f(s, T) = V(s)$ where $V(s)$ is the payout.

Applying the variable change $x = log(s)$ yields
\begin{gather*}
\frac{\partial f}{\partial t}(x, t) = -\frac{1}{2}\sigma^2 \frac{\partial^2 f}{\partial x^2}(x, t)
+ \frac{1}{2}(\sigma^2 - r) \frac{\partial f}{\partial x}(x, t) + rf(x, t)
\end{gather*}

with the final condition $f(x, T) = V(x) = V(e^s)$.

A common method to solve this PDE is to use finite differences to approximate the derivatives and then
solve the equation on a discrete mesh.

\section{Finite difference method}

We first define a mesh over space and time, usually the time is discretized such that the time step $dt$ is 1 day.
The boundaries are $0$ (today) and $T$ (the maturity of the option). The space mesh is centered to the log of the
current spot ($log(s_0)$)and the boundaries are set to $log(s_0) \pm5stddev = log(s_0) \pm 5 \sigma \sqrt{T}$.
Let's call $N$ the number of points in space and $dx$ the space step.

We use the following approximations for the derivatives:

\begin{itemize}
\item $\frac{\partial^2 f}{\partial x^2}(x_i, t_n) = \frac{f_{i+1}^n - 2 f_i^n + f_{i-1}^n}{dx^2}$
\item $\frac{\partial f}{\partial x}(x_i, t_n) = \frac{f_{i+1}^n - f_{i-1}^n}{2dx}$
\item $\frac{\partial f}{\partial t}(x_i, t_n) = \frac{f_i^{n+1} - f_i^{n}}{dt}$
\end{itemize}
where ${f_i^n = f(x_i, t_n)}$.

Let's define
\begin{gather*}L_i^n = -\frac{1}{2}\sigma^2\frac{f_{i+1}^n - 2 f_i^n + f_{i-1}^n}{dx^2} + \frac{1}{2}(\sigma^2 - r)  \frac{f_{i+1}^n - f_{i-1}^n}{2dx} + rf_i^n
\end{gather*}  the \textit{Black Scholes equation} then becomes a system of linear equations
\begin{gather*}
\frac{f_i^{n+1} - f_i^{n}}{dt} = L_i^{n+1}, i \in [1, N-1] \Longleftrightarrow
f_i^n = f_i^{n+1} - L_i^{n+1}dt, i \in [1, N-1]
\end{gather*}
Since we are solving a backward equation (given the payoff at maturity $T$, we want to compute the price of the option today), evaluating $L_i$
at $t^{n+1}$ yields an explicit scheme, i.e we can direclty compute $f_i^n$ given the price at the previous iteration $f_i^{n+1}$. While this scheme
converges fast, it is highly instable.

Another solution is to evaluate $L_i$ at $t^n$, we get
\begin{gather*}
\frac{f_i^{n+1} - f_i^{n}}{dt} = L_i^{n}, i \in [1, N-1] \Longleftrightarrow
f_i^n + L_i^n dt = f_i^{n+1}, i \in [1, N-1]
\end{gather*}
Here we need to invert a linear system to compute $f_i^n$. While this is unconditionnally stable, this is slow to converge.

To get the benefits from both schemes, the solution is to use a linear combination of them, this is called a theta scheme:
\begin{gather*}
\frac{f_i^{n+1} - f_i^{n}}{dt} = \theta L_i^n + (1-\theta) L_i^{n+1}
\end{gather*}
The closer $\theta$ is to $1$, the more stable the scheme is. The closer $\theta$ is to $0$ the faster the scheme converges. $\theta = \frac{1}{2}$ gives
the best convergence speed under unconditionnal stability and is thus usually chosen. It is known as the Crank-Nicholson scheme.

\section{Boundary conditions}

At the boundaries of the space mesh ($i = 0$ and $i = N$) we cannot use centered finite difference
\begin{itemize}
\item $\frac{\partial^2 f}{\partial x^2}(x_0, t_n) = \frac{f_{2}^n - 2f_{1}^n + f_{0}^n}{dx^2}$
\item $\frac{\partial f}{\partial x}(x_0, t_n) = \frac{f_{1}^n - f_{0}^n}{dx}$
\item $\frac{\partial^2 f}{\partial x^2}(x_N, t_n) = \frac{f_{N}^n - 2f_{N-1}^n + f_{N-2}^n}{dx^2}$
\item $\frac{\partial f}{\partial x}(x_N, t_n) = \frac{f_{N}^n - f_{N-1}^n}{dx}$
\end{itemize}

We then need to specify values of f at the boundaries ($f_0$ and $f_N$) to be able to solve the linear system. This is called Dirichlet boundary conditions. Another possibility is to impose the derivatives at the boundaries, these are the Neumann boundary conditions.

\section{Implement the solver in C++}

You have just joined the quantitative team of a bank, who needs to reimplement its PDE solver. Indeed, the first version was developed by a battalion of
interns who were learning C++ during their break for lunch, between 12:00 and 12:06 (because you know, the market does not close for lunch); as a surprising result, it is higly unstable and slow (but sometimes it gives a correct price). The villains have been punished: the interns have been fired and the manager responsible for this mess has been promoted, so at least you won't have to interact with him.

The new manager hired you for your expert skills in C++ and want you to fix the issue before mid January (or you will meet the same fate as the interns):

\begin{itemize}
\item Write the maths to get the exact expression of the linear system to solve
\item Implement the solver in C++, keeping in mind the following constraints
\begin{itemize}
\item the solver should be able to accept any kind of payoff
\item the solver must be flexible enough so the client can specify the boundary conditions
\item the solver must be flexible enough so the client can speficy different diffusion models (so $\sigma$ and $r$ might be more complicated than simple constants)
\item the client should be able to specify the mesh and the value of $\theta$
\end{itemize}
\item Test your implementation by rolling back the payoff of a call and comparing the result with the Back Scholes closed-form
\end{itemize}

Even if you are happy with your base compensation, you think an extra bonus cannot hurt. Once everything is implemented and works great, show your
manager how you can get the $\delta$, $\gamma$ and $\theta$ for free. Explain what is required to get the $\nu$ and then wait for your \st{manager}
bonus!
\end{document}


\subsection{Label for Each Day}

Since the data are to be reduced into daily records, there is a problem of determining the labels. For the purpose of our project, we have to assign to every observation (i.e. a specific day) a label representing the weather type of the next day based on precipitation data collected from different stations.

According to the U.S. National Weather Service (NWS), the threshold for daily precipitation is usually 0.01 inches (0.25 mm). With such threshold, we may get daily weather type for every station. It is then natural to let them vote for the daily weather type of the city: a day should be viewed as ``a day with precipitation" if and only if the precipitation recorded are more than 0.01 inches for over half of the stations. To make it clear, a specific day should be labeled according to the proportion of the stations recording more than 0.01 inches of precipitation in following day.

Such labels can be justified by showing that it meets the data from NWS pretty well.\footnote{In fact, the voting threshold, i.e. $50\%$, is also determined by minimizing the RSS (residual sum of squares).}

By applying such labels, we may notice that the data set is quite balanced: there are 974 days with precipitation among the 2192 days observed here.

\begin{figure}[h]
\center\includegraphics[width = .7\textwidth]{rainyday.png}
\caption{Monthly Number of Days Precipitation \(\geq\) 0.01 Inches}
\label{rainyday}
\end{figure}