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

\subsection{Pairwise Swap}
\label{q:pairswap}
\begin{question}
Consider p processes (p is even), each process has 1 data element. The even processes s swap their element with their neighbour s+1 (= pair-wise swap).
\begin{enumerate}
	\item Give a BSP algorithm.
	\item What is the $h$-relation?
	\item Suppose (non-BSP) 2-sided send and receive. When send transmits, hold execution until remote process receives. Then execute the communication and both processes continue. This is blocking pair-wise communication (as featured in MPI). Give the algorithm.
	\item Executing a send-receive communication has start-up time $s$, and data is transfered with bandwidth $b$. Is $s < l$? Will the cost be higher than the BSP algorithm from 1.1?
\end{enumerate}
\end{question}
\begin{solution} The solution to the subquestions are given below.
\begin{enumerate}
	\item The BSP program for one-to-all communication is given below.
\begin{lstlisting}
void pairwiseSwap(){
	bsp_begin(P);
	bsp_push_reg(element);
	// Important: The effect of bsp_push_reg only takes effect at the next superstep
	bsp_sync();
	int s = bsp_pid();
	if(s % 2 == 0){
		bsp_put(s+1, data, element, 0, sizeof(data));
	}else if{
		bsp_put(s-1, data, element, 0, sizeof(data));
	}
	bsp_sync();
	bsp_end();
}
\end{lstlisting}
	\item The $h$-relation is given by $h = max\{h_s,h_r\}$ \cite[p.~5]{bisseling04}. In the previous case, a processor sends one element and receives one. Therefore, $h = 1$. The total cost is $T(h) = g + l$.
	\item The non-BSP program for one-to-all communication is given below. We assume primitives \texttt{send(message, dest)} and \texttt{receive(message, sender)}. The data is not buffered.
\begin{lstlisting}
void pairwiseSwap(){
	init_comm(s,P);

	if(s % 2 == 0){
		send(s+1, data);
		receive(data, sendproc);
		if(sendproc != s + 1)
			ERROR
	}else if{
		receive(data, sendproc);
		send(s-1, data;
		if(sendproc != s - 1)
			ERROR
	}
}
\end{lstlisting}
\item The message-passing cost is modeled as $T(n) = s + bn$. Because each processor sends one element and receives another, the total cost is given by $T = 2(s+b)$. Assuming the same cost per word ($g=b$), the BSP algorithm is faster unless $2(s+g) < g + l$ or $2s < l - g$.
\end{enumerate}
\end{solution}

\subsection{SpMV}
\label{q:spmv}
\begin{question}
$A$ is a sparse $M \times N$ matrix, $x$ and $y$ are vectors.
\begin{enumerate}
	\item What is the sequential cost of $y=Ax$?
	\item Assume $A$ is distributed row-wise in a 1D fashion. Which phase, if any, of the classic SpMV phases (fan-out, SpMV, fan-in) will disappear? What will be the new total cost? What will be the parallel overhead?
	\item $H=(V,N)$ is a hypergraph of A. What hypergraph model would you use to distribute A using the previous 1D distribution type? Give definitions of $V$ and $N$ in that model.
	\item Assume SpMV algorithm 4.5 as in the book. Use your definition of $H$ from the previous question to write a cost function that measures the number of gets and puts.
\end{enumerate}
\end{question}
\begin{solution} The solution to the subquestions are given below.
\begin{enumerate}
	\item The sequential cost of $y=Ax$ is $T_{seq} = 2cm$ flops \cite[p.~166]{bisseling04} where $c$ is the fixed number of nonzeroes in a row of A and $m$ the amount of rows. This holds because the used data-structures make it possible to only take multiplications with a nonzero into account.
	\item The fan-in step will disappear. Because if a processor holds an entire row it can calculate the partial sum $y_i$ of that row easiliy without relying on any other partial computations \cite[p.~176]{bisseling04}.  The total (worst case) cost of the unchanged algorithm is
	\begin{equation}
		T_{MV} \leq \frac{2cn}{p} + n + 2 \Big( 1 - \frac{1}{p} \Big) ng + 4l
	\end{equation}
	$T_{MV}$ is the sum of the worst case costs of the supersteps:
	\begin{equation}
		T_{(0)} = \left(1 - \frac{1}{p} \right) ng + l
	\end{equation}
	\begin{equation}
		T_{(1)} = 2cn/p + l
	\end{equation}
	\begin{equation}
		T_{(2)} = \left( 1-\frac{1}{p} \right)ng + l
	\end{equation}
	\begin{equation}
		T_{(3)} = n + l
	\end{equation}
	These equations are for an $n \times n$ matrix \cite[p.~178]{bisseling04}. Because there is only one partial sum per row with the given distribution $T_{(3)}' = \frac{n}{p} + l $. Omitting the fan-in step means that $T_{(2)}$ is not applicable. In the rest of the costs we also have to take into account we are working with an $m \times n$ matrix which gives us:
		\begin{equation}
			T_{(0)} = T_{(0)}' = \left(1 - \frac{1}{p} \right) ng + l
		\end{equation}
		\begin{equation}
			T_{(1)}' = \frac{2c\mathbf{m}}{p} + l
		\end{equation}
		\begin{equation}
			T_{(3)} = T_{(3)}' = n + l
		\end{equation}
		\begin{equation}
			T_{MV}' \leq \frac{2c\mathbf{m}}{p} + \frac{n}{p} + \Big(1 - \frac{1}{p} \Big) ng + 3l
		\end{equation}
			The parallel overhead overhead is defined as the difference between the normalized cost and the ideal 1. So the overhead here is \cite[p.~141]{bisseling04}:
			\begin{equation}
				O = 1 - \frac{T_{MV}'}{T_{seq}/p} \leq 1 - \frac{\frac{2cm}{p} + \frac{n}{p} + \Big(1 - \frac{1}{p} \Big) ng + 3l}{2cm/p}
			\end{equation}
	\item ``A partitioning of a column-net model of A corresponds to a partitioning of the rows of A (a 1D row-wise distribution)'' \cite{slides6}. The colum-net model $H=(V,N)$ is defined as following: Let $I = \{0,1,\ldots,m-1\}$ and $J = \{0,1,\ldots,n-1\}$ then $I$ represents the rows and thus $V = I$ and $\forall j \in J$ we define a net $n_j \in N$ with
	\begin{equation}
		n_j = \{i \in I ~|~ a_{ij} \neq 0\}
	\end{equation}
	The hypergraph of the example $5 \times 4$ matrix is $\mathcal{H}=(\mathcal{V} =\{1,\ldots,5\},\mathcal{N} = \{\{4\},\{0, 3\},\{1,4\},\{2\}\})$ and it's graphical representation can be found in \autoref{fig:hypergraph}.

	\begin{equation}
		A_{5\times4} = \begin{bmatrix}
		0 & 3 & 0 & 0 \\
		0 & 0 & 1 & 4 \\
		0 & 0 & 0 & 0 \\
		0 & 5 & 0 & 0 \\
		9 & 0 & 2 & 0
		\end{bmatrix}
	\end{equation}

	\begin{figure}[H]
	\begin{tikzpicture}
		\node[vertex,label=above:\(v_0\)] (v0) {};
		\node[vertex,below of=v0,label=above:\(v_1\)] (v1) {};
		\node[vertex,right of=v0,label=above:\(v_2\)] (v2) {};
		\node[vertex,right of=v1,label=above:\(v_3\)] (v3) {};
		\node[vertex,right of=v2,label=above:\(v_4\)] (v4) {};
		\begin{pgfonlayer}{background}
			\draw[edge,color=red] (v4) -- (v4);
			\draw[edge,opacity=1,color=green] (v0) -- (v3);
			\draw[edge,color=yellow] (v1) -- (v4);
			\draw[edge,color=blue] (v2) -- (v2);
		\end{pgfonlayer}

		\node[elabel,color=red,label=right:\(n_0\)]  (n0) at (-3,0) {};
		\node[elabel,below of=n0,color=green,label=right:\(n_1\)]  (n1) {};
		\node[elabel,below of=n1,color=yellow,label=right:\(n_2\)]  (n2) {};
		\node[elabel,below of=n2,color=blue,label=right:\(n_3\)]  (n3) {};
	\end{tikzpicture}
	\centering
	\caption{The hypergraph of the $A_{5\times4}$ matrix.}
	\label{fig:hypergraph}
	\end{figure}
	To find the distribution model we still have to partition $\mathcal{H}$ over the available processors. Since we are working with a column-net model coarsening means combining similar rows. For this example we assume that there are 2 processors available so we execute a 2-way multi-level partition.
	\begin{equation}
	\begin{multlined}
		A_{5\times4} =
		\begin{bmatrix}
		\cdot & 1 & \cdot & \cdot \\
		\cdot & \cdot & 1 & 1 \\
		\cdot & \cdot & \cdot & \cdot \\
		\cdot & 1 & \cdot & \cdot \\
		1 & \cdot & 1 & \cdot
		\end{bmatrix}
		\xrightarrow[(r_0,r_3),(r_1,r_4)]{coarsen}
		\begin{bmatrix}
		\cdot & 1 & \cdot & \cdot \\
		1 & \cdot & 1 & 1 \\
		\cdot & \cdot & \cdot & \cdot \\
		\end{bmatrix}
		\xrightarrow[(r_0,r_2)]{coarsen}
		\begin{bmatrix}
		\cdot & 1 & \cdot & \cdot \\
		1 & \cdot & 1 & 1 \\
		\end{bmatrix}
		\\
		\xrightarrow[]{partition}
		\begin{bmatrix}
		\cdot & 1 & \cdot & \cdot \\
		\mathbf{1} & \cdot & \mathbf{1} & \mathbf{1} \\
		\end{bmatrix}
		\xrightarrow[(r0,r2)]{uncoarsen}
		\begin{bmatrix}
		\cdot & 1 & \cdot & \cdot \\
		\mathbf{1} & \cdot & \mathbf{1} & \mathbf{1} \\
		\cdot & \cdot & \cdot & \cdot \\
		\end{bmatrix}
		\xrightarrow[(r_0,r_3),(r_1,r_4)]{uncoarsen}
		\begin{bmatrix}
		\cdot & 1 & \cdot & \cdot \\
		\cdot & \cdot & \mathbf{1} & \mathbf{1} \\
		\cdot & \cdot & \cdot & \cdot \\
		\cdot & 1 & \cdot & \cdot \\
		\mathbf{1} & \cdot & \mathbf{1} & \cdot
		\end{bmatrix}
	\end{multlined}
	\end{equation}
	We found the following 2-way row partition: $\mathcal{P}_2 = \{\mathcal{V}_0 = \{0,2,3\}, \mathcal{V}_1 = \{1,4\}\}$. No refinements were needed.
	\item
		The connectivity of a hypergraph is given by the following formula.
		\begin{equation}
			\lambda_j = | \{\mathcal{V}_i \in \mathcal{P}_k |~ n_j \cap \mathcal{V}_i \neq \emptyset \} | ~\text{with}~n_j \in \mathcal{N}
		\end{equation}
		The hypergraph $\mathcal{H}=(\mathcal{V},\mathcal{N})$ is partitioned according to $\mathcal{P}_k = \{\mathcal{V}_0, \ldots, \mathcal{V}_{k-1}\}$, models the communication cost for the fan-out communication in the column-net model exactly. If we apply this to our example we find
		\begin{equation}
		\begin{array}{llll}
			\lambda_0  & = | \{ \mathcal{V}_i \in \mathcal{P}_k |~ n_0 = \{4\} \cap \mathcal{V}_i \neq \emptyset \} | & = | \{ \mathcal{V}_1 \} | & = 1 \\
			\lambda_1  & = | \{ \mathcal{V}_i \in \mathcal{P}_k |~ n_1 = \{0,3\} \cap \mathcal{V}_i \neq \emptyset \} | & = | \{ \mathcal{V}_0 \} | & = 1 \\
			\lambda_2  & = | \{ \mathcal{V}_i \in \mathcal{P}_k |~ n_2 = \{1,4\} \cap \mathcal{V}_i \neq \emptyset \} | & = | \{ \mathcal{V}_1 \} | & = 1 \\
			\lambda_3  & = | \{ \mathcal{V}_i \in \mathcal{P}_k |~ n_3 = \{2\} \cap \mathcal{V}_i \neq \emptyset \} | & = | \{ \mathcal{V}_0 \} | & = 1 \\
		\end{array}
		\end{equation}
		The connectivity however does not model the communication volume. To do so, the $(\lambda-1)$-metric is introduced, which is used in the cost formula below.
		\begin{equation}
			C = \sum_{n_j \in \mathcal{N}} (\lambda_j - 1) = 0
		\end{equation}
		This models the communication model exactly which models the amount of fan-out communication in the column-net model.
\end{enumerate}

\end{solution}

\subsection{Odd-Even Transposition}
\label{q:oddeven}
\begin{question}
Array $x$ has $n$ elements with $n$ a multiple of $p$. In odd-even transposition sort, the fundamental operation is compare-exchange (when each process has 1 element, $n = p$) or compare-split (each process has $n/p$ elements). Give for both cases:
\begin{enumerate}
	\item A BSP algorithm.
	\item Parallel execution time.
	\item Parallel efficiency.
	\item For $n/p$ elements: if $n$ increases (with a fixed $p$), what will happen to the parallel efficiency?
\end{enumerate}

\end{question}
\begin{solution} The solution to the subquestions are given below.
\begin{enumerate}
	\item A BSP implementation for the odd-even $n/p$ block transposition sort can be found in \autoref{lst:oddevenblock}. For the special case in which each processor should have only 1 element we assume that their are at least as many processors as there are data elements.
		\lstinputlisting[caption="BSP odd-even n/p block transposition sort.",label=lst:oddevenblock]{\codeSrc/oddevenblockbsp.c}
	\item We calculate the cost for the different supersteps.
	\begin{itemize}
		\item \textbf{Superstep (0)}: This superstep initializes the local array which contains $n/p$ elements.
		\begin{equation}
			T_0 = \frac{n}{p} + \frac{n}{p} \cdot \log(\frac{n}{p}) + l
		\end{equation}
		\item \textbf{Superstep (1)}: Each processors exchanges its local array with that of his neighbor.
		\begin{equation}
			T_1 = 2 g + l
		\end{equation}
		\item \textbf{Superstep (2)}: This superstep performs the compate-split operation. Here, 2 $n/p$ items are compared against eachother.
		\begin{equation}
			T_2 = \frac{n}{p} + l
		\end{equation}
	\end{itemize}
	Therefore, the total cost is given by the sums of the supersteps.
	\begin{equation}
		\begin{split}
			T_p(n) & = T_0 + n \cdot (T_1 + T_2) \\
				& = \frac{n}{p} + \frac{n}{p} \cdot \log(\frac{n}{p}) +l + n \cdot (2 g + l + \frac{n}{p} + l) \\
				& = \frac{n}{p} + \frac{n}{p} \cdot \log(\frac{n}{p}) +l + 2gn + \frac{n}{p}n + 2ln \\
				& = n \cdot \left( \frac{1}{p} + \frac{1}{p} \log(\frac{n}{p}) + 2g \right) + \left( 1 + 2n \right) l
		\end{split}
	\end{equation}
	\item The parallel efficiency is defined as
	\begin{equation}
		E_p (n) = \frac{T_\text{seq}(n)}{p T_p(n)} = \frac{S_p(n)}{p}
	\end{equation}
	The sequential time $T_\text{seq}$ is given by the sequential odd-even transposition algorithm, which is $n^2$. The parallel time is given by the answer to the previous question.
	\begin{equation}
		E_p(n) = \frac{n^2}{p \cdot T_p} = \frac{n^2}{p \cdot \left( n \cdot \left( \frac{1}{p} + \frac{1}{p} \log(\frac{n}{p}) + 2g \right) + \left( 1 + 2n \right) l \right) }
	\end{equation}
	\item Looking at the complexity of $E_p(n)$ we can say about the efficiency for $n \to \infty$:
		\begin{equation}
			E_p(n) = \frac{\mathcal{O}(n^2)}{\mathcal{O}(n \log(n) + n)}
		\end{equation}
	Because the nominator grows faster than the denominator we can see that the parallel efficiency increases with the size of the problem.

\end{enumerate}
\end{solution}

\subsection{Shared Memory}
\label{q:sm1}
\begin{question}
We have p threads running the same SPMD program on a shared memory computer. Will the following algorithm work? Describe any possible problems. Supply a working parallel algorithm.
\begin{lstlisting}[caption={SPMD program on a shared memory computer},label=lst:spmd]
double a;
double x[1000];
double y[1000];

void spmd(){
	for (i=0; i<1000; i+=p)
		a = a + x[i]y[i];
}
\end{lstlisting}

\end{question}
\begin{solution}
There are two common pitfalls with non-BSP shared memory implementations: \textbf{data races}, \textbf{false sharing} and \textbf{inefficient cache use}.
The given problem is an illustration of the first.
A data race occurs when multiple processors access the same memory location concurrently and at least one of them is writing.
This leads to inconsistencies and the value most likely to be incorrect.
\\\\
We briefly discuss the other two pitfalls while giving the correct implementation.
One solution is storing the partial sums separately.
Using an array of $p$ elements however would lead to \textbf{false sharing}.
On every iteration, the array would be marked dirty in the cache, even though the value change at index $i$ (at $p(i)$) is logically independent of the change of value at index $j$ (at $p(j)$).
The last pitfall occurs when we choose to extend the array so that each cache line is only accessed by a single processor.
This works, but leads to inefficient cache use because all processors would be accessing all cache lines.
The correct solution is given below which corresponds to the pseudo code in \cite{slides7}.

\begin{lstlisting}[caption={SPMD program on a shared memory computer},label=lst:solspmd]
double a[8p];
double x[1000];
double y[1000];
int n = 1000;
void spmd(){
	for (i=s*ceil(n/p); i<(s+1)*ceil(n/p)); i++)
		a[8s] += x[i]y[i];
}
\end{lstlisting}
\end{solution}
\end{document}


% ----------------------------------------------------------------------
\section{Poroelasticity with Infinitesimal Strain and No Faults}

We base this formulation for poroelsticity on Zheng et al. and
Detournay and Cheng (1993). We assume a slightly compressible fluid
that completely saturates a porous solid, undergoing infinitesimal
strain.

We begin with the conservation of linear momentum, including inertia,
borrowed from linear elasticity:
\begin{equation}
    \rho_s\frac{\partial^2 \vec{u}}{\partial t^2} = \vec{f}(t) + \nabla \cdot \tensor{\sigma}(\vec{u},p).
\end{equation}
Enforcing mass balance of the fluid gives
\begin{gather}
  \frac{\partial \zeta(\vec{u},p)}{\partial t} + \nabla \cdot \vec{q}(p) =
  \gamma(\vec{x},t) \text{ in } \Omega, \\
%
  \vec{q} \cdot \vec{n} = q_0(\vec{x},t) \text{ on }\Gamma_q, \\
%
  p = p_0(\vec{x},t) \text{ on }\Gamma_p,
\end{gather}
where $\zeta$ is the variation in fluid content, $\vec{q}$ is the rate
of fluid volume crossing a unit area of the porous solid, $\gamma$ is
the rate of injected fluid per unit volume of the porous solid, $q_0$
is the outward fluid velocity normal to the boundary $\Gamma_q$, and
$p_0$ is the fluid pressure on boundary $\Gamma_p$.

We require the fluid flow to follow Darcy's law (Navier-Stokes
equation neglecting inertial effects),
\begin{equation}
  \vec{q}(p) = -\frac{\tensor{k}}{\mu_{f}}(\nabla p - \vec{f}_f),
