% !Mode:: "TeX:UTF-8"
% !TEX program = xelatex
\section{Question 1}
\begin{equation*}
    \begin{aligned}
        \EE\中括号{\log\小括号{\frac{S(n\delta t)}{S_0}}} &= \EE\中括号{n\log d+\log\小括号{\frac{u}{d}}\sum_{i=1}^nR_i} \\
        &= n\log d + \log\小括号{\frac{u}{d}}\sum_{i=1}^n\EE\中括号{R_i} \\
        &= n\log d + \log\小括号{\frac{u}{d}}\sum_{i=1}^np \\
        &= n\log d + \log\小括号{\frac{u}{d}}np
    \end{aligned}
\end{equation*}

\begin{equation*}
    \begin{aligned}
        \var\中括号{\log\小括号{\frac{S(n\delta t)}{S_0}}} &= \var\中括号{n\log d+\log\小括号{\frac{u}{d}}\sum_{i=1}^nR_i} \\
        &= \var\中括号{\log\小括号{\frac{u}{d}}\sum_{i=1}^nR_i} \\
        &= \小括号{\log\小括号{\frac{u}{d}}}^2\sum_{i=1}^n\var\小括号{R_i} \\
        &= \小括号{\log\小括号{\frac{u}{d}}}^2\sum_{i=1}^nnp(1-p)
    \end{aligned}
\end{equation*}

\section{Question 2}
\subsection{Review}
The three papers discussed the method of estimating option prices by simulations. Boyle's paper first introduced the how to use Monte Carlo method to solve option valuation problems, he suggested that we can first generate the returns of the stock and then calculate the option value based on the risk neutrality assumption. 19 years later, Broadie and Glasserman presented two direct method for estimating option prices by simulation, including pathwise method and likelihood ration method. The two methods raise the computational speed and return unbiased estimates. In 2001, F.A.Longstaff and E.S.Schwartz extended the method to American options. The key is to use least squares to estimate the conditional expected payoff, which satisfy the situation where FDM can not be used, such as valuation of American options.

\subsection{From Binomial to Trinomial Method}
It's possible if the process allow the stock price at a time point equal to the price at previous time point for a given probability.

Then the trinomial tree is like the form according to Figure~\ref{F:trinomial-tree}, and it has parameters $u$, $d$, $p_1$, $p_2$, $M$.
\begin{figure}
    \centering
    \includegraphics[width=.5\textwidth]{figures/2019-12-25-trinomial-tree.png}
    \caption{Trinomial Tree}\label{F:trinomial-tree}
\end{figure}

\subsection{Appendix}
\paragraph{Paper I:} Phelim P. Boyle, \emph{Options: A Monte Carlo approach}\footnote{Journal of Financial Economics, Volume 4, Issue 3, 1977.}.

Abstract: This paper develops a Monte Carlo simulation method for solving option valuation problems. The method simulates the process generating the returns on the underlying asset and invokes the risk neutrality assumption to derive the value of the option. Techniques for improving the efficiency of the method are introduced. Some numerical examples are given to illustrate the procedure and additional applications are suggested.

\paragraph{Paper II:} Mark Broadie, Paul Glasserman, \emph{Estimating Security Price Derivatives Using Simulation}\footnote{Management Science, Volume 42, No. 2 1996.}.

Abstract: Simulation has proved to be a valuable tool for estimating security prices for which simple closed form solutions do not exist. In this paper we present two direct methods, a pathwise method and a likelihood ratio method, for estimating derivatives of security prices using simulation. With the direct methods, the information from a single simulation can be used to estimate multiple derivatives along with a security's price. The main advantage of the direct methods over resimulation is increased computational speed. Another advantage is that the direct methods give unbiased estimates of derivatives, whereas the estimates obtained by resimulation are biased. Computational results are given for both direct methods, and comparisons are made to the standard method of resimulation to estimate derivatives. The methods are illustrated for a path independent model (European options), a path dependent model (Asian options), and a model with multiple state variables (options with stochastic volatility).

\paragraph{Paper III:} Francis A. Longstaff, Eduardo S. Schwartz, \emph{Valuing American Options by Simulation: A Simple LeastSquares Approach}\footnote{The Review of Financial Studies, Volume 14, Issue 1, 2001.}.

Abstract: This article presents a simple yet powerful new approach for approximating the value of American options by simulation. The key to this approach is the use of least squares to estimate the conditional expected payoff to the optionholder from continuation. This makes this approach readily applicable in pathdependent and multifactor situations where traditional finite difference techniques cannot be used. We illustrate this technique with several realistic examples including valuing an option when the underlying asset follows a jumpdiffusion process and valuing an American swaption in a 20factor string model of the term structure.


\section{Gridworld [15 pts]}

Consider the following grid environment. Starting from any unshaded square, you can move up, down, left, or right. Actions are deterministic and always succeed (e.g. going left from state 16 goes to state 15) unless they will cause the agent to run into a wall. The thicker edges indicate walls, and attempting to move in the direction of a wall results in staying in the same square (e.g. going in any direction other than left from state 16 stays in 16). Taking any action from the green target square (no. 12) earns a reward of $r_g$ (so $r(12, a)$ = $r_g$ $\forall a$) and ends the episode . Taking any action from the red square of death (no. 5) earns a reward of $r_r$ (so $r(5, a)$ = $r_r$ $\forall a$) and ends the episode. Otherwise, from every other square, taking any action is associated with a reward $r_s \in \{-1, 0, +1\}$ (even if the action results in the agent staying in the same square). Assume the discount factor $\gamma = 1$, $r_g = +5$, and $r_r = -5$ unless otherwise specified.

\begin{figure}[h]
  \centering
    \includegraphics[width=0.3\textwidth]{tex/grid.png}
  	\label{fig:grid}
\end{figure}

\begin{enumerate}[label=(\alph*)]
\item (3pts) Define the value of $r_s$ that would cause the optimal policy to return the shortest path to the green target square (no. 12). Using this $r_s$, find the optimal value for each square.

\textbf{Answer:}

\begin{equation}
r_s = -1
\end{equation}

\begin{equation}
\begin{bmatrix}
0 & 1 & 2 & 3 \\
-5& 2 & 3 & 4 \\
2 & 3 & 4 & 5 \\
1 & 0& -1& -2
\end{bmatrix}
\end{equation}

\item (3pts) Lets refer to the value function derived in (a) as $V^{\pi_g}_{old}$ and the policy as $\pi_g$. Suppose we are now in a new gridworld where all the rewards ($r_s$, $r_g$, and $r_r$) have $+2$ added to them. Consider still following $\pi_g$ of the original gridworld, what will the new values $V^{\pi_g}_{new}$ be in this second gridworld?

\textbf{Answer:}

\begin{equation}
\begin{bmatrix}
12 & 11 & 10 & 9 \\
-3 & 10 & 9  & 8 \\
10 & 9  & 8  & 7 \\
11 & 12 & 13 & 14
\end{bmatrix}
\end{equation}

\item (3pts) Consider a general MDP with rewards, and transitions. Consider a discount factor of $\gamma$. For this case assume that the horizon is infinite (so there is no termination). A policy $\pi$ in this MDP induces a value function $V^\pi$ (lets refer to this as $V^\pi_{old}$). Now suppose we have a new MDP where the only difference is that all rewards have a constant $c$ added to them. Can you come up with an expression for the new value function $V^\pi_{new}$ induced by $\pi$ in this second MDP in terms of $V^\pi_{old}$, $c$, and $\gamma$?

\textbf{Answer:}

\begin{equation}
\begin{split}
V^\pi_{old}(s) & = \Ex_{\pi}[\sum_{t=0}^{\infty} \gamma^{t}r_t|s_0=s] \\
V^\pi_{new}(s) & = \Ex_{\pi}[\sum_{t=0}^{\infty}\gamma^{t}(r_t + c)|s_0=s] \\
               & = \Ex_{\pi}[\sum_{t=0}^{\infty}\gamma^{t}r_t|s_0=s] + c\sum_{t=0}^{\infty}\gamma^{t} \\
               & = V^\pi_{old}(s) + \frac{c}{1-\gamma}
\end{split}
\end{equation}

\item (2pts) Lets go back to our gridworld from (a) with the default values for $r_g$, $r_r$, $\gamma$ and with the value you specified for $r_s$. Suppose we now derived a second gridworld by adding a constant $c$ to all rewards ($r_s$, $r_g$, and $r_r$) such that $r_s = +2$. How does the optimal policy change (Just give a one or two sentence description)? What do the values of the unshaded squares become?

\textbf{Answer:}

It will become looping at states other than the terminate states, because each step will gain positive reward value.

Unshaded squares will become value $+\infty$.

\item (2pts) Now take the second gridworld from part (d) and change $\gamma$ such that $0 < \gamma < 1$. Can the optimal policy change and does it depend on your choice of gamma? (A brief description is sufficient, no formal proof or mathematical analysis required).

\textbf{Answer:}

If $\gamma$ is close to $1$, the result is same, all unshaded squares will have value $+\infty$ and optimal policy is keep looping.

if $\gamma$ is equal to some value closing to $0$, at some point optimal policy will be able to find a shortest path to green square.

\item (2pts) Lets go back to our gridworld from (a) with the default values for $r_g$, $r_r$, $\gamma$ and with the value you specified for $r_s$. In this gridworld, our optimal policy from any unshaded square never terminates in the red square. Now suppose $r_s$ can take on any real, non-infinite value and is not restricted to $\{+1, 0, -1\}$ anymore. Give a value of $r_s$ such that there are unshaded squares starting from which following the optimal policy results in termination in the red square.

\textbf{Answer:}

\begin{equation}
r_s \leq -5
\end{equation}

\end{enumerate}


\subsection{Projections}

A projection is a linear map which if applied again returns the original result.

A projection can drop a dimension for example.


\lab{GMRES}{GMRES}
\label{lab:GMRES}
\objective{In this lab we will learn how to use the GMRES algorithm.}

