%!TEX root = forallx-ubc.tex
\chapter{Entailment and Models for SL}
\label{ch.SLmodels}

This chapter offers a formal semantics for SL, allowing us to be more precise about the notion of truth in SL. We'll also highlight some important features of \emph{entailment}, a key concept in formal logic.

A formal, logical language is built from two kinds of elements: logical symbols and non-logical symbols. Connectives like `\eand' and `\eif' are logical symbols, because their meaning is specified within the formal language. When writing a symbolization key, you are not allowed to change the meaning of the logical symbols. You cannot say, for instance, that the `\enot' symbol will mean `not' in one argument and `perhaps' in another. The `\enot' symbol always means logical negation. It is used to translate the English language word `not', but it is a symbol of a formal language and is defined by its truth conditions.

The sentence letters in SL are non-logical symbols, because their meaning is not defined by the logical structure of SL. When we translate an argument from English to SL, for example, the sentence letter $M$ does not have its meaning fixed in advance; instead, we provide a symbolization key that says how $M$ should be interpreted in that argument.

In translating from English to a formal language, we provided symbolization keys which were interpretations of all the non-logical symbols we used in the translation. An \define{interpretation} gives a meaning to all the non-logical elements of the language. We'll also use the term `\define{model}' as another word for an interpretation. In our simple formal language SL, meaning is simply a matter of truth and falsity, relative to a given interpretation. These notions too need to be characterized in a formal way.

%
%
%When we gave definitions for a sentence of SL and for a sentence of QL, we distinguished between the \define{object language} and the \define{metalanguage}. The object language is the language that we are \emph{talking about}: either SL or QL. The metalanguage is the language that we use to talk about the object language: English, supplemented with some mathematical jargon. It will be important to keep this distinction in mind.

%\nix{box about Tarski? The logician Alfred Tarksi introduced this distinction ca.~1940. Tarski argued that the truth conditions for a language could never be expressed in the language itself --- the metalanguage needed to be more powerful than the object language. So it's simply not possible to give a definition of truth for SL that is itself a sentence of SL --- describing the semantics of SL requires a more powerful language.}

\section{Semantics for SL}
\label{sec.semanticsSL}

This section provides a rigorous, formal characterization of \emph{truth in SL} which builds on what we already know from doing truth tables. We were able to use truth tables to reliably test whether a sentence was a tautology in SL, whether two sentences were equivalent, whether an argument was valid, and so on. For instance: \metaA{} is a tautology in SL if and only if it is assigned `1' on every line of a complete truth table.

This worked because each line of a truth table corresponds to a way the world might be. We considered all the possible combinations of 1s and 0s for the sentence letters that made a difference to the sentences we cared about. The truth table allowed us to determine what would happen given these different combinations. An \emph{interpretation} in SL provides a truth value to each atomic sentence in use; and every combination of truth values is represented by an interpretation.

(Technical side note: one might be tempted to \emph{identify} interpretations with assignments of truth values to atomic sentences. (In past versions of this book, that's actually what I did.) For a variety of reasons, this is not (any longer) my preference. One reason is that there can be different ways to assign truth values to atomic sentences, corresponding to one and the same row of the truth table, if one includes values for atoms that are not mentioned in the truth table. In a truth table for a sentence that doesn't include an $R$, for instance, these two assignments of truth values to atoms are effectively equivalent, and correspond to the same row of the truth table: \{$P=1$, $Q=1$, $R=1$\}, \{$P=1$, $Q=1$, $R=0$\}. So there could be different interpretations corresponding to the same row of the truth table, but an interpretation \emph{determines} a row of a truth table.

A more complex motivation for my terminological choice here has to do with the relationship between SL and QL, a more complex language we'll learn later in the textbook. I'll return to this connection in \S\ref{sec.0PlaceModels}.)

Once we construct a truth table, the symbols `1' and `0' are divorced from their metalinguistic meaning of `true' and `false'. We interpret `1' as meaning `true', but the formal properties of 1 are defined by the characteristic truth tables for the various connectives.  The symbols in a truth table have a formal meaning that we can specify entirely in terms of how the connectives operate. For example, if $A$ is value 1, then $\enot A$ is value 0.

To formally define truth in SL, then, we want a function that assigns, for each interpretation, a 1 or 0 to each of the sentences of SL. We can interpret this function as a definition of truth for SL if it assigns 1 to all of the true sentences of SL and 0 to all of the false sentences of SL. Call this function `$v$' (for `valuation'). We want $v$ to be a function such that for any sentence \metaA{}, $v(\metaA{})=1$ if \metaA{} is true and $v(\metaA{})=0$ if \metaA{} is false.

Recall that the recursive definition of a wff for SL had two stages: The first step said that atomic sentences (solitary sentence letters) are wffs. The second stage allowed for wffs to be constructed out of more basic wffs. There were clauses of the definition for all of the sentential connectives. For example, if \metaA{} is a wff, then \enot\metaA{} is a wff.

Our strategy for defining the truth function, $v$, will also be in two steps. The first step will handle truth for atomic sentences; the second step will handle truth for compound sentences.

\section{Defining truth in SL}
How can we define truth for an atomic sentence of SL? Consider, for example, the sentence $M$. Without an interpretation, we cannot say whether $M$ is true or false. It might mean anything. If we use $M$ to symbolize `The moon orbits the Earth', then $M$ is true. If we use $M$ to symbolize `The moon is a giant turnip', then $M$ is false.

When we give a symbolization key for SL, we provide a translation into English of the sentence letters that we use. In this way, the interpretation specifies what each of the sentence letters \emph{means}. However, this is not enough to determine whether or not that sentence is true. The sentences about the moon, for instance, require that you know some rudimentary astronomy. Imagine a small child who became convinced that the moon is a giant turnip. She could understand what the sentence `The moon is a giant turnip' means, but mistakenly think that it was true.

So a symbolization key alone does not determine whether a sentence is true or false. Truth or falsity depends also on what the world is like. If $M$ meant `The moon is a giant turnip' and the real moon were a giant turnip, then $M$ would be true. To get a truth value via the symbolization key, one has to first translate the sentence into English, and then rely on one's knowledge of what the world is like.