\end{equation}
where $\tensor{k}$ is the intrinsic permeability, $\mu_f$ is the viscosity of the
fluid, $p$ is the fluid pressure, and $\vec{f}_f$ is the body force
in the fluid. If gravity is included in a problem, then usually
$\vec{f}_f = \rho_f \vec{g}$, where $\rho_f$ is the density of the
fluid and $\vec{g}$ is the gravitational acceleration vector.

\subsection{Constitutive Behavior}

We assume linear elasticity for the solid phase, so the constitutive behavior can be expressed
as
\begin{equation}
  \tensor{\sigma}(\vec{u},p) = \tensor{C} : \tensor{\epsilon} - \alpha p \tensor{I},
\end{equation}
where $\tensor{\sigma}$ is the stress tensor, $\tensor{C}$ is the
tensor of elasticity constants, $\alpha$ is the Biot coefficient
(effective stress coefficient), $\tensor{\epsilon}$ is the strain
tensor, and $\tensor{I}$ is the identity tensor.  For this case, we
will assume that the material properties are isotropic, resulting in
the following formulation for the stress tensor:
\begin{equation}
    \tensor{\sigma}(\vec{u},p) = \tensor{C}:\tensor{\epsilon} - \alpha p \tensor{I}
                                           = \lambda \tensor{I} \epsilon_{v} + 2 \mu - \alpha \tensor{I} p
\end{equation}
where $\lambda$ and $\mu$ are Lam\'e's parameters,
$\lambda = K_{d} - \frac{2 \mu}{3}$, $\mu$ is the shear modulus, and
the volumetric strain is defined as
$\epsilon_{v} = \nabla \cdot \vec{u}$.

For the constitutive behavior of the fluid, we use the volumetric
strain to couple the fluid-solid behavior,
\begin{gather}
  \zeta(\vec{u},p) = \alpha \Tr({\tensor{\epsilon}}) + \frac{p}{M}, \\
%
  \frac{1}{M} = \frac{\alpha-\phi}{K_s} + \frac{\phi}{K_f},
\end{gather}
where $1/M$ is the specific storage coefficient at constant strain,
$K_s$ is the bulk modulus of the solid, and $K_f$ is the bulk modulus
of the fluid. We can write the trace of the strain tensor as the dot
product of the gradient and displacement field, so we have
\begin{equation}
  \zeta(\vec{u},p) = \alpha (\nabla \cdot \vec{u}) + \frac{p}{M}.
\end{equation}

