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

\markright{tftb\_window}
\section*{\hspace*{-1.6cm} tftb\_window}

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

{\bf \large \sf Purpose}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
Window generation.
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Synopsis}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
h = tftb\_window(N)
h = tftb\_window(N,name)
h = tftb\_window(N,name,param)
h = tftb\_window(N,name,param,param2)
\end{verbatim}
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Description}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
        {\ty tftb\_window} yields a window of length {\ty N} with a given shape.\\

\hspace*{-.5cm}\begin{tabular*}{14cm}{p{1.5cm} p{8.5cm} c}
Name & Description & Default value\\
\hline
        {\ty N}      & length of the window\\
        {\ty name}   & name of the window shape & {\ty 'Hamming'}\\
        {\ty param}  & optional parameter\\
        {\ty param2} & second optional parameter\\
 \hline {\ty h}      & output window\\
\hline
\end{tabular*}
\vspace*{.5cm}

       Possible names are :\\
        {\ty 'Hamming', 'Hanning', 'Nuttall', 'Papoulis', 'Harris',
        'Rect', 'Triang', 'Bartlett', 'BartHann', 'Blackman',
        'Gauss', 'Parzen', 'Kaiser', 'Dolph', 'Hanna', 'Nutbess', 'spline'}\\

        For the gaussian window, an optional parameter {\ty k}
        sets the value at both extremities. The default value is {\ty 0.005}.\\

        For the Kaiser-Bessel window, an optional parameter
        sets the scale. The default value is {\ty 3*pi}.\\

        For the Spline windows, {\ty h=tftb\_window(N,'spline',nfreq,p)}
        yields a spline weighting function of order {\ty p} and frequency
        bandwidth proportional to {\ty nfreq}.

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

{\bf \large \sf Example}
\begin{verbatim}
         h=tftb\_window(256,'Gauss',0.005);
         plot(h);
\end{verbatim}

%\newpage

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

{\bf \large \sf Reference}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
[1] F. Harris ``On the Use of Windows for Harmonic Analysis with the
Discrete Fourier Transform'', Proceedings of the IEEE, Vol. 66, pp. 51-83,
1978.\\

[2] A.H. Nuttal, "A Two-Parameter Class of Bessel Weighting
	Functions for Spectral Analysis or Array Processing",
	IEEE Trans on ASSP, Vol 31, pp 1309-1311, Oct 1983.\\

[3] Y. Ho Ha, J.A. Pearce, "A New Window and Comparison to
	Standard Windows", Trans IEEE ASSP, Vol 37, No 2,
	pp 298-300, February 1989.\\

[4] C.S. Burrus, ``Multiband Least Squares FIR Filter Design'',
	Trans IEEE SP, Vol 43, No 2, pp 412-421, February 1995.
\end{minipage}


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

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

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

\setlength{\parindent}{0mm}
\setlength{\parskip}{1.0\baselineskip}

\usepackage{listings}
\usepackage{color}
\usepackage{pdfpages}

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

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

\begin{document}

\title{Becs-114.1100 Computational Science -- exercise round 7} % Replace the exercise round number
\author{Kunal Ghosh, 546247} % Replace with your name and student number
\maketitle
\section{Solution to Question 3}
\subsection{Determining a polynomial regressor to fit a given dataset}\label{prob3a}
In this exercise we are required to fit a polynomial regressor to a given dataset. The dataset can be found in Table 1.
\begin{table}[ht]
\centering
\label{initial-label}
\begin{tabular}{|c|c|}
\hline
 \textbf{x}&\textbf{y} \\ \hline
-9.7 & 3.76 \\
-7.3 & 1.78 \\
-5.4 & 1.52 \\
-5.0 & 1.31 \\
-3.01 & 0.31 \\
-2.13 & 0.23 \\
-1.2 & 0.45 \\
-0.56 & 0.29 \\
0.0 & 0.0 \\
1.2 & 0.45 \\
4.5 & 0.28 \\
6.7 & 2.12 \\
9.9 & 3.91 \\
10.0 & 3.47 \\
12.3 & 5.59 \\
\hline
\end{tabular}
\caption{Tabulated dataset that needs to be fit with a polynomial}
\end{table}

 The general idea here is that we try to fit polynomials of increasing degrees and when the variance of the errors of two successive polynomials become almost same ( difference less than 0.1 ) then we stop. For the given dataset, our algorithm determines that using a polynomial of degree n=2 results in the least variance with respect to the given data, as can be from Table 2.

We calculate the polynomials $q_n(x)$ of successively higher degrees (n) using the orthogonal polynomial generation scheme as shown below.
\begin{equation}
$q_{n+1}(x) = xq_{n}(x) - \alpha_n q_{n}(x) - \beta_n q_{n-1}(x)$
\end{equation}
here the base conditions are
$q_{0}(x) =  1$ and $q_{1}(x) = x - \alpha_{0}$

where $\alpha_{n} = \frac{<xq_{n},q_{n}>}{<q_{n},q_{n}>}$ and $\beta_{n} = \frac{<xq_{n},q_{n-1}>}{<q_{n-1},q_{n-1}>}$

once we have determined the degree n of the polynomial we want to fit. The polynomial equation is given by:

\begin{equation}
 $p_{n}(x) = \sum_{i=0}^{n}c_{i}q_{i}$}
\end{equation}

where $c_{i} = \frac{<F,q_{i}>}{<q_{i},q_{i}>}$ and $q_{i}$ is the orthogonal polynomial of degree i.

The output of the algorithm on the given dataset can be found in Table 2 and the dataset along with the polynomial regressor can be found in Figure 1.

\begin{table}[ht]
\centering
\label{polynomial_args}
\begin{tabular}{|c|c|c|c|c|}
\hline
 i  &    alpha   &    beta  &  variance     &     c \\ \hline
 0  &  0.686667  &  0.000000  & 2.990889 &  1.698000 \\
 1  & 2.777125  & 41.713196  & 2.541700  & 0.118797 \\
 2  & -0.453838 & 38.348371 &  0.089604 &  0.036500 \\
\hline
\end{tabular}
\caption{Tabulated values of $i$ : the degree n of the polynomial. alpha($\alpha$) and beta($\beta$) : The corresponding multipliers used to generate the nth degree polynomial in the equation \\ $q_{n+1}(x) = xq_{n}(x) - \alpha_n q_{n}(x) - \beta_n q_{n-1}(x)$ \and Finally c : is the coefficient used in evaluating the polynomial $p_{n}(x) = \sum_{i=0}^{n}c_{i}q_{i}$}
\end{table}

\begin{figure}[ht]
	\center
    \includegraphics[scale=0.75]{Q1_fig.png}
    \caption{Plot showing the data points and the polynomial as determined by the algorithm. The polynomial has been evaluated at 100 equidistant points between [-9.7, 12.3] and the degree of the polynomial fit is 2}
	\label{fig:Q1_fig}
\end{figure}

% \begin{figure}[ht]
% 	\center
%     \includegraphics[scale=0.75]{plotS_dash.png}
%     \caption{Plot showing the first derivative of the natural cubic interpoland S'(X). Values of $S'(X_i)$ for $X_i$ in the range of (min(knot values), max(knot values)) are shown as red circles.}
% 	\label{fig:sdash}
% \end{figure}

% \begin{table}[ht]
% \centering
% \label{my-label}
% \begin{tabular}{|c|c|c|}
% \hline
%  \textbf{n}&\textbf{RMS Error}&\textbf{Condition Number}  \\ \hline
%  2&0.73833521&15.0167409881  \\ \hline
%  5&1.02602254&282901.77002 \\ \hline
%  8&1.09505307&7657562245.89 \\ \hline
%  12&1.16783884&5.89342127254e+15 \\ \hline
%  15&2.11656192&1.74359790918e+17 \\ \hline
% \end{tabular}
% \caption{Table showing the values of n and the RMS error after solving the system of linear equations with \textbf{hilbert(n)} as the coefficients.}
% \end{table}
The corresponding python code can be found at \ref{code:problem3a}
\subsection{Fitting the polynomial regressor on \textit{World Population} data.}\label{prob3b}
In this question we are asked to repeat the polynomial regression as above, but on a different dataset. The dataset and the output of the algorithm can be found in Table 3 and Table 4 respectively.

In the figure below we have plotted the degree 4 polynomial as suggested by the algorithm. It can be seen that the polynomial does not fit the data very well.

This is because of the large set of missing values between years 1000 and 1650. Because of these missing values, the dataset doesn't represent the underlying trend very well, resulting in a bad polynomial fit.

Hence, polynomial regression is not very well suited to fit data with a lot of missing values, It should be used only when the dataset is fairly evenly spread through the range of values over which we want to fit the polynomial.

\textbf{NOTE:} In the subsequent section we evaluate the fit of the same data augmented with some dummy values between 1000 and 1650. To evaluate whether the fit of the polynomial regressor improves.
\begin{figure}[ht]
	\center
    \includegraphics[scale=0.75]{Q1b_fig.png}
    \caption{Plot showing the World population dataset and the 4th degree polynomial as suggested by our algorithm. It can be visually verified that the data is not very well fit with the given 4th degree polynomial}
	\label{fig:world_pop}
\end{figure}

\begin{table}[ht]
\centering
\label{population-label}
\begin{tabular}{|c|c|}
\hline
 \textbf{x}&\textbf{y} \\ \hline
1000 & 0.34 \\
1650 & 0.545 \\
1800 & 0.907 \\
1900 & 1.61 \\
1950 & 2.51 \\
1960 & 3.15 \\
1970 & 3.65 \\
1980 & 4.2 \\
1990 & 5.3 \\
\hline
\end{tabular}
\caption{The \textit{World Population} dataset that needs to be fit with the polynomial regressor used in the previous section.}
\end{table}

\begin{table}[ht]
\centering
\label{polynomial-args}
\begin{tabular}{|c|c|c|c|c|}
\hline
 i &       alpha  &       beta    &  variance   &         c\\ \hline
 0 &  1800.000000 &    0.000000   &   3.035407  &   2.468000\\
 1 &  1201.833741 &  90888.888889 &    1.821713 &    0.003755\\
 2 &  1671.383205 &  58100.462442 &    0.714551 &    0.000013\\
 3 &  1811.975200 &  10318.358986 &    0.270872 &    0.000000\\
 4 &  1881.282629 &  6073.275792  &   0.063930  &   0.000000\\
\hline
\end{tabular}
\caption{Tabulated values of $i$ : the degree n of the polynomial. alpha($\alpha$) and beta($\beta$) : The corresponding multipliers used to generate the nth degree polynomial in the equation \\ $q_{n+1}(x) = xq_{n}(x) - \alpha_n q_{n}(x) - \beta_n q_{n-1}(x)$ \and Finally c : is the coefficient used in evaluating the polynomial $p_{n}(x) = \sum_{i=0}^{n}c_{i}q_{i}$ these values are for the \textit{World Population} dataset. Here the algorithm computes that a polynomial of degree 4 would be the best fit for the given data.}
\end{table}
\clearpage
\subsubsection{Augmenting the world population data with dummy values to check if the overall fit of the polynomial regressor improves.}\label{augmented_section}
% Augmented data
In this section, we run a small experiment to check if the overall fit of the polynomial regressor improves if the dataset has fewer gaps ( missing values ).

For the experiment, we have augmented the \textit{World Population dataset} with a few dummy values between years 1000 and 1650. The new dataset is given in Table 5.

\begin{table}[ht]
\centering
\label{augmented-label}
\begin{tabular}{|c|c|}
\hline
 \textbf{x}&\textbf{y} \\ \hline
1000 & 0.34 \\
1100 & 0.38 \\
1200 & 0.42 \\
1300 & 0.5 \\
1450 & 0.51 \\
1650 & 0.545 \\
1800 & 0.907 \\
1900 & 1.61 \\
1950 & 2.51 \\
1960 & 3.15 \\
1970 & 3.65 \\
1980 & 4.2 \\
1990 & 5.3 \\
\hline
\end{tabular}
\caption{The \textit{World Population} dataset \textbf{\textit{augmented with dummy data}}  between years 1000 and 1650 to check the hypothesis that adding in the missing values indeed results in a polynomial regressor which has a better fit to the overall data.}
\end{table}
We run the same polynomial regression algorithm on the new dataset and find that a 4th degree polynomial would give the smallest least squares error with the augmented dataset. The output of the algorithm can be seen in Table 6.

\begin{table}[ht]
\label{table:polynomial_args_3}
\centering
\begin{tabular}{|c|c|c|c|c|}
\hline
 i &       alpha  &       beta     &  variance &         c\\ \hline
 0 &  1634.615385 &    0.000000    &  2.962034 &   1.847846\\
 1 &  1443.872563 &  129609.467456 &  1.225232 &  0.003619\\
 2 &  1433.542239 &  49394.313405  &  0.671342 &  0.000009\\
 3 &  1478.542841 &  67784.468510  &  0.342014 &  0.000000\\
 4 &  1492.567253 &  58694.769631  &  0.187048 &  0.000000\\
\hline
\end{tabular}
\caption{Tabulated values of $i$ : the degree n of the polynomial. alpha($\alpha$) and beta($\beta$) : The corresponding multipliers used to generate the nth degree polynomial in the equation \\ $q_{n+1}(x) = xq_{n}(x) - \alpha_n q_{n}(x) - \beta_n q_{n-1}(x)$ \and Finally c : is the coefficient used in evaluating the polynomial $p_{n}(x) = \sum_{i=0}^{n}c_{i}q_{i}$ these values are for the \textit{World Population} dataset \textit{\textbf{augmented with dummy data}} to test the hypothesis that adding the missing data between years 1000 and 1650 gives a better polynomial regressor for the entire dataset. Here the algorithm computes that a polynomial of degree 4 would be the best fit for the data.}
\end{table}

We see that visually overall fit of the 4th degree polynomial to the augmented dataset looks much better. See \ref{fig:test_fig}

\begin{figure}[ht]
	\center
    \includegraphics[scale=0.75]{Q1b_test_fig.png}
    \caption{Plot showing the augmented data and the polynomial regressor. It can be visually verified that this fit is better than the one in which the data between years 1000 and 1650 were missing.}
	\label{fig:test_fig}
\end{figure}

