%!TEX root = /home/renaud/Documents/EPL/tfe/latex/tfe.tex
\addcontentsline{toc}{section}{List of symbols}
\section*{List of Symbols}
Unless otherwise stated, bold lower cases indicate column vectors ($\b x$) and bold upper cases indicate matrices ($\b X$). The entries of a vector are denoted by a single subscript ($x_i$) and the entries of a matrix are denoted by brackets with a double subscript ($[\b X]_{i,j})$. The transposed sign is $^\t$.

\noindent As a general rule, subscript $t$ indicates a discrete-time quantity evaluated at time $t$, while parenthesis indicate a continuous-time quantity.

\noindent The time derivative of a quantity $x$ is denoted by the usual convention $\partial x/\partial t$, or by $\dot{x}$.
\subsection*{Clustering}
\begin{table}[H]
\begin{tabular}{ll}
	$n$ & Number of nodes of the graph.\\
	$\b A$ & Weighted adjacency matrix.\\
	$q_i$ & Total outgoing weight from node $i$ ($i \in \{1,\dots,n\}$).\\
	$\b q$ & Vector of the $q_i$'s.\\
	$\b Q$ & Matrix with the total outgoing weights on its diagonal: $\b Q = \diag(\b q)$.\\
	$\b M$ & Transition probability matrix for the discrete-time Markov process.\\
	$\b P(t)$ & Transition matrix for the continuous-time Markov process evaluated at time $t$.\\
	$p_{t,i}$ & Probability to be in node $i$ at Markov time $t$ ($i \in \{1,\dots,n\}$).\\
	$\b p_t$ & Discrete-time probability vector: $\b p_t = [p_{t,1},\dots,p_{t,n}]$.\\
	$\b p(t)$ & Continuous-time probability vector.\\
	$\bs \pi$ & Stationary distribution vector.\\
	$\bs \Pi$ & Matrix with the stationary distributions on its diagonal: $\bs \Pi = \diag(\bs \pi)$.\\
	$\b x_t$ & Random indicator vector at time $t$.\\
	$\b C(\b x_{t_0},\b x_{t_0+t})$ & Autocovariance matrix of $\b x$.\\
	$\P$ & A partition.\\
	$c$ & Number of communities.\\
	$\C_i$ & The $i$th community of the partition ($i \in \{1,\dots,c\}$).\\
	$\b H_{\P}$ & Indicator matrix of $\P$.\\
	$\mathcal{H}_{\P}(\cdot)$ & Operator $\b H_\P^\t (\cdot) \b H_\P$.\\
	$\b y_t$ & Community indicator vector at time $t$.\\
	$\b R_t(\P)$ & Discrete-time clustered autocovariance matrix for partition $\P$ at time $t$.\\
	$\b R(t;\P)$ & Continuous-time clustered autocovariance matrix for partition $\P$ at time $t$.\\
	$r_t(\P)$ & Discrete-time stability of partition $\P$.\\
	$r(t;\P)$ & Continuous-time stability of partition $\P$.\\
	$r_t$ & Discrete-time stability curve.\\
	$r(t)$ & Continuous-time stability curve.\\
	$\tau$ & Teleportation probability.\\
	$\lambda_i(\b q)$ & Rate at which continuous-time random walkers leave node $i$ ($i \in \{1,\dots,c\}$).\\
	$\bs \lambda(\b q)$ & Vector of the $\lambda_i(\b q)$'s.\\
	$\b L$ & Laplacian matrix.\\
	$\VI(\P_1,\P_2)$ & Variation of information between partitions $\P_1$ and $\P_2$.
\end{tabular}
\end{table}
\newpage
\subsection*{Transport model}
Adimensional variables are denoted by primes.
\begin{table}[H]
\begin{tabular}{ll}
	$C$ & Concentration function.\\
	$C(\b x,t)$ & Concentration function with its dependence on position and time written\\ & explicitly.\\
	$q$ & Source/sink term, or reactive term.\\
	$\b u$ & Velocity vector.\\
	$\b K$ & Diffusivity tensor (symmetric and positive-definite).\\
	$\b x$ & Position vector. In two dimensions, $\b x = (y,z)$.\\
	$\rho$ & Density of the mixture (seawater).\\
	$\rho_w$ & Density of pure water.\\
	$\Omega$ & Domain of the problem (time independent).\\
	$\partial \Omega$ & Boundary of $\Omega$.\\
	$|\Omega|$ & Volume (area in two dimensions) of $\Omega$.\\
	$\n$ & Outward unit normal vector.\\
	$[\cdot]_{*}$ & Quantity $\cdot$ evaluated at $*$.\\
	$\bar{C}(t)$ & Mean concentration over $\Omega$ that possibly depends on $t$.\\
	$\bar{C}$ & Mean concentration over $\Omega$, shown to be constant.\\
	$\hat{C}(\b x,t)$ & Deviation of the concentration with respect to $\bar{C}$.\\
	$\sigma^2(t)$ & Variance of the concentration function.\\
\end{tabular}
\end{table}

\subsection*{Compartment model}
In what follows, $i,j \in \{1,\dots,N\}$.
\begin{table}[H]
\begin{tabular}{ll}
	$N$ & Number of subdomains (or compartments).\\
	$\Omega_i$ & Subdomain $i$.\\
	$|\Omega_i|$ & Volume (area in two dimensions) of $\Omega_i$.\\
	$\Gamma_{i,j}$ & Interface between $\Omega_i$ and $\Omega_j$.\\
	$\Gamma_{i,e}$ & Interface between $\Omega_i$ and the environment.\\
	$|\Gamma_{i,j}|$ & Area (length in two dimensions) of $\Gamma_{i,j}$.\\
	$C_i(t)$ & Concentration function for compartment $i$.\\
	$q_i(t)$ & Net production rate over $\Omega_i$.\\
	$U_{i,j}$ & Advective "flux" from compartment $i$ to compartment $j$.\\
	$V_{i,j}$ & Diffusive "flux" from compartment $i$ to compartment $j$.\\
	$\sigma_{box}^2(t)$ & Box variance.\\
	$\b c$ & Vector of the compartment's concentrations: $\b c = [C_1(t),\dots,C_N(t)]^\t$.\\
	$\b q$ & Vector of the $q_i$'s: $\b q = [q_1(t),\dots,q_N(t)]^\t$.\\
	$\bs \omega$ & Vector of the $|\Omega_i|$'s: $\bs \omega = [|\Omega_1|,\dots,|\Omega_N|]^\t$.\\
	$\bs \Omega$ & Matrix with $\bs \omega$ on its diagonal: $\bs \Omega = \diag (\bs \omega)$.\\
	$\b A$ & Interaction matrix.\\
	$\b A_{\Delta t}$ & Discrete interaction matrix for time step $\Delta t$.\\
	$P_i(t)$ & Number of tracer's particles in compartment $i$ at time $t$.\\
	$P$ & Mass of one tracer's particle.\\
\end{tabular}
\end{table}

\subsection*{SDEs}
\begin{table}[H]
\begin{tabular}{ll}
	$\delta(t)$ & Dirac delta function.\\
	$\xi(t)$ & White noise.\\
	$W(t)$ & Wiener process.\\
	$\I$ & Indicate an Itô SDE or integral.\\
	$\bI$ & Indicate a backward Itô SDE or integral.\\
	$\esp{\cdot}$ & Expectation.\\
	$p$ & Probability density function.\\
	$p(x,t;y,s)$ & Probability density function with its dependence on position and time,\\& and the initial condition written explicitly.\\
	$X_i$ & Numerical approximation of $x(t_i)$.\\
	$\Delta W_i$ & $W(t_i)-W(t_{i-1})$.\\
	$\Delta t_i$ & $t_i-t_{i-1}$.\\
\end{tabular}
\end{table}

\subsection*{From clusters to compartments}
\begin{table}[H]
\begin{tabular}{ll}
	$\nby$ & Number of grid cells in the $y$-direction.\\
	$\nbz$ & Number of grid cells in the $z$-direction.\\
	$N_{cell}$ & Total number of grid cells.\\
	$m_{i,j}(T)$ & Probability that a particle ends up in grid cell $j$ after a time $T$\\ & if it was initially in grid cell $i$.\\
	$\b M(T)$ & Adjacency matrix of the graph at time $T$, which is also the transition\\ & probability matrix.\\
	$P_{i \rightarrow j}(T)$ & Number of particles in grid cell $j$ at time $T$, which were initially in grid cell $i$.\\
	$P_0$ & Number of particles released in each grid cell.\\
\end{tabular}
\end{table}


%  -----------------------------------------------------------------------------
%  Author         : Bimalka Piyaruwan Thalagala
%  GitHub         : https://github.com/bimalka98
%  Date Created   : 01.09.2020
%  Last Modified  : 06.03.2020
%  -----------------------------------------------------------------------------

\documentclass[a4paper,11pt]{article}%,twocolumn
\input{settings/packages}
\input{settings/page}
\input{settings/macros}
\usepackage[ framed, numbered]{matlab-prettifier}%framed,%
\usepackage{listings}
\usepackage{physics}
\usepackage{pdfpages}
\usepackage[toc,page]{appendix}

\begin{document}
\input{content/title_page}

\begin{abstract}
	Design procedure of a Finite Duration Impulse Response(FIR) bandpass Digital Filter which satisfies a set of prescribed specifications, is described in this report where windowing method in conjunction with the Kaiser window is used for the designing procedure. Operation of the filter was analyzed with a combination of sinusoidal signals. The design was implemented and tested using {\tt MATLAB R2018a} of the MathWorks Inc.
\end{abstract}

\pagebreak

\tableofcontents
\listoffigures
\listoftables
\vfill
\begin{center}
	\textbf{\textit{*PDF is clickable}}
\end{center}

\textit{\textbf{Note:}}\\
\textit{All the materials and executable {\tt MATLAB R2018a} Live Script related to the project can also be found at \url{https://github.com/bimalka98/Digital-Signal-Processing}}
\pagebreak

\section{Introduction}

This report describes the design procedure of an FIR bandpass digital filter which satisfies a predefined set of specifications as shown in the Table \ref{filterspecs}.\textbf{\textit{ Kaiser windowing}} method is used, due to its excellent capability to control filter's ripple ratio  and main-lobe width to facilitate a given set of specification by simply varying the related parameters. And most importantly a method is available to calculate those parameters through empirical formulae. Therefore this Kaiser Window is heavily used to design filters with prescribed specifications.\\

{\tt MATLAB R2018a} of the MathWorks Inc. is used to implement and analyze the digital filter. Filtering was done in the frequency domain rather than in the time domain since the time domain convolution is computationally expensive. Frequency domain analysis was done using \textbf{\textit{Fast Fourier Transform}} algorithms(\textit{using built-in {\tt fft()} and {\tt ifft()} functions}). Performance of the filter was analyzed using a combination of sinusoidal signal, whose frequency components lie in the middle of the three main regions(\textit{lower stopband, passband and upper stopband}) of the bandpass filter.

\section{Method}

First the digital filter is implemented through the procedure described in the \textbf{section 2.1}. Then its performance is analyzed as described in the \textbf{section 2.2}.

\subsection{Filter Implementation}
Digital filter implementation consists of the steps that are mentioned below. Subsections of this section of the report describes each one of them thoroughly for designing the FIR bandpass filter using Kaiser Window method.

\begin{enumerate}[\hspace{1cm}1.]
	\item Identifying the prescribed filter specifications
	\item Derivation of the filter Parameters
	\item Derivation of the Kaiser Window Parameters
	\item Derivation of The Ideal Impulse Response
	\item Truncating the Ideal Impulse Response to obtain Finite Impulse Response i.e Windowing
\end{enumerate}

\subsubsection{Prescribed Filter specifications}
Following table describes the desired specifications of the bandpass filter which need to be implemented. The notation used here is the same as the notation used in the reference material\cite{antonio} and they will be used throughout the report rather than numerical values.

\begin{table}[!h]
	\centering

	\begin{tabular}{l c r}
		\hline
		\textbf{Parameter}& \textbf{Symbol}&\textbf{Value}\\\hline
		&&\\
Maximum passband ripple(\textit{desired})&$\tilde{A_p}$&0.09 dB\\
Minimum stopband attenuation(\textit{desired})&$\tilde{A_a}$&48 dB\\
Lower passband edge&$\omega_{p1}$&400 rad/s\\
Upper passband edge&$\omega_{p2}$&800 rad/s\\
Lower stopband edge&$\omega_{a1}$&250 rad/s\\
Upper stopband edge&$\omega_{a2}$&900 rad/s\\
Sampling frequency&$\omega_s$&2600 rad/s\\
\hline\hline
	\end{tabular}
	\caption{Prescribed Filter specifications}
	\label{filterspecs}
\end{table}

Following Fig.\ref{idealbpfilter} illustrates the aforementioned specifications graphically for an idealized frequency response of a Bandpass filter. $\delta$ in the figure has the following relationship with peak to peak passband ripple(\textit{practical}) $A_p$ and the minimum stopband attenuation(\textit{practical}) $A_a$.\\

\begin{equation}
	\tilde{A_p} \ge A_p = 20\log(\frac{1+\delta}{1-\delta})
	\label{A_p}
\end{equation}
\begin{equation}
 \tilde{A_a} \le A_a = -20\log(\delta)
 \label{A_a}
\end{equation}

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.4]{figures/filtersepecs}
	\caption{Idealized frequency response of a Bandpass filter\cite{antonio}}
	\label{idealbpfilter}
\end{figure}

\subsubsection{Derivation of filter Parameters}

According to the given specifications following parameters are calculated; which then will be used to derive the parameters of the Kaiser window.

\begin{table}[!h]
	\centering
\begin{tabular}{l c c r}
	\hline
\textbf{Parameter}& \textbf{Symbol}& \textbf{Calculation}&\textbf{Value}\\\hline
&&&\\
Lower transition width& $B_{t1}$& $\omega_{p1} - \omega_{a1}$&150 rad/s\\
Upper transition width& $B_{t2}$&$\omega_{a2}-\omega_{p2}$&100 rad/s\\
Critical transition width& $B_t$&$\min(B_{t1},B_{t2})$&100 rad/s\\
Lower cutoff frequency& $\omega_{c1}$&$\omega_{p1}-B_t/2$&350 rad/s\\
Upper cutoff frequency& $\omega_{c2}$&$\omega_{p2}+B_t/2 $&850 rad/s \\
Sampling period& $T$& $2\pi / \omega_s$&$0.0024$ s\\
\hline\hline
\end{tabular}
\caption{Derivation of filter Parameters}
\end{table}

\subsubsection{Derivation of the Kaiser Window Parameters}

Following equation represents the Kaiser window which will be used to truncate the Infinite duration Impulse Response to obtain the Finite duration Impulse Response for our filter design. Further explanations of the same steps can be found in the \textit{sections 9.4.5} and \textit{9.4.6} in the reference material\cite{antonio}.

\begin{equation}
w_K(nT) = \begin{cases}
	\frac{I_0(\beta)}{I_0(\alpha)} & for \abs{n}  \leq \frac{N-1}{2} \\
	0 & Otherwise
	\label{kaiser}
\end{cases}
\end{equation}

where $\alpha$ is an independent parameter and $I_0(x)$ is the zeroth-order modified Bessel function of the first kind.\\
%372
\[
\beta = \alpha\sqrt{1-\left(\frac{2n}{N-1}\right)^2} \hspace*{1cm} \hspace*{1cm} I_0(x) = 1+\sum_{k=1}^{\infty}\left[\frac{1}{k!}\left(\frac{x}{2}\right)^k\right]^2
 \]

Now we have to calculate the required parameters as follows,

\begin{enumerate}[\hspace{1cm}a.)]
\item Choose $\delta$ in Eqs. \eqref{A_p} and \eqref{A_a} such that $\delta = \min(\tilde{\delta_p}, \tilde{\delta_a})$ where,