\begin{table}[htbp]
  \caption{Mathematical notation for poroelasticity with
    infinitesimal strain.}
  \label{tab:notation:poroelasticity}
  \begin{tabular}{lcp{3.5in}}
    \toprule
    {\bf Category} & {\bf Symbol} & {\bf Description} \\
    \midrule
    Unknowns           & $\vec{u}$ & Displacement field \\
                       & $\vec{v}$ & Velocity field \\
                       & $p$       & Pressure field (corresponds to pore fluid pressure) \\
                       & $\epsilon_{v}$ & Volumetric (trace) strain \\
    \hline
    Derived quantities & $\tensor{\sigma}$ & Cauchy stress tensor \\
                       & $\tensor{\epsilon}$ & Cauchy strain tensor \\
                       & $\zeta$ & Variation of fluid content (variation of fluid vol. per unit vol. of PM), $\alpha \epsilon_{v} + \frac{p}{M}$ \\
                       & $\rho_{b}$ & Bulk density, $\left(1 - \phi\right) \rho_{s} + \phi \rho_{f}$ \\
                       & $\vec{q}$ & Darcy flux, $-\frac{\tensor{k}}{\mu_{f}} \cdot \left(\nabla p - \vec{f}_{f}\right)$ \\
                       & $M$ & Biot Modulus, $\frac{K_{f}}{\phi} + \frac{K_{s}}{\alpha - \phi}$ \\
    \hline
    Common constitutive parameters & $\rho_{f}$ & Fluid density \\
                       & $\rho_{s}$ & Solid (matrix) density \\
                       & $\phi$ & Porosity \\
                       & $\tensor{k}$ & Permeability \\
                       & $\mu_{f}$ & Fluid viscosity \\
                       & $K_{s}$ & Solid grain bulk modulus \\
                       & $K_{f}$ & Fluid bulk modulus \\
                       & $K_{d}$ & Drained bulk modulus \\
                       & $\alpha$ & Biot coefficient, $1 - \frac{K_{d}}{K_{s}}$ \\
    \hline
    Source terms       & $\vec{f}$ & Body force per unit volume, for example: $\rho_{b} \vec{g}$ \\
                       & $\vec{f}_{f}$ & Fluid body force, for example: $\rho_{f} \vec{g}$ \\
                       & $\gamma$ & Source density; rate of injected fluid per unit volume of the porous solid \\
    \bottomrule
  \end{tabular}
\end{table}

\subsection{Quasistatic}

For ease of solution in the quasistatic case, we introduce a third
variable in the form of volumetric strain ($\epsilon_v$).  The
strong form of the problem may be expressed as
\begin{gather}
% Solution
\vec{s}^{T} = \left(\vec{u} \quad p \quad \epsilon_v\right), \\
% Elasticity
\vec{f}(t) + \nabla \cdot \tensor{\sigma}(\vec{u},p) = \vec{0} \text{ in } \Omega, \\
% Pressure
\frac{\partial \zeta(\vec{u},p)}{\partial t} - \gamma(\vec{x},t) + \nabla \cdot \vec{q}(p) = 0 \text{ in } \Omega, \\
% Vol. Strain
\nabla \cdot \vec{u} - \epsilon_{v} = 0 \text{ in } \Omega, \\
% Neumann traction
\tensor{\sigma} \cdot \vec{n} = \vec{\tau}(\vec{x},t) \text{ on } \Gamma_{\tau}, \\
% Dirichlet displacement
\vec{u} = \vec{u}_0(\vec{x}, t) \text{ on } \Gamma_{u}, \\
% Neumann flow
\vec{q} \cdot \vec{n} = q_0(\vec{x}, t) \text{ on } \Gamma_{q}, \text{ and } \\
% Dirichlet pressure
p = p_0(\vec{x},t) \text{ on } \Gamma_{p}.
\end{gather}
We place all terms for the elasticity, pressure, an volumetric strain
equations on the left-hand-side, consistent with PETSc TS implicit
time stepping.

%
We create the weak form by taking the dot product with the trial
functions $\trialvec[u]$, $\trialscalar[p]$, and
$\trialscalar[\epsilon_{v}]$ and
integrating over the domain:
\begin{gather}
% Weak conservation of momentum
\int_\Omega \trialvec[u] \cdot \left( \vec{f}(\vec{x},t) + \tensor{\nabla} \cdot \tensor{\sigma} (\vec{u},p) \right) \, d\Omega = 0, \\
% Weak conservation of mass
\int_\Omega  \trialscalar[p] \left( \frac{\partial \zeta(\vec{u},p)}{\partial t} - \gamma(\vec{x},t) + \nabla \cdot \vec{q}(p)\right) \, d\Omega = 0,\\
% Weak vol. strain
\int_{\Omega} \trialscalar[\epsilon_{v}]\cdot \left( \nabla \cdot \vec{u} - \epsilon_v \right) \, d\Omega.
\end{gather}
%
Applying the divergence theorem to the first two equations and
incorporating the Neumann boundary conditions yields
\begin{gather}
% Weak conservation of momentum
\int_\Omega \trialvec[u] \cdot \vec{f}(\vec{x},t) + \nabla \trialvec[u] : -\tensor{\sigma}(\vec{u},p_f) \,
d\Omega + \int_{\Gamma_\tau} \trialvec[u] \cdot \vec{\tau}(\vec{x},t) \, d\Gamma = 0, \\
% Weak conservation of mass
\int_\Omega  \trialscalar[p] \left( \frac{\partial \zeta(\vec{u},p_f)}{\partial t} - \gamma(\vec{x},t)\right)
+ \nabla \trialscalar[p] \cdot \left(-\vec{q}(p_f)\right) \, d\Omega + \int_{\Gamma_q} \trialscalar[p] q_0(\vec{x},t))\, d\Gamma = 0, \text{ and } \\
% Weak vol. strain
\int_{\Omega} \trialscalar[\epsilon_{v}] \cdot \left(\nabla \cdot \vec{u} - \epsilon_{v} \right) d\Omega = 0
\end{gather}

\subsubsection{Residual Pointwise Functions}

Identifying $F(t,s,\dot{s})$ and $G(t,s)$we have
\begin{align}
    % Displacement
  F^u(t,s,\dot{s}) &= \int_\Omega \trialvec[u] \cdot \eqnannotate{\vec{f}(\vec{x},t)}{\vec{f}^u_0}
                     + \nabla \trialvec[u] : \eqnannotate{-\tensor{\sigma}(\vec{u},p_f)}{\tensor{f}^u_1} \, d\Omega
                     + \int_{\Gamma_\tau} \trialvec[u] \cdot \eqnannotate{\vec{\tau}(\vec{x},t)}{\vec{f}^u_0} \, d\Gamma, \\
% Pressure
  F^p(t,s,\dot{s}) &= \int_\Omega  \trialscalar[p] \left[\eqnannotate{\frac{\partial \zeta(\vec{u},p_f)}{\partial t} - \gamma(\vec{x},t)} {f^p_0}\right]
                     + \nabla \trialscalar[p] \cdot \eqnannotate{-\vec{q}(p_f)}{\vec{f}^p_1} \, d\Omega
                     + \int_{\Gamma_q} \trialscalar[p] (\eqnannotate{q_0(\vec{x},t)}{f^p_0}) \, d\Gamma, \\
 % Volumetric Strain
  F^{\epsilon_{v}}(t,s,\dot{s}) &= \int_{\Omega} \trialscalar[\epsilon_{v}] \cdot \eqnannotate{\left(\nabla \cdot \vec{u} - \epsilon_{v} \right)}{f^{\epsilon_{v}}_{0}} \, d\Omega. \\
  G^u(t,s) &= 0, \\
             G^p(t,s) &= 0, \\
 G^{\epsilon_v} &= 0.
\end{align}

\subsubsection{Jacobian Pointwise Functions}

Three field results in a potential nine Jacobian pointwise functions for the LHS:

\begin{align}
%
% JF_UU
% Jf3uu
J_F^{uu} &= \frac{\partial F^u}{\partial u} + t_{shift} \frac{\partial F^u}{\partial \dot{u}} = \int_{\Omega} \nabla \trialvec[u] : \frac{\partial}{\partial u} (- \sigma(\vec{u},p,\epsilon_{v})) \
d\Omega = \int_{\Omega} \nabla \trialvec[u] : \frac{\partial}{\partial u} (-(\tensor{C}:\tensor{\varepsilon} -\alpha p \tensor{I})) \ d\Omega \\
&= \int_{\Omega} \nabla \trialvec[u] : -\tensor{C}: \frac{1}{2} (\nabla + \nabla^T) \basisvec[u] \ d\Omega = \int_{\Omega} \trialscalar[u]_{i,k}
\eqnannotate{\left(-C_{ikjl}\right)}{J_{f3}^{uu}} \basisscalar[u]_{j,l} \ d\Omega \\
%
% JF_UP
% Jf2up
J_F^{up} &= \frac{\partial F^u}{\partial p} + t_{shift} \frac{\partial F^u}{\partial \dot{p}} = \int_{\Omega} \nabla \trialvec[u] : \frac{\partial}{\partial p}(-(\tensor{C}:\tensor{\varepsilon} -\alpha p \tensor{I})) \ d\Omega =
\int_{\Omega} \trialscalar[u]_{i,j} \eqnannotate{\left(\alpha \delta_{ij}\right)}{J_{f2}^{up}} \basisscalar[p] \ d\Omega \\
%
% JF_UE
% Jf2ue
J_F^{u \epsilon_{v}} &= \frac{\partial F^u}{\partial \epsilon_{v}} + t_{shift} \frac{\partial F^u}{\partial \dot{\epsilon_{v}}} = \int_{\Omega} \nabla \trialvec[u] : \frac{\partial}{\partial \epsilon_{v}}
(-\sigma(\vec{u},p,\epsilon_{v})) \ d\Omega = \int_{\Omega} \nabla \trialvec[u] :
\frac{\partial}{\partial \epsilon_{v}} (-(\tensor{C}:\tensor{\varepsilon} -\alpha p \tensor{I})) \ d\Omega \\
&= \int_{\Omega} \nabla \trialvec[u] : \frac{\partial}{\partial \epsilon_{v}} \
\left[-\left(2 \mu \tensor{\epsilon} + \lambda \tensor{I} \epsilon_{v} - \alpha \tensor{I} p \right) \right] d\Omega =
\int_{\Omega} \trialscalar[u]_{i,j} \eqnannotate{\left(-\lambda \delta_{ij} \right)}{J_{f2}^{u \epsilon_{v}}} \basisscalar[\epsilon_{v}] d\Omega  \\
%
% JF_PU
%
J_F^{pu} &= \frac{\partial F^p}{\partial u} + t_{shift} \frac{\partial F^p}{\partial \dot{u}} = 0 \\
%
% JF_PP
% Jf0pp
J_F^{pp} &= \frac{\partial F^p}{\partial p} + t_{shift} \frac{\partial F^p}{\partial \dot{p}} =
\int_{\Omega} \nabla \trialscalar[p] \cdot \frac{\partial}{\partial p} -\left[-\frac{\tensor{k}}{\mu_{f}} \left(\nabla p - \vec{f} \right) \right] \ d\Omega  +
t_{shift}\int_{\Omega} \trialscalar[p] \frac{\partial}{\partial \dot{p}} \left[\alpha\dot{\epsilon}_{v} + \frac{\dot{p}}{M} - \gamma\left(\vec{x},t\right)\right] \ d\Omega \\
&= \int_{\Omega} \nabla \psi_{trial}^ p \left(\frac{\tensor{k}}{\mu_{f}} \nabla \cdot \psi_{basis}^p \right) \ d\Omega +
 \int_{\Omega} \trialscalar[p] \left(t_{shift} \frac{1}{M}\right) \basisscalar[p] \ d\Omega \\
