%!TEX root = ./writeup.tex

\newcommand{\bl}{B_{L,i}(x)}
\newcommand{\br}{B_{R,i}(x)}
\newcommand{\jil}{J_{i,L}}
\newcommand{\jir}{J_{i,R}}
\newcommand{\eddphi}[1]{\edd_{#1}\phi_{#1}}
\newcommand{\alphai}[2]{\frac{#1}{\Sigma_{t,#2}h_{#2}}}

\section{Mixed Hybrid Finite Element Method Acceleration}

The \gls{mhfem} as applied to Eqs. \ref{eq:zero} and \ref{eq:first} uses the following basis functions:
	\begin{subequations}
	\begin{equation} \label{mhfem:BL}
		\bl = \begin{cases}
			\frac{x_{i+1/2} - x}{x_{i+1/2} - x_{i-1/2}}, \ x \in [x_{i-1/2}, x_{i+1/2}] \\
			0, \ \text{otherwise}
		\end{cases}
	\end{equation}
	\begin{equation} \label{mhfem:BR}
		\br = \begin{cases}
			\frac{x - x_{i-1/2}}{x_{i+1/2} - x_{i-1/2}}, \ x \in [x_{i-1/2}, x_{i+1/2}] \\
			0, \ \text{otherwise}
		\end{cases}.
	\end{equation}
	\end{subequations}
The scalar flux is constant within a cell with discontinuous jumps at the cell edges. In other words,
	\begin{equation} \label{mhfem:flux}
		\phi_i(x) = \begin{cases}
			\phi_i, \ x \in (x_{i-1/2}, x_{i+1/2}) \\
			\phi_{i\pm 1/2}, x = x_{i\pm1/2} \\
			0, \ \text{otherwise}
		\end{cases}
	\end{equation}
with
	\begin{equation} \label{mhfem:sumphi}
		\phi(x) = \sum_{i=1}^I \phi_i(x).
	\end{equation}
The Eddington factor will be interpolated onto the same grid as the scalar flux so that cell edge and cell center values will be available.

The current, $J(x)$, is a piecewise linear function defined by
	\begin{equation} \label{mhfem:J}
		J(x) = \sum_{i=1}^I \jil \bl + \jir \br
	\end{equation}
where $\jil$ and $\jir$ are the current on the left and right edges of the cell.

\subsection{Interior}
On the interior cells, $i \in [2, I-1]$, each cell has five unknowns: $\phi_{i-1/2}$, $\phi_i$, $\phi_{i+1/2}$, $\jil$, and $\jir$. Since $\phi_{i+1/2} = \phi_{(i+1)-1/2}$, only $\phi_{i}$, $\phi_{i+1/2}$, $\jil$, and $\jir$ must be specified in each cell as the left edge of the cell is specified by the right edge of the previous cell. Thus four equations are needed for each interior cell.

The first is found by integrating Eq. \ref{eq:zero} over cell $i$ yielding the balance equation
	\begin{equation} \label{mhfem:balance}
		\jir - \jil + \Sigma_{a,i} \phi_i h_i = Q_i h_i.
	\end{equation}
Equations for the cell edge current, $\jil$ and $\jir$, are found by multiplying Eq. \ref{eq:first} by $\bl$ and $\br$ and integrating over cell $i$. This yields
	\begin{subequations}
	\begin{equation}
		\int_{x_{i-1/2}}^{x_{i+1/2}} \bl \dderiv{}{x} \edd(x) \phi(x) + \bl \Sigma_t(x) J(x) \ud x = 0
	\end{equation}
	\begin{equation}
		\int_{x_{i-1/2}}^{x_{i+1/2}} \br \dderiv{}{x} \edd(x) \phi(x) + \br \Sigma_t(x) J(x) \ud x = 0
	\end{equation}
	\end{subequations}
Integrating by parts produces
	\begin{subequations}
	\begin{equation} \label{mhfem:half1}
		-\edd_{i-1/2}\phi_{i-1/2} + \edd_i \phi_i + \Sigma_{t,i} h_i \left(\frac{\jil}{3}
			+ \frac{\jir}{6} \right) = 0
	\end{equation}
	\begin{equation} \label{mhfem:half2}
		\edd_{i+1/2}\phi_{i+1/2} - \edd_i \phi_i + \Sigma_{t,i}h_i \left(\frac{\jil}{6} + \frac{\jir}{3}\right) = 0.
	\end{equation}
	\end{subequations}
Eliminating $\jil$ from Eq. \ref{mhfem:half1} and $\jir$ from Eq. \ref{mhfem:half2}:
	\begin{equation} \label{mhfem:jir_f}
		\jir = \frac{-2}{\Sigma_{t,i}h_i} \left[ \eddphi{i-1/2} - 3\eddphi{i} + 2\eddphi{i+1/2} \right]
	\end{equation}
	\begin{equation} \label{mhfem:jil_f}
		\jil = \frac{-2}{\Sigma_{t,i} h_i} \left[ -2 \eddphi{i-1/2} + 3\eddphi{i} - \eddphi{i+1/2}\right]
	\end{equation}

The equation for $\phi_{i+1/2}$ is found by enforcing continuity of current such that the current on the right side of cell $i$ is equivalent to the current on the left side of cell $i+1$. In other words,
	\begin{equation}
		\jir = J_{i+1,L}
	\end{equation}
Applying this condition to Eqs. \ref{mhfem:jir_f} and \ref{mhfem:jil_f}:
	\begin{equation}
		\begin{aligned}
		\alphai{-2}{i} \eddphi{i-1/2} &+ \alphai{6}{i} \eddphi{i} - 4 \left(
			\frac{1}{\Sigma_{t,i}h_i} + \frac{1}{\Sigma_{t,i+1}h_{i+1}}\right)\eddphi{i+1/2} \\
		&+ \alphai{6}{i+1}\eddphi{i+1} - \alphai{2}{i+1}\eddphi{i+3/2} = 0.
		\end{aligned}
	\end{equation}

The required four equations can be reduced to two by replacing $\jir$ and $\jil$ in Eq. \ref{mhfem:balance} with Eqs. \ref{mhfem:jir_f} and \ref{mhfem:jil_f}. This produces the system of equations for every interior cell $i \in [2, I-1]$:
	\begin{subequations} \label{mhfem:intsys}
		\begin{equation} \label{mhfem:intsys1}
			\phi_i = \frac{
				\alphai{6}{i}\eddphi{i-1/2} + \alphai{6}{i}\eddphi{i+1/2} + Q_i h_i
			}{
				\Sigma_{a,i}h_i + \alphai{12}{i}\edd_i
			}
		\end{equation}
		\begin{equation} \label{mhfem:intsys2}
			\phi_{i+1/2} = \frac{
				\alphai{-2}{i} \eddphi{i-1/2} + \alphai{6}{i} \eddphi{i} + \alphai{6}{i+1}\eddphi{i+1} - \alphai{2}{i+1}\eddphi{i+3/2}
			}{4\left(
				\frac{1}{\Sigma_{t,i}h_i} + \frac{1}{\Sigma_{t,i+1}h_{i+1}}\right) \edd_{i+1/2}
			}
		\end{equation}
	\end{subequations}

\subsection{Boundary}
Equation \ref{mhfem:intsys} provides $2(I-2)$ equations for the $2I+1$ unknowns. Thus, five boundary equations are required. The values not accounted for in Eq. \ref{mhfem:intsys} are $\phi_{1/2}$, $\phi_{1}$, $\phi_{3/2}$, $\phi_{I}$, and $\phi_{I+1/2}$.

The Marshak boundary condition is
	\begin{equation}
		\phi(x) + 2J(x) = 0.
	\end{equation}
At $x = x_{1/2}$:
	\begin{equation}
		\phi_{1/2} + 2J_{1,L} = 0
	\end{equation}
Using Eq. \ref{mhfem:jil_f} evaluated at $i=1$,
	\begin{equation}
		\phi_{1/2} = \frac{
			\frac{6}{\Sigma_{t,i}h_i} \eddphi{1} - \frac{2}{\Sigma_{t,i}h_i}\eddphi{3/2}
		}{
			\frac{1}{2} + \frac{4}{\Sigma_{t,i}h_i}\edd_{1/2}
		}
	\end{equation}
Applying a reflecting boundary on the right edge $J(x_{I+1/2}) = J_{I,R} = 0$:
	\begin{equation}
		\phi_{I+1/2} = \frac{
			-\eddphi{I-1/2} + 3\eddphi{I}
		}{
			2\edd_{i+1/2}
		}
	\end{equation}
$\phi_1$ and $\phi_I$ are specified by Eq. \ref{mhfem:intsys1} with $i=1$ and $i=I$. The remaining unknown, $\phi_{3/2}$, is found through Eq. \ref{mhfem:intsys2} with $i=1$.
There are now $2I+1$ equations with $2I+1$ unknowns. This system can be solved with the inversion of a banded matrix of bandwidth five.


This chapter is an extended version of the recently published paper
\citep{MERLINI2019177} which collects results about
Riordan arrays in the framework of \textit{matrix functions};  actually,
the following methodology applies to any square matrix $m\times m$ with
\textit{exactly one} eigenvalue $\lambda$ of \textit{algebraic}
multiplicity $m \in\mathbb{N}$.  Generalized Lagrange bases are used to
construct Hermite polynomials that interpolate a family of functions;
moreover, we show a parallel application of such functions via Jordan
canonical forms and case studies are given.

\section{Introduction}

\label{sec:matrices:functions:introduction}
\input{Riordan-matrices-functions/introduction.tex}

\section{Basic definitions and notations}

\input{Riordan-matrices-functions/matrix-functions.tex}

Now we apply this framework to the Riordan group.

\section{Riordan matrices}

\input{Riordan-matrices-functions/riordan-group.tex}

\iffalse % \subsection{A Riordan array characterization of Hermite interpolating polynomials} {{{

\input{Riordan-matrices-functions/Ra-g-characterization.tex}

\subsection{A component matrices characterization of Hermite interpolating polynomials}

\input{Riordan-matrices-functions/Zs-g-characterization.tex}
\fi
% }}}

\section{Functions and polynomials}

In this section we instantiate the abstract framework just described to functions
\begin{displaymath}
\begin{split}
f(z)&=z^{r},\,{f(z)=\frac{1}{z}},\,{f(z)=\sqrt{z}},\,{f(z)=e^{\alpha z}},\\
f(z)&=log\,{z},\,f(z)=sin\,{z}\quad\text{and}\quad f(z)=cos\,{z},
\end{split}
\end{displaymath}
where $r,\alpha\in\mathbb{R}$; in parallel, we construct and show corresponding
Hermite interpolating polynomials in a sequence of theorems, respectively.
From now on, we use $m$ and $\lambda$ instead of $m_{1}$ and $\lambda_{1}$ to
simplify the notation; moreover, we instantiate $\lambda=1$ which is the
natural eigenvalue for Riordan arrays.

We start by generalizing the $r$-th power $A^{r}$, usually carried out
as $\underbrace{A\cdots A}_{r\text{ times}}$, to \textit{rational} powers
$r\in\mathbb{Q}$.

\input{Riordan-matrices-functions/function-pow.tex}

Instantiation $r=-1$ in the previous theorem yields a Hermite interpolating
polynomial for the inverse function which, in the explicit form, reduces to
a binomial transform.

\input{Riordan-matrices-functions/function-inverse.tex}

\vfill

Instantiation $r=\frac{1}{2}$ yields the interpolation of the square root function,
we report its derivation for completeness.

\input{Riordan-matrices-functions/function-sqrt.tex}

Matrix exponentiation is a well studied problem \citep{MOLERLOAN2003}, here
we show another way in the Riordan arrays domain.

\input{Riordan-matrices-functions/function-exp.tex}

We show a dual theorem of the previous one concerning the interpolation of the
logarithm function.

\vfill

\input{Riordan-matrices-functions/function-log.tex}

\begin{remark}
For the sake of completeness, a Hermite interpolating polynomial $g$ could
also be studied by relaxing the condition $\lambda=1$ thus considering
$\hat{g}(z,\lambda)$ which subsumes $g(z)=\hat{g}(z,1)$. Here are one of these
augmented polynomials interpolating the $log$ function
\iffalse
\begin{displaymath}
\begin{split}
\hat{I}_{8}{\left (z, \lambda \right )} &= - \frac{z^{7}}{\lambda^{8}} \\
&+ z^{6} \left(\frac{1}{\lambda^{7}} + \frac{7}{\lambda^{8}}\right) \\
&+ z^{5} \left(- \frac{1}{\lambda^{6}} - \frac{6}{\lambda^{7}} - \frac{21}{\lambda^{8}}\right) \\
&+ z^{4} \left(\frac{1}{\lambda^{5}} + \frac{5}{\lambda^{6}} + \frac{15}{\lambda^{7}} + \frac{35}{\lambda^{8}}\right) \\
&+ z^{3} \left(- \frac{1}{\lambda^{4}} - \frac{4}{\lambda^{5}} - \frac{10}{\lambda^{6}} - \frac{20}{\lambda^{7}} - \frac{35}{\lambda^{8}}\right) \\
&+ z^{2} \left(\frac{1}{\lambda^{3}} + \frac{3}{\lambda^{4}} + \frac{6}{\lambda^{5}} + \frac{10}{\lambda^{6}} + \frac{15}{\lambda^{7}} + \frac{21}{\lambda^{8}}\right) \\
&+ z \left(- \frac{1}{\lambda^{2}} - \frac{2}{\lambda^{3}} - \frac{3}{\lambda^{4}} - \frac{4}{\lambda^{5}} - \frac{5}{\lambda^{6}} - \frac{6}{\lambda^{7}} - \frac{7}{\lambda^{8}}\right) \\
&+ \frac{1}{\lambda} + \frac{1}{\lambda^{2}} + \frac{1}{\lambda^{3}} + \frac{1}{\lambda^{4}} + \frac{1}{\lambda^{5}} + \frac{1}{\lambda^{6}} + \frac{1}{\lambda^{7}} + \frac{1}{\lambda^{8}}
\end{split}
\end{displaymath}
and
\fi
\begin{displaymath}
\begin{split}
\hat{L}_{8}{\left (z,\lambda \right )} &= \frac{z^{7}}{7 \lambda^{7}} \\
&+ z^{6} \left(- \frac{1}{6 \lambda^{6}} - \frac{1}{\lambda^{7}}\right) \\
&+ z^{5} \left(\frac{1}{5 \lambda^{5}} + \frac{1}{\lambda^{6}} + \frac{3}{\lambda^{7}}\right) \\
&+ z^{4} \left(- \frac{1}{4 \lambda^{4}} - \frac{1}{\lambda^{5}} - \frac{5}{2 \lambda^{6}} - \frac{5}{\lambda^{7}}\right) \\
&+ z^{3} \left(\frac{1}{3 \lambda^{3}} + \frac{1}{\lambda^{4}} + \frac{2}{\lambda^{5}} + \frac{10}{3 \lambda^{6}} + \frac{5}{\lambda^{7}}\right) \\
&+ z^{2} \left(- \frac{1}{2 \lambda^{2}} - \frac{1}{\lambda^{3}} - \frac{3}{2 \lambda^{4}} - \frac{2}{\lambda^{5}} - \frac{5}{2 \lambda^{6}} - \frac{3}{\lambda^{7}}\right) \\
&+ z \left(\frac{1}{\lambda} + \frac{1}{\lambda^{2}} + \frac{1}{\lambda^{3}} + \frac{1}{\lambda^{4}} + \frac{1}{\lambda^{5}} + \frac{1}{\lambda^{6}} + \frac{1}{\lambda^{7}}\right) \\
&+ log{\left (\lambda \right )} - \frac{1}{\lambda} - \frac{1}{2 \lambda^{2}} - \frac{1}{3 \lambda^{3}} - \frac{1}{4 \lambda^{4}} - \frac{1}{5 \lambda^{5}} - \frac{1}{6 \lambda^{6}} - \frac{1}{7 \lambda^{7}}.
\end{split}
\end{displaymath}
\end{remark}

%Finally, we show two theorem concerning trigonometric functions $sin$ and $cos$, respectively.

\input{Riordan-matrices-functions/function-sin.tex}

\input{Riordan-matrices-functions/function-cos.tex}

\subsection{Case studies}

\input{Riordan-matrices-functions/matricial-Hermite-polys.tex}

\vfill

\input{Riordan-matrices-functions/case-study-pascal.tex}

\input{Riordan-matrices-functions/case-study-catalan.tex}

\input{Riordan-matrices-functions/case-study-stirling-II.tex}

Finally, we report sine and cosine function applications in Table
\ref{table:sin:cos:matrices:functions}; finally, Equation
\ref{eq:matrices:functions:sin:cos:identity} shows that the identity
$sin^{2}\,z + cos^{2}\,z=1$ is preserved by the framework of matrices functions
and even more trigonometric identities can be checked; for example, the
polynomial
\begin{displaymath}
\begin{split}
{SS_{ 8 }}{\left (z \right )} &= - \frac{z^{7}}{5040} cos{\left (2 \right )} + z^{6} \left(- \frac{1}{720} sin{\left (2 \right )} + \frac{1}{360} cos{\left (2 \right )}\right) \\
    &+ z^{5} \left(- \frac{1}{120} cos{\left (2 \right )} + \frac{1}{60} sin{\left (2 \right )}\right) + z^{4} \left(- \frac{1}{24} sin{\left (2 \right )} - \frac{1}{36} cos{\left (2 \right )}\right) \\
    &+ z^{3} \left(- \frac{1}{9} sin{\left (2 \right )} + \frac{1}{18} cos{\left (2 \right )}\right)  + z^{2} \left(\frac{7}{15} cos{\left (2 \right )} + \frac{1}{6} sin{\left (2 \right )}\right) \\
    &+ z \left(- \frac{19}{45} cos{\left (2 \right )} + \frac{14}{15} sin{\left (2 \right )}\right) - \frac{19}{45} sin{\left (2 \right )} - \frac{286}{315} cos{\left (2 \right )}
\end{split}
\end{displaymath}
interpolates the function $f(\theta)=sin(2\,\theta)$
which allows us to check the identity $sin(2\,\theta)=2\,sin\theta\,cos\theta$
for a Riordan matrix $\theta$, formally $SS(\theta)~=~2\,S(\theta)\,C(\theta)$.

% a comment about the correct definition of the Lagrange base. {{{
\iffalse
In the same spirit, many other functions and their relations with already
studied ones could be an interesting field of investigation -- we would recall
that before starting the lifting process of a scalar function, the user
has to pay attention to the correct definition of the generalized Lagrange base
because it all depends on the eigenvalues of the matrix under study.
\fi
% }}}

\begin{table}
%\vspace*{-1cm}
    \caption[][-1cm]{$sin{\mathcal{P}_{8}}$,$cos{\mathcal{P}_{8}}$,$sin{\mathcal{C}_{8}}$,$cos{\mathcal{C}_{8}}$,
$sin{\mathcal{S}_{8}}$ and $cos{\mathcal{S}_{8}}$}
\input{Riordan-matrices-functions/sines-and-cosines.tex}
\label{table:sin:cos:matrices:functions}
\end{table}

\section{Jordan canonical form}

\input{Riordan-matrices-functions/jordan-form.tex}
\input{Riordan-matrices-functions/case-study-fibonacci.tex}

\section*{Conclusions}

\input{Riordan-matrices-functions/conclusions.tex}

\vfill


\newpage

\captionsetup{labelformat=empty}

\section*{\texorpdfstring{Appendix D: VOI for conservation auctions
heuristic solution \emph{n} =
3}{Appendix D: VOI for conservation auctions heuristic solution n = 3}}\label{appendix-d-voi-for-conservation-auctions-heuristic-solution-n-3}
\addcontentsline{toc}{section}{Appendix D: VOI for conservation auctions
heuristic solution \emph{n} = 3}

\textbf{Priors}

Let \(A_1\), \(A_2\), \(A_3\) be assets. Each has some value \(c_1\),
\(c_2\), and \(c_3\) which are uncertain with a joint prior probability
distribution,

\begin{equation}
\begin{bmatrix}c_1 \\ c_2 \\ c_3\end{bmatrix}\sim\mathcal{N}\left(\begin{bmatrix}\mu_1 \\ \mu_2 \\ \mu_3\end{bmatrix}, \begin{bmatrix}\sigma^2_1 & 0 & 0 \\ 0 & \sigma^2_2 & 0\\ 0 & 0 & \sigma^2_3 \end{bmatrix}\right)
(\#eq:jointprior)
\end{equation}

where \(\mu_1\), \(\mu_2\), and \(\mu_3\) are means and \(\sigma_1\),
\(\sigma_2\) and \(\sigma_3\) are the prior standard deviations.

\textbf{Utilities}

We assign utilities to each rank order of \(c_1\), \(c_2\), and \(c_3\)
in combination with each action of purchasing one of \(A\), \(B\) or
\(C\),

\begin{equation}
\begin{aligned}
u(A_1, c_1 > c_2 > c_3)&=1\\
u(A_1, c_1 > c_3 > c_2)&=1\\
u(A_1, c_2 > c_1 > c_3)&=0.5\\
u(A_1, c_3 > c_1 > c_2)&=0.5\\
u(A_1, c_2 > c_3 > c_1)&=0\\
u(A_1, c_3 > c_2 > c_1)&=0\\
u(A_2, c_2 > c_1 > c_3)&=1\\
\mathrm{etc...}&
\end{aligned}
(\#eq:utilitiesapen2)
\end{equation}

such that utility is maximised when we purchase the highest ranked
asset, zero when we purchase the lowest ranked and somewhere inbetween
when we purchase the middle ranked asset.

\textbf{Ranking probabilties}

We can express the probability of any assets being in a given rank order
as the probabililty of two differences being less than zero. Such that,
for example,

\begin{equation}
\Pr(c_1 > c_2 > c_3) = \Pr(c_2 - c_1 < 0,  c_3 - c_2 < 0)
(\#eq:probrankapen)
\end{equation}

Given this we define two new variables, \(z_1\) and \(z_2\) where

\begin{equation}
\begin{aligned}
  z_1 &= c_2 - c_1,\\
  z_2 &= c_3 - c_2
\end{aligned}
(\#eq:z12apen)
\end{equation}

Even if \(c_1\), \(c_2\), and \(c_3\) are all uncorrelated \(z_1\) and
\(z_2\) will not be, where

\begin{equation}
\mathrm{cov}(z_1, z_2)=\mathrm{cov}(c_1, c_2) - \mathrm{var}(c_2) - \mathrm{cov}(c_1, c_3) + \mathrm{cov}(c_2, c_3)
(\#eq:covz)
\end{equation}

Which, when \(c_1\), \(c_2\), and \(c_3\) are all uncorrelated
simplifies to

\begin{equation}
\mathrm{cov}(z_1, z_2)=-\mathrm{var}(c_2)
(\#eq:covz2)
\end{equation}

Therefore the joint distribution of \(z_1\) and \(z_2\) is,

\begin{equation}
\begin{bmatrix}z_1\\z_2\end{bmatrix}
  \sim\mathrm{N}\left(
  \begin{bmatrix}\mu_2-\mu_1\\\mu_3-\mu_2\end{bmatrix},
  \begin{bmatrix}\sigma^2_1+\sigma^2_2&-\sigma^2_2\\-\sigma^2_2&\sigma^2_2+\sigma^2_3\end{bmatrix}\right)
(\#eq:jointzapen)
\end{equation}

To obtain \(\Pr(c_1 > c_2 > c_3)\) we evalute the multivariate
cumulative distribution function of \(z_1\) and \(z_2\),

\begin{equation}
\Phi(z_1,z_2)
(\#eq:phiz)
\end{equation}

within the limits, \(-\infty\) and 0.

\textbf{Expected value of perfect information}

To calculate the prior expected utility of purchasing any asset, we
weight the utilities for that action (eqn. 2) by the relevant
probablities calculated from eqns. 3--9. For instance;

\begin{equation}
\begin{aligned}
  \mathrm{E}[u(A_1)] = & 1 \times \Pr(c_1 > c_2 > c_3) + 1 \times \Pr(c_1 > c_3 > c_2)\,+ \\
  &0.5 \times \Pr(c_2 > c_1 > c_3) + 0.5 \times \Pr(c_3 > c_1 > c_2)\,+ \\
  &0 \times \Pr(c_2 > c_3 > c_1) + 0 \times \Pr(c_3 > c_2 > c_1)
\end{aligned}
(\#eq:EuA1apen)
\end{equation}

The expected value of perfect information then is,

\begin{equation}
\mathrm{EVPI}=1-\max(\mathrm{E}[u(A_1)],\mathrm{E}[u(A_2)],\mathrm{E}[u(A_3)])
(\#eq:evpiapen2)
\end{equation}

\textbf{Updating}

Now suppose we can update the priors for \(c_1\), \(c_2\), and \(c_3\)
by taking \(M\) samples from sampling distrubutions with, for
simplicity, some fixed variance of 1 and centered on \(\mu_1\),
\(\mu_2\) and \(\mu_3\) respectively. Further, we can define \(p_1\) and
\(p_2\) as the proportion of the \(M\) samples allocated to sampling for
\(A_1\) and \(A_2\) respectively with \(1 - p_1 - p_2\) being allocated
to \(A_3\). We can then use these samples to update the priors for
\(c_1\), \(c_2\), and \(c_3\) to obtain preposterior estimates,
\(c^\prime_1\), \(c^\prime_2\), and \(c^\prime_3\), where,

\begin{equation}
\begin{bmatrix}c^\prime_1 \\ c^\prime_2 \\ c^\prime_3\end{bmatrix}\sim\mathcal{N}\left(\begin{bmatrix}\mu_1 \\ \mu_2 \\ \mu_3\end{bmatrix}, \begin{bmatrix}\frac{\sigma^2_1}{Mp_1\sigma^2_1 + 1} & 0 & 0 \\ 0 & \frac{\sigma^2_2}{Mp_2\sigma^2_2 + 1} & 0\\ 0 & 0 & \frac{\sigma^2_3}{M(1 - p_1 - p_2)\sigma^2_3 + 1} \end{bmatrix}\right)
(\#eq:jointpost)
\end{equation}

\textbf{Expected value of sample information}

For any given new rank order, based on the updated preposterior
distributions, we can again calculate a probablity by defining new
variables (i.e., \(z^\prime_1\) and \(z^\prime_2\)) and evalute their
multivariate cumulative distribution as in eqns. 3--9. Therefore we can
obtain the preposterior expected utilities for each purchase action by
weighting the preposterior probablites by their respective utilities as
in eqn. 10. Accordingly the expected vale of sample information is

\begin{equation}
\mathrm{EVSI}=\max(\mathrm{E}^\prime[u(A_1)],\mathrm{E}^\prime[u(A_2)],\mathrm{E}^\prime[u(A_3)])-\max(\mathrm{E}[u(A_1)],\mathrm{E}[u(A_2)],\mathrm{E}[u(A_3)])
(\#eq:evsiapen2)
\end{equation}

\textbf{Optimisation}

Using eqns 8-13 we can find the optimal values of \(p_1\) and \(p_2\)
for any given \(M\) that will maximise the EVSI. Below we examine a
number of \_\_Case studies for different sets of prior distrbutions for
\(c_1\), \(c_2\) and \(c_3\).

\textbf{Summary}

\begin{itemize}
\tightlist
\item
  Optimal allocation sensitive to ratios of \(\mu\)'s.
\item
  Optimal allocation sensitive to ratio of \(\sigma\)'s.
\item
  Not always preferential to sample asset with greater uncertainty.
\item
  Solution is symmetrical.
\end{itemize}

\textbf{Case study 1: homogenous prior \(\sigma\)'s and homogenous prior
means}

\includegraphics{figure/x000_1_1_1-1.png} \clearpage

\textbf{Case study 2a: homogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 = \mu_2 > \mu_3\)

\includegraphics{figure/x110_1_1_1c-1.png} \clearpage

\textbf{Case study 2b: homogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 > \mu_2 = \mu_3\)

\includegraphics{figure/x100_1_1_1c-1.png} \clearpage

\textbf{Case study 2c: homogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 > \mu_2 > \mu_3\)

\includegraphics{figure/x10n1_1_1_1c-1.png} \clearpage

\textbf{Case study 3a: heterogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 > \mu_2 = \mu_3\)

\(\sigma_1 > \sigma_2 = \sigma_3\)

\includegraphics{figure/x100___1_1_1c-1.png} \clearpage

\textbf{Case study 3b: heterogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 > \mu_2 = \mu_3\)

\(\sigma_1 < \sigma_2 = \sigma_3\)

\includegraphics{figure/x1001_1_1c-1.png} \clearpage

\textbf{Case study 3c: heterogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 = \mu_2 > \mu_3\)

\(\sigma_1 = \sigma_2 < \sigma_3\)

\includegraphics{figure/x110__1__1_1c-1.png} \clearpage

\textbf{Case study 3d: heterogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 > \mu_2 = \mu_3\)

\(\sigma_1 = \sigma_2 > \sigma_3\)

\includegraphics{figure/x100__1__1_1c-1.png} \clearpage

\textbf{Case study 3e: heterogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 > \mu_2 = \mu_3\)

\(\sigma_1 = \sigma_2 < \sigma_3\)

\includegraphics{figure/x100_1_1__1c-1.png} \clearpage

\textbf{Case study 3f: heterogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 = \mu_2 > \mu_3\)

\(\sigma_1 = \sigma_3 > \sigma_2\)

\includegraphics{figure/x110__1_1__1c-1.png} \clearpage

\textbf{Case study 3g: heterogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 = \mu_2 > \mu_3\)

\(\sigma_1 = \sigma_3 < \sigma_2\)

\includegraphics{figure/x110_1__1_1c-1.png} \clearpage

\textbf{Case study 3h: heterogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 > \mu_2 = \mu_3\)

\(\sigma_1 < \sigma_2 < \sigma_3\)

\includegraphics{figure/x100_1__1___1c-1.png} \clearpage

\textbf{Case study 3i: heterogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 > \mu_2 = \mu_3\)

\(\sigma_2 < \sigma_1 < \sigma_3\)

\includegraphics{figure/x100__1_1___1c-1.png} \clearpage

\textbf{Case study 3j: heterogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 > \mu_2 = \mu_3\)

\(\sigma_1 > \sigma_2 > \sigma_3\)

\includegraphics{figure/x100___1__1_1c-1.png} \clearpage

\textbf{Case study 3k: heterogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 = \mu_2 > \mu_3\)

\(\sigma_1 < \sigma_2 < \sigma_3\)

\includegraphics{figure/x110_1__1___1c-1.png} \clearpage

\textbf{Case study 3l: heterogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 = \mu_2 > \mu_3\)

\(\sigma_1 < \sigma_3 < \sigma_2\)

\includegraphics{figure/x110_1___1__1c-1.png} \clearpage

\textbf{Case study 3m: heterogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 = \mu_2 > \mu_3\)

\(\sigma_1 > \sigma_2 > \sigma_3\)

\includegraphics{figure/x110___1__1_1c-1.png} \clearpage

\textbf{Case study 3n: heterogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 > \mu_2 > \mu_3\)

\(\sigma_1 < \sigma_2 = \sigma_3\)

\includegraphics{figure/x10n1_1__1__1c-1.png} \clearpage

\textbf{Case study 3o: heterogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 > \mu_2 > \mu_3\)

\(\sigma_1 = \sigma_2 < \sigma_3\)

\includegraphics{figure/x10n1__1__1_1c-1.png} \clearpage

\textbf{Case study 3p: heterogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 > \mu_2 > \mu_3\)

\(\sigma_1 = \sigma_3 > \sigma_2\)

\includegraphics{figure/x10n1__1_1__1c-1.png} \clearpage

\textbf{Case study 3q: heterogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 > \mu_2 > \mu_3\)

\(\sigma_1 = \sigma_2 < \sigma_3\)

\includegraphics{figure/x10n1_1_1__1c-1.png} \clearpage

\textbf{Case study 3r: heterogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 > \mu_2 > \mu_3\)

\(\sigma_1 > \sigma_2 = \sigma_3\)

\includegraphics{figure/x10n1__1_1_1c-1.png} \clearpage

\textbf{Case study 3s: heterogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 > \mu_2 > \mu_3\)

\(\sigma_1 = \sigma_3 < \sigma_2\)

\includegraphics{figure/x10n1_1__1_1c-1.png} \clearpage

\textbf{Case study 3t: heterogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 > \mu_2 > \mu_3\)

\(\sigma_1 < \sigma_2 < \sigma_3\)

\includegraphics{figure/x10n1_1__1___1c-1.png} \clearpage

\textbf{Case study 3u: heterogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 > \mu_2 > \mu_3\)

\(\sigma_2 < \sigma_1 < \sigma_3\)

\includegraphics{figure/x10n1__1_1___1c-1.png} \clearpage

\textbf{Case study 3v: heterogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 > \mu_2 > \mu_3\)

\(\sigma_1 < \sigma_3 < \sigma_2\)

\includegraphics{figure/x10n1_1___1__1c-1.png} \clearpage

\textbf{Case study 3w: heterogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 > \mu_2 > \mu_3\)

\(\sigma_1 > \sigma_3 > \sigma_2\)

\includegraphics{figure/x10n1__1___1_1c-1.png} \clearpage

\textbf{Case study 3x: heterogenous prior \(\sigma\)'s and heterogenous
prior means}

\(\mu_1 > \mu_2 > \mu_3\)

\(\sigma_1 > \sigma_2 > \sigma_3\)

\includegraphics{figure/x10n1___1__1_1c-1.png} \clearpage

\captionsetup{labelformat=default}


\section{Lower-degree Artifacts in Feistel Networks}
\SecLabel{impmono}

In previous section it was shown that the HDIM is a very convenient tool for attacking affine encodings. Affine encodings have rather low entropy and thus provably absent monomials of degree $2n-1$ in a Feistel Network provide enough equations to recover the encodings. However, much more equations are needed to recover a Feistel function. A straightforward direction is to consider lower-degree monomials as well, possibly at the cost of attacking fewer rounds. This has an extra benefit of finding more efficient integral distinguishers, since the data complexity of an integral distinguisher is exponential in the degree of the corresponding absent monomial in the ANF. In this section I use the generalization of the HDIM-ANF relation described in \SecRef{gen-hdim-anf} in order to prove absence of lower degree monomials in Feistel networks. The method is quite similar to the method used for proving type-I distinguishers in \SecRef{hdim-feistel}. The main idea is to replace the sum variable in the expression to an intermediate state and thus split the structure in two halves.

The monomials in the ANF of Feistel Networks can be classified by the degree on the left input branch and on the right input branch. Clearly, all monomials in the same class have equivalent possibility of appearing in the ANF, since such monomials can be interchanged by composing Feistel functions with bit permutations.

\begin{definition}[($(w_l,w_r)$-monomials]
Let $u_l,u_r \in \field{n}$ and let $u \eqdef (u_l, u_r) \in \field{2n}$.
The monomial $x^u$ is said to be a $(w_l,w_r)$-monomial if $\wt(u_l) = w_l$ and $\wt(u_r) = w_r$. $u$ is then said to be a $(w_l,w_r)$-exponent.
\end{definition}

\begin{theorem}
\Label{thm:low-nbij}
Let $S \in \nbij{r}{d}$ and let $f = \inprod{e_i, S}, n < i \le 2n,$ be any coordinate of the right output branch of $S$.
Let $u$ be a $(w_l,w_r)$-exponent. Then $\coef{u}{f} = 0$ if there exists an integer $r', 0 \le r' < r$ such that
$$
(n - w_l)\cdot\maxdegnbij{r'+1}{d} + (n - w_r)\cdot\maxdegnbij{r'}{d} + \maxdegnbij{r-r'}{d} < 2n.
$$

Similar result applies for $S \in \bij{r}{d}$ by using $\maxdegbij{r}{d}$.
\end{theorem}
\begin{proof}
Let $(a, b) \in \field{2n}$ denote the intermediate state of $S$ after $r'$ rounds. Let $(x_l,x_r) \in \field{2n}$ be the two input branches of $S$ as functions of $(a,b)$; $(y_l,y_r) \in \field{2n}$ be the two output branches of $S$ as functions of $(a,b)$. By Proposition~\Ref{prop:anf-alternative},
$$
\coef{u}{f} = \bigoplus_{z \in \field{2n}}
    (\lnot x_l)^{\lnot u_l}
    (\lnot x_r)^{\lnot u_r}
    \inprod{e_i, y_r},
$$
where $u_l,u_r \in \field{n}$ are the two halves of $u$.
$(x_l,x_r)$ can be computed using an $r'$-round Feistel Network and $(y_l,y_r)$ can be computed using an $(r-r')$-round Feistel Network. Further note that $\wt(\lnot u_l) = n - \wt(u_l)$ and $\wt(\lnot u_r) = n - \wt(u_r)$. The degree bounds follow:
\begin{itemize}
    \item $\deg{(\lnot x_l)^{\lnot u_l} } \le (n - w_l)\maxdegbij{r'+1}{d}$,
    \item $\deg{(\lnot x_r)^{\lnot u_r} } \le (n - w_r)\maxdegbij{r'}{d}$,
    \item $\deg{y_r} \le \maxdegbij{r - r'}{d}$.
\end{itemize}
The theorem follows by summing the degree bounds and comparing to the full degree $2n$.
\end{proof}

A trick for bijective Feistel functions can be applied similarly to Theorem~\Ref{thm:bij}.

\begin{theorem}
\Label{thm:low-bij}
Let $S \in \bij{r}{d}$ and let $f = \inprod{e_i, S}, n < i \le 2n$ be any coordinate of the right output branch of $S$.
Let $u$ be a $(w_l,w_r)$-exponent. Then $\coef{u}{f} = 0$ if there exists an integer $r', 0 \le r' < r - 3$ such that
$$
\max(d,\dinv) \cdot \proundd{
    (n - w_l)\cdot\maxdegbij{r'}{d} +
    (n - w_r)\cdot\maxdegbij{r'-1}{d} +
    \maxdegbij{r-r'-2}{d}
} < 2n.
$$
\end{theorem}
\begin{proof}
The variables chosen are $(a,c)$ instead of $(a,b)$ (see \FigRef{mitmproof}), where $(a,b)$ denotes the intermediate state of $S$ after $r'$ rounds, and $c = f_{r'+1}(b) \oplus a$. In this case $b$ can be expressed as $f_{r'+1}^{-1}(a \oplus c)$, and the degree of $b$ as a function of $(a,c)$ is upper bounded by $\dinv$.

Let $x_l,x_r$ be the two input branches of $S$ as functions of $(a,c)$; $y_l,y_r$ be the two output branches of $S$ as functions of $(a,c)$.
Similarly to previous proofs, the following bounds are derived:
\begin{itemize}
    \item $\deg{x_l} \le \max(d,\dinv)\cdot \maxdegbij{r'}{d}$,
    \item $\deg{x_r} \le \max(d,\dinv)\cdot \maxdegbij{r'-1}{d}$,
    \item $\deg{y_l} \le \max(d,\dinv)\cdot \maxdegbij{r-r'-1}{d}$,
    \item $\deg{y_r} \le \max(d,\dinv)\cdot \maxdegbij{r-r'-2}{d}$.
\end{itemize}
For Proposition~\Ref{prop:anf-alternative}, the following bounds are needed:
\begin{itemize}
    \item $\deg{(\lnot x_l)^{\lnot u_l} } \le (n - w_l)\maxdegbij{r'}{d}$,
    \item $\deg{(\lnot x_r)^{\lnot u_r} } \le (n - w_r)\maxdegbij{r'-1}{d}$,
    \item $\deg{y_r} \le \maxdegbij{r - r' - 2}{d}$.
\end{itemize}
The theorem follows by summing the degree bounds.
\end{proof}

\begin{corollary}
Let $S \in \bij{4}{n-1}$ and let $f = \inprod{e_i, S}, n < i \le 2n$ be any coordinate of the right output branch of $S$. Then the following monomials classes are absent in the ANF of $f$ (in total $2^n+n^2+n$ monomials):
{
\enumroman{}
\begin{enumerate}
    \item $(n-1,n-1)$,
    \item $(n-1,n)$,
    \item $(n,k)$ for any $0 \le k \le n$.
\end{enumerate}
}
\end{corollary}
\begin{proof}
Set $r' = 0$ in Theorem~\Ref{thm:low-bij}. Note that in this extreme case the term $\dinv \cdot (n - w_r)\cdot\maxdegbij{r'}{d}$ for $r'=0$ can be replaced by $(n - w_r)$, since the right input branch clearly has degree 1 on the chosen variables. For case $(i)$ the condition becomes
$$ 1 + (n-1) \pround{
1\cdot\maxdegbij{1}{n-1} +
\maxdegbij{1}{n-1}
} \le 2n-1 < 2n.$$
For case $(ii)$ the condition becomes
$$ 0 + (n-1) \pround{
1\cdot\maxdegbij{1}{n-1} + \maxdegbij{1}{n-1}
} \le 2n-2 < 2n.$$
For case $(iii)$ the condition becomes
$$ (n-k) + (n-1) \pround{
0 + \maxdegbij{1}{n-1}
} \le 2n-1-k < 2n.$$
\end{proof}


\chapter{Proofs and Proof Methods}
Writing proofs deepens your knowledge of the subject at hand. ECS 20 is a proof
heavy course and prepares you to write extensive proofs in upper division CS
and Math classes. Almost 60\% of your exam will be based on proofs and will
test your ability to write a flawless proof. Here are three important proof
methods that this chapter will cover:

\begin{itemize}
    \item Direct Proof
    \item Indirect Proof (Proof by Contraposition)
    \item Proof by Contradiction
    \item Proof by Cases
    \item Constructive and Non-Constructive Proofs
\end{itemize}

\section{Direct Proof}
This is the most basic proof method. Here, we will be given a statement of the
form $ P \rightarrow Q $ and you will be asked to prove this propositional
form. Here is how you could prove something directly:

% https://tex.stackexchange.com/questions/108236/enumerate-list-numbers-with-prefix
% TODO make a macro for steps
\begin{enumerate}[label=\textbf{Step \arabic*},leftmargin=*]
    \item Given $P \rightarrow Q$, assume P is true.
    \item Using $P$, try to use the given information of $P$ to arrive at $Q$.
\end{enumerate}

Here is a simple example:

\begin{theorem}
    If $a$ is an even integer, then $a^{3} - 6a$ is even.
\end{theorem}

\begin{proof}
    Let us use a direct proof.

    Assume $a$ is an even integer.

    Then,
    \[
        \begin{split}
            \exists k \in \mathbb{Z}, a &= 2k \\
            a^{3} - 6a &= (2k)^{3} - 6(2k) \\
            &= 8k^{3} - 12k \\
            &= 2(4k^{3} - 6k) \\
            &= 2m\text{, where } m = 4k^{3} - 6k \\
        \end{split}
    \]
    Thus, $a^3 - 6a$ is even. Therefore, if $a$ is even, then $a^{3} -6a$ is even
\end{proof}

\section{Indirect Proof (Proof by Contraposition)}
Suppose we are given a statement of the form $P \rightarrow Q$. Sometimes, a
direct proof might be very hard (sometimes impossible) to go about. Therefore
we resort to another proof method that simplifies things. In this proof, we
will try to show $\neg Q \rightarrow \neg P$ is true. By drawing out a simple
truth table you can show that $ P \rightarrow Q \Leftrightarrow \neg Q
\rightarrow \neg P$.

Here is how you can prove something by contraposition:

\begin{enumerate}[label=\textbf{Step \arabic*},leftmargin=*]
    \item Given $P \rightarrow Q$, Assume $\neg Q$ is true.
    \item Using $\neg Q$, try to arrive at $\neg P$.
\end{enumerate}

\noindent Here is a simple example:

\begin{theorem}
    If $a^{2}$ is even, then $a$ is even
\end{theorem}

\begin{proof}
    Let us use an indirect proof.

    Assume $a$ is odd (Assume $\neg Q$).

    Then,
    \[
        \begin{split}
            \exists k \in \mathbb{Z}, a &= 2k + 1 \\
            a^{2} &= (2k + 1)^{2} = 4k^2 + 4k + 1 \\
            \Leftrightarrow a^{2} &= 2(2k^{2} + 2k) + 1 \\
            \Leftrightarrow &= 2m + 1 \text{, where } m = 2k^{2} + 2k \\
        \end{split}
    \]

    Therefore, $a^{2}$ is odd.  Thus, we have shown that if $a$ is odd, then
    $a^{2}$ is odd.

    Therefore by contraposition, If $a^{2}$ is even then, $a$ is even.
\end{proof}

\section{Proof By Contradiction}
Suppose we are given asked to prove a given statement, Q, a direct proof might
not be feasible. Hence we resort to our last proof method to prove statements
that might look difficult to prove directly. In this proof method, we assume
$\neg Q$ to be true. Suppose you are given a statement of the form $P
\rightarrow Q$, then you assume $\neg (P \rightarrow Q)$, which is basically $P
\wedge \neg Q$.

Here is a simple example:
\begin{theorem}
    If $(a,b) \in \mathbb{Z}^{2}$, then $a^{2} - 4b \neq 2$
\end{theorem}

\begin{proof}
    Let us use a proof by contradiction. Assume $P \wedge \neg Q$ is true. Then
    $(a,b) \in \mathbb{Z}^{2} \wedge (a^{2} -4b = 2)$ is true.

    \[
        \begin{split}
            \text{If }&a^{2} - 4b = 2\text{, then} \\
            &a^{2} = 2 + 4b \\
            \iff &a^{2}\text{ is even} \\
            \iff &a\text{ is even \footnote{You can prove this by
            contraposition. However, If you're interested in using a direct
            proof to show that $a^{2}$ is even $\rightarrow a$ is even, then
            you'd have to wait till we reach number theory ;)}} \\
            \iff &\exists k \in \mathbb{Z}a = 2k \\
            \iff &(2k)^{2} - 4b = 2 \\
            \iff &4k^{2} - 4b = 2 \\
            \iff &2k^{2} - 2b = 1 \\
            \iff &2 \times (k^{2} - 2b) = 1 \\
        \end{split}
    \]
    $\Leftrightarrow$ An even integer is equal to the odd integer 1, which is a
    contradiction.

    This contradiction arises due to our incorrect assumption that $\neg (P
    \rightarrow Q)$ was true.

    Therefore, $P \rightarrow Q$ is true.

    Thus, If $(a,b) \in \mathbb{Z}^{2}$, then $a^{2} - 4b \neq 2$
\end{proof}

\section{Proof by Cases}
This is a proof method that is always used when you do not know the constraints
to a problem. Let us illustrate this with a very simple example:

\begin{theorem}
    If $a$ is an integer, then $2a + 1$ is odd.
\end{theorem}

\begin{proof}
    Here, we do not know if $a$ is an even or odd integer. Thus, we consider
    both cases and try to prove our hypothesis:

    \begin{caselist}
        \item $a$ is even. \\
            Then, ($\exists k \in \mathbb{Z}$)[$a = 2k$]. \\
            Then, $2a + 1 = 2(2k) + 1$, which is odd!
        \item $a$ is odd. \\
            Then, ($\exists k \in \mathbb{Z}$)[$a = 2k + 1$]. \\
            Then, $2a + 1 = 2(2k + 1) + 1$, which is also odd!
    \end{caselist}
\end{proof}

\section{Constructive and Non-Constructive Proofs}
\subsection{Constructive Proofs}
Constructive proofs are proof methods in which a specific example is provided.
These proofs are generally referred to as "\emph{Existence Proofs}".

Let us take two separate examples to illustrate this proof method.

\begin{example}
    Show that there exist two integers a and b such that $a + b$ and $a - b$
    are both prime numbers.
    \begin{proof}
        We only need to show a single pair $(a,b)$ for which $a + b$ and $a - b$
        are both prime numbers.

        Let us take $a = 5$ and $b = 2$.

        Then, $a + b = 7$ and $a - b = 3$.

        Both 7 and 3 are prime numbers.

        Thus, we found a particular pair of numbers for which the hypothesis holds.
    \end{proof}
\end{example}

\begin{example}
    Prove or disprove the following:
    \begin{center}
        For all $k \in \mathbb{Z}$ greater than 2, $2k + 7$ is a prime number.
    \end{center}
    \begin{proof}
        Let us try out a few numbers and see what we get:

        \begin{trylist}
            \item $k = 2$ \\
                Then, $2k + 7 = (2 \times 2) + 7 = 4 + 7 = 11$ which is a prime
                number.
            \item \textbf{$k = 3$} \\
                Then, $2k + 7 = (2 \times 3) + 7 = 6 + 7 = 13$ which is a prime number.
            \item $k = 4$ \\
                Then, $2k + 7 = (2 \times 4) + 7 = 8 + 7 = 15$ \textbf{which is NOT
                prime}.
        \end{trylist}

        \noindent Thus, we have found one example which violates the hypothesis.

        \noindent Thus, the given statement is \textbf{FALSE}!
    \end{proof}
\end{example}

\subsection{Non-Constructive Proofs}
This is another kind of an existence proof. In this method, we do not
explicitly find a value of 'x' such that $P(x)$ is true. Instead, we show that
there must exist some x for which $P(x)$ is true. This might seem like a
confusing definition, so let us use this in an example:

\begin{question}
    Show that there exists a pair of irrational numbers a and b such that $c =
    a^{b}$ is rational.
\end{question}

\begin{remark}
    This is a very famous and interesting problem in the field of proofs and
    discrete mathematics. Let us look at the solution:
\end{remark}

\begin{proof}
    We know that $\sqrt 2$ is irrational. Let us define $c = (\sqrt 2)^{\sqrt
    2}$.

    Additionally, let us define $d = c ^{\sqrt2}$.

    Then, $d = (\sqrt 2) ^{\sqrt 2 \times \sqrt 2} = (\sqrt 2) ^{2} = 2$ which
    is rational.
\end{proof}

Note that we did not explicitly find the values of a and b in the above
example. We simply showed that there could exist a pair of irrational numbers
in which $a^{b}$ could be rational.

\textbf{\emph{Trivia: }} Did you know that there is a constructive
proof for the above mentioned example? It is quite hard to find the right
examples for a and b, but it is possible. I haven't included the answers, but
do think about it!


\BoSSSopen{tutorial11-Stokes/StokesEq}
\graphicspath{{tutorial11-Stokes/StokesEq.texbatch/}}

\BoSSScmd{
/// \label{chap:stokesEquation}
/// \section*{What's new?}
/// \label{sec:stokesEquation_new}
/// \begin{itemize}
///     \item {implementation of the incompressible, steady Stokes equation}
///     \item advanced: {implementation of the Stokes flow behind a grid as an application}
/// \end{itemize}
///
///\section*{Prerequisites}
///\label{sec:stokesEquation_prereq}
///\begin{itemize}
/// \item implementation of numerical fluxes, chapter \ref{NumFlux}
/// \item spatial operator, chapter \ref{SpatialOperator}
/// \item implementation of the gradient operator, chapter \ref{sec:PoissonAsASystem}
/// \item implementation of the SIP operator, chapter \ref{sec:SIP}
///\end{itemize}
    /// %==========================================
    /// \section{Problem statement}
    /// %==========================================
/// The Stokes-equation is given as
/// \begin{alignat*}{3}
/// -\frac{1}{\reynolds} \Delta \vec{u}
///     & + \nabla \psi
///        & \ & = \vec{g}_\domain
/// \\
/// \operatorname{div} (\vec{u})
///     &
///        & \ & = 0
/// \end{alignat*}
/// We consider two types of boundary conditions for the Stokes equation,
/// Dirichlet (on $\Gamma_D$) and Neumman (on $\Gamma_N$). Those are defined as
/// \begin{alignat*}{3}
///         \vec{u} & =\vec{u}_D
///         & \ & \text{ on } \Gamma_D\ \text{ (Dirichlet)}, \\
///         %
///         \left( -\frac{1}{\reynolds}\ \nabla  \vec{u} +  \one_p \psi \right)
///                \vec{n}_{\partial \domain}        & = 0
///         & \ & \text{ on } \Gamma_N\ \text{ (Neumann) } .
/// \end{alignat*}
/// %==========================================
/// \section{Solution within the BoSSS framework}
/// %==========================================
 }
\BoSSSexeSilent
\BoSSScmd{
restart
 }
\BoSSSexeSilent
\BoSSScmd{
using ilPSP.LinSolvers;\newline
using BoSSS.Solution.Tecplot;\newline
using ilPSP.Connectors.Matlab;
 }
\BoSSSexe
\BoSSScmdSilent{
/// BoSSScmdSilent BoSSSexeSilent
using NUnit.Framework;
 }
\BoSSSexeSilent
\BoSSScmd{
/// To implement at which point of the boundary which condition is valid, we
/// define a global function
Func<double[],bool> IsDirichletBndy = null;\newline
/// which defines a mapping
/// \[
///    \vec{x} \mapsto \{ \code{true}, \code{false} \},
/// \]
/// where \code{true} actually indicates a Dirichlet boundary.
/// Since this function is defined as a global delegate, it can be altered
/// later on. In the same manner, the function
Func<double[],double[]> UDiri = null;\newline
/// defines the Dirichlet-value for the velocity at the boundary.
 }
\BoSSSexe
\BoSSScmd{
/// %==========================================
/// \subsection{Velocity divergence and pressure gradient}
/// %==========================================
 }
\BoSSSexe
\BoSSScmd{
/// At first, we implement the velocity divergence, i.e.
/// the continuity equation. We use the strong form, i.e.
/// \[
///    b(\vec{u},v) =
///    \oint_{\Gamma \backslash \Gamma_D}
///           \mean{v} \jump{\vec{u}} \cdot \vec{n}_\Gamma
///    \dA
///    -
///    \int_{\domain} \operatorname{div}(\vec{u}) v \dV.
/// \]
public class Divergence : \newline
\btab \btab BoSSS.Foundation.IEdgeForm, // edge integrals\newline
\btab \btab BoSSS.Foundation.IVolumeForm     // volume integrals\newline
\{\newline
    /// The parameter list for the divergence is empty:
\btab public IList<string> ParameterOrdering \{ \newline
\btab \btab get \{ return null; \} \newline
\btab \}\newline
 \newline
    /// We have a vector argument variable,
    /// the velocity $ [ u, v ] = \vec{u} $
    /// (our trial function):
\btab public IList<String> ArgumentOrdering \{ \newline
\btab \btab get \{ return new string[] \{ "u", "v" \}; \} \newline
\btab \}\newline
 \newline
\btab public TermActivationFlags VolTerms \{\newline
\btab \btab get \{\newline
\btab \btab \btab return TermActivationFlags.AllOn;\newline
\btab \btab \}\newline
\btab \}\newline
 \newline
\btab public TermActivationFlags InnerEdgeTerms \{\newline
\btab \btab get \{\newline
\btab \btab \btab return TermActivationFlags.AllOn; \newline
\btab \btab \}\newline
\btab \}\newline
 \newline
\btab public TermActivationFlags BoundaryEdgeTerms \{\newline
\btab    get \{\newline
\btab \btab    return TermActivationFlags.AllOn;\newline
\btab \btab \}\newline
\btab \}\newline
 \newline
    /// In the volume part, the integrand is $\operatorname{div}(\vec{u}) v$:
\btab public double VolumeForm(ref CommonParamsVol cpv, \newline
\btab \btab double[] U, double[,] GradU, \newline
\btab \btab double V, double[] GradV) \{\newline
\btab \btab double Acc = 0;\newline
\btab \btab for(int d = 0; d < cpv.D; d++) \{\newline
\btab \btab \btab Acc -= GradU[d,d]*V;\newline
\btab \btab \}\newline
\btab \btab return Acc;\newline
\btab \}\newline
 \newline
    /// On interior cell boundaries, we use a \emph{velocity penalty},
    /// $\mean{v} \jump{\vec{u}} \cdot \vec{n}_\Gamma$:
\btab public double InnerEdgeForm(ref CommonParams inp, \newline
\btab \btab double[] U\_IN, double[] U\_OT, double[,] GradU\_IN, double[,] GradU\_OT, \newline
\btab \btab double V\_IN, double V\_OT, double[] GradV\_IN, double[] GradV\_OT) \{\newline
 \newline
\btab \btab double Acc = 0;\newline
\btab \btab for(int d = 0; d < inp.D; d++) \{\newline
\btab \btab \btab Acc += 0.5*(V\_IN + V\_OT)*(U\_IN[d] - U\_OT[d])*inp.Normale[d];\newline
\btab \btab \}\newline
\btab \btab return Acc;\newline
\btab \}\newline
 \newline
    /// On the domain boundary, we have to distinguish between
    /// Dirichlet- and Neumann-boundary conditions; the function
    /// \code{uDiri} defines which of the two actually applies:
\btab public double BoundaryEdgeForm(ref CommonParamsBnd inp, \newline
\btab \btab double[] U\_IN, double[,] GradU\_IN, double V\_IN, double[] GradV\_OT) \{\newline
 \newline
\btab \btab double Acc = 0;\newline
 \newline
\btab \btab if(!IsDirichletBndy(inp.X)) \{\newline
\btab \btab \btab // On the Neumann boundary, we do not know an outer value for the\newline
\btab \btab \btab // velocity, so there is no penalization at all:\newline
\btab \btab \btab Acc = 0;    \newline
\btab \btab \} else \{\newline
\btab \btab \btab // On the Dirichlet boundary, the outer value for the velocity\newline
\btab \btab \btab // is given by the function/delegate 'UDiri':\newline
\btab \btab \btab double[] UD = UDiri(inp.X);\newline
\btab \btab \btab for(int d = 0; d < inp.D; d++) \{\newline
\btab \btab \btab \btab Acc += (U\_IN[d] - UD[d])*inp.Normale[d]*V\_IN;\newline
\btab \btab \btab \}\newline
\btab \btab \}\newline
\btab \btab return Acc;\newline
\btab \}\newline
\}
 }
\BoSSSexe
\BoSSScmd{
/// %==================================
/// \subsection{The gradient-operator}
/// %==================================
/// We use the variational formulation of the gradient operator, as it is explained in section \ref{subsec:poissonSystem_gradientOp}.
class Gradient\_d :\newline
\btab \btab BoSSS.Foundation.IEdgeForm, // edge integrals\newline
\btab \btab BoSSS.Foundation.IVolumeForm     // volume integrals\newline
\{\newline
\btab public Gradient\_d(int \_d) \{\newline
\btab \btab this.d = \_d;\newline
\btab \}\newline
 \newline
    /// The component index of the gradient:
\btab int d;\newline
 \newline
    /// As usual, we do not use parameters:
\btab public IList<string> ParameterOrdering \{ \newline
\btab \btab get \{ return null; \} \newline
\btab \}\newline
 \newline
    /// We have one argument, the pressure $\psi$:
\btab public IList<String> ArgumentOrdering \{ \newline
\btab \btab get \{ return new string[] \{ "psi" \}; \} \newline
\btab \}\newline
 \newline
\btab public TermActivationFlags VolTerms \{\newline
\btab \btab get \{ return TermActivationFlags.AllOn; \}\newline
\btab \}\newline
 \newline
\btab public TermActivationFlags InnerEdgeTerms \{\newline
\btab \btab get \{ return (TermActivationFlags.AllOn); \}\newline
\btab \}\newline
 \newline
\btab public TermActivationFlags BoundaryEdgeTerms \{\newline
\btab    get \{ return TermActivationFlags.AllOn; \}\newline
\btab \}\newline
 \newline
    /// The volume integrand, for a vector-valued test-function $\vec{v}$
    /// would be $-\operatorname{div}{\vec{v}} \psi$. Our test function $v$
    /// is scalar-valued, so e.g. for $\code{d} = 0$ we have
    /// $\vec{v} = (v,0)$. In this case, our volume integrand reduces as
    /// $-\operatorname{div}{\vec{v}} \psi = -\partial_x v \psi$:
\btab public double VolumeForm(ref CommonParamsVol cpv, \newline
\btab \btab    double[] Psi, double[,] GradPsi, \newline
\btab \btab    double V, double[] GradV) \{\newline
 \newline
\btab \btab double Acc = 0;\newline
\btab \btab Acc -= Psi[0]*GradV[d];\newline
\btab \btab return Acc;\newline
\btab \}        \newline
 \newline
    /// On interior cell edges, we simply use a central-difference flux.
    /// Again, we consider a scalar test function, so we have
    /// $ \jump{\psi} \vec{v} \cdot \vec{n} = \jump{\psi} v n_d $,
    /// where $n_d$ is the $d$--th component of $\vec{n}$:
\btab public double InnerEdgeForm(ref CommonParams inp, \newline
\btab \btab double[] Psi\_IN, double[] Psi\_OT, \newline
\btab \btab double[,] GradPsi\_IN, double[,] GradPsi\_OT, \newline
\btab \btab double V\_IN, double V\_OT, double[] GradV\_IN, double[] GradV\_OT) \{\newline
 \newline
\btab \btab double Acc = 0;\newline
\btab \btab Acc += 0.5*(Psi\_IN[0] + Psi\_OT[0])*inp.Normale[this.d]*(V\_IN - V\_OT);\newline
\btab \btab return Acc;  \newline
\btab  \}\newline
 \newline
\btab public double BoundaryEdgeForm(ref CommonParamsBnd inp, \newline
\btab \btab double[] Psi\_IN, double[,] GradPsi\_IN, double V\_IN, double[] GradV\_OT) \{\newline
 \newline
\btab \btab double Acc = 0;\newline
\btab \btab if(!IsDirichletBndy(inp.X)) \{\newline
\btab \btab \btab // On the Neumann boundary, we want the total stress to be zero,\newline
\btab \btab \btab // so there is no contribution from the pressure:\newline
\btab \btab \btab Acc = 0;\newline
\btab \btab \} else \{\newline
\btab \btab \btab // On the Dirichlet boundary, we do not know an outer value for \newline
\btab \btab \btab // the pressure, so we have to take the inner value:\newline
\btab \btab \btab Acc += Psi\_IN[0]*inp.Normale[this.d]*V\_IN;\newline
\btab \btab \}\newline
\btab \btab return Acc;              \newline
\btab \}\newline
\}
 }
\BoSSSexe
\BoSSScmd{
/// %==============================================================
/// \subsection{Tests on pressure gradient and velocity divergence}
/// %==============================================================
 }
\BoSSSexe
\BoSSScmd{
/// If our implementation is correct, we created a discretization of
/// \[
///  \left[ \begin{array}{cc}
///     0                 & \nabla \\
///   -\operatorname{div} & 0      \\
///  \end{array} \right]
/// \]
/// so the matrix should have the form
/// \[
///  \left[ \begin{array}{cc}
///     0     & B      \\
///     B^T   & 0      \\
///  \end{array} \right]
///  =: M,
/// \]
/// i.e. $M$ should be symmetric.
/// We are testing this using a channel flow configuration:
/// \begin{align*}
///     \Omega    := & (0,10) \times (-1,1) , \\
///     \Gamma_N  := & \{ (x,y) | \ x = 10 \} , \\
///     \Gamma_D  := & \partial \Omega \setminus \Gamma_D , \\
///     \vec{u}_D := & (1 - y^2, 0),
/// \end{align*}
/// using an equidistant grid.
 }
\BoSSSexe
\BoSSScmd{
/// We create a grid, a DG basis for velocity and pressure
/// and a variable mapping:
var xNodesChannel = GenericBlas.Linspace(0,10,31);// 30 cells in x-direction\newline
var yNodesChannel = GenericBlas.Linspace(-1,1,7); // 6 cells in y-direction\newline
var grdChannel    = Grid2D.Cartesian2DGrid(xNodesChannel,yNodesChannel);\newline
var grdDatChannel = new GridData(grdChannel);\newline
var VelBChannel   = new Basis(grdDatChannel, 2);  // velocity basis\newline
var PsiBChannel   = new Basis(grdDatChannel, 1);  // pressure basis \newline
var varMapChannel = new UnsetteledCoordinateMapping(\newline
\btab \btab \btab \btab \btab    VelBChannel,VelBChannel,PsiBChannel); // variable mapping
 }
\BoSSSexe
\BoSSScmd{
/// We specify the boundary conditions as delegates:
Func<double[],bool> IsDirichletBndy\_Channel \newline
\btab \btab = (X => Math.Abs(X[0] - 10) > 1.0e-10); // its Dirichlet, if x != 10\newline
Func<double[],double[]> UDiri\_Channel \newline
\btab \btab = (X => new double[2] \{ 1.0 - X[1]*X[1], 0\});
 }
\BoSSSexe
\BoSSScmd{
/// Let's create the operator which contains only the pressure gradient
/// and velocity divergence:
SpatialOperator GradDiv = new SpatialOperator(3,3, // 3 vars. in dom. & codom.\newline
\btab \btab \btab \btab \btab \btab    QuadOrderFunc.Linear(), // linear operator\newline
\btab \btab \btab \btab \btab \btab    "u", "v", "psi",  // names of domain variables\newline
\btab \btab \btab \btab \btab \btab    "mom\_x", "mom\_y", "conti"); // names of codom. vars\newline
GradDiv.EquationComponents["mom\_x"].Add(new Gradient\_d(0)); \newline
GradDiv.EquationComponents["mom\_y"].Add(new Gradient\_d(1)); \newline
GradDiv.EquationComponents["conti"].Add(new Divergence());\newline
GradDiv.Commit();
 }
\BoSSSexe
\BoSSScmd{
/// We create the matrix of the \code{GradDiv}-operator for
/// the channel configuration. Before that, we have to set values for the
/// global \code{IsDirichletBndy} and \code{UDiri}-variables.
IsDirichletBndy           = IsDirichletBndy\_Channel;\newline
UDiri                     = UDiri\_Channel;\newline
var GradDivMatrix\_Channel = GradDiv.ComputeMatrix(varMapChannel,\newline
\btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab   null,\newline
\btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab   varMapChannel);
 }
\BoSSSexe
\BoSSScmd{
///Finally, we can test the symmetry of the matrix:
var ErrMtx = GradDivMatrix\_Channel - GradDivMatrix\_Channel.Transpose();\newline
ErrMtx.InfNorm();
 }
\BoSSSexe
\BoSSScmdSilent{
/// NUnit test (few random tests) BoSSScmdSilent
Assert.LessOrEqual(ErrMtx.InfNorm(), 1e-12);
 }
\BoSSSexe
\BoSSScmd{
/// %====================================================================
/// \subsection{Adding the viscous operator, forming the Stokes operator}
/// %====================================================================
 }
\BoSSSexe
\BoSSScmd{
/// We use the SIP-operator from chapter \ref{sec:SIP} to model the viscous terms:
public class Viscous : \newline
\btab \btab IEdgeForm, // edge integrals\newline
\btab \btab IVolumeForm     // volume integrals\newline
\{\newline
    /// The velocity component:
\btab int d;\newline
 \newline
\btab public Viscous(int \_d) \{\newline
\btab \btab this.d = \_d;    \newline
\btab \}\newline
 \newline
    /// We implement Reynolds number and the polynomial degree,
    /// as well as the cell-wise length scales (required for
    /// the computation of the penalty factor) as global, static variables.
\btab public static double Re;\newline
\btab public static int PolynomialDegree;\newline
\btab public static MultidimensionalArray cj;\newline
 \newline
    /// We do not use parameters:
\btab public IList<string> ParameterOrdering \{ \newline
\btab \btab get \{ return new string[0]; \} \newline
\btab \}\newline
 \newline
    /// Depending on \code{d}, the argument variable
    /// should be either $u$ or $v$:
\btab public IList<String> ArgumentOrdering \{ \newline
\btab \btab get \{ \newline
\btab \btab \btab switch(d) \{\newline
\btab \btab \btab \btab case 0  : return new string[] \{ "u" \}; \newline
\btab \btab \btab \btab case 1  : return new string[] \{ "v" \}; \newline
\btab \btab \btab \btab default : throw new Exception();\newline
\btab \btab \btab \}\newline
\btab \btab \} \newline
\btab \}\newline
 \newline
    /// The \code{TermActivationFlags}, as usual:
\btab public TermActivationFlags VolTerms \{\newline
\btab \btab get \{\newline
\btab \btab \btab return TermActivationFlags.GradUxGradV;\newline
\btab \btab \}\newline
\btab \}\newline
 \newline
\btab public TermActivationFlags InnerEdgeTerms \{\newline
\btab \btab get \{\newline
\btab \btab \btab return (TermActivationFlags.AllOn);\newline
\btab \btab \}\newline
\btab \}\newline
 \newline
\btab public TermActivationFlags BoundaryEdgeTerms \{\newline
\btab    get \{\newline
\btab \btab    return TermActivationFlags.AllOn;\newline
\btab \btab \}\newline
\btab \}\newline
 \newline
    /// The integrand for the volume integral:
\btab public double VolumeForm(ref CommonParamsVol cpv, \newline
\btab \btab    double[] U, double[,] GradU,\newline
\btab \btab    double V, double[] GradV) \{               \newline
\btab \btab double acc = 0;\newline
\btab \btab for(int d = 0; d < cpv.D; d++)\newline
\btab \btab \btab acc += GradU[0, d] * GradV[d];\newline
\btab \btab return (1/Re)*acc;\newline
\btab \}\newline
 \newline
 \newline
    /// The integrand for the integral on the inner edges:
\btab public double InnerEdgeForm(ref CommonParams inp, \newline
\btab \btab double[] U\_IN, double[] U\_OT, double[,] GradU\_IN, double[,] GradU\_OT, \newline
\btab \btab double V\_IN, double V\_OT, double[] GradV\_IN, double[] GradV\_OT) \{\newline
 \newline
\btab \btab double eta = PenaltyFactor(inp.jCellIn, inp.jCellOut);\newline
 \newline
\btab \btab double Acc = 0.0;\newline
\btab \btab for(int d = 0; d < inp.D; d++) \{ // loop over vector components \newline
\btab \btab \btab // consistency term: -(\{\{ \textbackslash /u \}\} [[ v ]])*Normale\newline
\btab \btab \btab // index d: spatial direction\newline
\btab \btab \btab Acc -= 0.5 * (GradU\_IN[0, d] + GradU\_OT[0, d])*(V\_IN - V\_OT)\newline
\btab \btab \btab \btab \btab    * inp.Normale[d];\newline
 \newline
\btab \btab \btab // the symmetry term -(\{\{ \textbackslash /v \}\} [[ u ]])*Normale\newline
\btab \btab \btab Acc -= 0.5 * (GradV\_IN[d] + GradV\_OT[d])*(U\_IN[0] - U\_OT[0])\newline
\btab \btab \btab \btab \btab    * inp.Normale[d];;\newline
\btab \btab \}\newline
 \newline
\btab \btab // the penalty term eta*[[u]]*[[v]]\newline
\btab \btab Acc += eta*(U\_IN[0] - U\_OT[0])*(V\_IN - V\_OT);\newline
\btab \btab return (1/Re)*Acc;\newline
 \newline
\btab \}\newline
 \newline
    /// The integrand on boundary edges, i.e. on $\partial \Omega$:
\btab public double BoundaryEdgeForm(ref CommonParamsBnd inp, \newline
\btab \btab double[] U\_IN, double[,] GradU\_IN, double V\_IN, double[] GradV\_IN) \{\newline
 \newline
 \newline
\btab \btab double Acc = 0.0;\newline
 \newline
\btab \btab if(!IsDirichletBndy(inp.X)) \{\newline
\btab \btab \btab // Neumann boundary conditions, i.e. zero-stress:\newline
\btab \btab \btab Acc = 0;\newline
\btab \btab \} else \{\newline
\btab \btab \btab // Dirichlet boundary conditions\newline
\btab \btab \btab double uBnd = UDiri(inp.X)[d];\newline
 \newline
\btab \btab \btab for(int d = 0; d < inp.D; d++) \{ // loop over vector components \newline
\btab \btab \btab \btab // consistency term:\newline
\btab \btab \btab \btab Acc -= (GradU\_IN[0, d])*(V\_IN) * inp.Normale[d];\newline
\btab \btab \btab \btab // symmetry term:\newline
\btab \btab \btab \btab Acc -= (GradV\_IN[d])*(U\_IN[0]- uBnd) * inp.Normale[d];\newline
\btab \btab \btab \}\newline
 \newline
\btab \btab \btab // penalty term\newline
\btab \btab \btab double eta = PenaltyFactor(inp.jCellIn, -1);\newline
\btab \btab \btab Acc += eta*(U\_IN[0] - uBnd)*(V\_IN);\newline
\btab \btab \}\newline
 \newline
\btab \btab return (1/Re)*Acc;\newline
\btab \}\newline
 \newline
\btab double PenaltyFactor(int jCellIn, int jCellOut) \{\newline
\btab \btab double PenaltySafety = 2;\newline
\btab \btab double cj\_in         = cj[jCellIn];\newline
\btab \btab double penalty\_base  = PenaltySafety*PolynomialDegree*PolynomialDegree;\newline
\btab \btab double eta           = penalty\_base * cj\_in;\newline
\btab \btab if(jCellOut >= 0) \{\newline
\btab \btab \btab double cj\_out = cj[jCellOut];\newline
\btab \btab \btab eta           = Math.Max(eta, penalty\_base * cj\_out);\newline
\btab \btab \}\newline
\btab \btab return eta;\newline
\btab \}\newline
\}
 }
\BoSSSexe
\BoSSScmd{
/// Finally, we are ready to implement the Stokes operator:
SpatialOperator Stokes = new SpatialOperator(3,3, // 3 vars. in dom. & codom.\newline
\btab \btab \btab \btab \btab \btab  QuadOrderFunc.Linear(), // linear operator\newline
\btab \btab \btab \btab \btab \btab  "u", "v", "psi",  // names of domain variables\newline
\btab \btab \btab \btab \btab \btab  "mom\_x", "mom\_y", "conti"); // names of codom. vars\newline
Stokes.EquationComponents["mom\_x"].Add(new Gradient\_d(0)); \newline
Stokes.EquationComponents["mom\_x"].Add(new Viscous(0)); \newline
Stokes.EquationComponents["mom\_y"].Add(new Gradient\_d(1)); \newline
Stokes.EquationComponents["mom\_y"].Add(new Viscous(1));\newline
Stokes.EquationComponents["conti"].Add(new Divergence());\newline
Stokes.Commit();
 }
\BoSSSexe
\BoSSScmd{
/// Again, we create the matrix (now, for the Stokes operator) and check its
/// symmetry; we also have to set the Reynolds number and the polynomial
/// degree \emph{before} calling \code{ComputeMatrix} (since we are doing a
/// rather dirty trick by using global variables).
IsDirichletBndy          = IsDirichletBndy\_Channel;\newline
UDiri                    = UDiri\_Channel;\newline
Viscous.Re               = 20.0;\newline
Viscous.PolynomialDegree = VelBChannel.Degree;\newline
Viscous.cj               = grdDatChannel.Cells.cj;\newline
var StokesMatrix\_Channel = Stokes.ComputeMatrix(varMapChannel,\newline
\btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab null,\newline
\btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab varMapChannel);
 }
\BoSSSexe
\BoSSScmd{
/// Testing the symmetry:
var ErrMtx1 = StokesMatrix\_Channel - StokesMatrix\_Channel.Transpose();\newline
ErrMtx1.InfNorm();
 }
\BoSSSexe
\BoSSScmdSilent{
/// NUnit test (few random tests) BoSSScmdSilent
Assert.LessOrEqual(ErrMtx1.InfNorm(), 1e-12);
 }
\BoSSSexe
\BoSSScmd{
/// We also verify that our Stokes-matrix has full rank, i.e. we show that
/// matrix size and rank are equal:
 }
\BoSSSexe
\BoSSScmd{
StokesMatrix\_Channel.NoOfRows;
 }
\BoSSSexe
\BoSSScmd{
StokesMatrix\_Channel.rank();
 }
\BoSSSexe
\BoSSScmdSilent{
/// NUnit test (few random tests) BoSSScmdSilent
Assert.AreEqual(StokesMatrix\_Channel.rank(), StokesMatrix\_Channel.NoOfRows);
 }
\BoSSSexe
\BoSSScmd{
/// %======================================================
/// \subsection{Solving the Stokes equation in the channel}
/// %======================================================
 }
\BoSSSexe
\BoSSScmd{
/// We set the parameters and see whether we actually obtain the correct
/// solution; the exact solution of our problem is obviously
/// \begin{align*}
///     \vec{u}_{\text{ex}} = & (1 - y^2, 0 ), \\
///     \psi_{\text{ex}}    = & \frac{200}{\text{Re}} - x \frac{2}{\text{Re}} \\
/// \end{align*}
/// and since it is polynomial we should be able to obtain it
/// \emph{exactly} in our velocity-pressure-space of degrees $(2,1)$.
IsDirichletBndy          = IsDirichletBndy\_Channel;\newline
UDiri                    = UDiri\_Channel;\newline
Viscous.Re               = 20.0;\newline
Viscous.PolynomialDegree = VelBChannel.Degree;\newline
var StokesMatrix\_Channel = Stokes.ComputeMatrix(varMapChannel,\newline
\btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab null,\newline
\btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab varMapChannel);\newline
var StokesAffine\_Channel = Stokes.ComputeAffine(varMapChannel,\newline
\btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab null,\newline
\btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab varMapChannel);
 }
\BoSSSexe
\BoSSScmd{
/// Now, we are ready to solve the stokes equation. \BoSSS\ provides us with
/// a system
/// \[
///   \code{StokesMatrix\_Channel} \cdot (u,v,\psi)
///   + \code{StokesAffine\_Channel} = 0,
/// \]
/// so we have to multiply \code{StokesAffine\_Channel} with $-1$ to get a
/// right-hand-side.
double[] RHS = StokesAffine\_Channel.CloneAs();\newline
RHS.ScaleV(-1.0);
 }
\BoSSSexe
\BoSSScmd{
/// In order to store our solution, we have to create DG fields:
SinglePhaseField u               = new SinglePhaseField(VelBChannel,"u");\newline
SinglePhaseField v               = new SinglePhaseField(VelBChannel,"v");\newline
SinglePhaseField psi             = new SinglePhaseField(PsiBChannel,"psi");\newline
CoordinateVector SolutionChannel = new CoordinateVector(u,v,psi);
 }
\BoSSSexe
\BoSSScmd{
/// Solve the linear system using a direct method:
StokesMatrix\_Channel.Solve\_Direct(SolutionChannel, RHS);
 }
\BoSSSexe
\BoSSScmd{
/// We export the solution to a Tecplot file:
Tecplot("Channel", 0.0, 3, u, v, psi);
 }
\BoSSSexe
\BoSSScmd{
/// \emph{TODO}: Use Visit (or any other visualization software)
/// to inspect the solution!
 }
\BoSSSexe
\BoSSScmd{
/// %=====================================
/// \section{Advanced topics}
/// %=====================================
 }
\BoSSSexe
\BoSSScmd{
/// %=====================================
/// \subsection{Stokes flow behind a grid}
/// %=====================================
 }
\BoSSSexe
\BoSSScmd{
/// We use the following setting:
/// \begin{align*}
///     \Omega    := & (0,5) \times (-2,2) \\
///     \Gamma_N  := & \{ (x,y) | \ x = 5 \} , \\
///     \Gamma_D  := & \partial \Omega \setminus \Gamma_D , \\
///     \vec{u}_D := & (1 - (2 (y - \operatorname{floor}(y)) - 1)^2, 0), \\
/// \end{align*}
 }
\BoSSSexe
\BoSSScmd{
/// So, the boundary functions are:
Func<double[],bool> IsDirichletBndy\_GridFlow \newline
\btab \btab = (X => Math.Abs(X[0] - 5) > 1.0e-10); \newline
Func<double[],double[]> UDiri\_GridFlow \newline
\btab \btab = (X => new double[2] \{ 1.0 - (2*(X[1] - Math.Floor(X[1])) - 1).Pow2(),\newline
\btab \btab \btab \btab \btab \btab \btab \btab 0\});
 }
\BoSSSexe
\BoSSScmd{
/// \emph{TODO}: the rest is for you! One hint: in $y$-direction, use some
/// spacing so that you have cell boundaries at (least at) $y = -1, 0, 1$.
 }
\BoSSSexe