\[
\begin{split}
	\tilde{\delta_p} &= \frac{10^{0.05\tilde{A_p}}-1}{10^{0.05\tilde{A_p}}+1}\\
	&=\frac{10^{0.05*0.09}-1}{10^{0.05*0.09}+1}\\
	&=5.181\times10^{-3}
\end{split}
\hspace{2cm}
\begin{split}
	\tilde{\delta_a} &= 10^{-0.05\tilde{A_a}}\\
&=3.981\times10^{-3}
\end{split}
\]
\begin{center}
	$\therefore~ \delta = 3.981\times10^{-3} $
\end{center}

\item With the required $\delta$ defined, the actual stopband loss(attenuation) $A_a$ in dB can be calculated using Eq. \ref{A_a}.
\[
\begin{split}
	 \tilde{A_a} \le A_a &= -20\log(\delta)\\
	 &=-20\log(3.981\times10^{-3})\\
	A_a &= 48 ~dB
\end{split}
\]

\item Choose parameter $\alpha$ as,
    \[
\alpha = \begin{cases}
	0 & for~  A_a \leq 21 ~dB \\
	0.5842(A_a - 21)^{0.4} + 0.07886(A_a - 21) & for ~ 21 < A_a \leq 50 ~dB \\
	0.1102(A_a - 8.7) & for~  A_a > 50 ~dB
\end{cases}
\]

\[
\begin{split}
\therefore~ \alpha &= 0.5842(A_a - 21)^{0.4} + 0.07886(A_a - 21)\\
&=	0.5842(48 - 21)^{0.4} + 0.07886(48 - 21)\\
&=4.3125
\end{split}
\]

\item Choose parameter D as,

\[
D = \begin{cases}
	0.9222 & for ~ A_a \leq 21 ~dB \\

	\frac{A_a - 7.95}{14.36} & for ~ A_a > 21 ~dB
\end{cases}
\]\hspace{2cm}
\[
\begin{split}
	\therefore~ D &= \frac{A_a - 7.95}{14.36}\\
	&=\frac{48 - 7.95}{14.36}\\
	&=  2.7890
\end{split}
\]

\item Then select the lowest odd value of N that would satisfy the inequality,
\[
\begin{split}
	N  & \geq \frac{\omega_sD}{B_t}+1\\
	&\geq \frac{2600*2.79}{100}+1\\
	&\geq 73.51
\end{split}
\hspace{2cm}
\therefore~	N = 75
\]
\end{enumerate}

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.4]{figures/kaisertime}
	\caption{Derived Kaiser Window $w_K(nT)$ using calculated Parameters and Eq. \eqref{kaiser}}
\end{figure}

\subsubsection{Derivation of The Ideal Impulse Response}