The GMRES (``Generalized Minimal Residuals") algorithm is an effecient way to solve large linear systems.
It is an iterative method that uses Krylov subspaces to reduce a high-dimensional problem to a sequence of smaller dimensional
problems.

\begin{comment}
\section*{The Arnoldi Iteration and Approximate Solutions}
The basic idea of GMRES is as follows.
Let $A$ be an $m\times m$ matrix (real or complex), where $m$ is very large,
and let $b \in \mathbb{F}^m$ ($\mathbb{F}$ may either be the real or complex numbers).
Let $\mathcal{K}_n$ denote the order-$n$ Krylov subspace generated by $A$ and $b$.
In each iteration, we consider the least squares problem
\begin{equation}
\underset{x \in \mathcal{K}_n}{\text{minimize}}\qquad \|b-Ax\|_2.
\label{eq:GMRES_lstsq1}
\end{equation}
Now if $x \in K_n$, then $x$ can be expressed as a linear combination of basis vectors $b, Ab, \ldots, A^{n-1}b$, i.e.
\[
x = y_1b + y_2Ab + \cdots + y_nA^{n-1}b.
\]
If we let $K_n$ be the matrix whose columns are $b, Ab, A^{2}b, \cdots, A^{n-1}b$, then we can write this simply as
$x = K_n y$.
Then the solution of the least squares problem is the vector $K_{n}y$ such that $\|b-A K_{n}y\|_2$ is minimized.
\begin{figure}
\includegraphics[width=\textwidth]{LeastSquares}
\caption{GMRES involves solving the least squares problem repeatedly.}
\end{figure}

The major drawback of this approach is that it relies on the matrix $K_n$, which tends to be ill-conditioned due to its columns
being far from orthogonal (as discussed in Lab \ref{lab:kry_arnoldi}).
%To see this, suppose there is an eigenbasis for $A$ with associated eigenvalues, and suppose $\lambda$ is the largest eigenvalue.
%Then $\lambda^n$ is an eigenvalue of $A^n$.
%Since $\lambda$ is bigger than the other eigenvalues of $A$, $\lambda^n$ may be much, much bigger than the other eigenvalues of $A^n$,
%which are simply the eigenvalues of $A$ raised to the $n$th power.
%Thus the eigenvector associated with $\lambda$ often begins to dominate as we progress, with the end result being that the columns of
%$K_n$ become nearly linearly dependent.
The easiest fix in this situation is to use the Arnoldi iteration so that we have an orthonormal basis for $\mathcal{K}_n$ to work with.
Not only does this alleviate the problem of ill-conditioning, it also allows us to optimize in other ways, due to the special
structure of the matrices produced.

Let $q_1,\ldots, q_n$ be the orthonormal basis for $\mathcal{K}_n$ obtained by the Arnoldi iteration, and let $Q_n$ be the matrix
having these vectors as its columns.
Recall that $q_1 = b/\|b\|_2$.
Finally, let $H_n$ be the $(n+1)\times n$ upper Hessenberg matrix generated by the Arnoldi iteration, and let $e_1=(1,0,\cdots,0)$.

In this orthonormal basis, $x \in \mathcal{K}_n$ implies that there is some vector $y$ such that $x = Q_n y$.
Further, it is not hard to check that the matrices generated by the Arnoldi iteration satisfy the equation
\[
AQ_n = Q_{n+1}H_n.
\]
We also have the identity
\[
b = \|b\|_2q_1 = \|b\|_2Q_{n+1}e_1.
\]
Putting all of this together, we can rewrite the objective function of our least squares problem as follows:
\begin{align*}
\|b - Ax\|_2 &= \|Ax - b\|_2\\
&= \|AQ_ny - b\|_2\\
&= \|Q_{n+1}H_ny - \left(\|b\|_2Q_{n+1}e_1\right)\|_2\\
&= \|Q_{n+1}\left(H_n y - \|b\|_2e_1\right)\|_2.
\end{align*}

The matrix $Q_{n+1}$ has orthonormal columns, but it does not have enough columns to be a unitary matrix.
Let us extend the set $q_1,\ldots, q_{n+1}$ to an orthonormal basis $q_1,\ldots,q_{n+1},q_{n+2},\ldots,q_m$
of our space, and let $Q'$ be the matrix whose columns are equal to these vectors.
Then $Q'$ is now a unitary matrix, and hence preserves the norm, i.e. $\|Q'z\|_2 = \|z\|_2$ for all $z \in \mathbb{F}^m$.
Given $x \in \mathbb{F}^{n+1}$, if we define $x' \in \mathbb{F}^m$ to be
\[
x' =
\begin{bmatrix}
  x\\
  0\\
  \vdots\\
  0
\end{bmatrix},
\]
then you can easily check that
\[
Q'x' = Q_{n+1}x.
\]
From this, we deduce that
\begin{align*}
\|Q_{n+1}x\|_2 &= \|Q'x'\|_2\\
& = \|x'\|_2\\
&= \|x\|_2.
\end{align*}
Hence, we conclude that
\[
\|Q_{n+1}\left(H_n y - \|b\|_2e_1\right)\|_2 = \|H_n y - \|b\|_2e_1\|_2.
\]
Thus, the least squares problem given by \ref{eq:GMRES_lstsq1} is equivalent to the problem
\begin{equation}
\underset{y \in \mathbb{F}^n}{\text{minimize}}\qquad \|H_n y - \|b\|_2e_1\|_2.
\label{eq:GMRES_lstsq2}
\end{equation}
If $y$ is the solution to this problem, then the solution to \ref{eq:GMRES_lstsq1}, and hence an approximate
solution to $Ax = b$ is given by $x=Q_n y$.

We can measure how good our approximate solution is by considering the residual, which we define to be
\[
\frac{\|Ax-b\|_2}{\|b\|_2}.
\]
We can express this residual in terms of $y$ as follows:
\begin{equation}
\frac{\|H_n y - \|b\|_2e_1\|_2}{\|b\|_2}.
\label{eq:GMRES_residual}
\end{equation}
\end{comment}

\section*{The GMRES Algorithm}
Let $A$ be an invertible $m \times m$ matrix and let $\b$ be an $m$-vector.
Let $\mathcal{K}_n(A, \b)$ be the order-$n$ Krylov subspace generated by $A$ and $\b$.
The idea of the GMRES algorithm is that instead of solving $A\x = \b$ directly, we solve the least squares problem
\begin{equation}
\underset{\x \in \mathcal{K}_n}{\text{minimize}}\qquad \|\b-A\x\|_2
\label{eq:GMRES_lstsq1}
\end{equation}
for increasing values of $n$.
At each iteration, we compute the \emph{residual}, or error between the least squares solution and a true solution of $A\x = \b$.
The algorithm returns when this residual is sufficiently small.
In good circumstances, this will happen when $n$ is still much less than $m$.

The GMRES algorithm is integrated with the Arnoldi iteration for numerical stabiility, so that instead of solving \eqref{eq:GMRES_lstsq1}, at the $n^{th}$ iteration we solve
\begin{equation}
\underset{\y \in \mathbb{F}^n}{\text{minimize}}\qquad \|H_n \y - \|\b\|_2\e_1\|_2.
\label{eq:GMRES_lstsq2}
\end{equation}
Here, $H_n$ is the $(n+1)\times n$ upper Hessenberg matrix generated by the Arnoldi iteration and $\e_1$ is the vector $(1, 0, \ldots, 0)$ of length $n+1$.
If $\y$ is the minimizer for the $n^{th}$ iteration of \eqref{eq:GMRES_lstsq2}, then the residual is
\begin{equation}
\frac{\|H_n \y - \|\b\|_2\e_1\|_2}{\|\b\|_2},
\label{eq:GMRES_residual}
\end{equation}
and the corresponding minimizer for \eqref{eq:GMRES_lstsq1} is $Q_n\y$, where $Q_n$ is the matrix whose columns are $\q_1, \ldots, \q_n$ as defined by the Arnoldi iteration.
This algorithm is outlined in Algorithm \ref{alg:gmres}.
For a complete derivation see [TODO: ref textbook].

\begin{algorithm}
\begin{algorithmic}[1]
\Procedure{GMRES}{$A, \b, k, tol$}
	\State $Q \gets \empty{\size{\b}}{k+1}$			\Comment{Initialize}
	\State $H \gets \zeros{k+1}{ k}$
	\State $Q[:,0] = \b/\norm{\b}_2$
    \For{$n=1\ldots k$}
        \State Set entries of $Q$ and $H$ as in Arnoldi iteration
        \State Calculate least squares solution $\y$ of \ref{eq:GMRES_lstsq2}.
        \State Calculate the residual $res$ given by Equation \ref{eq:GMRES_residual}.
        \If{$res < tol$}
            \State \pseudoli{return} $Q[:,n+1]\y, \,\, res$
        \EndIf
    \EndFor
    \State \pseudoli{return} $Q[:,n+1]\y, \,\, res$
\EndProcedure
\end{algorithmic}
\caption{The GMRES algorithm. This algorithm operates on a vector $\b$ and matrix $A$.
It iterates $k$ times or until the residual is less than $tol$, returning an approximate solution to $A\x=\b$ and the error in this approximation.}
\label{alg:gmres}
\end{algorithm}

%\begin{warn}
%The Python function \li{linalg.lstsq} solves a least squares problem, returning not only the vector, $y$, but also the residual,
%the rank of the matrix, and the singular values.
%Be careful when you write your code that you access the correct results and don't just assume that \li{linalg.lstsq} returns the
%vector that you want.
%The least squares solver also returns a residual, but it's not the number we reference in this book, so be sure to take the square
%root of the residual reported by the solver and divide by $\norm{b}$ to get $res=\norm{Ax-b}/\norm{b}$.
%\end{warn}

% Hint: explain how to find lstsq to find the residual?
\begin{problem}
Use Algorithm \ref{alg:gmres} to complete the following Python function implementing the GMRES algorithm.
\begin{lstlisting}
def gmres(b, Amul, k=100, tol=1E-8):
    '''Use the GMRES algorithm to approximate the solution to Ax=b, where A is the linear operator defined by `Amul'.

    INPUTS:
    b    - A NumPy array.
    Amul - A function handle. Should describe a linear operator.
    k    - Maximum number of iterations of the GMRES algorithm. Defaults
           to 100.
    tol  - Stop iterating if the residual is less than `tol'. Defaults to
           1E-8.

    RETURN:
    Return (y, res) where `y' is an approximate solution to Ax=b and `res'
    is the residual.

    Examples:
    >>> A = np.array([[1,0,0],[0,2,0],[0,0,3]])
    >>> Amul = lambda x: A.dot(x)
    >>> b = np.array([1, 4, 6])
    >>> gmres(Amul, b)
    (array([ 1.,  2.,  2.]), 1.5083413465299765e-17)
    '''
\end{lstlisting}
You make make the following assumptions:
\begin{enumerate}
\item The input \li{b} is a real array and the function \li{Amul()} always outputs real arrays.
\item The vectors found by the Arnoldi iteration will never be zero. In other words, $\b$ will never be in a proper invariant subspace of $A$.
\end{enumerate}

Hint: Use \li{scipy.linalg.lstsq()} to solve the least squares problem.
\label{prob:MyGMRES}
\end{problem}

\subsection*{Convergence of GMRES}
At the $n$-th iteration, GMRES computes the best approximate solution $\x \in \mathcal{K}_n$ to $A\x = \b$.
If $A$ is full rank, then $\mathcal{K}_m = \mathbb{F}^m$, so the $m^{th}$ iteration will always return an exact answer.
However, we say the algorithm converges after $n$ steps if the $n^{th}$ residual is sufficiently small.

The rate of convergence of GMRES depends on the eigenvalues of $A$.

\begin{problem}\label{prob:plot_gmres}
Finish the following Python function by modifying your solution to Problem \ref{prob:MyGMRES}.

\begin{lstlisting}
def plot_gmres(b, A, tol=1E-8):
    '''Use the GMRES algorithm to approximate the solution to Ax=b.

    INPUTS:
    b   - A 1-D NumPy array of length m.
    A   - A 2-D NumPy array of shape mxm.
    tol - Stop iterating and create the desired plots if the residual is
          less than `tol'. Defaults to 1E-8.

    OUTPUT:
    Follow the GMRES algorithm until the residual is less than tol, for a
    maximum of m iterations. Then create the two following plots (subplots
    of a single figure):

    1. Plot the eigenvalues of A in the complex plane

    2. Plot the convergence of the GMRES algorithm by plotting the
    iteration number on the x-axis and the residual on the y-axis.
    Use a log scale on the y-axis.
    '''
\end{lstlisting}

Use this function to investigate the convergence of GMRES as follows.
Define an $m\times m$ matrix
\[A_n = nI+P,\]
 where $I$ is the $m \times m$ identity matrix and $P$ is a $m \times m$ matrix of numbers from a random normal distribution with mean 0 and standard deviation $1/(2\sqrt{m})$.
 Call \li{plot_gmres} on $A_n$ for $n=-4,-2,0,2,4$. Use $m=200$ and let \li{b} be an array of ones. What do you conjecture about the convergence of the GMRES algorithm?

 Hints:
 \begin{enumerate}
 \item Create a plot with a log scale on the y-axis with the function \li{plt.semilogy()}.
 \item Create a matrix with entries from a random normal distribution with \li{np.random.rand()}.
 \item Output for $n=2$, $m=200$ is in Figure \ref{fig:plot_gmres} below.
 \end{enumerate}
Ideas for this problem were taken from Example 35.1 on p.271 of \cite{Trefethen1997}.

\begin{figure}[H]
\includegraphics[width=.7\textwidth]{plot_gmres.pdf}
\caption{The left plot is the eigenvalues of the matrix $A_2$, which is defined in Problem \ref{prob:plot_gmres}.
The right plot is the convergence of the GMRES algorithm on $A_2$ with starting vector $\b = (1, 1, \ldots, 1)$.
This figure is one possible output of the function \li{plot_gmres()}.}
\label{fig:plot_gmres}
\end{figure}
\end{problem}

\section*{Improving GMRES}
There are many ways to make the GMRES algorithm more robust and efficient.

\subsection*{Breakdowns in GMRES}
One of the selling points of GMRES is that it can't break down unless it has reached an exact solution.

The only way GMRES could break down is if a vector found by the Arnoldi iteration is 0.
That is, suppose, we have already computed
\[\mathcal{K}_n(A, \b) = \text{span}\{\b, A\b, \ldots, A^{n-1}\b\} = \text{span}\{\q_1, \ldots, \q_n\}.\]
We next compute $A^n\b$ and orthogonalize it against $\mathcal{K}_n(A, \b)$, yielding $\q_{n+1}$.
But if $A^n\b \in \mathcal{K}_n(A,\b)$ then $\q_{n+1}$ will be 0, and our algorithm will break when we try to normalize $\q_{n+1}$.

In this situation, the least squares solution to \eqref{eq:GMRES_lstsq2} is an \emph{exact} solution to $A\x=\b$.
In other words, $\b$ is in the $\text{span}\{\q_1, \ldots, \q_n\}$.

\begin{problem}
Update your solution to Problem \ref{prob:MyGMRES} to deal with the scenario described above.
Hint: Use something similar to lines 11-12 of the Arnoldi algorithm in Lab \ref{lab:kry_arnoldi}.
\label{prob:GMRES3}
\end{problem}

% This section is poorly written. It is vague, not mathematically precise.
% If we ever want to include it, then it needs a lot of work.
\begin{comment}
\subsection*{Optimizing Least-Squares for GMRES (Optional)}
The Hessenberg structure and the Krylov subspace relations enable us to save time on the least-squares part of the problem if we use QR factorization.
Observe that if $H_n$ can be factored as $Q_n R_n$, where $Q_n$ is not the same matrix as above and $R_n$ is invertible upper triangular, we may solve the least squares problem by simply solving $R_n x_n=\norm{b} Q_{n}^{H}e_1$ via back substitution.
There are two ways in which we can speed up this process.
First, we take advantage of the Hessenberg structure by using the techniques from Problem \ref{prob:givens_hessenberg} in Lab \ref{lab:givens}.
Recall that the technique in this situation was to use Givens rotations to eliminate the subdiagonal elements one at a time.
This process, which was part of a previous lab, reduces the operation count from $O(n^3)$ to $O(n^2)$.
The second speedup comes from the fact that we already know the QR factorization for $H_{n-1}$ from the previous step of the algorithm.
This means that we can simply update the QR factorization from the previous step rather than computing it all over again.
Since $H_{n}$ has only one more column and row than $H_{n-1},$ all we need to do is update the last column by performing all previous Givens rotations on just the last column of $H_n$, which requires only $O(n)$ work.
Then we perform one final Givens rotation on $H_n$ to eliminate the new subdiagonal entry which was not present in $H_{n-1}$.
Thus, the QR factorization of $H_n$ can be reduced from an $O(n^3)$ process to only $O(n)$ using these special techniques.

The back substitution necessary to solve the least squares problem can also be reduced to an operation of $O(n)$.

The speedup from $O(n^3)$ to $O(n)$ is very good, but it can only partially alleviate the problems that come with a problem that is ill-suited for GMRES.
It may still be useful because it allows us to perform more iterations in a reasonable amount of time.
In many situations, the simple technique of the next section will keep $n$ low enough that the optimizations from this section are not critical.

 \begin{problem}
 \label{prob:GMRES2}
 (Optional) Modify MyGMRES to incorporate these optimizations, and call this program MyGMRES1.
 Run both programs on a series of five random $100\times 100$ matrices and compare the time each requires.
 Are the gains substantial?
 Try it again matrices of size $1000\times 1000$ or larger, and see how substantial the difference becomes.
 Try the same thing using the techniqes of the next section.
 Explain why the difference in performance is less dramatic this time.
 \end{problem}
\end{comment}

\subsection*{GMRES with restarts}
The first few iterations of GMRES have low spatial and temporal complexity.
However, as $k$ increases, the $k^{th}$ iteration of GMRES becomes more expensive in both time and memory.
In fact, computing the $k^{th}$ iteration of GMRES for very large $k$ can be prohibitively complex.

This issue is addressed by using GMRES(k), or GMRES with restarts.
When $k$ becomes large, this algorithm restarts GMRES but with an improved initial guess.
GMRES with restarts is outlined in Algorithm \ref{alg:gmres_k}.

\begin{algorithm}
\begin{algorithmic}[1]
\Procedure{GMRES(k)}{$A, \b, k, tol, restarts$}
  \State $r \gets 0$ \Comment{Restart counter}
  \State $Q \gets \empty{\size{b}}{k+1}$			\Comment{Initialize}
\State $H \gets \zeros{k+1}{ k}$
   \While{$r \leq restarts$}
	\State{ Perform the GMRES algorithm, obtaining a least squares solution $\y$}
	\State{ If the desired tolerance was reached, return. Otherwise, continue.}
    \State $b \gets Q[:,k+1]\y$
    \State $r \gets r+1$
    \EndWhile
    \State \pseudoli{return} $Q[:,k+1]\y, \,\, res$		\Comment{Return the approximate solution and the residual}
\EndProcedure
\end{algorithmic}
\caption{The GMRES(k) algorithm. This algorithm performs GMRES on a vector $\b$ and matrix $A$. It iterates $k$ times before restarting.
It terminates after $restarts$ restarts or when the residual is less than $tol$, returning an approximate solution to $A\x=\b$ and the error in this approximation. }
\label{alg:gmres_k}
\end{algorithm}

The algorithm GMRES(k) will always have manageable spatial and temporal complexity, but it is less reliable than GMRES.
If the true solution $\x$ to $A\x=\b$ is nearly orthogonal to the Krylov subspaces $\mathcal{K}_n(A, \b)$ for $n\leq k$, then GMRES(k) could converge very slowly or not at all.

\begin{problem}
Implement Algorithm \ref{alg:gmres_k} with the following function.
\begin{lstlisting}
def gmres_k(b, Amul, k=100, tol=1E-8, restarts=50):
    '''Use the GMRES(k) algorithm to approximate the solution to Ax=b, where A is the linear operator defined by `Amul'.

    INPUTS:
    b        - A NumPy array.
    Amul     - A function handle. Should describe a linear operator.
    k        - Maximum number of iterations of the GMRES algorithm before
              restarting. Defaults to 100.
    tol      - Stop iterating if the residual is less than `tol'. Defaults
              to 1E-8.
    restarts - Maximum number of restarts. Defaults to 50.

    RETURN:
    Return (y, res) where `y' is an approximate solution to Ax=b and `res'
    is the residual.
    '''
\end{lstlisting}

Compare the speed of \li{gmres()} from Problem \ref{prob:MyGMRES} and \li{gmres_k()} on the matrices in Problem \ref{prob:plot_gmres}.
\label{prob:GMRES3}
\end{problem}

\section*{GMRES in SciPy}
The GMRES algorithm is implemented in SciPy as the function \li{scipy.sparse.linalg.gmres()}.
Here we use this function to solve $A\x=\b$ where $A$ is a random $300 \times 300$ matrix and $\b$ is a random vector.

\begin{lstlisting}
>>> import numpy as np
>>> from scipy import sparse as spar
>>> from scipy import linalg as la
>>>
>>> A = np.random.rand(300, 300)
>>> b = np.random(300)
>>> x, info = spar.linalg.gmres(A, b)
>>> info
3000
\end{lstlisting}

The function outputs two objects: the approximate solution \li{x} and a constant \li{info} telling if the function converged.
If \li{info=0} then convergence occured; if \li{info} is positive then it equals the number of iterations performed.
In this case, the function performed 3000 iterations of GMRES before returning the approximate solution \li{x}.
We can check how close the solution is.
\begin{lstlisting}
>>> la.norm(A.dot(x)-b)
4.744196381683801
\end{lstlisting}

We can get a better approximation using GMRES with restarts.
\begin{lstlisting}
>>> # Restart after 1000 iterations
>>> x, info = spar.linalg.gmres(A, b, restart=1000)
>>> info
0
>>> la.norm(A.dot(x)-b)
1.0280404494143551e-12
\end{lstlisting}
This time, the returned approximation \li{x} is about as close to a true solution as we could hope for.


\section{Variance}

\[
(\sigma)^2 = \frac{1}{N}\sum\limits_{i=1}^n (x_{i} - \mu)^2
\]


\lab{Numerical Methods for Initial Value Problems; Harmonic Oscillators}{Numerical Methods for Initial Value Problems; Harmonic Oscillators}
\label{lab:ivp}

\objective{Implement several basic numerical methods for initial value problems (IVPs), and use them to study harmonic oscillators.}

\section*{Methods for Initial Value Problems}
Consider the initial value problem
\begin{align}
	\begin{split}
y' &= f(x,y),\quad a \leq x \leq b, \\
y(a) &= y_0,
	\end{split}\label{ivp:generic}
\end{align}
where $f$ is a continuous function.
A solution of \eqref{ivp:generic} is a continuously differentiable function $y(x)$ that satisfies the equation $y' = f(x,y)$ on the interval $[a,b]$ and for which $y(a) = y_0$.
In this lab we will focus on numerical methods for approximating $y(x)$, and sidestep the important mathematical problem of verifying that \eqref{ivp:generic} has a unique solution.

For many IVPs it is impossible to find a closed-form, analytic expression for the solution.
When there is a closed-form expression for the solution, it may not be very useful.
In both cases, numerical methods must be relied on to understand the solutions of \eqref{ivp:generic}.

As an example, consider the initial value problem
\begin{align}
	\begin{split}
y'(x) &= \sin y(x), \\
y(0) &= y_0.
	\end{split}\label{ivp:example}
\end{align}
The solution $y(x)$ is defined implicitly by
\[x = \ln \left|\frac{\cos y_0 + \cot y_0}{\csc y + \cot y} \right|.\]
This analytic expression does not provide much intuition, so we turn to a combination of qualitative and numerical methods.
Since $\sin (n \pi) = 0$, this differential equation has constant solutions $y_n(x) = n \pi,$ $n \in \mathbb{N}$.
We can also use an IVP solver to numerically approximate solutions for several other initial values.
After plotting these solutions (see Figure \ref{ivp:int_curves}), it becomes obvious how solutions of \eqref{ivp:example} behave in general.

\begin{figure}
\centering
\includegraphics[width=\textwidth]{example.pdf}
\caption{Several solutions of \eqref{ivp:example}, using Numpy's IVP solver \li{dopri5}. }
\label{ivp:int_curves}
\end{figure}

\section*{Numerical Methods}
Numerical methods for solving initial value problems require us to approximate the solution on a set of grid points $a = x_0< x_1<\hdots< x_n = b$ in our interval.
For simplicity we will assume that each of the $n$ subintervals $[x_{i-1},x_i]$ has equal length $h = (b-a)/n$. $h$ is called the \textit{step size}.
We then look for values $y_0, y_1, \hdots, y_n$ that approximate the solution at the grid points.
For each $i$, Taylor's theorem says that
\begin{align*}
y(x_{i+1}) &= y(x_{i}) + h y'(x_i) + \frac{h^2}{2} y''(\xi_i)\text{ for some }\xi_i \in [x_i,x_{i+1}].
\end{align*}
The quantity $\frac{h^2}{2} y''(\xi_i)$ is negligible for small $h$, and thus
\begin{align*}
y(x_{i+1}) &\approx y(x_{i}) + h y'(x_i)  ,\\
&\approx y(x_{i}) + h f(x_i,y(x_i)).
\end{align*}
This approximation leads to a method called Euler's method: Letting $y_0 = y(a)$, $y_{i+1}$ is given by $y_{i+1} = y_i +hf(x_i,y_i)$ for $i = 0, 1, \hdots, n-1$.
Euler's method is a first order method, with error $\mathcal{O}(h^1)$.
% \begin{enumerate}
% \item Let $y_0 = y(a)$.
% \item For $i = 0, 1, \hdots, n-1$, let $y_{i+1} = y_i +hf(x_i,y_i)$.
% \end{enumerate}

A similar application of Taylor's theorem shows that
\begin{align*}
y(x_{i}) &= y(x_{i+1}) - h y'(x_{i+1}) + \frac{h^2}{2} y''(\xi_i) \text{ for some } \xi_i \in [x_i,x_{i+1}]; \\
\end{align*}
thus for small $h$
\begin{align*}
y(x_{i+1}) &\approx  y(x_{i}) + h f(x_{i+1},y(x_{i+1})).
\end{align*}
This approximation leads to another first order method called the backwards Euler method: Letting $y_0 = y(a)$, for $i = 0, \hdots, n-1$ we  solve  $y_{i} = y_{i+1}-hf(x_{i+1},y_{i+1})$ for $y_{i+1}$.

Note that for both the Euler and backwards Euler methods, only $y_i, f,$ and other points in the interval $[x_i, x_{i+1}]$ are needed to find $y_{i+1}$.
Because of this, they are called \textit{one-step methods}.

Euler's method is an \textit{explicit method}.
The backwards Euler method is an \textit{implicit method} since an equation must be solved at each step to find $y_{i+1}$.
Explicit and implicit methods each have advantages and disadvantages.
While implicit methods require an equation to be solved at each time step, they often have better stability properties than explicit methods.

\begin{figure}[ht]
\centering
\includegraphics[width=\textwidth]{prob1.pdf}
\caption{The solution of \eqref{ivp:prob1}, alongside several approximations using Euler's method.}
\label{ivp:euler}
\end{figure}

\begin{problem} The solution of
\begin{align}
	\begin{split}
		y' &= y - 2x + 4,\quad 0 \leq x \leq 2, \\
		y(0) &= 0,
	\end{split}\label{ivp:prob1}
\end{align}
is given by $y(x) = -2+2x + 2e^x.$
Use Euler's method to numerically approximate the solution with step sizes $h = 0.4, 0.2$, and $0.1.$
Check that your results match Figure \ref{ivp:euler}.
\end{problem}

So how do we come up with numerical methods with higher order accuracy?
Using Taylor's theorem (as we did for Euler's method) to create higher-order one-step methods would lead to numerically approximating derivatives of $f(t,y)$ - not very desirable.

Let us look for a second order method of the form $y_{i+1} = y_i + a f(x_i+b, y_i+c)$.
By expanding $a f(x+b, y+c)$ with Taylor's theorem and matching constants in the equation
\begin{align*}
f(x,y) + \frac{h}{2}f'(x,y) &= f(x,y) + \frac{h}{2}\frac{\partial f}{\partial x}(x,y) + \frac{h}{2}\frac{\partial f}{\partial y}(x,y) \cdot f(x,y),
\end{align*}
we find that $a = h, b = h/2,$ and $c = h/2$.
This method is called the Midpoint method.
IVP solvers with this general form are called \textit{Runge-Kutta methods}.
% Another second order Runge-Kutta method is the modified Euler method:
% $y_{i+1} = y_i + \frac{h}{2}[ f(x_i, y_i) + f(x_{i+1}, y_i+ hf(x_i, y_i))]$

There are many Runge-Kutta methods with varying orders of accuracy.
Methods of order four or higher are most commonly used. A fourth order Runge-Kutta method iterates as follows:
\begin{align*}
	\begin{split}
K_1 &= f(x_i,y_i), \\
K_2 &= f(x_i + \frac{h}{2}, y_i + \frac{h}{2} K_1),\\
K_3 &= f(x_i + \frac{h}{2} , y_i + \frac{h}{2} K_2),\\
K_4 &= f(x_{i+1} , y_i + h K_3),\\
y_{i+1} &= y_i + \frac{h}{6}(K_1 + 2K_2 + 2K_3 + K_4).
	\end{split}
\end{align*}

Notice that these methods are doing a type of quadrature where we are sampling the function at different points and then performing computation using the samples and some inherent weights.
For example, consider a differential equation
\[ y' = f(t).\]
Since the function $f$ has no $y$ dependence, this is a simple integration problem, and these IVP methods become well known quadrature methods. In this case, Euler's method corresponds to the left hand sum, and backward Euler's method corresponds to the right hand sum.
The modified Euler and midpoint methods are second order IVP methods that correspond to the trapezoidal and midpoint rules for integration, respectively. RK4 corresponds to Simpson's rule for integration.

\begin{comment}
\begin{problem}
Suppose a differential equation is given by
\[ y' = f(t).\]
Since the function $f$ has no $y$ dependence, this a simple integration problem.
Which integration approximation methods correspond to Euler's method, backward Euler's method, modified Euler's method, the Midpoint method, and the fourth order Runge-Kutta method (RK4)?
\end{problem}
\end{comment}

\section*{Advantages of Higher-Order Methods}

Higher-order methods are usually much more efficient. One way to measure this efficiency is to determine how many times the right hand side of the initial value problem must be evaluated to provide a desired accuracy.  As an example, consider the initial value problem
\begin{align}
	\begin{split}
		y' &= y \cos(x), x \in [0,8],\\
		y(0) &= 1.
	\end{split}
	\label{ivp:efficiency_problem}
\end{align}
Figure \ref{ivp:efficiency_figure} illustrates the comparative efficiency of the Euler, Midpoint, and RK4 methods.  The figure also demonstrates another point: since the lower order methods require more floating point operations, floating point error limits the highest possible accuracy that can be achieved with lower order methods.

\begin{figure}[ht]
\centering
\includegraphics[width=\textwidth]{Efficiency.pdf}
\caption{Here we graph the relative error in computing the solution of \eqref{ivp:efficiency_problem} at $x = 8$, versus the number of times the right side of \eqref{ivp:efficiency_problem} must be evaluated.  }
\label{ivp:efficiency_figure}
\end{figure}

Let $t^*$ be an approximation of some value $t$. The relative error of the approximation is
\[ \frac{|t^*-t|}{|t|}.
\]
Note that the relative error is simply the absolute error $|t^*-t|$ normalized by the size of $t$.  A method with order $p$ has error of the form
\[E(h) = C h^p. \]
This means that the graph of $\log (E)$ versus $\log(h)$ has slope $p$.
The relative error of a numerical method can be approximated and graphed to verify that $p$th order convergence is occurring. For example, consider the IVP
 \begin{align}
 	\begin{split}
 y' &= y - 2x + 4,\quad 0 \leq x \leq 2, \\
 y(0) &= 0.
 	\end{split} \label{ivp:prob2}
 \end{align}
The following code solves the initial value problem on several grids using the Euler method, approximates the relative error in computing $y(2)$ and creates a plot (see Figure \ref{ivp:relative_error}).

\begin{lstlisting}
import matplotlib.pyplot as plt

a, b, ya = 0., 2., 0.

def ode_f(x,y):
	return np.array([y -2*x + 4.])

best_grid = 320					#  number of subintervals in most refined grid
# Requires an implementation of the euler method
best_val = euler(ode_f,ya,a,b,best_grid)[-1]

smaller_grids = [10,20,40,80]  # number of subintervals in smaller grids
h = [2./N for N in smaller_grids]
Euler_sol = [euler(ode_f,ya,a,b,N)[-1] for N in smaller_grids]
Euler_error = [abs(( val - best_val)/best_val ) for val in Euler_sol]

plt.loglog(h, Euler_error, '-b', label="Euler method"	  , linewidth=2.)
plt.show()

\end{lstlisting}

\begin{problem} Consider the IVP \eqref{ivp:prob2}.
Use the Midpoint method and the fourth order Runge-Kutta method (RK4) to approximate the value of the solution at $x = 2$, with a step size of $h = 0.2,$ $ 0.1,$ $0.05 $, $0.025,$ and $0.0125.$
Create a log-log plot of the relative error of each approximation using the \li{loglog} function in \li{matplotlib} (see Figure \ref{ivp:relative_error}).
\end{problem}

\begin{figure}[ht]
\centering
\includegraphics[width=\textwidth]{relative_error.pdf}
\caption{The solution of $y' -y= -2x+4,$ $y(0) = 0$, is $y(x) = -2+2x + 2e^x.$ This loglog plot shows the relative error in numerically approximating $y(2)$, using step sizes $h = 0.2,$ $0.1,$ $0.05,$ $0.025,$ and $0.0125$. The slope of each line demonstrates the first, second, and fourth order convergence of the Euler, Midpoint, and RK4 methods, respectively.}
\label{ivp:relative_error}
\end{figure}

\section*{Harmonic Oscillators and Resonance}
Harmonic oscillators show up often in classical mechanics.
A few examples include the pendulum (with small
 displacement), spring-mass systems, and the flow of electric current through various types of circuits.
A harmonic oscillator can be described by an initial value problem of the form
\begin{align*}
	&{}my'' + \gamma y' + ky = f(t) ,\\
	&{}y(0) = y_0,\quad
	y'(0) = y'_0.
\end{align*}
We will describe the construction of this mathematical model in the context of a spring-mass system.

Suppose an object with mass $m$ is placed at the end of a horizontal spring.
The natural position of the object is called the \textit{equilibrium position} for the system.
If the object is displaced from its equilibrium position and given an initial velocity,
it will act like a harmonic oscillator.
The principal property of a harmonic oscillator $y(t)$ is that once $y$ leaves its equilibrium value $y = 0$, it experiences a restoring force $F_r = -ky.$
This force pushes $y$ back towards its equilibrium.
Hooke's law says that this holds true for a
spring-mass system if the displacement $y$ is small.

Often there is an additional damping force $F_d$, often due to some type of friction.
This force is usually proportional to the $y'$ (the \emph{velocity}), is always in the opposite direction of $y'$, and represents energy leaving the system. (You can think of it as drag.)
Thus we have $F_d = -\gamma y', $ where $ \gamma \geq 0$ is constant.
We may also need to consider an additional external force $f(t)$, or a driving force, that is interacting with our spring-mass system.

By using Newton's law we obtain
\begin{align*}
ma &= F = F_r + F_d + f(t),\\
my'' &= -ky -\gamma y' + f(t).
\end{align*}

\section*{Simple harmonic oscillators}
A simple harmonic oscillator is a harmonic oscillator that is not damped ($\gamma =0$), and is free ($f = 0$) rather than forced ($f \not = 0$).
A simple harmonic oscillator can described by the IVP
\begin{align*}
&{}my'' + ky = 0,\\
&{}y(0) = y_0,\quad
y'(0) = y_0'.
\end{align*}
The solution of this IVP is $y = c_1\cos (\omega_0 t) + c_2 \sin (\omega_0 t)$ where $\omega_0 = \sqrt{k/m}$ is the natural frequency of the oscillator and $c_1$ and $c_2$ are determined by applying the initial conditions. This in turn can be written in the form
\[y = A\sin (\omega_0 t + \delta) .\]

To solve this IVP using the fourth order Runge Kutta method (RK4), we need to write this system in the form
\[z'(t) = f(t,z(t)) \]
We can do this by letting $z_1 = y, z_2 = y'$. Then we have \[     z'=
 \left[\begin{array}{c}z_1 \\z_2\end{array}\right]'  =  \left[\begin{array}{c}z_2 \\\frac{-k}{m}z_1\end{array}\right]= f(z).\]

\begin{problem} Use the RK4 method to solve for the simple harmonic oscillator satisfying
\begin{align}
	\begin{split}
&{}my'' + ky = 0,\quad 0 \leq t \leq 20, \\
&{}y(0) = 2, \quad
y'(0) = -1,
	\end{split}
	\label{ivp:simple_oscillator}
\end{align}
for $m = 1$ and $k =1$. Note that in your implementation of RK4, the constants $K_1, K_2, K_3,$ and $K_4$ become vectors with $n$ entries, where $n$ is the number of equations in the first-order system.

Plot your numerical approximation of $y(t)$.
Compare this with its numerical approximation when $m = 3$ and $k =1$. Consider: Why does the difference in solutions make sense physically?
\end{problem}

\begin{figure}[ht]
\centering
\includegraphics[width=\textwidth]{simple_oscillator.pdf}
\caption{Solutions of \eqref{ivp:simple_oscillator} for several values of $m$.}
\label{ivp:simple_oscillator_figure}
\end{figure}

\section*{Damped free harmonic oscillators}
We now consider damped free harmonic oscillators.
These systems are described by the differential equation
\[my''(t) +\gamma y'(t) + ky(t) = 0.\]
For fixed values of $m$ and $k$, it is interesting to study the effect of the damping coefficient $\gamma$.

The roots of the characteristic equation are \[r_1,r_2 = \frac{-\gamma \pm \sqrt{\gamma^2 -4km}}{2m} .\]
Note that the real parts of $r_1$ and $r_2$ are always negative, and so any solution $y(t)$ will decay over time due to a dissipation of the system energy.
There are several cases to consider for the general solution of this equation:
\begin{enumerate}
\item If $\gamma^2 > 4km$, then the general solution is $y(t) = c_1 e^{r_1t} + c_2e^{r_2t}$. Here the system is said to be $\textit{overdamped}$.
Notice from the general solution that there is no oscillation in this case.
\item If $\gamma^2 = 4km$, then the general solution is $y(t) = c_1 e^{\gamma t/2m} + c_2 te^{\gamma t/2m}$. Here the system is said to be $\textit{critically damped}$.
\item If $\gamma^2 < 4km$, then the general solution is
\begin{align*}
y(t) &= e^{-\gamma t/2m} \left[c_1\cos(\mu t) + c_2 \sin (\mu t)\right],\\
&= R e^{-\gamma t/2m}  \sin (\mu t + \delta),
\end{align*}
where $R$ and $\delta$ are fixed, and $\mu = \sqrt{4km-\gamma^2}/2m.$ This system does oscillate.
\end{enumerate}

\begin{problem}
Use the RK4 method to solve for the damped free harmonic oscillator satisfying
\begin{align*}
&{}y'' +\gamma y'+ y = 0, \quad 0 \leq t \leq 20,\\
&{}y(0) = 1, \quad
y'(0) = -1.
\end{align*}
For $\gamma = 1/2,$ and $\gamma = 1$, simultaneously plot your numerical approximations of $y$.
Find $y(20)$ accurate to four significant digits, by checking that the relative error is less than $5\times 10^{-5}$.
How many subintervals do you need?
\end{problem}

\section*{Forced harmonic oscillators without damping}
Let's look at the systems described by the differential equation
\begin{align}
my''(t)  + ky(t) &= F(t). \label{Forced_harm_osc}
\end{align}
In many instances the external force $F(t)$ is periodic, so let us assume that $F(t) = F_0 \cos(\omega t)$.
If $\omega_0 = \sqrt{k/m} \not = \omega,$ then the  general solution of \ref{Forced_harm_osc} is given by
\[y(t) = c_1 \cos (\omega_0 t) + c_2\sin (\omega_0 t) + \frac{F_0}{m(\omega_0^2 - \omega^2)} \cos (\omega t).\]
If $\omega_0 = \omega$, then the general solution is
\[y(t) = c_1 \cos (\omega_0 t) + c_2\sin (\omega_0 t) + \frac{F_0}{2m\omega_0} t \sin (\omega_0 t).\]

In the case that $\omega_0 = \omega$, the solution contains a term that grows arbitrarily large as $t \to \infty$.
If we included damping then the solution would be bounded, but would still be large for small $\gamma$ and $\omega$ close to $\omega_0$.

Consider a physical spring-mass system.
Equation \ref{Forced_harm_osc} holds only for small oscillations (this is where Hooke's law is applicable).
For larger oscillations, this equation will not hold.
However, the fact that the equation predicts large oscillations suggests the spring-mass system could fall apart as a result of the external force.
Mechanical resonance has been known to cause failure of bridges, buildings, and airplanes.

\begin{problem}
Use the RK4 method to solve for the damped and forced harmonic oscillator satisfying
\begin{align}
	\begin{split}
&{}2y'' + \gamma y' + 2y = 2 \cos (\omega t), \quad 0 \leq t \leq 40,\\
&{}y(0) = 2, \quad
y'(0) = -1.
	\end{split}
	\label{ivp:damped_forced_oscillator}
\end{align}
For the following values of $\gamma$ and $\omega,$ plot your numerical approximations of $y$ and find $y(40)$ accurate to four significant digits: $(\gamma, \omega) = (0.5, 1.5),$ $(0.1, 1.1),$ and $(0, 1).$
\end{problem}

\begin{figure}[ht]
\centering
\includegraphics[width=\textwidth]{damped_forced_oscillator.pdf}
\caption{Solutions of \eqref{ivp:damped_forced_oscillator} for several values of $\omega$ and $\gamma$.}
\label{ivp:damped_forced_oscillator_figure}
\end{figure}


\section{Derivative Rules}\label{sec:DerivativeRules}
Using the definition of the derivative of a function is quite tedious.
In this section we introduce a number of different shortcuts that can be used to compute the derivative.
Recall that the \ifont{definition of derivative} is:

Given any number $x$ for which the limit
$$f'(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}$$
exists, we assign to $x$ the number $f'(x)$.

Next, we give some basic \ifont{derivative rules} for finding derivatives without having to use the limit definition directly.

\begin{theorem}{Derivative of a Constant Function}{Derivative of a Constant Function}
Let $c$ be a constant, then $\ds{\frac{d}{dx}(c)=0}$.
\end{theorem}

\begin{proof}
Let $f(x)=c$ be a constant function. By the definition of derivative:
$$f'(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}=\lim_{h\to 0}\frac{c-c}{h}=\lim_{h\to 0}0=0.$$
\end{proof}

\begin{example}{Derivative of a Constant Function}{DerivativeConstantFunction}
 The derivative of $f(x)=17$ is $f'(x)=0$ since the derivative of a constant is $0$.
\end{example}

\begin{theorem}{The Power Rule}{The Power Rule}
If $n$ is a positive integer, then $\ds{\frac{d}{dx}(x^n)=nx^{n-1}}$.
\end{theorem}

\begin{proof}
We use the formula:
$$x^n-a^n=(x-a)(x^{n-1}+x^{n-2}a+\cdots+xa^{n-2}+a^{n-1})$$
which can be verified by mulitplying out the right side.
Let $f(x)=x^n$ be a power function for some positive integer $n$.
Then at any number $a$ we have:
$$f'(a)=\lim_{x\to a}\frac{f(x)-f(a)}{x-a}=\lim_{x\to a}\frac{x^n-a^n}{x-a}=\lim_{x\to a}(x^{n-1}+x^{n-2}a+\cdots+xa^{n-2}+a^{n-1})=na^{n-1}.$$
\end{proof}

It turns out that the Power Rule holds for any real number $n$ (though it is a bit more difficult to prove).

\begin{theorem}{The Power Rule (General)}{The Power Rule (General)}
If $n$ is any real number, then $\ds{\frac{d}{dx}(x^n)=nx^{n-1}}$.
\end{theorem}

\begin{example}{Derivative of a Power Function}{DerivativePowerFunction}
By the power rule, the derivative of $g(x)=x^4$ is $g'(x)=4x^3$.
\end{example}

\begin{theorem}{The Constant Multiple Rule}{The Constant Multiple Rule}
If $c$ is a constant and $f$ is a differentiable function, then $$\ds{\frac{d}{dx}[cf(x)]=c\frac{d}{dx}f(x)}.$$
\end{theorem}

\begin{proof}
For convenience let $g(x)=cf(x)$.
Then:
$$g'(x)=\lim_{h\to 0}\frac{g(x+h)-g(x)}{h}=\lim_{h\to 0}\frac{cf(x+h)-cf(x)}{h}$$
$$=\lim_{h\to 0}c\left[\frac{f(x+h)-f(x)}{h}\right]=c\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}=cf'(x),$$
where $c$ can be moved in front of the limit by the Limit Rules.
\end{proof}

\begin{example}{Derivative of a Multiple of a  Function}{DerivativeMultipleFunction}
By the constant multiple rule and the previous example, the derivative of $F(x)=2\cdot(17+x^4)$ is
$$F'(x)=2(4x^3)=8x^3.$$
\end{example}

\begin{theorem}{The Sum/Difference Rule}{The Sum/Difference Rule}
If $f$ and $g$ are both differentiable functions, then
$$\ds{\frac{d}{dx}[f(x)\pm g(x)]=\frac{d}{dx}f(x)\pm\frac{d}{dx}g(x)}.$$
\end{theorem}

\begin{proof}
For convenience let $r(x)=f(x)\pm g(x)$.
Then:
$$\begin{array}{ccl}
r'(x)&=&\ds{\lim_{h\to 0}\frac{r(x+h)-r(x)}{h}}\\
\\
&=&\ds{\lim_{h\to 0}\frac{[f(x+h)\pm g(x+h)]-[f(x)\pm g(x)]}{h}}\\
\\
~&=&\ds{\lim_{h\to 0}\left[\frac{f(x+h)-f(x)}{h}\pm\frac{g(x+h)-g(x)}{h}\right]}\\
\\
&=&\ds{\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}\pm\lim_{h\to 0}\frac{g(x+h)-g(x)}{h}}\\
\\
~&=&f'(x)+g'(x)\\\end{array}$$
\end{proof}

\begin{example}{Derivative of a Sum/Difference of Functions}{DerivativeSumDifferenceFunction}
By the sum/difference rule, the derivative of $h(x)=17+x^4$ is
$$h'(x)=f'(x)+g'(x)=0+4x^3=4x^3.$$
\end{example}

\begin{theorem}{The Product Rule}{The Product Rule}
If $f$ and $g$ are both differentiable functions, then
$$\ds{\frac{d}{dx}[f(x)\cdot g(x)]=f(x)\frac{d}{dx}[g(x)]+g(x)\frac{d}{dx}[f(x)]}.$$
\end{theorem}

\begin{proof}
For convenience let $r(x)=f(x)\cdot g(x)$.
As in the previous proof, we want to separate the functions $f$ and $g$.
The trick is to add and subtract $f(x+h)g(x)$ in the numerator.
Then:
$$\begin{array}{ccl}
r'(x)&=&\ds{\lim_{h\to 0}\frac{f(x+h)g(x+h)-f(x+h)g(x)+f(x+h)g(x)-f(x)g(x)}{h}}\\
\\
~&=&\ds{\lim_{h\to 0}\left[f(x+h)\frac{g(x+h)-g(x)}{h}+g(x)\frac{f(x+h)-f(x)}{h}\right]}\\
\\
~&=&\ds{\lim_{h\to 0}f(x+h)\cdot\lim_{h\to 0}\frac{g(x+h)-g(x)}{h}+\lim_{h\to 0}g(x)\cdot\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}}\\
\\
~&=&f(x)g'(x)+g(x)f'(x)\\\end{array}$$
\end{proof}

\begin{example}{Derivative of a Product of Functions}{DerivativeProductFunction}
Find the derivative of $\ds h(x)=(3x-1)(2x+3)$.
\end{example}

\begin{solution}
One way to do this question is to expand the expression.
Alternatively, we use the product rule with $f(x)=3x-1$ and $g(x)=2x+3$.
Note that $f'(x)=3$ and $g'(x)=2$, so,
$$h'(x)=(3)\cdot(2x+3)+(3x-1)\cdot(2)=6x+9+6x-2=12x+7.$$
\end{solution}

\begin{theorem}{The Quotient Rule}{The Quotient Rule}
If $f$ and $g$ are both differentiable functions, then $$\ds{\frac{d}{dx}\left[\frac{f(x)}{g(x)}\right]
=\frac{g(x)\frac{d}{dx}[f(x)]-f(x)\frac{d}{dx}[g(x)]}{[g(x)]^2}}.$$
\end{theorem}

\begin{proof}
The proof is similar to the previous proof but the trick is to add and subtract the term $f(x)g(x)$ in the numerator.
We omit the details.
\end{proof}

\begin{example}{Derivative of a Quotient of Functions}{DerivativeQuotientFunction}
Find the derivative of $\ds h(x)=\frac{3x-1}{2x+3}$.
\end{example}

\begin{solution}
By the quotient rule (using $f(x)=3x-1$ and $g(x)=2x+3$) we have:
$$h'(x)=\frac{\frac{d}{dx}(3x-1)\cdot (2x+3)-(3x-1)\cdot\frac{d}{dx}(2x+3)}{(2x+3)^2}$$
$$=\frac{3(2x+3)-(3x-1)(2)}{(2x+3)^2}=\frac{11}{(2x+3)^2}.$$
\end{solution}

\begin{example}{Second Derivative}{}
Find the second derivative of $f(x)=5x^3+3x^2$.
\end{example}

\begin{solution}
We must differentiate $f(x)$ twice:
$$f'(x)=15x^2+6x,$$
$$f''(x)=30x+6.$$
\end{solution}

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

\begin{enumialphparenastyle}

%%%%%%%%%%
\begin{ex}
Find the derivatives of the following functions.
\begin{multicols}{3}
\begin{enumerate}
	\item	$\ds x^{100}$
	\item	$\ds x^{-100}$
	\item	$\ds {1\over x^5}$
	\item	$\ds x^\pi$
	\item	$\ds x^{3/4}$
	\item	$\ds x^{-9/7}$
	\item	$\ds 5x^3+12x^2-15$
	\item	$\ds -4x^5 + 3x^2 - 5/x^2$
	\item	$\ds 5(-3x^2 + 5x + 1)$
	\item	$\ds (x+1)(x^2+2x-3)$
	\item	$\ds (x+1)(x^2+2x-3)^{-1}$
	\item	$\ds x^3(x^3-5x+10)$
	\item	$\ds (x^2+5x-3)(x^5)$
	\item	$\ds (x^2+5x-3)(x^{-5})$
	\item	$\ds (5x^3+12x^2-15)^{-1}$
\end{enumerate}
\end{multicols}
\begin{sol}
\begin{multicols}{3}
\begin{enumerate}
	\item	$\ds 100x^{99}$
	\item	$\ds -100x^{-101}$
	\item	$\ds -5x^{-6}$
	\item	$\ds \pi x^{\pi-1}$
	\item	$\ds (3/4)x^{-1/4}$
	\item	$\ds -(9/7)x^{-16/7}$
	\item	$\ds 15x^2+24x$
	\item	$\ds -20x^4+6x+10/x^3$
	\item	$\ds -30x+25$
	\item	$\ds 3x^2+6x-1$
	\item	$\ds -\frac{x^2+2x+5}{(x^2+2x-3)^2}$
	\item	$\ds 3x^2(x^3-5x+10)+x^3(3x^2-5)$
	\item	$\ds x^4(7x^2+30x-15)$
	\item	$\ds\frac{-3x^2-20x+15}{x^6}$
	\item	$\ds -\frac{3x(5x+8)}{(5x^3+12x^2-15)^2}$
\end{enumerate}
\end{multicols}
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Find an equation for the tangent line to $\ds f(x) = x^3/4 - 1/x$ at $x=-2$.
\begin{sol}
$y=13x/4+5$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
Find an equation for
the tangent line to $\ds f(x)= 3x^2 - \pi ^3$ at $x= 4$.
\begin{sol}
$\ds y=24x-48-\pi^3$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
Suppose the position of an object at time $t$ is  given by
$\ds f(t)=-49 t^2/10+5t+10$. Find a function giving the speed of the object
at time $t$. The acceleration of an object is the rate at which its
speed is changing, which means it is given by the derivative of the
speed function. Find the acceleration of the object at time $t$.
\begin{sol}
$-49t/5+5$, $-49/5$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
Let $\ds f(x) =x^3$ and $c= 3$. Sketch the graphs of $f$,
$cf$, $f'$, and $(cf)'$ on the same diagram.
\end{ex}

%%%%%%%%%%
\begin{ex}
The general polynomial $P$ of degree $n$ in the variable $x$
has the form $\ds P(x)= \sum _{k=0 } ^n a_k x^k = a_0 + a_1 x + \ldots
+ a_n x^n$. What is the derivative (with respect to $x$)
of $P$?
\begin{sol}
$\ds\sum_{k=1}^n ka_kx^{k-1}$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
Find a cubic polynomial whose graph has horizontal tangents at
$(-2 , 5)$ and $(2, 3)$.
\begin{sol}
$\ds x^3/16-3x/4+4$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
Prove that $\ds{d\over dx}(cf(x))= cf'(x)$ using the
definition of the derivative.
\end{ex}

%%%%%%%%%%
\begin{ex}
Suppose that $f$ and $g$ are differentiable at $x$. Show
that $f-g$ is differentiable at $x$ using the two linearity
properties from this section.
\end{ex}

%%%%%%%%%%
\begin{ex}
Use the product rule to compute the derivative of $\ds f(x)=(2x-3)^2$.
 Sketch the function.  Find an equation of the tangent line to the curve at
 $x=2$.  Sketch the tangent line at $x=2$.
\begin{sol}
$f'=4(2x-3)$, $y=4x-7$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
Suppose that $f$, $g$, and $h$ are differentiable functions.
Show that $(fgh)'(x) = f'(x) g(x)h(x) + f(x)g'(x) h(x) + f(x) g(x)
h'(x)$.
\end{ex}

%\exercise
%State and prove a rule to compute $(fghi)'(x)$,
%similar to the rule in the previous problem.
%
%\remark{Product notation}
%Suppose $\ds f_1 , f_2 , \ldots f_n$ are functions.
%The product of all these functions can be written
%$$ \prod _{k=1 } ^n f_k.$$
%This is similar to the use of $\ds \sum$ to denote a
%sum.
%For example,
%$$\prod _{k=1 } ^5 f_k =f_1 f_2 f_3 f_4 f_5$$
%and
%$$
%\prod _ {k=1 } ^n k = 1\cdot 2 \cdot \ldots \cdot n = n!.$$
%We sometimes use somewhat more complicated conditions; for example
%$$\prod _{k=1 , k\neq j } ^n f_k$$
%denotes the product of $\ds f_1$ through $\ds f_n$ except for $\ds f_j$.
%For example,
%$$\prod _{k=1 , k\neq 4} ^5 x^k = x\cdot x^2 \cdot x^3 \cdot x^5 =
%x^{11}.$$
%\endremark
%
%\exercise
%  The \dfont{generalized product rule}
%says that if $\ds f_1 , f_2 ,\ldots ,f_n$ are differentiable functions at
%  $x$ then
%$${d\over dx}\prod _{k=1 } ^n f_k(x) =
%\sum _{j=1 } ^n \left(f'_j (x) \prod _{k=1 , k\neq j} ^n
%   f_k (x)\right).$$
%Verify that this is the same as your answer to the previous problem
%when $n=4$,
%and write out what this says when $n=5$.

%%%%%%%%%%%
%\begin{ex}
%Find all points on the graph of
%$\ds f(x)=\sin^2(x)$ at which the tangent line is horizontal.
%\begin{sol}
%$n\pi/2$, any integer $n$
%\end{sol}
%\end{ex}
%
%%%%%%%%%%%
%\begin{ex}
%Find all points on the graph of $\ds f(x) = 2\sin(x) -
%\sin^2(x)$ at which the tangent line is horizontal.
%\begin{sol}
%$\pi/2+n\pi$, any integer $n$
%\end{sol}
%\end{ex}
%
%%%%%%%%%%%
%\begin{ex}
%Find an
 %equation for the tangent line to $\ds \sin^2(x)$ at
%$x=\pi/3$.
%\begin{sol}
%$\sqrt3x/2+3/4-\sqrt3\pi/6$
%\end{sol}
%\end{ex}
%
%%%%%%%%%%%
%\begin{ex}
%Find an equation for the tangent line to $\ds \sec ^2 x$
%at $x=\pi/3$.
%\begin{sol}
%$\ds 8\sqrt3x+4-8\sqrt3\pi/3$
%\end{sol}
%\end{ex}
%
%%%%%%%%%%%
%\begin{ex}
%Find an equation for the tangent line to $\ds \cos ^2 x -
%\sin ^2 (4x)$ at $x=\pi/6$.
%\begin{sol}
%$\ds 3\sqrt3x/2-\sqrt3\pi/4$
%\end{sol}
%\end{ex}
%
%%%%%%%%%%%
%\begin{ex}
%Find the points on the curve $\ds y= x+ 2\cos x$ that have a
%horizontal tangent line.
%\begin{sol}
%$\ds \pi/6+2n\pi$, $5\pi/6+2n\pi$, any integer $n$
%\end{sol}
%\end{ex}
%
%%%%%%%%%%%
%\begin{ex}
%Let $C$ be a circle of radius $r$. Let $A$ be an arc on $C$
%subtending a central angle $\theta$. Let $B$ be the chord of
%$C$ whose endpoints are the endpoints of $A$. (Hence, $B$ also
%subtends $\theta$.) Let $s$ be the length of $A$
%and let $d$ be the length of $B$. Sketch a diagram of the situation
%and compute $\ds \lim_{\theta \to 0^+ } s/d$.
%\end{ex}

%%%%%%%%%%
\begin{ex}
Compute the derivative of $\ds {x^3\over x^3-5x+10}$.
\begin{sol}
$\ds {3x^2\over x^3-5x+10}-{x^3(3x^2-5)\over (x^3-5x+10)^2}$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
Compute the derivative of $\ds {x^2+5x-3\over x^5-6x^3+3x^2-7x+1}$.
\begin{sol}
$\ds {2x+5\over x^5-6x^3+3x^2-7x+1}-{(x^2+5x-3)(5x^4-18x^2+6x-7)\over(x^5-6x^3+3x^2-7x+1)^2}$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
Compute the derivative of $\ds {x\over\sqrt{x-625}}$.
\begin{sol}
$\ds \frac{x-1250}{2(x-625)^{3/2}}$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
Compute the derivative of $\ds {\sqrt{x-5}\over x^{20}}$.
\begin{sol}
$\ds \frac{200-39x}{2x^{21}\sqrt{x-5}}$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
Find an equation for the tangent line to $\ds f(x) = (x^2 -
4)/(5-x)$ at $x= 3$.
\begin{sol}
$\ds y=17x/4-41/4$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
Find an equation for the tangent line to
$\ds f(x) = (x-2)/(x^3 + 4x - 1)$ at $x=1$.
\begin{sol}
$y=11x/16-15/16$
\end{sol}
\end{ex}

%\exercise Let $P$ be a polynomial of degree $n$ and let $Q$ be a
%polynomial of degree $m$ (with $Q$ not the zero polynomial).
%Using sigma notation we can write
%$$P=\sum _{k=0 } ^n a_k x^k,\qquad
%Q=\sum_{k=0}^m b_k x^k.
%$$
%Use sigma notation to write the derivative of the
%{\dfont rational function\index{rational function}\/}
%$P/Q$.
%% \begin{sol} $\left(\sum_{k=0}^m b_k x^k\sum _{k=1}^n ka_k x^{k-1}-
%% \sum _{k=0 } ^n a_k x^k\sum_{k=1}^m kb_k x^k\right)/
%% \left(\sum_{k=0}^m b_k x^k\right)^2$
%% \end{sol}\end{ex}

%\exercise The curve $\ds y=1/(1+x^2)$ is an example of a class of
%curves each of which is called a {\dfont witch of
%Agnesi\index{witch of Agnesi}}.
%Sketch the curve and find the tangent line to the curve at
%$x= 5$. (The word {\em witch\/} here is a mistranslation of the
%original Italian, as described at
%$$\hbox{\url{http://mathworld.wolfram.com/WitchofAgnesi.html}
%\vb|http://mathworld.wolfram.com/WitchofAgnesi.html|\endurl}$$
%and
%$$\eqalign{
%\hbox{\url{http://instructional1.calstatela.edu/sgray/Agnesi/WitchHistory/Historynamewitch.html}
%\vb|http://|\endurl}%
%&\!\!\hbox{\url{http://instructional1.calstatela.edu/sgray/Agnesi/WitchHistory/Historynamewitch.html}
%\vb|instructional1.calstatela.edu/sgray/Agnesi/|\endurl}\cr
%&\hbox{\url{http://instructional1.calstatela.edu/sgray/Agnesi/WitchHistory/Historynamewitch.html}
%\vb|WitchHistory/Historynamewitch.html|\endurl.)}\cr
%}$$
%\begin{sol} $y=19/169-5x/338$
%\end{sol}\end{ex}
%%% \footnote{Due to a mistranslation of the Italian word
%%%   \emph{versiera} which actually refers to a rope that turns the
%%%   sail.}.

%%%%%%%%%%
\begin{ex}
If $f'(4) = 5$, $g'(4) = 12$, $(fg)(4)= f(4)g(4)=2$, and $g(4) = 6$,
compute $f(4)$ and $\ds{d\over dx}{f\over g}$ at 4.
\begin{sol}
$13/18$
\end{sol}
\end{ex}

\end{enumialphparenastyle}


The previous results can help improving the performance of any task involving comparisons performed homomorphically such as private database queries, $k$-nearest neighbour search, top-$k$ selection or step function evaluation in neural nets. In this section we choose to demonstrate the gain brought by our approach for sorting and min/max search which are subroutines needed for the aforementioned tasks.

\subsection{Sorting}
\label{subsec:sorting}

    To demonstrate the efficiency of our comparison algorithms, we applied them to a popular computational task that demands multiple comparisons, sorting.
	The best homomorphic sorting algorithm in terms of running time is the direct sorting algorithm due to {\c C}etin et al.~\cite{CDSS15}.
	For a given array $A = [a_0,\dots,a_{N-1}]$, this algorithm computes a \emph{comparison matrix} $\mL$ defined by
	\begin{align*}
		\mL_{ij} =
		\begin{cases}
			\LT(a_i, a_j) & \text{ if } i < j, \\
			0 & \text{ if } i = j, \\
			1 - \LT(a_j, a_i) & \text{ if } i > j.
		\end{cases}
	\end{align*}

        \emph{Example} : for $A = [5,1,7,2,3]$, the matrix $L\in\{0,1\}^{5\times 5}$ is given by:

        $$ \mL = \left(
          \begin{array}{ccccc}
            0 & 0 & 1 & 0 & 0 \\
            1 & 0 & 1 & 1 & 1 \\
            0 & 0 & 0 & 0 & 0 \\
            1 & 0 & 1 & 0 & 1 \\
            1 & 0 & 1 & 0 & 0
          \end{array}
\right)$$

        It is easy to see that the Hamming weight of the $i$th row of $\mL$ is unique and equal to the array index of $a_i$ after sorting the array $A$ in the descending order.
	For example, the zero weight indicates that there are no elements of $A$ bigger than $a_i$.
	Thus, $a_i$ has a zero index in $A$ after sorting; in other words, $a_i$ is the maximum element of $A$.

	Let $A'$ be a sorted version of $A$ in the descending order.
	To compute $A'[i]$ for any $i \in [0,N-1]$, we homomorphically select an element $a_j$ such that $\wt(\mL[j]) = i$.
	This can be done with the following sum
	\begin{align}\label{eq:sorting_extraction}
		A'[i] = \sum_{j=0}^{N-1} \EQ_{[0,N-1]}(i,\wt\left(\mL[j]\right)) \cdot a_j\,.
	\end{align}
	Note that the equality function should be defined on the set $[0,N-1]$, which implies that $N$ must be smaller than the plaintext modulus $p$.

	\begin{remark}
		Since the matrix $\mL$ is defined by $N(N-1)/2$ elements, it can be costly to keep it in memory for large~$N$.
		Instead, we can compute the Hamming weights of its rows by iteratively computing one comparison $\LT(a_i, a_j)$ with $i < j$ at a time.
		To achieve this, we create an array of size $N$ initialized with zeros that eventually will store the Hamming weights.
		Then, we add the outcome of $\LT(a_i, a_j)$ to the $i$th element of this array and the result of $1-\LT(a_i, a_j)$ to the $j$th element.
		In this approach, only $N$ elements of the Hamming weight array are being kept in RAM.
	\end{remark}

	The direct sorting algorithm requires $N(N-1)/2$ less-than operations to compute the matrix $\mL$ and $N^2$ equality operations to compute sorted elements of $A'$.
	While computing equalities, we can reduce the total number of non-scalar multiplications if $N$ is large enough.
	Recall $\EQ_\S$ needs $M = \log_2(p-1) + \wt(p-1) - 1$ non-scalar multiplications for any $\S \subseteq [0,p-1]$.
	Hence, to compute $\EQ_{[0,N-1]}(i,\wt\left(\mL[j]\right))$ for all $i \in [0,N-1]$, we should perform $N M$ multiplications.
	%$\EQ(i,\wt\left(\mL[j]\right)) = 1 - (i-\wt\left(\mL[j]\right))^{p-1}$
	Using Lemma~\ref{lem:difference_to_p-1} (see Appendix~\ref{app:proof_thm_less_than_total_degree}), we can rewrite:
	\begin{align*}
		\EQ_{[0,N-1]}\left(i,\wt\left(\mL[j]\right)\right) = 1 - \sum_{k=0}^{p-1} i^k \cdot \wt\left(\mL[j]\right)^{p-1-k}\,.
	\end{align*}
	If we precompute the powers $\wt\left(\mL[j]\right)^{p-1-k}$, then we need only $p-2$ non-scalar multiplications to compute all the equalities $\EQ(i,\wt\left(\mL[j]\right))$ as the index $i$ is not encrypted.
	Hence, if $N > (p-2)/M$, this approach results in a smaller number of non-scalar multiplications.
	Yet, this method introduces $p-1$ scalar multiplications (by powers $i^k$) and $p-2$ additions.
	However, these operations are much faster in HE schemes than non-scalar multiplication such that the gain from reducing non-scalar multiplications becomes dominant.

	The main advantage of direct sorting is that its multiplicative depth is independent of the array length, namely $d = d\left(\LT\right) + \ceil{\log_2 (p-1)} + 1$ with $d\left(\LT\right)$ given in Equation (\ref{eq:circuit-depth}).

	This allows to avoid large encryption parameters and costly bootstrapping operations.
	%We can further reduce this depth by computing the Hamming weight modulo a plaintext modulus $p$ that is equal or larger than the length of an array $n$.

\subsection{Minimum and maximum of an array}
\label{sec:min/max}

	Another application of our comparison algorithms is concerned with finding a minimum (or maximum) element of an array.
	To find the minimum of an array with $N$ elements, at least $N-1$ calls of the pairwise minimum function are required~\cite[Chapter 9]{CLR09}, which can be achieved, for instance, by the \emph{tournament method}.

	The tournament method consists of $\ceil{\log N}$ iterations.
	In each iteration, the input array is divided into pairs.
	If the array length is odd, one element is stashed for the next iteration.
	Then, the maximum of each pair is removed from the array.
	The algorithm stops when only one element is left; this is the minimum of the input array, see Figure~\ref{fig:minimum_tournament}.
	\begin{figure}
		\centering
		\begin{tikzpicture}
			% first stage
			\draw[black, thin] (0,0) rectangle (1,1);
			\draw[black, thin] (0,1.5) rectangle (1,2.5);
			\draw[black, thin] (0,3.0) rectangle (1,4);
			\draw[black, thin] (0,4.5) rectangle (1,5.5);

			\node at (0.5, 5.0) {$a_0$};
			\node at (0.5, 3.5) {$a_1$};
			\node at (0.5, 2.0) {$a_2$};
			\node at (0.5, 0.5) {$a_3$};

			% second stage
			\draw[black, thin] (1.5,0.75) rectangle (2.5,1.75);
			\draw[black, thin] (1.5,3.75) rectangle (2.5,4.75);

			\node at (2.0, 1.25) {$\min$};
			\node at (2.0, 4.25) {$\min$};

			\draw[black, thin, ->] (1,0.5) -- (2,0.5) -- (2,0.75);
			\draw[black, thin, ->] (1,2) -- (2,2) -- (2,1.75);

			\draw[black, thin, ->] (1,3.5) -- (2,3.5) -- (2,3.75);
			\draw[black, thin, ->] (1,5) -- (2,5) -- (2,4.75);

			% third stage
			\draw[black, thin] (3.0,2.25) rectangle (4.0,3.25);

			\node at (3.5, 2.75) {$\min$};

			\draw[black, thin, ->] (2.5,1.25) -- (3.5,1.25) -- (3.5,2.25);
			\draw[black, thin, ->] (2.5,4.25) -- (3.5,4.25) -- (3.5,3.25);

			% final outcome
			\draw[black, thin, ->] (4.0,2.75) -- (5.0,2.75);
			\node at (6.5, 2.75) {$\min(a_0,a_1,a_2,a_3)$};

		\end{tikzpicture}
		\caption{The tournament method of finding the minimum of an array. In each stage, the array elements are divided into pairs. Only minimum of a pair go to the next stage.}
		\label{fig:minimum_tournament}
	\end{figure}
	Unfortunately, the tournament method has a big multiplicative complexity, namely $\ceil{\log N} \cdot d(\min(x,y))$.
	In the HE world, this enforces us to use either impractical encryption parameters~\cite{TMP15} or a slow bootstrapping function.

	To reduce the depth of the array minimum algorithm, we can combine the tournament method and direct sorting.
	Let $A = [a_0,\dots,a_{N-1}]$ be an input array.
	First, we perform $T$ iterations of the tournament algorithm, which leaves us with an array $A' = [a'_0, \dots, a'_{N'-1}]$ of length $N' = \ceil{N/2^T}$ containing minimal elements of $A$.
	Then, we can find the minimum by computing the comparison table $\mL$ as in direct sorting and extracting one of the minimal elements.
	If $M(f)$ is the non-scalar multiplicative complexity of a function $f$, then the total number of non-scalar multiplications to find the minimum of an array is approximately equal to
	\begin{align*}
		(N - N') \cdot M\left(\min(x,y)\right) + \frac{N'(N'-1)}{2} \cdot M\left(\LT\right) \\
		+ M(\mathtt{Extraction}).
	\end{align*}

	The extraction of a minimum element can be done with two methods.
	In the first approach, we use the fact that the Hamming weight of the comparison table row corresponding to the minimum is equal to $N'-1$.
	Hence, we can retrieve the minimum as in~(\ref{eq:sorting_extraction})
	\begin{align}\label{eq:our_minimum}
		\min(A') = \sum_{i=0}^{N'-1} \EQ_{[0,N'-1]}(N'-1,\wt\left(\mL[i]\right)) \cdot a'_i.
	\end{align}
	Here, the multiplicative depth is equal to
	\begin{align*}
		T \cdot d(\min(x,y)) + d(\LT) + \ceil{\log_2 (p-1)} + 1
	\end{align*}
	which is independent of the input array length $N$.
	Since $\EQ_{[0,N'-1]}$ need $\log_2(p-1) + \wt(p-1) - 1$ non-scalar multiplications, then the number of non-scalar multiplications needed to extract the array minimum is equal to
	\begin{align*}
		M(\mathtt{Extraction}) = N' (\log_2(p-1) + \wt(p-1) - 1).
	\end{align*}
	Shaul et al.~\cite{PoPETS:SFR20} proposed another circuit to extract the array minimum that exploits the fact that the comparison table row related to the minimum contains only $1$ except for the main diagonal entry.
	In other words, the product of $\prod_{j=1, j \ne i}^{N'} \mL_{ij} = 1$ if and only if $a'_i = \min(A')$.
	Hence, the minimal element is equal to
	\begin{align}\label{eq:shaul_minimum}
		\min(A') = \sum_{i=0}^{N'-1} a'_i \cdot \prod_{j=1, j \ne i}^{N'} \mL_{ij}.
	\end{align}
	The resulting depth of this circuit amounts to
	\begin{align*}
		T \cdot d\left(\min(x,y)\right) + d(\LT) + \ceil{\log (N'-1)} + 1.
	\end{align*}
	This extraction circuit requires the following number of multiplications
	\begin{align*}
		M(\mathtt{Extraction}) = N' (N'-2).
	\end{align*}
	This implies that for small enough $N'$, Shaul's circuit~(\ref{eq:shaul_minimum}) has a smaller depth or/and a smaller multiplication complexity than the circuit in~(\ref{eq:our_minimum}).
	Furthermore, Shaul's circuit supports any length $N' > p$, whereas (\ref{eq:our_minimum}) requires $N' \le p$ such that $\wt(\mL[i])$ do not overflow modulo $p$.

	In the experiments conducted in Section~\ref{sec:impl-results}, we use the best of these approaches for given $N$, $T$ and $p$.

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


\lab{Pseudospectra}{Pseudospectra}
\label{lab:pseudospectra}
\objective{The Pseudospectrum of a matrix is related to its eigenvalues.  The Pesudospectra of normal and non-normal matrices will be investigated.  We will plot pseudospectra and examine the characteristics of different types of matrices.}

\section*{Pseudospectra Definition}
Eigenvalues of matrices are extremely important, as there are a myriad of practical applications in which they are used.  In Quantum Mechanical systems, particularly, many operators can be represented by matrices, and the eigenvalues are the physical observables of the system.  Though we can make extremely precise measurements, there is often an $\epsilon$-small error in measurements.  In this case, we are interested not just in the eigenvalues, but in the ``almost" eigenvalues, the $\epsilon$ region surrounding the actual eigenvalue.  The $\epsilon$ region around the eigenvalues of a square matrix $A$ represents the $\epsilon$-pseudospectrum (plural \emph{pseudospectra}) of $A$, which can be denoted $\sigma_{\epsilon}(A)$.

To be more precise, the \emph{point spectrum} of $A$ is defined as
\begin{align*}
	\sigma(A)&=\{z \in \mathbb{C}: det(zI-A)=0\}\\
	&=\{z \in \mathbb{C}: (zI-A)^{-1} \text{ is undefined}\}.
\end{align*}
The point spectrum of $A$ contains all the eigenvalues of $A$.  By convention, if $z$ is an eigenvalue of $A$, then $\|(zI-A)^{-1}\|=\infty$.
The $\epsilon$-pseudospectrum of a matrix is a generalization of its point spectrum, and for a matrix $A$ is defined as
\begin{equation}
	\sigma_{\epsilon}(A)=\{z \in \mathbb{C}:\|(zI-A)^{-1}\| > \epsilon^{-1}\}.
\end{equation}
Note that this definition requires the use of a matrix norm. Any matrix norm can be used, but in this lab we will use the norm induced by the 2-norm.

In the case of the 2-norm, the pseudospectrum has an equivalent definition:
\begin{equation}
	\label{equ:spec}
	\sigma_{\epsilon}(A)=\{z \in \mathbb{C}: z \in \sigma(A+E) \text{ for some $E$ with } \|E\|<\epsilon\}.
\end{equation}
When the entries of a matrix are slightly altered, changes in the point spectrum also occur.  The pseudospectrum can then be thought of as the set of eigenvalues that can result from small changes in the entries of a matrix.  If $A$ is an $n \times n$ matrix, then we can use Equation \ref{equ:spec} with some $n \times n$ matrix $E$ satisfying $\lVert E \rVert < \epsilon$ to find the $\epsilon$-pseudospectrum of $A$, which will be a set of all complex numbers $z$ which are in the point spectrum of $A+E$.

One means of plotting the $\epsilon$-pseudospectrum of $A$ is by generating several random matrices $E$ with norm $\epsilon$, and then plotting the eigenvalues $z$ of all of them together.  The horizontal axis is the real part of $z$, with the vertical axis being the imaginary part of $z$.

\begin{figure}
\begin{center}
\begin{subfigure}[b]{.49\textwidth}
\centering
\includegraphics[width=\textwidth]{ps_scatter1}
\end{subfigure}
\begin{subfigure}[b]{.49\textwidth}
\centering
\includegraphics[width=\textwidth]{ps_scatter2}
\end{subfigure}
\caption{Scatter plots representing the $\epsilon$-pseudospectrum of the matrix given in Problem 1. The plot on the left uses $\epsilon=10^{-6}$. The plot on the right uses $\epsilon=10^{-3}$. In both plots, the eigenvalues of twenty matrices $A+E$ were plotted.}
\label{fig:ps_scatter}
\end{center}
\end{figure}

\begin{problem}
Complete the function below for plotting the pseudospectrum of a matrix $A$ by plotting the point spectra of several matrices $A+E$, where $\lVert E \rVert = \epsilon$.

\begin{lstlisting}
def ps_scatter_plot(A, epsilon=.001, num_pts=20):
    '''Plots the 'poorman's pseudospectrum' of a matrix A

    Parameters:
    A : ndarray of size (n,n)
        The matrix whose pseudospectrum is to be plotted.
    epsilon : float
        The norm of the random matrices that are generated.
        Defaults to 10**-3
    num_pts : int
        The number of matrices, E, that will be used in the
        algorithm. Defaults to 20.
    '''
\end{lstlisting}

To get random matrices of the right norm, generate random matrices, divide them by their respective 2-norms, and then multiply by epsilon.
Have your code plot the eigenvalues (point spectrum) of $A$ in one color, and the point spectrum of each $A+E$ in a different color.  The $\epsilon$-pseudospectrum of $A$ can be though of as the region enclosed by your points.  Test your code on the 120x120 matrix:
\begin{equation}
	\begin{pmatrix}
		0  &  i  &  -1  &  0  & \cdots  &  0  \\
		-i &  0  &   i  & -1 & \ddots  &  \vdots  \\
		1  & -i  &   0  &  i &\ddots&  0  \\
		0   & 1 & -i & 0 &\ddots& -1\\
		\vdots   & \ddots    & \ddots & \ddots &\ddots & i\\
		 0  &  \cdots     &   0     &    1  &   -i  & 0
		\end{pmatrix}
\label{prob:pseudoplot}
\end{equation}

Your plot should resemble those in Figure \ref{fig:ps_scatter}.
\end{problem}

This method of plotting, often called the ``poorman's pseudospectrum", can only show the $\epsilon$-pseudospectrum of $A$ for one value of $\epsilon$ at a time, and even then it just provides a bound for the pseudospectrum. In many cases it does not give sufficient information, so we will now explain how to plot the $\epsilon$-pseudospectrum using a more precise algorithm.

\section*{An Equivalent Pseudospectral Definition}

Recall the following definition:

\begin{equation}
\sigma _{\epsilon}(A) = \{ z \in \mathbb{C} \, : \, \lVert (zI_n-A)^{-1} \rVert > \epsilon ^{-1}\}	.
\end{equation}

When we are using the induced 2-norm, we get that $\lVert (zI_n-A)^{-1} \rVert$ is the largest singular value of $(zI_n-A)^{-1}$.  This is also defined for a matrix $B$ as the square root of the largest eigenvalue of $B^{H} B$
 This is equal to $1/s_{min}(zI_n-A)$, where $s_{min}(zI_n-A)$ is the smallest singular of $(zI_n-A)$. Hence, we can write

\begin{equation}
\sigma _{\epsilon}(A) = \{ z \in \mathbb{C} \, | \, s_{min}(A-zI_n) < \epsilon \}.
\end{equation}

\section*{The Lanczos Method}

Using this new definition for $\sigma _{\epsilon}(A)$, we can now plot pseudospectra using Lanczos iterations. Lanczos iterations make use of the Arnoldi method described in Lab \ref{lab:kry_arnoldi} in order to more efficiently compute the smallest eigenvalue of $(zI_n-A)$. This method allows us to plot the pseudospectrum as a contour plot, where each contour corresponds to a fixed value of $\epsilon$. The algorithm is provided below.

\begin{algorithm}
\begin{algorithmic}[1]
\Procedure{Pseudospectrum}{$A, m, epsilon$}
	\State $T \gets schur(A)$			\Comment{Compute the Schur decomposition of A}
	\State $eigsA \gets diagonal(T)$
	\State $xvals,yvals \gets psgrid(eigsA,m)$
	\State $sigmin \gets \zeros{m}{m}$
	\For{$k=0\ldots m-1$}
	    \For{$j=0\ldots m-1$}
		    \State $T_1 \gets (xvals[k]+i*yvals[j])I_n-T$
		    \State \Comment{$I_n$ is the $n\times n$ identity matrix.}
		    \State $T_2 \gets T1^*$        \Comment{Compute the conjugate transpose.}
		    \State $sigold \gets 0$    \Comment{Initialize variables.}
		    \State $qold \gets \zeros{n}{1}$
		    \State $beta \gets 0$
		    \State $H \gets \zeros{n}{n}$
		    \State $q \gets$ random, complex-valued $n \times 1$ array of norm 1
		    \State \Comment{Assume both real and imaginary parts are normally distributed}
		    \For{$p=0 \ldots N-2$}
			    \State $b_1 \gets$ solution to $T_2x = q$
			    \State $b_2 \gets$ solution to $T_1x = b_1$
			    \State $v \gets b_2 - beta*qold$
			    \State $alpha \gets real(q^**v)$
			    \State $v \gets v - alpha*q$
			    \State $beta \gets \norm{v}$
			    \State $qold \gets q$
			    \State $q \gets v/beta$
			    \State $H[p+1,p] \gets beta$
			    \State $H[p,p+1] \gets beta$
			    \State $H[p,p] \gets alpha$
			    %\State $eigsH \gets$ eigenvalues of $H[:p+1,:p+1]$
			    \State $sig \gets$ absolute value of largest eigenvalue of $H[:p+1,:p+1]$
			    \If{$|sigold/sig-1|<0.001$}
			        \State Break    \Comment{End loop if $sig$ is close to $sigold$}
			    \EndIf
			    \State $sigold \gets sig$
			\EndFor
			\State $sigmin[j,k] \gets \sqrt{sig}$
	    \EndFor
	\EndFor
	\State Plot the log of the values in $sigmin$ as a contour plot on the grid determined by $xvals$ and $yvals$.
\EndProcedure
\end{algorithmic}
\caption{The Lanczos Method. This algorithm accepts a $n \times n$ square matrix, $A$; a list of values for $\epsilon$; and an accuracy measure of $m$. It computes the $10^{-\epsilon}$-pseudospectrum of $A$ for each value of $\epsilon$ and produces the contour plot on the grid determined by $xvals$ and $yvals$ as returned by psgrid().}
\label{alg:lanczos_method}
\end{algorithm}

Note that while this algorithm gives better results than our previous method of plotting pseudospectra, it is much slower.

\begin{figure}
\begin{center}
\includegraphics[width=\textwidth]{ps_contour}
\caption{Contour plot of the $\epsilon$-pseudospectrum of the matrix given in Problem 1, with $\epsilon=10^{-2},10^{-3},\cdots,10^{-7}$. }
\label{fig:ps_contour}
\end{center}
\end{figure}

\begin{problem}
Finish the function below for implementing Algorithm \ref{alg:lanczos_method} in Python. Make the function also plot the eigenvalues of the matrix on the same plot.

\begin{lstlisting}
def ps_contour_plot(A, m = 20, epsilon_vals=None):
    '''Plots the pseudospectrum of the matrix A as a contour plot.  Also,
    plots the eigenvalues.

    Parameters:
        A : square, 2D ndarray
            The matrix whose pseudospectrum is to be plotted
        m : int
            accuracy
        epsilon_vals : list of floats
            If k is in epsilon_vals, then the epsilon-pseudospectrum
            is plotted for epsilon=10**-k
            If epsilon_vals=None, the defaults of plt.contour() are used
            instead of any specified values.
     '''

\end{lstlisting}

Test your code on the matrix from Problem 1. Your plot should look like Figure \ref{fig:ps_contour}. You will need to choose a grid on which to plot the values computed in this algorithm. Use a grid that is roughly twice as large as one needed to plot the eigenvalues. To make your code run faster, make sure to use functions designed for Hermitian or triangular matrices whenever possible.
\begin{enumerate}
\item Plot the eigenvalues and contour lines on a complex plane.
\item $m$ defaults to 20 but if you want the plot to look like Figure \ref{fig:ps_contour}, you will need m to be close to 150.  Note that this will take a while longer.
\item In order to plot the contour lines.  Use the following code.
\begin{lstlisting}
plt.contour(xvals,yvals,np.log10(sigmin), levels=epsilon_vals)
\end{lstlisting}
\end{enumerate}

\end{problem}

\section*{Hermitian, Normal, and Nonnormal Matrices}
Recall that a matrix $A$ is Hermitian if it is equal to its conjuage transpose, i.e. $A^{H} = A$.  All the eigenvalues of Hermitian matrices are real.  However, if the elements change slightly (adding $E$), these matrices quickly fall out of the class of Hermitian matrices, and the $\epsilon$-pseudospectra may gain an imaginary component.

A broader class of matrices that includes all Hermitian matrices is the class of \emph{normal matrices}.  A normal matrix commutes with its conjugate transpose, i.e. $A A^{H} = A^{H} A$. In the case that $A$ is normal, it can be shown that the $\epsilon$-pseudospectrum consists of the union of circular sets about each of the eigenvalues of $A$. When $A$ is not a normal matrix, the pseudospectrum can have more varied shapes. Because of this, plotting the pseudospectrum of a nonnormal matrix can provide more information about its behavior.

Figure \ref{fig:ps_normal} provides an example of this. The plots show the pseudospectra of the matrices:

\begin{equation}
	B = \begin{pmatrix}
		-1 & 0 & 0\\
		0 & 1 & 0\\
		0 & 0 & i
	\end{pmatrix}, \qquad 	B' = \begin{pmatrix}
		-1 & -1 & -1\\
		0 & 1 & 1\\
		0 & 0 & i
	\end{pmatrix}.
\end{equation}

Note that these matrices have the same set of eigenvalues, but that while $B$ is normal, $B'$ is not. The plot of the pseudospectrum of $B$ shows the expected circular regions, but the plot for $B'$ lacks such symmetry. Another example of this can be seen in Figure \ref{fig:ps_scatter}. The pseudospectrum does not expand out from all of the eigenvalues evenly. Most of the region contained in the pseudospectrum lies on the sides of the plot, and not close to the center.\\

Because the pseudospectra of normal matrices behave so consistently, the study of pseudospectra focuses on nonnormal matrices. Pseudospectra can also be used to study infinite-dimensional linear operators, but different methods are required for plotting their pseudospectra.

\begin{figure}
\begin{center}
\begin{subfigure}[b]{.49\textwidth}
\centering
\includegraphics[width=\textwidth]{ps_normal}
\end{subfigure}
\begin{subfigure}[b]{.49\textwidth}
\centering
\includegraphics[width=\textwidth]{ps_nonnormal}
\end{subfigure}
\caption{Plots of the Pseudospectrum of two matrices with the same eigenvalues. The plot on the left is for a normal matrix, and the one on the right is for a nonnormal matrix. The contours correspond to $\epsilon=10^{-0.4},10^{-0.8},10^{-1.2}$}
\label{fig:ps_normal}
\end{center}
\end{figure}

\begin{problem}
Using your function from problem \ref{prob:pseudoplot}, investigate how the pseudospectra of Nonnormal, Hermitian, and Normal matrices differ.  To do so, use the matrix given in problem \ref{prob:pseudoplot} as the Nonnormal matrix.

For the Hermitian matrix, change the subdiagonal of $1$s to be $-1$.

For the Normal matrix, reset all of the subdiagonal entries to be zero (change the $1$ diagonal and $-i$ diagonal to zeros).  Set all of the diagonal entries to be random complex values (so as to get different eigenvalues).

Plot your results.
\end{problem}

% ~\cite{Tufte2005}


\subsection{Strain Homogenization}

The derivation for the homogenized strain tensor, $\left< \boldsymbol{\epsilon}\right>$, begins in a similar manner to the homogenized stress tensor derivation with the familiar definition of the spatial average:

\begin{equation}
\langle\boldsymbol{\epsilon}\rangle=\frac{1}{A^{h}}\int_{\Omega^{h}}\boldsymbol{\epsilon}^m dA\label{eqn:strain2}
\end{equation}

At this point, it becomes convenient to assume a small displacement formulation of strain. This displacement assumption limits the applicability of the strain homogenization, but in the context of large scale geomechanics, this assumption remains reasonable. As such, the linear infinitesimal strain tensor can be written in terms of the displacement vector, $\mathbf{u}^m$:

\begin{equation}
\boldsymbol{\epsilon}^m=\frac{1}{2}\left[\nabla\mathbf{u}^m+\left(\nabla^\top \mathbf{u}^m\right)\right]\label{eqn:strain1}
\end{equation}

The above integral can be converted to the following boundary integral using the divergence theorem:

\begin{equation}
\langle\boldsymbol{\epsilon}\rangle=\frac{1}{2A^{h}}\oint_{\Gamma^{h}}\left[\mathbf{u}^m\otimes\mathbf{n}+\mathbf{n}\otimes\mathbf{u}^m\right]d\Gamma\label{eqn:strain5-1}
\end{equation}

where $\mathbf{n}$ is the outward pointing normal to $\Gamma_h$.

When $\Gamma_h$ is defined by a set of line segments over which the displacement is also linear, the boundary integral can be rewritten as a summation over each of the $N$ boundary segments. Let $\bar{\mathbf{u}}^m_{I}$ denote the average displacement along the $I^{th}$ boundary segment of the homogenization boundary, which is calculated as the average of the two nodal displacements defining the boundary of each segment. Let $\mathbf{n}_{I}$ represent the outward pointing normal to the $I^{th}$ boundary segment on the homogenization boundary.  Let the length of boundary segment $I$ be denoted by $L_{I}$. The homogenized strain can be rewritten as

\begin{equation}
\langle\boldsymbol{\epsilon}\rangle=\frac{1}{2A^{h}}\sum_{I=1}^{N}\left[\bar{\mathbf{u}}^m_{I}\otimes\mathbf{n}_{I}+\mathbf{n}_{I}\otimes\bar{\mathbf{u}}^m_{I}\right]L_{I}\label{eqn:strain7}
\end{equation}