&= \int_{\Omega} \psi_{trial,k}^p \eqnannotate{\left(\frac{\tensor{k}}{\mu_{f}} \delta_{kl}\right)}{J_{f3}^{pp}} \psi_{basis,l}^p \ d\Omega +
\int_{\Omega} \trialscalar[p] \eqnannotate{\left(t_{shift} \frac{1}{M}\right)}{J_{f0}^{pp}} \basisscalar[p] \ d\Omega \\
%
% JF_PE
% Jf0pe
J_F^{p\epsilon_{v}} &= \frac{\partial F^p}{\partial \epsilon_{v}} + t_{shift} \frac{\partial
F^p}{\partial \dot{\epsilon_{v}}} = \int_{\Omega} \trialscalar[p] \eqnannotate{\left(t_{shift} \alpha \right)}{J_{f0}^{p\epsilon_{v}}}
\basisscalar[\epsilon_{v}] \ d\Omega \\
%
% JF_EU
% Jf1eu
J_F^{\epsilon_{v}u} &= \frac{\partial F^{\epsilon_{v}}}{\partial u} + t_{shift} \frac{\partial F^{\epsilon_{v}}}{\partial \dot{u}} =
\int_{\Omega} \psi_{trial}^{\epsilon_{v}} \nabla \cdot \vec{\psi}_{basis}^u \ d\Omega = \int_{\Omega}
\basisscalar[\epsilon_{v}] \eqnannotate{\left(\delta_{ij}\right)}{J_{f1}^{\epsilon_{v}u}}
\basisscalar[u]_{i,j} \ d\Omega\\
%
% JF_EP
%
J_F^{\epsilon_{v}p} &= \frac{\partial F^{\epsilon_{v}}}{\partial p} + t_{shift} \frac{\partial F^{\epsilon_{v}}}{\partial \dot{p}} = 0 \\
%
% JF_EE
%
J_F^{\epsilon_{v}\epsilon_{v}} &= \frac{\partial F^\epsilon_{v}}{\epsilon_{v}} + t_{shift} \frac{\partial F^{\epsilon_{v}}}{\partial \dot{\epsilon_{v}}} =
\int_{\Omega} \basisscalar[\epsilon_{v}] \eqnannotate{\left(-1\right)}{J_{f0}^{\epsilon_{v}\epsilon_{v}}} \basisscalar[\epsilon_{v}] \ d\Omega
\end{align}

\subsection{Dynamic}

For compatibility with PETSc TS algorithms, we want to turn the second
order elasticity equation into two first order equations. We introduce
velocity as a unknown, $\vec{v}=\frac{\partial u}{\partial t}$, which
leads to a slightly different three field problem,
\begin{gather}
% Solution
\vec{s}^{T} = \left(\vec{u} \quad p \quad \vec{v}\right) \\
% Displacement
\frac{\partial \vec{u}}{\partial t} = \vec{v} \text{ in } \Omega \\
% Pressure
\frac{\partial \zeta(\vec{u},p)}{\partial t } - \gamma(\vec{x},t) + \nabla \cdot \vec{q}(p) = 0 \text{ in } \Omega \\
% Velocity
\rho_{b} \frac{\partial \vec{v}}{\partial t} = \vec{f}(\vec{x},t) + \nabla \cdot \tensor{\sigma}(\vec{u},p) \text{ in } \Omega \\
% Neumann traction
\tensor{\sigma} \cdot \vec{n} = \vec{\tau}(\vec{x},t) \text{ on } \Gamma_{\tau} \\
% Dirichlet displacement
\vec{u} = \vec{u}_{0}(\vec{x}, t) \text{ on } \Gamma_{u} \\
% Neumann flow
\vec{q} \cdot \vec{n} = q_{0}(\vec{x}, t) \text{ on } \Gamma_{q} \\
% Dirichlet pressure
p = p_{0}(\vec{x},t) \text{ on } \Gamma_{p}
\end{gather}

For compatibility with PETSc TS explicit time stepping algorithms, we
need the left hand side to be $F = (t,s,\dot{s}) = \dot{s}$. We
replace the variation of fluid content variable, $\zeta$, with its
definition in the conservation of fluid mass equation and solve for
the rate of change of pressure,
\begin{gather}
    \frac{\partial}{\partial t}\left[\alpha \epsilon_{v} + \frac{p}{M}\right] - \gamma\left(\vec{x},t\right) + \nabla \cdot \vec{q} = 0 \\
    \alpha \dot{\epsilon}_{v} + \frac{\dot{p}}{M} - \gamma \left(\vec{x},t\right) + \nabla \cdot \vec{q} = 0 \\
    \frac{\dot{p}}{M} = \gamma \left(\vec{x},t \right) - \alpha \dot{\epsilon}_{v} -\nabla \cdot \vec{q} \\
    \frac{\dot{p}}{M} = \gamma \left(\vec{x},t \right) - \alpha \left( \nabla \cdot \dot{\vec{u}} \right) -\nabla \cdot \vec{q}.
\end{gather}
We write the volumetric strain in terms of displacement, because this
dynamic formulation does not include the volumetric strain as an
unknown.

Using trial functions $\trialvec[u]$, $\trialscalar[p]$, and $\trialvec[v]$, and incorporating the
Neumann boundary conditions, the weak form may be written as:
\begin{align}
    % Displacement
    \int_{\Omega} \trialvec[u] \cdot \left( \frac{\partial \vec{u}}{\partial t} \right)d \Omega &= \int_{\Omega} \trialvec[u] \cdot \left( \vec{v} \right) d \Omega \\
    % Pressure
    \int_{\Omega} \trialscalar[p] \left( \frac{1}{M}\frac{\partial p}{\partial t} \right) d\Omega &=
    \int_{\Omega} \trialscalar[p] \left[\gamma(\vec{x},t) - \alpha \left(\nabla \cdot \dot{\vec{u}}\right) \right]  + \nabla \trialscalar[p] \cdot \vec{q}(p) \ d\Omega +
    \int_{\Gamma_q} \trialscalar[p] (-q_0(\vec{x},t)) \ d\Gamma, \\
   % Velocity
   \int_\Omega \trialvec[v] \cdot \left( \rho_{b} \frac{\partial
   \vec{v}}{\partial t} \right) \,
   d\Omega &= \int_\Omega \trialvec[v] \cdot \vec{f}(\vec{x},t) + \nabla \trialvec[v] :
   -\tensor{\sigma} (\vec{u},p_f) \, d\Omega + \int_{\Gamma_\tau} \trialvec[u]
   \cdot \vec{\tau}(\vec{x},t) \, d\Gamma.
\end{align}

\subsubsection{Residual Pointwise Functions}

With explicit time stepping the PETSc TS assumes the LHS is $\dot{s}$ , so we only need the RHS residual functions:

\begin{align}
% Displacement
  G^u(t,s) &= \int_{\Omega} \trialvec[u] \cdot \eqnannotate{\vec{v}}{\vec{g}_0^u} d \Omega, \\
% Pressure
  G^p(t,s) &= \int_\Omega \trialscalar[p] \eqnannotate{\left(\gamma(\vec{x},t) - \alpha (\nabla \cdot \dot{\vec{u}})\right)}{g^p_0} + \nabla \trialscalar[p] \cdot \eqnannotate{\vec{q}(p_f)}{\vec{g}^p_1} \, d\Omega
 + \int_{\Gamma_q} \trialscalar[p] (\eqnannotate{-q_0(\vec{x},t)}{g^p_0}) \, d\Gamma, \\
 % Velocity
 G^v(t,s) &= \int_\Omega \trialvec[v] \cdot \eqnannotate{\vec{f}(\vec{x},t)}{\vec{g}^v_0} + \nabla \trialvec[v] :\eqnannotate{-\tensor{\sigma}(\vec{u},p_f)}{\tensor{g}^v_1} \, d\Omega + \int_{\Gamma_\tau} \trialvec[u] \cdot \eqnannotate{\vec{\tau}(\vec{x},t)}{\vec{g}^v_0} \, d\Gamma.
\end{align}

\subsubsection{Jacobians Pointwise Functions}

These are the pointwise functions associated with $M_{u}$, $M_{p}$,
and $M_{v}$ for computing the lumped LHS Jacobian. We premultiply the
RHS residual function by the inverse of the lumped LHS Jacobian while
$s_\mathit{tshift}$ remains on the LHS with $\dot{s}$. As a result,
we use LHS Jacobian pointwise functions, but set $s_\mathit{tshift} = 1$. The
LHS Jacobians are:
\begin{align}
% Displacement
M_{u} &= J_F^{uu} = \frac{\partial F^u}{\partial u} + s_{tshift} \frac{\partial F^u}{\partial \dot{u}} =
\int_{\Omega} \trialscalar[u]_{i} \eqnannotate{s_{tshift} \delta_{ij}}{J^{uu}_{f0}} \basisscalar[u]_{j} \, d \Omega \\
% Pressure
M_{p} &= J_F^{pp} = \frac{\partial F^p}{\partial p} + s_{tshift} \frac{\partial F^p}{\partial \dot{p}} =
\int_{\Omega} \trialscalar[p] \eqnannotate{\left(s_{tshift} \frac{1}{M}\right)}{J_{f0}^{pp}} \basisscalar[p] \ d\Omega \\
% Velocity
M_{v} &= J_F^{vv} = \frac{\partial F^v}{\partial v} + s_{tshift} \frac{\partial F^v}{\partial \dot{v}} =
\int_{\Omega} \trialscalar[v]_{i}\eqnannotate{\rho_{b}(\vec{x}) s_{tshift} \delta_{ij}}{J^{vv}_{f0}} \basisscalar[v]_{j} \  d \Omega
\end{align}


\section{Transformations and Compositions}\label{sec:Transformations}

\input{2-functions/2-2-1-transformations}
\input{2-functions/2-2-2-compositions}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Opensolutionfile{solutions}[ex]
\section*{Exercises for \ref{sec:Transformations}}

\begin{enumialphparenastyle}

%%%%%%%%%%
\begin{ex}
Starting with the graph of $\ds y=\sqrt{x}$, the graph of $\ds y=1/x$, and the
graph of $\ds y=\sqrt{1-x^2}$ (the upper unit semicircle), sketch the
graph of each of the following functions:
\begin{multicols}{2}
\begin{enumerate}
	\item	$\ds f(x)=\sqrt{x-2}$
	\item	$\ds f(x)=-1-1/(x+2)$
	\item	$\ds f(x)=4+\sqrt{x+2}$
	\item	$\ds y=f(x)=x/(1-x)$
	\item	$\ds y=f(x)=-\sqrt{-x}$
	\item	$\ds f(x)=2+\sqrt{1-(x-1)^2}$
	\item	$\ds f(x)=-4+\sqrt{-(x-2)}$
	\item	$\ds f(x)=2\sqrt{1-(x/3)^2}$
	\item	$\ds f(x)=1/(x+1)$
	\item	$\ds f(x)=4+2\sqrt{1-(x-5)^2/9}$
	\item	$\ds f(x)=1+1/(x-1)$
	\item	$\ds f(x)=\sqrt{100-25(x-1)^2}+2$
\end{enumerate}
\end{multicols}
\end{ex}