% \begin{figure}[ht]
% 	\center
%     \includegraphics[scale=0.75]{matlabS_dash.png}
%     \caption{Plot showing the first derivative of the natural cubic interpoland S'(X) as calculated using Matlab. Values of $S'(X_i)$ for $X_i$ in the range of (min(knot values), max(knot values)) are shown as red circles.}
% 	\label{fig:sdash_matlab}
% \end{figure}
% The corresponding plots created by matlab are much more smoother because matlab implements an additional constraint that the third derivatives of the piecewise polynomials are also equal at the second and the last knots. This are apparently called the "Not-A-Knot" end conditions. This is only done when the length of \textbf{t} and \textbf{y} are same.
%
% Natural splines are a good choice only when the functions have 0 second derivative at the end points. I read it up from here. \url{http://www.mathworks.com/matlabcentral/newsreader/view_thread/172988} would really appreciate some more information about why using the Not-A-Knot condition is better.
% \\
% The corresponding matlab code can be found at \ref{code:problem2b}
\clearpage

\section{Appendix A}\label{code:problem3a}
Python source code for \ref{prob3a} and \ref{prob3b}.
{\footnotesize
\begin{lstlisting}
import numpy as np
import pylab as pl

def dot_prod(x,y):
    # the lenght of arrays must be same.
    assert(len(x) == len(y))
    # get corresponding values from x and y
    # for v in zip(x,y)
    # find their product and put the result in a list, list comprehension.
    # sum over the list
    return np.sum([v[0]*v[1] for v in zip(x,y)])

def get_next_alpha(x,qPrev):
    # Caclulating new alpha now
    alpha_num = dot_prod(x*qPrev,qPrev)
    alpha_denom = dot_prod(qPrev,qPrev)
    return np.true_divide(alpha_num, alpha_denom)

def get_next_beta(x,qPrev,qOld):
    # Calculating new beta now
    beta_num = dot_prod(x*qPrev,qOld) # beta's numerator
    beta_denom = dot_prod(qOld,qOld) # beta's denominator
    return np.true_divide(beta_num, beta_denom)

def get_parameters(x,y):
    m = len(x) - 1
    # qi-2
    qOld = np.ones(x.shape)

    # Initial alpha alues
    alpha = np.zeros(x.shape)
    alpha[0] = np.true_divide(dot_prod(x * qOld, qOld),dot_prod(qOld,qOld))

    # allocating space for beta
    beta = np.zeros(x.shape)

    # qi-1
    qPrev = x - alpha[0]

    # Initializing c
    c = np.zeros(x.shape)
    c[0] = np.true_divide(dot_prod(y,qOld),dot_prod(qOld,qOld))
    c[1] = np.true_divide(dot_prod(y,qPrev),dot_prod(qPrev,qPrev))

    # Rho i and i-1
    rho = np.ones(x.shape)
    rho[0] = dot_prod(y,y) - np.true_divide(np.square(dot_prod(y,qOld)),dot_prod(qOld,qOld))
    rho[1] = rho[0] - np.true_divide(np.square(dot_prod(y,qPrev)),dot_prod(qPrev,qPrev))

    # Start with degree 1
    n = 1

    # Initializing sigma_sq
    sigma_sq = np.ones(x.shape)
    sigma_sq[0] = np.true_divide(rho[0], m)
    sigma_sq[1] = np.true_divide(rho[1], m-n)
    while(True):
        # Calculating new beta now
        beta[n] = get_next_beta(x,qPrev,qOld)

        # Caclulating new alpha now
        alpha[n] = get_next_alpha(x,qPrev)

        # Calculating q_n+1
        qNew = x*qPrev - alpha[n]*qPrev - beta[n]*qOld

        # Calculating new C
        c[n+1] = np.true_divide(dot_prod(y,qNew),dot_prod(qNew,qNew))

        # Calculate rho_n+1
        rho[n+1] = rho[n] - np.true_divide(np.square(dot_prod(y,qNew)),dot_prod(qNew,qNew))
        # Calculate sigma_sq_n+1
        sigma_sq[n+1] = np.true_divide(rho[n+1],m-(n+1))
        # if sigma_sq_n+1 > sigma_sq_n
        # or abs(sigma_sq_n - sigma_sq_n+1) < 0.1
        # Then stop
        if sigma_sq[n+1] > sigma_sq[n] or np.abs(sigma_sq[n] - sigma_sq[n+1]) < 0.1:
            break
        else:
            # Update n += 1
            n += 1
            # update qOld,qPrev = qPrev,qNew
            np.copyto(qOld,qPrev)
            np.copyto(qPrev,qNew)

    return c,alpha,beta,sigma_sq,n

def evaluate(t,alpha,beta,c,n):
    x_vals = np.asarray(t)
    retVals = []
    for x in x_vals:
        sum = 0
        # qi-2
        qOld = np.ones(1)
        sum += c[0]*qOld
        # qi-1
        qPrev = x - alpha[0]
        sum += c[1]*qPrev
        for i in range(2,n+1): # 2,n+1 because 0 and 1 are already calculated
            qNew = x*qPrev - alpha[i]*qPrev - beta[i]*qOld
            qOld = qPrev
            qPrev = qNew
            sum += c[i] * qNew
        retVals.append(sum)
    return retVals

def process_and_plot(data,pl,file_name=None):
    x,y = map(np.asarray,zip(*data))
    c,alpha,beta,sigma_sq,n = get_parameters(x,y)

    print("\n{:>2} {:>10} {:>10} {:>10} {:>10}".format("i","alpha","beta","variance","c"))
    for i in range(n+1):
        print("{:2d} {:10f} {:10f} {:10f} {:10f}".format(i,alpha[i],beta[i],sigma_sq[i],c[i]))
        # print(i,alpha[i],beta[i],sigma_sq[i],c[i])

    pl.scatter(x,y,label="Original Data")
    xvals = np.linspace(min(x),max(x),100)
    yvals = evaluate(xvals,alpha,beta,c,n)
    pl.plot(xvals,yvals,c='r',label="Fitted Polynomial")
    pl.xlim(min(x)-5,max(x)+5)
    pl.ylim(min(y)-5,max(y)+5)
    pl.xlabel("X")
    pl.ylabel("Y")
    pl.legend()
    if file_name != None:
        pl.savefig(file_name)

if __name__ == '__main__':
    # Q3.a data
    data = [(-9.70,3.76),
            (-7.30,1.78),
            (-5.40,1.52),
            (-5.00,1.31),
            (-3.01,0.31),
            (-2.13,0.23),
            (-1.20,0.45),
            (-0.56,0.29),
            (0.00,0.00),
            (1.20,0.45),
            (4.50,0.28),
            (6.70,2.12),
            (9.90,3.91),
            (10.00,3.47),
            (12.30,5.59)]
    pl.figure()
    process_and_plot(data,pl,"Q1_fig.png")
    # test data, almost straight line
    data_2 = [(1,4),(2,4.5),(3,3.9),(4,4.6),(4,3.7)]
    pl.figure()
    process_and_plot(data_2,pl)
    # Q3.b data
    data_3 = [(1000,0.340),(1650,0.545),(1800,0.907),(1900,1.61),(1950,2.51),(1960,3.15),(1970,3.65),(1980,4.20),(1990,5.30)]
    pl.figure()
    process_and_plot(data_3,pl,"Q1b_fig.png")
    # Q3.b test data
    test_data = [(1000,0.340),(1100,0.38),(1200,0.42),(1300,0.5),(1450,0.51),(1650,0.545),(1800,0.907),(1900,1.61),(1950,2.51),(1960,3.15),(1970,3.65),(1980,4.20),(1990,5.30)]
    pl.figure()
    process_and_plot(test_data,pl,"Q1b_test_fig.png")

    pl.show()
    pl.close()
\end{lstlisting}
}
\end{document}


\documentclass[10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{fullpage}
\usepackage{exsheets}

\linespread{1.1}
\setlength\parindent{0pt}

\begin{document}

\section{Problem: The nonlinear pendulum}

The equation of motion for the pendulum using Newton's second law can be written in terms of the displacement angle $\theta$ as:
%
\begin{equation}\label{eq:pendulum}
\frac{d^2\theta}{dt^2} = - \frac{g}{l} \sin \theta
\end{equation}

We first use the trick to turn the second-order equation into two first-order equations by introducing a new variable $\omega$:
%
\begin{eqnarray}
\frac{d\theta}{dt} & = & \omega \label{eq:pendulum1}\\
\frac{d\omega}{dt} & = & -\frac{g}{l} \sin \theta \label{eq:pendulum2}
\end{eqnarray}

\begin{question}
Write a program to solve the equations~(\ref{eq:pendulum1}) and (\ref{eq:pendulum2}) using the second-order Runge-Kutta method for a pendulum with a 10 cm arm.
Calculate the angle $\theta$ of displacement for several periods of the pendulum starting from the initial condition $(\theta, \omega) = (179^\circ, 0)$.
\end{question}

\begin{question}
Make a plot of the total energy of the system as a function of time:
%
\begin{equation}
E = K + U = \frac{1}{2} m (\omega^2 L^2) + m g L (1 - \cos \theta)
\end{equation}
\end{question}

\newpage

\section{Problem: The 1-D diffusion equation}

The 1-dimensional diffusion equation assuming constant diffusion coefficient and steady state is
%
\begin{equation}\label{eq:diffusion}
-D\frac{\partial^2 f(x)}{\partial x^2} = Q(x)
\end{equation}
%
where $x$ is a spatial coordinate between $[-H,H]$ and we assume as boundary conditions: $f(x = \pm 1) = 0$.

To provide us with an analytical solution (with the correct boundary conditions) we prescribe
%
\begin{equation}
\tilde f(x) = \cos(\pi x / 2)
\end{equation}
%
to be an analytical solution and we derive the correspondent source term using equation~(\ref{eq:diffusion}):
%
\begin{equation}
Q(x) = \dots
\end{equation}

Equipped now with a source term, we find numerically the solution of the equation~(\ref{eq:diffusion}) as a limit for $t \rightarrow \infty$ of the time-dependent equation:
%
\begin{equation}
\frac{\partial f(x,t)}{\partial t} -D \frac{\partial^2 f(x,t)}{\partial x^2} = Q(x)
\end{equation}

We do this by using the Crank-Nicolson method and by plotting as a function of time a measure (e.g., 2-norm) of the distance between the numerical solution and the analytical one.

\begin{question}
Using the parameter values $H = 1$, $D = 0.1$, $dt = 10^{-3}$, $N = 2^8$ the plot should look like the following fig.
\end{question}

\begin{center}
\includegraphics[scale=0.35]{DiffusionTimeEvolution.pdf}
\end{center}

\begin{question}
Show also the case for $N = 2^9$ and $N = 2^{10}$. What is the error ratio in the limit $t \rightarrow \infty$ between these cases?
\end{question}

\end{document}


\chapter{Finite element discretisation}
\label{sec:discretization}

In this chapter we introduce a mixed finite element discretisation for a steady-state incompressible MHD problem that model electrically conductive fluids. Following the setting in \cite{schotzau2004mixed}, using curl-conforming elements for the magnetic field and conforming continuous elements for the velocity field. The resulting discretisation is verified though a series of numerical experiments which appear later in Chapter \ref{chap:results}. For simplicity, we only discuss in detail homogeneous Dirichlet boundary conditions, that is
\begin{equation} \label{eq:homogeneousBC}
    \uu{u} = \uu{0} \quad \mbox{and} \quad \uu{n}\times \uu{b} = \uu{0}.
\end{equation}
Inhomogeneous conditions as in \eqref{eq:bc} can be incorporated in a straightforward fashion.

\section{Variational formulation}
\label{sec:variation}

To express \eqref{eq:mhd}, \eqref{eq:bc} in weak form we follow \cite{schotzau2004mixed} and denote the $L^2$ inner product on $L^2(\Omega)^d$ by $(\cdot,\cdot)_\Omega$, for $d = 2,3$. We introduce the standard Sobolev spaces
\begin{equation} \label{eq:FuncSpace} \nonumber
 \left. \begin{aligned}
\uu{V}&=H_0^1(\Omega)^d=\left\{\,\uu{u}\in H^1(\Omega)^d\,:\,\text{$\uu{u}=\uu{0}$ on $\partial\Omega$}\,\right\},\\
Q&=L^2_0(\Omega)=\{\,p\in L^2(\Omega)\,:\,(p\,,1)_\Omega=0\,\},\\
\uu{C}&=H_0({\rm curl};\Omega) = \left\{\,\uu{b}\in L^2(\Omega)^d\,:\,\nabla\times\uu{b}\in L^2(\Omega)^{2d-3}, \
\text{$\uu{n}\times\uu{b}=\uu{0}$ on $\partial\Omega$}\,\right\},\\
S&=H^1_0(\Omega)=\{\,r\in H^1(\Omega)\,:\,r=0\ \mbox{on $\partial\Omega$}\,\},
 \end{aligned}
 \right.
 \qquad \text{}
\end{equation}
We write $\|\cdot\|_{L^2(\Omega)}$, $\|\cdot\|_{H^1(\Omega)}$ and $\|\cdot\|_{H(\rm{curl};\Omega)}$ for the associated natural norms. More precisely, for a vector fields $\uu{u},\uu{b}$ and a scalar functions $r$ the norms are defined as follows:
\begin{equation} \nonumber
 \left. \begin{aligned}
    \|\uu{u}\|_{L^2 (\Omega)} &= \left({\int_{\Omega} \uu{u}\cdot\uu{u}\;dx}\right)^{\frac{1}{2}},\\
   \|\uu{u}\|_{H^1(\Omega)} &=  \left(\|\uu{u}\|_{L^2(\Omega)}^2 + \|\nabla  \uu{u}\|_{L^2(\Omega)}^2 \right)^{\frac{1}{2}},\\
   \|\uu{b}\|_{H(\rm{curl},\Omega)} &=  \left(\|\uu{b}\|_{L^2(\Omega)}^2 + \|\nabla \times \uu{b}\|_{L^2(\Omega)}^2 \right)^{\frac{1}{2}}, \\
    \|r\|_{L^2 (\Omega)} &= \left({\int_{\Omega} r^2\;dx}\right)^{\frac{1}{2}},\\
    \|r\|_{H^1(\Omega)} &=  \left(\|r\|_{L^2(\Omega)}^2 + \|\nabla  r\|_{L^2(\Omega)}^2 \right)^{\frac{1}{2}},\\
 \end{aligned}
 \right.
 \qquad \text{}
\end{equation}
where $\|\nabla  \uu{u}\|_{L^2(\Omega)}^2$ is the $L^2$-norm of the gradient tensor $\nabla \uu{u}$. The weak formulation of the incompressible MHD system (\ref{eq:mhd}) and the boundary conditions (\ref{eq:bc}) consists in finding~$(\uu{u},p,\uu{b},r)\in \uu{V} \times Q\times \uu{C} \times S$ such that
\begin{subequations}
\label{eq:weak}
\begin{eqnarray}
\label{eq:weak1} A(\uu{u},\uu{v}) + O(\uu{u};\uu{u},\uu{v})
+C(\uu{b};\uu{v},\uu{b})
+B(\uu{v}, p) & =& (\uu{f}, \uu{v})_{\Omega},\\[.1cm]
\label{eq:weak2}
B(\uu{u},q)&=&0, \\[.1cm]
\label{eq:weak3}
M(\uu{b},\uu{c})-C(\uu{b};\uu{u},\uu{c})+D(\uu{c},r)&=& (\uu{g},\uu{c})_\Omega, \\[.1cm]
\label{eq:weak4} D(\uu{b},s)&=&0,
\end{eqnarray}
\end{subequations}
for all $(\uu{v},q,\uu{c},s)\in \uu{V} \times Q\times \uu{C}\times
S$. The individual variational forms are given by
\begin{alignat*}2\nonumber
&A(\uu{u},\uu{v})=  \int_\Omega \nu \, \nabla\uu{u}:
\nabla\uu{v}\,d\uu{x},&\qquad  & O(\uu{w};\uu{u},\uu{v}) = \int_\Omega
(\uu{w}\cdot\nabla)\uu{u} \cdot\uu{v} \, d\uu{x},
\\[.1cm]
&  B(\uu{u},q) = -\int_\Omega\,(\nabla\cdot\uu{u}) \,q \,d\uu{x},
&\qquad  &
 M(\uu{b},\uu{c})= \int_\Omega\, \kappa\nu_m
(\nabla\times\uu{b})\cdot(\nabla\times\uu{c})\,d\uu{x},\\[0.1cm]
& D(\uu{b},s) = \int_\Omega\, \uu{b} \cdot \nabla s\,
d\uu{x}, & \qquad &
C(\uu{d};\uu{v},\uu{b}) =  \int_\Omega \kappa\, (\uu{v}\times\uu{d})\cdot
(\nabla\times\uu{b})\, d\uu{x},
\end{alignat*}
where  $\nabla \uu{u}:\nabla \uu{u}$ is  defined as
$$\nabla \uu{u}:\nabla \uu{u} = \sum^d_{i,j=1}(\nabla \uu{u})_{ij}(\nabla \uu{u})_{ij}.$$ In \cite{schotzau2004mixed} it has been shown that this formulation of a the problem is discrete energy-stable and has a unique solution for small data.

\section{Mixed finite element discretisation}

Consider the domain $\Omega$ to be divided up into a regular and quasi-uniform mesh ${\mathcal T}_h=\{K\}$ consisting of triangles ($d = 2$) or tetrahedra ($d = 3$)  with mesh size $h$. Based on the function spaces defined in \eqref{eq:FuncSpace}, our finite element approximation will be sought in the finite spaces given by:
\begin{equation}
\label{eq:FiniteSpace}
\begin{split}
\uu{V}_h &=  \{\, \uu{u}\in H^1( \Omega)\, :\, \uu{u}|_K \in {\mathcal P}_{k}(K)^d, \, K \in{\mathcal T}_h \, \},\\[.1cm]
Q_h&=  \{\, p\in L^2(\Omega) \cap H^1(\Omega)\,:\, p|_K \in {\mathcal P}_{k-1}(K), \, K \in{\mathcal T}_h \,\},\\[.1cm]
\uu{C}_h &=  \{\, \uu{b}\in H_0({\rm curl}; \Omega) \,:\, \uu{b}|_K \in {\mathcal P}_{k-1}(K)^d \oplus \uu{R}_k(K), \, K \in{\mathcal T}_h \,\},\\[.1cm]
S_h&=  \{\, r\in H_0^1(\Omega) \,:\, r|_K \in {\mathcal P}_{k}(K), \, K \in {\mathcal T}_h \, \},
\end{split}
\end{equation}
for $k\geq 2$. Here we note that we are using ${\mathcal P_k}/{\mathcal P_{k-1}}$ Taylor-Hood elements for the fluid unknowns $(\uu{u},p)$ \cite{taylor1973numerical}. For the magnetic variables $(\uu{b},r)$ we use the curl-conforming \nedelec elements of the first kind \cite{nedelec1980mixed}. These choices of finite elements spaces $\uu{V}_h, \, \uu{C}_h, \, Q_h$ and $S_h$ imply that  we have conforming subspaces to our Sobolev spaces $\uu{V}, \, \uu{C}, \,Q$ and $S$, respectively. Then the finite element solution consists in finding $(\uu{u}_h,p_h,\uu{b}_h,r_h)\in \uu{V}_h\times Q_h\times \uu{C}_h\times S_h$ such that
\begin{subequations}
\label{eq:VariationForm}
\begin{eqnarray}
\label{eq:bn1} \hspace{-15mm} A(\uu{u}_h,\uu{v}) + \tilde{O}(\uu{u}_h;\uu{u}_h,\uu{v}) +C(\uu{b}_h;\uu{v},\uu{b}_h) +B(\uu{v}, p_h) & = & ( \uu{f},\uu{v}),\\[.1cm]
\label{eq:bn2}
B(\uu{u}_h,q)&=& 0, \\[.1cm]
\label{eq:bn3} M(\uu{b}_h,\uu{c})-C(\uu{b}_h;\uu{u}_h,\uu{c})+ D(\uu{c},r_h)&=& (\uu{g},\uu{c}),\\[.1cm]
\label{eq:bn4} D(\uu{b}_h,s)&=&0,
\end{eqnarray}
\end{subequations}
for all $(\uu{v},q,\uu{c},s)\in \uu{V}_h\times Q_h \times \uu{C}_h\times S_h$.

The forms $A, M, B, D$ and $C$ stay the same as on the continuous level. However, for the convection term $\tilde{O}(\cdot;\cdot,\cdot)$ we to modify the form $O(\uu{w};\uu{u},\uu{v})$ in a standard fashion to ensure the energy-stability property
\begin{equation} \label{eq:convection}
    \tilde{O}(\uu{w};\uu{u},\uu{u}) = 0, \quad \forall \uu{w},\uu{u} \in  \uu{V}_h.
\end{equation}
To ensure this property we integrate by parts the convection form $O(\uu{w};\uu{u},\uu{u})$  to obtain
\begin{equation} \nonumber
 \left. \begin{aligned}
     \int_\Omega (\uu{w}\cdot\nabla)\uu{u} \cdot\uu{u} \, d\uu{x} =& -\frac{1}{2}\int_{\Omega} \nabla \cdot \uu{w} \uu{u} \cdot \uu{u} \, d\uu{x}
     +\frac{1}{2}\int_{\partial \Omega} \uu{w}\cdot \uu{n} |\uu{u}|^2\, ds,
 \end{aligned}
 \right.
 \qquad \text{}
\end{equation}
recalling that $\uu{n}$ is the unit outward normal on $\partial \Omega$. Therefore, we choose the modified convection form $\tilde{O}(\uu{w};\uu{u},\uu{v})$ as
$$\tilde{O}(\uu{w};\uu{u},\uu{v}) =  \int_\Omega (\uu{w}\cdot\nabla)\uu{u} \cdot\uu{v} \, d\uu{x} +\frac{1}{2}\int_{\Omega} \nabla \cdot \uu{w} \uu{u} \cdot \uu{v}\, d\uu{x}-\frac{1}{2}\int_{\partial \Omega} \uu{w}\cdot \uu{n} \uu{u} \cdot \uu{v}\, ds.$$
By construction, the property \eqref{eq:convection} is now satisfied. Note also that for homogeneous boundary conditions as assumed in \eqref{eq:homogeneousBC}, the boundary integral term in $\tilde{O}$ can be omitted.

Again in \cite{schotzau2004mixed} it has been shown that this formulation of a MHD is discrete energy-stable and has a unique solution for small data. Also, optimal order error estimates in the mesh size $h$ have been derived for small data using the stability property \eqref{eq:convection}. Namely, for sufficiently smooth solutions, we have that
$$\|\uu{u}-\uu{u}_h\|_{H^1(\Omega)}+\|\uu{b}-\uu{b}_h\|_{H(\rm{curl};\Omega)}+\|p-p_h\|_{L^2(\Omega)}+\|r-r_h\|_{H^1(\Omega)} \leq C h^k,$$
for a constant $C>0$ independent of the mesh size. However, the $L^2$-norm error for the velocity field is of order $\mathcal{O}(h^{k+1})$ (as $\uu{V}_h$ consists of a full polynomial space on each element). In contrast, we cannot expect $L^2$-norm errors of  order $\mathcal{O}(h^{k+1})$ for the magnetic field (as $\uu{C}_h$ does not consists of a full polynomial space on each element).

\subsection{Matrix representation}

This variational formulation \eqref{eq:VariationForm} now can be converted into a matrix representation. To do this, we introduce the basis function for the finite elements spaces in \eqref{eq:FiniteSpace}:
\begin{alignat}2
\label{eq:bases1}
\uu{V}_h & = \mbox{span}\langle  \uu{\psi}_j \rangle _{j=1}^{n_u}, & \qquad &
Q_h  = \mbox{span} \langle  \alpha_i \rangle _{i=1}^{m_u},\\[0.1cm]
 \uu{C}_h& =\mbox{span}\langle \uu{\phi}_j \rangle _{j=1}^{n_b}, & \qquad & S_h = \mbox{span} \langle \beta_i
\rangle_{i=1}^{m_b}.
\end{alignat}
The aim now is to find the coefficient vectors $u = (u_1, \ldots , u_{n_u}) \in \mathbb{R}^{n_u}$, $p = (p_1, \ldots , p_{m_u}) \in \mathbb{R}^{m_u}$, $b = (b_1, \ldots , b_{n_b}) \in \mathbb{R}^{n_b}$, and $r = (r_1, \ldots , r_{m_b}) \in \mathbb{R}^{m_b}$ of the finite element functions $(\uu{u}_h, p_h,\uu{b}_h, r_h)$. As usual, this is done by writing the bilinear forms in \eqref{eq:VariationForm} in terms of the following stiffness matrices and load vectors:
\begin{alignat*}2
A_{i,j} &= A(\uu{\psi}_j,\uu{\psi}_i), &\quad  &1 \leq i,j \leq n_u,\\[0.1cm]
B_{i,j} &= B(\uu{\psi}_j,\alpha_i), &\quad &1 \leq i \leq m_u, \ 1 \leq j \leq n_u,\\[.1cm]
D_{i,j} &= D(\uu{\phi}_j,\beta_i),  & & 1 \leq i \leq m_b,\ 1 \leq j \leq n_b,\\[.1cm]
M_{i,j}&= M(\uu{\phi}_j,\uu{\phi}_i), &\qquad & 1 \leq i,j \leq n_b,\\[.1cm]
f_i &= (\uu{f},\uu{\psi}_i)_\Omega, & & 1\leq i\leq n_u,\\[.1cm]
g_i &= (\uu{g},\uu{\phi}_i)_\Omega, & & 1\leq i \leq n_b.
\end{alignat*}
For the two non-linear forms, $\tilde{O}$ and $C$, we define the corresponding stiffness matrices with respect to given finite element functions $\uu{w} \in \uu{V}_h$ and $\uu{d}_h\in \uu{C}_h$ in the first argument and their associated coefficient vectors $w$ and $d$ as
\begin{alignat*}2
O(w)_{i,j} &=\tilde{O}(\uu{w};\uu{\psi}_j,\uu{\psi}_i), &\quad  &1 \leq i,j \leq n_u,\\[.1cm]
C(d)_{i,j} &= C(\uu{d};\uu{\psi}_j,\uu{\phi}_i), & & 1\leq i \leq n_b,\ 1 \leq j \leq n_u.
\end{alignat*}

Thus, the numerical solution to \eqref{eq:mhd} consists in solving the non-linear system
\begin{equation}
\label{eq:matrix-system}
\left(
\begin{array}{cccc}
A+O(u) & B^T & C^T(b) & 0\\
B & 0 & 0 & 0\\
-C(b) & 0 & M & D^T \\
0 & 0 & D & 0
\end{array}
\right)
\,
\left(
\begin{array}{c}
u\\
p\\
b\\
r
\end{array}
\right) =
\left(
\begin{array}{c} f\\0\\g\\0
\end{array}
\right).
\end{equation}
where the vectors  $u\in\mathbb{R}^{n_u}$, $p\in\mathbb{R}^{m_u}$,  $b\in\mathbb{R}^{n_b}$, and $r\in\mathbb{R}^{m_b}$ are the unknown coefficients of the finite element functions. We shall omit the dependence of $O$ and $C$ on $b$ and $u$, respectively, and simply  write $O$ and $C$.

\section{Picard iteration (P)}
\label{sec:nonlinear}
The discrete system \eqref{eq:matrix-system} is non-linear, and therefore appling a non-linear solver to this problem is necessary. A common choice to deal with the non-linearity within the incompressible Navier-Stokes equations in isolation is to perform Oseen or Picard iterations \cite{elman2005finite}. This involves linearising around the current velocity and solving for updates.

We adapt this approach for the full MHD system as well. Given a current iterate $(\uu{u}_h,p_h,\uu{b}_h,r_h)$  we solve for updates $(\delta \uu{u}_h,\delta p_h,\delta \uu{b}_h,\delta r_h)$ and introduce the next iterate by setting:
\begin{equation}\nonumber
\begin{array}{cc}
% \label{eq:updates}
\uu{u}_h& \hspace{-3mm} \rightarrow \uu{u}_h +\delta \uu{u}_h, \quad p_h \rightarrow p_h +\delta p_h,\\
\uu{b}_h& \hspace{-3mm}  \rightarrow \uu{b}_h +\delta \uu{b}_h, \quad r_h \rightarrow r_h +\delta r_h.
\end{array}
\end{equation}
In variational form, the updates $(\delta \uu{u}_h,\delta p_h,\delta \uu{b}_h,\delta r_h)\in \uu{V}_h\times Q_h \times \uu{C}_h\times S_h$ are found by solving the Picard system (P):
\begin{equation} \nonumber
% \label{eq:picard}
\begin{split}
A(\delta\uu{u}_h, \uu{v}) +\tilde{O}(\uu{u};\delta\uu{u}_h,\uu{v})+ C(\uu{b}_h;\uu{v},\delta \uu{u}_h) + B(\uu{v}, \delta p_h) & = R_u(\uu{u}_h,\uu{b}_h,p_h;\uu{v}),\\[.1cm]
B(\delta\uu{u}_h,q)&= R_p(\uu{u}_h;q), \\[.1cm]
M(\delta \uu{b}_h,\uu{c})+
D(\uu{c},\delta r_h)-C(\uu{b}_h;\delta \uu{u}_h,\uu{v})&= R_b(\uu{u}_h,\uu{b}_h,r_h;\uu{c}),\\[.1cm]
D(\delta \uu{b}_h,s)&= R_r(\uu{b}_h;s),
\end{split}
\end{equation}
for all $(\uu{v},q,\uu{c},s)\in \uu{V}_h\times Q_h \times \uu{C}_h\times S_h$. The right hand side linear forms correspond to the residual at the current iteration $(\uu{u}_h,p_h,\uu{b}_h,r_h)$ defined by:
\begin{align*}
 R_u(\uu{u}_h,\uu{b}_h,p_h;\uu{v})&=(\uu{f}, \uu{v})_\Omega-A(\uu{u}_h,\uu{v})
-  \tilde{O}(\uu{u}_h;\uu{u}_h,\uu{v})  - C(\uu{b}_h;\uu{v},\uu{b}_h)-B(\uu{v},p_h),\\[.1cm]
R_p(\uu{u}_h;q)&=-B(\uu{u}_h,q),\\[.1cm]
 R_b(\uu{u}_h,\uu{b}_h,r_h;\uu{c})&=(\uu{g,c})_\Omega -M(\uu{b}_h,\uu{c})
+ C(\uu{b}_h;\uu{u}_h,\uu{c})-D(\uu{c},r_h),\\[.1cm]
R_r(\uu{b}_h;s)&=-D(\uu{b}_h,s),
\end{align*}
for all $(\uu{v},q,\uu{c},s)\in \uu{V}_h\times Q_h \times \uu{C}_h\times S_h$.

In \cite{schotzau2004mixed} it is shown that for small data the Picard iteration (P) will converge to the exact solution given any initial guess.

To formulate the variational form of the Picard iteration (P) in matrix form, let $({u},p,{b},r)$ be the coefficient vectors associated with $(\uu{u}_h,p_h,\uu{b}_h,r_h)$ and $(\delta{u},\delta p,\delta{b},\delta r)$ be the coefficient vectors of $(\delta \uu{u}_h,\delta p_h,\delta \uu{b}_h,\delta r_h)$, then it can readily seen that the Picard iteration (P) amounts to solving the matrix system
\begin{equation}
\label{eq:mhd_saddle}
%\mathcal{K} x \equiv
\left(
\begin{array}{cccc}
A+O & B^T & C^T & 0\\
B & 0 & 0 & 0 \\
-C & 0 & M & D^T\\
0 & 0 & D & 0
\end{array}
\right)
\,
\left(
\begin{array}{c}
\delta u\\
\delta p\\
\delta b\\
\delta r
\end{array}
\right)  =
\begin{pmatrix}
r_u \\
r_p\\
r_b\\
r_r
\end{pmatrix},
\end{equation}
with
\begin{align*}
r_u &= f- Au -O(u) u - C(b)^T b- B^T p,\\[0.1cm]
r_p &=-B u,\\[0.1cm]
r_b &=g-Mu+C(b)b-D^T r,\\[0.1cm]
r_r &=-D b.
\end{align*}
Here, the matrix $A$  is symmetric positive-definite (SPD), $O$ is non-symmetric and $-C,C^T$ appear in a skew symmetric fashion. We also note that $M$ is symmetric positive-semidefinite (SPSD) with nullity $m_b$ corresponding to the discrete gradients.

\section{Decoupled iterations}
\label{sec:FEMdecouple}

The full MHD system \eqref{eq:mhd}, \eqref{eq:bc} is a coupled system consisting of the incompressible Navier-Stokes and Maxwell's equations, coupled through the non-linear skew symmetric coupling term $C$. In addition, the convection term $O$ is non-linear as well. These two terms make the numerical solution challenging. Therefore, if one or both of these terms is small then it may be possible to iterate explicitly. In particular if the coupling term, $C$, is small then we may completely decouple the system into a Navier-Stokes problem and a Maxwell problem. The two resulting decoupling schemes are what we call Magnetic and Complete Decoupling and are both described below.

\subsection{Magnetic decoupling (MD)}
\label{sec:FEMmd}

Consider the first situation where there is  weak coupling within the system, that is when $C$ is small. Then it may be possible to drop these terms to completely decouple the system into the two subproblems, the Navier-Stokes and Maxwell's equations. We will call this Magnetic decoupling.
% For a given solution $(\uu{u}_h,p_h,\uu{b}_h,r_h)$, neglecting the coupling terms in \eqref{eq:picard} results in solving for the updates $(\delta \uu{u}_h,\delta p_h,\delta \uu{b}_h,\delta r_h) \in \uu{V}_h \times Q_h \times \uu{C}_h \times S_h$  such that
% \begin{equation}
% \label{eq:picard_explicit_MD}
% \begin{split}
% A(\delta\uu{u}_h, \uu{v}) +O(\uu{u};\delta\uu{u}_h,\uu{v})+ B(\uu{v}, \delta p_h) & = R_u(\uu{u}_h,\uu{b}_h,p_h;\uu{v})\\[.1cm]
% B(\delta\uu{u}_h,q)&= R_p(\uu{u}_h;q), \\[.1cm]
% M(\delta \uu{b}_h,\uu{c})+
% D(\uu{c},\delta r_h)&= R_b(\uu{u}_h,\uu{b}_h,r_h;\uu{c}),\\[.1cm]
% D(\delta \uu{b}_h,s)&=R_r(\uu{b}_h;s),
% \end{split}
% \end{equation}
% where again $(\uu{v},q,\uu{c},s)\in\uu{V}_h\times Q_h\times\uu{C}_h\times S_h$ and $R_u$, $R_p$, $R_b$ and $R_r$ which are defined in section \ref{sec:nonlinear}. Again, let $({u},p,{b},r)$ be the coefficient vectors of $(\uu{u}_h,p_h,\uu{b}_h,r_h)$ and $(\delta{u},\delta p,\delta{b},\delta r)$ be the coefficient vectors of $(\delta \uu{u}_h,\delta p_h,\delta \uu{b}_h,\delta r_h)$, then this amounts to solving the linear system:
Then \eqref{eq:mhd_saddle} amounts
\begin{equation}
\label{eq:matrix_MD}
%\mathcal{K} x \equiv
\left(
\begin{array}{cccc}
A+O(u) & B^T & 0 & 0\\
B & 0 & 0 & 0 \\
0 & 0 & M & D^T\\
0 & 0 & D & 0
\end{array}
\right)
\,
\left(
\begin{array}{c}
\delta u\\
\delta p\\
\delta b\\
\delta r
\end{array}
\right)  =
\begin{pmatrix}
r_u \\
r_p\\
r_b\\
r_r
\end{pmatrix},
\end{equation}
with
\begin{align*}
r_u &= f- Au -O u - C^T b- B^T p,\\[0.1cm]
r_p &=-B u,\\[0.1cm]
r_b &=g-Mu+Cb-D^T r,\\[0.1cm]
r_r &=-D b.
\end{align*}
From \eqref{eq:matrix_MD} we can see that the system is now completely decoupled. This enable us to apply solve each individual subproblem separately and possibly in parallel.

\subsection{Complete decoupling}

For the second decoupling scheme, we again consider there to be weak coupling of the system but we also consider that the fluid equations are diffusion dominated and hence can exclude the convection terms.
% This is the simplest technique as it removes all non-linear terms. Again, for a given solution $(\uu{u}_h,p_h,\uu{b}_h,r_h)$, removing the coupling and convection terms in \eqref{eq:picard} results in solving for the updates $(\delta \uu{u}_h,\delta p_h,\delta \uu{b}_h,\delta r_h) \in \uu{V}_h \times Q_h \times \uu{C}_h \times S_h$  such that
% \begin{equation}
% \label{eq:picard_explicit_CD}
% \begin{split}
% A_h(\delta\uu{u}_h, \uu{v}) + B(\uu{v}, \delta p_h) & = R_u(\uu{u}_h,\uu{b}_h.p_h;\uu{v})\\[.1cm]
% B(\delta\uu{u}_h,q)&= R_p(\uu{u}_h;q), \\[.1cm]
% M(\delta \uu{b}_h,\uu{c})+
% D(\uu{c},\delta r_h)&= R_b(\uu{u}_h,\uu{b}_h,r_h;\uu{c}),\\[.1cm]
% D(\delta \uu{b}_h,s)&=R_r(\uu{b}_h;s),
% \end{split}
% \end{equation}
% where $(\uu{v},q,\uu{c},s)\in\uu{V}_h\times Q_h\times\uu{C}_h\times S_h$.  Taking $({u},p,{b},r)$ as the coefficient vectors of $(\uu{u}_h,p_h,\uu{b}_h,r_h)$ and $(\delta{u},\delta p,\delta{b},\delta r)$ be the coefficient vectors of $(\delta \uu{u}_h,\delta p_h,\delta \uu{b}_h,\delta r_h)$, then the proposed decoupled linear system is
This amounts to
\begin{equation}
\label{eq:matrix_CD}
%\mathcal{K} x \equiv
\left(
\begin{array}{cccc}
A & B^T & 0 & 0\\
B & 0 & 0 & 0 \\
0 & 0 & M & D^T\\
0 & 0 & D & 0
\end{array}
\right)
\,
\left(
\begin{array}{c}
\delta u\\
\delta p\\
\delta b\\
\delta r
\end{array}
\right)  =
\begin{pmatrix}
r_u \\
r_p\\
r_b\\
r_r
\end{pmatrix},
\end{equation}
with
\begin{align*}
r_u &= f- Au -O u - C^T b- B^T p,\\[0.1cm]
r_p &=-B u,\\[0.1cm]
r_b &=g-Mu+Cb-D^T r,\\[0.1cm]
r_r &=-D b.
\end{align*}
This is the simplest technique as it removes all non-linear terms and hence leaves the linear Stokes problem in the upper $(1,1)$ block matrix.

In this chapter we have introduced a mixed finite element approximation to the full MHD system given in \eqref{eq:mhd} and \eqref{eq:bc}. We followed the mixed approach outlined in \cite{schotzau2004mixed} and expressed the MHD system in the matrix form \eqref{eq:mhd_saddle}. Using the Picard iteration  \eqref{eq:mhd_saddle} we introduced two possible decoupling schemes which maybe simpler to solve for depending on the parameters ($\kappa$, $\nu$ and $\nu_m$). The next chapter will discuss possible preconditioning approaches to these systems.


\documentclass[11pt, oneside]{article}
\usepackage{geometry}
\geometry{letterpaper}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{natbib}
\usepackage{hyperref}
\hypersetup{colorlinks=true,linkcolor=blue,citecolor=blue,urlcolor=blue}
\usepackage{parskip}

\newcommand{\R}{\mathbb{R}}
\newcommand{\C}{\mathbb{C}}
\title{CSS-only quadrant rotations}
\author{}
\date{}

\begin{document}
\maketitle

\section{The first rotation}

We want to rotate a quadrant square of side length $L$ by $\pi/2$, translating it diagonally
as we go to avoid intersecting the crossbars.
As we do the rotation, the corner of the quadrant moves through an angle of $\pi/2$ from $-\pi/4$ to $\pi/4$,
requiring a shift of
\begin{align*}
m(t) &= \frac{L}{\sqrt{2}} \cos(\pi t/2 - \pi/4) d - \frac{L}{2} d = \cos(\pi t/2 - \pi/4) w + w_0
\end{align*}
as $t$ goes from 0 to 1, where $d = (\pm 1, \pm 1)$ is the appropriate diagonal vector.
In terms of the complex plain, the effect we want is the animated transform
\begin{align*}
f(t,z) &= w_0 + \cos(\pi t/2 - \pi/4) w + e^{i \pi t/2} z
\end{align*}
where $z \in \C$, $w \in \C$ is the maximum translation, and $t \in [0,1]$.  Let
\begin{align*}
e(t) &= \exp(2 \pi i t) \\
c(t) &= \cos(2 \pi t) \\
s(t) &= \sin(2 \pi t)
\end{align*}
so that
\begin{align*}
s(t) &= \frac{i}{2} (e(-t) - e(t)) \\
c(t) &= \frac{1}{2} (e(t) + e(-t))
\end{align*}
and $f(t,z)$ becomes
\begin{align*}
f(t,z) &= w_0 + c(t/4 - 1/8) w + e(t/4) z \\
  &= w_0 + (e(t/4 - 1/8) + e(-t/4 + 1/8)) w/2 + e(t/4) z \\
  &= w_0 + e(t/4)e(-1/8) w/2 + e(-t/4)e(1/8) w/2 + e(t/4) z \\
  &= w_0 + e(t/4)(e(-1/8) w/2 + e(-t/2)e(1/8) w/2 + z) \\
  &= w_0 + e(t/4)(e(-1/8) w/2 + e(-t/2) (e(1/8) w/2 + e(t/2) z))
\end{align*}
which is now expressible as primitive transforms:
\begin{align*}
f(t) &= T(w_0) R(t/4) T(e(-1/8) w/2) R(-t/2) T(e(1/8) w/2) R(t/2)
\end{align*}
where $R$, $T$ are rotate, translate.  If we set the CSS transform property to that sequence of 5 primitive
transforms, everything will animate correctly.

\section{The second rotation}

But wait!  If we extrapolate this into the next $\pi/2$, our translation will go in the wrong way.  To fix this, we
need to make our transition correction ping-pong back between $-\pi/4$ and $\pi/4$ without leaving that
interval:
\begin{align*}
f(t,z) &= w_0 + \cos(\pi/2 \operatorname{jag}(t) - \pi/4) w + e^{i \pi t/2} z \\
\operatorname{jag}(t) &= \min(\operatorname{rem}(t, 2), \operatorname{rem}(2-t,2))
\end{align*}
where $\operatorname{rem}$ is the (correctly signed) floating point remainder function.  Fortunately for us we
only need to evaluate $\operatorname{jag}$ at integers, where it has the simpler form
\begin{align*}
\operatorname{jag}(n) &= \operatorname{rem}(n, 2)
\end{align*}
Setting $j = \operatorname{jag}(t)$, we can repeat our derivation:
\begin{align*}
f(t,z) &= w_0 + c(j/4 - 1/8) w + e(t/4) z \\
  &= w_0 + (e(j/4 - 1/8) + e(-j/4 + 1/8)) w/2 + e(t/4) z \\
  &= w_0 + e(j/4)e(-1/8) w/2 + e(-j/4)e(1/8) w/2 + e(t/4) z \\
  &= w_0 + e(j/4)(e(-1/8) w/2 + e(-j/2)e(1/8) w/2 + e(t/4-j/4) z) \\
  &= w_0 + e(j/4)(e(-1/8) w/2 + e(-j/2) (e(1/8) w/2 + e(t/4+j/4) z)) \\
f(t) &= T(w_0) R(j/4) T(e(-1/8) w/2) R(-j/2) T(e(1/8) w/2) R(t/4+j/4)
\end{align*}

\end{document}


\renewcommand{\inl}{\textsf{inl}\;}
\renewcommand{\inr}{\textsf{inr}\;}
\newcommand{\caseof}[3]{\textsf{case}\,#1\,\textsf{of inl}\,x \Rightarrow #2 \mid \textsf{inr}\,y \Rightarrow #3}

\section{Extension: Disjoint sums}

We will now extend our simply-typed lambda-calculus to disjoint sums.

\[
\begin{array}{llcl}
\mbox{Types}  & A & \bnfas & \ldots \mid A + B\\
\mbox{Terms}  & M & \bnfas & \ldots \mid \inl M \mid \inr M \mid \caseof{M}{N_1}{N_2}
\end{array}
\]

Let us first extend our definition of $\SN$ and $\SNe$ (see Fig.~\ref{fig:sncase}).

\begin{figure}
 \centering

\[
\begin{array}{c}
\multicolumn{1}{l}{\mbox{Neutral terms}} \\% [0.5em]
\infer{\Gamma \vdash \caseof{M}{N_1}{N_2} : C \in \SNe}{\Gamma \vdash M : A + B \in \SNe & \Gamma, x{:}A \vdash N_1 : C \in \SN & \Gamma, y{:}B \vdash N_2 : C\in \SN}
\\% [0.5em]
%
\multicolumn{1}{l}{\mbox{Normal terms}} \\% [0.5em]
\infer{\Gamma \vdash \inl M : A + B \in \SN}{\Gamma \vdash M : A \in \SN} \qquad \infer{\Gamma \vdash \inr M : A + B \in \SN}{\Gamma \vdash M : B \in \SN}
\\% [0.5em]
\multicolumn{1}{l}{\mbox{Strong head reduction}} \\[1em]
\infer{\Gamma \vdash \caseof{(\inl M)}{N_1}{N_2} \redSN [M/x]N_1 : C}{\Gamma \vdash M : A \in \SN & \Gamma, y{:}B \vdash N_2 : C \in \SN}
\\[0.75em]
\infer{\Gamma \vdash \caseof{(\inr M)}{N_1}{N_2} \redSN [M/x]N_2 : C}{\Gamma \vdash M : B \in \SN & \Gamma, x{:}A \vdash N_1 : C \in \SN}
\\[0.75em]
\infer{\Gamma \vdash \caseof{M}{N_1}{N_2} \redSN \caseof{M'}{N_1}{N_2} : C}{\Gamma \vdash M \redSN M' : A + B}
\end{array}
\]

   \caption{Inductive definition of strongly normalizing terms - extended for case-expressions and injections}
   \label{fig:sncase}
 \end{figure}

Next, we extend our definition of semantic type to disjoint sums. A first attempt might be to define $\denot{A + B}$ as follows:

\paragraph{Attempt 1}
\begin{alignat*}{3}
\inden{\Gamma}{M}{A + B} ~\text{iff} &&M = \inl M' &~\text{and}~ \inden{\Gamma}{M'}{A}, \\
& ~~\emph{or}~ &M = \inr M' &~\text{and}~ \inden{\Gamma}{M'}{B}.
% \den{A+B} := \{\inl M \mid M \in \den{A} \} \cup \{\inr M \mid M \in \den{B} \}
\end{alignat*}

However, this definition would not satisfy the key property $\CR3$ and hence would fail to be a reducibility candidate. For example,  while we have $\inden{y : A}{\inl y}{A + B}$, it is not the case that $\inden{y : A}{(\lambda x. \inl x)\;y}{A + B}$, despite the fact that $(\lambda x. \inl x)\;y \redSN \inl y$.
\\[1em]
Our definition of $\denot{A + B}$ is not closed under the reduction relation $\redSN$. Let $\A$ denote the denotation of $\denot{A}$. We then define the closure of $\denot{A} = \A$, written as  $\clos\A$, inductively as follows:

%\[
%\begin{array}{c}
%\ianc{M \in \A}{M\in \clos\A}{}  \qquad
%\ianc{M \in \SNe}{M \in \clos\A}{} \qquad
%\ibnc{M \in \clos\A}{N \redSN M}{N \in \clos\A}{}
%\end{array}
%\]

\[
\begin{array}{c}
\ianc{\Gamma \vdash M \in \A}{\Gamma \vdash M \in \clos\A}{} \qquad
\ianc{\Gamma \vdash M : A \in \SNe}{\Gamma \vdash M \in \clos\A}{} \qquad
\ibnc{\Gamma \vdash M : \clos\A}{\Gamma \vdash N \redSN M : A}{\Gamma \vdash N : \clos\A}{}
\end{array}
\]

and we define

\[
\begin{array}{lcl}
\inden{\Gamma}{M}{A + B} & \text{iff} & \Gamma \vdash M \in \clos{ \{\inl M' \mid \inden{\Gamma}{M'}{A} \} \cup \{\inr M' \mid \inden{\Gamma}{M'}{B} \}  }
\end{array}
\]

