%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Problem Set/Assignment Template to be used by the
%% Food and Resource Economics Department - IFAS
%% University of Florida's graduates.
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Version 1.0 - November 2019
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Ariel Soto-Caro
%%  - asotocaro@ufl.edu
%%  - arielsotocaro@gmail.com
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Numbered environment

\documentclass[12pt]{article}
\usepackage{design_ASC}
\theoremstyle{definition}
\newtheorem{exmp}{Example}[section]
\newtheorem{slo}{Solution}[section]
\newcommand*{\Perm}[2]{{}^{#1}\!P_{#2}}%
\newcommand*{\Comb}[2]{{}^{#1}C_{#2}}%
%% -----------------------------
%% TITLE
%% -----------------------------
\title{\textbf{Lecture 3}} %% Assignment Title

\author{\textbf{Ibrahim} Abou Elenein}

\date{\today} %% Change "\today" by another date manually
%% -----------------------------
%% -----------------------------

%% %%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\setlength{\droptitle}{-5em}
%% %%%%%%%%%%%%%%%%%%%%%%%%%
\maketitle
% --------------------------
% Start here
% --------------------------

% %%%%%%%%%%%%%%%%%%%
\section{Sample Spaces}
The set of all possible outcomes of a random
experiment is called a \textit{sample space}
\section{Events}
An event E, is a set of some outcomes of a
probability experiment, i.e. a subset of the sample
\[
    E \subseteq S
\]
If an event E contains no outcomes, then E is an impossible event.

\section{Probability}
\[
    P(E) = \frac{N(E)}{N(S)}
\]
\[
    0 \leq P (E) \leq 1  ;  \ P(S) = 1
\]
\begin{exmp}
    A card is drawn from a standard deck.
    Find the probabilities of the following events.
    \begin{itemize}
        \item Getting a queen.
        \item Getting a club.
        \item Getting a number.
    \end{itemize}

\end{exmp}
\begin{slo}
    \[
        |S| = 52
    \]
    E1 is the event of getting a queen,
    $\displaystyle P(E1) = \frac{|E1|}{|S|} = \frac{4}{52}=  \frac{1}{13}$
    \\
    E2 is the event of getting a club,
    $\displaystyle P(E2) = \frac{|E2|}{|S|} = \frac{13}{52}=  \frac{1}{4}$ \\
    E3 is the event of getting a number,
    $ \displaystyle P(E3) = \frac{|E3|}{|S|} = \frac{40}{52}=  \frac{10}{13}$ \\

\end{slo}

\section{Counting Rules}
\subsection{Multiplication Rule}
In a sequence of two experiments, if the
first experiment can occur in $m$ different
ways and the second one can occur in $n$
different ways, then the whole sequence
can occur in $m \times n$  different ways.
\begin{exmp}

    Consider the manufacturing of:
    number-plates consisting of two letters followed by four digits
    \begin{enumerate}
        \item  How many plates are possible?
            \[
                26 \times 26 \times 10 \times 10 \times 10 \times 10 = 6760000
            \]
        \item How many plates are possible, if no letter or digit can be repeated?
            \[
                26 \times 25 \times 10 \times 9 \times 8 \times 7 = 3276000
            \]
    \end{enumerate}

\end{exmp}

\begin{exmp}
    In a class of 10 students, 6 are to be chosen and seated in a row for a
    picture.
    How many different pictures are possible?
    \[
        10 \times 9 \times 8 \times 7 \times 6 \times 5 \ different \  pictures.
    \]
    \textbf{Tip} what if they were to sit in a circle?
\end{exmp}

\subsection{Permutaion}
An arrangement of $n$ objects \textsc{ a specific order} using $k$ objects at a time is
called a permutation and it is denoted by $\Perm{n}{k}$.
The number of repetition-free
permutations (linear arrangements) of size $k$ from a set of $n$ distinct objects
is given by:
\[
    \Perm{n}{k} = \frac{n!}{(n - k)!}; \ \ 0 \leq k \leq n
\]

\subsection{Combination}
A selection of $k$ distinct objects \textsc{without regard to order} out of $n$
objects is called a combination and it is denoted by $\Comb{n}{k} $
The number of repetition-free combinations of size k from n distinct objects is given:
\[
    \Comb{n}{k} = \frac{\Perm{n}{k}}{k!} = \frac{n!}{(n - k)!k!}; \ \  0 \leq k \leq n
\]
\subsection{Problems}
\begin{exmp}

\end{exmp}
\begin{exmp}
    In a class of 10 students, three are to be chosen to represent the class in a
    competition.  How many selections are possible? \\

    Note that, here, the students are not selected in any
    specific order.

    The number of selections is $\Comb{10}{3} $

\end{exmp}

\begin{exmp}
    A student is taking a Math-401 test in which 7 questions out of 10 are to be
    answered. In how many ways can the student answer the exam if :
\end{exmp}
\begin{enumerate}
    \item Any 7 questions may be selected.
        \begin{center}
            The student can answer the exam in $\Comb{10}{7}$
        \end{center}
    \item The first 2 questions must be selected.
        \begin{center}
            The student can answer the exam in $\Comb{8}{5}$
        \end{center}
    \item The student must choose 3 questions from the first 5 and 4 questions from the
        \begin{center}
            The student can answer the exam in $\Comb{5}{3} \times \Comb{5}{4} = 50$
        \end{center}
\end{enumerate}

\begin{exmp}
    What is the number of (possibly meaningless) words that are made up of all the
    letters in the word \textbf{chemistry}
\end{exmp}
\begin{center}
    The number of such words id $\Perm{9}{9}$
\end{center}

\begin{exmp}
    What is the number of permutations of the letters in the word \textbf{ball}?
    \begin{center}
        Note that there are repeated letter so it's not 4 distinct objects.
    \end{center}
    \begin{center}
        The number of permutations is $\displaystyle  \frac{\Perm{4}{4}}{2!} = 12$
    \end{center}
\end{exmp}

\begin{exmp}
    What is the number of permutations of the letters in the word \textbf{Pepper}?

    \begin{center}
        The number of permutations is $\displaystyle  \frac{\Perm{6}{6}}{2!3!}$
    \end{center}
\end{exmp}

\begin{exmp}
    The owner of a pizzeria prepares every pizza by always combining 4 different
    ingredients. How many ingredients does he need, at least, if he would like to
    offer 30 different pizzas in the menu?
    \begin{center}
        we need to find $n$ such that $\Comb{n}{4} = 30$
    \end{center}
    \begin{center}
        $\displaystyle \frac{n!}{(n-4)!4!} = 30$ \\
    \end{center}
    \begin{center}
        $\displaystyle \frac{n(n-1)(n-2)(n-3)(n-4)!}{(n-4)!} = 30 \times 4!$
    \end{center}
    \begin{center}
        $\displaystyle n(n-1)(n-2)(n-3) - 720 = 0$ \\

        By solving the equation $n = 6.8 \Rightarrow $he needs at least 7 ingredients
    \end{center}

\end{exmp}

\begin{exmp}
    You play a simple card game. You draw 3 cards. If any of them is a King or
    the three cards are of the same suit, you win otherwise you lose.
    How many hands are you winning?
    \begin{center}
        A = draw at least one king in a hand of 3 cards \\
        N(A) = One King or Two or Three = \\
        $ (\Comb{4}{1} \times \Comb{48}{2})+ (\Comb{4}{2} \times \Comb{48}{1})
        + (\Comb{4}{3} \times \Comb{48}{0})
        $
    \end{center}
    \begin{center}
        B = A hand draw 3 cards with  same suit \\
        $N(B) = \Perm{4}{1} \times \Perm{13}{3}$ \\
    \end{center}
    \begin{center}
        $A \cap B$ = a hand with draw 3 cards with a king and the same suit.
        $N(A \cap B) = \Comb{4}{1} \times \Comb{1}{1} \Comb{12}{2}  $
    \end{center}
    \begin{center}
        $N(A \cup  B) = N(A) + N(B) - N(A \cap B) = 5684.$
    \end{center}
\end{exmp}

\begin{exmp}
    In how many ways you can arrange the word “ORANGE”, if:
    \begin{itemize}
        \item 2 vowels and 2 consonants are used to make 4-letter words.
            \begin{center}
                No.of ways = $\Comb{3}{2} \times \Comb{3}{2} \times 4! $
            \end{center}
        \item 2 vowels and 3 consonants are used to make 5-letter words.
            \begin{center}
                No.of ways = $\Comb{3}{2} \times \Comb{3}{3} \times 5! $
            \end{center}
        \end{itemize}
\end{exmp}
\begin{exmp}
    There are 8 men and 9 women on a committee selection pool.
    A committee consisting of:
    a president, a vice-president, and 3 coordinators; is to
    be formed.
    In how many ways can exactly three women be on the committee?
    \begin{center}
        $\displaystyle N = \Comb{9}{3} \times \Comb{8}{2}  \times [\frac{5!}{3!}]$
    \end{center}

\end{exmp}
\end{document}


\subsection{Cauchy's integral formula}


    \filetitle{addparam}{Add VAR parameters to a database (struct)}{VAR/addparam}

	\paragraph{Syntax}\label{syntax}

\begin{verbatim}
D = addparam(V,D)
\end{verbatim}

\paragraph{Input arguments}\label{input-arguments}

\begin{itemize}
\item
  \texttt{V} {[} VAR {]} - VAR object whose parameter matrices will be
  added to database (struct) \texttt{D}.
\item
  \texttt{D} {[} struct {]} - Database to which the model parameters
  will be added.
\end{itemize}

\paragraph{Output arguments}\label{output-arguments}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  `D {[} struct {]} - Database with the VAR parameter matrices added.
\end{itemize}

\paragraph{Description}\label{description}

The newly created database entries are named \texttt{A\_} (transition
matrix), \texttt{K\_} (constant terms), \texttt{J\_} (coefficient matrix
in front of exogenous inputs), \texttt{B\_} (matrix of instantaneous
whock effects), and \texttt{Cov\_} (covariance matrix of shocks). Be
aware that all existing database entries in \texttt{D} named
\texttt{A\_}, \texttt{K\_}, \texttt{B\_}, or \texttt{Omg\_} will be
overwritten.

\paragraph{Example}\label{example}

\begin{verbatim}
D = struct();
D = addparam(V,D);
\end{verbatim}


\documentclass[12pt,a4paper]{article}
\usepackage{algorithm, algpseudocode, amsmath, amssymb, caption, csquotes, empheq, geometry, graphicx, hyperref, listings, multirow, physics, siunitx, subcaption, upgreek}
\usepackage[section]{placeins}

\title{Computational Physics\\Problem Set 3}
\author{Saleh Shamloo Ahmadi\\Student Number: 98100872}
\date{October 17, 2021}

\hypersetup{colorlinks=true, urlcolor=cyan}

\newcommand{\bdfig}{../fig/ballistic-deposition}
\newcommand{\pfig}{../fig/percolation}

\begin{document}
	\maketitle
    \section{Ballistic Deposition}
    Ballistic deposition models add particles randomly to a surface and in the case of non-random
    ballistic deposition models, apply specific rules after each \enquote{deposition}.

    In general, we can approximately describe the growth of each model with the constants $\alpha$, $\beta$,
    and $z$ (not independent); If there is any interaction between neighboring points, the roughness of the
    surface (defined as the standard deviation of heights in one-dimensional systems) will approach an upper limit
    $w_s$ ($w(t)$ is the roughness) at \emph{time of saturation} $t_s$. Then
    \begin{gather}
        w(t) \sim t^\beta (\text{before saturation}), \\
        t_s \sim L^z, \quad w_s \sim L^\alpha \sim t_s^\beta \sim L^{z\beta},
    \end{gather}
    so we should have $\alpha = z\beta$. Note that depending on the method used to determine saturation time, $\beta$
    before saturation can be different form $\beta$ obtained from saturation.

    \subsection{Ballistic Deposition with Relaxation}
    In this model, after each deposition, the particle is dropped onto a neighboring point if it is at a lower height
    (less particles have been deposited on that point). If more than one neighbor has a lower height, the particle is
    dropped onto the point with the lowest height.

    The faint \enquote{bands} in each plot shows the standard deviation of each data point taken over all runs.
    \newgeometry{top=0.1in, bottom=1in}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\bdfig/bd-relax-vis}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\bdfig/bd-relax-mean}
        \caption{$\text{slope}=0.005\pm\num{6e-20}$}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\bdfig/bd-relax-roughness-200}
        \caption{$\beta=0.224\pm0.001$}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\bdfig/bd-relax-roughness-100}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\bdfig/bd-relax-roughness-50}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\bdfig/bd-relax-roughness-25}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\bdfig/bd-relax-roughness-12}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\bdfig/bd-relax-sat-alpha}
        \caption{$\alpha = 0.500\pm0.003$}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\bdfig/bd-relax-sat-z}
        \caption{$z = 3.02\pm0.04$}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\bdfig/bd-relax-sat-beta}
        \caption{$\beta = 0.165\pm0.002$}
    \end{figure}
    \restoregeometry
    \begin{table}[htb!]
        \centering
        \caption{Ballistic Deposition with Relaxation}
        \begin{tabular}{|c|c|c|}
            \hline
            $L$ & $t_s$ & $w_s$ \\
            \hline
            12 & 108 & 0.759364 \\
            \hline
            25 & 837 & 1.07834 \\
            \hline
            50 & 8550 & 1.53467 \\
            \hline
            100 & 63042 & 2.18055 \\
            \hline
            200 & 502161 & 3.08905 \\
            \hline
        \end{tabular}
    \end{table}
    I calculated the time of saturation by fitting a line through the trailing points with constant $w(t)$.
    This method is not optimal. It would be better to find the intersection of the linear fit and constant fit lines.
    Due to a lack of time, I am unfortunately stuck with this (this was the first method I tried and there was no time
    left before the deadline to improve my methodology).

    As you can see, this description of the model is not perfect, since $\beta$ is not the same as $L$ changes.
    \subsection{Ballistic Deposition (regular)}
    In this model, particles stick to the first point they come in contact with. This means each particle will be stuck
    at the maximum heigh in its dropping neighborhood
    ($\text{max}\{h_{\text{left}}, h_{\text{drop}} + 1, h_{\text{right}}\}$).

    Results have similar characteristics to the last model. Notably, $\beta$ obtained from saturation is equal
    within the margin of error and $\beta$ obtained from ftting to data is also very close. $\alpha$ and $z$ are
    smaller, since correlation is stronger in this model (height of each point is directly related to the neighboring
    heights, instead of being indirectly affected by them through the distribution of heights).
    \begin{table}[hbt!]
        \centering
        \caption{Ballistic Deposition (regular)}
        \begin{tabular}{|c|c|c|}
            \hline
            $L$ & $t_s$ & $w_s$ \\
            \hline
            12 & 107 & 2.42859 \\
            \hline
            25 & 406 & 2.84437 \\
            \hline
            50 & 2715 & 4.20796 \\
            \hline
            100 & 12386 & 4.94841 \\
            \hline
            200 & 65003 & 6.75316 \\
            \hline
        \end{tabular}
    \end{table}
    \newgeometry{top=0.1in, bottom=1in}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\bdfig/bd-vis}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\bdfig/bd-mean}
        \caption{$\text{slope}=\num{1.0698e-2}\pm\num{9e-6}$}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\bdfig/bd-roughness-200}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\bdfig/bd-roughness-100}
        \caption{$\beta=0.254\pm0.003$}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\bdfig/bd-roughness-50}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\bdfig/bd-roughness-25}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\bdfig/bd-roughness-12}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\bdfig/bd-sat-alpha}
        \caption{$\alpha = 0.37\pm0.03$}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\bdfig/bd-sat-z}
        \caption{$z = 2.31\pm0.07$}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\bdfig/bd-sat-beta}
        \caption{$\beta = 0.161\pm0.008$}
    \end{figure}
    \restoregeometry
    \subsubsection{Correlation Length}
    In the ballistic deposition model, each point can have long range effect on further points through clustering with
    its neighbors, which in turn cluster with their own neighbors, and so on. To find the range of interaction, or
    \emph{the correlation length}, we can isolate the particles that stem from a single point
    (the \enquote{tree} attached to a \enquote{seed}).
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\bdfig/bd-iso-vis}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\bdfig/bd-correlation}
        \caption{$\text{slope} = 0.522\pm0.002$.\\The slope is the growth exponent for of the correlation length.}
    \end{figure}
    \section{Percolation}
    \subsection{Depth-First Search}
    Using a modified version of the depth-first search algorithm (a.k.a. DFS) that is designed and optimized for grids,
    percolation problems can be solved efficiently (time complexity $\mathcal{O}(L^d)$, where L is the lenght
    of the grid and d is the number of dimentions). Algorithm \ref{alg:dfs} outlines the implementation.
    \begin{algorithm}
        \caption{DFS for solving a percolation problem}
        \label{alg:dfs}
        \begin{algorithmic}[1]
            \Function{DFS}{$G$}
            \parbox[t]{0.75\linewidth}{\Comment{G is a boolean graph representing the empty/full states
            of each cell of the grid}}
                \State make stack $S$
                \ForAll{vertices $v$ \textbf{in} the starting row/column of the grid}
                    \If{$v$ \textbf{is} \textit{true}}
                        \State $S.push(v)$
                        \While{$S$ is not empty}
                            \State v = $S.pop()$
                            \If{$v$ is in the final row/column}
                                \State \textbf{return} \textit{true}
                            \ElsIf{$v$ is not already visited}
                                \State mark $v$ as visited
                                \ForAll{$w$ \textbf{in} $G.neighbors(v)$}
                                \State $S.push(w)$
                                \parbox[t]{0.6\linewidth}{\Comment{Prioritize the neighbor in the direction of
                                the percolation's destination to optimize the performance for emptier grids}}
                                \EndFor
                            \EndIf
                        \EndWhile
                    \EndIf
                \EndFor
                \State \textbf{return} \textit{false}
            \EndFunction
        \end{algorithmic}
    \end{algorithm}
    \newgeometry{left=0in, right=0in}
    \begin{figure}
        \centering
        \begin{subfigure}{0.45\linewidth}
            \centering
            \includegraphics[width=\linewidth]{\pfig/dfs-55}
        \end{subfigure}
        \begin{subfigure}{0.45\linewidth}
            \centering
            \includegraphics[width=\linewidth]{\pfig/dfs-60}
        \end{subfigure}
        \begin{subfigure}{0.45\linewidth}
            \centering
            \includegraphics[width=\linewidth]{\pfig/dfs-58}
        \end{subfigure}
    \end{figure}
    \restoregeometry
    \subsection{Coloring}
    We can use (a very inefficient\footnote{$\mathcal{O}(L^{2d})$ time complexity}) coloring algorithm, as described in
    the lecture notes, to label the clusters in the grid. Then, If a cluster connecting the two ends of the grid exists,
    percolation is possible.
    \begin{figure}
        \centering
        \begin{subfigure}{0.45\linewidth}
            \centering
            \includegraphics[width=\linewidth]{\pfig/color-45}
        \end{subfigure}
        \begin{subfigure}{0.45\linewidth}
            \centering
            \includegraphics[width=\linewidth]{\pfig/color-50}
        \end{subfigure}
        \begin{subfigure}{0.45\linewidth}
            \centering
            \includegraphics[width=\linewidth]{\pfig/color-55}
        \end{subfigure}
        \begin{subfigure}{0.45\linewidth}
            \centering
            \includegraphics[width=\linewidth]{\pfig/color-60}
        \end{subfigure}
    \end{figure}
    \subsection{Hoshen--Kopelman}
    This is a much more efficient $\mathcal{O}(L^d)$ algorithm for labeling the clusters. You can read more about it
    on the \href{https://en.wikipedia.org/wiki/Hoshen%E2%80%93Kopelman_algorithm}{Wikipedia page}.
    \newgeometry{top=0.3in, bottom=0.3in, left=1in, right=1in}
    \thispagestyle{empty}
    \begin{figure}
        \centering
        \begin{subfigure}{\linewidth}
            \centering
            \includegraphics[width=\linewidth]{\pfig/hk-50}
        \end{subfigure}
        \begin{subfigure}{\linewidth}
            \centering
            \includegraphics[width=\linewidth]{\pfig/hk-55}
        \end{subfigure}
        \begin{subfigure}{\linewidth}
            \centering
            \includegraphics[width=\linewidth]{\pfig/hk-60}
        \end{subfigure}
    \end{figure}
    \restoregeometry
    \subsubsection{Percolation Probability}
    The probablity of there existing a path from the top to the bottom of a lattice (denoted by $Q$) exhibits critical
    behavior; If $p$ is the probablity of forming bonds (or sites) in the lattice, then is a sudden jump from 0 to 1
    probablity at a critical probablity $p_c$. In an infinite lattice, the path connecting the top and bottom of
    the lattice is called the \emph{infinite open cluster}.

    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\pfig/percolate-full-10}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\pfig/percolate-full-100}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\pfig/percolate-full-200}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\pfig/percolate-zoom-10}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\pfig/percolate-zoom-100}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\pfig/percolate-zoom-200}
    \end{figure}

    As you can see, the transition from 0 to 1 probablity at $p_c$ becomes sharper as the side-length of the lattice
    increases.

    We use $Q_\infty$ to denote the probablity of a cell being a part of an open cluster connecting the top and bottom
    of the lattice. To calculate $Q_\infty$, we can modify the Hoshen--Kopelman algorithm to record cluster sizes for
    every label (that is, the \enquote{root} labels, or the final label that the cluster is connected to, such that
    $\mathrm{Label(number) = number}$). Then, $Q_\infty$ will be the sum of the sizes of the open clusters devided by
    the surface area of the lattice.

    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\pfig/percolate-qinfty-10}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\pfig/percolate-qinfty-100}
    \end{figure}

    As you can see, $Q_\infty$ exhibits the same critical behavior as $Q$.
\end{document}


\input{../../utils/header.tex}

\begin{document}

\title{Machine Learning (41204-01)\\HW \#7}
\author{Will Clark and Matthew DeLio \\
\textsf{\{will.clark,mdelio\}@chicagobooth.edu} \\
University of Chicago Booth School of Business}
\date{\today}
\maketitle

\section{Zachary's Karate Club}

Zachary's Karate Club is a network describing a university karate club from the early 1970's. The network has 34 vertices and 78 edges as depicted in \cref{fig:karate_network}. The network was observed and described by Wayne W. Zachary during and after it split into two factions (led by John A and Mr. Hi, denoted with blue circles and red squares, respectively). Because we know the features of the network and the factions into which it split, it is an ideal data set on which to test out various community detection algorithms.

\begin{figure}[!htb]
\centering
\caption{Zachary's Karate Club and Factions}
\includegraphics[scale=.5,trim={0.75in 0.75in 0.75in 0.75in}, clip=True]{karate_network.pdf}
\label{fig:karate_network}
\end{figure}

We used the algorithms listed below to try and determine the underlying community structure in Zachary's karate club. The results are visualized in \cref{fig:edge_betweenness}-\cref{fig:walktrap}. The hierarchical algorithm can be cut into two groups and compared directly to the ground truth of the observed factions, but algorithms with more groups are tougher to evaluate. If an algorithm produces more than two groups, as long as the groups are contained entirely within one faction or the other, we consider there to be no mis-classifications. In the associated figures, members of Mr. H's faction are in squares and members of John A's faction are in circles; the colors are set according to algorithmically-determined groups.
\begin{itemize}
\item \textbf{Edge Betweenness}: A hierarchical algorithm that we cut to obtain two groups. The default setting is for the algorithm to consider the network edge weights, but in doing so the algorithm misclassifies two vertices (Actors 3 and 14; see \cref{fig:edge_betweenness}). By ignoring the edge weights, the algorithm only mis-classifies one vertex (Actor 3).
\item \textbf{Greedy Modularity Optimization (Fast Greedy)}: A hierarchical algorithm that we cut to obtain two groups. It correctly predicts the faction for all vertices (see \cref{fig:fast_greedy}).
\item \textbf{Infomap}: A non-hierarchical algorithm that splits the data into 3 groups. Two groups are made entirely of members from Mr. Hi's faction (see \cref{fig:infomap}).
\item \textbf{Propagating Labels}: A non-hierarchical algorithm that splits the data into 3 groups. Two groups are made entirely of members from Mr. Hi's faction. It classifies the networks into the same groups as the Infomap algorithm (see \cref{fig:label_prop}).
\item \textbf{Leading Eigenvector}: This is another hierarchical algorithm, although the basic \textsf{cutat} function produces an error when we attempt to break the network into two communities.\footnote{\textsf{Warning message: In cutat(cl, 2) : Cannot have that few communities}} As an alternative, we visualize the network as a dendrogram and use the two largest branches as our estimate of the factions. The restuls are not good; five actors in each group are mis-categorized. The results are in \cref{fig:leading_eigen}. This is far and away the worst algorithm for this data set, although it may be because of the implementation in \textsf{igraph}.
\item \textbf{Multi-level Modularity Optimization (Louvain)}: A non-hierarchical algorithm that splits the data into 4 groups. Two groups are made entirely of members from Mr. Hi's faction and two groups are made entirely of members from John A's faction. This algorithm breaks Mr. Hi's faction into the same groups that the Infomap and Propagating Labels algorithms do (see \cref{fig:louvain}).
\item \textbf{Optimal Structure}: A non-hierarchical algorithm that splits the data into 4 groups. The four groups are identical to those identified by the Multi-Level Modularity Optimization algorithm above (see \cref{fig:optimal}).
\item \textbf{Statistical Mechanics (Spinglass)}: A non-hierarchical algorithm that splits the data into 4 groups. The groups are nearly the same as those identified in the Optimal Structure and Multi-Level Modularity Optimation algorithms, except Actor 24 has switched groups within John A's faction (see \cref{fig:spinglass}).
\item \textbf{Short Random Walks (Walktrap)}: A hierarchical algorithm that we cut to obtain two groups. It correctly predicts the faction for all vertices (see \cref{fig:walktrap}).
\end{itemize}
Ultimately, all algorithms besides the Edge Betweenness and Leading Eigenvector algorithm correctly break the network into the correct groups, either the observed factions or subsets of the observed factions.

%%%%%%%%%%%%%%%%%%%% KARATE COMMUNITY GRAPHS %%%%%%%%%%%%%%%%%%%%
\begin{figure}
\centering
\begin{subfigure}[b]{0.32\textwidth}
\caption{Edge Betweenness}
\includegraphics[width=\textwidth,trim={0.75in 0.75in 0.75in 0.75in}, clip=True]{edge_betweenness.pdf}
\label{fig:edge_betweenness}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.32\textwidth}
\caption{Greedy Optimization}
\includegraphics[width=\textwidth,trim={0.75in 0.75in 0.75in 0.75in}, clip=True]{fast_greedy.pdf}
\label{fig:fast_greedy}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.32\textwidth}
\caption{Infomap}
\includegraphics[width=\textwidth,trim={0.75in 0.75in 0.75in 0.75in}, clip=True]{infomap.pdf}
\label{fig:infomap}
\end{subfigure}

\begin{subfigure}[b]{0.32\textwidth}
\caption{Propagating Labels}
\includegraphics[width=\textwidth,trim={0.75in 0.75in 0.75in 0.75in}, clip=True]{label_prop.pdf}
\label{fig:label_prop}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.32\textwidth}
\caption{Leading Eigenvector}
\includegraphics[width=\textwidth,trim={0.75in 0.75in 0.75in 0.75in}, clip=True]{leading_eigen.pdf}
\label{fig:leading_eigen}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.32\textwidth}
\caption{Multi-level Optimization}
\includegraphics[width=\textwidth,trim={0.75in 0.75in 0.75in 0.75in}, clip=True]{louvain.pdf}
\label{fig:louvain}
\end{subfigure}

\begin{subfigure}[b]{0.32\textwidth}
\caption{Optimal Structure}
\includegraphics[width=\textwidth,trim={0.75in 0.75in 0.75in 0.75in}, clip=True]{optimal.pdf}
\label{fig:optimal}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.32\textwidth}
\caption{Statistical Mechanics}
\includegraphics[width=\textwidth,trim={0.75in 0.75in 0.75in 0.75in}, clip=True]{spinglass.pdf}
\label{fig:spinglass}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.32\textwidth}
\caption{Short Random Walks}
\includegraphics[width=\textwidth,trim={0.75in 0.75in 0.75in 0.75in}, clip=True]{walktrap.pdf}
\label{fig:walktrap}
\end{subfigure}
\caption{Community Detection Algorithms for Zachary's Karate Club}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Wikipedia}
\subsection{Clustering}
Due to the size of and the time required to cluster our data-set, we apply only one of the better performing algorithms from the previous section.  Of two that correctly partitioned the Karate data in the previous section (\textbf{Greedy} and \textbf{Short Random Walk}) we choose to apply \textbf{Short Random Walk} here.

Before looking at the results of the clustering algorithm, we first look at node connectedness to see if we can learn anything we can learn from these data.  \Cref{fig:wiki_conn} shows that many articles have a few connections and few nodes have many connections with a, roughly, linear decline between these two extremes in log-log space.  Because of the large number of poorly connected articles, we'd expect the clustering algorithm to generate a large number of small clusters as it struggles to find common groups for these articles.  Likewise we expect that the well-connected clusters should be easily identifiable and clustered together.  \Cref{fig:wiki_size} shows a histogram of the cluster-size; as we predict we do see a large number of small clusters.

\begin{figure}[!htb]
  \centering
  \begin{subfigure}[b]{0.49\textwidth}
    \caption{Vertex Connectedness}
    \includegraphics[width=\textwidth]{wiki_edge_hist.pdf}
    \label{fig:wiki_conn}
  \end{subfigure}
  \hfill
  \begin{subfigure}[b]{0.49\textwidth}
    \caption{Cluster-Size}
    \includegraphics[width=\textwidth]{wiki_cl_hist.pdf}
    \label{fig:wiki_size}
  \end{subfigure}
  \caption{Cluster and Node Histograms}
\end{figure}

We examine the largest 4 clusters to see how well a sample of the topics (see \cref{tab:wiki_topics}).  Very roughly, it appears that the topics are clusters as follows:
\begin{itemize}
\item \textbf{\#5} Mathematics (Number Theory)
\item \textbf{\#11} Physics / Politics - This cluster appears to be fairly poorly formed
\item \textbf{\#19} Chemistry
\item \textbf{\#20} Telecommunications
\end{itemize}

While not perfect, it appears as though the random walk clustering algorithm does a decent job finding relationships between articles within the graph network.  While difficult to visualize, \cref{fig:wiki_clust} attempts to show the largest 8 clusters along with a couple of the most connected-topics selected from each.\footnote{This was done in an attempt to separate, spatially, the article labels}

\begin{figure}[!htb]
  \centering
  \caption{Visual Representation of the Largest 8 Clusters}
  \includegraphics[width=\textwidth]{wiki_clust.pdf}
  \label{fig:wiki_clust}
\end{figure}

\input{wiki_topics.tex}

\begin{appendices}

\end{appendices}

\end{document}

% \input{.tex}

% \begin{figure}[!htb]
%   \centering
%   \begin{subfigure}[b]{0.49\textwidth}
%     \caption{}
%     \includegraphics[width=\textwidth]{.pdf}
%     \label{fig:}
%   \end{subfigure}
%   \hfill
%   \begin{subfigure}[b]{0.49\textwidth}
%     \caption{}
%     \includegraphics[width=\textwidth]{.pdf}
%     \label{fig:}
%   \end{subfigure}
%   \caption{}
% \end{figure}

% \begin{figure}[!htb]
%   \centering
%   \caption{}
%   \includegraphics[scale=.5]{.pdf}
%   \label{fig:}
% \end{figure}


\newpage{}

\hypertarget{quality-assurance}{%
\section{Quality Assurance}\label{quality-assurance}}

Now if we create algorithms we need to say how good they are. For that
we are using the following methods.

\hypertarget{the-graphical-approach}{%
\subsection{The graphical approach}\label{the-graphical-approach}}

One easy way would be to draw a graph which shows the predictions and
the real values for time spent, while each task is understood as a
category of its own.

We sort that graph by actual duration so we should see the distribution
of durations and around that a hopping range of dots that describes what
the algorithm tells us.

A better algorithm should be closer to the real data. Any algorithm
should never match perfectly, as then we would have a 1:1 mapping. And
that is an over-fit for sure.

\hypertarget{mean-squared-error}{%
\subsection{Mean squared error}\label{mean-squared-error}}

The mean squared error is a common approach to calculate a value for the
quality of an algorithmi. It gets bigger with every estimate we did
wrong.

The formula can be described as:

For every value you predict:

\begin{itemize}
\tightlist
\item
  Calculate the difference between the predicted value and the real
  value
\item
  Sum the squares of each difference
\item
  Divide all the sum by the count of the data entries you check
\end{itemize}

Now, since we want to prevent overfitting we need to prevent
underfitting as well. Since we are talking about seconds and most of the
recorded tasks have a duration in the range of up to 50000 seconds that
means that most tasks are completed in about 13,89 hours. So what about
an error margin of about 5 hours. Which means just as something to think
of, we want the squared error to not exceed squared(5x60x60) =
324.000.000 .

\hypertarget{above-and-below}{%
\subsection{Above and below}\label{above-and-below}}

As a third criteria we have the idea that estimations might even each
other out. In a prefect scenario this would mean that 50\% of the
estimations are too high while the other 50\% are too low. To find out
how good we match we add 1 to a variable for every estimation we find
above the real value and then divide it by the number of tasks. The
result should be .5 when hitting the target.

\hypertarget{the-data}{%
\subsection{The Data}\label{the-data}}

For training and estimating the quality of algorithms I use herein a
dataset that consists of all the tasks that we at the software
development shop at my employer recorded during the time between july
and december 2020 and january 2021 to august 2021. That means there are
two datasets available.

Unfortunately - since this is confidential information - I cannot
publish it alongside this material. But the errors and images can be
shared at it can advance the algorithms - and it is everything that I
have right now. So that will do.

I'll call them: swe2020 and swe2021.

Now let us get a glance at the data as well the first algorithm.


\documentclass[]{article}
\usepackage{amsmath}
\usepackage{xcolor}
\newtheorem{Def}{Definition}
%opening
\title{MTH 343 Numerical Analysis Lecture 2: Review of Computer Arithmetic}
\author{Sheikh Abdul Raheem Ali}

\begin{document}

\maketitle

\section*{Remarks}

\begin{enumerate}
	\item Numerical Analysis requires such tedious \& repetitive operations that only a computer can perform quickly \& without and mistakes.
	\item Computers are dumb and must be given complete instructions of every step. Programs can be written in any language you like.
	\item Writing code is not very important because extensive commercial software packages are available.
		\begin{enumerate}
			\item IMSL: International Mathematics \& Statistics Library
			\item NAG: Numerical Algorithm Group
			\item LAPACK: Linear Algebra package
		\end{enumerate}
		Alternatives: Computer Algebra Systems
		\begin{enumerate}
			\item Mathematica
			\item Maple
			\item MATLAB
		\end{enumerate}

\end{enumerate}

\section*{Floating-Point Arithmetic}

In computers, numbers are stored as floating point quantities in the general form:
\[ \pm \cdot (d_1 d_2 d_3 \ldots d_p) \cdot \beta^e, \] where
$ p $ = precision, the number of significant bits (digits), $ e $ = an integer exponent ranging from $ E_{min} $ to $ E_{max} $, $ \beta $ = the number base, normally 2, 10, 16,
$ d_i: $ ranges from 0 to $ \beta - 1 $, and $ d_1 d_2 d_3 \ldots d_p $ is called the fractional part (mantissa).

Sometimes numbers are normalized: $ 0.023 -> 0.23 \cdot 10^{-1} $

Let us examine the case $ \beta = 10 $ (Decimal)

\begin{eqnarray*}
	3216 &=& 3\cdot10^3 + 2\cdot10^2 + 1\cdot10^1 + 6\cdot10^0\\
	&=& 10^4(3\cdot10^{-1} + 2\cdot10^{-2} + 1\cdot10^{-3} + 6\cdot10^{-4}) \\
	&=& (.3216)\cdot10^4
\end{eqnarray*}

Now let us examine the case $ \beta = 2 $ (Binary)

\begin{eqnarray*}
65 &=& 2^6 + 2^0 \\
      &=& 2^7(2^{-1}) + 2^{-7} \\
 &=&(.1000001)_2 \cdot 2^7
\end{eqnarray*}

\begin{eqnarray*}
	23 &=& 2^4 + 2^3 + 2^2 \\
	&=& 2^5(2^{-1} + 2^{-2} + 2^{-3}) \\
	&=& (.111)_2 \cdot 2^5
\end{eqnarray*}

\begin{eqnarray*}
	  85 &=& 2^6 + 2^4 + 2^2 + 2^0 \\
	&=& 2^7(2^{-1} + 2^{-3} + 2^{-6} + 2^{-7}) \\
	&=& (.1010011)_2 \cdot 2^7
\end{eqnarray*}

\begin{eqnarray*}
	5.75 &=& 2^2 + 2^0 + 2^{-1} + 2^{-2} \\
	&=& 2^3(2^{-1} + 2^{-3} + 2^{-4} + 2^{5}) \\
	&=& (.10111)_2 \cdot 2^3
\end{eqnarray*}

\[ 0.6 = (.1001100110011001\ldots)_2 \]

This last example shows us a conversion error: the decimal is recurring, but since the computer only has a finite number of bits, the value is truncated at some point.

\begin{Def}[Round off error:]
The error that is produced when a computer is used to perform real-number calculations is called round-off error.
\end{Def}

There are two ways of truncating the mantissa:

\begin{enumerate}
	\item Chopping
	\item Rounding
\end{enumerate}

Ex. $ 13.76573 = .1376 {\bf{\color{red} |}} 573 \cdot 10^2 $

4 digits chopping: $ .1376 \cdot 10^2 $
4 digits rounding: $ .1377 \cdot 10^2 $

Numbers are \textbf{rounded} when stored in the floating point format.

\end{document}


\section{Generators and Iterators} % (fold)
\label{sec:iterators}

\begin{frame}[fragile]\frametitle{For loops}
    \framesubtitle{}

    Recall that in Python we can loop over the elements in a list simply
    by saying

    \begin{verbatim}
        for elem in li:
            # do something
    \end{verbatim}

    We also say that we are iterating over the list.

\end{frame}

\begin{frame}[fragile]\frametitle{Fibonnaci numbers}
    \framesubtitle{}

    Say we want to iterate over the first $n$ Fibonacci numbers:

    \begin{verbatim}
        for elem in fib(n):
            # do something
    \end{verbatim}

    \vfill

    How to implement Fib(n)?

\end{frame}

\begin{frame}[fragile]\frametitle{fib(n)}
    \framesubtitle{}

    We can use our function from the \emph{lists} homework, which returns a list
    with the first $n$ Fibonacci numbers.

    \vfill

    What happens when $n$ is large, say $n = 10^6$ or more generally $n = 10^k$?

    \pause\vfill

    Problem: we have to store the entire list...

    \pause Eventually we will run out of memory.

\end{frame}

\begin{frame}\frametitle{Generators}
    \framesubtitle{The yield keyword}

    Instead, we can use so called generators

    \vfill

    Only difference with functions: use \texttt{yield} instead of \texttt{return}

\end{frame}

\begin{frame}\frametitle{Example: Range}
    \framesubtitle{}

    \codeblock{code/generator_range.py}

\end{frame}

\begin{frame}\frametitle{Advantage over lists}
    \framesubtitle{}

    Note, in this case we never store the entire list.
    \vfill
    This scales much better!
    \vfill
    (though Fibonacci numbers scale poorly, but you get the idea)

\end{frame}

\begin{frame}\frametitle{Back to Fibonacci}
    \framesubtitle{}

    How to write a generator for the Fibonacci sequence?

    \codeblock{code/generator_fib.py}

\end{frame}

\begin{frame}\frametitle{Generators are a simple form of iterators}
    \framesubtitle{}

    Iterators underlie many aspects of Python such as
    \begin{itemize}
        \item List comprehensions
        \item Generators
    \end{itemize}

\end{frame}

\begin{frame}[fragile]\frametitle{Iterators are classes}
    \framesubtitle{}

    \begin{itemize}
        \item Init method to initialize iterator
        \item \verb|__iter__| method to set up the iterator
        \item \verb|next| method to loop over the entries
    \end{itemize}

    More control, but also more code.

\end{frame}

\begin{frame}\frametitle{Range as iterator}
    \framesubtitle{}

    A slight overkill...
    \codeblock{code/iterator_range.py}

\end{frame}

\begin{frame}\frametitle{Fibonacci iterator}
    \framesubtitle{No room for whitespace :(}

    \codeblock{code/iterator_fib.py}

\end{frame}

% section iterators (end)


%% LyX 1.5.6 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[english,preprint]{revtex4}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{graphicx}
\usepackage{babel}

\begin{document}

\title{MATLAB version of the APBS}

\date{\today}

\begin{abstract}
This is the first version. This solver uses the biconjugate gradient
stabilized method and the inexact LU decomposition to numerically
solve the linearized PB equation on the finest (target) 3D-grid. This
version requires the shifted dielectric and the ion accessibility
coefficient (kappa function) maps as generated by the APBS code as
well as the corresponding pqr file generated by the pdb2pqr code.
It uses standard three-linear splines (spl0) to spread the charge
density along the nearest grid points if needed. The resulting electrostatic
potential and charge maps are saved in dx format. For visualization
purpose, this code also generates two files (.fig and .tiff) corresponding
to the graphical representation of the electrostatic potential surface.
\end{abstract}
\maketitle

\subsection*{Description}

This code is based on Michel Holst's thesis and Nathan Baker's APBS
approach. The box-method is used to discretize the following (linearized)
PB equation

\begin{equation}
-\nabla.\left(\epsilon\left(\mathbf{r}\right)\nabla u\left(\mathbf{r}\right)\right)+\bar{\kappa}\left(\mathbf{r}\right)u\left(\mathbf{r}\right)=magic\sum_{i=1}^{N}z_{i}\delta\left(\mathbf{r}-\mathbf{r}_{i}\right)\label{eq:one}\end{equation}

where $u\left(\mathbf{r}\right)=e_{c}\Phi\left(\mathbf{r}\right)/K_{B}T$
and $magic=4\pi e_{c}^{2}/K_{B}T.$ For a diagonal dielectric tensor,
the resulting discretized linear PB equations at the nodes $u_{ijk}=u\left(x_{i},y_{j},z_{k}\right)$
for $1\leq i\leq N_{x}$, $1\leq j\leq N_{y}$ and $1\leq k\leq N_{z}$
reads

\[
\left[\epsilon_{i-1/2,j,k}^{x}\frac{\left(h_{j-1}+h_{j}\right)\left(h_{k-1}+h_{k}\right)}{4h_{i-1}}+\epsilon_{i+1/2,j,k}^{x}\frac{\left(h_{j-1}+h_{j}\right)\left(h_{k-1}+h_{k}\right)}{4h_{i}}+\right.\]

\[
\epsilon_{i,j-1/2,k}^{y}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{k-1}+h_{k}\right)}{4h_{j-1}}+\epsilon_{i,j+1/2,k}^{y}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{k-1}+h_{k}\right)}{4h_{j}}+\]

\[
\epsilon_{i,j,k-1/2}^{k}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{j-1}+h_{j}\right)}{4h_{k-1}}+\epsilon_{i,j,k+1/2}^{k}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{j-1}+h_{j}\right)}{4h_{k}}+\]

\[
\left.\kappa_{ijk}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{j-1}+h_{j}\right)\left(h_{k-1}+h_{k}\right)}{8}\right]u_{ijk}+\]

\[
\left[-\epsilon_{i-1/2,j,k}^{x}\frac{\left(h_{j-1}+h_{j}\right)\left(h_{k-1}+h_{k}\right)}{4h_{i-1}}\right]u_{i-1jk}+\left[-\epsilon_{i+1/2,j,k}^{x}\frac{\left(h_{j-1}+h_{j}\right)\left(h_{k-1}+h_{k}\right)}{4h_{i}}\right]u_{i+1jk}+\]

\[
\left[-\epsilon_{i,j-1/2,k}^{y}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{k-1}+h_{k}\right)}{4h_{j-1}}\right]u_{ij-1k}+\left[-\epsilon_{i,j+1/2,k}^{y}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{k-1}+h_{k}\right)}{4h_{j}}\right]u_{ij+1k}+\]

\[
\left[-\epsilon_{i,j,k-1/2}^{k}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{j-1}+h_{j}\right)}{4h_{k-1}}\right]u_{ijk-1}+\left[-\epsilon_{i,j,k+1/2}^{k}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{j-1}+h_{j}\right)}{4h_{k}}\right]u_{ijk+1}=\]

\begin{equation}
magic\frac{\left(h_{i-1}+h_{i}\right)\left(h_{j-1}+h_{j}\right)\left(h_{k-1}+h_{k}\right)}{8}f_{ijk}\label{eq:two}\end{equation}

in which

\[
h_{i}=x_{i+1}-x_{i},\: h_{j}=y_{j+1}-y_{j}\: h_{k}=z_{k+1}-z_{k}\]

The delta functions appearing in the right hand side of the starting
equations are approximated with linear B-splines (spl0) which spread
the point like charge along the nearest neighborhood. The resulting
$f_{ijk}$ represent the smearing of the point charges along the grid
points.

For more details, including used unit system, please refer to the
Michel Holst's thesis and the APBS user guide online. To visualize
more clearly the problem, let's explicitly write the first equations
for a cubic grid of 5x5x5 containing general coefficients

\[
a_{222}u_{222}+a_{122}u_{122}+a_{322}u_{322}+a_{212}u_{212}+a_{232}u_{232}+a_{221}u_{221}+a_{223}u_{223}=f_{222}\]

\[
a_{322}u_{322}+a_{222}u_{222}+a_{422}u_{422}+a_{312}u_{312}+a_{332}u_{332}+a_{321}u_{321}+a_{323}u_{323}=f_{322}\]

\[
a_{422}u_{422}+a_{122}u_{122}+a_{322}u_{322}+a_{212}u_{212}+a_{232}u_{232}+a_{221}u_{221}+a_{223}u_{223}=f_{422}\]

\[
a_{232}u_{232}+a_{132}u_{132}+a_{332}u_{332}+a_{222}u_{222}+a_{242}u_{242}+a_{231}u_{231}+a_{233}u_{233}=f_{232}\]

\[
\ldots\]

in which the nodes are arranged using the natural ordering

\[
U=[u_{111},u_{211},..,u_{N_{x}11,}u_{121},..,u_{221},u_{321},..,u_{N_{x}21}...,u_{N_{x}N_{y}N_{z}}]^{T}\]

Note that the prescribed values of nodes $u_{1jk},u_{N_{x},j,k},u_{i,1,k},u_{i,N_{y},k},u_{ij1}$
and $u_{ijN_{z}}$ along the faces of the box coming from the Dirichlet
boundary conditions will have their corresponding elements removed
in such a way that only equations for the interior nodes remain. In
other words, we will only consider the following set of unknown nodes

\[
U=[u_{222},u_{322},..,u_{N_{x}-1,22,}u_{232},..,u_{332},u_{432},..,u_{N_{x}-2,32}...,u_{N_{x}-1,N_{y}-1,N_{z}-1}]^{T}\]

in such a way that the previous equations become

\[
a_{222}u_{222}+a_{322}u_{322}+a_{232}u_{232}+a_{223}u_{223}=f_{222}-a_{122}u_{122}-a_{212}u_{212}-a_{221}u_{221}\equiv b_{222}\]

\[
a_{322}u_{322}+a_{222}u_{222}+a_{422}u_{422}+a_{332}u_{332}+a_{323}u_{323}=f_{322}-a_{312}u_{312}-a_{321}u_{321}\equiv b_{322}\]

\[
a_{422}u_{422}+a_{322}u_{322}+a_{232}u_{232}+a_{223}u_{223}=f_{422}-a_{122}u_{122}-a_{212}u_{212}-a_{221}u_{221}\equiv b_{422}\]

\[
a_{232}u_{232}+a_{332}u_{332}+a_{222}u_{222}+a_{242}u_{242}+a_{233}u_{233}=f_{232}-a_{132}u_{132}-a_{231}u_{231}\equiv b_{232}\]

in which the boundary $u's$ are conveniently brought to the right-hand-side
of the equations. The resulting left-hand side equations can be written
in compact form in term of matrix vector product as follows

\[
Au=b\]

in which

\[
u\left(p\right)=u_{ijk},\qquad b\left(p\right)=b_{ijk},\qquad p=(k-2)(N_{x}-2)(N_{y}-2)+(j-2)(N_{x}-2)+i-1\]

\[
i=2,..,N_{x}-2,\quad j=2,..,N_{y}-2,\quad k=2,..,N_{z}-2\]

%
\begin{figure}
\includegraphics[scale=0.75]{Amatrix}

\caption{A Matrix representation}

\end{figure}

and $A$ is a (seven banded block tri-diagonal form) $(N_{x}-2)(N_{y}-2)(N_{z}-2)$
by $(N_{x}-2)(N_{y}-2)(N_{z}-2)$ squared symmetric positive definite
matrix containing the following nonzero elements (see figure 1):

\begin{itemize}
\item The main diagonal elements
\end{itemize}
\[
d_{0}(p)=\left[\epsilon_{i-1/2,j,k}^{x}\frac{\left(h_{j-1}+h_{j}\right)\left(h_{k-1}+h_{k}\right)}{4h_{i-1}}+\epsilon_{i+1/2,j,k}^{x}\frac{\left(h_{j-1}+h_{j}\right)\left(h_{k-1}+h_{k}\right)}{4h_{i}}+\right.\]

\[
\epsilon_{i,j-1/2,k}^{y}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{k-1}+h_{k}\right)}{4h_{j-1}}+\epsilon_{i,j+1/2,k}^{y}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{k-1}+h_{k}\right)}{4h_{j}}+\]

\[
\epsilon_{i,j,k-1/2}^{k}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{j-1}+h_{j}\right)}{4h_{k-1}}+\epsilon_{i,j,k+1/2}^{k}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{j-1}+h_{j}\right)}{4h_{k}}+\]

\begin{equation}
\left.\kappa_{ijk}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{j-1}+h_{j}\right)\left(h_{k-1}+h_{k}\right)}{8}\right]\label{eq:three}\end{equation}

\begin{itemize}
\item The Next upper band diagonal, which is shifted in one column to the
left from the first column, contains the following elements
\end{itemize}
\begin{equation}
\left[d_{1}(p)=-\epsilon_{i+1/2,j,k}^{x}\frac{\left(h_{j-1}+h_{j}\right)\left(h_{k-1}+h_{k}\right)}{4h_{i}}\right]\label{eq:four}\end{equation}

\begin{itemize}
\item The second upper band diagonal which is shifted $N_{x}-2$ columns
from the first column
\end{itemize}
\begin{equation}
d_{2}(p)=\left[-\epsilon_{i,j+1/2,k}^{y}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{k-1}+h_{k}\right)}{4h_{j}}\right]\label{eq:five}\end{equation}

\begin{itemize}
\item The third upper band diagonal which is shifted $(N_{x}-2)(N_{y}-2)$
columns from the first column
\end{itemize}
\begin{equation}
d_{3}(p)=\left[-\epsilon_{i,j,k+1/2}^{k}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{j-1}+h_{j}\right)}{4h_{k}}\right]\label{eq:six}\end{equation}

The remaining elements of the upper triangular squared matrix A are
set equal to zero. By symmetry we obtain the lower triagonal elements
of the matrix A. Because the matrix A is sparse and large, we can
implement efficient methods that optimally solve the linear system
for U. Specifically, we use the biconjugate gradient stabilized method
combined with the inexact LU decomposition of the matrix A. Having
the numerical values for the nodes in the interior of the box, we
finally add the previously removed prescribed values along the six
faces to get the solution over the complete set of grid points.

\subsection*{Computational algorithm}

\begin{enumerate}
\item We read the input file .inm to get the APBS input files (shifted dielectric
coefficients, kappa function and pqr data file) as well as the number
of grid points, the box Lengths, and temperature.
\item By using linear B-splines, we discretize the charge density to get
$f_{ijk}$ for $i=1,..,N_{x},\quad j=1,..,N_{y},\quad k=1,..,N_{z}$.
We also calculate the Dirichlet boundary condition along the six faces
of the box $u_{1jk},u_{N_{x},j,k},u_{i,1,k},u_{i,N_{y},k},u_{ij1}$
and $u_{ijN_{z}}$ using the temperature, the value of the bulk dielectric
coefficient (usually water) and ionic strength.
\item By using the expressions (\ref{eq:three}), (\ref{eq:four}),(\ref{eq:five}),
and (\ref{eq:six}), we evaluate the nonzero components of the matrix
$A$, e.g., the diagonal elements $d_{0}(p),d_{1}(p),d_{2}(p),$ and
$d_{3}(p),$ for $p=(k-2)(N_{x}-2)(N_{y}-2)+(j-2)(N_{x}-2)+i-1$ and
$i=2,..,N_{x}-1,\quad j=2,..,N_{y}-1,\quad k=2,..,N_{z}-1$. The values
for the shifted dielectric coefficients and kappa function elements
are obtained from the APBS input files. The values of the mesh size
$h_{i},h_{j}$ and $h_{k}$ are obtained from the number of grid points
and the Length of the box. Next, we built the sparse upper triangular
matrix A by filling with zeros the remaining elements of the matrix
A. Next, we obtain the lower triangular elements of the matrix A by
using the following symmetry property $A_{pq}=A_{qp}$ for $q=1,..,(N_{x}-2)(N_{y}-2)(N_{z}-2)$
and $p=q,..,(N_{x}-2)(N_{y}-2)(N_{z}-2)$.
\item By using the values obtained for the discretized charge density $f_{ijk}$
and the values of the Dirichlet boundary elements multiplied by the
appropriate shifted dielectric coefficient values, we evaluate the
elements of $b_{ijk}$. We use the natural ordering $p=(k-2)(N_{x}-2)(N_{y}-2)+(j-2)(N_{x}-2)+i-1$
and $i=2,..,N_{x}-1,\quad j=2,..,N_{y}-1,\quad k=2,..,N_{z}-1$ to
construct the corresponding vector $b(p)$ (one index) from the data
array structure (three indices) $b_{ijk}$.
\item We use the inexact $LU$ decomposition of the matrix $A$. The default
tolerance value is set equal to 0.25 which provides a fast evaluation
of the matrices $L$ and $U$.
\item The resulting $L$ and $U$ matrices, the matrix $A$ and the vector
$b$ are used to approximately solve $Au=b$ for the vector $u$ using
the biconjugate gradient stabilized method. The default accuracy is
set equal to 10\textasciicircum{}-9 and the maximum number of iteration
equal to 800.
\item We use the natural ordering relationship to convert the resulting
vector $u(p)$ to data array structure to get the numerical solution
for $u_{ijk}$ for $i=2,..,N_{x}-1,\quad j=2,..,N_{y}-1,\quad k=2,..,N_{z}-1$.
\item Finally we add the previously removed values of the nodes at the faces
of the box to obtain the solution for the nodes $u_{ijk}$ over the
complete set of grid points, namely for $i=1,..,N_{x},\quad j=1,..,N_{y},\quad k=1,..,N_{z}$.
\item The electrostatic potential $u_{ijk}$ and the charge $f_{ijk}$ maps
are saved in dx format files.
\item The electrostatic potential surface $u_{ij(N_{z}+1)/2}$ is saved
in tiff and fig format files for visualization purpose.
\end{enumerate}

\end{document}


\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{breqn}
\usepackage{graphicx}
\usepackage[left=1in,right=1in,top=1in,bottom=1in]{geometry}
\usepackage{cleveref}

\makeatletter
\newcommand*{\declarecommand}{%
  \@star@or@long\declare@command
}
\newcommand*{\declare@command}[1]{%
  \provide@command{#1}{}%
  \renew@command{#1}%
}
\makeatother

\declarecommand{\x}{{\mathbf{x}}}
\declarecommand{\p}{{\mathbf{p}}}

\declarecommand{\u}{{\mathbf{u}}}
\declarecommand{\U}{{\mathbf{U}}}
\declarecommand{\grad}{\nabla}
\declarecommand{\Wi}{\textnormal{Wi}}
\declarecommand{\Id}{\mathbb{I}}
\declarecommand{\f}{{\mathbf{f}}}
\declarecommand{\F}{{\mathbf{F}}}
\declarecommand{\n}{{\mathbf{n}}}
\declarecommand{\X}{{\mathbf{X}}}
\declarecommand{\b}{{\mathbf{b}}}
\declarecommand{\a}{{\mathbf{a}}}
\declarecommand{\bmu}{{\boldsymbol{\mu}}}
\declarecommand{\tS}{{\tilde S}}

\begin{document}

\section{The Bingham closure}

The Bingham closure is given by:
\begin{equation}
    S(\x) = \int\psi_B(\x,\p)\p\p\p\p\,d\p,
\end{equation}
where the Bingham distribution $\psi_B(\x,\p)$ is defined by the constraints that:
\begin{subequations}
    \begin{align}
        \int\psi_B(\x,\p)\,d\p      &= \phi(x),  \\
        \int\psi_B(\x,\p)\p\p\,d\p  &= D(\x),
    \end{align}
\end{subequations}
where $\phi$ and $D$ are the zeroth and second moments with respect to the true distribution function $\psi$. We assume that $\psi_B(\x,\p)$ takes the form:
\begin{align}
    \psi_B(\x,\p) = Ae^{B:\p\p}.
\end{align}
Given $\phi(\x)$ and $D(\x)$, our goals is to find the coefficients $A$ and $B$. Because $B$ only appears contracted against a symmetric matrix, it is sufficient to assume that $B$ is symmetric. In fact, we will see that our goal is even simpler than this: we will only be interested in computing $S:E$ and $S:D$, where $E=\grad\u+\grad\u^\intercal$. The purpose of this package is to provide optimized routines for computing these contractions. This documentation describes how the package works.

\section{The Bingham Closure in 2D}

\subsection{A simple formula for the closure}

From Chaubal and Leal, $B$ and $D$ are diagonalized in the same frame. We assume that we are in this frame; and compute the closure here. We will clean up details afterwards. In this frame, we have that:
\begin{subequations}
    \begin{align}
        1 &= \int_0^{2\pi}Ae^{\lambda_0\cos^2\theta + \lambda_1\sin^2\theta}\,d\theta,   \\
        \mu_0 &= \int_0^{2\pi}Ae^{\lambda_0\cos^2\theta + \lambda_1\sin^2\theta}\cos^2\theta\,d\theta,   \\
        \mu_1 &= \int_0^{2\pi}Ae^{\lambda_0\cos^2\theta + \lambda_1\sin^2\theta}\sin^2\theta\,d\theta,
    \end{align}
\end{subequations}
Note we have assumed here that $\phi=1$. $\mu$ and $\lambda$ are the eigenvalues of $D$ and $B$ respectively, with $\mu_0>\mu_1$. We note that $\lambda$ can only be fixed up to an additive constant: to see this, let $\lambda_0$ and $\lambda_1$ solve the above equations.  Then letting $\tilde\lambda_i=\lambda_i+C$ for $i=0,1$, we have:
\begin{equation}
    \int_0^{2\pi}Ae^{\tilde\lambda_0\cos^2\theta + \tilde\lambda_0\sin^2\theta}f(\theta)\,d\theta = \int_0^{2\pi}Ae^{C}e^{\lambda_0\cos^2\theta + \lambda_1\sin^2\theta}f(\theta)\,d\theta,
\end{equation}
which simply changes the definition of $A$. We can choose a convenient choice of $C$ then; it is convenient to choose $C$ so that $\lambda_0 + \lambda_1 = 0$. Then we have that:
\begin{subequations}
    \begin{align}
        1 &= \int_0^{2\pi}Ae^{\lambda_0(\cos^2\theta - \sin^2\theta)}\,d\theta,   \\
        \mu_0 &= \int_0^{2\pi}Ae^{\lambda_0(\cos^2\theta - \sin^2\theta)}\cos^2\theta\,d\theta.
    \end{align}
\end{subequations}
Exploiting the trig identity that $\cos^2\theta-\sin^2\theta=\cos(2\theta)$, and because:
\begin{equation}
    \int_0^{2\pi}e^{\lambda_0\cos(2\theta)}\,d\theta = 2\pi I_0(\lambda_0),
\end{equation}
we find that
\begin{equation}
    A = \frac{1}{2\pi I_0(\lambda_0)},
\end{equation}
where $I_v$ is the modified Bessel function of the first kind. Thus we find:
Now we're down to the single equation:
\begin{equation}
    \mu_0 = \frac{1}{2\pi I_0(\lambda_0)}\int_0^{2\pi}e^{\lambda_0\cos(2\theta)}\cos^2\theta\,d\theta.
\end{equation}
Evaluating this final integral and simplifying gives:
\begin{equation}
    2\mu_0 = 1 + \zeta(\lambda_0),
\end{equation}
where we have defined $\zeta(x)=I_1(x)/I_0(x)$. Thus to find $\lambda_0$ given $\mu_0$, we simply need to solve this nonlinear equation for $\lambda_0$.

\subsection{Solution of the closure equation and numerical issues}

In this section we consider the issues with solving the nonlinear equation:
\begin{equation}
    2\mu = 1 + \zeta(\lambda)
    \label{eqn:bingham_nonlinear}
\end{equation}
for $\lambda$, given $\mu\in[0.5,1.0]$. Note that $\mu_0$, the largest eigenvalue, must live in this range becuase it is the largest eigenvalue and the eigenvalues sum to $1$. The fundamental problem with simply throwing a naive Newton solver at this equation is that as $\mu\to1$, $\lambda\to\infty$. While not a problem in and of itself, $\zeta(\lambda)$ is the ratio of $I_1(\lambda)$ and $I_0(\lambda)$. Both of these functions diverge exponentially fast as $\lambda$ gets large, and so naive evaluation of the ratio fails. Nevertheless, their ratio converges to $1$: our primary challenge is to find a way to evaluate $\zeta$ stably for large arguments. Fortunately, we are in luck! As it turns out, we can write:
\begin{equation}
    I_\nu(z) = \frac{e^z}{\sqrt{2\pi z}}\mathcal{P}_\nu(z),
\end{equation}
where $\mathcal{P}_\nu(z)$ is a power series in $z^{-1}$ that converges for sufficiently large $z$. Our strategy is clear, then. For small arguments, we can evaluate $\zeta$ directly. For larger arguments, we compute $\zeta$ by:
\begin{equation}
    \zeta(\lambda) = \mathcal{P}_1(\lambda)/\mathcal{P}_0(\lambda).
\end{equation}
In my numerical experiments, the power series $\mathcal{P}_0$ and $\mathcal{P}_1$ converge rapidly when the argument is at least $20$, and direct evaluation has no issues for this size argument. We thus evaluate $\zeta$ directly for $|\lambda|\leq20$, and indirectly via the power series representations for $|\lambda|>20$.

In order to find $\lambda$, we compute the solution to the equation $1/2 + \zeta(\lambda)/2 - \mu = 0$. The Jacobian is given by:
\begin{equation}
    2\mathcal{J}(\lambda) = 1 - \zeta(\lambda)/\lambda - \zeta(\lambda)^2.
\end{equation}
Fortunately, the singularity in $\zeta(\lambda)/\lambda$ is removable. We construct a function to evaluate this quantity using the function\_generator package, on an approximation interval of $[ -1.001, 1.0]$. These bounds are chosen so that evaluation points for the Chebyshev interpolants used by the function\_generator do not live at the singularity. When $|\lambda|>1$, we evaluate this quantity direclty.

\subsection{Fast evaluation of the closure equation}

This provides a stable way to compute $\lambda(\mu)$ for every value of $\mu\in[0.5, 1.0]$, but it requires a Newton iteration for every value of $\mu$ given. Instead, we might consider constructing an interpolant for this function. Unfortunately as mentioned before, as $\mu\to1$, $\lambda\to\infty$. Because the function\_generator package allows for adaptive, brute force interpolation, it could probably handle this. But we can be smarter. Note that what we actually want to calculate is:
\begin{equation}
    S(\x) = \psi_B(\x,\p)\p\p\p\p\,d\p.
\end{equation}
By exploiting identities, as we will show momentarily, we can reduce computing all of this to computing:
\begin{equation}
    S_{0000}(\x) = \int_0^{2\pi}Ae^{\lambda(\x)\cos(2\theta)}\cos^4\theta\,d\theta.
\end{equation}
Again, this function can be computed analytically, and dropping the $\x$, the result is:
\begin{equation}
    S_{0000} = \frac{1}{2} - \frac{\zeta(\lambda)}{4\lambda} + \frac{\zeta(\lambda)}{2}
    \label{eqn:bingham_integral}
\end{equation}
Now we're getting somewhere: we just define the function $S_{0000}(\mu)$ as:
\begin{itemize}
    \item Given, $\mu$, solve \Cref{eqn:bingham_nonlinear} for $\lambda$ using a Newton iteration,
    \item Evaluate \Cref{eqn:bingham_integral} with the argument $\lambda$ from the first step.
\end{itemize}
Fortunately, $S_{0000}(\mu)$ is a bounded and relatively smooth function of $\mu$. Thus we can use function\_generator to construct a nearly machine-precision and very accurate approximation of $S_{0000}(\mu)$.

\section{The full algorithm}

We now assume that we are given $D(\x)$ and outline a method for computing $(S:E)(\x)$ and $(S:D)(\x)$. Because these computations are done pointwise, we will omit $\x$ for the remainder. We first compute the eigendecomposition of $D$:
\begin{equation}
    D = \Omega\Lambda\Omega^\intercal.
\end{equation}
Since $D$ is symmetric positive-definite, we may apply the routine np.linalg.eigh (which is faster than np.linalg.eig) in order to find the ordered eigenvalues. We call $\mu_0$ the larger eigenvalue, and $\mu_1$ the smaller eigenvalue. Using the methodology from above, we compute $\tilde S_{0000}(\mu_0)$. Note that here it is denoted explicitly as $\tilde S_{0000}$ - this is because this is not actually $S_{0000}$, but that value in the diagonalized frame. Via identites, we may compute some of the other components as:
\begin{align}
    \tilde S_{0011} &= \mu_0 - S_{0000},    \\
    \tilde S_{1111} &= \mu_1 - S_{0011},    \\
    \tilde S_{0001} &= 0, \\
    \tilde S_{0111} &= 0,
\end{align}
We now simply have to perform a rotation:
\begin{equation}
    S_{ijkl} = \Omega_{im}\Omega_{jn}\Omega_{kq}\Omega_{lp}\tilde S_{mnqp},
\end{equation}
to get $S$ back. Calculuating this sum is somewhat nasty. Luckily we can exploit symmetries/identites to speed things up:
\begin{align}
    S_{0000} &= \Omega_{00}^4\tilde S_{0000} + 4\Omega_{00}^3\Omega_{01}\tilde S_{0001} + 6\Omega_{00}^2\Omega_{01}^2\tilde S_{0011} + 4\Omega_{00}\Omega_{01}^3\tilde S_{0111} + \Omega_{01}^4 S_{1111},  \\
    S_{0001} &= \Omega_{00}^3\Omega_{10}\tilde S_{0000} + (3\Omega^2\Omega_{01}\Omega_{10} + \Omega_{00}^3\Omega_{11})\tilde S_{0001} + 3(\Omega_{00}\Omega_{01}^2\Omega_{10} + \Omega_{00}^2\Omega_{01}\Omega_{11})\tilde S_{0011} +   \\
        &\qquad(3\Omega_{00}\Omega_{01}^2+\Omega_{11}+\Omega_{01}^3\Omega_{10})\tilde S_{0111} + \Omega_{01}^3\Omega_{11}\tilde S_{1111}.
\end{align}
Note that $\tilde S_{0001}=\tilde S_{0111}=0$, and so these terms can be left out of the sums in implementation, reducing these rotations to:
\begin{align}
    S_{0000} &= \Omega_{00}^4\tilde S_{0000} + 6\Omega_{00}^2\Omega_{01}^2\tilde S_{0011} + \Omega_{01}^4 S_{1111},  \\
    S_{0001} &= \Omega_{00}^3\Omega_{10}\tilde S_{0000} + 3(\Omega_{00}\Omega_{01}^2\Omega_{10} + \Omega_{00}^2\Omega_{01}\Omega_{11})\tilde S_{0011} + \Omega_{01}^3\Omega_{11}\tilde S_{1111}.
\end{align}
Knowing these, we can exploit more identites to find the rest of the components:
\begin{subequations}
    \begin{align}
        S_{0000} + S_{0011} &= D_{00},  \\
        S_{0100} + S_{0111} &= D_{01},  \\
        S_{1100} + S_{1111} &= D_{11}.
    \end{align}
\end{subequations}
Finally now that we have these components of $S$, we just need to compute $S:E$ and $S:D$. For a general symmetric tensor $T$, we have that:
\begin{subequations}
    \begin{align}
        (S:T)_{00} &= S_{0000}T_{00} + S_{0011}T_{11} + 2S_{0001}T_{01},    \\
        (S:T)_{01} &= S_{0001}T_{00} + S_{0111}T_{11} + 2S_{0011}T_{01},    \\
        (S:T)_{11} &= S_{0011}T_{00} + S_{1111}T_{11} + 2S_{0111}T_{01}.
    \end{align}
\end{subequations}
Since $S:T$ is symmetric, $(S:T)_{10}=(S:T)_{01}$.

\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{float}

\newcommand{\comment}[1]{}

\title{Final Project Team 6}
\author{Berenice, Gabriela, Juan, Héctor, Damián}
\date{May 2021}

\begin{document}
    \maketitle
    \tableofcontents
    \newpage

    \begin{abstract}
        The hypothesis of this theorem is that we have a function $F$ that is continuous in a closed interval $[a, b]$, differentiable in the open interval $(a, b)$ and whose values in its extremes $F(a)$ and $F(b)$ match.
        The thesis of the theorem is that, in this case, the derived function vanishes at some point in the interval $(a, b)$
        Notice that since the interval is closed, it makes sense to talk about both $F(a)$ and $F(b)$. We will see that, intuitively, this statement is very simple.
        Rolle's theorem guarantees us that, under these conditions, there must be at least a certain value $x$ of the interval $(a, b)$ for which $F'(x) = 0$. But it only assures us that there has to be that value, not tells us nothing about its how to find it.

        This will be necessary for the problem, because it has a great complexity and will make us develop different mathematical skills to understand how the application of integrals and derivatives works.
    \end{abstract}

    \section{Problem}
    Let $f$ be a three times differentiable function (defined on $\mathbb{R}$
    and real-valued) such that $f$ has at least five distinct real zeros.
    Prove that $f + 6f' + 12f'' + 8f'''$ has at least two distinct real zeros.

    \subsection{Polynomial Function}
    A polynomial is generally represented as $P(x)$. The highest power of the variable of $P(x)$ is known as its degree. Degree of a polynomial function is very important as it tells us about the behaviour of the function $P(x)$ when x becomes very large, and also helps us to know the number of roots that we can have in a function. The domain of a polynomial function is entire real numbers $\mathbb{R} $.

    \comment{
    \section*{Rolle's Thorem}
    Let $f$ be a continuous function on $[a, b]$ and differentiable on $]a, b[$ such that $f(a) = f(b)$. Then there exists $c \in ]a, b[ $ such that $f'(c) = 0$.

    This theorem will be admitted because its proof requires results which are not seen in this course. If $f$ is the constant function, the result is obvious. Otherwise, since $f$ is continuous over $[a, b]$, $f$ is bounded over $[a, b]$
    and reaches its bounds. This means that there exists $m \in  [a, b] $ and $M \in  [a, b] $ such that $\forall x \in  [a, b]$, $f(m) \leqslant  f(x) \leqslant f(M) (f(m) \neq  f(M)$ because $f$ is not a constant function$)$. As $f(a) = f(b)$, then we have the following cases:

    \begin{enumerate}
        \item if $m = a$ or $m = b$, then $M \in  ]a, b[$, hence we have $c = M$;
        \item if $M = a$ or $M = b$, then $m \in  ]a, b[$, hence we have $c = m$;
        \item $m \in  ]a, b[$ and $M \in  ]a, b[$, hence we have $c = m$ or $c = M$.
    \end{enumerate}

    So in all cases $f$ admits a local extremum at a point $c$ of $]a, b[$ and is differentiable on $]a, b[$, hence, according to the proposition 22, $f'(c) = 0$.
    \section*{Hint}
    Use $g : x \rightarrow  e^ {\alpha x} $
    }

    \subsection{Rolle's Theorem}
    Suppose $f(x)$ is a function that satisfies all of the following.
    $f(x)$ is continuous on the closed interval $[a,b]$.
    $f(x)$ is differentiable on the open interval $(a,b)$.
    $$f(a) = f(b)$$
    Then there is a number c such that $a<c<b$ and $f'(c)=0$. Or, in other words $f(x)$ has a critical point in $(a,b)$.

    \begin{figure}[H]
        \centering
        \includegraphics[width=340px]{img/rollestheorem.jpg}
        \caption{Graphic of Rolle's Theorem}
    \end{figure}

    In other words, if a continuous curve passes through the same y-value (such as the x-axis) twice and has a unique tangent line (derivative) at every point of the interval, then somewhere between the endpoints it has a tangent parallel to the x-axis. The theorem was proved in 1691 by the French mathematician Michel Rolle, though it was stated without a modern formal proof in the 12th century by the Indian mathematician Bhaskara II. Other than being useful in proving the mean-value theorem, Rolle’s theorem is seldom used, since it establishes only the existence of a solution and not its value.

    This theorem helps us to know how many roots (distinct zeros) our function will have once we have derived it three times.

    \subsection{Exponential Function}
    An exponential function is a Mathematical function in form $f(x) = a^x$, where $x$ is a variable and $a$ is a constant which is called the base of the function and it should be greater than $0$. The most commonly used exponential function base is the transcendental number $e$, which is approximately equal to $2.71828$.

    An exponential function is defined by the formula $f(x) = a^x$, where the input variable x occurs as an exponent. The exponential curve depends on the exponential function and it depends on the value of the x.

    The exponential function is an important mathematical function which is of the form

    $$f(x) = a^x$$

    Where $a > 0$ and is not equal to $1$.

    $x$ is any real number.

    If the variable is negative, the function is undefined for $-1 < x < 1$.

    $$$$

    Here,
    $x$ is a variable
    $a$ is a constant, which is the base of the function.

    An exponential curve grows, or decay depends on the exponential function. Any quantity that grows or decays by a fixed per cent at regular intervals should possess either exponential growth or exponential decay.

    \pagebreak
    \section{Solution}
    Applying Rolle's Theorem:

    $f(x) \rightarrow 5$ distinct real zeros

    $f'(x) \rightarrow 4$ distinct real zeros

    $f''(x) \rightarrow 3$ distinct real zeros

    $f'''(x) \rightarrow 2$ distinct real zeros

    \subsection{Hint}
    We should use $e^{\alpha x}$

    \begin{itemize}
        \item Can be differentiable as much as we want
        \item Can have all zeros the function needs
        \item The differentiable function would express the no zeros
    \end{itemize}

    \subsection{Set up:}
    $$g(x) = e^{\alpha x} f(x)$$
    We infer the derivatives from the product rule:
    $$(f(x) g(x))' = f'(x) g(x) + f(x) g'(x)$$
    So, we have the consecuent derivatives:
    \begin{equation}
        \begin{split}
        &g'(x) = \alpha e^{\alpha x} f(x) + e^{\alpha x} f'(x)\\
        &g''(x) = \alpha ^2 e^{\alpha x} f(x) + 2\alpha ^2 e^{\alpha x} f'(x) + \alpha e^{\alpha x} f''(x)\\
        &g'''(x) = \alpha ^3 e^{\alpha x} f(x) + 3\alpha ^2 e^{\alpha x} f'(x) + 3\alpha e^{\alpha x} f''(x) + e^{\alpha x} f'''(x)\\
        \end{split}
    \end{equation}
    Simplifying from the algebraic properties:
    $$g'''(x) = e^{\alpha x} (\alpha ^3  f(x) + 3\alpha ^2f'(x) + 3\alpha f''(x) +  f'''(x))$$

    \subsection{Substitution}
    In the problem we have: $f + 6f' + 12f'' + 8f'''$

    \begin{equation}
        \begin{split}
            &8g'''(x) = 8e^{\alpha x} (\alpha ^3 f(x) + 3\alpha ^2 f'(x) + 3\alpha f''(x) + f'''(x))\\
            &8g'''(x) = e^{\alpha x} (8\alpha ^3 f(x) + 24\alpha ^2 f'(x) + 24\alpha f''(x) + 8f'''(x))\\
        \end{split}
    \end{equation}
    Using $\alpha$ as $\frac{1}{2}$ we got:
    \begin{equation}
        \begin{split}
            8g'''(x) =
            e^{\frac{x}{2}}
            \left(8\left(\frac{1}{2}\right)^3 f(x) +
            24\left(\frac{1}{2}\right)^2 f'(x) +
            24\left(\frac{1}{2}\right) f''(x) +
            8f'''(x)\right)
        \end{split}
    \end{equation}

    So with that we get:

    \begin{equation}
        \begin{split}
            8g'''(x) =
            \frac{e^{\frac{x}{2}}}{8}
            \left(f(x) +
            6 f'(x) +
            12 f''(x) +
            8f'''(x)\right)
        \end{split}
    \end{equation}

    \subsection{Answer:}
    $$g(x) = 8e^{\frac{x}{2}} f(x)$$

    In this case, $g'''(x)$ has at least 2 zeros

    \section{Appendix}
    Before we found the final solution, we made many attempts on how
    we could resolve this:

    \begin{figure}[H]
        \centering
        \includegraphics[width=340px]{img/firstattempt.png}
        \caption{First Attempt}
    \end{figure}

    \begin{figure}[H]
        \includegraphics[width=340px]{img/secondattempt.png}
        \caption{Second Attempt}
    \end{figure}

    \begin{figure}[H]
        \includegraphics[width=340px]{img/thirdattempt.png}
        \caption{Third Attempt}
    \end{figure}

    \pagebreak
    \section{Bibliography}
    \begin{itemize}
        \item Hosch, W. L. (2021, April 15). Rolle’s theorem | Definition, Equation, \& Facts. Encyclopedia Britannica. \\
        \href{https://www.britannica.com/science/Rolles-theorem}{https://www.britannica.com/science/Rolles-theorem}

        \item Dawkins, P. (2019, February 21). Calculus I - The Mean Value Theorem. \\
        \href{https://tutorial.math.lamar.edu/classes/calci/MeanValueTheorem.aspx}{https://tutorial.math.lamar.edu/classes/calci/MeanValueTheorem.aspx}

        \item A. (2021, March 22). General Data Protection Regulation(GDPR) Guidelines BYJU’S. BYJUS. \\
        \href{https://byjus.com/maths/exponential-functions/}{https://byjus.com/maths/exponential-functions/}

        \item A. (2021, March 22). General Data Protection Regulation(GDPR) Guidelines BYJU’S. BYJUS.\\
        \href{https://byjus.com/maths/polynomial-functions/}{https://byjus.com/maths/polynomial-functions/}
    \end{itemize}

\end{document}


\documentclass{beamer}
\usepackage{hyperref}
\usepackage{subfig}  %% To include subfigures
\usepackage{media9} %
\usepackage{ragged2e}  % Allow justification
\usepackage{url}
\usepackage[margin=20pt,font=small,labelfont=bf,labelsep=period]{caption}

\hypersetup{pdfstartview={Fit}, bookmarks=True, pdftitle={Wave Propagation Lectures},
            pdfauthor={Nicolas Guarin-Zapata}, pdfsubject={Lectures},
            pdfkeywords={Waves, Elasticity, Numerical Methods}}  % Configure hyperref

%--- New commands ----%
\newcommand{\footref}[1]{\textsuperscript{\ref{#1}}}
\newcommand{\pardiff}[2]{\frac{\partial #1}{\partial #2}}
\newcommand{\pardiffd}[2]{\frac{\partial^2 #1}{\partial #2^2}}
%---------------------%

%\usefonttheme[onlymath]{serif}  % Make equations to be in serif fonts
\usefonttheme{serif}  % Make equations to be in serif fonts

\begin{document}

%title
\title[Wave propagation in solids] % (optional, only for long titles)
{Wave propagation:}
\subtitle{Numerical tools}
\author[Guarin-Zapata, Nicolas] % (optional, for multiple authors)
{Nicol\'as Guar\'in Zapata\\ \texttt{\small nguarin@purdue.edu}\\
{\tiny Slides available at: \url{https://github.com/nicoguaro/CE597-slides}}}
\institute{Civil Engineering Department\\
  Purdue University}
\date{\today}
\subject{Wave propagation}

% Title page
\frame{\titlepage}

% Outline
\begin{frame}
	\frametitle{Outline}
	\tableofcontents
\end{frame}
%

%  Relations between elastic constants
\section{Relations between elastic constants}
\begin{frame}[shrink=50]
\frametitle{Relations between elastic constants}
\centering

\vspace{3cm}
\begin{table}[h]
\centering %
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|}
\hline
 & $(K,E)$  & $(K,\lambda)$  & $(K,G)$  & $(K,\nu)$  & $(E,G)$  & $(E,\nu)$  & $(\nu,G)$  & $(\nu,\lambda)$  & $(G,\lambda)$  & $(G,M)$ \\
\hline
$K=$  & $K$  & $K$  & $K$  & $K$  & $\frac{EG}{3(3G-E)}$  & $\frac{E}{3(1-2\nu)}$  & $\lambda+\frac{2G}{3}$  & $\frac{\lambda(1+\nu)}{3(1-2\nu)}$  & $\frac{2G(1+\nu)}{3(1-2\nu)}$  & $M-\frac{4G}{3}$ \\
\hline
$E=$  & $E$  & $\frac{9K(K-\lambda)}{3K-\lambda}$  & $\frac{9KG}{2K+G}$  & $3K(1-2\nu)$  & $E$  & $E$  & $\frac{G(3\lambda+2G)}{\lambda+G}$  & $\frac{\lambda(1+\nu)(1-2\nu)}{\nu}$  & $2G(1+\nu)$  & $\frac{G(3-M-4G)}{M-2G}$ \\
\hline
$\lambda=$  & $\frac{3K(3KE)}{9K-E}$  & $\lambda$  & $K-\frac{2G}{3}$  & $\frac{3K\nu}{1+\nu}$  & $\frac{G(E-2G)}{EG-E}$  & $\frac{E\nu}{(1+\nu)(1-2\nu)}$  & $\lambda$  & $\lambda$  & $\frac{2G\nu}{1-2\nu}$  & $M-2G$ \\
\hline
$G=$  & $\frac{3KE}{9K-E}$  & $\frac{3(K-\lambda)}{2}$  & $G$  & $\frac{3K(1-2\nu)}{2(1+\nu)}$  & $G$  & $\frac{E}{2(1+\nu)}$  & $G$  & $\frac{\lambda(1-2\nu)}{2\nu}$  & $G$  & $G$ \\
\hline
$\nu=$  & $\frac{3K-E}{6K}$  & $\frac{\lambda}{3K-\lambda}$  & $\frac{3K-2G}{2(3K+G)}$  & $\nu$  & $\frac{E}{2G}-1$  & $\nu$  & $\frac{\lambda}{2(\lambda+G)}$  & $\nu$  & $\nu$  & $\frac{M-2G}{2(M-G)}$ \\
\hline
$M=$  & $\frac{3K(3K+E)}{9K-E}$  & $3K-2\lambda$  & $K+\frac{4G}{3}$  & $\frac{3K(1-\nu)}{1+\nu}$  & $\frac{G(4G-E)}{3G-E}$  & $\frac{E(1-\nu)}{(1+\nu)(1-2\nu)}$  & $\lambda+2G$  & $\frac{\lambda(1-\nu)}{\nu}$  & $\frac{2G(1-\nu)}{1-2\nu}$  & $M$ \\
\hline
\end{tabular}
\end{table}

{\large $K$: Bulk modulus, $\lambda$: Lam\'e's first parameter, $E$: Young's
modulus, $G$: Shear modulus, $\nu$: Poisson's ratio, $M$: P-wave
modulus.}
\end{frame}

% Elastodynamics equations
\section{Elastodynamic wave equations}
\subsection{Navier-Cauchy Equations}
\begin{frame}
\frametitle{Navier-Cauchy Equations}
The Navier-Cauchy equations read
\begin{equation}
\rho{ \pardiffd{\bold{u}}{t}} = \bold{f} + ( \lambda + 2G)\nabla(\nabla \cdot \bold{u}) - G\nabla \times (\nabla \times \bold{u}) \enspace ,
\label{eq:navierVec}
\end{equation}
or, rearranging the constants
\begin{equation}
 \pardiffd{\bold{u}}{t} = \bold{f} + \alpha^2\nabla(\nabla \cdot \bold{u}) - \beta^2\nabla \times (\nabla \times \bold{u}) \enspace ,
\label{eq:navierVec2}
\end{equation}
being $\alpha$ the speed of the P-wave and $\beta$ the speed of the S-wave. According to this, our problem just depends on two material properties, the two wave speeds.

\end{frame}

% Relations for elastic wave speeds
\subsection{Relations for elastic wave speeds}
\begin{frame}[allowframebreaks]
\frametitle{Relations for elastic wave speeds}
The P-wave is a dilatational wave with speed $\alpha$ given by
\begin{align*}
 & \alpha^{2}=\frac{\lambda+2G}{\rho},\qquad\alpha^{2}=\frac{G(1-\nu)}{\rho},\\
 & \alpha^{2}=\frac{M}{\rho},\qquad\alpha^{2}=\frac{E(1-\nu)}{(1+\nu)(1-2\nu)\rho},\\
 & \alpha^{2}=\frac{2\beta^{2}(1-\nu)}{1-2\nu}.
\end{align*}
 The S-wave is a distorsional wave with speed $\beta$ given by
\begin{align*}
 & \beta^{2}=\frac{G}{\rho}\\
 & \beta^{2}=\frac{E}{2(1+\nu)\rho},\\
 & \beta^{2}=\frac{\alpha^{2}(1-2\nu)}{2(1-\nu)}.
\end{align*}
 Some particular values for the ratio
\[
\frac{\alpha^{2}}{\beta^{2}}=\frac{2(1-\nu)}{1-2\nu}
\]
 are
\begin{align*}
 & \frac{\alpha^{2}}{\beta^{2}}=\frac{4}{3}\quad\mbox{for }\nu=-1\enspace,\\
 & \frac{\alpha^{2}}{\beta^{2}}=2\quad\mbox{for }\nu=0\enspace,\\
 & \frac{\alpha^{2}}{\beta^{2}}=4\quad\mbox{for }\nu=\frac{1}{3}\enspace,\\
 & \frac{\alpha^{2}}{\beta^{2}}\rightarrow\infty\quad\mbox{when }\nu\rightarrow\frac{1}{2}\enspace.
\end{align*}
\end{frame}

\subsection{Ashby chart: $E$ vs. $\rho$}
\begin{frame}{Ashby chart: $E$ vs. $\rho$}
\begin{figure}
\includegraphics[height=6cm]{img/E_vs_density-vector.pdf}
\caption{Ashby chart for Young Modulus vs density. The lines show the sound speed, that is the speed for a wave in a rod made of this
material.  This value is between the longitudinal and shear wave speeds for Poisson ratios in (-0.5,0.5). \cite{ashby2005}}
\end{figure}
\end{frame}

% Solution
\section{Solution}
\subsection{Time domain vs. Frequency domain}
\begin{frame}[allowframebreaks]{Time domain vs. Frequency domain}
If we are interested in waves, we are interested in dynamic behavior. To find the solutions to the (linear) equations  we can use one of two approaches:
\begin{itemize}
\item \textbf{Time domain,} in this case the equations are solved directly.
\item \textbf{Frequency domain,} the solution is expressed as the superposition (sum) of individual waves with different frequencies. This means that the solutions to the equation (and forces) are of the form
\[\bold{u} = \bold{U} \exp(-i\omega t), \text{and } \bold{f} = \bold{F} \exp(-i\omega t)\]
substituting in the original equation we obtain
\begin{equation}
 \alpha^2\nabla(\nabla \cdot \bold{U}) - \beta^2\nabla \times (\nabla \times \bold{U})  + \bold{F} = -\omega^2\bold{U} \enspace ,
\label{eq:navier_freq}
\end{equation}

\pagebreak
Then, we transformed the problem of solving a dynamic equation into solving a set of steady-state problems. For every waveform there is an equivalent function in the frequency domain, i.e., its spectrum.\footnote{This can be formally defined using the Fourier transform, and computed numerically (efficiently) using the FFT algorithm.}
\end{itemize}

\begin{figure}[h]
\centering
\subfloat[Ricker pulse.]{\includegraphics[width=0.4\textwidth]{img/ricker_pulse.pdf}}\qquad
\subfloat[Ricker pulse spectrum.]{\includegraphics[width=0.4\textwidth]{img/ricker_area=2.pdf}}
\caption{Ricker pulse and its spectrum.}

\end{figure}
\end{frame}

\subsection{Numerical methods for waves in solids}
\begin{frame}
\frametitle{Numerical methods for waves in solids}
Exact solutions for the Navier-Cauchy equations are more an exception than the norm. So, we need methods to approximate the solutions (that's the role of numerical methods in general). A rough classification of the methods used in elastodynamics is:
\begin{itemize}
\item Spectral/Pseudo-spectral methods
\item Domain Discretization methods
\begin{itemize}
\item Finite Difference Methods (FDM)
\item Finite Volume Methods (FVM)
\item Finite Element Methods (FEM)
\item Boundary Element Methods (BEM)
\end{itemize}
\end{itemize}
\end{frame}

\subsubsection{Spectral/Pseudo-spectral methods}
\begin{frame}{Spectral/Pseudo-spectral method}
In this class of methods we expand the functions of interest in terms of a (orthogonal) basis \cite{wiki:pseudo_spectral}, i.e.
\[\bold{U}(\bold{x}) = \sum\limits_{n=1}^N c_n h_n(\bold{x}) \enspace ,\]
e.g., we can use a combination of sine functions
\[\bold{U}(\bold{x}) = \sum\limits_{n=1}^N c_n \sin(k_n\bold{x}) \enspace .\]
This methods produce very accurate solutions and present good convergence rates, but are difficult to apply for complex geometries.
\end{frame}

\subsubsection{Domain discretization methods}
\begin{frame}{Domain dizcretization methods}
In this set of methods the domain of interest is subdivided (discretized) to obtain a system of linear/algebraic equations. After applying the discretization process we end up with a system like
\[[K]\lbrace \bold{u}\rbrace + [M]\lbrace \bold{a}\rbrace = \lbrace \bold{f}\rbrace \enspace ,\]
in the time domain, and
\[[K]\lbrace \bold{U}\rbrace - \omega^2 [M]\lbrace \bold{U}\rbrace = \lbrace \bold{F}\rbrace \enspace ,\]
in the frequency domain. In both equations $[K]$,  and $[M]$ are termed stiffness and mass matrices.

\end{frame}

\subsubsection{Finite Difference Methods}
\begin{frame}{Finite Difference Methods}
In FDM the domain is (commonly) decomposed in rectangular regions where the function is constant \cite{wiki:FDM}. The differential equation is approximated with difference equations, e.g.
\[\pardiff{u}{x} \approx \frac{u(x + \Delta x) - u(x)}{h}\]
\begin{figure}
\includegraphics[height=4cm]{img/FDM.pdf}
\caption{Schematic domain discretized in rectangular cells.}
\end{figure}
\end{frame}

\subsubsection{Finite Volume Methods}
\begin{frame}{Finite Volume Methods}
In FVM the geometry is decomposed into regions, denoted as \emph{Finite volumes}. Some quantities are evaluated (as fluxes) at the interface between neighboring \emph{finite volumes}, these methods are conservative. This method is more used in CFD, but it is also popular for wave propagation.\footnote{See for example Clawpack: \url{http://depts.washington.edu/clawpack/}.}
\begin{figure}
\includegraphics[height=4cm]{img/FVM.pdf}
\caption{A finite volume and its surfaces.}
\end{figure}

\end{frame}

\subsubsection{Finite Element Methods}
\begin{frame}{Finite Element Methods}
The FEM is a method that is based on variational principles. It approximates a function in a finite set of points over the domain. To populate the matrices, the domain is split in several subregion called \emph{elements}.

\textcolor{blue}{But I'm sure you are going to talk more about this in the rest of the course}
\begin{figure}
\includegraphics[height=4cm]{img/Piecewise_linear_function2D.pdf}
\caption{A piecewise function represented via {finite elements}. From: \url{https://commons.wikimedia.org/wiki/File:Piecewise_linear_function2D.svg}}
\end{figure}

\end{frame}

\subsubsection{Boundary Element Methods}
\begin{frame}{Boundary Element Methods}
The BEM is similar in formulation to the FEM. The main difference lies in the dimensionality of the mesh, since it only requires the discretization of the contour. This method is really popular in fracture mechanics and wave propagation, the latter due to the capability of represent infinite domains.
\begin{figure}
\includegraphics[height=4cm]{img/BEM.pdf}
\caption{2D domain and the discretization of its contour using Boundary Elements.}
\end{figure}

\end{frame}

\subsection{CFL Condition}
\begin{frame}[allowframebreaks]
\frametitle{CFL Condition}
\justifying
The Courant-Friedrichs-Lewy condition (CFL condition) is a necessary
condition for convergence while solving PDEs by the method of finite
differences \cite{CFL}. It arises when explicit time-marching schemes are used.

The criterion could be stated as
\begin{align*}
C=v_{x}\frac{\Delta t}{\Delta x}\leq C_{max}\qquad\mbox{in 1D}\enspace;\\
C=v_{x}\frac{\Delta t}{\Delta x}+v_{y}\frac{\Delta t}{\Delta y}\leq C_{max}\qquad\mbox{in 2D}\enspace;\\
C=v_{x}\frac{\Delta t}{\Delta x}+v_{y}\frac{\Delta t}{\Delta y}+v_{z}\frac{\Delta t}{\Delta z}\leq C_{max}\qquad\mbox{in 3D}\enspace;
\end{align*}
 Where $v_{x_{i}}$ is the wave speed in the
$x_{i}$ direction, $\Delta x_{i}$ is the minimum spatial discretization
in $x_{i}$ direction, $\Delta t$ is the time step and $C_{max}$
is the maximum allowable value for $C$, which depends on the time
discretization scheme but should be less than 1.

A graphical representation of the criterion is given in Figure \ref{fig:CFL}. Intuitively, we can think about the CFL condition as a limit in the speed for transferring information from one node to its neighbors; this \emph{speed} should be less than the speed for propagation of phenomena in the wave.
\begin{figure}
\centering
\includegraphics[height=4cm]{img/CFLcondition.png}
\caption{Graphic representation of the CFL condition in 1D. \textit{L.A. Barba et al. Practical Numerical Methods with Python, 2014.}}\label{fig:CFL}
\end{figure}

In elastodynamics, and using the the FEM, the criterion could be re-stated as
\begin{align*}
C\leq\alpha\frac{\Delta t}{h}\leq C_{max}\qquad\mbox{in 1D}\enspace;\\
C\leq2\alpha\frac{\Delta t}{h}\leq C_{max}\qquad\mbox{in 2D}\enspace;\\
C\leq3\alpha\frac{\Delta t}{h}\leq C_{max}\qquad\mbox{in 3D}\enspace;
\end{align*}
 where $\alpha$ is the speed for the P-wave and $h$ is the
minimum distance between consecutive nodes. This give us the maximum
allowable timestep as
\begin{align}
\Delta t\leq C_{max}\frac{h}{\alpha}\qquad\mbox{in 1D}\enspace;\\
\Delta t\leq\frac{C_{max}}{2}\frac{h}{\alpha}\qquad\mbox{in 2D}\enspace;\\
\Delta t\leq\frac{C_{max}}{3}\frac{h}{\alpha}\qquad\mbox{in 3D}\enspace.
\end{align}
\end{frame}

\subsection{Nyquist-Shannon sampling criterion}
\begin{frame}[allowframebreaks]
\frametitle{Nyquist-Shannon sampling criterion}
\justifying
The Nyquist--Shannon sampling theorem is a fundamental
result in the field of information theory, in particular telecommunications and signal processing. Sampling is the process of converting a signal (for example, a function of continuous time or space) into a numeric sequence (a function of discrete time or space).
\begin{figure}
\centering
\includegraphics[height=2cm]{img/{CPT-sound-nyquist-thereom-1.5percycle}.pdf}
\caption{The samples of several different sine waves can be identical, when at least one of them is at a frequency above half the sample rate. From: \url{https://commons.wikimedia.org/wiki/File:CPT-sound-nyquist-thereom-1.5percycle.svg}}
\end{figure}

Shannon's version of the theorem states \cite{Shannon}:

\begin{quote}
If a function $x(t)$ contains no frequencies higher than $B$ hertz,
it is completely determined by giving its ordinates at a series of
points spaced $1/(2B)$ seconds apart.
\end{quote}
This theorem implies for us in the numerical simulation of wave propagation
that
\[
h\leq\frac{\lambda}{2}\enspace,
\]
 where $h$ is the maximum distance between consecutive nodes and
$\lambda$ is the shortest wavelength that want to be sampled. So,
the selection of $h$ is commonly
\[
h=\frac{\lambda}{k}\enspace,
\]
 where $k>2$ is a factor that depends on the numerical method. For
finite element methods $k$ is commonly 10.
\end{frame}

\begin{frame}[allowframebreaks]{References}
\def\newblock{}
\bibliographystyle{plain}
\begin{thebibliography}{1}

\bibitem{book:arfken} George B. Arfken \& Hans. J. Weber. Mathematical Methods for Physicists. Elsevier Academic Press, 6th Edition, San Diego, 2005.

\bibitem{ashby2005} Ashby, Michael F. ``Materials
selection in mechanical design." MRS BULLETIN 30 (2005): 995.

\bibitem{CFL} Courant, R.; Friedrichs, K.; Lewy, H. (1928), \emph{\"Uber
die partiellen Differenzengleichungen der mathematischen Physik} (in
German), Mathematische Annalen 100 (1): 32--74.

\bibitem{Shannon} C. E. Shannon, \emph{Communication in the presence
of noise}, Proc. Institute of Radio Engineers, vol. 37, no. 1, pp.
10--21, Jan. 1949. Reprint as classic paper in: Proc. IEEE, vol. 86,
no. 2, (Feb. 1998).

\bibitem{book:waves-rays} Michael A. Slawinski. Waves and rays in Elastic Continua. Second Edition, 2007.

\bibitem{wiki:FDM} Finite difference method. (2014, April 29). In Wikipedia, The Free Encyclopedia. Retrieved 00:11, September 25, 2014, from \url{http://en.wikipedia.org/w/index.php?title=Finite_difference_method&oldid=606345831}

\bibitem{wiki:FVM} Finite volume method. (2014, April 22). In Wikipedia, The Free Encyclopedia. Retrieved 00:44, September 25, 2014, from \url{http://en.wikipedia.org/w/index.php?title=Finite_volume_method&oldid=605282055}

\bibitem{wiki:ondas} Wikipedia community. Onda (f\'isica) [on line]. Wikipedia, La enciclopedia libre, 2010 ; 22 November 2010.

\bibitem{wiki:pseudo_spectral} Pseudo-spectral method. (2014, March 14). In Wikipedia, The Free Encyclopedia. Retrieved 23:12, September 24, 2014, from \url{http://en.wikipedia.org/w/index.php?title=Pseudo-spectral_method&oldid=599542680}

\end{thebibliography}

\end{frame}

\end{document}


%*******************************************
% Lab 03: Priority Encoder
%*******************************************
\chapter{Priority Encoder}

\section{Purpose}

Often a circuit will receive data from several sources at one time and there must be a way to prioritize those inputs. This circuit creates a simple priority encoder for nine different inputs. This is a fairly simple circuit but is best explained by building and ``playing around'' with it rather than attempting to understand a printed text; thus, the explanation for this lab is somewhat limited.

\section{Procedure}

Start \LE and create a subcircuit named \lstinline[columns=fixed]|Encoder|. Open that subcircuit and place 12 \texttt{AND} gates as illustrated in Figure \ref{fig:03-01}.

\begin{figure}[H]
	\centering
	\includegraphics[width=\maxwidth{.95\linewidth}]{gfx/03-01}
	\caption{AND Gates}
	\label{fig:03-01}
\end{figure}

The gates have one data bit and these properties:

\begin{itemize}
	\item \textbf{U1}: Five inputs, numbers two, three, and four negated.
	\item \textbf{U2}: Four inputs, numbers two and three negated.
	\item \textbf{U3}: Three inputs, number two negated.
	\item \textbf{U4}: Two inputs, none negated.
	\item \textbf{U5}: Four inputs, numbers two and three negated.
	\item \textbf{U6}: Four inputs, numbers one and two negated.
	\item \textbf{U7-U12}: Two inputs, none negated.
\end{itemize}

Many of the output signals need to be combined with \texttt{OR} gates and those should be added next, as in Figure \ref{fig:03-02}. Note: U16 is a \texttt{NOR} (\textit{Gates} library) gate.

\begin{figure}[H]
	\centering
	\includegraphics[width=\maxwidth{.95\linewidth}]{gfx/03-02}
	\caption{OR Gates Added}
	\label{fig:03-02}
\end{figure}

This encoder is designed to prioritize nine input lines so nine inputs must be added, as illustrated in Figure \ref{fig:03-03}.

\begin{figure}[H]
	\centering
	\includegraphics[width=\maxwidth{.95\linewidth}]{gfx/03-03}
	\caption{Inputs Added}
	\label{fig:03-03}
\end{figure}

Wiring this circuit is the most challenging part of the build. As illustrated in Figure \ref{fig:03-04}, the inputs are wired to several different \texttt{AND} gates.

\begin{figure}[H]
	\centering
	\includegraphics[width=\maxwidth{.95\linewidth}]{gfx/03-04}
	\caption{Wiring the Encoder}
	\label{fig:03-04}
\end{figure}

Finally, four output ports are added, as illustrated in Figure \ref{fig:03-05}.

\begin{figure}[H]
	\centering
	\includegraphics[width=\maxwidth{.95\linewidth}]{gfx/03-05}
	\caption{Nine-line Priority Encoder}
	\label{fig:03-05}
\end{figure}

This circuit is designed to output a \acf{BCD} number, so no further conversion is needed to be able to read the highest priority input line. At this point, the circuit is complete and the \textit{poke} tool can be used to change the inputs and observe how that high input bit drives the outputs.

To finish the project, open the \lstinline[columns=fixed]|main| circuit and drop the \lstinline[columns=fixed]|Encoder| on the drawing canvas. Add nine inputs and label them \textit{In1} through \textit{In9}. Place a four-bit output labeled \textit{PriOut} and wire the four outputs through a splitter to that output port. To make it easier to read the \ac{BCD} number, connect a Hex Digit Display (\textit{Input/Output} library) to the four-bit bus between the splitter and output port. The completed \lstinline[columns=fixed]|main| circuit is illustrated in Figure \ref{fig:03-06}.

\begin{figure}[H]
	\centering
	\includegraphics[width=\maxwidth{.95\linewidth}]{gfx/03-06}
	\caption{Main Circuit}
	\label{fig:03-06}
\end{figure}

In Figure \ref{fig:03-06}, notice that two inputs are selected, \textit{In4} and \textit{In6}. Since \textit{In6} is a higher priority (it is a larger number), the output is set for six and \textit{In4} is ignored.

\subsection{Testing the Circuit}

The circuit is now complete. It should be tested by entering various combinations of inputs and observing that the output always displays the highest numbered input.

\section{Deliverable}

To receive a grade for this lab, create the Nine-line Priority Encoder circuit as defined in this lab. Be sure the standard identifying information is at the top left of the circuit, similar to this:

\bigskip
% The minipage environment keeps the three lines together - no page break.
\begin{minipage}{\linewidth}
	\begin{verbatim}
	George Self
	Lab 03: Nine-line Priority Encoder
	February 18, 2018
	\end{verbatim}
\end{minipage}
\bigskip

Save the file with this name: \emph{\texttt{Lab03\_Encoder}} and submit that file for grading.