\textbf{\textit{Note : Here subscript `d' implies ``desired'', as it is the ideal response of the filter. Subscript d will be omitted to indicate a given expression is no longer ideal.\\}}

The frequency response of an ideal bandpass filter with cutoff frequencies $\omega_{c1}$ and $\omega_{c2}$ is given by,

\[
H_d(e^{j\omega T}) = \begin{cases}
1& for~ -\omega_{c2} \le \omega \le -\omega_{c1}\\
1& for ~~~~~\omega_{c1} \le \omega \le \omega_{c2}\\
0& Otherwise
\end{cases}
\]

Using the Inverse Fourier Transform, impulse response of the above $H(e^{j\omega T})$ is calculated.
\[
\begin{split}
	h_d(nT) &=\frac{1}{\omega_s}\int_{-\omega_s/2}^{\omega_s/2} H(e^{j\omega T})e^{j\omega nT}~d\omega\\
	&=\frac{1}{\omega_s}\left[\int_{-\omega_{c2}}^{-\omega_{c1}} e^{j\omega nT} ~d\omega+ \int_{\omega_{c1}}^{\omega_{c2}} e^{j\omega nT}~ d\omega\right]\\
	&=\frac{1}{\omega_s} \left[ \left.\frac{e^{j\omega nT}}{jn T}\right|_{-\omega_{c2}}^{-\omega_{c1}} + \left.\frac{e^{j\omega nT}}{jn T}\right|_{\omega_{c1}}^{\omega_{c2}} \right]\\
	&= \frac{1}{j\omega_s nT} \left[  e^{-j\omega_{c1}nT} - e^{-j\omega_{c2}nT}  +  e^{j\omega_{c2}nT} - e^{j\omega_{c1}nT}  \right] ; where~\omega_sT = 2\pi\\
	&= \frac{1}{\pi n}\left[ \frac{(e^{j\omega_{c2}nT} - e^{-j\omega_{c2}nT})}{2j} - \frac{(e^{j\omega_{c1}nT} - e^{-j\omega_{c1}nT})}{2j}  \right] ; rearanging\\
	&=	\frac{1}{\pi n}\left[   \sin(\omega_{c2}nT) - \sin(\omega_{c1}nT)  \right]; from~ Euler's ~Eq.
\end{split}
\]

\begin{equation}
	\therefore ~ h_d(nT) = \begin{cases}
		\frac{1}{\pi n}\left[   \sin(\omega_{c2}nT) - \sin(\omega_{c1}nT)  \right] & \forall n \ne 0\\
		&\\
		\frac{2}{\omega_s}\left( \omega_{c2} - \omega_{c1}\right)& for~ n = 0
	\end{cases}
\label{idealresponse}
\end{equation}

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.4]{figures/ideal-impulse}
	\caption{Anti-Causal Ideal Impulse Response $h_d(nT)$}
\end{figure}

\subsubsection{Truncating the Ideal Impulse Response to obtain a Finite Impulse Response}

By multiplying the ideal impulse response $h_d(nT)$ in Eq. \eqref{idealresponse} with the Kaiser window $w_K(nT)$ in Eq. \eqref{kaiser}, the ideal infinite impulse response can be truncated to obtain the finite impulse response $h(nT)$ for practical implementation.
\begin{equation}
h(nT) = 	w_K(nT).h_d(nT)
\end{equation}

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.4]{figures/fir-truncated}
	\caption{Anti-Causal Finite Impulse Response $h(nT)$}
	\label{firimp}
\end{figure}

Obtaining the transfer function in the $\mathcal{Z}$ domain using $\mathcal{Z}$ Transformation,
\begin{equation}
	\begin{split}
			H'(z) &= \mathcal{Z}\left\{ h(nT) \right\}\\
		&=\mathcal{Z}\left\{ w_K(nT).h_d(nT) \right\}
	\end{split}
\end{equation}

The response is anti-causal it need to be shifted in the time domain to make it causal and get the final filter response$H(Z)$, in $\mathcal{Z}$ domain it is represented as follows. Then the equation is evaluated at $e^{j\omega}$ to get the frequency response of the filter
\begin{equation}
	H(z)= z^{-\left(\frac{N-1}{2}\right)}.H'(z)
\end{equation}

\subsection{Filter Performance Evaluation}

Performance of the filter was evaluated by using the following excitation $x(nT)$ which is a combination of three sinusoidal signals. Frequencies of these three sinusoidal signals are specified as follows to cover all three bands(\textit{lower stopband, passband and upper stopband}) in the bandpass filter.

\[x(nT) = \sum_{i=1}^{3}sin(w_inT)\]

\begin{table}[!h]
	\centering

	\begin{tabular}{l c c r}
		\hline
	\textbf{Parameter}& \textbf{Symbol}& \textbf{Calculation}&\textbf{Value}\\\hline
	&&&\\
	Middle frequency of the lower stopband& $\omega_1$&$\frac{0+ \omega_{a1}}{2}$&125 rad/s\\
	Middle frequency of the passband &$\omega_2$&$\frac{\omega_{p1}+\omega_{p2}}{2}$&600 rad/s\\
	Middle frequency of the upper stopband& $\omega_3$&$\frac{\omega_{a2}+\omega_s/2}{2}$&1100 rad/s\\
	\hline\hline
	\end{tabular}
\caption{Frequencies for Filter Performance Evaluation}
\end{table}

The required filtered output can be obtained by time domain convolution of the input signal $x(nT)$ with the impulse response $h(nT)$ in Fig. \ref{firimp}. But as mentioned previously the convolution is computationally expensive operation. Therefore filtering is done in the frequency domain which then simplifies into a simple multiplication. For that Discrete Fourier Transform(DFT) of the input signal $x(nT)$ and DFT of $h(nT)$ are obtained through the {\tt fft()} function in MATLAB. Then they are multiplied to get the frequency domain output and {\tt ifft()} is applied on that to get the time domain output signal.

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.4]{figures/in-signal}
	\caption{Input Signal and the Desired Output Signal}
\end{figure}

\pagebreak
\section{Results}

\subsection{FIR Filter Characteristics}

Filter Characteristics showed in this section was obtained through the procedure explained in the \textit{section 2.1 Filter Implementation}.\\

Anti-Causal Impulse Response of the Filter can be right shifted in time domain to obtain the Causal Impulse Response as shown below. Note that there, the samples(n) axis starts from 0 and continues up to 74. That is N-1. Whereas previously it was from -37 to 37.

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.4]{figures/causal-impulse-response}
	\caption{Causal Impulse Response of the Filter}
\end{figure}
\vfill
Magnitude Response of the Filter was obtained through the MATLAB's {\tt freqz()} function which computes the frequency, magnitude, and phase response of given digital filter(\textit{impulse response}).
\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.4]{figures/magnitude-response}
	\caption{Magnitude Response of the Filter}
\end{figure}

\pagebreak
Magnitude Response of the Filter for the frequencies in the Passband can be simply obtained through limiting the range of Angular Frequency axis using MATLAB's {\tt xlim([$\omega_{p1}$ $\omega_{p2}$])} function. It allows us to identify the nature of the passband  ripples of a kaiser window of order 75. According to the prescribed specifications Maximum passband ripple(\textit{desired})  $\tilde{A_p}$ was 0.09 dB. And we can observe that the designed filter has achieved that goal.

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.4]{figures/magnitude-resp-passband}
	\caption{Magnitude Response of the Filter for the frequencies in the Passband}
\end{figure}

\pagebreak
\subsection{Input Output Signal Characteristics}

As explained in the \textbf{Filter Performance Evaluation} section input signal is a combination of three sinusoidal signals which has its frequency components lie in the three bands of the bandpass filter. This fact is clearly visible in the frequency spectrum of the Input Signal.

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.45]{figures/input-signal}
	\caption{Input signal representation in Time and Frequency Domains}
\end{figure}
\vfill
By inspecting the frequency spectrum of the output signal we can observe that output signal is a filtered version of the input signal and that the correct frequencies have been passed through the filter perfectly. Because both the frequencies that were in the lower and upper stop bands are filtered out and disappeared completely from the frequency spectrum of the output signal. This guarantees that the Kaiser window method has  yield an almost ideal bandpass filter which satisfies the prescribed specifications.

\begin{figure}[!h]
	\centering
	\includegraphics[scale=0.45]{figures/output-signal}
	\caption{Output signal representation in Time and Frequency Domains}
\end{figure}
\pagebreak

\section{Conclusions}

Depending on the results it can be concluded that the Kaiser Window method has yielded a near ideal passband filter characteristics which satisfies all the prescribed specifications. Even though there are some ripples in the passband and the stopband, the filter characteristics can be optimized without much effort through tuning the parameters ($\alpha$ and $N$) to obtain perfect and at the same time practical filter for a given application. In addition to that computational complexity of the parameters which are used to obtain the Kaiser Window is also much less and most importantly there is a clear method to obtain those parameters through empirical formulae.\\

But when a filter is practically implemented in hardware, usage of the computational resources plays a vital role and as the order of the filter grows resource requirement grows drastically. Therefore,  as a major disadvantage of the Kaiser Window method, the high order of the filters can be pointed out. Filter of order 75 was required to achieve the given specs in our case while there are other filter designing methods which yield the same characteristics with much lower order and therefore much lower resource requirements.

\vfill
\hrule
\vspace{0.5cm}
\bibliographystyle{plain}
\bibliography{refer}

\vspace{1cm}
\hrule
\vspace{0.5cm}
\begin{appendices}
	\section{Matlab Code for the Implementation}
	\includepdf[pages=-]{code/bandpass180631J.pdf}
\end{appendices}

%---------------------------------------------------------------------------
\end{document}
-

Project Report Template/Project Report Template.tex

FIR Filter Design-Project Friday, 5 March 2021/LaTeX Report/180631J-Project-EN2570.tex


\section{Cleavage algorithm}

A grain (crystal) is described by its rotation
tensor, $\tenrtwo{R}^c$, with the usual meaning:
a vector in the crystal coord. system, $x^c$,
is transformed into a vector in spatial (cellular) coord. system,
$x^s$, as

\begin{equation}
 x^{s}
  =
   \tenrtwo{R}^{c} \cdot x^{c}
 \label{eq:cgca:pub}
\end{equation}

It is assumed that the cleavage is controlled by
$\sigma_1$, the max principal stress.
If $\sigma_1^s$ is the max principal stress vector
in the spatial coord. system, then
$\sigma_{1}^{c} = \tenrtwo{R}^{s} \cdot \sigma_{1}^{s}$
is the max principal stress vector in the crystal
coord. system.
Here:

\begin{equation}
 \tenrtwo{R}^{s}
 \equiv
 \tenrtwo{R}^{-c}
  \equiv
   \left (
           \tenrtwo{R}^{c}
   \right)^{-1}
    \equiv
     \left (
             \tenrtwo{R}^{c}
     \right)^{T}
\label{eq:cgca:wonk}
\end{equation}

Each crystal plane, \{hkl\}, has a partucular
surface energy, $\gamma_{hkl}$.
We postulate that the work of cleavage
is equal to the surface energy.
The work of cleavage is $\sigma_{hkl}$ times the distance necessary
to break the atomic bonds.
Following the ideas of \cite{gilman1959},
we take this distance equal to $a_0$, the
relaxation distance, which is the atom
diameter in the cleavage plane.
So the cleavage condition will be

\begin{equation}
 \sigma_{hkl}
   a_0
    =
     \gamma_{hkl}
\label{eq:cleav}
\end{equation}

from which the stress required to
cleave \{hkl\} plane is

\begin{equation}
 \sigma_{hkl}
  =
   \frac{ \gamma_{hkl}} { a_0}
\label{eq:muk}
\end{equation}

From \cite{gilman1959}, $\gamma_{100}$=1440,
$\gamma_{110}$=1710 and $\gamma_{111}$=5340erg/cm$^2$
(1erg/cm$^2$ = 10$^{-3}$J/m$^2$) and
$a_0=1.37 \times 10^{-10}$m.
This gives:
$\sigma_{100}=1.05 \times 10^4$MPa,
$\sigma_{110}=1.25 \times 10^4$MPa,
$\sigma_{111}=4.90 \times 10^4$MPa.
These values must be scaled, of course, because
the CA model is not on the atomic scale and because
material imperfections lower these stresses dramatically.
Anyway, the important factor is that we can differentiate
between different cleavage planes based on their
surface energies.

In BCC crystals there are 24 symmetric rotation tensors,
$\tenrtwo{R}_{sym}^{1\ldots 24}$,
including the identity tensor,
So, if $n_{hkl}$ is a unit normal vector to some \{hkl\} plane,
then $n_{hkl}^{1\ldots 24} =
\tenrtwo{R}_{sym}^{1\ldots 24} \cdot n_{hkl}$ are 24 normal
vectors describing all planes of the same class.

It is useful to split the maximum principal stress vector,
$\sigma_{1}^{c}$, into the magnitude, $||\sigma_{1}^{c}||$,
and the unit direction vector, $e_\sigma$:
$\sigma_{1}^{c} = ||\sigma_{1}^{c}|| e_\sigma$.

The maximum principal stress resolved to a \{hkl\} plane
is $S^{hkl} = ||\sigma_{1}^{c}|| e_\sigma \cdot n_{hkl}$.

Finally, because for cleavage analysis we are looking
for the weakest planes, we want to choose those planes
which maximise $S^{hkl}$, i.e.

\begin{equation}
 S^{hkl}
  =
   ||\sigma_{1}^{c}||
    \max
     \left|
       e_\sigma \cdot n_{hkl}^{1\ldots 24}
     \right|
\label{eq:hok}
\end{equation}
%
where the only planes under consideration
are \{100\}, \{110\} and \{111\}, although the surface
energy of \{111\} planes is so high that it is practically
impossible to cleave those.
Even if $e_\sigma \cdot n_{111}=1$, the resolved stress
on \{110\} plane will still be higher, and the cleavage
is likely to occur on \{110\}.
In addition to finding three maximum $S^{hkl}$ values,
one for each \{hkl\} orientation, we need to store
the three corresponding normal vectors, which
maximise the stress, $n_{hlk}^{max}$.

From \eqref{eq:muk} cleavage will occur when
$S^{hkl} \ge \gamma_{hlk}/a_0$.
For the algorithm it is useful to define:
$p^{100}=S^{100}/(\gamma^{100}/a_0)$,
$p^{110}=S^{110}/(\gamma^{110}/a_0)$,
$p^{111}=S^{111}/(\gamma^{111}/a_0)$
and
$p_{max}=\max( p^{100}, p^{110}, p^{111})$.

The first part of the cleavage algorithm
can be thus summarised as follows:

\begin{algorithm}[H]
\SetAlgoLined
%\SetKwData{And}{and}
\SetKwInOut{Input}{input}
\SetKwInOut{Output}{output}

\Input{ $\sigma_{1}^s$, $\tenrtwo{R}^c$ }
\Output{ $p^{100}$, $p^{110}$, $p^{111}$, $p^{max}$,
 $n_{100}^{max}$, $n_{110}^{max}$, $n_{111}^{max}$ }
\BlankLine
$\sigma_{1}^{c} = (\tenrtwo{R}^c)^T \cdot \sigma_1^s$\;
$ S^{100} = ||\sigma_{1}^{c}||
  \max \left| e_\sigma \cdot n_{100}^{1\ldots 24}
     \right|$; $n_{100}^{max}$ \;
$ S^{110} = ||\sigma_{1}^{c}||
  \max \left| e_\sigma \cdot n_{110}^{1\ldots 24}
     \right|$; $n_{110}^{max}$ \;
$ S^{111} = ||\sigma_{1}^{c}||
  \max \left| e_\sigma \cdot n_{111}^{1\ldots 24}
     \right|$; $n_{111}^{max}$ \;
$p^{100}=S^{100}/(\gamma^{100}/a_0)$ \;
$p^{110}=S^{110}/(\gamma^{110}/a_0)$ \;
$p^{111}=S^{111}/(\gamma^{111}/a_0)$ \;
$p_{max}=\max( p^{100}, p^{110}, p^{111})$\;
\caption{
Cleavage algorithm, calculating max resolved
stress}
\label{algo:clvg1:p}
\end{algorithm}

There are special cell states representing
cleavage cracks on \{100\}, \{110\}, \{111\} planes,
$s_{100}$, $s_{110}$, $s_{111}$.
We call the set of all these states {\em cleavage states},
$s_c=[s_{100}, s_{110}, s_{111}]$.

Finally, we can decide whether cleavage will happen,
and if so, on which plane.
The algorithm can be constructed as follows.
The outputs are the unit vector, $n^s$, normal to the
active cleavage plane, in the spatial coord. system,
and the cleavage cell state, $s$.
The vector $n^c$ is first calculated in the grain
coord. sys. and then rotated to the spatial coord. sys.,
$n^s$.

\begin{algorithm}[H]
\SetAlgoLined
%\SetKwData{And}{and}
\SetKwInOut{Input}{input}
\SetKwInOut{Output}{output}

\Input{ $p^{100}$, $p^{110}$, $p^{111}$, $p_{max}$,
 $n_{100}^{max}$, $n_{110}^{max}$, $n_{111}^{max}$ }
\Output{ $n^s$, $s$, flag }
\BlankLine
{$n^s$=(0,0,0), $s=0$, flag=FALSE}\;
\If{$p_{max} \ge 1$}{
flag=TRUE\;
cleavage on \{100\}, $n^c=n_{100}^{max}$, $s=s_{100}$\;
\If{$p^{110} > p^{100}$}{
cleavage on \{110\}, $n^c=n_{110}^{max}$, $s=s_{110}$\;
}
\If{$p^{111} > p^{100}$ and
    $p^{111} > p^{110}$}{
cleavage on \{111\}, $n^c=n_{111}^{max}$, $s=s_{111}$\;
}
$n^s = \tenrtwo{R}^c \cdot n^c$\;
}
\caption{
Cleavage algorithm, calculating
the cleavage plane}
\label{algo:clvg1:n}
\end{algorithm}

So now we know the cleavage vector, $n$,
in the spatial coord. system
and the cleavage type (state), $s$.
This is all that is required to propagate a
type $s$ crack through the cellular automata.

\subsection{Cleavage representation in the cellular model}

Imagine that some cells with states
of the $s_c$ set are
randomly scattered across the model.
We call these {\em crack nuclei}.
The model is subjected to some max. principal
stress vector, $\sigma^1$.
The question we want answered is this:
how will the cracks grow from the nuclei?

One possibility is to analyse each
crack edge cell.
Importantly, this immediately leads to the
need to have two coarrays, one for storing
grain numbers, and another for storing
fracture states of cells.
By analysing both arrays together one
can know what grain a given fractured
cell belongs.
As shown in Sec. \ref{sec:space:coarray},
we implement this by using a 4D coarray.

We scan over all undamaged cells.
If there is a cleaved neighbour,
such that the vector connecting the
cleaved and the central cells is on
or near the cleavage plane, then the
state of the central cell is changed
to the given cleavage state.
Note that it is possible that the given
cleavage state and the neighbour cleavage
state will differ.
The algorithm is summarised as follows:

\begin{algorithm}[H]
\SetAlgoLined
%\SetKwData{And}{and}
\SetKwInOut{Input}{input}
\SetKwInOut{Output}{output}

\Input{ $n^s$, $s$, threshold }
\Output{cell state change}
\BlankLine
 \For { i=1,26 } {
  \If { $i$th cell is cleaved } {
   $e_i$ is a unit vector from
    the central cell to $i$th cell\;
   \If { $|e_i \cdot n^s| <$ threshold } {
    central cell state is changed to $s$\;
    exit\;
   }
  }
 }
\caption{
Cleavage algorithm, propagating cleavage
crack through the cellular model.
}
\label{algo:clvg2}
\end{algorithm}

According to \cite{shterenlikht2013c},
if the threshold is 0.1733, then there will
always be at least 2 cells close enough
to the cleavage plane, so that they can
be considered cleaved.
However, this is probably unsatisfactory,
as the crack will propagate as just a straight
line in this case, not as a 2D plane.
So, perhaps the threshold must be lifted so
that there are at least 4 cells on the cleavage
plane in all cases.

The above algorithm will change state only of the
neighbouring cells.
Thus the speed of crack propagation is 1 cell/increment.

\subsection{Open questions}

Now the really hard bit: somebody must deside how often
to run the first part of the algorithm, i.e.
$(\sigma_1^s) \to (n^s,s)$.
In other words, some decision must be made regarding
how fast $\sigma_1^s$ might be changing, therefore
how often to recalculate the $n^s,s$ pair.

Equally crucial: how large an area of the complete
cellular model is subjected to the same $\sigma_1^s$?
Is it always the whole model?
Is it some small region of the complete cellular model?

These questions are the key for designing a parallel
algorithm.
Is each image using the same $\sigma_1^s$, or
separate?

For now I make a massive assumption that the complete
cellular model is always subjected to the same
macroscopic $\sigma_1^s$.
This means that all images use the same $\sigma_1^s$.

The cleavage algorithms are implemented as purely
serial routines.
Some parallel driver routines will have to go on top.
A prototype algorithm, executed by each image, is as follows:

\begin{algorithm}[H]
\SetAlgoLined
%\SetKwData{And}{and}
\SetKwInOut{Input}{input}
\SetKwInOut{Output}{output}

\Input{cellular array, $\tenrtwo{R}^c$ array}
\Output{possible cell state change to cleaved}
\BlankLine
\texttt{old state} = liquid\_state\;
\For { all cells }{
pick cell i\;
read its grain state, \texttt{new state}\;
\If { grain of cell i is intact }{
 \If { \texttt{new state} $\ne$ \texttt{old state} }{
  run algs. \ref{algo:clvg1:p} and
   \ref{algo:clvg1:n}: $n^s$, $s$, flag\;
  \texttt{state old = state new} \;
 }
 \If { flag = TRUE }{
  run alg. \ref{algo:clvg2}: change state
   of cell $i$ to $s$\;
 }
}
}
\caption{
Complete cleavage algorithm, top level view.
}
\label{algo:clvg:top}
\end{algorithm}

Note that the \texttt{new state} $\ne$
\texttt{old state} check
is intended for crossing the grain
boundary.
Because the grain orientation has changed,
the algs. \ref{algo:clvg1:p} and
\ref{algo:clvg1:n} have to be rerun,
and the new $n^s$, $s$ and flag need
to be calculated.
However, this algorithm completely ignores
that fracture across a grain boundary
is much more complex.

\subsection{Crossing grain boundary}

For a review of the state-of-art
at the beginning of the centry refer, e.g.
to \cite{gerberich2003v8}, specifically
chapters \cite{gerberich2003},
\cite{gerberich2003a}, \cite{milligan2003} and in particular
\cite{tan2003}.
A more fundamental and older account
can be found in \cite{liebowitz1968},
specifically chapters \cite{thomson1968},
\cite{bilby1968} and particularly \cite{beachem1968}.

One major problem of a cellular
approach is that this is a local
approach.
It is very hard to model planes or
other global geometrical entities.
Hence the models of the type used
in \cite{smith2012} can deal with crossing
of the grain boundary easier.
In such geometrical (global) models
the process is simple: as soon as a
cleavage crack reaches a grain boundary
at some spatial point, a cleavage plane
in the following grain is fully determined,
thus allowing for the analysis of the
fracture of the boundary fragment
defined by the grain boundary plane
and by the two cleavage planes in both
grains.
In contrast, in a cellular (local) model,
there is no global cleavage plane defined
in a grain.
When each new cell is analysed, it has
a chance of propagating cleavage into
a neighbouring grain, and hence starting
another cleavage plane.
If left unchecked, this process quickly
leads to the bulk of the model being
populated with cleaved cells.
Such model is, of course, not physical.

Note that the geometrical model is not
physical either.
It simply looks at the final result of
the cleavage propagation and tries to
reproduce it.
It is doubtful that the physical reality
of cleavage propagation is close to
the global geometrical view.

Anyway, in a cellular model some extra
criteria has to be added to prevent
proliferation of new cleavage cracks
at the boundary.
We introduce another logical array,
which records the status of crack
crossing the boundary between any
two grains.
In an undamaged model, this array
is \texttt{.FALSE.} everywhere.
When the cleavage crack crosses a grain
boundary between grains A and B,
then the corresponding entry in this
array
is changed to \texttt{.TRUE.}.

In addition, a new failure state is
added: \texttt{state\_gb\_failed}.

Cells on the grain boundary are cells that
have neighbours from more than 1 grain,
e.g. grains A and B.
Such cells can fail if (a) the crack propagation
status between A and B is \texttt{.FALSE.}
or (b)
if the crack propagation status is \texttt{.TRUE.}
and the central cell has a neighbour of the type
\texttt{state\_gb\_failed}.
The first case corresponds to the cleavage
crack propagating from one grain to another by
crossing the grain boundary.
The second case corresponds to the failure of the
grain boundary itself.
This algorithm is surely artificial, however,
I don't know how the actual physical process
takes place.

In this algorithm there is only one cleavage
plane generated in a grain from a particular
boundary.
However, it is still possible to have multiple
cleavage cracks in a grain if another cleavage
crack starts to propagage from another boundary.


\documentclass[12pt]{article}

\usepackage{amsmath,amsfonts,amssymb}
\usepackage{amsthm}
\usepackage{enumerate}

\theoremstyle{definition}
\newtheorem{definition}{Definition}[section]
\newtheorem{proposition}[definition]{Proposition}

\begin{document}

\title{Math 220 Proof Portfolio}
\date{}

\maketitle

%%%%%
%%%%% Entry 1
%%%%%

\section{The first entry (change this section name)}

Put your first entry here, unless you are choosing not to revise it from the version submitted with Problem Set $6$. If you are not revising Entry $1$, you can leave this section blank (or you can paste in your Entry $1$ and write at the top that you have not revised it).

You should change all uses of \texttt{$\backslash$section} to \texttt{$\backslash$subsection} in your \LaTeX\ source, since the entire entry is now one section of the overall portfolio. As an example, I've pasted the first chunk of the template source code below.

\subsection*{Introduction}

The purpose of this document is to establish the irrationality of a certian class of real numbers, namely roots of prime numbers. First recall some basic facts and definitions.

\begin{enumerate}
\item
A real number $a$ is called \textit{rational} if there exist integers $m,n$ with $n \neq 0$ and  $ a = \frac{m}{n}$.
\item
(\textit{B\'ezout's identity})
If $a$ and $b$ are two integers, not both $0$, then there exist integers $u,v$ such that
$$a u + bv = \gcd(a,b).$$
Furthermore, if there exist $u,v \in \mathbb{Z}$ such that $au+bv=1$, then $\gcd(a,b) = 1$.
\item
(\textit{Euclid's lemma}, weak form)
If $p$ is a prime number and $a,b$ are integers such that $p \mid ab$, then either $p \mid a$ or $p \mid b$.
\end{enumerate}

\subsection{Preliminary results}

We now prove two preliminary lemmas.\\

[etc.]

%%%%%
%%%%% Entry 2
%%%%%
\newpage
\section{The second entry (change this section name)}

Write your second entry here, unless you submitted a draft at the end of classes and do not wish to revise it.

%%%%%
%%%%% Entry 3
%%%%%
\newpage
\section{The third entry (change this section name)}

Write your third entry here.

%%%%%
%%%%% Entry 4
%%%%%
\newpage
\section{The fourth entry (change this section name)}

Write your fourth entry here.

%%%%%
%%%%% Reflection
%%%%%
\newpage
\section{Reflection essay}

Write your short essay refleting on mathematical reasoning and proof here.

\end{document}


\section{\texttt{CG\_CPP}:
 calculation of the minimum angle (maximum projection)
 between a vector in a CA coord. system and a crystal
 vector}

\label{sec:cg_cpp}

\subsection{Purpose}

The aim of this routine is to find a crystallographic
direction (or a crystallographic plane having this
direction as a normal)
that is closest to the direction of the
applied vector in CA coord. system. If the applied
vector is the direction of the maximum principal
stress, then this plane is a candidate for cleavage
propagation.

\subsection{See also}

\hyperref[sec:cg_clvg]{\texttt{CG\_CLVG}},
\hyperref[sec:cg_csym]{\texttt{CG\_CSYM}}

\subsection{Interface}

\begin{verbatim}

! IN:
!        AVEC - A VECTOR IN CA COORD. SYSTEM
!           R - ROTATION TENSOR, DEFINING THE ORIENTATION OF A CRYSTAL
!        CVEC - CRYSTAL VECTOR, IN CRYSTAL COORD. SYSTEM
! OUT:
!     MAXPROJ - MAX PROJECTION OF THE AVEC ONTO CVEC AND ALL OTHER
!               CRYSTAL VECTORS DEFINING PLANES OF THE SAME FAMILY
!               AS CVEC.
!         CCA - THE CRYSTAL VECTOR IN CA COORD. SYSTEM, WHICH
!               PRODUCES THE MAX PROJ VALUE.
!
!**********************************************************************73
!
! ONELINE: MIN ANGLE BETWEEN A VECTOR AND A NORMAL TO A CRYSTAL PLANE
!
!**********************************************************************73

 SUBROUTINE CG_CPP(AVEC,R,CVEC,MAXPROJ,CCA)

 IMPLICIT NONE

 REAL(KIND=8),INTENT(IN) :: AVEC(3),R(3,3),CVEC(3)
 REAL(KIND=8),INTENT(OUT) :: MAXPROJ,CCA(3)

\end{verbatim}

\subsection{Details}

This routine is non-trivial only because of the
need to take crystal symmetry into account. Refer
to Fig. \ref{fig:cg_cpp} below.

\begin{figure}[htb]
\centering
\includegraphics[width=0.5\textwidth]{./cg_cpp.pdf}
\caption{Schematic diagram illustrating
\hyperref[sec:cg_cpp]{\texttt{CG\_CPP}}
routine. Refer to the main text for details.}
\label{fig:cg_cpp}
\end{figure}

Consider a CA array with its coord. system (superscript
CA). A vector $\vec{a}$ is defined in this coord. system.

Within the CA array there is a grain (crystal) defined by
its rotation tensor $\mathbf{R}$, which has the usual
meaning: a vector $\vec{c}$ in the crystal coord. system
is transformed into a vector in the CA coord. system as

\begin{equation}
 c^{CA}
  =
   \mathbf{R} c^{C}
 \label{eq:cpp:pub}
\end{equation}
%
where superscript $C$ means relative to the crystal
coord. system.

Both $\vec{a}$ and $\vec{c}$ are unit vectors.

The dot product of $\vec{a}$ and $\vec{c}$ is
obtained simply as (in tensor notation)

\begin{equation}
 \texttt{dot product }
  =
   a\cdot\mathbf{R}c
 \label{eq:cpp:pluck}
\end{equation}

However, due to crystal symmetry, $\vec{c}$ defines
a class of planes, not just one plane. To take this into
account we use rotational symmetry tensors, $\mathbf{R}^S$,
see \hyperref[sec:cg_csym]{\texttt{CG\_CSYM}}.
For cubic crystals there are 24 distinct $\mathbf{R}^S$
tensors, including the trivial $\mathbf{R=I}$.

So we get 24 dot products, of which we choose
the one with the maximum absolute value
for output

\begin{equation}
 \texttt{max dot product }
  =
   \max
    |
     a \cdot \mathbf{R} \mathbf{R}^S c
    |
 \label{eq:cpp:mumps}
\end{equation}

The rotational symmetry
tensor, producing the maximum dot product value,
$\mathbf{R}^S_{max}$ is used to calculate the
crystallographic plane, of type defined by $\vec{c}$,
that is closest to being normal to $\vec{a}$:
$\mathbf{R}^S_{max}c$.


\begin{savequote}[75mm]

\end{savequote}

\chapter{Model and Simulation}
{\lettrine[lines=3,slope=1pt,nindent=1pt,]{\textcolor{SchoolColor}{C}}{onsider a $Y \times X$\ \ bounded lattice graph} where $X,Y\in\N$. For simplicity, let $Z = YX$. In each vertex, there is a single residence for a single agent. We allow for vacant houses, hence, there are $Z$ houses and $N$ agents where $N \leq Z$. Agents are defined by a their race, $r\in\{$White, Black, Hispanic$\}$ and income $w$. This paper simplifies the wealth distribution with the function $w(x)\in[1,3]$ where:
\begin{equation}
\label{eq:w}
    w(x) = \left\{\begin{array}{lr}
        1 & $x$<\$35k\\
        2 & \$35k \leq $x$ \leq \$100k\\
        3 & $x$ >\$100k
        \end{array}\right\}.
\end{equation}
The framework in this model allows for any discrete measure of wealth and race. The wealth levels are smoothed to three levels to simplify to low, medium, and high wealth. Since the model is tailored to Chicago, we exclude Asians, Native Americans, and mixed races, as they represent less than 6\% of the population in Chicago. The Schelling model has difficulty incorporating small minorities in the model. Given that this paper is simulating an aggregate of agents, the Schelling model will not be able to interact the small minority residents in a meaningful manner.

\section{Schelling Segregation}
In the 1971 Schelling model, agents were defined as either an `X' or `O' to indicate their race and are placed onto the board in some random or non-random order. This paper has three distinct races. We define a tolerance level $\tau\in[0,1]$ which is the minimum level that each agent will accept being a minority in their neighborhood. If $\tau = 0.4$, then each agent $i$ of race $r$ will be dissatisfied unless at least 40\% of its neighbors are also of race $r$. Tolerance levels are common across all agents regardless of race. Neighborhoods are Morse Neighborhoods, which are defined by the 8 closest points to the agent on a grid. This is shown in Figure \ref{fig:morse} below. Other Schelling models use Manhattan distance, usually with $d=1$ or $d=2$, as in Figure \ref{fig:MD}. However, Zhang 2011 simulates that changing the neighborhood does not significantly alter the underlying segregation, just the speed of convergence\cite{zhang11}.

\begin{figure}[h!]
\centering
\includegraphics[scale=.33]{figures/H8.jpeg}
\caption{Diagram of Morse Neighborhood}
\label{fig:morse}
\end{figure}

At each step, all the agents simultaneously look at their neighborhood to calculate their ratio of matching race. If that ratio is smaller than the minimum tolerance level, then the agent is \textit{unsatisfied}. All \textit{unsatisfied} agents are simultaneously removed from the board. In Schelling, the agents are placed back into the board randomly, as in Algorithm \ref{alg:euclid}. Luckily, the complexity of the algorithm is $O(n^2)$ at each step. However, at high tolerance levels, the number of steps the algorithm takes grows quickly. For this reason, we run the algorithm until there is at most 10\% \textit{unsatisfied} agents.

 \begin{algorithm}[h!]
  \caption{Schelling Process}\label{alg:euclid}
  \begin{algorithmic}[1]
  	\Function{Schelling}{$agents, grid, tolerance$}\Comment{Agents are defined by their race}
   		\Procedure{Satisfaction}{$agent_i, subgrid, tolerance$}\Comment{The $subgrid$ is a Morse neighborhood of $agent_i$}
        	\State Let $sat = \frac{\text{matching agents in $subgrid$ to $agent_i$}}{\text{size of subgrid}}$
       		\If {$tolerance < sat $}
       			\State return $TRUE$. \Comment{The agent is $satisfied$.}
                \Else \ return $FALSE$. \Comment{The agent is $unsatisfied$.}
    		\EndIf
        \EndProcedure
    	\While{for any $agent_i$, if Satisfaction is FALSE}
        	\State Remove all $unsatisfied$ agents
            \State Place all $unsatisfied$ agents into randomly assigned empty spots on the grid
        \EndWhile{}
  	\EndFunction{}
  \end{algorithmic}
  \end{algorithm}
  \begin{figure}[h!]
  \caption{Algorithm of Schelling Process}
\end{figure}

It is worth noting that the Schelling algorithm can also be presented as a Markov Chain, although with a complicated transition matrix. At each vertex, each house on the board has some probability $p$ of being race $r$ or vacant ($r=0$). While moving from each state is non-trivial, if we already know which agents are satisfied then the transition matrix is more simple. Hence, we may imagine an initial matrix state $M_{i=0}$. All the odd states remove the previously \textit{unsatisfied} agents and replace them with vacant houses. All the even states return the \textit{unsatisfied} agents to $M$. This may be represented as the following figures from left to right:

\begin{figure}[h!]
\begin{floatrow}
{%
\includegraphics[scale=0.25]{figures/EX0.jpeg}
}
{%
\includegraphics[scale=0.25]{figures/Ex1.jpeg}
}
{%
\includegraphics[scale=0.25]{figures/Ex2.jpeg}
}
\end{floatrow}
\caption[Example of Schelling Process]{Example of Schelling: $\tau = 50\%,\ M_{i=0} \rightarrow M_{i=2}$}
\end{figure}

The model in this paper adds a housing gradient as well as a wealth distribution to the original Schelling algorithm.

\section{Schelling with a Housing Market}

The housing market is a simple lottery. We take a housing price gradient as given from the data to rank the houses and rank the agents in order of wealth. The agents choose lexicographically with higher wealth agents choosing higher-priced houses. We look at the census tract data for median housing evaluation in Chicago for 2000 and 2010 to model the housing gradient. Spaces can also be empty, allowing the model to have neighborhoods that are more ``urban'' \textit{i.e.} have fewer empty dots, and to somewhat simulate urban decay. All this information is found in the 2000 and 2010 census tract data. Currently, the model does not attempt to simulate differing density levels. They may be done by using the continuous Schelling algorithm, however, this does slow the algorithm.

\begin{figure}[h!]
\centering
\begin{subfigure}{.45\textwidth}
  \centering
\includegraphics[scale=.15]{figures/c_2000.png}
\end{subfigure}%
\begin{subfigure}{.45\textwidth}
  \centering
\includegraphics[scale=.15]{figures/c_2000_inner.png}
\end{subfigure}
\label{fig:chicago2000}
\begin{subfigure}{.45\textwidth}
  \centering
\includegraphics[scale=.15]{figures/c_2010.png}
\end{subfigure}%
\begin{subfigure}{.45\textwidth}
  \centering
\includegraphics[scale=.15]{figures/c_2010_inner.png}
\end{subfigure}
\caption[Chicago Median Housing Evaluation, 2000-2010]{Chicago: Median Housing Evaluation (\$) in 2000 and 2010}
\label{fig:chicago2000}
\end{figure}

As one may note with Figure \ref{fig:chicago2000} below, the monocentric model of cities in urban economics roughly fits for Chicago \cite{brueckner87}. The center of the city is the most valued and house prices decay outward. However, within the city, there are several instances of high wealth next to low wealth. Urban Economists have a difficult time explaining why there is low wealth accumulation within the city center. Some researchers claim it is due to a combination of larger houses in the suburbs as well as access to better transportation \cite{glaeser08a}. The low wealth individual may only use public transportation while higher wealth individuals own their car(s). Both of these claims may be incorporated into the algorithm in future papers. This may be residual from the 1960s white flight where white people 'retreated' from the more mixed inner city neighborhoods to the mostly white suburbs. The algorithm developed in this paper may be used to analyze the transition, however, the algorithm is missing the structural racism that was present during this time period\cite{cutler97, cutler99}. Similarly, the algorithm is somewhat invariant to initial conditions\cite{zhang11}. It is not clear whether neighborhoods ought to be invariant or whether they are time dependent. Several authors have modeled the housing market with different lags and it is not clear how many lags a housing market may have\cite{glaeser07}. Other tipping algorithms have looked into studying this problem with mixed results\cite{card07}.

\begin{wrapfigure}{r}{.4\textwidth} %this figure will be at the right
    \centering
    \includegraphics[scale=0.6]{figures/House_Price.jpeg}
    \caption[Simulated Housing Gradient Evaluation]{Simulated Housing Gradient Evaluation. More purple colors mean higher housing values. More red colors mean lower housing values. The decay in evaluation is defined by a euclidean distance}
\end{wrapfigure}

In 2010, the highest valued plot of land in Chicago moved northwest from the center. For now, we will assume that there is strict linear decay in the model for both 2000 and 2010. The housing gradient evaluation is simply a Euclidean distance from the center right of the plane. In future papers, one may need to construct a better fitting valuation gradient to better match the city being fitted. This paper does not analyze the robustness of precision of the price gradient. Similarly, upgrading the resolution using a Pointillist map may better capture the underlying distribution. This is especially true for cities which are currently gentrifying or have natural boundaries that create nonlinear (and/or even non-monotonic) price gradients. However, it is also worth noting that the housing market is only interested in ranking. Hence, changing the metric or the $2^{nd}$ derivative of price with respects to space should not change the ranking at all.

This paper modifies the algorithm to incorporate the housing market. Agents still only observe the race of other agents to determine whether they are satisfied. However, now \textit{unsatisfied} agents are sorted back lexicographically into the grid by their wealth. Specifically, agents are sorted by wealth and agents with higher wealth choose higher priced (or valued) houses. This process is described by Algorithm \ref{alg:extendschelling} below. This process allows for better clumping of wealthy agents. However, it allows for clumping of wealth agents in non-central locations. The complexity remains at $O(n^2)$ and there is not much change to the constant term in the complexity.

 \begin{algorithm}[htbp!]
  \caption{Extended Schelling Process}\label{alg:extendschelling}
  \begin{algorithmic}[1]
  	\Function{Extended Schelling}{$agents, grid, tolerance$}\Comment{Race and wealth define the agents.}
   		\Procedure{Satisfaction}{$agent_i, subgrid, tolerance$}\Comment{$subgrid$ is a Morse neighborhood of $agent_i$.}
        	\State Let $sat = \frac{\text{matching agents in $subgrid$ to $agent_i$}}{\text{size of subgrid}}$
       		\If {$tolerance < sat $}
       			\State return $TRUE$. \Comment{The agent is $satisfied$.}
                \Else \ return $FALSE$. \Comment{The agent is $unsatisfied$.}
    		\EndIf
        \EndProcedure
    	\While{for any $agent_i$, if Satisfaction is FALSE}
        	\State Remove all $unsatisfied$ agents from grid.
            \State Rank all $unsatisfied$ agents by wealth.
            \State Rank all grid spots by the housing price index.
            \State Place all $unsatisfied$ agents in order of wealth to the most valuable real estate on grid.
        \EndWhile{}
  	\EndFunction{}
  \end{algorithmic}
  \end{algorithm}
  \begin{figure}[h!]
\caption{Algorithm of Extended Schelling Process}
\end{figure}

\begin{figure}[h!]
\centering
\includegraphics[scale=0.5]{figures/chart4.png}
\includegraphics[scale=0.4]{figures/chart3.png}
\caption[Demographics of Chicago]{Demographics of Chicago (Source: US2010)\cite{brown10}}
\label{tab:chicago2}
\end{figure}

The extended Schelling algorithm, by virtue of incorporating both race and wealth, requires a distribution of race and wealth within the population. In fact, the extended algorithm allows for some nuance in the distribution, including individual wealth distribution by race. This allows the extended Schelling algorithm to incorporate the significant wealth differences between races. Figure \ref{tab:chicago2} shows the racial makeup of Chicago in 2000 and 2010. Table \ref{tab:chicago} shows the adjusted racial and wealth make up of Chicago in 2000 and 2010 for the simulation. Races with less than 10\% of the population were dropped as Schelling has difficulty modeling with such low numbers. The wealth distribution represents the fraction of individuals with wealth in the categories described by $w(x)$ in Equation \ref{eq:w}. Note that incomes generally moved up. These incomes, however, are not adjusted for inflation. Census tracts only provide ranges of income, so there is no way to correct for inflation because there is not enough resolution to correctly adjust the category of the edge cases. However, noting that all races saw about 5\% of the population move from income 1 to income 2, it is likely that inflation accounts for much of this change. This effect is different for Whites than for nonwhites, however. Nonwhites saw about 5\% of individuals move from income 2 to income 3, but Whites saw nearly 15\% make that jump.

\begin{table}[h!]
\centering
\caption{Demographics of Chicago}
\label{tab:chicago}
\scalebox{1}{
\begin{tabular}{lllll}
\hline
\multicolumn{5}{l}{Demographics of Chicago Used in Simulations}                                                        \\ \hline \hline
\multicolumn{1}{l|}{Year}     & \multicolumn{2}{l|}{2000}                       & \multicolumn{2}{l}{2010}   \\ \hline
\multicolumn{1}{l|}{Race}     & Population Ratio & \multicolumn{1}{l|}{Wealth Distribution}  & Population Ratio & Wealth Distribution  \\
\multicolumn{1}{l|}{White}    & 60\%           & \multicolumn{1}{l|}{30\%|50\%|20\%} & 56\%            & 25\%|41\%|34\%  \\
\multicolumn{1}{l|}{Black}    & 26\%           & \multicolumn{1}{l|}{53\%|40\%|07\%} & 27\%            & 49\%|40\%|11\%  \\
\multicolumn{1}{l|}{Hispanic} & 14\%           & \multicolumn{1}{l|}{43\%|50\%|07\%} & 17\%            & 37\%|51\%|12\%  \\ \hline \hline
\end{tabular}
}
\end{table}

Other than the significant increase in White wealth, the demographics of Chicago show mostly stable trends from 2000 to 2010. There is a 5\% upwards income drift and a slight decrease in the white population as a share. Asians and other small mass races are excluded from this analysis; the population ratio is the ratio of Chicago's White, Black, and Hispanic population, not the share of its total population. By simulating Chicago with the 2000 and 2010 demographics, one can see whether similar tolerance levels reproduce Chicago's actual segregation statistics on average. If the tolerance level $\tau$ decreases, then this simulation would be evidence of Chicago becoming more racially tolerant.


\input{../header_class}

%---------- start document ---------- %
 \section{poly.ring -- polynomial rings}\linkedzero{poly.ring}
 \begin{itemize}
   \item {\bf Classes}
   \begin{itemize}
     \item \linkingone{poly.ring}{PolynomialRing}
     \item \linkingone{poly.ring}{RationalFunctionField}
     \item \linkingone{poly.ring}{PolynomialIdeal}
   \end{itemize}
 \end{itemize}

\C

 \subsection{PolynomialRing -- ring of polynomials}\linkedone{poly.ring}{PolynomialRing}
 A class for uni-/multivariate polynomial rings.
 A subclass of \linkingone{ring}{CommutativeRing}.

 \initialize
 \func{PolynomialRing}{%
   \hiki{coeffring}{CommutativeRing},\ %
   \hikiopt{number\_of\_variables}{integer}{1}}{\out{PolynomialRing}}\\
 \spacing
 \quad \param{coeffring} is the ring of coefficients.
 \param{number\_of\_variables} is the number of variables.
 If its value is greater than \(1\), the ring is for multivariate polynomials.
  \begin{at}
    \item[zero]\linkedtwo{poly.ring}{PolynomialRing}{zero}:\\ zero of the ring.
    \item[one]\linkedtwo{poly.ring}{PolynomialRing}{one}:\\ one of the ring.
  \end{at}
 \method
 \subsubsection{getInstance -- classmethod}\linkedtwo{poly.ring}{PolynomialRing}{getInstance}
 \func{getInstance}{%
   \hiki{coeffring}{CommutativeRing},\ %
   \hiki{number\_of\_variables}{integer}}{\out{PolynomialRing}}\\
 \spacing
 \quad return the instance of polynomial ring with coefficient ring
 \param{coeffring} and number of variables \param{number\_of\_variables}.

 \subsubsection{getCoefficientRing}\linkedtwo{poly.ring}{PolynomialRing}{getCoefficientRing}
 \func{getCoefficientRing}{}{CommutativeRing}

 \subsubsection{getQuotientField}\linkedtwo{poly.ring}{PolynomialRing}{getQuotientField}
 \func{getQuotientField}{}{Field}

 \subsubsection{issubring}\linkedtwo{poly.ring}{PolynomialRing}{issubring}
 \func{issubring}{\hiki{other}{Ring}}{\out{bool}}

 \subsubsection{issuperring}\linkedtwo{poly.ring}{PolynomialRing}{issuperring}
 \func{issuperring}{\hiki{other}{Ring}}{\out{bool}}

 \subsubsection{getCharacteristic}\linkedtwo{poly.ring}{PolynomialRing}{getCharacteristic}
 \func{getCharacteristic}{}{\out{integer}}

 \subsubsection{createElement}\linkedtwo{poly.ring}{PolynomialRing}{createElement}
 \func{createElement}{seed}{\out{polynomial}}\\
 \quad Return a polynomial. \param{seed} can be a polynomial,
 an element of coefficient ring, or any other data suited for the first
 argument of uni-/multi-variate polynomials.

 \subsubsection{gcd}\linkedtwo{poly.ring}{PolynomialRing}{gcd}
 \func{gcd}{a, b}{\out{polynomial}}\\
 \quad Return the greatest common divisor of given polynomials (if possible).
 The polynomials must be in the polynomial ring.
 If the coefficient ring is a field, the result is monic.

 \subsubsection{isdomain}\linkedtwo{poly.ring}{PolynomialRing}{isdomain}
 \subsubsection{iseuclidean}\linkedtwo{poly.ring}{PolynomialRing}{iseuclidean}
 \subsubsection{isnoetherian}\linkedtwo{poly.ring}{PolynomialRing}{isnoetherian}
 \subsubsection{ispid}\linkedtwo{poly.ring}{PolynomialRing}{ispid}
 \subsubsection{isufd}\linkedtwo{poly.ring}{PolynomialRing}{isufd}

 Inherited from \linkingone{ring}{CommutativeRing}.

 \subsection{RationalFunctionField -- field of rational functions}\linkedone{poly.ring}{RationalFunctionField}
  \initialize
  \func{RationalFunctionField}{%
    \hiki{field}{Field},\
    \hiki{number\_of\_variables}{integer}}{%
    \out{RationalFunctionField}}\\
  \spacing
  \quad A class for fields of rational functions.
  It is a subclass of \linkingone{ring}{QuotientField}.\\
  \spacing
  \quad \param{field} is the field of coefficients, which should be a
  \linkingone{ring}{Field} object.
  \param{number\_of\_variables} is the number of variables.\\
  \spacing
  \begin{at}
    \item[zero]\linkedtwo{poly.ring}{RationalFunctionField}{zero}:\\ zero of the field.
    \item[one]\linkedtwo{poly.ring}{RationalFunctionField}{one}:\\ one of the field.
  \end{at}
%
  \method
  \subsubsection{getInstance -- classmethod}\linkedtwo{poly.ring}{RationalFunctionField}{getInstance}
  \func{getInstance}{%
    \hiki{coefffield}{Field},\ %
    \hiki{number\_of\_variables}{integer}}{\out{RationalFunctionField}}\\
  \spacing
  \quad return the instance of {\tt RationalFunctionField} with coefficient
  field \param{coefffield} and number of variables \param{number\_of\_variables}.

  \subsubsection{createElement}\linkedtwo{poly.ring}{RationalFunctionField}{createElement}
  \func{createElement}{*\hiki{seedarg}{list}, **\hiki{seedkwd}{dict}}{\out{RationalFunction}}\\

  \subsubsection{getQuotientField}\linkedtwo{poly.ring}{RationalFunctionField}{getQuotientField}
  \func{getQuotientField}{}{\out{Field}}

  \subsubsection{issubring}\linkedtwo{poly.ring}{RationalFunctionField}{issubring}
  \func{issubring}{\hiki{other}{Ring}}{\out{bool}}\\

  \subsubsection{issuperring}\linkedtwo{poly.ring}{RationalFunctionField}{issuperring}
  \func{issuperring}{\hiki{other}{Ring}}{\out{bool}}\\

  \subsubsection{unnest}\linkedtwo{poly.ring}{RationalFunctionField}{unnest}
  \func{unnest}{}{\out{RationalFunctionField}}\\
  \spacing
  \quad If self is a nested {\tt RationalFunctionField} i.e. its
  coefficient field is also a {\tt RationalFunctionField}, then the
  method returns one level unnested {\tt RationalFunctionField}.\\
  \quad For example:
\begin{ex}
>>> RationalFunctionField(RationalFunctionField(Q, 1), 1).unnest()
RationalFunctionField(Q, 2)
\end{ex}

  \subsubsection{gcd}\linkedtwo{poly.ring}{RationalFunctionField}{gcd}
  \func{gcd}{\hiki{a}{RationalFunction},\ \hiki{b}{RationalFunction}}{%
  \out{RationalFunction}}\\
  \spacing
  \quad Inherited from \linkingone{ring}{Field}.

 \subsubsection{isdomain}\linkedtwo{poly.ring}{RationalFunctionField}{isdomain}
 \subsubsection{iseuclidean}\linkedtwo{poly.ring}{RationalFunctionField}{iseuclidean}
 \subsubsection{isnoetherian}\linkedtwo{poly.ring}{RationalFunctionField}{isnoetherian}
 \subsubsection{ispid}\linkedtwo{poly.ring}{RationalFunctionField}{ispid}
 \subsubsection{isufd}\linkedtwo{poly.ring}{RationalFunctionField}{isufd}

 Inherited from \linkingone{ring}{CommutativeRing}.

  \subsection{PolynomialIdeal -- ideal of polynomial ring}\linkedone{poly.ring}{PolynomialIdeal}

  A subclass of \linkingone{ring}{Ideal} represents ideals of polynomial rings.

  \initialize
  \func{PolynomialIdeal}{%
    \hiki{generators}{list},\ %
    \hiki{polyring}{PolynomialRing}}{\out{PolynomialIdeal}}\\
  \spacing
  \quad Create an object represents an ideal in a polynomial ring
  \param{polyring} generated by \param{generators}.
  \begin{op}
    \verb/in/ & membership test\\
    \verb/==/ & same ideal?\\
    \verb/!=/ & different ideal?\\
    \verb/+/ & addition\\
    \verb/*/ & multiplication\\
  \end{op}
  \method
  \subsubsection{reduce}\linkedtwo{poly.ring}{PolynomialIdeal}{reduce}
  \func{reduce}{\hiki{element}{polynomial}}{\out{polynomial}}\\
  \spacing
  \quad Modulo \param{element} by the ideal.

  \subsubsection{issubset}\linkedtwo{poly.ring}{PolynomialIdeal}{issubset}
  \func{issubset}{\hiki{other}{set}}{\out{bool}}\\

  \subsubsection{issuperset}\linkedtwo{poly.ring}{PolynomialIdeal}{issuperset}
  \func{issuperset}{\hiki{other}{set}}{\out{bool}}\\

\C

%---------- end document ---------- %

\input{../footer}


\documentclass[12pt]{cdblatex}
\usepackage{fancyhdr}
\usepackage{footer}

\begin{document}

% =================================================================================================
% create checkpoint file

\bgroup
\CdbSetup{action=hide}
\begin{cadabra}
   import cdblib
   checkpoint_file = 'tests/semantic/output/rnc2rnc.json'
   cdblib.create (checkpoint_file)
   checkpoint = []
\end{cadabra}
\egroup

% =================================================================================================
\section*{From one RNC to another}

Consider an RNC frame with RNC cooridnates $x^{a}$.

In the {\tts geodesic-bvp} code the two point boundary value problem (for the geodesic connecting
two points) was solved. There is a bonus in that calculation -- it can be trivaly adapted to the
case of transforming form one RNC into another.

The starting point is the basic equation for the geodesic connecting $P$ (with coordinaties
$x^{a}$) to Q (with coordinates $x^{a} + Dx^{a}$)
\begin{equation*}
   x^a(s) = x^a_i + s y^a - \sum_{k=2}^\infty\>\frac{1}{k!}\>\Gamma^{a}{}_{\ubk}y^{.\ubk} s^k
\end{equation*}
The affine parameter $s$ varies form 0 (at $P$) to 1 (at $Q$).

A new RNC frame, with origin at $P$, can be defined via the $y^{a}$ with the coordinates of $Q$ in
the new RNC frame defined by $y^{a}$ (since $s=1$ at $Q$). Recall that in an RNC all geodesics
through the origin are described by $y^{a}(s) = s y^{a}$. Thus the transformation from $x^a$ to
$y^a$ satisfies
\begin{equation*}
   x^a = x^a_i + y^a - \sum_{k=2}^\infty\>\frac{1}{k!}\>\Gamma^{a}{}_{\ubk}y^{.\ubk}
\end{equation*}
where the $\Gamma^{a}{}_{\ubk}$ are the generalised connections of the $x^a$ frame evaluated at
$x^a=0$. This equation can be inverted to express $y^a$ in terms of $x^a$. This computation is
done in the {\tts geodesic-bvp} code -- we only quote the results here (at the end).

The new $y^a$ frame has origin at $P$. Its coordinate axes are aligned with those (at $P$) of the
origianl RNC frame. To see this just note that $\partial x^a/\partial y^b = \delta^a_b$ at $P$.
Thus the metric at $P$ in the new frame has values $g_{ab}(x)$ (i.e., exactly those of the
original RNC frame). Note that this means that the coordinate axes of the new frame are not
ncessarily orthogonal.

The calculations in this code are trivial. It uses the $y^{a}$ found in {\tts geodesic-bvp} as
the basis of the transformation from $x^{a}$ to $y^{a}$. Most of the code involves reformatting
the $y^{a}$.

\clearpage

\begin{cadabra}
   {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w#}::Indices(position=independent).

   \nabla{#}::Derivative.

   g_{a b}::Metric.
   g^{a b}::InverseMetric.

   R_{a b c d}::RiemannTensor.
   R^{a}_{b c d}::RiemannTensor.

   # Dx{#}::LaTeXForm{"{\Dx}"}.  # LCB: currently causes a bug, it kills ::KeepWeight for Dx

   import cdblib

   Y5 = cdblib.get ('y5','geodesic-bvp.json')

   Y50 = cdblib.get ('y50','geodesic-bvp.json')
   Y52 = cdblib.get ('y52','geodesic-bvp.json')
   Y53 = cdblib.get ('y53','geodesic-bvp.json')
   Y54 = cdblib.get ('y54','geodesic-bvp.json')
   Y55 = cdblib.get ('y55','geodesic-bvp.json')

   # this copies y5* from geodesic-bvp.json to rnc2rnc.json

   cdblib.create ('rnc2rnc.json')

   cdblib.put ('rnc2rnc',Y5,'rnc2rnc.json')

   cdblib.put ('rnc2rnc0',Y50,'rnc2rnc.json')
   cdblib.put ('rnc2rnc2',Y52,'rnc2rnc.json')
   cdblib.put ('rnc2rnc3',Y53,'rnc2rnc.json')
   cdblib.put ('rnc2rnc4',Y54,'rnc2rnc.json')
   cdblib.put ('rnc2rnc5',Y55,'rnc2rnc.json')

\end{cadabra}

% =================================================================================================
% the remaining code is just for pretty printing

\clearpage

\begin{cadabra}
   # note: keeping numbering as is (out of order) to ensure R appears before \nabla R etc.
   def product_sort (obj):
       substitute (obj,$ x^{a}                            -> A001^{a}               $)
       substitute (obj,$ Dx^{a}                           -> A002^{a}               $)
       substitute (obj,$ g^{a b}                          -> A003^{a b}             $)
       substitute (obj,$ \nabla_{e f g h}{R_{a b c d}}    -> A008_{a b c d e f g h} $)
       substitute (obj,$ \nabla_{e f g}{R_{a b c d}}      -> A007_{a b c d e f g}   $)
       substitute (obj,$ \nabla_{e f}{R_{a b c d}}        -> A006_{a b c d e f}     $)
       substitute (obj,$ \nabla_{e}{R_{a b c d}}          -> A005_{a b c d e}       $)
       substitute (obj,$ R_{a b c d}                      -> A004_{a b c d}         $)
       sort_product   (obj)
       rename_dummies (obj)
       substitute (obj,$ A001^{a}                  -> x^{a}                         $)
       substitute (obj,$ A002^{a}                  -> Dx^{a}                        $)
       substitute (obj,$ A003^{a b}                -> g^{a b}                       $)
       substitute (obj,$ A004_{a b c d}            -> R_{a b c d}                   $)
       substitute (obj,$ A005_{a b c d e}          -> \nabla_{e}{R_{a b c d}}       $)
       substitute (obj,$ A006_{a b c d e f}        -> \nabla_{e f}{R_{a b c d}}     $)
       substitute (obj,$ A007_{a b c d e f g}      -> \nabla_{e f g}{R_{a b c d}}   $)
       substitute (obj,$ A008_{a b c d e f g h}    -> \nabla_{e f g h}{R_{a b c d}} $)

       return obj

   def get_xDxterm (obj,n,m):

       x^{a}::Weight(label=numx,value=1).
       Dx^{a}::Weight(label=numDx,value=1).

       tmp := @(obj).
       distribute  (tmp)

       foo = Ex("numx = " + str(n))
       bah = Ex("numDx = " + str(m))
       keep_weight (tmp, foo)
       keep_weight (tmp, bah)

       return tmp

   def reformat (obj,scale):
       foo  = Ex(str(scale))
       bah := @(foo) @(obj).
       distribute     (bah)
       bah = product_sort (bah)
       rename_dummies (bah)
       canonicalise   (bah)
       substitute     (bah,$Dx^{b}->zzz^{b}$)
       factor_out     (bah,$x^{a?},zzz^{b?}$)
       substitute     (bah,$zzz^{b}->Dx^{b}$)
       ans := @(bah) / @(foo).
       return ans

   def rescale (obj,scale):
       foo  = Ex(str(scale))
       bah := @(foo) @(obj).
       distribute  (bah)
       substitute  (bah,$Dx^{b}->zzz^{b}$)
       factor_out  (bah,$x^{a?},zzz^{b?}$)
       substitute  (bah,$zzz^{b}->Dx^{b}$)
       return bah

   term0 := @(Y50).  # cdb (term0.101,term0)
   term2 := @(Y52).  # cdb (term2.101,term2)
   term3 := @(Y53).  # cdb (term3.101,term3)
   term4 := @(Y54).  # cdb (term4.101,term4)
   term5 := @(Y55).  # cdb (term5.101,term5)

   term0 = reformat (term0,1)  # cdb (term0.102,term0)
   term2 = reformat (term2,1)  # cdb (term2.102,term2)
   term3 = reformat (term3,1)  # cdb (term3.102,term3)
   term4 = reformat (term4,1)  # cdb (term4.102,term4)
   term5 = reformat (term5,1)  # cdb (term5.102,term5)

   xDxterm12 = get_xDxterm (term2,1,2)   # cdb(xDxterm12.101,xDxterm12)

   xDxterm13 = get_xDxterm (term3,1,3)   # cdb(xDxterm13.101,xDxterm13)
   xDxterm22 = get_xDxterm (term3,2,2)   # cdb(xDxterm22.101,xDxterm22)

   xDxterm14 = get_xDxterm (term4,1,4)   # cdb(xDxterm14.101,xDxterm14)
   xDxterm23 = get_xDxterm (term4,2,3)   # cdb(xDxterm23.101,xDxterm23)
   xDxterm32 = get_xDxterm (term4,3,2)   # cdb(xDxterm32.101,xDxterm32)

   xDxterm15 = get_xDxterm (term5,1,5)   # cdb(xDxterm15.101,xDxterm15)
   xDxterm24 = get_xDxterm (term5,2,4)   # cdb(xDxterm24.101,xDxterm24)
   xDxterm33 = get_xDxterm (term5,3,3)   # cdb(xDxterm33.101,xDxterm33)
   xDxterm42 = get_xDxterm (term5,4,2)   # cdb(xDxterm42.101,xDxterm42)

   xDxterm12 = rescale ( reformat (xDxterm12,    3),     3 )   # cdb(xDxterm12.102,xDxterm12)

   xDxterm13 = rescale ( reformat (xDxterm13,   12),   -12 )   # cdb(xDxterm13.102,xDxterm13)
   xDxterm22 = rescale ( reformat (xDxterm22,   24),   -24 )   # cdb(xDxterm22.102,xDxterm22)

   xDxterm14 = rescale ( reformat (xDxterm14,  180),  -180 )   # cdb(xDxterm14.102,xDxterm14)
   xDxterm23 = rescale ( reformat (xDxterm23,  720),  -720 )   # cdb(xDxterm23.102,xDxterm23)
   xDxterm32 = rescale ( reformat (xDxterm32,  720),  -720 )   # cdb(xDxterm32.102,xDxterm32)

   xDxterm15 = rescale ( reformat (xDxterm15,  360),  -360 )   # cdb(xDxterm15.102,xDxterm15)
   xDxterm24 = rescale ( reformat (xDxterm24, 2160), -2160 )   # cdb(xDxterm24.102,xDxterm24)
   xDxterm33 = rescale ( reformat (xDxterm33, 1080), -1080 )   # cdb(xDxterm33.102,xDxterm33)
   xDxterm42 = rescale ( reformat (xDxterm42,  360),  -360 )   # cdb(xDxterm42.102,xDxterm42)

   checkpoint.append (term0)
   checkpoint.append (term2)
   checkpoint.append (term3)
   checkpoint.append (term4)
   checkpoint.append (term5)

\end{cadabra}

\clearpage

% =================================================================================================
\section*{Tranformation between two RNC frames}

\begin{align*}
     y^{a} = \ny{0}^{a} + \ny{2}^{a} + \ny{3}^{a} + \ny{4}^{a} + \ny{5}^{a} + \BigO{\eps^6}
\end{align*}

\begin{dgroup*}
   \begin{dmath*} \ny{0}^{a} = \cdb{term0.102} \end{dmath*}
   \begin{dmath*} \ny{2}^{a} = \cdb{term2.102} \end{dmath*}
   \begin{dmath*} \ny{3}^{a} = \cdb{term3.102} \end{dmath*}
   \begin{dmath*} \ny{4}^{a} = \cdb{term4.102} \end{dmath*}
   \begin{dmath*} \ny{5}^{a} = \cdb{term5.102} \end{dmath*}
\end{dgroup*}

\clearpage

% =================================================================================================
\section*{Tranformation between two RNC frames}

Same as before but with an improved format (maybe) for the expressions.

\begin{align}
   y^{a} = \ny{0}^{a} + \ny{2}^{a} + \ny{3}^{a} + \ny{4}^{a} + \ny{5}^{a} + \BigO{\eps^6}
\end{align}

\begin{dgroup}
   \begin{dmath} \ny{0}^{a} = Dx^{a} \end{dmath}
\end{dgroup}

\begin{dgroup}
   \begin{dmath} \ny{2}^{a} = \ny{2}^{a}_1 \end{dmath}
   \begin{dmath}   3 \ny{2}^{a}_1 = \cdb{xDxterm12.102} \end{dmath}
\end{dgroup}

\begin{dgroup}
   \begin{dmath} \ny{3}^{a} = \ny{3}^{a}_1 + \ny{3}^{a}_2 \end{dmath}
   \begin{dmath} -12 \ny{3}^{a}_1 = \cdb{xDxterm13.102} \end{dmath}
   \begin{dmath} -24 \ny{3}^{a}_2 = \cdb{xDxterm22.102} \end{dmath}
\end{dgroup}

\begin{dgroup}
   \begin{dmath} \ny{4}^{a} = \ny{4}^{a}_1 + \ny{4}^{a}_2 + \ny{4}^{a}_3 \end{dmath}
   \begin{dmath} -180 \ny{4}^{a}_1 = \cdb{xDxterm14.102} \end{dmath}
   \begin{dmath} -720 \ny{4}^{a}_2 = \cdb{xDxterm23.102} \end{dmath}
   \begin{dmath} -720 \ny{4}^{a}_3 = \cdb{xDxterm32.102} \end{dmath}
\end{dgroup}

\begin{dgroup}
   \begin{dmath} \ny{5}^{a} = \ny{5}^{a}_1 + \ny{5}^{a}_2 + \ny{5}^{a}_3 + \ny{5}^{a}_4 \end{dmath}
   \begin{dmath}  -360 \ny{5}^{a}_1 = \cdb{xDxterm15.102} \end{dmath}
   \begin{dmath} -2160 \ny{5}^{a}_2 = \cdb{xDxterm24.102} \end{dmath}
   \begin{dmath} -1080 \ny{5}^{a}_3 = \cdb{xDxterm33.102} \end{dmath}
   \begin{dmath}  -360 \ny{5}^{a}_4 = \cdb{xDxterm42.102} \end{dmath}
\end{dgroup}

% =================================================================================================
% export checkpoints in json format

\bgroup
\CdbSetup{action=hide}
\begin{cadabra}
   for i in range( len(checkpoint) ):
      cdblib.put ('check{:03d}'.format(i),checkpoint[i],checkpoint_file)
\end{cadabra}
\egroup

\end{document}


%!TEX root = ../main.tex
\section{Random walks and preferential attachment}\label{section:random-walk}

\subsection{Theoretical degree distribution}
One of the weaknesses of the BA model is that this requires a calculation across all existing vertices on the graph. This requirements then destroys the potential for it to exhibit emergent properties based on local behaviour. In real-world networks, the new `vertices' rarely have a global knowledge of the other network vertices. Attachment by performing a random walk is a solution proposed by \citet{Saramaki2004}. In this model, a vertex is chosen at random from existing vertices and then executes a random walk of length $L$ from that vertex. The new vertex then attaches to the destination vertex.

Preferential attachment then follows from the fact that the random walker is more likely to end up at a more highly connected vertex. This better models real-world models such as interconnected webpages, since we are likely to click on links to webpages from webpages we are already visiting.

This model was thought to be able to reproduce the BA degree distribution even for $L=1$ \citep{Saramaki2004,J.P.Saramaki2004}. While this is the case for large $L$, \citet{Cannings2013} later showed that the $L=1$ and $m=1$ degree sequence is fundamentally different from the BA model, unless we allow an indefinite length for the random walk.

\subsection{Numerical simulation}

A few considerations were taken into account when simulating the random walk:

\begin{enumerate}
	\item Should we reset the random walk for each new vertex added, or for each new edge we add to a vertex?
	\item Is the walk self-avoiding?
\end{enumerate}

For point 1, we followed the convention in \citet{Saramaki2004} where we continue the random walk from the previous destination vertex, until we reach $m$ destination vertices, that is, the random walk only resets when a new vertex is added. For point 2, it was decided that the walk will not be self-avoiding, otherwise it would get stuck easily.

For $L=0$, the degree distribution reduces to that of random attachment, as expected.

The raw degree distribution generated by $L = 1$ is shown in \autoref{fig:rw-fixed-n-degree-dist}. We can see quite clearly that the case of $m = 1$ does not follow a power law distribution of preferential attachment.

\begin{figure}
    \centering
    \includegraphics[height=0.5\linewidth]{img/rw-fixed-n-degree-dist}
    \caption{Degree distribution of the random walk model for $N=10^5$, $L=1$, and varying $m$. The dotted lines show the theoretical BA distribution for the same $m$. }
    \label{fig:rw-fixed-n-degree-dist}
\end{figure}

To test whether it follows a power law distribution, we can use the same technique in \autoref{subsection:ppa-numerical-analysis}. This was first done for $L=1$, $N=10^5$, and various values of $m$, and the following p-values were obtained:

\begin{center}
\begin{tabular}{ c | c}
m & $p$-value \\
\hline
1 & 0.00 \\
2 & 0.00 \\
4 & 0.029 \\
8 & 0.768 \\
16 & 0.648 \\
\end{tabular}
\label{table:rw-kstest}
\end{center}

As we can see from the table, for small $m$. Indeed, this was verified by \citet{Cannings2013} who showed that in this case, the degree distribution converges to a degenerate limiting solution in which almost every vertex has degree 1. At $m=8$, however, it is already highly plausible that the degree distribution follows that of the preferential attachment model.

Numerical simulations were done for fixed $N$ and $m$ but varying $L$ from 1 to 8, with the $m$ chosen to be $8$ from previous results and $N = 10^5$. The same technique was used to test whether it follows a power law distribution, and we obtain the result of $p = 0.64$ to an accuracy of $\pm 0.05$ for all $L$. This shows that it is plausible that the degree distribution for the random walk attachment follows a power law, and this holds regardless of $L$.


% This LaTeX was auto-generated from MATLAB code.
% To make changes, update the MATLAB code and republish this document.

\documentclass{article}
\usepackage{graphicx}
\usepackage{color}

\sloppy
\definecolor{lightgray}{gray}{0.5}
\setlength{\parindent}{0pt}

\begin{document}

\section*{Accelerated Gradient Descent}

\subsection*{Contents}

\begin{itemize}
\setlength{\itemsep}{-1ex}
   \item Gradient
   \item Gradient Descent
   \item Accelerated Gradient Descent
   \item Plot contour
   \item Reference
\end{itemize}

\subsection*{Gradient}

\begin{par}
Gradient descent method is based on gradient
\end{par} \vspace{1em}
\begin{par}
$$ \nabla f  = \frac{\partial f}{\partial x_1 }\mathbf{e}_1 + \cdots +
\frac{\partial f}{\partial x_n }\mathbf{e}_n $$
\end{par} \vspace{1em}
\begin{par}
gradient always point to the asent direction
\end{par} \vspace{1em}

\subsection*{Gradient Descent}

\begin{par}
f is object function, and this is unconstrained
\end{par} \vspace{1em}
\begin{par}
$$\min_{x} f $$
\end{par} \vspace{1em}

\subsection*{Accelerated Gradient Descent}

\begin{par}
for t = 1,2,...
\end{par} \vspace{1em}
\begin{par}
$$x^{(t)} = y^{(t-1)} - alpha \nabla f(y^{t-1})$$
\end{par} \vspace{1em}
\begin{par}
$$y^{(t)} = x^{(t)} + (t-1) / (t+2) * (x^{(t)} - x^{(t-1)})$$
\end{par} \vspace{1em}
\begin{verbatim}
f = (@(X) (exp(X(1,:)-1) + exp(1-X(2,:)) + (X(1,:) - X(2,:)).^2));
%f = (@(X) (sin(0.5*X(1,:).^2 - 0.25 * X(2,:).^2 + 3) .* cos(2*X(1,:) + 1 - exp(X(2,:))) ))
\end{verbatim}

\subsection*{Plot contour}

\begin{verbatim}
[X, Y] = meshgrid(-2:0.1:2);
XX = [reshape(X, 1, numel(X)); reshape(Y, 1, numel(Y))];
%surf(X, Y, reshape(f(XX), length(X), length(X)))
contour(X, Y, reshape(f(XX), length(X), length(X)), 50);

hold on;
\end{verbatim}

\includegraphics [width=4in]{test_01.eps}
\begin{par}
plot gradient of function
\end{par} \vspace{1em}
\begin{verbatim}
for i=1:5:length(XX)
    tmp = XX(:,i);
    g = gradient_of_function(f, tmp);
    %plot([tmp(1),tmp(1)+g(1)*0.02],[tmp(1),tmp(2)+g(1)*0.02]);
    quiver(tmp(1),tmp(2),g(1)*0.02,g(2)*0.02);
end
\end{verbatim}

\includegraphics [width=4in]{test_02.eps}
\begin{par}
calculation
\end{par} \vspace{1em}
\begin{verbatim}
x0 = [-1; -1];
\end{verbatim}
\begin{par}
without wolfe step, fix step as alpha = 0.01, $x_k = x_{k-1} + alpha * (-\nabla f)$
\end{par} \vspace{1em}
\begin{verbatim}
[x_gf, v_gf, h_gf] = gradient_fix_step(f, x0)
[x_af, v_af, h_af] = accelerated_gradient_fix_step(f, x0)
\end{verbatim}

        \color{lightgray} \begin{verbatim}
x_gf =

   -0.0287
    0.5194

v_gf =

    2.2750

h_gf =

  Columns 1 through 7

   -1.0000   -1.0014   -1.0012   -0.9997   -0.9970   -0.9933   -0.9886
   -1.0000   -0.9261   -0.8590   -0.7977   -0.7413   -0.6894   -0.6413

  Columns 8 through 14

   -0.9830   -0.9766   -0.9696   -0.9619   -0.9537   -0.9449   -0.9357
   -0.5966   -0.5550   -0.5161   -0.4796   -0.4454   -0.4131   -0.3826

  Columns 15 through 21

   -0.9261   -0.9161   -0.9058   -0.8952   -0.8843   -0.8732   -0.8619
   -0.3538   -0.3266   -0.3007   -0.2761   -0.2526   -0.2303   -0.2089

  Columns 22 through 28

   -0.8503   -0.8387   -0.8269   -0.8150   -0.8029   -0.7908   -0.7786
   -0.1885   -0.1689   -0.1501   -0.1320   -0.1147   -0.0980   -0.0818

  Columns 29 through 35

   -0.7664   -0.7541   -0.7418   -0.7294   -0.7170   -0.7047   -0.6923
   -0.0663   -0.0512   -0.0367   -0.0226   -0.0089    0.0044    0.0172

  Columns 36 through 42

   -0.6800   -0.6676   -0.6553   -0.6431   -0.6308   -0.6186   -0.6065
    0.0298    0.0420    0.0538    0.0654    0.0767    0.0877    0.0985

  Columns 43 through 49

   -0.5944   -0.5823   -0.5704   -0.5585   -0.5466   -0.5348   -0.5231
    0.1090    0.1193    0.1294    0.1393    0.1490    0.1585    0.1678

  Columns 50 through 56

   -0.5115   -0.4999   -0.4884   -0.4770   -0.4657   -0.4544   -0.4433
    0.1770    0.1860    0.1949    0.2036    0.2121    0.2206    0.2289

  Columns 57 through 63

   -0.4322   -0.4212   -0.4103   -0.3995   -0.3887   -0.3781   -0.3675
    0.2370    0.2451    0.2531    0.2609    0.2686    0.2763    0.2838

  Columns 64 through 70

   -0.3570   -0.3466   -0.3363   -0.3261   -0.3160   -0.3059   -0.2960
    0.2912    0.2986    0.3058    0.3130    0.3201    0.3271    0.3341

  Columns 71 through 77

   -0.2861   -0.2764   -0.2667   -0.2571   -0.2475   -0.2381   -0.2288
    0.3409    0.3477    0.3544    0.3611    0.3677    0.3742    0.3806

  Columns 78 through 84

   -0.2195   -0.2103   -0.2012   -0.1922   -0.1833   -0.1745   -0.1657
    0.3870    0.3933    0.3996    0.4058    0.4120    0.4181    0.4241

  Columns 85 through 91

   -0.1570   -0.1484   -0.1399   -0.1315   -0.1231   -0.1148   -0.1066
    0.4301    0.4361    0.4419    0.4478    0.4536    0.4593    0.4650

  Columns 92 through 98

   -0.0985   -0.0904   -0.0825   -0.0746   -0.0668   -0.0590   -0.0513
    0.4706    0.4762    0.4818    0.4873    0.4927    0.4982    0.5035

  Columns 99 through 101

   -0.0437   -0.0362   -0.0287
    0.5089    0.5142    0.5194

x_af =

    0.7415
    1.1499

v_af =

    1.7998

h_af =

  Columns 1 through 7

   -1.0000   -1.0014   -1.0012   -0.9993   -0.9950   -0.9875   -0.9761
   -1.0000   -0.9261   -0.8590   -0.7823   -0.6989   -0.6115   -0.5223

  Columns 8 through 14

   -0.9604   -0.9398   -0.9140   -0.8829   -0.8463   -0.8043   -0.7571
   -0.4333   -0.3459   -0.2615   -0.1807   -0.1043   -0.0325    0.0345

  Columns 15 through 21

   -0.7050   -0.6484   -0.5877   -0.5234   -0.4562   -0.3866   -0.3154
    0.0969    0.1548    0.2086    0.2587    0.3057    0.3499    0.3920

  Columns 22 through 28

   -0.2430   -0.1703   -0.0976   -0.0257    0.0449    0.1139    0.1807
    0.4324    0.4716    0.5100    0.5481    0.5861    0.6242    0.6627

  Columns 29 through 35

    0.2451    0.3067    0.3654    0.4209    0.4732    0.5223    0.5680
    0.7016    0.7410    0.7807    0.8208    0.8611    0.9013    0.9413

  Columns 36 through 42

    0.6106    0.6500    0.6864    0.7198    0.7505    0.7786    0.8042
    0.9809    1.0197    1.0575    1.0941    1.1292    1.1626    1.1940

  Columns 43 through 49

    0.8276    0.8487    0.8679    0.8851    0.9005    0.9142    0.9262
    1.2234    1.2505    1.2752    1.2975    1.3174    1.3346    1.3494

  Columns 50 through 56

    0.9366    0.9455    0.9528    0.9587    0.9631    0.9661    0.9677
    1.3618    1.3718    1.3794    1.3849    1.3884    1.3899    1.3897

  Columns 57 through 63

    0.9679    0.9667    0.9643    0.9606    0.9558    0.9499    0.9429
    1.3878    1.3845    1.3798    1.3740    1.3672    1.3595    1.3510

  Columns 64 through 70

    0.9350    0.9263    0.9169    0.9069    0.8964    0.8855    0.8743
    1.3419    1.3322    1.3222    1.3118    1.3012    1.2904    1.2795

  Columns 71 through 77

    0.8630    0.8517    0.8404    0.8293    0.8185    0.8080    0.7979
    1.2686    1.2578    1.2471    1.2366    1.2263    1.2163    1.2067

  Columns 78 through 84

    0.7883    0.7793    0.7708    0.7629    0.7557    0.7492    0.7433
    1.1974    1.1886    1.1803    1.1725    1.1653    1.1587    1.1527

  Columns 85 through 91

    0.7382    0.7337    0.7300    0.7269    0.7246    0.7229    0.7218
    1.1474    1.1428    1.1388    1.1356    1.1330    1.1312    1.1300

  Columns 92 through 98

    0.7214    0.7216    0.7224    0.7237    0.7256    0.7279    0.7307
    1.1295    1.1296    1.1304    1.1317    1.1336    1.1361    1.1389

  Columns 99 through 101

    0.7339    0.7375    0.7415
    1.1422    1.1459    1.1499

\end{verbatim} \color{black}
    \begin{par}
find suitable step size
\end{par} \vspace{1em}
\begin{verbatim}
[x_g, v_g, h_g] = gradient(f, x0)
[x_a, v_a, h_ax, h_ay] = accelerated_gradient(f, x0)

% built-in method
[x_in, v_in] = fminunc(f, x0)
\end{verbatim}

        \color{lightgray} \begin{verbatim}
x_g =

    0.7960
    1.2038

v_g =

    1.7974

h_g =

  Columns 1 through 7

   -1.0000   -1.0271   -0.4515    0.6432    0.8185    0.7755    0.7859
   -1.0000    0.4778    0.2130    1.0809    1.1279    1.1801    1.2059

  Columns 8 through 12

    0.7925    0.7963    0.7956    0.7959    0.7960
    1.2007    1.2024    1.2033    1.2039    1.2038

x_a =

    0.7960
    1.2038

v_a =

    1.7974

h_ax =

  Columns 1 through 7

   -1.0000   -1.0169    0.7311    1.0381    1.0156    1.0217    0.9513
   -1.0000   -0.0764    0.9765    1.3202    1.4675    1.4183    1.3769

  Columns 8 through 14

    0.9013    0.8435    0.8050    0.7829    0.7738    0.7789    0.7808
    1.3051    1.2559    1.2170    1.1899    1.1882    1.1842    1.1906

  Columns 15 through 21

    0.7879    0.7930    0.7966    0.7966    0.7968    0.7971    0.7968
    1.1945    1.1993    1.2037    1.2046    1.2050    1.2048    1.2047

  Columns 22 through 25

    0.7966    0.7963    0.7961    0.7960
    1.2043    1.2040    1.2038    1.2038

h_ay =

  Columns 1 through 7

   -1.0000   -1.0169    1.1681    1.1609    1.0044    1.0251    0.9073
   -1.0000   -0.0764    1.2398    1.4577    1.5411    1.3902    1.3510

  Columns 8 through 14

    0.8679    0.8031    0.7770    0.7662    0.7668    0.7829    0.7823
    1.2573    1.2214    1.1888    1.1695    1.1870    1.1811    1.1956

  Columns 15 through 21

    0.7936    0.7972    0.7997    0.7966    0.7970    0.7973    0.7965
    1.1978    1.2032    1.2074    1.2054    1.2053    1.2045    1.2046

  Columns 22 through 25

    0.7964    0.7961    0.7959    0.7960
    1.2040    1.2038    1.2036    1.2039

Warning: Gradient must be provided for trust-region algorithm;
  using line-search algorithm instead.

Local minimum found.

Optimization completed because the size of the gradient is less than
the default value of the function tolerance.

x_in =

    0.7961
    1.2039

v_in =

    1.7974

\end{verbatim} \color{black}
    \begin{par}
plot descent steps
\end{par} \vspace{1em}
\begin{verbatim}
for i=2:length(h_gf)
    tmp1 = h_gf(:,i-1);
    tmp2 = h_gf(:,i);
    quiver(tmp1(1),tmp1(2),tmp2(1)-tmp1(1),tmp2(2)-tmp1(2), 0, 'g','LineWidth',3)
end

for i=2:length(h_af)
    tmp1 = h_af(:,i-1);
    tmp2 = h_af(:,i);
    quiver(tmp1(1),tmp1(2),tmp2(1)-tmp1(1),tmp2(2)-tmp1(2), 0, 'b','LineWidth',3)
end

for i=2:length(h_g)
    tmp1 = h_g(:,i-1);
    tmp2 = h_g(:,i);
    quiver(tmp1(1),tmp1(2),tmp2(1)-tmp1(1),tmp2(2)-tmp1(2), 0, 'r','LineWidth',2)
end

for i=2:length(h_ax)
    tmp1 = h_ax(:,i-1);
    tmp2 = h_ax(:,i);
    quiver(tmp1(1),tmp1(2),tmp2(1)-tmp1(1),tmp2(2)-tmp1(2), 0, 'c','LineWidth',2)
end

for i=2:length(h_ay)
    tmp1 = h_ay(:,i-1);
    tmp2 = h_ay(:,i);
    quiver(tmp1(1),tmp1(2),tmp2(1)-tmp1(1),tmp2(2)-tmp1(2), 0, 'm','LineWidth',2)
end
\end{verbatim}

\includegraphics [width=4in]{test_03.eps}

\subsection*{Reference}

\begin{enumerate}
\setlength{\itemsep}{-1ex}
   \item \begin{verbatim}http://stronglyconvex.com/blog/accelerated-gradient-descent.html\end{verbatim}
\end{enumerate}

\end{document}


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

\title{Why ReLU(instead of sigmoid/tanh)?}
\author{}
\date{}

\usepackage{natbib}
\usepackage{graphicx}
\usepackage{amsmath,amssymb}
\usepackage[left=2.5cm,right=2.5cm,top=1cm,bottom=1.25cm]{geometry}
\usepackage{hyperref}
\usepackage{multicol}
\usepackage[export]{adjustbox}
\usepackage{sidecap}
\usepackage{float}
\usepackage[export]{adjustbox}
\hypersetup{colorlinks=true,urlcolor=blue}
\renewcommand{\thesubsection}{\arabic{subsection}.}
\pagenumbering{gobble}
\usepackage{subcaption}

\begin{document}

\maketitle

\section*{ReLU}
The rectified Linear Unit is defined as
\begin{equation*}
    f(x)=\begin{cases}
    x, & \text{$x \geqslant 0$}.\\
    0, & \text{$x<0$}.
    \end{cases}
\end{equation*}
and its derivative is
\begin{equation*}
    f'(x)=\begin{cases}
    1, & \text{$x\geqslant 0$}.\\
    0, & \text{$x<0$}.
    \end{cases}
\end{equation*}

\section*{Avoiding vanishing gradient}
Lets have a look look at the derivative of the three common activation functions- sigmoid, tanh and relu.
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.75]{derivatives.png}
    \caption{Residual block}
    \label{fig:Figure 1}
\end{figure}
As evident from the figure derivative of both sigmoid and tanh approaches zero in both direction. What this means is when using gradient descent based algorithm for learning, the propagated error signal becomes smaller and smaller for those those neurons and they hardly contribute to learning at all. This is especially worse when using large number of layers. Because gradient is multiplicative the earlier layers receive less and less gradient even if the error signal is large to begin with. ReLU on the other hand has a derivative one for positive values. So even if the error signal becomes large there is no saturation of gradient and if large number of layers are stacked the propagated error signal does not diminish as it is always multiplied by one. This greatly accelerates the convergence of gradient descent compared to the sigmoid/tanh functions.

\section*{Sparse representations}
The rectified linear activation allows a network to obtain sparse representations(most of the entries are zero). For example, even if we initialize weights with a zero mean normal distribution, around 50\% of the hidden units will have zero valued output. But why should we care about sparse representation? As it turns out neurons in the human brain encode information in sparse and distributed way. Percentage of neurons active at the same time is believed to between 1\% and 4\%. From a computational point of view it provides several benefits. One is we can easily obtain variable size representations. Not all inputs contain same amount of information. We would probably want a lengthy description for high information input compared to the relatively low information inputs. We can just vary the number of active neurons to obtain different length of description. Also sparse representations are more likely to be linearly separable compared to their dense counterpart. It can also reflect original data format. For example in text related tasks where the original data is very sparse. Sparse representations are not highly entangled like the dense ones. Because for dense representation any small variation in the input data will modify most of the entries in the representation vector. But for sparse representation the set of non-zero features will be roughly be the same by small change in the input. In deep learning we want to disentangle the factors explaining the variation in the data.

\section*{Biological plausibility}
Biological neurons show one sided activation meaning they fire above a certain threshold or not fire at all. ReLU shows this one sided behavior, tanh is anti symmetric around zero which is absent in biological data(sigmoid and tanh are equivalent up to a linear transformation since $tanh(x) = 2\sigma (2x) - 1 $).

\begin{figure}[H]
  \begin{subfigure}[b]{0.4\textwidth}
    \includegraphics[width=\textwidth]{biological_neuron.png}
    \label{fig:1}
  \end{subfigure}
  \begin{subfigure}[b]{0.45\textwidth}
    \includegraphics[width=\textwidth]{activations.png}
    \label{fig:2}
  \end{subfigure}
  \caption{\textit{Left:}Common neural activation functions motivated by biological data. \textit{Right:}sigmoid, tanh and relu activation functions}
\end{figure}

\section*{Better gradient direction} The second derivative of ReLU is zero almost everywhere. This means that the gradient direction is far more useful for learning than it would be with activation functions that introduce second-order effects(sigmoid/tanh).

\section*{Computationally cheap}
Computing ReLU and its derivative much cheaper than sigmoid or tanh where one needs to calculate exponential which is costly.

\section*{Potential problems with ReLU}
\begin{itemize}
    \item ReLU is Non-differentiable at zero which might prevent gradient descent based technique to work. However in practice we can just pick a value of 0 or 1 arbitrarily where the input is 0 and it works just fine.
    \item ReLU outputs are not bounded like sigmoid(bounded in $[0, 1]$) or tanh $[-1, +1]$. So when using deeper networks the gradient can blow up the activation. It can be prevented by using techniques like batch normalization or some other forms of regularization.
    \item ReLU neurons can sometimes be pushed into states in which they become inactive for essentially all inputs. In this state, no gradients flow backward through the neuron, and so the neuron becomes stuck in a perpetually inactive state and "dies." This is known as the dying ReLU problem. In some cases, large numbers of neurons in a network can become stuck in dead states, effectively decreasing the model capacity. This problem typically arises when the learning rate is set too high. It may be mitigated by using Leaky ReLUs instead, which assign a small positive slope to the left of x = 0.
    \item Since ReLU doesn't have any form of symmetry, in order to efficiently represent symmetric / antisymmetric behavior in the data, a rectifier network would need twice as many hidden units as a network of symmetric / antisymmetric activation functions like tanh.
\end{itemize}

\section*{Further reading}
\begin{itemize}
    \item \href{http://proceedings.mlr.press/v15/glorot11a/glorot11a.pdf}{Deep Sparse Rectifier Neural Networks}
    \item \href{https://www.iro.umontreal.ca/~lisa/pointeurs/TR1312.pdf}{Learning Deep Architectures for AI}
    \item \href{https://en.wikipedia.org/wiki/Rectifier_(neural_networks)}{Rectifier (neural networks)}
    \item \href{https://arxiv.org/pdf/1502.01852.pdf}{Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification}
\end{itemize}

\end{document}