\subsection{Semantic type $\denot{A + B}$ is a reducibility candidate}
We first extend our previous theorem which states that all denotations of types must be in $\CR$.

\begin{theorem}
For all types $C$, $\denot{C}  \in \CR$.
\end{theorem}
\begin{proof}
By induction on the structure of $C$. We highlight the case for disjoint sums.

\paragraph{Case $C = A + B$.}

  \begin{enumerate}
  \item \textit{Show} $\CR1$. Assume that $\inden{\Gamma}{M}{A + B}$. We consider different subcases and prove by an induction on the closure defining $\denot{A + B}$ that $\Gamma \vdash M : A + B \in \SN$.

\paragraph{Subcase:} $\Gamma \vdash M \in \{\inl N \mid \inden{\Gamma}{N}{A}\}$. Therefore $M = \inl N$. Since $\inden{\Gamma}{N}{A}$ and by i.h. ($\CR1$), $\Gamma \vdash N : A \in \SN$. By definition of $\SN$, we have that $\Gamma \vdash \inl N : A + B \in \SN$.

\paragraph{Subcase:} $\Gamma \vdash M \in \{\inr N \mid \inden{\Gamma}{N}{B}\}$. Therefore $M = \inr N$. Since $\inden{\Gamma}{N}{B}$ and by i.h. ($\CR1$), $\Gamma \vdash N : B \in \SN$. By definition of $\SN$, we have that $\Gamma \vdash \inr N : A + B \in \SN$.