%%%%%%%%%%
\begin{ex}
The graph of $f(x)$ is shown below.
Sketch the graphs of the following functions.
\vadjust{\rightline{%
\vbox to 0pt{\vskip-5pt\beginpicture
\normalgraphs
\setcoordinatesystem units <1truecm,1truecm>
\setplotarea x from 0 to 3.25, y from -1.5 to 2
\axis bottom shiftedto y=0 ticks numbered from 1 to 3 by 1 /
\axis left ticks numbered from -1 to 2 by 1 /
\setquadratic
\plot
0.000 -1.285 0.054 -0.643 0.108 -0.088 0.162 0.387 0.217 0.788
0.271 1.120 0.325 1.391 0.379 1.604 0.433 1.766 0.488 1.882
0.542 1.956 0.596 1.993 0.650 1.998 0.704 1.976 0.758 1.929
0.812 1.862 0.867 1.779 0.921 1.684 0.975 1.578 1.029 1.467
1.083 1.351 1.138 1.235 1.192 1.120 1.246 1.008 1.300 0.903
1.354 0.805 1.408 0.716 1.462 0.637 1.517 0.571 1.571 0.517
1.625 0.476 1.679 0.450 1.733 0.438 1.788 0.441 1.842 0.458
1.896 0.490 1.950 0.536 2.004 0.595 2.058 0.666 2.112 0.749
2.167 0.841 2.221 0.943 2.275 1.051 2.329 1.164 2.383 1.279
2.438 1.396 2.492 1.510 2.546 1.620 2.600 1.722 2.654 1.813
2.708 1.890 2.762 1.949 2.817 1.987 2.871 2.000 2.925 1.983
2.979 1.932 3.033 1.842 3.088 1.710 3.142 1.528 3.196 1.294
3.250 1.000 /
\endpicture\vskip0pt\vss}\hskip8cm}}
\vspace{1.5in}

\begin{multicols}{2}
\begin{enumerate}
	\item	$\ds y=f(x-1)$
	\item	$\ds y=1+f(x+2)$
	\item	$\ds y=1+2f(x)$
	\item	$\ds y=2f(3x)$
	\item	$\ds y=2f(3(x-2))+1$
	\item	$\ds y=(1/2)f(3x-3)$
	\item	$\ds y=f(1+x/3)+2$
	\item	$\ds y=|f(x)-2|$
\end{enumerate}
\end{multicols}
\end{ex}

%%%%%%%%%%
\begin{ex}
Suppose $f(x) = 3x-9$ and $\ds g(x) = \sqrt{x}$.  What is the
domain of the composition $(g\circ f)(x)$?
\begin{sol}
$\ds \{x\mid x\ge3\}$, $\{x\mid x\ge0\}$
\end{sol}
\end{ex}

\end{enumialphparenastyle}


%---------------------------Jacobian-----------------------------
\section{Jacobian}

The minimum Jacobian computed at each vertex is used:
\[
q = \min_{i\in\{0,1,2,3\}} \left\{ \alpha_i \right\}
\]

\quadmetrictable{Jacobian}%
{$L^2$}%                                    Dimension
{$[0.DBL\_MAX]$}%                           Acceptable range
{$[0,DBL\_MAX]$}%                           Normal range
{$[-DBL\_MAX,DBL\_MAX]$}%                   Full range
{$1$}%                                      Unit square
{\cite{knu:00}}%                            Citation
{v\_quad\_jacobian}%                        Verdict function name


\documentclass[12pt]{article}
\usepackage{amsmath}
\begin{document}
    \begin{itemize}
        \item input: words list (of a word sense)
        \item output: partition of the list (one or two)
    \end{itemize}
    \paragraph{MCMC sampling}~

    The optimal partition is: ($G_i$ can be empty)
    $$\{G\} = \text{argmax}_{G_1,G_2}\sum_{i=1,2}\sum_{a,b\in G_i, a<b}\text{cosSim}(a,b)-\sum_{a\in G_1, b\in G_2}\text{cosSim}(a,b)$$
    \begin{enumerate}
        \item Start with random partition.
        \item Do 3-4 until convergence.
        \item Create a new partition $\{G\}'$ by moving a random selected word $l$ from $G_i$ to $G_j$.
        \item Accept this with probability $\min(1, p)$, where
        $$\log p=\gamma \left(\sum_{m\in G_j,m\neq l} \text{cosSim}(l,m)-\sum_{n\in G_i,n\neq l}\text{cosSim}(l,n)\right)$$
    \end{enumerate}
\end{document}


%!TEX root = ../../dissertation.tex

\section{Comparison using a neoclassical growth model}

The neoclassical growth model is often used as a benchmark to measure the
effectiveness of a solution method. We keep with this tradition and analyze the
stochastic neoclassical growth model with seven alternative solution methods,
which are outlined in \cite{AMMT2016}. Each method is implemented in Julia,
Matlab, and Python which allows us to see how the performance of different
solution methods may vary by programming language.

\subsection{The model}

We consider a dynamic programming problem of finding the value function,
$V$, that solves the Bellman equation,

\begin{align}
  V \left(k, z\right) &= \max_{c, k^{\prime }} u\left(c\right) +\beta E\left[ V\left( k^{\prime },z^{\prime }\right) \right] \label{vf} \\
  \text{s.t.\quad}k^{\prime } &= \left( 1-\delta \right) k + z f\left( k\right) - c \label{bc} \\
  \ln z^{\prime} &= \rho \ln z + \varepsilon^{\prime },\qquad \varepsilon^{\prime } \sim \mathcal{N}\left( 0,\sigma ^{2}\right), \label{ts}
\end{align}

where $k$, $c$ and $z$ are capital, consumption and productivity level,
respectively; $\beta \in \left( 0,1\right) $; $\delta \in \left( 0,1\right] $ ;
$\rho \in \left( -1,1\right) $; $\sigma \geq 0$; the utility and production
functions, $u$ and $f$, respectively, are strictly increasing, continuously
differentiable and strictly concave. The primes on variables denote next-period
values, and $E\left[ V\left( k^{\prime },z^{\prime }\right) \right] $ is an
expectation conditional on state $\left( k,z\right) $.

\paragraph{Optimality conditions.}

The first order condition (FOC) and envelope condition (EC) of the problem
(\ref{vf})-(\ref{ts}), respectively, are

\begin{equation}
  u^{\prime}\left(c\right) = \beta E \left[ V_{1}\left(k^{\prime }, z^{\prime}\right) \right] \label{f2}
\end{equation}

\begin{equation}
  V_{1}\left(k, z\right) = u^{\prime }\left( c\right) \left[ 1-\delta + zf^{\prime }\left( k\right) \right]. \label{f3}
\end{equation}%

By combining (\ref{f2}) and (\ref{f3}), we obtain the Euler equation%

\begin{equation}
  u^{\prime }\left(c\right) = \beta E\left[ u^{\prime }\left( c^{\prime}\right) \left[ 1-\delta +z^{\prime }f^{\prime }\left( k^{\prime} \right) \right] \right]. \label{f23}
\end{equation}

\paragraph{Parameterization and implementation details.}

We parameterize the model (\ref{vf})-(\ref{ts}) by using a Constant Relative
Risk Aversion (CRRA) utility function, $u\left( c\right) =\frac{c^{1-\gamma
}-1}{1-\gamma }$, and a Cobb-Douglas production function, $f\left( k\right)
=Ak^{\alpha }$. We choose parameters to be set at $A=\frac{1/\beta -(1-\delta
)}{\alpha }$, $\alpha =1/3$, $\beta =0.99$, $\delta =0.025$, $ \rho =0.95$ and
$\sigma =0.01$. As a solution domain, we use a rectangular, uniformly spaced
grid of $10\times 10$ points for capital and productivity between 0.9 and 1.1
for both variables.

We integrate by using a $10$-node Gauss-Hermite quadrature rule and approximate
the policy and value functions by using complete ordinary polynomials up to
degree 5. As an initial guess, we use a linear approximation to the capital
policy function. To solve for polynomial coefficients, we use fixed-point
iteration.

All computations are performed using Julia v0.6.0, Matlab version 9.2.0
(R2017a), and Python 3.6 on a MacBook Pro with a 2.7 GHz Intel Core i7
processor and 16 GB of RAM. For Julia and Python, the particular package
versions can be found in the corresponding notebooks submitted on QuantEcon's Notebook site\footnote{The
\href{http://notes.quantecon.org/submission/5b5f71779cd7f00015be6350}{Matlab notebook},
\href{http://notes.quantecon.org/submission/5b5f70db9cd7f00015be634e}{Python notebook},
\href{http://notes.quantecon.org/submission/5b5f711d9cd7f00015be634f}{Julia notebook}}.

\subsection{Value iterative methods}

We analyze three value iterative algorithms for solving the model: (1)
conventional value function iteration method analyzed in e.g., \cite{AF2014} ;
(2) a variant of envelope condition method (ECM) of \cite{MM2013} that finds a
solution to Bellman equation by iterating on value function; and (3) endogenous
grid method (EGM) of \cite{Carroll2006}. We present a brief description of each
algorithm below. A detailed description of each algorithm is included in
Appendix A.

\subsubsection{Conventional Value Function Iteration}

Conventional VFI constructs the policy rule for consumption by combining FOC
(\ref{f2}) and budget constraint (\ref{bc}):

\qquad \newline
{\small
\begin{tabular}{l}
\hline \hline
\textbf{Algorithm 1. Conventional VFI} \\ \hline
Given $V$, for each point $\left( k,z\right) $, define the following recursion: \\
\quad i). Numerically solve for $c$ satisfying $u^{\prime }\left(c\right) =\beta E \left[ V_{1}\left( \left( 1-\delta \right) k+zf\left( k\right) -c,z^{\prime }\right) \right] $. \\
\quad ii). Find $k^{\prime }=\left( 1-\delta \right) k+zf\left( k\right) -c$. \\
\quad iii). Find $\widehat{V}\left( k,z\right) =u\left( c\right) +\beta E \left[ V\left( k^{\prime },z^{\prime }\right) \right] $. \\
Iterate on i)-iii) until convergence $\widehat{V}=V$. \\ \hline \hline
\end{tabular}
}

\qquad \newline

\subsubsection{Envelope Condition Method}

ECM, proposed in \cite{MM2013}, finds consumption from envelope
condition (\ref{f3}) and budget constraint (\ref{bc}). In this specific model,
the consumption function can be constructed analytically:

\qquad \newline

{\small
\begin{tabular}{l}
\hline \hline
\textbf{Algorithm 2. ECM-VF} \\ \hline
Given $V$, for each point $\left( k,z\right) $, define the following recursion: \\
\quad i). Find $c=u^{\prime -1}\left[ \frac{V_{1}\left( k,z\right) }{1-\delta +zf^{\prime }\left( k\right) }\right]$. \\
\quad ii). Find $k^{\prime }=\left( 1-\delta \right) k+zf\left( k\right) -c$. \\
\quad iii). Find $\widehat{V}\left( k,z\right) =u\left( c\right) +\beta E \left[ V\left( k^{\prime },z^{\prime }\right) \right]$. \\
Iterate on i)-iii) until convergence $\widehat{V}=V$. \\ \hline \hline
\end{tabular}
}

\qquad \newline

\subsubsection{Endogenous Grid Method}