We want a logical system that can proceed without astronomical investigation. Moreover, we want to abstract away from the specific commitments of a given symbolization key. So our logical definition of truth will proceed in a different way. We ignore any proffered symbolization key, and take, from a given interpretation, a \emph{truth value assignment}. Formally, this is just a function that tells us the truth value of all the atomic sentences. Call this function `$a$' (for `assignment'). We define $a$ for all sentence letters \script{P}, such that
\begin{displaymath}
a(\script{P}) =
\left\{
	\begin{array}{ll}
	1 & \mbox{if \script{P} is true},\\
	0 & \mbox{otherwise.}
	\end{array}
\right.
\end{displaymath}
This means that $a$ takes any atomic sentence of SL and assigns it either a one or a zero; one if the sentence is true, zero if the sentence is false.

You can think of $a$ as being like a row of a truth table. Whereas a truth table row assigns a truth value to a few atomic sentences, the truth value assignment assigns a value to every atomic sentence of SL. There are infinitely many sentence letters, and the truth value assignment gives a value to each of them. When constructing a truth table, we only care about sentence letters that affect the truth value of sentences that interest us. As such, we ignore the rest.

It is important to note that the truth value assignment, $a$, is not part of the language SL. Rather, it is part of the mathematical machinery that we are using to describe SL. It encodes which atomic sentences are true and which are false.

We now define the truth function, $v$, using the same recursive structure that we used to define a wff of SL.

\begin{enumerate}
\item If \metaA{} is a sentence letter, then $v(\metaA{})=a(\metaA{})$.
%\setcounter{Example}{\arabic{enumi}}\end{enumerate}
%...
% Break out of the {enumerate} environment to say something about what is
% going on. Using \setcounter in this way preserves the numbering, so
% that the list can resume after the comments.

%This is a mathematical equals sign, not the identity predicate we defined for QL.

% Resume the {enumerate} environment and restore the counter.
%...
%\begin{enumerate}\setcounter{enumi}{\arabic{Example}}
\item If \metaA{} is ${\enot}\metaB{}$ for some sentence \metaB{}, then
\begin{displaymath}v(\metaA{}) =
	\left\{\begin{array}{ll}
	1 & \mbox{if $v(\metaB{}) = 0$},\\
	0 & \mbox{otherwise.}
	\end{array}\right.
\end{displaymath}

\item If \metaA{} is $(\metaB{}\eand\metaC{})$ for some sentences \metaB{}, \metaC{}, then
\begin{displaymath}v(\metaA{}) =
	\left\{\begin{array}{ll}
	1 & \mbox{if $v(\metaB{}) = 1$ and $v(\metaC{}) = 1$,}\\
	0 & \mbox{otherwise.}
	\end{array}\right.
\end{displaymath}
\setcounter{Example}{\arabic{enumi}}\end{enumerate}
%...
\label{truthdefinition}
You may be tempted to worry that this definition is circular, because it uses the word `and' in trying to define `and.' But remember, we are not attempting to give a definition of the English word `and'; we are giving a definition of truth for sentences of SL containing the logical symbol `\eand.' We define truth for object language sentences containing the symbol `\eand' using the metalanguage word `and.' There is nothing circular about that.

%...
\begin{enumerate}\setcounter{enumi}{\arabic{Example}}
\item If \metaA{} is $(\metaB{}\eor\metaC{})$ for some sentences \metaB{}, \metaC{}, then
\begin{displaymath}v(\metaA{}) =
	\left\{\begin{array}{ll}
	0 & \mbox{if $v(\metaB{}) = 0$ and $v(\metaC{}) = 0$,}\\
	1 & \mbox{otherwise.}
	\end{array}\right.
\end{displaymath}
%\setcounter{Example}{\arabic{enumi}}\end{enumerate}
%...
%Notice that this defines truth for sentences containing the symbol `\eor' using the word `and.'
%...
%\begin{enumerate}\setcounter{enumi}{\arabic{Example}}
\item If \metaA{} is $(\metaB{}\eif\metaC{})$ for some sentences \metaB{}, \metaC{}, then
\begin{displaymath}v(\metaA{}) =
	\left\{\begin{array}{ll}
	0 & \mbox{if $v(\metaB{}) = 1$ and $v(\metaC{}) = 0$,}\\
	1 & \mbox{otherwise.}
	\end{array}\right.
\end{displaymath}

\item If \metaA{} is $(\metaB{}\eiff\metaC{})$ for some sentences \metaB{}, \metaC{}, then
\begin{displaymath}v(\metaA{}) =
	\left\{\begin{array}{ll}
	1 & \mbox{if $v(\metaB{}) = v(\metaC{})$},\\
	0 & \mbox{otherwise.}
	\end{array}\right.
\end{displaymath}
\end{enumerate}

Since the definition of $v$ has the same structure as the definition of a wff, we know that $v$ assigns a value to \emph{every} wff of SL. Since the sentences of SL and the wffs of SL are the same, this means that $v$ returns the truth value of every sentence of SL.

Truth in SL is always truth \emph{relative to} some interpretation, because the definition of truth for SL does not say whether a given sentence is true or false. Rather, it says how the truth of that sentence relates to a truth value assignment.

\section{Semantic entailment}

We are now in a position to give more precise definitions of terms like `tautology', `contradiction', and so on. Truth tables provided a way to \emph{check} whether a sentence was a tautology in SL, but they did not \emph{define} what it means to be a tautology in SL. We will give definitions of these concepts for SL in terms of \define{entailment}.

The relation of semantic entailment is about satisfiability --- that is, whether there is any possible interpretation that meets a certain set of conditions. `\metaA{} entails \metaB{}', means that there is no interpretation for which \metaA{} is true and \metaB{} is false. An interpretation provides a valuation function that gives truth values to atomic sentences; so \metaA{} entails \metaB{} if and only if every assignment of truth values to atomic sentences that makes \metaA{} true also makes \metaB{} true. (We could just as well say that  `\metaA{} entails \metaB{}', means that there is no \emph{valuation function} that makes \metaA{} true and \metaB{} false. Interpretations specify valuation functions.)

We abbreviate entailment with a symbol called the \emph{double turnstile}:
$\metaA{}\models\metaB{}$ means `\metaA{} semantically entails \metaB{}.'

The double turnstile, like `\metaA{}', etc., is part of the \emph{metalanguage} we use to discuss SL; it is not part of SL itself.

\section{Entailment, validity, and informally good arguments}

Entailment is a formal notion. It is connected in important ways to the informal notion of a good argument, but when considering entailment in SL, it is important to remember to apply the definitions rigorously and precisely, rather than relying on your sense of whether the argument is a good one. The notions can come apart in some surprising ways.

Let's start with a straightforward example. Consider whether this entailment claim is true: $$(P\eand Q) \models (P\eor Q)$$ This is true if and only if every interpretation that satisfies $(P\eand Q)$, also satisfies  $(P\eor Q)$. Hopefully it is obvious to you that this is true. Only an interpretation that assigns 1 to both $P$ and $Q$ will satisfy the left, and any such interpretation will certainly satisfy the right as well. (You could draw the truth table to verify this if you want the practice.) So, as one might naturally expect, $(P\eand Q) \models (P\eor Q)$.

Here is a less intuitive example. What should we make of this claim?

\begin{quote}
$(P\eand Q) \models (A\eiff\enot\enot A)$
\end{quote}

Notice that the sentence letters on the left-hand-side here are completely different letters from those on the right-hand-side. So there is a straightforward sense in which the two sides of the turnstile have \emph{nothing to do with one another}. Nevertheless, this \emph{is} a true entailment claim. This is because it satisfies the definition: every interpretation that satisfies the left (i.e., every interpretation with a valuation function assigns `1' to both $P$ and $Q$), also satisfies $(A\eiff\enot\enot A)$. This for the simple reason that every valuation \emph{whatsoever} satisfies $(A\eiff\enot\enot A)$; it is a tautology.

From this example we can see that a tautology will be entailed by anything whatsoever. If the right-hand-side of the entailment claim is a tautology, it doesn't matter what's on the left --- you know it's going to be true.

So these are all true:

\begin{earg}
\item[] $P \models (P \eor \enot P)$
\item[] $Q \models (P \eor \enot P)$
\item[] $(P \eand \enot P) \models (P \eor \enot P)$
\end{earg}

\section{Tautologies are entailed by the empty set}

In the examples so far, we've been talking about one sentence entailing another. But we can also describe a set of several sentences as jointly entailing an SL sentence: $$\metaA{}_1,\metaA{}_2,\metaA{}_3,\cdots\models\metaB{}$$ means that there is no truth value assignment for which all of the sentences in the set $\{\metaA{}_1,\metaA{}_2,\metaA{}_3,\cdots\}$ are true and \metaB{} is false. It will sometimes be convenient to use a variable that can stand in for any set of sentences; just as `\metaA{}' can stand for any sentence of SL, we can let `\metaSetX{}' stand in for any set of sentences in SL. (In the case of `$\metaA{}\models\metaB{}$' above, `\metaSetX{}' is the singleton set containing just \metaA{}.)

In general, we may say that $$\metaSetX{}\models\metaB{}$$ means that there's no interpretation satisfying every member of set \metaSetX{} without also satisfying sentence \metaB{}.

We saw in the previous section that a tautology ---  an SL sentence that is true in every interpretation --- is entailed by any sentence. The reasoning applies more generally: a tautology will be entailed by any set of sentences. This includes the \emph{empty set}, which can be written `$\emptyset$'. In general `$\metaSetX{} \models \metaA{}$' says that every interpretation that satisfies everything in \metaSetX{}, satisfies \metaA{}; in the special case where \metaSetX{} is $\emptyset$, every interpretation trivially satisfies everything in \metaSetX{}. So `$\emptyset \models \metaA{}$' says that absolutely every interpretation satisfies \metaA{}. That is, it says that \metaA{} is a tautology.

By convention, we can leave the left side of an entailment claim blank, instead of writing in $\emptyset$. In other words, $$\models \metaA{} $$ is shorthand for $$ \emptyset \models \metaA{}.$$

So these entailment claims are true, because in each case the sentence on the right is a tautology:

\begin{earg}
\item[] $\models (P \eor \enot P)$
\item[] $\models (P \eiff P)$
\item[] $\models ((P \eand\enot P)\eif (A \eor B))$
\end{earg}

And these entailment claims are not true, because each sentence on the right is not a tautology:

\begin{earg}
\item[] $\models P$
\item[] $\models (P \eand \enot P)$
\item[] $\models \enot(P \eif \enot P)$
\end{earg}

(If you need help seeing why a given sentence is or is not a tautology, draw out the truth table and check to see whether it has a 1 on every row. See \S\ref{sec.usingtruthtables}.)

\section{Inconsistent sentences entail absurdities}

Consider this entailment claim: $$(P \eand \enot P) \models Q$$This statement is true. It says that every interpretation satisfying $(P \eand \enot P)$ also satisfies $Q$. But $(P \eand \enot P)$ is a contradiction --- no interpretations satisfy it. So trivially, `every' interpretation that satisfies it satisfies $Q$.

This explanation had nothing to do with the specifics of the sentence $Q$. Exactly the same considerations would demonstrate that, for any sentence \metaB{}, $(P \eand \enot P) \models \metaB{}$. And the same goes for any other contradictory sentence, or any set of sentences that are mutually inconsistent. These are all true:

\begin{earg}
\item[] $(P \eand \enot P) \models (\enot Q \eif R)$
\item[] $P, (\enot Q \eand \enot P) \models (Q \eiff P)$
\item[] $(P \eand\enot P) \models ((A_1 \eor A_2) \eif \enot (A_3 \eiff (A_4 \eand \enot A_2)))$
\end{earg}

You can think of evaluating an entailment claim as like checking to see whether a rule is being violated or not. If there's a rule that says every student with a dog has to have a permit, you check each student with a dog, and make sure they have a permit. If you find someone without a dog, it doesn't matter whether they have a permit or not. (If you don't have a dog, you're not breaking the rule.) Or if you find someone with a permit, it doesn't matter whether they have a dog. (Nobody with a permit can be breaking the rule.) An entailment claim is like a rule that says every interpretation that satisfies the left-hand-side must also satisfy the right-hand-side. To verify it, you can ignore any interpretation that falsifies the left, or that satisfies the right.

In each of the cases above, you don't even need to examine the sentence at the right to confirm that the entailment claim is true, since you already know that no interpretations satisfy the sentences on the left. (This is analogous to knowing that no students have a dog --- in this case you don't have to check to see whether anyone has a permit to confirm that the rule is being respected.) Any entailment claim with an unsatisfiable set of sentences on the left is true. Unsatisfiable sets of sentences entail \emph{every} sentence.

We have a special notation for describing this situation. We write: $$\metaSetX{} \models \bot$$ to indicate that the set \metaSetX{} entails an \emph{absurdity}; think of `$\bot$' as standing in for an arbitrary unsatisfiable SL wff. So you can think of `$\metaSetX{} \models \bot$' as saying that every interpretation that satisfies $\metaSetX{}$ satisfies the unsatisfiable. That's just another way of saying that \emph{no} interpretation satisfies $\metaSetX{}$. (Compare: `the only Klingons who hate honour are the Klingons who aren't Klingons at all.')

%By convention, we treat $$\metaSetX{} \models$$ as shorthand for $$\metaSetX{} \models \bot.$$ So if you see an entailment claim with nothing on the right, read it as saying that the left is unsatisfiable.
%\label{sec.botshorthand}

So these entailment claims are true, because the set of sentences on the left is not satisfiable:

\begin{earg}
\item[] $(P \eand \enot P) \models \bot$
\item[] $P, (\enot Q \eand \enot P) \models\bot$
\item[] $(P \eiff\enot P) \models\bot$
\end{earg}

And these entailment claims are not true, because the sentences on the left \emph{can} be satisfied:

\begin{earg}
\item[] $(P \eor \enot P) \models\bot$
\item[] $P, \enot Q, (R \eor Q) \models\bot$
\item[] $\enot P, \enot Q, (P \eif \enot\enot Q) \models\bot$
\end{earg}

(If you need help seeing why a given set of sentences can be satisfied, draw out the truth table and check to see if any row assigns a 1 to each sentence. See \S\ref{sec.usingtruthtables}.)

\section{Defining concepts in terms of entailment}

The double turnstile symbol allows us to give concise definitions for various concepts in SL:
\begin{quote}
A \define{tautology in SL} is a sentence \metaA{}  such that $\emptyset\models\metaA{}$.

A \define{contradiction in SL} is a sentence \metaA{} such that $\metaA{}\models\bot$.

A sentence is \define{contingent in SL} if and only if it is neither a tautology nor a contradiction.

An argument with premises \metaSetX{} and conclusion \metaA{} is \define{valid in SL} if and only if $\metaSetX{}\models\metaA{}$.

Two sentences \metaA{} and \metaB{} are \define{logically equivalent in SL} if and only if both $\metaA{}\models\metaB{}$ and $\metaB{}\models\metaA{}$.

A set of sentences $\metaSetX{}$ is \define{inconsistent in SL} if and only if $\metaSetX{}\models \bot$. Otherwise $\metaSetX{}$ is \define{consistent in SL}.\label{def.consistencySL}

 \end{quote}

\practiceproblems

\problempart
\label{HW3.C}
Each of the following claims can be evaluated with truth tables. For each, what would you look for in a completed truth table to evaluate it? The Greek letters can stand for any arbitrary sentence of SL. The first claim has the answer filled out for you.

\begin{earg}
		\item[0.] $\Phi$ is a tautology.
		To evaluate this claim, check to see whether the main connective of $\Phi$ has a 1 under it in every row. If so, it is true.
		\item $\Phi \models \Psi$.
		To evaluate this claim, check to see whether...

		\item $\Phi$ is contingent.
\item $\Phi \models \bot$
		\item $\emptyset \models \Phi$
	\end{earg}

\problempart
Determine whether each entailment claim is true. You may construct a truth table to test it if you like, but these examples are simple enough so that you may be able to just think it through and get the right answer.
\begin{earg}
\item $Q \models (P \eor Q)$
\item $P, Q \models (P \eor P)$
\item $P \eiff Q, P \models Q$ %  \textcolor{red}{True.}
\item $S \models (Q \eif Q)$   %\textcolor{red}{True.}
\item $P \eand \enot P \models (Q \eor \enot Q)$%   \textcolor{red}{True.}
\item $(P \eand \enot P) \models (Q \eand \enot Q)$
\item $(P \eor \enot P) \models \bot$
\item $P \eor \enot P \models Q$%   \textcolor{red}{False: $\{P=1, Q=0\}$.}
\item $\models (P \eor \enot P)$
\item $\models (P \eand \enot P)$
\item $(A \eor B) \eif \enot P \models (P \eor \enot P)$
\item $(P \eiff Q) \models ((P \eand Q) \eor \enot (P \eor Q))$
\end{earg}


\chapter{Widely Linear Filtering and Adaptive Spectrum Estimation}

\input{report/widely-linear-filtering-and-adaptive-spectrum-estimation/complex-LMS-and-widely-linear-modelling/index}

\input{report/widely-linear-filtering-and-adaptive-spectrum-estimation/adaptive-AR-model-based-time-frequency-estimation/index}

\input{report/widely-linear-filtering-and-adaptive-spectrum-estimation/a-real-time-spectrum-analyser-using-least-mean-square/index}


%
% body.tex
%
% Copyright © 2020 Libao Jin <jinlibao@outlook.com>
% Distributed under terms of the MIT license.
%
Please note that the deadline will be enforced as per the previous homework. Remember that you are allowed to work in teams of two on this assignment. You are encouraged to prepare your work in \LaTeX{}; a template will be provided to help you put it all together. If you choose  to submit a hard copy, you may submit only one copy for a team, indicating the names of both contributors. Online submission is encouraged, however, in that case both members of a team should submit the PDF file containing  their work and showing both their names. \\[20pt]
\textbf{Instruction}
\begin{enumerate}[label={\arabic*.}]
  \item Go to \url{https://www.overleaf.com} and sign in (required).
  \item Open \href{https://www.overleaf.com/read/qczrkwtzpxft}{template}, click \emph{Menu} (up left corner), then \emph{Copy Project}.
  \item Go to \verb|LaTeX/meta.tex| (the file \verb|meta.tex| under the folder \verb|LaTeX|) to change the section and your name, e.g.,
    \begin{itemize}
      \item change title to \verb|\title{MATH 3340-01 Scientific Computing Homework 3}|
      \item change author to \verb|\author{Albert Einstein \& Carl F. Gauss}|
    \end{itemize}
  \item For Problem 1, 2, 3, you need to write function/script files, store results to output files, and save graphs to figure files. Here are suggested names for function files, script files, output files, and figure files:
    \begin{table}[!hbtp]
      \centering
      % \caption{caption}
      % \label{tab:label}
      \begin{tabular}{cllll}
        \toprule
        Problem & Function File        & Script File     & Output File       & Figure File       \\
        \midrule
        1       & \verb|jacobi.m|      & \verb|hw3_p1.m| & \verb|hw3_p1.txt| & \verb|hw3_p1.pdf| \\
        2       & \verb|gaussSeidel.m| & \verb|hw3_p2.m| & \verb|hw3_p2.txt| & \verb|hw3_p2.pdf| \\
        3       &                      & \verb|hw3_p3.m| & \verb|hw3_p3.txt| & \verb|hw3_p3.pdf| \\
        \bottomrule
      \end{tabular}
    \end{table}

    Once finished, you need to upload these files to the folder \verb|src| on Overleaf. If you have different filenames, please update the filenames in \verb|\lstinputlisting{../src/your_script_name.m}| accordingly. You can code in the provided files in \href{https://libaoj.in/courses/2021s/MATH3340/Homework/3/hw3.zip}{hw3.zip}, and use the MATLAB script \verb|save_results.m| to generate the output files and store the graphs to \verb|.pdf| files automatically (the script filenames should be exactly same as listed above).
  \item Recompile, download, and submit the generated PDF.
  \item You may find \href{https://libaoj.in/files/LaTeX.Mathematical.Symbols.pdf}{\LaTeX{}.Mathematical.Symbols.pdf} and the second part of \href{https://libaoj.in/courses/2021s/MATH3341/slides/Math.3341.Lab.01.Slides.pdf}{Lab 01 Slides} and \href{https://libaoj.in/courses/2021s/MATH3341/slides/Math.3341.Lab.02.Slides.pdf}{Lab 02 Slides} helpful.
\end{enumerate}

\newpage
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Problem 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Problem 1}%
\label{sec:problem_1}
Solve, using a MATLAB code, the following system:
\begin{equation*}
  \begin{cases}
    5x - y + z + w   = 9 \\
    x + 7y + 2z + 2w = 3 \\
    2x + y + 5z + w  = 7 \\
    x - y + z + 4w   = 6
  \end{cases}
\end{equation*}
Use the Jacobi method with with a tolerance of $10^{-5}$ for the norm of the residual. Arrange your results in a table of the form
\begin{table}[!hbtp]
  \centering
  \caption{caption}
  \label{tab:label}
  \begin{tabular}{ccccc}
    \toprule
    iteration & $x$      & $y$      & $z$      & $w$      \\
    \midrule
    $0$       & $0$      & $0$      & $0$      & $0$      \\
    $1$       & $1$      & $2$      & $3$      & $4$      \\
    $\vdots$  & $\vdots$ & $\vdots$ & $\vdots$ & $\vdots$ \\
    \bottomrule
  \end{tabular}
\end{table}
so that you can see how $x$, $y$, $z$ and $w$ change with each iteration. You can create such a table using \verb|disp| in a loop; you may also use variants of \verb|printf| (in C Language), i.e., \verb|fprintf| or \verb|sprintf| if you are familiar with these functions, but do not use the MATLAB \verb|table| command as it does not do quite what is expected here. Also, as obvious from the table above, start with the zero guess. Moreover, plot the norm of the residual versus the iteration number; use a logarithmic scale on the vertical axis (the residual axis). Turn in your code and the output as described. The code can be organized as:
\begin{enumerate}
  \item a function file that implements Jacobi's method; and
  \item a script that calls the function with with the appropriate inputs and processes the results.\end{enumerate}
Your plot should have a plot title, axes labels and a legend. Use the \verb|help plot| command and the class notes  to investigate the various options  available when plotting.
\begin{solution}
  \quad
  \begin{itemize}
    \item
      Function file \verb|jacobi.m|
      \lstinputlisting[style=MATLAB]{../src/jacobi.m}
    \item
      Script file \verb|hw3_p1.m|
      \lstinputlisting[style=MATLAB]{../src/hw3_p1.m}
    \item
      Output file \verb|hw3_p1.txt|
      \lstinputlisting[style=Plain]{../src/hw3_p1.txt}
      \begin{figure}[!hbtp]
        \centering
        \includegraphics[width=0.8\linewidth]{../src/hw3_p1.pdf}
        \caption{}%
        \label{fig:}
      \end{figure}
  \end{itemize}
\end{solution}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Problem 2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Problem 2}%
\label{sec:problem_2}
Solve again the system above, in MATLAB, using the Gauss-Seidel method. Produce the same results as for the previous problem.
\begin{solution}
  \quad
  \begin{itemize}
    \item
      Function file \verb|gaussSeidel.m|
      \lstinputlisting[style=MATLAB]{../src/gaussSeidel.m}
    \item
      Script file \verb|hw3_p2.m|
      \lstinputlisting[style=MATLAB]{../src/hw3_p2.m}
    \item
      Output file \verb|hw3_p2.txt|
      \lstinputlisting[style=Plain]{../src/hw3_p2.txt}
      \begin{figure}[!hbtp]
        \centering
        \includegraphics[width=0.8\linewidth]{../src/hw3_p2.pdf}
        \caption{}%
        \label{fig:}
      \end{figure}
  \end{itemize}
\end{solution}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Problem 3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Problem 3}%
\label{sec:problem_3}
Solve the twenty-one systems of equations:
\begin{equation*}
  \begin{cases}
    7x + y + 2z = 0.01 m^{2} - 2m \\
    x - 5y + 2z = 2 - m \\
    2x + y + 5z = 9
  \end{cases}
\end{equation*}
obtained by setting, in turn, the value of the quantity $m$ on the right-hand side to all integers in between, and including, $m = 0$ and $m = 20$. This time use the LU-decomposition of the system matrix that is implemented in MATLAB by the \verb|lu| function. Perform the decomposition only once, then use the lower- and upper-triangular factors repeatedly to find each successive solution. Turn in the code and a plot of the first and second components of the solution (that is, the values of $x$ and $y$) as a function of thee right-hand side parameter $m$.
\begin{solution}
  \quad
  \begin{itemize}
    \item
      Script file \verb|hw3_p3.m|
      \lstinputlisting[style=MATLAB]{../src/hw3_p3.m}
    \item
      Output file \verb|hw3_p3.txt|
      \lstinputlisting[style=Plain]{../src/hw3_p3.txt}
      \begin{figure}[!hbtp]
        \centering
        \includegraphics[width=0.8\linewidth]{../src/hw3_p3.pdf}
        \caption{}%
        \label{fig:}
      \end{figure}
  \end{itemize}
\end{solution}


\documentclass{article}
\usepackage{fullpage}
\usepackage{nopageno}
\usepackage{amsmath}
\allowdisplaybreaks

\newcommand{\abs}[1]{\left\lvert #1 \right\rvert}

\begin{document}
\title{Notes}
\date{January 24, 2014}
\maketitle
\section*{page 26 number 2}

notice that pde is stated for the interior of the region
model can break down on the edges
u stays at zero for time zero which is consistent with initial condition
notice that the slope at $x=1$ for initial condition is $-\pi$ but is 1 for boundary condition

heat is flowing in (proportional to negative slope).

second boundary condition says that heat flow into the rod at x=1 (temperature gradient at $x=1$ is positive) which means low temperature inside, high temperature outside

notice boundary conditions are $0<t<\infty$ so initial condition doesn't intersect boundary condition, because solution is discontinuous at $t=0$

\section*{last time}
lessons 2 and 3 where PDE is $u_t=\alpha^2 u_{xx}$ IC is $u(x,0)$ for different types of pde's and different types of bc's

\section*{lesson 4}
derivation of pde
page 28
letter A is area of cross section
\subsection*{condition 1}
rod consists of homogeneous conducting material.  temperature varies in rod, but substrate doesn't. homogeneous medium.
\subsection*{condition 2}
laterally insulated-no heat flow across lateral surface
\subsection*{condtion 3}
thin rod, temperature is uniform across cross section.
\begin{align*}
  u_t&=\alpha^2u_{xx} \text{on} 0<x<L, 0<t<\infty\\
  u(x,t)=\text{temperature in degrees}& u(x,t)& \text{is degrees}\\
  \text{total heat energy in} [x_1,x_2]=\int_{x_1}^{x_2}{cupA,\mathrm{d}x} & c&=\text{specific heat}=\frac{\text{calories}}{\text{degree of mass}}\\
  &&p&=\text{density}\\
  \text{rate of change of energy}&=\frac{\mathrm{d}}{\mathrm{d}t}\int_{x_1}^{x_2}{cupA,\mathrm{d}x}=\int_{x_1}^{x_2}{cpu_t A,\mathrm{d}x}\\
  \intertext{law of conservation of energy}
  &=\text{flow rate of energy at end points}+\text{generation rate of energy(internal)}\\
  \text{generation rate}&=\int_{x_1}^{x_2}{f(s,t)A,\mathrm{d}s} & f&=\frac{\text{cal}}{\text{sec cm}^3}\\
  \text{flow rate}&=-DA(c\rho u)_x \frac{\text{cal}}{\text{cm}^2}\\
  \intertext{D is thermal diffusivity}
  &=-DA(c\rho u)_x-(-DA(c\rho u)_x)+\int_{x_1}^{x_2}{f(s,t)A,\mathrm{d}s}
\end{align*}

we are thinking of stuff flowing into and out of the region $[x_1,x_2]$
\subsection*{condition 1}
tube of solution of stuff. homogeneous medium (liquid)
\subsection*{condition 2}
tube (glass or whatever) allows no flow of stuff through it. similar to insulation above.
\subsection*{condition 3}
concentration of stuff is uniform by symmetry
\begin{align*}
  \omega_t=D\omega_{xx} \text{on} 0<x<L, 0<t<+\infty\\
  \omega(x,t)=\text{density of stuff}\\
  \text{total stuff in} [x_1,x_2]=\int_{x_1}^{x_2}{\omega(x,t)A,\mathrm{d}x}
  \text{rate of change of stuff}&=\frac{\mathrm{d}}{\mathrm{d}t}\int_{x_1}^{x_2}{\omega A,\mathrm{d}x}=\int_{x_1}^{x_2}{\omega_t A,\mathrm{d}x}\\
  \intertext{law of conservation of stuff (mass or whatever)}
  &=\text{flow rate of stuff at endpoints}+\text{generation rate of stuff}
  \text{generation rate}&=\frac{\text{stuff}}{\text{sec}}=\int_{x_1}^{x_2}{f(s,t)A,\mathrm{d}s} & f&=\frac{\text{stuff}}{\text{sec cm}^3}\\
  \text{flow rate of stuff at x}&=-DA\omega_x\\
  \intertext{D is diffusion coefficient cm squared per second, this is Fick's law}
  &=-DA\omega_x(x_1,t)-(-DA\omega_x(x_2,t))+\int_{x_1}^{x_2}{f(s,t)A,\mathrm{d}s}
\end{align*}
\end{document}


\chapter{Legendre expansions of energy-angle probability densities
in the laboratory frame}
\label{Sec:Legendre-lab}
Another representation of joint energy-angle probability densities
$\pi(E', \mu \mid E)$ in \xendl\ is as a table of the Legendre
coefficients $\pi_\ell(E' \mid E)$ in the expansion
\begin{equation}
  \pi( E', \mu \mid E) = \sum_\ell
  \left(
    \ell + \frac{1}{2}
  \right)
  \pi_\ell( E' \mid E) P_\ell( \mu ).
 \label{piLegendre}
\end{equation}
Here, $E$ denotes the energy of the incident particle in the
laboratory frame.  For the outgoing particle, the energy $E'$ and
direction cosine $\mu$ may be given in either center-of-mass or laboratory
coordinates.  The treatment of laboratory-frame data is discussed in this section,
center-of-mass data in the next.  Data given in the laboratory frame are
much easier to deal with because no boost is involved.

This type of data is ordered according to
\begin{equation}
\{ E, \{ E', \{ \pi_\ell(E' \mid E) \} \} \}.
 \label{ENDF-I4}
\end{equation}
All of the data for the lowest incident energy $E$
is given first, ordered according to outgoing energy~$E'$.  For
given values of $E$ and $E'$, the data consist of Legendre coefficients
$\pi_\ell(E', \mid E)$.  Note that for this data format, the number
of Legendre coefficients may vary, depending on the energies $E$ and~$E'$.

The {\gettransfer} code also handles data for Legendre expansions of
energy-angle probability densities in the {\ENDL} format~\cite{Omega},
\begin{equation}
\{ \ell, \{ E, \{ E', \pi_\ell(E', \mid E) \} \} \}.
 \label{ENDL-I4}
\end{equation}
That is, the $\ell = 0$ data are given first, ordered according to
incident energy~$E$.  The data then consist of pairs
$\{ E', \pi_\ell(E', \mid E) \}$ for given $\ell$ and $E$.
%This data format is deprecated, however.

\section{Computation of the transfer matrices for data in the laboratory frame}
The calculation of the transfer
matrices for laboratory-frame data proceeds as follows.
In terms of $\pi_\ell(\Elab'   \mid E)$,
the integral Eq.~(\ref{Inum}) for the number-preserving transfer matrix
takes the form
\begin{equation}
   \Inum_{g,h,\ell} =
     \int_{\calE_g} dE \, \sigma ( E ) M(E) w(E) \widetilde \phi_\ell(E)
   \int_{\calE_h' } d\Elab'   \, \pi_\ell(\Elab'   \mid E),
 \label{InumI4}
\end{equation}
and Eq.~(\ref{Ien}) for the energy-preserving transfer matrix becomes
\begin{equation}
   \Ien_{g,h,\ell} =
     \int_{\calE_g} dE \, \sigma ( E ) M(E) w(E) \widetilde \phi_\ell(E)
     \int_{\calE_h' } d\Elab'   \, \pi_\ell(\Elab'   \mid E) \Elab'  .
 \label{IenI4}
\end{equation}

Computation of the integrals Eqs.~(\ref{InumI4}) and~(\ref{IenI4})
depends on the type of interpolation used with respect to the
energy $E$ of the incident particle, and the procedures are
exactly the same as for integration in Eqs.~(\ref{InumI4-0})
and~(\ref{IenI4-0}) of the isotropic energy probability densities
$\pi_0(\Elab'   \mid E)$.  Thus, if unit-base interpolation is to
be used for $\pi_\ell(\Elab'   \mid E)$, then the map Eq.~(\ref{unitbaseMap})
converts
the integrals Eqs.~(\ref{InumI4}) and~(\ref{IenI4}) to the form
\begin{equation}
   \Inum_{g,h,\ell} =
     \int_{\calE_g} dE \, \sigma ( E ) M(E) w(E) \widetilde \phi_\ell(E)
   \int_{\widehat\calE_h' } d\widehat \Elab'   \,
     \widehat\pi_\ell(\widehat \Elab'   \mid E)
 \label{InumhatI4}
\end{equation}
and
\begin{equation}
   \Ien_{g,h,\ell} =
     \int_{\calE_g} dE \, \sigma ( E ) M(E) w(E) \widetilde \phi_\ell(E)
   \int_{\widehat\calE_h' } d\widehat \Elab'   \,
     \widehat\pi_\ell(\widehat \Elab'   \mid E) \Elab'  .
 \label{IenhatI4}
\end{equation}
In these intergrals $\widehat\calE_h'$ denotes result of mapping the
outgoing energy bin $\calE_h'$ with the transformation Eq.~(\ref{unitbaseMap}).
Furthermore, $\Elab'  $ in Eq.~(\ref{IenhatI4}) is to be obtained from $\widehat \Elab'  $
using the inverse unit-base mapping Eq.~(\ref{unitbaseInvert}).

The geometrical considerations involved in integrating
Eqs.~(\ref{InumhatI4}) and~(\ref{IenhatI4}) over the incident energy bin~$\calE_g$
and the mapped outgoing energy bin~$\widehat\calE_h'$ are illustrated
in Figure~\ref{Fig:unit-base-region}.

\section{Form of the input file for Legendre coefficient data in the laboratory frame}
These data may be input in either of two forms, the format
in Eq.~(\ref{ENDF-I4}) from \ENDF\ with all Legendre
coefficients given together at each incident energy $E$ and outgoing energy~$E'$
or that  in Eq.~(\ref{ENDL-I4}) with
one Legendre order at a time.
For both formats, all energies must be in the same units as the energy groups.

\subsection{Input of all Legendre coefficients together}\label{Sec:ENDF-I4-data}
For energy-angle tables in the standard format of Eq.~(\ref{ENDF-I4}),
the Section~\ref{data-model} line in the input
file to identify the data is\\
      \Input{Process: Legendre energy-angle data}{}\\
and the model-dependent data in Section~\ref{model-info} consists of the
Legendre coefficients $\pi_\ell(E' \mid E)$ in Eq.~(\ref{ENDF-I4}) at incident energies~$E$
and outgoing energies~$E'$.

The format for the Legendre coefficient data in
Section~\ref{model-info} given at $K$ values of $E$ is\\
  \Input{Product Frame: lab}{}\\
  \Input{Legendre data by incident energy:}{$n = K$}\\
  \Input{Incident energy interpolation:}{probability interpolation flag}\\
  \Input{Outgoing energy interpolation:}{list interpolation flag}\\
where the interpolation flag for incident energy is one for probability density
tables as in Section~\ref{interp-flags-probability}, and that for outgoing energy
is for a simple list.
These lines are followed by $K$ sections of the form\\
  \Input{ Ein: $E$:}{\texttt{n = $J_k$}}\\
for $J_k$ outgoing energies $E$.  For each value of $E$ there is
data\\
  \Input{  Eout: $E'$:}{\texttt{n = $L$}}\\
with Legendre coefficients $\pi_\ell(E' \mid E)$ for $\ell = 0$, 1, \ldots\ , $L - 1$.

An example of these data with energies in MeV is\\
  \Input{Legendre data by incident energy:  n = 26}{}\\
   \Input{Incident energy interpolation: lin-lin cumulativepoints}{}\\
  \Input{Outgoing energy interpolation: flat}{}\\
   \Input{Ein: 1.140200e+01:  n = 2}{}\\
  \Input{  Eout: 0.000000e+00:  n = 5}{}\\
 \Input{ \indent  1.000000e+11}{}\\
 \Input{ \indent  0.000000e+00}{}\\
 \Input{ \indent  0.000000e+00}{}\\
   \Input{ \indent  0.000000e+00}{}\\
   \Input{ \indent  0.000000e+00}{}\\
    \Input{ Eout: 1.000000e-11:  n = 5}{}\\
   \Input{ \indent  0.000000e+00}{}\\
   \Input{ \indent  0.000000e+00}{}\\
   \Input{ \indent  0.000000e+00}{}\\
   \Input{ \indent  0.000000e+00}{}\\
   \Input{ \indent  0.000000e+00}{}\\
\Input{}{$\cdots$}\\
   \Input{Ein: 2.000000e+01:  n = 27}{}\\
    \Input{ Eout: 0.000000e+00:  n = 5}{}\\
   \Input{ \indent  4.179200e-02}{}\\
   \Input{ \indent  0.000000e+00}{}\\
   \Input{ \indent  4.179200e-06}{}\\
   \Input{ \indent  0.000000e+00}{}\\
   \Input{ \indent  3.395500e-07}{}\\
 \Input{ \indent}{ etc.}

 \subsection{Input of one Legendre coefficient at a time}\label{sec:ENDL-I4}
For data given one Legendre coefficient at a time as in Eq.~(\ref{ENDL-I4}),
the line in Section~\ref{data-model} of the input
file identifying the data is\\
      \Input{Process: Legendre EEpP data transfer matrix}{}\\
The first lines in the data for Section~\ref{model-info} are\\
  \Input{Product Frame: lab}{}\\
 \Input{LEEpPData:}{$n = L$}\\
where $L$ is the number of Legendre coefficients, one greater than the
order of the Legendre expansion.  The interpolation flags as
in Section~\ref{interp-flags-probability} are\\
   \Input{Incident energy interpolation:}{probability interpolation flag}\\
  \Input{Outgoing energy interpolation:}{list interpolation flag}\\
The interpolation flag for incident energy is one for probability density
tables as in Section~\ref{interp-flags-probability}, and that for outgoing energy
is for a simple list.

The data are then given in $L$ sections, each of the form\\
   \Input{ order: l = $\ell$:}{\texttt{n = $K$}}\\
where $K$ is the number of incident energies.
For each incident energy $E'$ there is a block of data\\
    \Input{ Ein:}{$E$: \quad \texttt{n = $J_k$}}\\
for $J_k$ pairs of values of outgoing energy $E'$ and
Legendre coefficient $\pi_\ell(E' \mid E)$.  For energies measured in MeV,
these data may look like\\
  \Input{LEEpPData: n = 4}{}\\
  \Input{Incident energy interpolation: lin-lin unitbase}{}\\
  \Input{Outgoing energy interpolation: lin-lin}{}\\
  \Input{order: l = 0: n = 10}{}\\
   \Input{Ein:  3.350000000000e+00 : n = 3}{}\\
  \Input{ \indent   3.716500000000e-01   0.000000000000e+00}{}\\
  \Input{ \indent   3.716800000000e-01   2.857140000000e+04}{}\\
  \Input{ \indent   3.717200000000e-01   0.000000000000e+00}{}\\
  \Input{Ein:  4.460200000000e+00 : n = 2}{}\\
  \Input{ \indent   1.238900000000e-01   1.008970000000e+00}{}\\
   \Input{ \indent  1.115000000000e+00   1.008970000000e+00}{}\\
 \Input{$\cdots$}{}\\
  \Input{Ein:  2.000000000000e+01 : n = 2}{}\\
     \Input{ \indent  1.699600000000e-02  1.232820000000e-01}{}\\
     \Input{ \indent  8.128500000000e+00  1.232820000000e-01}{}\\
   \Input{order: l = 1: n = 10}{}\\
    \Input{Ein:  3.350000000000e+00 : n = 3}{}\\
     \Input{ \indent  3.716500000000e-01  0.000000000000e+00}{}\\
     \Input{ \indent  3.716800000000e-01  2.690500000000e+04}{}\\
     \Input{ \indent  3.717200000000e-01  0.000000000000e+00}{}\\
\Input{$\cdots$}{}\\
    \Input{order:l = 3: n = 10}{}\\
    \Input{Ein:  3.350000000000e+00 : n = 3}{}\\
     \Input{ \indent  3.716500000000e-01  0.000000000000e+00}{}\\
     \Input{ \indent  3.716800000000e-01  2.690500000000e+04}{}\\
     \Input{ \indent  3.717200000000e-01  0.000000000000e+00}{}\\
\Input{$\cdots$}{}\\
  \Input{Ein:  2.000000000000e+01 : n = 28}{}\\
     \Input{ \indent  1.699600000000e-02  1.172400000000e-01}{}\\
     \Input{ \indent  3.283800000000e-02 -8.646000000000e-03}{}\\
     \Input{ \indent  4.868100000000e-02 -3.589400000000e-02}{}\\
     \Input{ \indent  6.452400000000e-02 -4.528500000000e-02}{}\\
     \Input{ \indent  8.036700000000e-02 -4.921500000000e-02}{}\\
     \Input{ \indent  1.120500000000e-01 -5.186400000000e-02}{}\\
    \Input{ \indent }{$\cdots$}\\
    \Input{ \indent  7.082900000000e+00  7.783200000000e-02}{}\\
     \Input{ \indent  8.128500000000e+00  1.172400000000e-01}{}


\documentclass{cmc}

\begin{document}

\pagestyle{fancy}
\lhead{\textit{\textbf{Computational Motor Control, Spring 2018} \\
    Python exercise, Lab 4, GRADED}} \rhead{Student \\ Names}

\section*{Student names: \ldots (please update)}

\textit{Instructions: Update this file (or recreate a similar one,
  e.g.\ in Word) to prepare your answers to the questions. Feel free
  to add text, equations and figures as needed. Hand-written notes,
  e.g.\ for the development of equations, can also be included e.g.\
  as pictures (from your cell phone or from a scanner).
  \textbf{\corr{This lab is graded.}} and need to be submitted before
  the \textbf{\corr{Deadline : 03-04-2018 Midnight}}.  \\ Please
  submit both the source file (*.doc/*.tex) and a pdf of your
  document, as well as all the used and updated Python functions in a
  single zipped file called \corr{lab4\_name1\_name2\_name3.zip} where
  name\# are the team member’s last names.  \corr{Please submit only
    one report per team!}}

\textit{In this exercise, you will explore the different modelling
  techniques that can be used to control a single joint and
  segment. We initially start by exploring a single joint controlled
  by a pair of antagonist spring like muscles and then extend the
  model by adding dampers to it. These only represent the passive
  dynamics observed in a real musculoskeletal system. To make the
  behaviour more realistic we then study more complex hill muscle
  model in detail. }

\section*{Exercise 1 : Pendulum model with passive elements}
\label{sec:question-1}

Mechanical behavior of muscle tissue can be described by simple
passive elements such as springs and dampers. These elements, when
combined properly, allow to study the behavior of muscle under
compressive and tensile loads.

\subsection*{Explore the pendulum model with two antagonist spring
  elements}

In this question the goal is to add two antagonist springs to the
pendulum model which you are already familiar with from lab 2
exercises. For simplicity we assume the springs directly apply a
torsional force on to the pendulum.  Use equation \ref{eqn:spring} to
develop the spring model.

\textit{\textbf{Note} : The springs can only produce force in
  one-direction like the muscles.  That is, they can only apply a
  pulling force and apply a zero force when compressed.  You need to
  accomodate for this condition for springs S1 and S2 in the equation
  shown below}

The setup for the pendulum with a pair of antagonist springs is as
shown in figure \ref{fig:pendulum_spring}. Use \fileref{exercise1.py},
\fileref{lab4\_pendulum.py} and \fileref{SystemParameters.py} files to
complete the exercise.

\begin{equation}
  \label{eqn:spring}
  F_{s} = K*(\theta_{ref} - \theta)
\end{equation}

Where,
\begin{itemize}
\item $F_{s}$ : Torsional Spring force
\item $K$ : Spring Constant
\item $\theta_{ref}$ : Spring reference angle
\item $\theta$ : pendulum angle
\end{itemize}

\begin{figure}[H]
  \centering
  \includegraphics[width=.3\textwidth]{figures/pendulum_spring}
  \caption[pendulum with spring]{Pendulum model with two springs s1
    and s2}
  \label{fig:pendulum_spring}
\end{figure}

\subsection*{1.a Does the system have a stable limit cycle behavior?
  Describe and run an experiment to support your answer.}
\label{subsec:1.a}

\subsection*{1.b Explore the role of spring constant ($K$) and spring
  reference angle ($\theta_0$) in terms of range of motion, amplitude,
  ... \\ Support your responses with relevant plots }

\subsection*{1.c Explain the behavior of the model when you have
  different spring constants ($K$) and spring reference angles
  ($\theta_{ref}$). Support your responses with relevant plots}

\subsection*{Explore the pendulum model with two antagonist spring and damper elements}
Over time muscles lose energy while doing work. In order to account
for this property, let us now add a damper in parallel to the spring
model. Use equation \ref{eqn:damper} to develop the damper model.

\textit{\textbf{Note} : Like the previous springs, dampers can only
  produce force in one-direction.  That is, they can only apply a
  damping force in the pulling direction and apply a zero force when
  compressed. You need to accomodate for this condition for dampers B1
  and B2 in the equation shown below}

Again use \fileref{exercise1.py}, \fileref{lab4\_pendulum.py} and
\fileref{SystemParameters.py} files to complete the exercise. The
setup for the pendulum model with a pair of antagonist spring and
dampers in parallel is as shown in figure
\ref{fig:pendulum_spring_damper}.

\begin{equation}
  \label{eqn:damper}
  F_{B} = B*(\dot{\theta})
\end{equation}

Where,
\begin{itemize}
\item $F_{B}$ : Torsional Damper force
\item $B$ : Damping Constant
\item $\dot{\theta}$ : pendulum angular velocity
\end{itemize}

\begin{figure}[H]
  \centering
  \includegraphics[width=.3\textwidth]{figures/pendulum_spring_damper}
  \caption[pendulum with spring]{Pendulum model with two springs S1
    and S2 and two dampers B1 and B2}
  \label{fig:pendulum_spring_damper}
\end{figure}

\subsection*{1.d How does the behavior now change compared to
  1.a. Briefly explain and support your responses with relevant plots}

\subsection*{1.e Can you find a combination of spring constant ($K$),
  damping constant ($B$) and spring reference angle ($\theta_{ref}$) that
  makes the pendulum rest in a stable equilibrium at
  ($\theta = \pi/6$) radians? Describe the parameters used and support
  your response with relevant plots.}

\subsection*{1.f What is the missing component between a real muscle
  and the muscle model with passive components that you just
  explored? What behaviour's do you lack because of this missing component?}

\newpage
\section*{Exercise 2 : Hill muscle model}
\label{sec:question-2}

In exercise 1, you explored the role of different passive components
and the effects of its parameters on the system. In this exercise, we
try to understand the contractile or the active element of the hill
muscle model. The components of the hill muscle are described in
figure \ref{fig:hill_muscle}. The equations used to model the hill
muscle can be found in the pdf \fileref{HillMuscleEquations.pdf}

Use \fileref{exercise2.py}, \fileref{lab4\_mass.py},
\fileref{SystemParameters.py} and \fileref{Muscle.py} files to
complete the exercise.

\begin{figure}[H]
  \centering \includegraphics[scale=2.5]{figures/hill_muscle}
  \caption{Hill muscle model}
  \label{fig:hill_muscle}
\end{figure}

Where,

\begin{itemize}
\item $PE$ : Parallel element (Prevents muscle from over stretching)
\item $BE$ : Muscle Belly (Prevents muscle from collapsing on itself)
\item $SE$ : Series element or the muscle tendon element
\item $CE$ : Contractile Element or the active element
\item $l_{opt}$ : Muscle optimal fiber length
\item $l_{slack}$ : Muscle tendon slack length
\item $l_{CE}$ : Contractile element length
\item $l_{MTC}$ : Muscle Tendon Complex length
\end{itemize}

\begin{figure}[H]
  \centering
  \begin{subfigure}[b]{0.49\textwidth}
    { \centering
      \includegraphics[width=\textwidth]{figures/isometric_muscle}
      \label{fig:isometric_muscle}
    }
    \caption{Isometric muscle setup :\\ To study the relationship
      between Force-Length.}
  \end{subfigure}
  \begin{subfigure}[b]{0.49\textwidth}
    { \centering
      \includegraphics[width=\textwidth]{figures/isotonic_muscle}
      \label{fig:isotonic_muscle}
    }
    \caption{Isotonic muscle setup :\\ To study the relationship
      between Force-Velocity.}
  \end{subfigure}
  \caption{Muscle Length-Velocity-Force Setup}
  \label{fig:muscle-setup}
\end{figure}

\subsection*{Muscle Force-Length Relationship}
\label{sec:muscle-force-length}
In this exercise you will explore the relation between the length and
velocity of the muscle. In order to do this we replicate the set-up
show in figure \ref{fig:muscle-setup}.Here the length of the muscle is
held constant by attaching it's tendon to two fixed points. While
applying a constant stimulation, observing the force produced will
give the relationship between muscle contractile element length and
force.
\subsection*{2.a For a given stimulation, explore the relationship
  between active and passive muscle forces and the length of the
  contractile element.  Plot the force-length relationship curve.
  Discuss the different regions in the plot}

\subsection*{2.b In (2.a), you explored the muscle force-length
  relationship for a given stimulation. What happens to the
  relationship when the stimulation is varied between [0 - 1]? Support
  your response with relevant plots.}

\subsection*{2.c Describe how the fiber length ($l_{opt}$) influences
  the force-length curve.  (Compare a muscle comprised of short muscle
  fibers to a muscle comprised on long muscle fibers.)}

\subsection*{Muscle Velocity-Tension Relationship}
In this exercise you will explore the relation between the force and
velocity of the muscle. In order to do this we replicate the set-up
show in figure \ref{fig:muscle-setup}. Here the length of the muscle is
allowed to vary by attaching one of its end to a fixed point and the
other to a variable external load. While applying a constant load
initially and holding the muscle at constant length, a quick release
is performed to let the muscle contract and pull the weight. The
maximum velocity during this quick release will give us the
relationship between muscle contractile velocity and the force

\subsection*{2.d For a stimulation of 1.0 and starting at optimal
  muscle length, explore the relationship between contracticle element
  velocity and external load. Plot the Velocity-Tension relationship
  curve. Include shortening and lengthening regions}

\subsection*{2.e For the muscle force-velocity relationship, why is
  the lengthening force greater than the force output during
  shortening?}

\subsection*{2.f How does the parameter muscle activation influence the
  force-velocity relationship.  Show and explain the behavior for
  multiple muscle activation}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:


\documentclass{article}
\usepackage[utf8]{inputenc}

\title{MAT257 Notes}
\author{Jad Elkhaleq Ghalayini}
\date{January 9 2018}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{mathtools}
\usepackage{enumitem}
\usepackage{graphicx}
\usepackage{cancel}

\usepackage[margin=1in]{geometry}

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}
\newtheorem{definition}{Definition}
\newtheorem*{corollary}{Corollary}
\newtheorem{exercise}{Exercise}
\newtheorem{claim}{Claim}

\DeclareMathOperator{\Int}{Int}
\DeclareMathOperator{\grad}{grad}
\DeclareMathOperator{\Ker}{Ker}
\DeclareMathOperator{\Ima}{Im}

\newcommand{\reals}[0]{\mathbb{R}}
\newcommand{\nats}[0]{\mathbb{N}}
\newcommand{\ints}[0]{\mathbb{Z}}
\newcommand{\rationals}[0]{\mathbb{Q}}
\newcommand{\brac}[1]{\left(#1\right)}
\newcommand{\sbrac}[1]{\left[#1\right]}
\newcommand{\mc}[1]{\mathcal{#1}}
\newcommand{\eval}[3]{\left.#3\right|_{#1}^{#2}}
\newcommand{\ip}[2]{\left\langle#1,#2\right\rangle}
\newcommand{\prt}[2]{\frac{\partial #1}{\partial #2}}
\newcommand{\mb}[1]{\mathbf{#1}}
\newcommand{\hlfspc}[0]{\mathbb{H}}
\newcommand{\loint}[0]{\operatorname{L}\int}
\newcommand{\hiint}[0]{\operatorname{U}\int}
\newcommand{\indic}[1]{\chi_{#1}}

\begin{document}

\maketitle

\section{Partition of unity}

\subsection{Bump Functions}

A bump function, in the one dimensional case, is a function which goes up to 1 from 0, stays there for a while, and then returns to zero. But we're interested in doing this in \(n\)-dimensions. How? Given an open \(U \subset \reals^n\), and compact \(C \subset U\), we want to construct a \(\mc{C}^\infty\) function \(f: U \to \reals\) with \(0 \leq f \leq 1\) such that \(\forall x \in C, f(x) = 1\) and \(f(x) = 0\) outside some compact subset of \(U\). So it looks like the 1-dimensional bump function, except over an \(n\)-dimensional area \(C\).

How can we construct such a function? We'll do it as a kind of series of exercises, based on one particular function, which you can imagine ought to be the kind of thing that you have to build this function, that is,
\begin{equation}
  f(x) = \left\{\begin{array}{ccc}
    e^{1/x^2} & \text{if} & x \neq 0 \\
    0 & \text{if} & x = 0
  \end{array}\right.
  \label{bumpbuild}
\end{equation}
We proceed as follows, building up by steps:
\begin{enumerate}

  \item There is a \(\mc{C}^\infty\) function \(g: \reals \to \reals\) such that \(g > 0\) on \((-1, 1)\), 0 elsewhere. It's easy to write down a formula for such a function using the function given in \ref{bumpbuild}, as follows:
  \begin{equation}
    g(x) = \left\{\begin{array}{ccc}
      e^{-\frac{1}{(x - 1)^2}}e^{-\frac{1}{(x + 1)^2}} & \text{if} & x \in (-1, 1) \\
      0 & \text{if} & x \notin (-1, 1)
    \end{array}\right.
    \label{g}
  \end{equation}
  This works because \(e^{-\frac{1}{(x - 1)^2}}\) becomes completely flat at \(x = 1\), whereas \(e^{-\frac{1}{(x + 1)^2}}\) becomes completely flat at \(x = -1\).

  \item Given \(a \in \reals^n, \delta > 0\), there is a \(C^\infty\) function \(g_{a, \delta}: \reals^n \to \reals\) such that
  \begin{equation}
    g_{a, \delta} > 0 \text{ on } |x - a| < \delta, g_{a, \delta} = 0 \text{ outside}
  \end{equation}
  So how could achieve this? We could use the function defined in \ref{g} to write
  \begin{equation}
    g_{a, \delta} = g\left(\frac{|x - a|^2}{\delta^2}\right)
  \end{equation}

  \item There is a \(\mc{C}^\infty\) function \(h: U \to \reals\) such that \(h\) is greater than 0 on \(C\) and \(h = 0\) outside a compact subset of \(U\). So how do we do this?

  We could cover \(C\) with a finite set of open balls \(U_1,...,U_k\) with centers \(a_1,...,a_k\) and radii \(\delta_1,...,\delta_k\) whose closures lie in \(U\). We can do this since \(C\) is compact. Once we do this, we can define quite simply
  \begin{equation}
    h(x) = \sum_{i = 1}^kg_{a_k, \delta_k}(x)
    \label{compactsum}
  \end{equation}

  \item As a fourth thing, going back to the real line, let's show that if we're given any \(\epsilon \in \reals^+\), we can find a \(\mc{C}^\infty\) function \(\psi_\epsilon\) which is zero up to 0, becomes 1 at \(\epsilon\), and then remains 1 forever after. How can we do this from what we've already done? We're going to do it starting with a \(\mc{C}^\infty\) function like that in equation \ref{g}, but instead of on \((-1, 1)\), on \((0, \epsilon)\). More generally, actually, we can use any function \(g\) such that \(g > 0\) on \((0, \epsilon)\) and is zero outside. We can then define \(\psi_\epsilon\) by
  \begin{equation}
    \psi_\epsilon(x) = \frac{\int_0^xg}{\int_0^\epsilon g}
    \label{riseup}
  \end{equation}

  \item Now, putting everything together, how can we define the bump function that we wanted to begin with? We begin with the function \(h\) as defined in equation \ref{compactsum}, and we're going to modify it using equation \ref{riseup} as follows: letting \(\epsilon = \min_Ch > 0\), which exists since \(C\) is compact, let
  \begin{equation}
    f(x) = \psi_\epsilon \circ h
  \end{equation}

\end{enumerate}

Now there are other ways of doing it. One of them is to write down one integral formula from the beginning, but it would be harderto see just where this formula comes from in the first place. We can now get to defining partitions of unity:
\begin{theorem}
  Given \(A \subset \reals^n\) and an open cover \(\mc{O}\) of \(A\), there is a countable collection \(\Phi\) of \(\mc{C}^\infty\) functions \(\varphi\) on \(\reals^n\) with the following properties:
  \begin{enumerate}

    \item \(0 \leq \varphi \leq 1\)

    \item For all \(x \in A\), there is an open neighborhood \(V_x\) of \(x\) in \(\reals^n\) such that all but finitely many \(\varphi\) vanish on \(V_x\)

    \item For all \(x \in A\), \(\sum_{\varphi \in \Phi}\varphi(x) = 1\) (sum is finite in a neighborhood of \(x\))

    \item For all \(\varphi in \Phi\), there is some open \(U \in \mc{O}\) such that \(\varphi = 0\) outside a compact subset of \(U\).

  \end{enumerate}
\end{theorem}
\begin{definition}
  A collection \(\varphi\) satisfying (1),(2),(3) above is called a \underline{\(\mc{C}^\infty\) partition for \(A\)}. If (4) holds, this partition is called \underline{subordinate to \(\mc{O}\)}
\end{definition}
This is all \(\mc{C}^\infty\) stuff, but we could have made something weaker. We could have asked for a \(\mc{C}^r\) partition, or even a partition that was merely continous. So suppose we did that, and only cared about a \(\mc{C}^0\) partition. Then, we could also, though the proof will have to come next time, we could use functions that look like trapezoids, using \(|x|\) instead of \(e^{-\frac{1}{x^2}}\). If we wanted a \(\mc{C}^r\) partition of unity, then of course, we could do the same thing, but only caring about \(\mc{C}^r\) instead of \(\mc{C}^\infty\), and hence using the function given by
\begin{equation}
  f(x) = \left\{\begin{array}{ccc}
    |x^{r + 1}| & \text{on} & [0, \infty) \\
    -|x^{r + 1}| & \text{on} & (-\infty, 0]
  \end{array}\right.
\end{equation}

\end{document}


\documentclass[../research.tex]{subfile}

\subsection{Lebesgue Integration}
\label{sec:4.1}

\subsection{The Banach-Tarski Paradox}
\label{sec:4.3}

\subsection{Order Theory}
\label{sec:4.4}

    \begin{definition}[Partial order]
        Given a set $P$, a \textit{partial order}, $\leq$, is a binary relation on $P$ satisying the
        following:
        \begin{enumerate}
            \item Reflexivity: $p\leq p \quad\forall p\in P$
            \item Antisymmetry: $p\leq q\wedge q\leq p\Rightarrow$ $p=q\quad\forall p,q\in P$
            \item Transitivity: $p\leq q$, $q\leq r$ $\Rightarrow$ $p\leq r\quad\forall p,q,r\in P$
        \end{enumerate}
        We call a set with a partial order a \textit{poset}, $(P,\leq)$.
    \end{definition}

    \begin{definition}[Lattice]
    \end{definition}

\subsection{Diffeology}
\label{sec:4.5}

\subsection{Tensors}
\label{sec:4.6}

    \subsubsection{Dual space of a vector space}
    \label{sec:4.6.1}

        \begin{definition}[Linear functional]
        \end{definition}

    \subsubsection{Cotangent vectors}
    \label{sec:4.6.2}

    \subsubsection{Differential Forms}
    \label{sec:4.6.3}


\begin{intro}
  In the previous chapter, we studied discretizations with
  $\div V_h = Q_h$ with two advantages. First, due to
  \slideref{Corollary}{galerkin-mixed-u-kerb} the velocity error is
  independent of the pressure. Second, the divergence converges faster
  than the gradient. A natural question arising is whether we can do
  something similar for the Stokes problem. There, the equation
  \begin{gather}
    \form(\div v_h, q_h) = 0 \qquad\forall q_h\in Q_h,
  \end{gather}
  would immediately imply $\div v_h=0$, that is, the discrete solution
  is exactly divergence free.

  The answer to this question is a current research topic. So far,
  beginning with the element by Scott and Vogelius, several methods
  have been proposed for special mesh geometry or macro meshes. The
  difficulty is balancing the condition $\div V_h = Q_h$ with the
  $H^1$-conformity of the velocity space. All the spaces in the
  previous chapter were only $\Hdiv$-conforming with discontinuous
  tangential components.

  A fairly simple solution to this question though can be obtained by
  using discontinuous Galerkin methods. These were introduced to
  obtain formulations \emph{consistent} with $H^1$ while not
  \emph{conforming}. Thus, we can apply them directly to
  Raviart-Thomas and Brezzi-Douglas-Marini elements to obtain a
  consistent method with divergence free solutions.

  We begin this chapter by a quick review of the interior penalty
  method before diving into divergence conforming methods.
\end{intro}

\section{The interior penalty method}
\input{../fem/ip}
\subsection{Bounded formulation in $H^1$}
\input{../fem/ip-lifting}

\section{Divergence conforming IP}
\begin{remark}
  The extension of the interior penalty method to vector-valued
  problems is obvious. Furthermore, since the method generates an
  elliptic bilinear form on the discontinuous space $V_h$, this
  ellipticity is inherited by any subspace of
  $V_h\cap\Hdiv(\domain)$. Thus, we can write down the weak
  formulation of a divergence conforming DG method for the Stokes
  equations. In the following definition, we assume slip or no-slip
  boundary conditions, that is, $v\cdot\n=0$ on the whole boundary.
\end{remark}

\begin{Definition}{hdiv-ip}
  A divergence conforming DG method for the Stokes equations consists
  of a discrete velocity space $V_h\subset \Hdiv_0(\domain)$ and a
  pressure space $Q_h\subset L^2_0(\domain)$ such that
  \begin{gather}
    \label{eq:hdivdg:1}
    \div V_h = Q_h.
  \end{gather}
  Using the interior penalty bilinear form $a_h(.,.)$, we search for
  solutions $(u_h,p_h)\in V_h\times Q_h$ such that for all $(v,q)\in
  V_h\times Q_h$ there holds
  \begin{gather}
    \label{eq:hdivdg:2}
    a_h(u_h,v) +\form(\div v,p_h)+\form(\div u_h,q) = f(v).
  \end{gather}
\end{Definition}

\begin{remark}
  Due to the fact that $V_h\not\subset V$, we have introduced the  norm
  $\norm{.}_{1,h}$ on $V_h$. In particular, the norm $\norm{.}_1$ is
  not defined for all elements of $V_h$. Therefore, we need a
  modification of Fortin's lemma (\slideref{Lemma}{fortin}), where the
  norm on the left hand side of the stability
  estimate~\eqref{eq:galerkin:16} uses the discrete norm, namely,
  \begin{gather}
    \norm{\Pi_{V_h}v}_{V_h} \le c \norm{v}_V,
  \end{gather}
\end{remark}

\begin{Lemma}{dg-fortin}
  Let $\{\mesh_h\}$ be a shape-regular sequence of meshes. Then, the
  \putindex{canonical interpolation} operators of the
  Brezzi-Douglas-Marini and Raviart-Thomas elements admit the bound
  \begin{gather}
    \label{eq:hdivdg:4}
    \norm{I_h v}_{1,h} \le c \snorm{v}_1
  \end{gather}
\end{Lemma}

\begin{proof}
  First, we note that all degrees of freedom are defined as cell or
  face integrals with smooth weight functions. Thus, they are bounded
  on $H^1$. Thus, since the local polynomial spaces are finite
  dimensional, there holds on the reference cell $\refcell$ and its
  faces $\refface$:
  \begin{align}
    \norm{I_{\refcell} v}_{1;\refcell} &\le c \snorm{v}_{1;\refcell},
    \\
    \norm{I_{\refcell} v}_{0;\refface} &\le c \snorm{v}_{1;\refcell}.
  \end{align}
  On shape regular meshes, we have the scaling property
  \begin{align}
    \snorm{f}_{m;\cell} &\simeq h_\cell^{\frac{d}{2}-m},
    \\
    \snorm{f}_{m;\face} &\simeq h_\face^{\frac{d-1}{2}-m},
  \end{align}
  such that for a face $\face$ of cell $\cell$
  \begin{align}
    \norm{I_{\cell} v}_{1;\cell} & \le c \snorm{v}_{1;\cell},
    \\
    \norm{I_{\cell} v}_{0;\refface} &\le c h^{\frac12} \snorm{v}_{1;\cell}.
  \end{align}
  We conclude
  \begin{gather}
    \norm{I_h v}_{1,h}^2 \le \sum_{\cell\in\mesh_h}
    \biggl[\norm{I_{\cell} v}_{1;\cell}^2
    + 4 \sum_{\face\subset\d\cell}
    \norm*{\tfrac{\ipp_0}{h_F} I_{\cell} v}_{0;\face}^2
    \biggr] \le c \snorm{v}_{1}^2.
  \end{gather}
\end{proof}

\begin{Corollary}{hdivdg-infsup}
  Assume that the inf-sup condition~\eqref{eq:stokes:1} in
  \slideref{Theorem}{stokes-infsup} holds.
  Then, the method in \slideref{Definition}{hdiv-ip}
  admits the inf-sup condition
  \begin{gather}
    \label{eq:hdivdg:3}
    \inf_{q_h\in Q_h} \sup_{v_h\in V_h}
    \frac{\form(\div v_h,q_h)}{\norm{v_h}_{1,h}\norm{q_h}_0} \ge \beta,
  \end{gather}
  with a constant $\beta >0$ independent of $h$.
\end{Corollary}

\begin{proof}
  First, we make use of the fact that $q_h\in Q_h \subset Q$ to deduce
  from \slideref{Theorem}{stokes-infsup} the there is a function $w\in
  V$ with $\div v=q_h$ and $\norm{v}_1 \le \norm{q_h}_0$. To this
  function, we apply the \putindex{Fortin operator} to define $v_h =
  I_h v$. By the preceding lemma, we have
  \begin{gather}
    \norm{v_h}_{1,h} \le c \norm{v}_1 \le \norm{q_h}_0,
  \end{gather}
  which proves the inf-sup condition.
\end{proof}

\begin{Theorem}{hdivdg-convergence}
  Assume that $(u_h,p_h)\in V_h\times Q_h$ is the solution to the
  divergence conforming DG method in \slideref{Definition}{hdiv-ip} and
  that the continuous Stokes problem is well-posed as in
  \slideref{Theorem}{stokes-infsup}. Then, for the Raviart-Thomas
  pairs $RT_k/\P_k$ and $RT_{[k]}/\Q_k$ with $k\ge 1$ and
  $u$ sufficiently smooth there holds
  \begin{align}
    \label{eq:hdivdg:5}
    \norm{u-u_h}_{1,h} &\le h^k \snorm{u}_{k+1}, \\
    \label{eq:hdivdg:7}
    \norm{p-p_h}_{0} &\le h^k \bigl(\snorm{u}_{k+1} + \snorm{p}_{k}\bigr).
  \end{align}
  Furthermore,
  \begin{gather}
    \label{eq:hdivdg:6}
    \div u_h = 0.
  \end{gather}
\end{Theorem}

\begin{proof}
  The proof follows the lines of the abstract theory of
  \slideref{Theorem}{galerkin-mixed-u-kerbh} and
  \slideref{Theorem}{galerkin-mixed-p}. But since the setting with
  $V_h\not\subset V$ exceeds the assumptions of the abstract theory,
  we adapt the proofs instead of using the results.

  Due to consistency of the method, we have
  \begin{gather}
    a_h(u-u_h, v_h) + \form(\div v_h, p-p_h)
    + \form(\div u-\div u_h, q_h) = 0.
  \end{gather}
  Testing with $v_h=0$ and using $\div V_h = Q_h$ immediately yields
  $\div u_h = \div u = 0$, or
  \begin{gather}
    \ker{B_h} \subset \ker B.
  \end{gather}
  In order to use the ellipticity of $a_h(.,.)$, we insert
  arbitrary functions $w_h \in \ker{B_h}$ and $r_h\in Q_h$. Choosing
  $q_h = 0$ yields the error equation
  \begin{gather}
    \label{eq:hdivdg:8}
    a_h(u_h-w_h, v_h) + \form(\div v_h, p_h-r_h)
    = a_h(u-w_h, v_h) + \form(\div v_h, p-r_h).
  \end{gather}
  Testing with $v_h=u_h-w_h$ and employing $\div v_h=0$, we obtain
  \begin{gather}
    \ellipa \norm{u_h-w_h}_{1,h}^2
    \le a_h(u_h-w_h,u_h-w_h)
    = a_h(u-w_h,u_h-w_h).
  \end{gather}
  Now, we use the canonical interpolation $w_h = I_h u$ to obtain
  \begin{gather}
    \ellipa \norm{u_h-w_h}_{1,h}^2
    \le \frac\ellipa2 \norm{u_h-w_h}_{1,h}^2
    + \frac{c}{2\ellipa} h^{2k} \snorm{u}_{k+1}^2.
  \end{gather}

  Finally, we use the inf-sup condition to find a test function
  $v_h\in V_h$ such that $\div v_h = p_h-r_h$ and $\beta \norm{v_h}_{1,h}
  \le \norm{p_h-r_h}$. Then, the error equation~\eqref{eq:hdivdg:8}
  yields
  \begin{multline}
    \norm{p_h-r_h}
    = \frac{\form(\div v_h,p_h-r_h)}{\norm{p_h-r_h}}
    \\
    = \frac{a_h(u-u_h, v_h) + \form(\div v_h, p-r_h)}
    {\norm{p_h-r_h}}
    \le \tfrac{\norm{a_h}}\beta \norm{u-u_h}_{1,h}
    + \norm{p-r_h}_0
    .
  \end{multline}
  Using the previously proven error estimate for $u_h$ and the
  $L^2$-projection $r_h = \Pi_h p$ yields the result.
\end{proof}

\section{Error estimates by duality}

\begin{intro}
  So far, we have only considered estimates in the so called
  \putindex{energy norm}, that is, a norm such that $a_h(.,.)$ is
  bounded and elliptic\footnote{We use the term energy norm loosely
    here. Strictly speaking, the energy norm would be
    $\norm{v}_A = \sqrt{a_h(v,v)}$.}.

  In the context of elliptic equations, we have seen the duality
  argument of Aubin and Nitsche, which allows us to obtain optimal
  estimates in weaker norms, for instance in $L^2$.

  A particular difficulty here is the fact, that we have to test the
  dual solution with the error \emph{and} exploit some kind of
  Galerkin orthogonality. Thus, we cannot use consistency as before
  and will introduce residual operators later. The analysis here is a
  simplified version of the corresponding results
  in~\cite{GiraultKanschatRiviere14}.
\end{intro}

\begin{Definition}{dual-stokes}
  The \putindex{dual problem} to the Stokes problem in weak for
  consists of finding $(u^*,p^*)\in V_h\times Q_h$ such that for all
  $v\in V$ and $q\in Q$ there holds
  \begin{gather}
    \label{eq:hdivdg:9}
    \form(\nabla v,\nabla u^*) + \form(\div u^*,q) + \form(\div v,p^*)
    = \form(\psi,v).
  \end{gather}
\end{Definition}

\begin{Assumption}{stokes-regularity}
  The dual Stokes problem admits the elliptic regularity estimate
  \begin{gather}
    \label{eq:hdivdg:10}
    \norm{u^*}_{2} \le c \norm{f}_0.
  \end{gather}
\end{Assumption}

\begin{remark}
  Like for scalar elliptic equations, the elliptic regularity
  assumption holds for domains with smooth boundary or with piecewise
  smooth boundary where every corner is convex.
\end{remark}

\begin{Definition}{hdivdg-residual-operators}
  For the solutions $(u,p)\in V\times Q$ and $(u^*,p^*)\in V\times Q$
  of the primal and dual Stokes problem, respectively, we define the
  residual operators
  \begin{align}
    \operatorname{Res}(u,p;v) &= a_h(u,v)+\form(\div v,p) - \form(f,v),
    \\
    \operatorname{Res}^*(v;u^*,p^*)
                              &= a_h(v,u^*)+\form(\div v,p^*) -
                                \form(\psi,v),
  \end{align}
  for $v\in V+V_h$.
\end{Definition}

% From Girault/Kanschat/Riviere

\begin{Lemma}{hdivdg-residual-1}
  Let $(u,p)\in V\times Q$ be the solution to the Stokes problem with
  right hand side $f\in L^2(\domain;\R^d)$. Assume $u\in
  H^s(\domain;\R^d)$ and $p\in H^{s-1}(\domain)$ with $s>3/2$. Then,
  we have for $v\in V+V_h$:
  \begin{gather}
    \label{eq:hdivdg:11}
    \form(f,v) = \form(\nabla u,\nabla v)_{\mesh_h}
    -\forme(\nabla u,\mvl{v\otimes n})_{\faces_h^i}
    -\forme(\d_n u,v)_{\faces_h^\d}
    + \form(\div v,p).
  \end{gather}
\end{Lemma}

\begin{proof}
  We set out from the strong form of the Stokes equations and
  integrate by parts.
  \begin{align}
    \form(f,v) &= \form(-\Delta u + \nabla p, v)
    \\
    &= \form(\nabla u,\nabla v)_{\mesh_h}
      - \sum_{\cell\in\mesh_h} \forme(\d_n u,v)_{\d\cell}
      - \form(\div v,p)
      .
  \end{align}
  Under the regularity assumptions of the lemma, all of these
  integrals make sense at least as duality pairings. In particular,
  $\d_n u\in L^2(\d\cell)$, and thus we can split $\d\cell$ into
  individual faces. Therefore,
  \begin{gather}
    \sum_{\cell\in\mesh_h} \forme(\d_n u,v)_{\d\cell}
    = \forme(\nabla u,\mvl{v\otimes n})_{\faces_h^i}
    +\forme(\d_n u,v)_{\faces_h^\d}.
  \end{gather}
  The proof concludes by collecting the results.
\end{proof}

\begin{Corollary}{hdivdg-residual-2}
  The residual operators can be expressed as
  \begin{gather}
    \label{eq:hdivdg:12}
    \begin{split}
    \operatorname{Res}(u,p;v)
    &= a_h(u,v)
      - \form(\nabla u,\nabla v)_{\mesh_h}
      \\
      &\qquad
      + \forme(\nabla u,\mvl{v\otimes n})_{\faces_h^i}
      +\forme(\d_n u,v)_{\faces_h^\d}.
      \\
    \operatorname{Res}^*(u^*,p^*;v)
    &= a_h(v,u^*)
      - \form(\nabla u,\nabla v)_{\mesh_h}
      \\
      &\qquad
      + \forme(\nabla u,\mvl{v\otimes n})_{\faces_h^i}
      +\forme(\d_n u,v)_{\faces_h^\d}.
    \end{split}
  \end{gather}
  In particular, the residual operators do not depend on the pressure
  solutions.
\end{Corollary}

\begin{Theorem}{hdivdg-l2}
  Let the assumptions of \slideref{Theorem}{hdivdg-convergence}
  and \slideref{Assumption}{stokes-regularity} hold. Then,
  \begin{gather}
    \norm{u-u_h}_0 \le c h^{k+1} \snorm{u}_{k+1}.
  \end{gather}
\end{Theorem}

\begin{Problem}{hdivdg-l2}
  Adapt the proof of \slideref{Theorem}{ip-lifting-l2} to prove
  \slideref{Theorem}{hdivdg-l2}.
\begin{solution}
   We again consider the auxiliary problem
  \begin{gather}
    a(v,u^*) +(\nabla\cdot v, p^*)= \form(u-u_h,v),\qquad\forall v\in V.
  \end{gather}
  Using the definition of the dual residual, we obtain the equation
  \begin{gather}
    \form(u-u_h, v) = a_h(v,u^*) +(\nabla\cdot v, p^*)- \Res^*(u^*,p^*;v),\qquad\forall v\in V+V_h.
  \end{gather}
  Testing with $v=u-u_h$ yields
  \begin{gather}
    \norm{u-u_h}^2 = a_h(u-u_h,u^*) +(\nabla\cdot (u-u_h), p^*)- \Res^*(u^*, p^*;u-u^*).
  \end{gather}
  Additionally, we us the error equation
  \begin{gather}
    a_h(u-u_h, v_h)+(\nabla\cdot v_h, p-p_h) = \Res(u,p;v_h),
  \end{gather}
  tested with $v_h = I_h u^*$, to obtain
  \begin{align}
    \norm{u-u_h}^2 &= a_h(u-u_h,u^*-I_h u^*) +(\nabla\cdot (u-u_h), p^*-q_h)\\
    &\quad- \Res^*(u^*, p^*;u-u^*)
    -\underbrace{(\nabla\cdot I_h u^*, p-p_h)}_{0} + \Res(u,p;I_h u^*).
  \end{align}
  Using the regularity of $u^*$, the first term on the right
  admits the estimate
  \begin{gather}
    \abs{a_h(u-u_h, u^*-I_h u^*)}
    \le \norm{u-u_h}_{1,h}\norm{u^*-I_hu^*}_{1,h}
    \le c h \norm{u-u_h}_{1,h}.
  \end{gather}
  as before.

  For the second term consider $q_h=\Pi_{Q_h} p^*$
  \begin{align}
   (\nabla\cdot (u-u_h), p^*-\Pi_{Q_h} p^*)\leq \norm{\nabla \cdot  (u-u_h)} \norm{p^*-\Pi_{Q_h} p^*}=0
  \end{align}

  For the third term we use \slideref{Lemma}{ip-lifting-residual-2}
  to obtain
  \begin{gather}
    \abs{\Res^*(u^*,u-u_h)} \le c h \snorm{u^*}_2 \norm{u-u_h}_{1,h}.
  \end{gather}
  Finally, using $\jmp{u^*} = 0$, the same lemma yields
  \begin{align}
    \abs{\Res(u, I_h u^*)}
    &\le c h \snorm{u}_2
      \bigl(\norm{\sqrt{\ipp_h}\jmp{I_h u^*}}_{\faces_h^i}
      + \norm{\sqrt{\ipp_h}I_h u^*}_{\faces_h^\d}\bigr)
    \\
    & = c h \snorm{u}_2
      \bigl(\norm{\sqrt{\ipp_h}\jmp{u^*-I_h u^*}}_{\faces_h^i}
      + \norm{\sqrt{\ipp_h}(u^*-I_h u^*)}_{\faces_h^\d}\bigr)
    \\
    & \le c h \snorm{u}_2 h^k \snorm{u^*}_{k+1}
  \end{align}
  This is exactly the same proof we used before.
  Using the energy estimate in \slideref{Theorem}{ip-lifting-h1} we
  can conclude the prove.
\end{solution}

\end{Problem}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "main"
%%% End:


% Appendix A

\chapter{EA training algorithm} % Main appendix title

\label{AppendixA} % For referencing this appendix elsewhere, use \ref{AppendixA}

\lhead{Appendix A. \emph{EA training algorithm }} % This is for the header on each page - perhaps a shortened title

\begin{lstlisting}[caption={EA training algorithm}, label={lst:ea-training},]
% data structure
Struct Individual {
    won_against = []
    lost_against = []
    fitness = 0
}

% The TeachingIndividual is only here for indication,
% you can have a different set of metadata
Struct TeachingIndividual extends Individual {
    strategy_name: string
}

Struct LearningIndividual extends Individual {
    values: array representing the training parameters
}

% hyperparameters
iterations = 100
mutation_rate = 0.1
trainable_params = 10
nb_population = 10
new_member_per_population = 7
individual_kept_nb = nb_population - new_member_per_population

% initialization
training_individuals = []
FOR _ in nb_population:
    training_individuals.append(LearningIndividual(
        values: random number array from -1 to 1 of size trainable_params,
    ))
teaching_individuals = [
    TeachingIndividual(strategy_name: "equilibrium")
    TeachingIndividual(strategy_name: "random")
    TeachingIndividual(strategy_name: "bet")
    TeachingIndividual(strategy_name: "fold")
    TeachingIndividual(strategy_name: "close_equilibrium")
]

% run
FOR _ in iterations:
    population = training_individuals + teaching_individuals

    % make them play against eachother
    FOR i = 0; i to population.size():
        FOR j = i + 1; j to population.size():
            a, b = population[i], population[j]
            result = evaluate(a, b)
            IF result == WIN:
                a.won_against.append(b)
                b.lost_against.append(a)
            ELSE IF result == LOST:
                b.won_against.append(a)
                a.lost_against.append(b)

    % assign fitness
    FOR individual in population.size():
        individual.fitness = 0
        FOR looser_individual in individual.lost_against:
            individual.fitness += 1 / looser_individual.lost_against.size()

    % generate new population
    selected_individuals = randomly select new_member_per_population
                             of training_individuals proportionaly to their fitness
    FOR new_individual in selected_individuals:
        new_individual.values = randomly change the values from -1 to 1
                                  with a probability of mutation_rate

    % process selection and assign a new population
    individual_kept = select individual_kept_nb best training_individuals
                        based on their fitness
    training_individuals = selected_individuals + individual_kept
\end{lstlisting}


\subsubsection{\stid{3.15} Sake: Trilinos/PEEKS} \label{subsubsect:trilinos}
\paragraph{Overview}
Trilinos is a large and widely used toolkit for scientific computing, with many users both at DOE labs, in academia, and in industry.
This project is focused on making Trilinos ready for exascale. One part is to port a core set of Trilinos packages to relevant architectures
(including NVIDIA, AMD, and Intel GPUs). The other part is to design algorithms that work well on accelerators and at large scale (the focus of
the PEEKS sub-project).

\paragraph{Key  Challenges}
The Trilinos software library needs to be adapted to the new architectures. This is a large undertaking as there are many packages. Therefore, we focus on a core subset of Trilinos related to linear algebra and solvers. We are sensitive that Trilinos has a large user base and therefore need to minimize any user interface changes. Performance portability across a wide range of platforms is a key challenge.

Developing scalable iterative (Krylov) solvers for the US leadership supercomputers
deployed in ECP, we acknowledge three major challenges coming from the hardware
architecture:
\begin{enumerate}
\item
Performance portability for Krylov solvers to perform well on different architectures with a single code base
such that the applications relying on Trilinos for their linear solver needs can smoothly transition to
the new ECP machines.
\item
Fine-grained parallelism in a single node that has to be exploited efficiently
by the iterative solver and the preconditioner.
\item
Rising communication and synchronization cost as the
computational power is growing much faster than memory power, resulting in
increased pressure on the bandwidth of all cache/memory levels.
\end{enumerate}

These challenges require the redesign of existing iterative solvers with respect
to higher parallelism, a reduced number of
communication and synchronization points, favoring computations over
communication, and possibly adopting multiprecision algorithms for efficient hardware
utilization.

\paragraph{Solution Strategy}

The primary thrusts of the Trilinos/PEEKS project are:
\begin{enumerate}
  \item Performance portability:
        We plan to rely heavily on the Kokkos and Kokkos Kernels libraries as that provide kernels that are performance portable across a variety of platforms, including CPU and GPU (NVIDIA, AMD, Intel). There is still much porting work, as some packages rely on UVM (Unified Virtual Memory), which is not widely supported.
        We will ensure the readiness of the following four solver packages on ECP platforms:
        distributed linear algebra (Tpetra), Krylov solvers (Belos), algebraic preconditioners and smoothers (Ifpack2),
        and direct solver interfaces (Amesos2).
  \item Low-synchronization Krylov methods:
    	We will develop and deploy pipelined and
	communication-avoiding Krylov methods in production-quality code, and
	we are actively collaborating with the ECP ExaWind project to integrate
        our new features into their application. Another bottleneck we will address is the orthogonalization needed in GMRES (such as CGS and MGS).
\end{enumerate}

\paragraph{Recent Progress}
\begin{enumerate}
\item Removal of dependency on UVM:
we have removed the dependence of the four solver packages on UVM.
On NVIDIA GPUs, UVM simplifies coding by providing automatic data migration between the CPU and GPU memory. However, there are concerns about how well this will be supported, especially in term of performance, on future GPU systems such as Frontier and Aurora. Hence, removing UVM usage was a prerequisite for preparation to run on Frontier and Aurora platforms.
More generally, the dependence on UVM posed a risk in term of the solver performance, while explicitly managing the data movement will likely improve solver performance.

\item HIP backend support:
we have ensured the Trilinos solver stacks run on the Frontier early access system (Spock) using HIP backend (without UVM) and resolved initial performance problems. Several performance optimizations remain open which we plan to address this year.

\item Polynomial preconditioning: We implemented and deployed a GMRES-based polynomial preconditioner in Trilinos/Belos. This can be used as a preconditioner by itself, as a smoother in multigrid (MueLu), or be combined (nested) with any existing preconditioner. Our code is portable to both CPU and GPU. We showed the method is robust and it does not need any user knowledge of eigenvalues of the matrix/operator, unlike the Chebyshev method. In collaboration with the Exagraph ECP project, it has been integrated into the Sphynx spectral partitioner (Trilinos/Zoltan2).

\item Low-synch orthogonalization: In collaboration with CU Denver and NREL, we developed and implemented a novel low-synch version of Classical Gram-Schmidt (CGS), called DCGS2. By delaying orthonalization and norms, we reduce the synchronization requirement to once per iteration (even with two passes of CGS). The method is more numerically stable than previous attempts at low-synch orthogonalization. Preliminary results on Summit show that the new DCGS2 outperforms the current CGS2 and MGS methods, and achieves up to 66 Gflop/s on 192 GPUs.
\end{enumerate}

\paragraph{Next Steps}
Our next efforts are:
\begin{enumerate}
\item Support OpenMP and SYCL backends and
      perform baseline performance assessments to determine which kernels or algorithms need performance optimization
      on platforms that are relevant to ECP.
\item Conduct performance optimization using HIP, SYCL, and OpenMP backends on the platforms and problems
      that are relevant to ECP.
\item Implement graph assembly on a GPU to enable faster matrix assembly and to avoid data movement between host and device.
\item Design an unified solver interface to all the Trilinos solvers in order to ease the effort of ECP applications
      to switch between solvers and also to compose different solvers in one framework, both of which are needed for the success of ECP application projects especially when the solver options differ across architectures.
\item Deploy DCGS2 (or related orthogonalization method) in Trilinos/Belos, likely as an option in GMRES (which is used by many ECP applications).
\item Study Block Krylov methods, which may be particularly well suited to GPUs, and also appear to reduce communication.
\end{enumerate}


\subsection{User-defined Functions}

As your code becomes longer,
you will start to realize that similar processing or
calculation appears several times in a macro or through macro sets.
To simplify such redundancy, one could write a
separate \ilcom{function} that works as a module for macros.
For example, if you have a simple code like:
\lstinputlisting[morekeywords={*,}]{code/code15.ijm}
It should be easy for you to expect that this macro will print out "3" in the Log window.
From this macro, we could extract part of it and make a separate function.
\lstinputlisting[morekeywords={*,}]{code/code15_1.ijm}
This is not a macro, but is a program that works as a unit.
Functions can be embedded in macro. \ilcom{ReturnAdd }(code 15.1) is the name of the function,
and the following \ilcom{(n, m)} are the variables that will be used in the function. Within the function,
n and m will be added and the result of which is substituted in to a new variable p.
\ilcom{return p} in line 4 will return a value as an output of the function.
We call such custom-made function as ``user-defined function''. Using this function, code 15 can be rewritten as
\lstinputlisting[morekeywords={*,}]{code/code15_2.ijm}
or simpler, by nesting the custom made function inside ImageJ native function \ilcom{print()},
\lstinputlisting[morekeywords={*,}]{code/code15_3.ijm}
Macro interpreter reads the macro line by line. When the interpreter sees \ilcom{ReturnAdd(a, b)},
the interpreter first tries to find the function within the ImageJ Build-in function.
If its not there, the interpreter looks for the function within the same macro file\ldots
(user-defined function (e.g. \ilcom{ReturnAdd(a, b)} must be written in the same macro file.
Here is how it looks like: a macro that uses a function.
%figure
\begin{figure}[htbp]
\begin{center}
\includegraphics[scale=0.6]{fig/fig2411_usingFunction.png}
\caption{A macro file with function}
\label{fig:MacroWithFunction}
\end{center}
\end{figure}

In this simple case,
you might not feel the convenience of the user-defined function,
but you will start to feel its power as you start writing longer codes.
Advantages of using \ilcom{function} are
\begin{enumerate}
\item Once written in a macro file, it could be used as a single line function
as many times as you want in the macro file. This also means that if there is a bug,
fixing the function solves the problem in all places where the function is used.
\item Long codes could be simplified to an explicit outline of events. Such as:
\begin{lstlisting}[numbers=none]
macro "whatever" {
    	function1;
		function2;
		function3;
}
\end{lstlisting}
\end{enumerate}

Let's go back to the code 14, the automatic threshold adjusting macro.

At the beginning of the code, we check if the active image if it is a stack.
There is another check after that, to see if the image is adjusted with threshold level.

\lstinputlisting[linerange={1-10}]{code/code14.ijm}

We can make a function for checking stack (line 3 to 5) and another function that checks
if the stack is adjusted with threshold level (from line 6 to 9) as below.

\lstinputlisting{code/code16_17functions.ijm}

Then the initial part of code 14 (line 3 to 9) can now be replaced with
these two functions\footnote{For a complete coding of 14.1,
getThreshold(lower, upper) should appear again in line 8 to get lower and upper threshold value
of the reference image.}.

\lstinputlisting[linerange={1-5}]{code/code14_1.ijm}

\begin{indentexercise}{1}
The following macro asks the user to input x and y coordinates of two points,
calculate the distance between those points and prints out the distance.
Modify the code so that the distance calculation is done in a separate function.

\lstinputlisting{code/code18.ijm}

Note that function \ilcom{pow()} in the code is defined as
\begin{indentCom}
\textbf{pow}(base, exponent)\\
Returns the value of base raised to the power of exponent.
\end{indentCom}
For example, \ilcom{pow(4, 2)} returns 16.
\end{indentexercise}


%\begin{document}
\section{Integrated Autocorrelation Time}%
\label{sec:integrated_autocorrelation_time}
%
We include below plots of the integrated autocorrelation time for the
topological charge, \(\tau_{\mathrm{int}}^{\mathcal{Q}}\), for \(\mathcal{Q}
\in \mathbb{Z}\).
%
\begin{figure}[htpb]
  \centering
  \begin{subfigure}{\textwidth}
    \centering
    \includegraphics[width=\textwidth]{autocorrs/tau_int_vs_draws}%
    \caption{\label{fig:tau_int_vs_draws}Estimate of \(\tau_\mathrm{int}\) vs
      number of draws (length of chain) across \(\beta = 5\) (left), \(\beta =
      6\) (center), and \(\beta = 7\) (right), for both HMC (grayscale) and the
    trained sampler (pink).}
    % \caption{Estimate of \(\tau_{\mathrm{int}}^{\mathcal{Q}}\) vs number of
    %   samples, $N$ for \(\beta = 5\) (left), \(6\) (middle), and \(7\)
    %   (right).}%
  \end{subfigure}
  \begin{subfigure}{\textwidth}
    \centering
    \includegraphics[width=\textwidth]{autocorrs/tau_int_vs_traj_len}
    \caption{\label{fig:tau_int_vs_traj_len}Estimate of \(\tau_{\mathrm{int}}\)
      vs trajectory length, \(\lambda\) across \(\beta = 5\) (left), \(\beta =
      6\) (center), and \(\beta = 7\) (right), for both HMC (grayscale) and the
    trained sampler (pink).}
    % \caption{Estimate of \(\tau_{\mathrm{int}}^{\mathcal{Q}}\) vs trajectory
    %   length, \(N_\mathrm{LF} \cdot \varepsilon\) for \(\beta = 5\) (left), \(6\)
    %   (middle), and \(7\) (right).}%
      % \label{fig:tau_int_vs_traj_len}
  \end{subfigure}%
  \caption{Intermediate steps in the calculation of the integrated
    autocorrelation time \(\tau_{\mathrm{int}}^{\mathcal{Q}}\) vs \(\beta\) for
  \(\beta = 5\) (left), \(6\) (middle) \(7\) (right).}
  % \begin{subfigure}{\textwidth}
  %   \centering
  %   \includegraphics[width=0.7\textwidth]{autocorrs/tau_int_vs_beta}
  %   \caption{Estimate of \(\tau_{\mathrm{int}}^{\mathcal{Q}}\) vs \(\beta\) for
  %     both HMC and L2HMC samplers.}%
  %   \label{fig:tau_int_vs_beta}
  % \end{subfigure}
\end{figure}
%
\begin{figure}[htpb]
  \centering
  \includegraphics[width=0.475\textwidth]{autocorrs/tau_int_vs_beta}
  \caption{Estimate of \(\tau_{\mathrm{int}}^{\mathcal{Q}}\) vs \(\beta\) for both
    HMC and L2HMC samplers.}%
  \label{fig:tau_int_vs_beta}
\end{figure}


\section{The auxiliary function \makepair}
\label{sec:makepair}

In order to be able to specify functions that work
on pairs of indices we introduce in the following listing
the type \sizetypepair.

\begin{listing}[hbt]
\begin{minipage}{0.99\textwidth}
\lstinputlisting[style=acsl-block, frame=single]{Source/size_type_pair.h}
\end{minipage}
  \caption{\Label{lst:size_type_pair}The type \sizetypepair}
\end{listing}

\index[examples]{size\_pair\_type@\texttt{size\_pair\_type}}

%\clearpage

We will also use the auxiliary function \makepair which turns
two indices \inl{first} and \inl{second} into an object of \sizetypepair.
The specification and implementation of \specref{makepair} is shown here.

\input{Listings/make_pair.h.tex}

\clearpage


\subsection{User defined functional form}
\label{sec:jastrowuserform}

To aid in implementing different forms for $u_{ab}(r)$, there is a script that uses a symbolic expression to generate the appropriate code (with spatial and parameter derivatives).
The script is located in \texttt{src/QMCWaveFunctions/Jastrow/codegen/user\_jastrow.py}.
The script requires Sympy (\url{www.sympy.org}) for symbolic mathematics and code generation.

To use the script, modify it to specify the functional form and a list of variational parameters.
Optionally, there may be fixed parameters - ones that are specified in the input file, but are not part of the variational optimization.
Also one symbol may be specified that accepts a cusp value in order to satisfy the cusp condition.
There are several example forms in the script.  The default form is the simple Pad\'e.

Once the functional form and parameters are specified in the script, run the script from the \texttt{codegen} directory and recompile QMCPACK.
The main output of the script is the file \texttt{src/QMCWaveFunctions/Jastrow/UserFunctor.h}.
The script also prints information to the screen, and one section is a sample XML input block containing all the parameters.

There is a unit test in \texttt{src/QMCWaveFunctions/test/test\_user\_jastrow.cpp} to perform
some minimal testing of the Jastrow factor.   The unit test will need updating to properly test
new functional forms.   Most of the changes relate to the number and name of variational
parameters.

\begin{table}[h]
\begin{center}
\begin{tabular}{l c c c l }
\hline
\multicolumn{5}{l}{Jastrow element} \\
\hline
\bfseries name & \bfseries datatype & \bfseries values & \bfseries defaults  & \bfseries description \\
\hline
name & text &    & (required) & Unique name for this Jastrow function \\
type & text & One-body & (required) & Define a one-body function \\
     &      & Two-body & (required) & Define a two-body function \\
function & text & user & (required) & User-defined functor \\
\multicolumn{5}{l}{See other parameters as approriate for one or two-body functions} \\
  \hline
\multicolumn{5}{l}{elements}\\ \hline
& Correlation & & & \\ \hline
\multicolumn{5}{l}{Contents}\\ \hline
& (None)  & & &  \\ \hline
\end{tabular}
%\end{tabular*}
\end{center}
\end{table}