\paragraph{Subcase:} $\Gamma \vdash M : A + B \in \SNe$. By definition of $\SN$, we conclude that $\Gamma \vdash M : A + B \in \SN$.

\paragraph{Subcase:} $\Gamma \vdash M \redSN M' : A + B$ and $\inden{\Gamma}{M'}{A+B}$.
\\[0.5em]
$\Gamma \vdash M \redSN M' : A + B$ and $\inden{\Gamma}{M'}{A + B}$ \hfill by assumption\\
$\Gamma \vdash M' : A + B \in \SN$ \hfill by inner i.h. \\
$\Gamma \vdash M : A + B \in \SN$ \hfill by reduction $\redSN$

 \item \textit{Show} $\CR2$. If $\Gamma \vdash M : A + B \in \SNe$, then $\inden{\Gamma}{M}{A + B}$. \\
By definition of the closure, if $\Gamma \vdash M : A + B \in \SNe$, we have $\inden{\Gamma}{M}{A + B}$.

  \item \textit{Show} $\CR3$. If $\Gamma \vdash M \redSN M' : A + B$ and $\inden{\Gamma}{M'}{A+B}$
    then $\inden{\Gamma}{M}{A+B}$.\\
By definition of the closure, if $\Gamma \vdash M \redSN M' : A + B$ and $\inden{\Gamma}{M'}{A+B}$, we have
$\inden{\Gamma}{M}{A+B}$.
\end{enumerate}