Finally, EGM of \cite{Carroll2006} constructs a grid on $\left( k^{\prime
},z\right) $ by fixing the future endogenous state variable $k^{\prime }$ and
by treating the current endogenous state variable $k$ as unknown; see also
\cite{BF2007} for a discussion of the EGM method. Since $k^{\prime }$ is fixed,
EGM computes $E\left[ V_{1}\left( k^{\prime },z^{\prime }\right) \right] $
up-front and thus can avoid costly interpolation and approximation of
expectation in a root finding procedure.

\qquad \newline

{\small
\begin{tabular}{l}
\hline \hline
\textbf{Algorithm 3. EGM of Carroll (2005)} \\ \hline
Given $V$, for each point $\left( k^{\prime },z\right) $, define the
following recursion: \\
\quad i). Find $c=u^{\prime -1}\left \{ \beta E\left[ V_{1}\left( k^{\prime}, z^{\prime }\right) \right] \right \}$. \\
\quad ii). Solve for $k$ satisfying $k^{\prime }=\left( 1-\delta \right)k + zf\left(k\right) - c$. \\
\quad iii). Find $\widehat{V}\left( k,z\right) =u\left( c\right) +\beta E \left[ V\left( k^{\prime },z^{\prime }\right) \right]$. \\
Iterate on i)-iii) until convergence $\widehat{V}=V$. \\ \hline \hline
\end{tabular}%
}

\qquad \newline

In Step ii) of EGM, we still need to find $k$ numerically. However, for the
studied model, \cite{Carroll2006} shows a change of variables that makes it
possible to avoid finding $k$ numerically on each iteration (except of the
very last iteration). In order to streamline our code, we do not use this
change of variables so the running time of our version of the EGM method
will be considerably larger than for the version of the method in \cite
{Carroll2006}. However, this fact does not distort our comparison analysis
since we use the same implementation in all three languages.

\subsubsection{Comparison results of Matlab, Python and Julia for value iterative methods}

The results from our comparison of the three iterative methods are in Table
\ref{clmm:Table1}. Conventional VFI is the most time consuming in all three
languages because it requires us to find the root of (\ref{f2}) for each $(k,
z)$. Given $(k, z)$, each evaluation of equation (\ref{f2}) requires the computer
to compute conditional expectation by interpolating over the $t+1$ values.
Numerical root finders must do this repeatedly. The reason EGM performs better
than VFI is that it is easier to solve equation (\ref{f2}) with respect to $c$
given $\left(k^{\prime}, z\right)$ than to solve it with respect to $c$ given
$(k, z)$ because we only need to evaluate conditional expectation once if
we know $k^{\prime}$. The ECM method requires no numerical solver which is why
it is so efficient relative to the other two methods.\footnote{A version of the
envelope condition argument is used in \cite{AHLLM2017} to construct a new
class of fast and efficient numerical methods for solving dynamic economic
models in continuous time.}

Julia produces solutions between 5 and 50 times faster than Matlab and Python
for VFI and EGM. The reasons Julia performs so much better on these two
algorithms is because the entire language is built on JIT compilation. This
means that the repeated calls to the Julia objective
function are cheap (relative to an interpreted language like Matlab or Python)
because these subsequent function calls execute already compiled machine code.

Our results for the ECM show similar performance for all three languages. The
main reason for this is that the method does not require a non-linear solver to
compute the policy for consumption and computations can be vectorized. The
vectorization puts the languages on more similar footing because they all end
up calling out to the same high performant BLAS routines implemented in C or
Fortran.

\subsection{Policy iterating methods}

We analyze four algorithms that construct policy functions for the standard
neoclassical stochastic growth model: Algorithm 4 is a conventional policy iteration (PI)
method, e.g., \cite{SR2004}; Algorithm 5 is a variant of ECM that implements policy
iteration; Algorithm 6 is a version of ECM that solve for derivative of value function
instead of value function itself; Algorithm 7 an Euler equation algorithm. Again we
give a brief description of each algorithm but a more detailed description of
these algorithms is provided in Appendix A.

\subsubsection{Conventional Policy Iteration}

Conventional policy iteration constructs a solution to the Bellman equation by
iterating on the consumption function using FOC (\ref{f2}).

\qquad \newline

{\small
\begin{tabular}{l}
\hline \hline
\textbf{Algorithm 4. Conventional PI} \\ \hline
Given $C$, for each point $\left( k,z\right) $, define the following
recursion: \\
\quad i). Find $K\left(k, z\right) = \left( 1 - \delta \right)k + z f\left(k\right) - C\left( k,z\right)$ \\
\quad ii). Solve for $V$ satisfying $V\left(k, z\right) = u\left(C\left(k, z\right) \right) + \beta E\left[V\left(K\left(k, z\right), z^{\prime} \right) \right]$ \\
\quad iii). Find $\widehat{C}$ satisfying $u^{\prime }\left( \widehat{C}\left( k,z\right) \right) =\beta E\left[ V_{1}\left( \left( 1-\delta \right)k + z f\left(k\right) - \widehat{C}\left(k, z\right), z^{\prime}\right) \right]
$ \\
Iterate on i)-iii) until convergence $\widehat{C}=C$. \\ \hline \hline
\end{tabular}%
}

\qquad

\subsubsection{ECM Policy Iteration}

ECM-PI the variant of ECM that performs PI instead of VFI. It constructs a
solution to the Bellman equation by iterating on the consumption function using
EC (\ref{f3}).

\qquad \newline

{\small
\begin{tabular}{l}
\hline \hline
\textbf{Algorithm 5. ECM-PI} \\ \hline
Given $C$, for each point $\left( k,z\right) $, define the following
recursion: \\
\quad i). Find $K\left(k, z\right) = \left( 1 - \delta \right) k+zf\left(k\right) - C\left(k, z\right)$ \\
\quad ii). Solve for $V$ satisfying $V\left(k, z\right) = u\left( C\left(k, z\right) \right) + \beta E\left[ V\left(K\left(k, z\right), z^{\prime}\right) \right]$ \\
\quad iii). Find $\widehat{C}\left(k, z\right) = u^{\prime -1}\left[\frac{V_{1}\left(k, z\right)}{1 - \delta + z f^{\prime }\left(k\right)}\right]$ \\
Iterate on i)-iii) until convergence $\widehat{C}=C$. \\ \hline \hline
\end{tabular}%
}

\qquad

\subsubsection{Derivative Policy Iteration}

The ECM analysis also suggests a useful recursion for the derivative of value
function. We first construct consumption function $C\left(k, z\right)$
satisfying FOC (\ref{f2}) under the current value function

\begin{align}
\beta E\left[V_{1}\left( k^{\prime },z^{\prime }\right) \right] = u^{\prime }\left(C\left(k, z\right) \right), \label{vk4}
\end{align}

and we then use (\ref{f3}) to obtain the derivative of value function for next
iteration. This leads to a solution method that we call ECM-DVF.

The main difference between ECM-DVF from the previously studied ECM-VF consists
in that we iterate on $V_{1}$ without computing $V$ on each iteration. We only
compute $V$ at the very end, when both $V_{1}$ and the optimal policy functions
are constructed. Again, neither numerical maximization nor a numerical solver
is necessary under ECM-DVF but only direct calculations. Similarly to PI
methods, Euler equation methods do not solve for value function but only for
decision (policy) functions. One possible decision function is a derivative of
value function. Thus, the ECM-DVF recursion (\ref{vk4}) can be also viewed as
an Euler equation written in terms of the derivative of value function.

\qquad \newline

{\small
\begin{tabular}{l}
\hline \hline
\textbf{Algorithm 6. ECM-DVF} \\ \hline
Given $V_{1}$ for each point $\left( k,z\right) $, define the following recursion: \\
\quad i). Find $c = u^{\prime -1}\left[ \frac{V_{1}\left(k, z\right)}{1 - \delta +z f^{ prime}\left(k\right) }\right]$ \\
\quad ii). Find $k^{\prime }=\left( 1-\delta \right) k+zf\left( k\right) -c$ \\
\quad iii). Find $\widehat{V}_{1}\left(k, z\right) =\beta \left[ 1-\delta +z f^{\prime }\left(k\right) \right] E \left[V_{1}\left(k^{\prime}, z^{\prime }\right) \right]$ \\
Iterate on i)-iii) until convergence $\widehat{V}_{1}=V_{1}$ \\
Given the converged policy functions, find $V$ satisfying $V\left(k, z\right) = u\left(c\right) + \beta E \left[V\left(k^{\prime }, z^{\prime}\right) \right]$ \\ \hline \hline
\end{tabular}
}

\subsubsection{Euler Equation Methods}

Policy iteration has similarity to Euler equation methods; see \cite
{Judd1998}, \cite{Santos1999} for a general discussion of such methods. Euler
equation methods approximate policy functions for consumption $ c=C\left(
k,z\right) $, capital $k^{\prime }=K\left( k,z\right) $ (or other policy
functions) to satisfy (\ref{bc}), (\ref{ts}) and (\ref{f23}). Below, we provide
an example of Euler equation method (many other recursions for such methods are
possible).

\qquad \newline

{\small
\begin{tabular}{l}
\hline \hline
\textbf{Algorithm 7. Euler equation algorithms} \\ \hline
Given $C\left( k,z\right) $, for each point $\left( k,z\right) $, define the
following recursion: \\
\quad i). Find $K\left(k, z\right) =\left(1 - \delta\right) k + z f\left(k\right) -C\left( k,z\right)$ \\
\quad ii). Find $c^{\prime} = C\left(K\left(k, z\right), z^{\prime}\right)$ \\
\quad iii). Find $\widehat{C}\left(k, z\right) = u^{\prime -1}\left \{\beta E \left[ u^{\prime}\left(c^{\prime}\right) \left(1 - \delta + z^{\prime} f^{\prime}\left(k^{\prime}\right) \right) \right] \right \}$ \\
Iterate on i)-iii) until convergence $\widehat{C}=C$. \\ \hline \hline
\end{tabular}
}

\subsubsection{Comparison results of Matlab, Python and Julia for policy iterative methods}

In Table \ref{clmm:Table2}, we see that conventional PI performs the worst because it relies on a
numerical solver, but, as with the value iterative methods, this reliance is less problematic for
Julia than for Matlab or Python.

Overall, we found that the speed of most algorithms was comparable across all three languages. The
exception was that Julia performed significantly better than the other programs whenever there was
any numerical optimization or root-finding involved. However, these speedups came at the cost of
spending a (relatively small) amount of extra time being careful about how the code was written to
ensure that the Julia compiler is able to infer the type of each variable in the most time-intensive
parts of the program.


\section{Propositional Logic}

Propositional logic is about arguments.
An argument contains two things:

\begin{itemize}
	\item
	      One or more premises.
	\item
	      One conclusion.
\end{itemize}

Both of these things must be propositions.
This means they must evaluate to either true ({\bf 1}) or false ({\bf 0}).
Take the following example:

\begin{table}[h]
	\centering
	\begin{tabular}{l}
		All humans are mortal \\
		Socrates is human     \\
		\hline
		$\therefore$ Socrates is mortal
	\end{tabular}
	\caption{An example of an argument in English}
\end{table}

In this example a premise ``All humans are mortal'' is given.
This is something which can be either true or false.
Given that both premises are true, we must accept the conclusion to also be true.
In this case that means Socrates is a mortal.
Propositional logic is not a one-size-fit-all solution.
It does not have {\bf quantifiers}, which means you cannot specify an amount of something.
Propositional statements are generally not in english.
The are replaces with {\bf propositional variables} like \(p\), \(q\) and \(r\).

\subsection{Valid $\neq$ Sound}
There is a difference in {\bf sound} and {\bf valid} arguments.
A {\bf valid} argument is an argument which is `syntactically' valid.
This means that the argument holds if all permises are true, but it does not mean the permises hold up.
When the arguments hold up in the real world, then the argument is also {\bf sound}.
The given example is both valid as well as sound.
If you change both occurances of `mortal' to `immortal'
then the example is still valid, but no longer sound as the argument no longer holds up in the real world.

\subsection{Logical connectives}
{\bf Logical connectives} or {\bf logical operators} is the `glue' between propositions.
A list of possible logical connectives are shown in table \ref{tab:logicalConnectives}.
Logical connectives can be used to compare or combine multiple propositional variables.
Take for example the following formula: \(q \land p\).
This takes two propositional variables \(p\) and \(q\) and compares that.
This returns another boolean, namely true when both \(p\) and \(q\) are true, or false in every other case.
Using this knowledge you can create something called a {\bf compound proposition}.
This means it combines multiple proposition to a single, larger one.
An example of a compound proposition is \((q \land p) \lor \neg r\).
Both \(q \land p\) and \(\neg r\) are propositions, and combining gives a new one.
If you execlude the parenthesis then the formula is parsed from left to right.
So \(q \land p \land r\) is equal to \((q \land p) \land r\) but not to \(q \land (p \land r)\).

\begin{table}[h]
	\centering
	\begin{tabular}{c | l | l}
		Symbol     & Name         & Effect                                                \\
		\hline
		$\land$    & conjunction  & True when both propositions are true                  \\
		$\lor$     & disjunction  & True when one or both propositions are true           \\
		$\oplus$   & exclusive or & True when one of the propositions is true             \\
		$\neg$     & negation     & True when the proposition is false                    \\
		$\implies$ & implication  & True if both sides are true or the left side is false \\
		$\iff$     & iff          & True if both sides are true                           \\
	\end{tabular}
	\caption{A list of logical connectives}
	\label{tab:logicalConnectives}
\end{table}

\subsection{Logical equivalence}
If you want to check if two propositions are equivalent
(e.g. have the same value all the time), then you can use a {\bf truth table}.
A truth table is an table with all possible outcomes of a proposition.
If two propositions have the outcome in the truth table then the two propositions are equivalent.
take this truth table for example:

\begin{table}[h]
	\centering
	\begin{tabular}{c | c | c | c}
		\(q\) & \(p\) & \(q \land p\) & \(\neg(\neg q \lor \neg p)\) \\
		\hline
		0     & 0     & 0             & 0                            \\
		0     & 1     & 0             & 0                            \\
		1     & 0     & 0             & 0                            \\
		1     & 1     & 1             & 1                            \\
	\end{tabular}
\end{table}

As you can see, the two statements have the same values in the truth table, and are logically equivalent.

\subsection{Sufficient or Necessary}
Given is the following implication \(p \implies q\).
\(p\) is in this statement the {\bf hypothesis}, and \(q\) is the {\bf conclusion}.
If the hypothesis is true then it's sufficient to say the conclusion is also true.
If the conclusion is true then it's necessary to say the hypothesis is also true.
It's impossible for the conclusion to be false if the hypothesis is true.
But when the hypothesis is false then the conclusion can be both true and false.
So if the hypothesis is false then you can say that the statement holds.

\subsection{Contrapositive, Converse and Inverse}
Every implication has a contrapositive, converse and a inverse.
They can, but don't have to be logically equivalent.
Given the implication `If this is Tuesday, then we are in Belgium' or in logic form \(p \implies q\)
we can get the contrapositive, converse and inverse of it.
The {\bf contrapositive} is \(\neg q \implies \neg p\), so `If we aren't in Belgium, then this isn't Tuesday'.
The {\bf converse} is \(q \implies p\), so `If we are in Belgium, then this is Tuesday'.
The {\bf inverse } is \(\neg p \implies \neg q\), so `If this isn't Tuesday, then we aren't in Belgium'.

\subsection{Tautology, Contradiction and Contingency}
There are more categories where propositions can fit in. These three are one of those.
A tautology is a proposition which always evaluate to true. An example is the proposition \(q \lor \neg q\).
A contradiction is the opposite, it'll always evaluate to false. An example is \(q \land \neg q\).
A contingency is every thing else, which can be true or false. For example \(q\).

\begin{table}[h]
	\centering
	\begin{tabular}{c | c | c | c}
		\(q\) & \(q \lor \neg q\) & \(q \land \neg q\) & \(q\) \\
		\hline
		0     & 1                 & 0                  & 0     \\
		0     & 1                 & 0                  & 0     \\
		1     & 1                 & 0                  & 1     \\
		1     & 1                 & 0                  & 1     \\
	\end{tabular}
\end{table}


\subsection*{ Orientations of Vector Spaces }

\exercisehead{13.1}
\[
\begin{gathered}
  (E_1 \dots E_n) \sim (E_1 \dots E_n) \\
  E_i = \delta_i^{\, \, j } E_j \\
  \text{det}{ (\delta_i^{\, \, j} )} = 1
\end{gathered}
\]

If $E_i = B_i^{\, \, j} \widetilde{E}_j$, \, $\text{det}{B_i^{\, \, j }} >0$,
% \[
%(B^{-1})_i^{\, \, k} E_k = (B^{-1})_i^{\, \, k } B_k^{\, \, j} \widetilde{E}_j = E_
% #\]

$\text{det}{(BB^{-1})} = \text{det}{B} \text{det}{B^{-1}} = 1$.  $\text{det}{B^{-1}} >0$ since $\text{det}{B} >0$.

If $E_i = B_i^{\, \, j} \widetilde{E}_j$
\[
\begin{aligned}
  & \widetilde{ \widetilde{E}}_i = A_i^{\, \, j} E_j \\
  & \widetilde{ \widetilde{E}}_i = A_i^{\, \, k } B_k^{\, \, j} \widetilde{E}_j \quad \quad \text{det}{AB} = \text{det}{A} \text{det}{B} >0
\end{aligned}
\]
So it's an equivalence relation and since $\text{det}{B} \neq 0$, $\text{det}{B} >0$ or $\text{det}{B} <0$ and as above there are only 2 equivalence classes; $\text{det}{A} \gtrless 0$

\subsection*{ Orientations of Manifolds }

lcaol frame $(E_i)$ for $M$ is (positively) oriented if $(\left. E_1 \right|_p \dots \left. E_n \right|_p)$ positively oriented basis for $T_pM$.  $\forall \, p \in U$

cont. if $\forall \, p \in M$, $p\in $ domain of oriented local frame.

orientation of $M$ is cont., pointwise orientation.

$M$ orinetable if $\exists \, $ orientation to $M$

\exercisehead{13.2}  connected domain $U$.  Consider some $\Omega \in \Lambda^m(V)$,  consider
\[
\begin{aligned}
  & U \to \mathbb{R} \\
  & p \mapsto ( \left. E_1 \right|_p \dots \left. E_n \right|_p ) \mapsto \Omega( \left. E_1 \right|_p \dots \left. E_n \right|_p ) = \text{det}{ ( \left.  \epsilon^i(E_j) \right|_p ) }
\end{aligned}
\]

$\text{det}$ cont. function s.t. $\text{det} = \begin{cases} +1 \\ -1 \end{cases}$ on $U$.  $U$ connected so $\forall \, $ cont. functions from $X$ to $\lbrace 0, 1 \rbrace$ or, the same, $\lbrace 1 , -1 \rbrace$, constant.  \\
So $\Omega( \left. E_1 \right|_p \dots \left. E_n \right|_p ) = \text{det}{ ( \left. \epsilon^i (E_j) \right|_p )}$ constant on $U$, otherwise $U$ separated.

\begin{proposition}[13.4]
$\text{dim}{M} = m \geq 1$

$\forall \, m$-form $\Omega$ on $M$, $\Omega\neq 0$ determines a unique orientation of $M$ s.t. $\Omega$ positively oriented $\forall \, p \in M$, \\
Conversely, if $M$ given orientation, then $\exists \, $ smooth $m$-form $\Omega$ on $M$ that's positively oriented $\forall \, p \in M$
\end{proposition}

$F:M \to N$ local diffeomorphism.

$\forall \, p \in M$, $\exists \, (U, \varphi)$ chart and consider $U_F \subset U$ s.t. $F(U_F)$ open, $\left. F\right|_{U_F} : U_F \to F(U_F)$ diffeomorphism.

For $F(p) \in N$, $\exists \, (V, \psi)$ chart and consider $F(U_F) \cap V$

$\psi F\varphi^{-1}(x^1 \dots x^m) = F^j(x^1 \dots x^m)$ \quad \quad $\text{det}{(\partial_i F^j)} >0$ suppose.

\subsection*{The Orientation Covering}

\subsection*{Orientations of Hypersurfaces }

interior multiplication or contraction with $X$
\[
i_X\omega(Y_1 \dots Y_{k-1}) = \omega(X,Y_1 \dots Y_{k-1})
\]
$i_X \omega$ obtained by inserting $X$ into the first slot.

Notation $X \righthalfcup \omega = i_X \omega$

Suppose $M$ smooth manifold \\
\phantom{Suppose} $S\subset M$ submanifolds (immersed or embedded)

vector field along $S$ is cont. $N: S \to TM$ s.t. $N_p \in T_pM$ \quad $\forall \, p \in S$

(Note difference between vector field along $S$ and vector field on $S$, s.t. $N_p \in T_pS \, \forall \, p$)

$N_p \in T_pM$, $p\in S$ transverse to $S$ if $T_pM$ spanned by $N_p, T_pS$

Similarly, vector field $N$ along $S$ transverse to $S$ if $N_p$ transverse to $S$, \, $\forall \, p \in S$

\begin{proposition}[15.21, 13.12 in previous version] Suppose $M$ oriented smooth $m$-manifold \\
\phantom{Proposition 13.12 Suppose  } $S$ immersed hypersurface in $M$.  \\
\phantom{Proposition 13.12 Suppose  } $N$ transverse vector field along $S$.

Then $S$ has unique orientation s.t. $\forall \, p \in S$, $(E_1 \dots E_{n-1})$ oriented basis for $T_pS$ iff $(N_p, E_1 \dots E_{n-1})$ oriented basis for $T_pM$

If $\Omega$ orientation form for $M$, \\
then $\left. (N \righthalfcup \Omega) \right|_S \equiv \left. i_N\Omega \right|_S $ orientation form for $S$ with respect to this orientation.
\end{proposition}

Recall that smooth hypersurface $S$ is $S\subseteq M$ equipped with $i: S \hookrightarrow M$, smooth immersion, i.e. $Di$ injective.

Now orientation form $\omega = dN_p \wedge dE_1 \wedge \dots \wedge dE_{n-1}$, then \\
$i_{N_p} \omega  =dE_1 \wedge \dots \wedge dE_{n-1}$
\[
i^*_S (i_{N_p}\omega) = i^*_S (dE_1 \wedge \dots \wedge dE_{n-1}) = i^*_S(dE_1) \wedge \dots \wedge i^*_S(dE_{n-1}) = d(i^*_SE_1) \wedge \dots \wedge d(i_S^*E_{n-1})
\]

\begin{proof} Let $\Omega$ \\
\phantom{proof Let } $\omega  = \left. (N \righthalfcup \Omega) \right|_S$ $m-1$ form.

$(E_1 \dots E_{n-1}) $ basis for $T_pS$ \\
$N$ transverse to $S$ implies $(N_p, E_1 \dots E_{n-1})$ basis for $T_pM$.

$\Omega$ orientation form so $\Omega$ nonvanishing.
\[
\omega_p(E_1 \dots E_{n-1}) = \Omega_p(N_p, E_1 \dots E_{n-1}) \neq 0
\]
since $\omega_p(E_1 \dots E_{n-1}) >0$ iff $\Omega_p(N_p, E_1 \dots E_n) >0$,  \\
orientation determined by $\omega$ is the 1 defined in the statement of the proposition.

\end{proof}


% Created 2021-09-28 Tue 10:05
% Intended LaTeX compiler: pdflatex
\documentclass[presentation,aspectratio=169]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{khpreamble}
\DeclareMathOperator{\atantwo}{atan2}
\def\ucolor{blue!80!black}
\def\ycolor{green!60!black}
\newcommand*{\incolor}[1]{\textcolor{\ucolor}{#1}}
\newcommand*{\outcolor}[1]{\textcolor{\ycolor}{#1}}
\usetheme{default}
\author{Kjartan Halvorsen}
\date{\today}
\title{Frequency response}
\hypersetup{
 pdfauthor={Kjartan Halvorsen},
 pdftitle={Frequency response},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 26.3 (Org mode 9.4.6)},
 pdflang={English}}
\begin{document}

\maketitle

\section{Bode diagram}
\label{sec:org30472a2}

\begin{frame}[label={sec:org2c8c6d5}]{Response of LTI systems to sinusoids}
\begin{center}
  \begin{tikzpicture}[scale = 0.8, node distance=20mm, block/.style={rectangle, draw, minimum width=15mm}, sumnode/.style={circle, draw, inner sep=2pt}]

  \node[coordinate] (refinput) {};
  \node[block, right of=refinput] (motor) {$G(s)$};
  \node[coordinate, right of=motor, node distance=20mm] (output) {};

  \draw[\ucolor, ->] (refinput) -- node[above, pos=0.3] (voltsignal) {$u$} (motor);
  \draw[\ycolor, ->] (motor) -- node[above, pos=0.5] (velsignal) {$y$} (output);
  \end{tikzpicture}
\end{center}

Let \(\incolor{u(t) = \sin\omega_1 t}\). Then, after transients have died out,
\[ \outcolor{y(t)}= \outcolor{|G(\omega_1)| \sin \big( \omega_1 t + \arg G(i\omega_1)\big)}. \]
\end{frame}

\begin{frame}[label={sec:orgd7e24dc}]{The Bode diagram}
\[ y(t) = \underbrace{|G(i\omega_1)|}_{\text{amplification}} \sin \big( \omega_1 t + \underbrace{\arg G(i\omega_1)}_{\text{phase shift}} \big) \]

The Bode diagram shows the \alert{magnitude} and \alert{phase} of the transfer function evaluated on the positive imaginary axis. It thus contains all information about the steady-state response of the system to input signals of different frequency.
\end{frame}

\begin{frame}[label={sec:org8f5caea}]{Specifications on the frequency properties of the closed-loop system}
\begin{center}
\includegraphics[width=0.899\linewidth]{../../figures/spec-bode-closed-loop-new}
\end{center}
\end{frame}

\begin{frame}[label={sec:org9073d5b}]{Exercise: Reading the Bode diagram}
\begin{center}
\includegraphics[width=\linewidth]{../../figures/alias-example-bode-GC}
\end{center}
which of the below responses \alert{is not} compatible with the Bode diagram?

\begin{center}
\includegraphics[width=\linewidth]{../../figures/example-bode-GC-timeseries}
\end{center}
\end{frame}

\begin{frame}[label={sec:orgcb17626}]{From loop gain to closed-loop gain}
 \begin{center}
 \begin{tikzpicture}
\tikzset{node distance=2cm,
    block/.style={rectangle, draw, minimum height=12mm, minimum width=14mm},
    sumnode/.style={circle, draw, inner sep=2pt}
}

  \node[coordinate] (input) {};
  \node[sumnode, right of=input, node distance=20mm] (sum) {\tiny $\sum$};
  \node[block,right of=sum, node distance=30mm] (fb) {$F(s)$};
  \node[block,right of=fb, node distance=30mm] (plant) {$G(s)$};
  \node[coordinate, right of=plant, node distance=30mm] (output) {};
  \node[coordinate, right of=plant, node distance=22mm] (measure) {};
  \draw[->] (input) -- node[above, pos=0.2] {$y_{ref}(t)$} (sum);
  \draw[->] (sum) -- node[above] {$e(t)$} (fb);
  \draw[->] (fb) -- node[above] {$u(t)$} (plant);
  \draw[->] (plant) -- node[at end, above] {$y(t)$} (output);
  \draw[->] (measure) -- ++(0, -18mm) -| (sum) node[left, pos=0.96] {$-$};
  \draw[red] (3.8, -1) rectangle (9.4, 1.7);
  \node[red] at (8, 1.4) {$G_o(s)$};
  \end{tikzpicture}
\end{center}

\[ G_c(i\omega) = \frac{G(i\omega)F(i\omega)}{1 + G(i\omega)F(i\omega)} = \frac{G_o(i\omega)}{1 + G_o(i\omega)} \]
\[ |G_c(i\omega)| = \frac{|G_o(i\omega)|}{|1 + G_o(i\omega)|} = \frac{|G_o(i\omega)|}{|G_o(i\omega) - (-1)|} \]

\pause

\alert{Keep the loop gain \(G_o(i\omega)\) away from -1!}
\end{frame}

\begin{frame}[label={sec:orgd4c2941}]{If the phase shift is \(\pi\)}
\(G_o(i\omega_1) = -1\), \(|G_o(i\omega_1)| = 1\), \(\arg G_o(i\omega_1) = -\pi\)

\begin{center}
  \begin{tikzpicture}[node distance=22mm, block/.style={rectangle, draw, minimum width=15mm}, sumnode/.style={circle, draw, inner sep=2pt}]

    \node[coordinate] (input) {};
    \node[circle, fill, inner sep=1pt, right of=input, node distance=24mm] (sum) {};
    \node[circle, fill, inner sep=1pt, below of=sum, node distance=5mm] (sum2) {};
    \node[coordinate, below of=sum, node distance=2.5mm] (summid) {};
    \node[circle, fill, inner sep=1pt, right of=summid, node distance=5mm] (sum3) {};
    \node[block, right of=sum3, node distance=20mm] (plant)  {$G_o(s)$};
    \node[coordinate, right of=plant, node distance=40mm] (output) {};

    \draw[->] (input) -- node[above, pos=0.1, color=blue!80!black] {$u(t)=\sin(\omega_1 t)$} (sum);
    \draw[->] (plant) -- node[coordinate, pos=0.5] (measure) {} node[above, pos=0.3, anchor=south west, color=orange!80!red] {$y(t)=\sin\big(\omega_1 t -\pi\big) = -\sin(\omega_1 t)$} (output);
    \draw[->] (sum3) -- node[above] {} (plant);
    \draw[->] (measure) -- ++(0,-16mm) -| node[pos=0.95, left] {$-$} (sum2);
    \draw (sum) to (sum3);
  \end{tikzpicture}
\end{center}
\pause
Closed-loop transfer function: \(G_c(s) = \frac{G_o(s)}{1 + G_o(s)}\)
\begin{tcolorbox}
We want \[ 1 + G_o(i\omega) \neq 0, \quad \forall \omega \]
If not, then the closed-loop system will have poles on the imaginary axis (in the s-domain).
\end{tcolorbox}
\end{frame}

\begin{frame}[label={sec:org72d5978}]{The simplified Nyquist criterion in the s-plane}
\begin{center}
\includegraphics[width=0.65\linewidth]{../../figures/implane-nyquist-contour-map}
\end{center}
\begin{tcolorbox}
If the open-loop system (the loop gain) is not unstable, i.e. $G_o(s)$ has no poles in the right-half plane, then the closed-loop system will be stable if the Nyquist curve \textbf{do not encircle the point \(s=-1\)}. The point $s=-1$ should stay on the left side of the Nyquist curve when we go along the curve from low to high frequencies.
\end{tcolorbox}
\end{frame}

\begin{frame}[label={sec:org6de397c}]{Stability margins}
\begin{center}
\includegraphics[width=0.38\linewidth]{../../figures/implane-nyquist-margins}
\end{center}
\begin{itemize}
\item Cross-over frequency: The frequency \(\omega_c\) for which \(|G_o(i\omega)| = 1\).
\item Phase margin: The angle \(\varphi_m\) to the negative real axis for the point where the Nyquist curve intersects the unit circle. \[\varphi_m = \arg G_o(i\omega_c) - (-180\degree) = \arg G_o(i\omega_c) + 180\degree\]
\end{itemize}
\end{frame}
\begin{frame}[label={sec:org3d52280}]{Stability margins}
\begin{center}
\includegraphics[width=0.38\linewidth]{../../figures/implane-nyquist-margins}
\end{center}
\begin{itemize}
\item phase-cross-over frequency: The frequency \(\omega_p\) for which \(\arg G_o(i\omega) = -180\degree\).
\item Gain margin: The gain \(K=A\) that would make the Nyquist curve of \(KG_o(i\omega h)\) go through the point \(-1 + i0\). This means that \[ |G_o(i\omega_p h| = \frac{1}{A}. \]
\end{itemize}
\end{frame}

\section{Freq-domain specs}
\label{sec:org1202c00}
\begin{frame}[label={sec:org9c619a7}]{How to achieve the frequency-domain specifications}
\[G_c(i\omega) = \frac{ G_o(i\omega)}{1 + G_o(i\omega)}\]

\alert{Activity}
\begin{enumerate}
\item If \(Go(i\omega_1) = -0.5\) what is \(|G_c(i\omega_1)|\)?
\item If \(Go(i\omega_1) = -i\) what is \(|G_c(i\omega_1)|\)?
\end{enumerate}
\end{frame}

\begin{frame}[label={sec:org7b0788d}]{How to achieve the frequency-domain specifications}
\begin{columns}
\begin{column}{0.28\columnwidth}
\[G_c(i\omega) = \frac{ G_o(i\omega)}{1 + G_o(i\omega)}\]

\includegraphics[width=1.1\linewidth]{../../figures/spec-bode-closed-loop-new}

Which of the Bode plots to the right shows the correct loop gain \(G_o(i\omega)\)?
\end{column}

\begin{column}{0.72\columnwidth}
\begin{center}
\includegraphics[width=1.02\linewidth]{../../figures/spec-bode-open-loop-new}
\end{center}
\end{column}
\end{columns}
\end{frame}

\begin{frame}[label={sec:org8d81bce}]{Stability margins excercise}
\begin{center}
  \includegraphics[width=.6\linewidth]{../../figures/bode-example-margin2.pdf}
\end{center}

\alert{Activity} Determine the cross-over frequency \(\omega_c\), the phase cross-over frequency \(\omega_p\), the phase margin and the amplitude margin.
\end{frame}
\end{document}