\end{proof}

 \subsection{Revisiting the fundamental lemma}

 We can now revisit the fundamental lemma.

 \begin{lemma}[Fundamental lemma]
 If $\Gamma \vdash M : C$ and $\inden{\Gamma'}{\sigma}{\Gamma}$
 then $\inden{\Gamma'}{[\sigma]M}{C}$.
 \end{lemma}
 \begin{proof}
 By induction on $\Gamma \vdash M : C$.

 \paragraph{Case} $\D = \ianc{\Gamma \vdash M \hastype A}{\Gamma \vdash \inl M \hastype A + B}{}$
 \\[1em]
 $\inden{\Gamma'}{\sigma}{\Gamma}$ \hfill by assumption \\
 $\inden{\Gamma'}{[\sigma]M}{A}$ \hfill by i.h. \\
 $\inden{\Gamma'}{\inl [\sigma]M}{A + B}$ \hfill by definition of $\denot{A + B}$ \\
 $\inden{\Gamma'}{[\sigma] \inl M}{A + B}$ \hfill by subst. definition

 \paragraph{Case} $\D = \ianc{\Gamma \vdash M \hastype B}{\Gamma \vdash \inr M \hastype A + B}{}$
 \\[1em]
 similar to the case above.

 \paragraph{Case} $\D = \icnc{\Gamma \vdash M : A + B}{\Gamma, x{:}A \vdash M_1 :  C}{\Gamma, y{:}B \vdash M_2 : C}
 {\Gamma \vdash \caseof{M}{M_1}{M_2} : C}{}$
 \\[1em]
 $\inden{\Gamma'}{\sigma}{\Gamma}$ \hfill by assumption \\
 $\inden{\Gamma'}{[\sigma]M}{A + B}$ \hfill by i.h.
 \\[1em]
 We consider different subcases and prove by induction on the closure defining $\denot{A + B}$, that $\inden{\Gamma'}{[\sigma](\caseof{M}{M_1}{M_2})}{C}$.

\paragraph{Subcase $\Gamma' \vdash [\sigma]M \in \{\inl N \mid \inden{\Gamma'}{N}{A}\}$}$\;$\\[1em]
$[\sigma]M = \inl N$ for some $\inden{\Gamma'}{N}{A}$ \hfill by assumption \\
$\Gamma' \vdash N : A \in \SN$ \hfill by $\CR1$ \\
$\Gamma' \vdash \inl N : A + B \in \SN$ \hfill by definition \\
% $x \in \den{A}$,
$\inden{\Gamma'}{\sigma}{\Gamma}$ \hfill by assumption \\
$\inden{\Gamma'}{[\sigma, N/x]}{\Gamma, x:A}$ \hfill by definition \\
$\inden{\Gamma'}{[\sigma, N/x]M_1}{C}$ \hfill by outer i.h \\
$\inden{\Gamma', y{:}B}{y}{B}$  \hfill by definition \\
$\inden{\Gamma', y{:}B}{[\sigma, y/y]}{\Gamma, y:B}$ \hfill by definition \\
$\inden{\Gamma', y{:}B}{[\sigma, y/y]M_2}{C}$ \hfill by outer i.h. \\
$\Gamma', y{:}B \vdash [\sigma, y/y]M_2 : C \in \SN$ \hfill by $\CR1$ \\
$\Gamma' \vdash \caseof{(\inl N)}{[\sigma,x/x]M_1}{[\sigma, y/y]M_2} \redSN [\sigma, N/x]M_1 : C$ \hfill by $\redSN$\\
$\caseof{(\inl N)}{[\sigma,x/x]M_1}{[\sigma, y/y]M_2}$ \\
$\qquad = [\sigma](\caseof{M}{M_1}{M_2}) $ \hfill by subst. definition and $[\sigma]M = \inl N$\\
$\inden{\Gamma'}{[\sigma](\caseof{M}{M_1}{M_2})}{C}$ \hfill by $\CR3$

\paragraph{Subcase $\Gamma' \vdash [\sigma]M \in \{\inr N \mid \inden{\Gamma'}{N}{B}\}$}$\;$\\[1em]
similar to the case above.

\paragraph{Subcase: $\Gamma' \vdash [\sigma]M : A + B \in \SNe$}.$\;$\\
$\inden{\Gamma'}{\sigma}{\Gamma}$ \hfill by assumption \\
$\inden{\Gamma', x{:}A}{x}{A}$ \hfill by definition \\
$\inden{\Gamma', y{:}B}{y}{B}$  \hfill by definition \\
$\inden{\Gamma', x{:}A}{[\sigma, x/x]}{\Gamma, x:A}$ \hfill by definition \\
$\inden{\Gamma', y{:}B}{[\sigma, y/y]}{\Gamma, y:B}$ \hfill by definition \\
$\inden{\Gamma', x{:}A}{[\sigma, x/x]M_1}{C}$ \hfill by outer i.h. \\
$\inden{\Gamma', y{:}B}{[\sigma, y/y]M_2}{C}$ \hfill by outer i.h. \\
$\Gamma', x{:}A \vdash [\sigma, x/x]M_1 : C \in \SN$ \hfill by $\CR1$ \\
$\Gamma', y{:}B \vdash [\sigma, y/y]M_2 : C \in \SN$ \hfill by $\CR1$ \\
$\Gamma' \vdash \caseof{[\sigma]M}{[\sigma, x/x]M_1}{[\sigma, y/y]M_2} : C \in \SNe$ \hfill by $\SNe$ \\
$\Gamma' \vdash [\sigma](\caseof{M}{M_1}{M_2}) : C \in \SNe$ \hfill by substitution def. \\
$\inden{\Gamma'}{[\sigma](\caseof{M}{M_1}{M_2})}{C}$ \hfill by $\CR2$

\paragraph{Subcase: $\Gamma' \vdash [\sigma]M \redSN M' : A + B$ and $\inden{\Gamma'}{M'}{A+B}$}$\;$\\
$\Gamma' \vdash [\sigma]M \redSN M' : A + B$ and $\inden{\Gamma'}{M'}{A+B}$ \hfill by assumption \\
$\inden{\Gamma'}{\caseof{M'}{[\sigma,x/x]M_1}{[\sigma,y/y]M_2}}{C}$ \hfill by inner i.h. \\
$\Gamma' \vdash \caseof{[\sigma]M}{[\sigma,x/x]M_1}{[\sigma,y/y]M_2} $ \\
$\qquad \qquad\redSN
\caseof{M'}{[\sigma,x/x]M_1}{[\sigma,y/y]M_2} : C$ \hfill by $\redSN$\\
$\inden{\Gamma'}{[\sigma](\caseof{M}{M_1}{M_2})}{C}$ \hfill by $\CR3$

\end{proof}

 \section{Extension: Recursion}
 \newcommand{\zero}{\mathsf{z}}
 % \renewcommand{\suc}{\textsf{s}\;}
 % \newcommand{\recnat}[3]{\textsf{rec} (#1,\,#2,\; #3)}
 \newcommand{\recnat}[3]{\recmatch{}{#1}{#2}{#3}}
 % \renewcommand{\nat}{\textsf{Nat}}
 We now extend our simply-typed lambda-calculus to include natural numbers
 defined by $\zero$ and $\suc t$ as well as a primitive recursion operator
 written as $\recnat{M}{M_z}{M_s}$ where $M$ is the argument we recurse over,
 $M_z$ describes the branch taken if $M = \zero$ and $M_s$ describes the branch
 taken when $M = \suc N$ where $n$ will be instantiated with $N$ and $f\;n$
 describes the recursive call.

\[
\begin{array}{llcl}
\mbox{Types}  & A & \bnfas & \ldots \mid \nat \\
\mbox{Terms}  & t & \bnfas & \ldots \mid \zero \mid \suc t \mid \recnat{t}{t_z}{t_s}
\end{array}
\]

To clarify, we give the typing rules for the additional constructs.

\[
\begin{array}{c}
\infer{\Gamma \vdash \zero : \nat}{} \qquad
\infer{\Gamma \vdash \suc M : \nat}{\Gamma \vdash M : \nat}
\\[1em]
\infer{\Gamma \vdash \recnat M {M_z} {M_s} : C}{
\Gamma \vdash M : \nat & \Gamma \vdash M_z : C &
\Gamma, n:\nat,\;f\,n:C \vdash M_s : C}
\end{array}
\]

We again extend our definition of $\SN$ and $\SNe$.

\[
\begin{array}{c}
\multicolumn{1}{l}{\mbox{Neutral terms}} \\[1em]
\infer{\Gamma \vdash \recnat{M}{M_z}{M_s} : C \in \SNe}{\Gamma \vdash M : \nat \in \SNe & \Gamma \vdash M_z : C \in \SN & \Gamma, n \hastype \nat, f~n \hastype C \vdash M_s : C \in \SN}\\[1em]
\multicolumn{1}{l}{\mbox{Normal terms}} \\[1em]
\infer{\Gamma \vdash \zero : \nat \in \SN}{} \qquad \infer{\Gamma \vdash \suc M : \nat \in \SN}{\Gamma \vdash M : \nat \in \SN}\\[1em]
\multicolumn{1}{l}{\mbox{Strong head reduction}} \\[1em]
\infer{\Gamma \vdash \recnat{\zero}{M_z}{M_s} \redSN M_z : C}{\Gamma, n \hastype \nat, f~n \hastype C \vdash M_s : C \in SN} \\[1em]
\infer{\recnat{(\suc N)}{M_z}{M_s} \redSN [N/n,\, f_r/f\,n]M_s : C}{
  \Gamma \vdash N : \nat \in \SN & \Gamma \vdash M_z : C \in \SN & \Gamma, n \hastype \nat, f~n \hastype C \vdash M_s : C \in SN & f_r = \recnat{N}{M_z}{M_s}} \\[1em]
\infer{\Gamma \vdash \recnat{M}{M_z}{M_s} \redSN \recnat{M'}{M_z}{M_s} : C}{\Gamma \vdash M \redSN M' : \nat}
\end{array}
\]

 \section{Extension: Natural numbers}
Here we add natural numbers to our language and show how the language remains normalizing.
\subsection{Semantic type $\denot{\nat}$} We define the denotation of $\nat$ as
 follows:

% \[
% \den{\nat} := \clos{\{\zero\}\cup \{\suc M \mid M \in \den{\nat}\} }
% \]

	\[
	\begin{array}{lcl}
	\inden{\Gamma}{M}{\nat} & \text{iff} & \Gamma \vdash M \in \clos{ \{\zero\}\cup \{\suc M' \mid \inden{\Gamma}{M'}{\nat}\} }
	\end{array}
	\]

\subsection{Semantic type $\denot{\nat}$ is a reducibility candidate}
We again extend our previous theorem which states that all denotations of types must be in $\CR$.

\begin{theorem}
For all types $C$, $\denot{C}  \in \CR$.
\end{theorem}
\begin{proof}
By induction on the structure of $C$. We highlight the case for $\nat$.

\paragraph{Case $C = \nat$.}

\begin{enumerate}
\item \textit{Show} $\CR1$. Assume $\inden{\Gamma}{M}{\nat}$. We consider different subcases
  and prove by induction on the closure defining $\denot{\nat}$ that $\Gamma \vdash M : \nat \in \SN$.
%

\paragraph{Subcase:} $M = \zero$. By definition of $\SN$, $\Gamma \vdash \zero : \nat \in \SN$.

\paragraph{Subcase:} $M = \suc N$ where $\inden{\Gamma}{N}{\nat}$.  By i.h. ($\CR1$),
$\Gamma \vdash N : \nat \in \SN$. By definition of $\SN$, $\Gamma \vdash \suc N : \nat \in \SN$.

 \paragraph{Subcase:} $\Gamma \vdash M  : \nat \in \SNe$. By definition of $\SN$, $\Gamma \vdash M : \nat \in \SN$.

 \paragraph{Subcase:} $\Gamma \vdash M \redSN M' : \nat$ and $\inden{\Gamma}{M'}{\nat}$.
 \\
 $\Gamma \vdash M \redSN M' : \nat$ and $\inden{\Gamma}{M'}{\nat}$ \hfill by assumption \\
 $\Gamma \vdash M' : \nat \in \SN$ \hfill by inner i.h. \\
 $\Gamma \vdash M  : \nat \in \SN$ \hfill by reduction $\redSN$

 \item \textit{Show} $\CR2$. If $\Gamma \vdash M : \nat \in \SNe$, then $\inden{\Gamma}{M}{\nat}.$ \\
	 By definition of the closure, if $\Gamma \vdash M : \nat \in \SNe$, then $\inden{\Gamma}{M}{\nat}$.

 \item \textit{Show} If $\Gamma \vdash M \redSN M' : \nat$ and $\inden{\Gamma}{M'}{\nat}$, then $\inden{\Gamma}{M}{\nat}$. \\
   By definition of the closure, if $\Gamma \vdash M \redSN M' : \nat$ and $\inden{\Gamma}{M'}{\nat}$, we have $\inden{\Gamma}{M}{\nat}$.
 \end{enumerate}
\end{proof}

 \subsection{Revisiting the fundamental lemma}

 We can now revisit the fundamental lemma.
 \begin{lemma}[Fundamental lemma]
 If $\Gamma \vdash M : C$ and $\inden{\Gamma'}{\sigma}{\Gamma}$
 then $\inden{\Gamma'}{[\sigma]M}{C}$.
 \end{lemma}
 \begin{proof}
 By induction on $\Gamma \vdash M : C$.

 \paragraph{Case} $\D = \ianc{}{\Gamma \vdash \zero : \nat}{}$
 \\
 $[\sigma]\zero = \zero$ \hfill by subst. def \\
 $\inden{\Gamma'}{\zero}{\nat}$ \hfill by definition.

 \paragraph{Case} $\D = \ianc{\Gamma \vdash M : \nat}{\Gamma \vdash \suc M :  \nat}{}$
 \\
 $\inden{\Gamma'}{\sigma}{\Gamma}$ \hfill by assumption \\
 $\inden{\Gamma'}{[\sigma]M}{\nat}$ \hfill by i.h. \\
 $\inden{\Gamma'}{\suc [\sigma]M}{\nat}$ \hfill by definition \\
 $\inden{\Gamma'}{[\sigma] \suc M}{\nat}$ \hfill by subst. def

 \paragraph{Case} $\D = \icnc
 {\Gamma \vdash M : \nat}
 {\Gamma \vdash M_z : C}
 {\Gamma, n:\nat,\,f~n:C \vdash M_s : C}
 {\Gamma \vdash \recnat M {M_z} {M_s} : C}{}$
 \\
 $\inden{\Gamma'}{\sigma}{\Gamma}$ \hfill by assumption \\
 $\inden{\Gamma'}{[\sigma]M}{\nat}$ \hfill by i.h. \\[1em]
 We distinguish cases based on $\inden{\Gamma'}{[\sigma]M}{\nat}$ and prove by induction on $\inden{\Gamma'}{[\sigma]M}{\nat}$ that $\inden{\Gamma'}{[\sigma](\recnat M {M_z} {M_s})}{C}$.

 \paragraph{Subcase } $[\sigma]M = \zero$.
 \\
 $\inden{\Gamma', n \hastype \nat, f~n \hastype C}{n}{\nat}$ \hfill by definition of $\SNe$, $\denot{\nat}$ \\
 $\inden{\Gamma', n \hastype \nat, f~n \hastype C}{f~n}{C}$ \hfill by definition of $\SNe$, $\denot{\nat}$ \\
 $\inden{\Gamma', n \hastype \nat, f~n \hastype C}{[\sigma, n/n, f~n/f~n]}{\Gamma, n:\nat, f~n:C}$ \hfill by definition \\
 $\inden{\Gamma', n \hastype \nat, f~n \hastype C}{[\sigma, n/n, f~n/f~n]M_s}{C}$ \hfill by outer i.h. \\
 $\Gamma', n \hastype \nat, f~n \hastype C \vdash [\sigma, n/n, f~n/f~n]M_s : C \in \SN$ \hfill by $\CR1$ \\
 $\inden{\Gamma'}{[\sigma]M_z}{C}$ \hfill by outer i.h. \\
 $\Gamma' \vdash \recnat \zero {[\sigma]M_z} {[\sigma, n/n,f~n/f~n]M_s} \redSN [\sigma]M_z : C$
 \hfill by $\redSN$ \\
 $\recnat \zero {[\sigma]M_z} {[\sigma, n/n,f~n/f~n]M_s}$ \\
 $\qquad = [\sigma](\recnat{M}
 {M_z} {M_s})$ \hfill by subst. def. and $[\sigma]M = \zero$\\
 $\inden{\Gamma'}{[\sigma](\recnat{M} {M_z}{M_s}}{C}$ \hfill by $\CR3$.

 \paragraph{Subcase } $[\sigma]M = \suc M'$ where $\inden{\Gamma'}{M'}{\nat}$.
 \\
 $\inden{\Gamma'}{M'}{\nat}$ \hfill by assumption \\
 $\Gamma \vdash M' : \nat \in \SN$ \hfill by $\CR1$ \\
 $\inden{\Gamma'}{[\sigma]M_z}{C}$ \hfill by outer i.h. \\
 $\Gamma' \vdash [\sigma]M_z \in \SN$ \hfill by $\CR1$ \\
 $\inden{\Gamma', n \hastype \nat, f~n \hastype C}{[\sigma, n/n, f~n/f~n]}{\Gamma, n:\nat, f~n:C}$ \hfill by definition \\
 $\inden{\Gamma', n \hastype \nat, f~n \hastype C}{[\sigma, n/n, f~n/f~n]M_s}{C}$ \hfill by outer i.h. \\
 $\Gamma', n \hastype \nat, f~n \hastype C \vdash [\sigma, n/n, f~n/f~n]M_s : C \in \SN$ \hfill by $\CR1$ \\
 $\inden{\Gamma'}{\recnat{M'}{[\sigma]M_z}{[\sigma, n/n, f~n/f~n]M_s}}{C}$ \hfill by inner i.h. \\
 $\inden{\Gamma'}{[\sigma, M'/n, (\recnat{M'}{[\sigma]M_z}{[\sigma, n/n, f~n/f~n]M_s})/f~n]}{\Gamma, n:\nat, f~n:C}$ \\
 $~$ \hfill by definition \\
 $\inden{\Gamma'}{[\sigma, M'/n, (\recnat{M'}{[\sigma]M_z}{[\sigma, n/n, f~n/f~n]M_s})/f~n]M_s}{C}$ \\
 $~$ \hfill by outer i.h. \\
 $\Gamma' \vdash \recnat{(\suc M')}{[\sigma]M_z}{[\sigma, n/n, f~n/f~n]M_s} $ \\
 $\qquad \qquad \redSN
 [\sigma, M'/n, (\recnat{M'}{[\sigma]M_z}{[\sigma, n/n, f~n/f~n]M_s})/f~n]M_s : C$ \\
 $~$ \hfill by $\redSN$ \\
 $\inden{\Gamma'}{[\sigma](\recnat{M}{M_z}{M_s})}{C}$ \hfill by $\CR3$.

 \paragraph{Subcase } $\Gamma' \vdash [\sigma]M : \nat \in \SNe$. \\
 $\inden{\Gamma'}{[\sigma]M_z}{C}$ \hfill by outer i.h.\\
 $\Gamma' \vdash [\sigma]M_z : C \in \SN$ \hfill by $\CR1$\\
 $\inden{\Gamma', n \hastype \nat, f~n \hastype C}{[\sigma, n/n, f~n/f~n]}{\Gamma, n:\nat, f~n:C}$ \hfill by definition \\
 $\inden{\Gamma', n \hastype \nat, f~n \hastype C}{[\sigma, n/n, f~n/f~n]M_s}{C}$ \hfill by outer i.h. \\
 $\Gamma', n \hastype \nat, f~n \hastype C \vdash [\sigma, n/n, f~n/f~n]M_s : C\in \SN$ \hfill by $\CR1$ \\
 $\Gamma \vdash \recnat{[\sigma]M}{[\sigma]M_z}{[\sigma, n/n, f~n/f~n]M_s} : C \in \SNe$ \hfill by $\SNe$\\
 $\Gamma' \vdash [\sigma](\recnat{M}{M_z}{M_s}) : C \in \SNe$ \hfill by subst. def. \\
 $\inden{\Gamma'}{[\sigma](\recnat{M}{M_z}{M_s})}{C}$ \hfill by $\CR2$.

 \paragraph{Subcase } $\Gamma' \vdash [\sigma]M \redSN M' : \nat$ and $\inden{\Gamma'}{M'}{\nat}$.\\
 $\Gamma' \vdash [\sigma]M \redSN M' : \nat$ and $\inden{\Gamma'}{M'}{\nat}$ \hfill by assumption.\\
 $\inden{\Gamma'}{\recnat{M'}{[\sigma]M_z}{[\sigma,n/n, f~n/f~n]M_s}}{C}$ \hfill by inner i.h. \\
 $\Gamma' \vdash \recnat{[\sigma]M}{[\sigma]M_z}{[\sigma,n/n, f~n/f~n]M_s}$ \\
 $\qquad \qquad \redSN \recnat{M'}{[\sigma]M_z}{[\sigma,n/n, f~n/f~n]M_s} : C$ \hfill by $\redSN$\\
 $\inden{\Gamma'}{[\sigma](\recnat{M}{M_z}{M_s})}{C}$ \hfill by $\CR3$.

 \end{proof}

%  \section{Exercises}
%  \begin{problem}
% % \begin{exercise}
%  The Def. \ref{def:norm}, defines strong normalization informally. We can replace this definition with a more formal definition.

%  \begin{definition}[Inductive definition of strongly normalizing terms] A term $M$ is strongly normalizing, if all its reducts are strongly normalizing, i.e.
%  $M \csn$ if for all $M'$, if $M \red M'$ then $M' \in \csn$.
%  \end{definition}

%  This definition gives rise to an induction principle to reason about strongly normalizing terms. To prove $\forall M \csn. P(M)$, we can assume the property holds for $P(M')$ for any $M'$ s.t. $M \red M'$. Using this induction principle, we can now prove  constructively that any subterm of a strongly normalizing term is itself normalizing.

%  Before we however, precisely define our notion of subterm to simplify our reasoning. We write $M = C[N]$ for $N$ is a subterm of $M$; $C$ denotes an evaluation context, i.e. the term $M$ where we identify $N$ as a subterm at a given position. Evaluation contexts can  be defined as follows.

%  \[
%  \begin{array}{lcl}
%  \mbox{Evaluation Context}\;C & \bnfas & [ ] \mid \lambda x. C \mid C\;N \mid M\;C
%  \end{array}
%  \]

%  As an alternative to the congruence rules, we can redefine evaluation using evaluation contexts:

%  \[
%  \ianc{M \red N}{C[M] \red C[N]}{}
%  \]

% \begin{theorem}
% Any subterm of a strongly normalizing term is strongly normalizing itself, i.e. if $M \csn$ and $N$ is a subterm of $M$, i.e. $M = C[N]$ then $N \in \csn$.
% \end{theorem}
% \end{exercise}
% \end{problem}

% \begin{problem}
%   \begin{exercise}
% Extend the semantic strong normalization proof to treat $A \times B$.
% \begin{itemize}
% \item Extend our definition of normal and neutral terms (i.e. $\SN$ and $\SNe$). You might also need to extend our definition of strong head reduction (i.e. $\redSN$).
% \item Define an appropriate denotation of $\den{A \times B}$.
% % \den{A * B} := {M | \fst(M) \in \den{A} or \snd{M} \in \den{B}}
% \item Show that $\den{A \times B}$ is a reducibility candidate.
% \item Show the additional cases in the fundamental lemma.
% \end{itemize}
%   \end{exercise}
% \end{problem}


\chapter{Algorithms for Parametric Runs}
\label{sec:algParRun}

The here described algorithms for
parametric runs can be used to determine how sensitive a function is
with respect to a change in the independent variables.
They can also be used to do a parametric
sweep of a function over a set of parameters.
The algorithm described in Section~\ref{sec:algParametric} varies one parameter at a time while holding all other parameters fixed at the value
specified by the keyword \texttt{Ini}. The algorithm described in Section~\ref{sec:algParRunGen}, in contrast, constructs a mesh in the space of the independent parameters, and evaluates the objective function at each mesh point.

\section{Parametric Runs by Single Variation}
\lab{sec:algParametric}
\subsection{Algorithm Description}

The \texttt{Parametric} algorithm allows doing parametric runs where
one parameter at a time is varied and
all other parameters are fixed at their initial values
(specified by the keyword \texttt{Ini}).

Each parameter must have a lower and upper bound.
For the logarithmic scale, the lower and upper bounds must be bigger than zero.
To allow negative increments, the lower bound can be larger than the upper bound.
The absolute value of the keyword \texttt{Step} defines in how many intervals
each coordinate axis will be divided.
If $\text{\texttt{Step}} < 0$, then the spacing is logarithmic; otherwise it is linear. Set $\text{\texttt{Step}} = 0$ to keep the parameter always fixed at the value
specified by \texttt{Ini}.

This algorithm can also be used with discrete parameters. This allows, for example, using a string to specify a window construction.
\\

The spacing is computed as follows:
For simplicity, the explanation is done for one parameter.
Let $l \triangleq \text{\texttt{Min}}$, $u \triangleq \text{\texttt{Max}}$ and $m \triangleq |\text{\texttt{Step}}|$,
where \texttt{Min}, \texttt{Max} and \texttt{Step} are specified in the command file.\\

\noindent
If $\text{\texttt{Step}} < 0$, we compute, for $i \in \{0, \ldots , m \}$,
\begin{subequations}
  \begin{eqnarray}
    p & = & \frac{1}{m} \, \log \frac{u}{l}, \\
    x_i & = & l \, 10^{p \, i}.
  \end{eqnarray}
If $\text{\texttt{Step}} > 0$, we compute, for $i \in \{0, \ldots , m \}$,
  \begin{equation}
    x_i = l + \frac{i}{m} \, (u-l).
  \label{eq:AlgParLinSpa}
  \end{equation}
  \label{subeq:AlgParSpa}
\end{subequations}

\begin{example}[Parametric run with logarithmic and linear spacing]
{\em
Suppose the parameter specification is of the form
\vspace{-0.5\baselineskip}
\begin{lstlisting}
Vary{
   Parameter{ Name = x1; Ini = 5; Step = -2; Min = 10; Max = 1000; }
   Parameter{ Name = x2; Ini = 3; Step = 1;  Min = 2;  Max = 20;   }
}
\end{lstlisting}
\vspace{-0.5\baselineskip}
and the cost function takes two arguments, $x_1, x_2 \in \Re$.
Then, the cost function will be evaluated at the points\\
\noindent $(x_1, x_2) \in \{ (10,3), \ (100,3), \ (1000,3), \ (5,2), \ (5,20) \}$.\rbox \\
}
\end{example}

\subsection{Keywords}
For this algorithm, the command file (see page~\pageref{par:comFil}) can contain continuous and discrete parameters.\\

The \texttt{Parametric} algorithm is invoked by the following specification in the command file:
\begin{lstlisting}
Algorithm{
   Main = Parametric;
   StopAtError = true | false;
}
\end{lstlisting}

\noindent The keywords have the following meaning:
\begin{codedescription}
\item[Main]
The name of the main algorithm.
\item[StopAtError]
If \texttt{true}, then the parametric run stops if a simulation error occurs.
If \texttt{false}, then the parametric run does not stop if a simulation error occurs.
The failed function evaluation will be assigned the function value zero.
For information, an error message will be written to the user interface
and the optimization log file.
\end{codedescription}

% ===================================
\section{Parametric Runs on a Mesh}
\label{sec:algParRunGen}
\subsection{Algorithm Description}

In contrast to the algorithm \texttt{Parametric}, the algorithm \texttt{Mesh} spans a multi-dimensional grid in the space of the independent parameters, and it evaluates the objective function at each grid point.

Note that the number of function evaluations increases exponentially with the number
of independent parameters.
For example, a $5$-dimensional grid with $2$ intervals in each dimension requires $3^5=243$ function evaluations, whereas a $10$-dimensional grid would require $3^{10}=59049$ function evaluations.\\

The values that each parameter can take on are computed in the same way
as for the algorithm \texttt{Parametric}. Therefore, the specification of a
\texttt{Parameter} underlies the same constraints as for the algorithm \texttt{Parametric}, which is described above.

\begin{example}[Parametric run on a mesh]~\\
{\em
Suppose the parameter specification is of the form
\vspace{-0.5\baselineskip}
\begin{lstlisting}
Vary{
  Parameter{ Name = x1; Min = -10; Ini = 99; Max = 10; Step = 1; }
  Parameter{ Name = x2; Min = 1; Ini = 99; Max = 100; Step = -2; }
}
\end{lstlisting}
\vspace{-0.5\baselineskip}
and the cost function takes two arguments, $x_1, x_2 \in \Re$.
Then, the cost function will be evaluated at the points\\
$(x_1, x_2) \in
\{(-10,\,  1)$,
$ ( 10, \, 1)$,
$ (-10, \,  10)$,
$ ( 10, \,  10)$,
$ (-10, \, 100)$,
$ ( 10, \, 100)\}$.

An alternative specification for $x_2$ that uses a discrete parameter and gives the same result is
\vspace{-0.5\baselineskip}
\begin{lstlisting}
Parameter{
  Name = x2;
  Ini = "1";
  Values = "1, 10, 100";
}
\end{lstlisting}
\vspace{-\baselineskip}
\rbox
}
\end{example}

\subsection{Keywords}

The \texttt{Mesh} algorithm is invoked by the following specification in the command file:
\begin{lstlisting}
Algorithm{
   Main        = Mesh;
   StopAtError = true | false;
}
\end{lstlisting}

\noindent The keywords have the following meaning:
\begin{codedescription}
\item[Main]
The name of the main algorithm.
\item[StopAtError]
If \texttt{true}, then the parametric run stops if a simulation error occurs.
If \texttt{false}, then the parametric run does not stop if a simulation error occurs.
The failed function evaluation will be assigned the function value zero.
For information, an error message will be written to the user interface
and the optimization log file.
\end{codedescription}

\label{sec:algImpEnd}


\section{Typed realizability}
\label{sec:typed-realizability}

RZ is based on \emph{typed realizability} by John
Longley~\cite{Longley99}.   This variant of realizability corresponds most
directly to programmers' intuition about implementations.

We approach typed realizability and its relationship to
real-world programming by way of example. Suppose we are asked to
design a data structure for the set $\mathcal{G}$ of all finite
simple%
\iflong
\footnote{Simple means at most one arrow between any two vertices.}
\fi % \iflong
\ directed graphs with vertices labeled by distinct integers.
%
\iflong
An exemplar
directed graph~$G$ is shown in Figure~\ref{fig:digraph}.
%
\begin{figure}
  \centering
  \includegraphics[width=0.3\textwidth]{digraph}
  \caption{A finite directed graph $G$}
  \label{fig:digraph}
\end{figure}
\fi % \iflong
%
One common representation of such graphs uses a pair of lists $(\ell_V, \ell_A)$, where
$\ell_V$ is the list of vertex labels and $\ell_A$ is the \emph{adjacency list}
representing the arrows by pairing the labels of each source and target.
\iflong
For the above graph $G$,
$\ell_V = [1; 2;
3; 4]$ and $\ell_A = [(1,2); (2,2); (2,3); (3,2); (3;1)]$.
\fi % \iflong
%
Thus we define the datatype of graphs as\footnote{We use OCaml
  notation in which $\clist{t}$ classifies finite lists of elements of
  type~$t$, and $t_1 * t_2$ classifies pairs containing a value of
  type $t_1$ and a value of type $t_2$.}
%
\begin{equation*}
  \ctype \mathtt{graph} = \clist{\cint} \ \ * \ \ \clist{(\cint * \cint)}
\end{equation*}
%
However, this is not a complete description of the intended representation, as
there are representation invariants and conditions not expressed by the type, e.g.,
%
\iflong
\begin{enumerate}
\item The order in which vertices and arrows are listed is not
  important%
; for example, $[1;2;3;4]$ and $[4;1;2;3]$ represent the same vertices.
\item Each vertex and arrow must be listed exactly once.
\item The source and target of each arrow must appear in the list of vertices.
\end{enumerate}
\else % \iflong
the order in which vertices and arrows are listed is not
important, each vertex and arrow must be listed exactly once, and
the source and target of each arrow must appear in the list of vertices.
\fi %\iflong

%
Thus, to implement the mathematical set~$\mathcal{G}$, we must not
only decide on the underlying datatype $\mathtt{graph}$, but also
determine what values of that type represent which elements
of~$\mathcal{G}$. As we shall see next, this can be expressed either
using a \emph{realizability relation} or a \emph{partial equivalence
  relation (per)}.

\subsection{Modest sets and pers}
\label{sec:modest-sets-pers}

\iflong
We now define typed realizability as it
applies to OCaml. Other general-purpose programming languages could be
used instead, as long as they provide the usual ground types, product
and function types.\footnote{It is also convenient to work with a
language that supports sum types, as this allows a more natural
representation of disjoint unions.}
\else
We now define typed realizability as it
applies to OCaml. Other general-purpose programming languages could be
used instead.
\fi % \iflong

Let $\Type$ be the collection of all (non-parametric) OCaml types. To
each type $t \in \Type$ we assign the set $\values{t}$ of values of
type~$t$ that behave \emph{functionally} in the sense of
Longley~\cite{longley99when}. Such values are represented by
terminating expressions that do not throw exceptions or return
different results on different invocations. They may \emph{use}
exceptions, store, and other computational effects, provided they
appear functional from the outside; a useful example using
computational effects is presented in
Section~\ref{sec:we-show-modulus-of-continuity-example}. A functional
value of function type may diverge as soon as it is applied. The
collection $\Type$ with the assignment of functional values
$\values{t}$ to each $t \in \Type$ forms a \emph{typed partial
  combinatory algebra (TPCA)}%
\iflong
, which provides a theoretical basis for
the definition of a realizability model that suits our needs%
\fi%\iflong
.

Going back to our example, we see that an implementation of directed
graphs $\mathcal{G}$ specifies a datatype $\typeOf{\mathcal{G}} =
\mathtt{graph}$ together with a \emph{realizability relation}
$\rz_{\mathcal{G}}$ between $\mathcal{G}$ and
$\values{\mathtt{graph}}$. The meaning of $(\ell_V, \ell_A)
\rz_\mathcal{G} G$ is ``OCaml value $(\ell_V, \ell_A)$
represents/realizes/implements graph $G$''.
%
\iflong
%
There are two natural
conditions that $\rz_\mathcal{G}$ ought to satisfy: (1) for every $G
\in \mathcal{G}$ there should be at least one realizer $(\ell_V,
\ell_A)$ representing it, and (2) if $(\ell_V, \ell_A)$ represents
both $G$ and $G'$ then $G = G'$.\footnote{The latter condition is
  called \emph{modesty} and is not strictly necessary for the
  development of the theory, though programmers would naturally expect
  it to hold.} If $(\ell_V, \ell_A)$ and $(\ell'_V, \ell'_A)$
represent the same graph (e.g., because $\ell_V$ is a permutation of
$\ell'_V$, and similarly for $\ell_A$ and $\ell'_A$) we say that they
are \emph{equivalent} and write $(\ell_V, \ell_A) \per_\mathcal{G}
(\ell'_V, \ell'_A)$. The relation $\per_\mathcal{G}$ is a
\emph{partial} equivalence relation (symmetric and transitive, but not
reflexive) because not every $(\ell_V, \ell_A) \in
\values{\mathtt{graph}}$ represents a graph.

\smallskip

A general definition is in order. A \emph{modest set} is
%
\else % iflong
%
Generalizing from this, we define a \emph{modest set} to be
%
\fi
%
a triple $A = (\setOf{A}, \typeOf{A}, {\rz_A})$ where $\setOf{A}$ is
the \emph{underlying set}, $\typeOf{A} \in \Type$ is the
\emph{underlying type}, and $\rz_A$ is a \emph{realizability relation}
between $\values{\typeOf{A}}$ and $\setOf{A}$, satisfying
%
\iflong
%
\begin{enumerate}
\item \emph{totality:} for every $x \in \setOf{A}$ there is $v \in
  \values{\typeOf{A}}$ such that $v \rz_A x$, and
\item \emph{modesty:} if $u \rz_A x$ and $u \rz_A y$ then $x = y$.
\end{enumerate}
%
\else % iflong
%
(1) \emph{totality:} for every $x \in \setOf{A}$ there is $v \in
\values{\typeOf{A}}$ such that $v \rz_A x$, and (2) \emph{modesty:} if
$u \rz_A x$ and $u \rz_A y$ then $x = y$.
%
\fi % iflong
%
The \emph{support} of $A$ is the set $\support{A} = \set{v \in
  \values{\typeOf{A}} \such \xsome{x}{\setOf{A}}{v \rz_A x}}$ of those
values that realize something. We define the relation $\per_A$ on
$\values{\typeOf{A}}$ by
%
\begin{equation*}
  u \per_A v
  \iff
  \some{x}{\setOf{A}}{u \rz_A x \land v \rz_A x} \;.
\end{equation*}
%
From totality and modesty of $\rz_A$ it follows that $\per_A$ is a per,
i.e., symmetric and transitive. Observe that $\support{A} = \set{v \in
  \values{\typeOf{A}} \such v \per_A v}$, whence $\per_A$
restricted to $\support{A}$ is an equivalence relation. In fact, we
may recover a modest set up to isomorphism from $\typeOf{A}$ and
$\per_A$ by taking $\setOf{A}$ to be the set of equivalence classes of
$\per_A$, and $v \rz_A x$ to mean $v \in x$.

The two views of implementations, as modest sets $(\setOf{A},
\typeOf{A}, {\rz_A})$, and as pers $(\typeOf{A}, {\per_A})$, are
equivalent.\footnote{And there is a third view, as a partial surjection
  $\delta_A : {\subseteq}\values{\typeOf{A}} \epito \setOf{A}$, with
  $\delta_A(v) = x$ when $v \rz_A x$. This is how realizability is
  presented in Type Two Effectivity~\cite{Wei00}.}
%
We concentrate on the view of modest sets as pers. They are more
convenient to use in RZ because they refer only to types and values,
as opposed to arbitrary sets.
%
Nevertheless, it is useful to understand
how modest sets and pers arise from natural programming practice.

\iflong
%
Modest sets form a category whose objects are modest sets and
morphisms are the realized functions. A \emph{realized function} $f :
A \to B$ is a function $f : \setOf{A} \to \setOf{B}$ for which there
exists $v \in \values{\typeOf{A} \to \typeOf{B}}$ such that, for all
$x \in \setOf{A}$ and $u \in \typeOf{A}$,
%
\begin{equation}
  \label{eq:rz-function-space}
  u \rz_A x \implies v\;u \rz_B f(x) \;.
\end{equation}
%
This condition is just a mathematical expression of the usual idea
that~$v$ is an implementation of~$f$ if it does to realizers
what~$f$ does to the elements they represent.
\fi

\iflong
The equivalent category of pers has as objects
\else
Pers form a category whose objects are
\fi
%
pairs $A = (\typeOf{A}, {\per_A})$ where $\typeOf{A} \in \Type$ and
$\per_A$ is a per on $\values{\typeOf{A}}$. A morphism $A \to B$ is
represented by a function $v\in \values{\typeOf{A} \to \typeOf{B}}$
such that, for all $u, u' \in \support{A}$,
%
\iflong
\begin{equation}
  \label{eq:per-exponential}
  u \per_A u' \implies v\;u \per_B v\;u' \;.
\end{equation}
%
Values $v$ and $v'$ that both satisfy~\eqref{eq:per-exponential}
\else % \iflong
$u \per_A u' \implies v\;u \per_B v\;u'$.  Two such functions $v$ and $v'$
\fi
represent the same morphism if, for all $u, u' \in \support{A}$,
$u \per_A u'$ implies $v\;u \per_B v'\;u'$.

\iflong
%
The category of pers has a very rich structure. For example, we may
form a cartesian product $A \times B$ of pers $A$ and $B$ by
%
\begin{align*}
  \typeOf{A \times B} &= \typeOf{A} * \typeOf{B},\\
  (u_1, v_1) \per_{A \times B} (u_2, v_2) &\iff
  u_1 \per_A u_2 \land v_1 \per_B v_2.
\end{align*}
%
The projections $\pi_1 : A \times B \to A$ and $\pi_2 : A \times B \to
B$ are realized by $\mathtt{fst}$ and $\mathtt{snd}$, respectively.

The morphisms between pers~$A$ and~$B$ again form a per
$B^A$, also written as $A \to B$, called the \emph{exponential} of~$A$
and~$B$, with
%
\begin{align*}
  \typeOf{B^A} &= \typeOf{A} \to \typeOf{B},\\
  \support{B^A} &=
  \set{v \in \values{\typeOf{A} \to \typeOf{B}} \such
    \all{u,u'}{\values{\typeOf{A}}}{u \per_A u' \implies v\,u \per_B v\,u'}}
  \\
  u \per_{B^A} v &\iff u, v \in \support{A} \land
  \xall{w}{\support{A}}{u\;w \per_B v\;w}.
\end{align*}
%
The evaluation map $e : B^A \times A \to B$ is realized by OCaml
application, $\cfun{(u,v)}{u\;v}$. If a function $f : C \times A \to
B$ is realized by $v$, then its transpose $\tilde{f} : C \to B^A$,
$\tilde{f}(z)(x) = f(z,x)$, is realized by $\cfun{z\;x}{v \; (z,x)}$.
This shows that the category of pers is cartesian closed. In
Section~\ref{sec:transl-sets-terms} we review other canonical
constructions on modest sets.

\bigskip
\else % \iflong
%
The category of pers has a very rich structure, namely that of a
regular locally cartesian closed category~\cite{Bauer:00}. This
suffices for the interpretation of first-order logic and (extensional)
dependent types~\cite{JacobsB:cltt}.
%
\fi % \iflong

\iflong
%
As an example we consider the cyclic group on seven elements $(\ZZ_7,
0, {-}, {+})$. To implement the group, we must give a representation
of $\ZZ_7$ as a per~$Z = (\typeOf{Z}, {\per_Z})$, and
provide realizers for the neutral element~$0$, negation~$-$, and
addition~$+$.

One possibility is to choose $\cint$ as the underlying type
$\typeOf{Z}$, and to let $\support{Z}$ be only the integers \texttt{0}
through \texttt{6}. Then negation and addition must work modulo
\texttt{7} (i.e., must return an integer in the range
\texttt{0}--\texttt{6} when given integers in this range). The neutral
element would be the integer constant \texttt{0}, and the equivalence
$\per_Z$ would be integer equality.

Alternatively, we could take $\cint$ as the underlying type
$\typeOf{Z}$, but let $\support{Z}$ include all values of type $\cint$. In this
case, negation and addition could be simply integer addition and
negation\footnote{Taking care to prevent integer
  overflow.}. Here the neutral element could be implemented as any
integer multiple of \texttt{7}, and the equivalence $\per_Z$ would be
equivalence-modulo-7.

Both of these pers happen to be \emph{decidable}, i.e., it can be
algorithmically decided whether two values in $\support{Z}$ represent the same element
of~$\ZZ_7$.\fi % \iflong

\iflong
%
Not all pers are decidable.
%
\else
%
Not all pers are \emph{decidable}, i.e., there may be no algorithm for deciding
when two values are equivalent.
%
\fi
%
Examples include implementations of semigroups with an undecidable
word problem~\cite{post47:_recur_unsol_probl_thue}%
\iflong, implementations of computable sets of integers (which might
be realized by membership functions of type $\cint\to\cbool$),\fi
\ and implementations of computable real numbers (which might be
realized by infinite Cauchy sequences).
%
\iflong
%
There is no presupposition that pers are computable
(implementable). We can require decidable equivalence by adding a
suitable axiom; see Section~\ref{sec:decidable-sets}. \fi

\subsection{Interpretation of logic}
\label{sec:interpretation-logic}

In the realizability interpretation of logic, each formula~$\phi$ is
assigned a set of \emph{realizers}, which can be thought of as
computations that witness the validity of~$\phi$. The situation is
somewhat similar, but not equivalent, to the propositions-as-types
translation of logic into type theory, where proofs of a
proposition correspond to terms of the corresponding type. More
precisely, to each formula~$\phi$ we assign an underlying type
$\typeOf{\phi}$ of realizers, but unlike the propositions-as-types
translation, not all terms of type $\typeOf{\phi}$ are necessarily
valid realizers for~$\phi$, and some terms that are realizers may not
correspond to any proofs (for example, if they denote partial
functions or use computational effects).

It is customary to write $t \rz \phi$ when $t \in
\values{\typeOf{\phi}}$ is a realizer for~$\phi$. The underlying types
and the realizability relation~$\rz$ are defined inductively on the
structure of~$\phi$; an outline is shown in Figure~\ref{fig:rz-logic}.
We say that a formula~$\phi$ is \emph{valid} if it has at least one
realizer.
%
\begin{figure*}[t]
  \textbf{Underlying types of realizers:}
\[
  \begin{array}{ll@{\qquad}ll}
    \typeOf{\itrue} &= \mathtt{unit} &
    \typeOf{\ifalse} &= \mathtt{unit} \\
    \typeOf{\iequal{x}{y}} &= \mathtt{unit} &
    \typeOf{\iand{\phi}{\psi}} &= \oprod{\typeOf{\phi}}{\typeOf{\psi}} \\
    \typeOf{\iimply{\phi}{\psi}} &= \oarrow{\typeOf{\phi}}{\typeOf{\psi}} &
    \typeOf{\ior{\phi}{\psi}} &=
    \osumtyx{\mathtt{or}_0}{\typeOf{\phi_0}}{\mathtt{or}_1}{\typeOf{\phi_1}} \\
    \typeOf{\iforall{x}{A}{\phi}} &= \oarrow{\typeOf{A}}{\typeOf{\phi}} &
    \typeOf{\iexists{x}{A}{\phi}} &= \oprod{\typeOf{A}}{\typeOf{\phi}}
  \end{array}
\]
  \textbf{Realizers:}
\[
  \begin{array}{ll}
    () \rz \itrue & \\
    () \rz \iequal{x}{y}
    &\quad\text{iff}\quad
    x = y
    \\
    (t_1,t_2) \rz \iand{\phi}{\psi}
    &\quad\text{iff}\quad
    \text{$t_1 \rz \phi$ and $t_2 \rz \psi$}
    \\
    t \rz \iimply{\phi}{\psi}
    &\quad\text{iff}\quad
    \text{for all $u \in \typeOf{\phi}$, if $u \rz \phi$ then $t\,u
      \rz \psi$}
    \\
    \oinj{\mathtt{or}_0}{t} \rz \ior{\phi}{\psi}
    &\quad\text{iff}\quad
    \text{$t \rz \phi$}
    \\
    \oinj{\mathtt{or}_1}{t} \rz \ior{\phi}{\psi}
    &\quad\text{iff}\quad
    \text{$t \rz \psi$}
    \\
    t \rz \iforall{x}{A}{\phi(x)}
    &\quad\text{iff}\quad
    \text{for all $u \in \typeOf{A}$, if $u \rz_A x$ then $t\,u \rz \phi(x)$}
    \\
    (t_1, t_2) \rz \iexists{x}{A}{\phi(x)}
    &\quad\text{iff}\quad
    \text{$t_1 \rz_A x$ and $t_2 \rz \phi(x)$}
  \end{array}
\]
\vspace{-0.5cm}
  \caption{Realizability interpretation of logic (outline)}
  \label{fig:rz-logic}
\end{figure*}

In classical mathematics, a predicate on a set~$X$ may be viewed as a
subset of~$X$ or a (possibly non-computable) function $X \to \oProp$,
where $\oProp = \set{\ofalse, \otrue}$ is the set of truth values.
Accordingly, since in realizability propositions are witnessed by
realizers,
%
\iflong
a predicate~$\phi$ on a modest set~$A$ may be viewed as a
subset of $\setOf{A} \times \values{\typeOf{\phi}}$, or a (possibly
non-computable) function $\setOf{A} \times \values{\typeOf{\phi}} \to
\set{\ofalse, \otrue}$. In terms of pers, which is what RZ uses,
\fi
%
a predicate~$\phi$ on a per~$A = (\typeOf{A}, {\per_A})$ is a
(possibly non-computable) function $\phi : \values{\typeOf{A}} \times
\values{\typeOf{\phi}} \to \oProp$ that is
%
\iflong
\begin{itemize}
\item \emph{strict:} if $\phi(u,v)$ then $u \in \support{A}$, and
\item \emph{extensional:} if $\phi(u_1,v)$ and $u_1 \per_A u_2$ then
  $\phi(u_2,v)$.
\end{itemize}
\else % \iflong
\emph{strict} (if $\phi(u,v)$ then $u \in \support{A}$)
and \emph{extensional} (if $\phi(u_1,v)$ and $u_1 \per_A u_2$ then
  $\phi(u_2,v)$).
\fi

\iflong
We illustrate how the realizability interpretation extracts the
computational content of a proposition. To make an interesting
example, suppose
\else % \iflong
Suppose
\fi % \iflong
we have implemented the real
numbers~$\RR$ as a per~$R = (\mathtt{real}, {\per_R})$, and
consider
\iflong
the statement
that every cubic $x^3 + a x + b$ has a root,
%
\begin{equation}
  \label{eq:square-root}%
  \iforall{a}{R}{\iforall{b}{R}{\iexists{x}{R}{\iequal{x^3 + a x + b}{0}}}}.
\end{equation}
\else % \iflong
$\iforall{a}{R}{\iforall{b}{R}{\iexists{x}{R}{\iequal{x^3 + a x + b}{0}}}}$.
\fi % \iflong
%
By computing according to Figure~\ref{fig:rz-logic}, we see that
a realizer for this proposition is a value~$r$ of type
$\oarrow{\mathtt{real}}{\oarrow{\mathtt{real}}{\oprod{\mathtt{real}}{\mathtt{unit}}}}$
such that, if $t$ realizes $a \in \RR$ and $u$ realizes $b \in
\RR$, then $\oapp{\oapp{r}{t}}{u} = (v, w)$ with $v$ realizing a real
number~$x$ such that $x^3 + a x + b = 0$, and with $w$ being trivial. (This
can be ``thinned'' to a realizer of type
$\oarrow{\mathtt{real}}{\oarrow{\mathtt{real}}{\mathtt{real}}}$ that
does not bother to compute~$w$.) In essence, the realizer~$r$
computes a root of the cubic equation. Note
that $r$ is \emph{not} extensional, i.e., different realizers~$t$
and~$u$ for the same~$a$ and~$b$ may result in different roots.
To put this in another way, $r$ realizes a \emph{multi-valued}
function\footnote{The multi-valued nature of the realizer comes from
  the fact that it computes \emph{any one} of many values, not that it
  computes \emph{all} of the many values.} rather than a per
morphism. It is well known in computable mathematics that certain
operations, such as equation solving, are only computable if we allow
them to be multi-valued. They arise naturally in RZ as translations of
$\forall\exists$~statements.

\iflong
There are propositions whose realizers are ``irrelevant'' or free of
computational content. For example, realizers for $\itrue$ and
equality have type $\ounit$. Another example is a negation
$\inot{\phi}$, which is defined to be the same as
$\iimply{\phi}{\ifalse}$, whose realizers have type
$\oarrow{\typeOf{\phi}}{\ounit}$. Such realizers do not compute
anything useful, and we may as well throw them away. Sometimes only a
part of a realizer is computationally irrelevant, as we saw in the
last example. Propositions that are free of computational content
are characterized as the \emph{$\lnot\lnot$-stable propositions}. A
proposition~$\phi$ is said to be $\lnot\lnot$-stable, or just
\emph{stable} for short, when $\iimply{\inot{\inot{\phi}}}{\phi}$ is
valid. Any \emph{negative} proposition, i.e., one built from $\itrue$,
$\ifalse$, $=$, $\land$, $\Rightarrow$ and $\forall$ is stable, but
there may be other propositions that are stable and are not written
in the negative form.

It would be unproductive to bother the programmer with requirements
for useless code.
%
\else
%
Some propositions, such as equality and negation, have ``irrelevant'' realizers
free of computational content. Sometimes only a
part of a realizer is computationally irrelevant.
Propositions that are free of computational content are
characterized as the \emph{$\lnot\lnot$-stable propositions}. A
proposition~$\phi$ is said to be $\lnot\lnot$-stable, or just
\emph{stable} for short, when $\iimply{\inot{\inot{\phi}}}{\phi}$ is
valid.
%
\fi
%
On input, one can specify whether abstract predicates
have computational content. On output, extracted realizers
go through a \emph{thinning} phase, which removes
irrelevant realizers.

\iflong
\subsection{Uniform families of modest sets}
\fi % \iflong
\label{sec:uniform-families}

Many structures are naturally viewed as families of sets, or sets
depending on parameters, or \emph{dependent types} as they are called
in type theory. For example, the $n$-dimensional Euclidean space
$\RR^n$ depends on the dimension $n \in \NN$, the Banach space
$\mathcal{C}([a,b])$ of uniformly continuous real functions on the
closed interval $[a,b]$ depends on $a, b \in \RR$ such that $a < b$,
etc. In general, a family of sets $\family{A_i}{i \in I}$ is an
assignment of a set $A_i$ to each $i \in I$ from an \emph{index
  set}~$I$.

\iflong
In the category of modest sets the appropriate notion is that of a
\emph{uniform} family~$\family{A_i}{i \in I}$, which is an assignment
of a modest set $A_i = (\setOf{A_i}, \typeOf{A}, {\rz_{A_i}})$ to each
$i \in \setOf{I}$, where $I$ is an index modest
set~\cite[6.3]{JacobsB:cltt}. The uniformity comes from the
requirement that all the~$A_i$'s share the same underlying
type~$\typeOf{A_i} = \typeOf{A}$. It is a desirable restriction from
the implementation point of view, because it removes dependencies at
the level of types. Note also that there is no dependency on the
realizers, only on the elements of the underlying set.

We may express uniform families in terms of pers, too.
\else
In the category of pers the appropriate notion is that of a
\emph{uniform} family.
%
\fi
A uniform family of pers $\family{A_i}{i \in I}$ indexed
by a per~$I$ is given by an underlying type $\typeOf{A}$ and a family
of pers $(\per_{A_i})_{i \in \values{\typeOf{I}}}$ that is
%
\iflong
\begin{itemize}
\item \emph{strict:} if $u \per_{A_i} v$ then $i \in \support{I}$, and
\item \emph{extensional:} if $u \per_{A_i} v$ and $i \per_I j$ then $u
  \per_{A_j} v$.
\end{itemize}
\else % \iflong
strict (if $u \per_{A_i} v$ then $i \in \support{I}$) and
extensional (if $u \per_{A_i} v$ and $i \per_I j$ then $u
  \per_{A_j} v$).
\fi % \iflong

\iflong
We may form the \emph{sum} $\depsum{i \in I}{A_i}$ of a uniform family
$\family{A_i}{i \in I}$ as
%
\begin{align*}
  \typeOf{\depsum{i \in I}{A_i}} &=
  \typeOf{I} \times \typeOf{A}
  \\
  (i_1, u_1) \per_{\depsum{i \in I}{A_i}} (i_2, u_2)
  &\iff
  i_1 \per_I i_2 \land u_1 \per_{A_{i_1}} u_2
\end{align*}
%
and the \emph{product} $\depprod{i \in I}{A_i}$ as
%
\begin{align*}
  \typeOf{\depprod{i \in I}{A_i}} &=
  \typeOf{I} \to \typeOf{A}
  \\
  \support{\depprod{i \in I}{A_i}} &=
  \set{v \in \values{\typeOf{I} \to \typeOf{A}} \such
    \all{i, j}{\values{\typeOf{I}}}{i \per_{I} j \implies v\,i
      \per_{A_i} v\,j}
    } \\
  u \per_{\depprod{i \in I}{A_i}} v
  &\iff
  u, v \in \support{\depprod{i \in I}{A_i}} \land
  \all{i,j}{\values{\typeOf{I}}}{
    i \per_I j \implies u\;i \per_{A_i} v\;j
  }.
\end{align*}
%
These constructions allow us to interpret (extensional) dependent type
theory in the category of modest sets.
\else % \iflong
We can also form the \emph{sum} $\depsum{i \in I}{A_i}$ or
\emph{product} $\depprod{i \in I}{A_i}$ of
a uniform family, allowing an interpretation of (extensional) dependent type
theory.
\fi % \iflong

\iflong
As an example of a uniform family we consider the cyclic group
$(\ZZ_n, 0, {-}, {+})$ of order~$n$. To keep things simple, we assume
that~$n$ ranges over natural numbers that can be represented by
type~$\cint$ (i.e., $\typeOf{N}=\cint$), and that $\per_N$ is equality.
%
The uniform family $\family{Z_n}{n \in N}$ is then like the cyclic
group of order~$7$, with $7$ replaced by~$n$. Ignoring overflow, the
underlying type would be $\typeOf{Z_n} = \cint$. Any of the
implementations suggested for $\ZZ_7$ would work here, with~$7$
replaced by the parameter~$n$; in one case we would have $u \per_{Z_n}
v \iff u = v$ and in the other $u \per_{Z_n} v \iff u
\mathbin{\mathrm{mod}} n = v \mathbin{\mathrm{mod}} n$.
%
Negation would be specified as a constant of dependent type
$\Pi_{n \in N} Z_n \to Z_n$. Its realizer \texttt{neg} would then have
type $\typeOf{N}\to\typeOf{Z_n}\to{\typeOf{Z_n}}$, i.e.,
$\cint\to\cint\to\cint$, so that $\mathtt{neg}(n)$ would be a realizer
for negation on $\ZZ_n$. The realizer for addition would similarly
take an extra argument $n$.

\fi % \iflong

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "cie-long"
%%% End: