\documentclass[12pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{amsmath}
%\usepackage{relsize}

%\usepackage{fixmath}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage[usenames,dvipsnames]{color}
%\usepackage[small,font=it]{caption}
\usepackage{amssymb}
%\usepackage{icomma}
%\usepackage{hyperref}
%\usepackage{mcode}
\usepackage{verbatim}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{units}
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{\footnotesize \parbox{11cm}{Braginskii matrix approach to classical transport from the linearized Fokker-Planck collision operator}}
\rhead{\footnotesize \parbox{2cm}{Stefan Buller}}
\renewcommand\headheight{24pt}
%\usepackage{dirtytalk} \say{} command for quotations

\makeatletter
\newenvironment{tablehere}
  {\def\@captype{table}}
  {}

\newenvironment{figurehere}
  {\def\@captype{figure}}
  {}

\newsavebox\myboxA
\newsavebox\myboxB
\newlength\mylenA

\newcommand*\obar[2][0.75]{% OverBAR, adds bar over an element
    \sbox{\myboxA}{$\m@th#2$}%
    \setbox\myboxB\null% Phantom box
    \ht\myboxB=\ht\myboxA%
    \dp\myboxB=\dp\myboxA%
    \wd\myboxB=#1\wd\myboxA% Scale phantom
    \sbox\myboxB{$\m@th\overline{\copy\myboxB}$}%  Overlined phantom
    \setlength\mylenA{\the\wd\myboxA}%   calc width diff
    \addtolength\mylenA{-\the\wd\myboxB}%
    \ifdim\wd\myboxB<\wd\myboxA%
       \rlap{\hskip 0.5\mylenA\usebox\myboxB}{\usebox\myboxA}%
    \else
        \hskip -0.5\mylenA\rlap{\usebox\myboxA}{\hskip 0.5\mylenA\usebox\myboxB}%
    \fi}

\makeatother

%\def\equationautorefname{ekvation}
%\def\tableautorefname{tabell}
%\def\figureautorefname{figur}
%\def\sectionautorefname{sektion}
%\def\subsectionautorefname{sektion}

\DeclareMathOperator\erf{erf}
%\DeclareMathOperator\mod{mod}

\newcommand{\ordo}[1]{{\cal O}\left( #1 \right)}
\DeclareMathOperator{\sgn}{sgn}
%\renewcommand{\vec}[1]{\boldsymbol{#1}}
\newcommand{\im}{\ensuremath{\mathrm{i}}}
\newcommand{\e}{\ensuremath{\mathrm{e}}}
\newcommand{\E}{\ensuremath{\mathcal{E}}}
\newcommand{\p}{\ensuremath{\partial}}
\newcommand{\Z}{\ensuremath{\mathbb{Z}}}

\newcommand{\bra}[1]{\langle #1 \mid}
\newcommand{\ket}[1]{\mid #1 \rangle}
\newcommand\matris[4]{\ensuremath{\begin{pmatrix} #1 & #2 \\ #3 & #4\end{pmatrix}}}
\renewcommand{\d}{\ensuremath{\mathrm{d}}}
%\DeclareMathOperator*{\sgn}{sgn}
\newcommand{\todo}[1]{\textcolor{red}{#1}}
\newcommand{\lang}{\left\langle}
\newcommand{\rang}{\right\rangle}

%to get leftrightarrow over tensors of rank-2.
\def\shrinkage{2.1mu}
\def\vecsign{\mathchar"017E}
\def\dvecsign{\smash{\stackon[-1.95pt]{\mkern-\shrinkage\vecsign}{\rotatebox{180}{$\mkern-\shrinkage\vecsign$}}}}
\def\dvec#1{\def\useanchorwidth{T}\stackon[-4.2pt]{#1}{\,\dvecsign}}
\usepackage{stackengine}
\stackMath
\def\perfect{\textsc{Perfect}}

\lstset{language=[90]Fortran,
  basicstyle=\ttfamily,
  keywordstyle=\color{red},
  commentstyle=\color{green},
  morecomment=[l]{!\ }% Comment only with space after !
   frame=single,
   breaklines=true,
   postbreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\color{blue}\hookrightarrow\space}}
}

\usepackage[
    backend=biber,
    style=alphabetic,
    citestyle=authoryear,
    natbib=true,
    url=false,
    doi=false,
    isbn=false,
    eprint=false,
    sorting=none,
    maxcitenames=2,
    maxbibnames=3
]{biblatex}

\AtEveryBibitem{\clearfield{month}}
\AtEveryCitekey{\clearfield{month}}
\AtEveryBibitem{\clearfield{issue}}
\AtEveryCitekey{\clearfield{issue}}

\bibliography{../../../plasma-bib}

%=================================================================
\begin{document}
\textcolor{red}{Update 2019-03-08: the normalized heat flux was a factor 2 too high due to using the wrong normalization. This has been corrected.}

\section{Drift-kinetic classical transport}
These notes derive the classical transport under the same set of assumptions as in the linearized drift-kinetic equation solved by the $\delta f$-code neoclassical code \textsc{Sfincs} (and probably most other neoclassical codes).

\subsection{Motivation}
For a mass-ratio expanded ion-impurity collision operator, we found that the classical transport of collisional impurities in W7-X can be comparable to the neoclassical transport. Thus, it would be of interest to have a general numerical tool to calculate the classical transport alongside the neoclassical transport from a drift-kinetic solver such as \textsc{Sfincs}.

\subsection{Gyrophase dependent part of $f$}
In many ways, calculating the classical transport is simpler than calculating the neoclassical transport, as the gyrophase dependent part of the distribution function is smaller than the gyrophase-independent in the expansion parameter $\rho/L$, and the gyrophase dependent part to required order is given entirely by the zeroth order gyrophase independent distribution. When $f_{a0}$ is a Maxwellian $f_{aM}$, the gyrophase dependent part is given by
\begin{equation}
\tilde{f}_{a1} = -\vec{\rho}_a(\gamma) \cdot \nabla f_{Ma} = -\vec{\rho}(\gamma) \cdot \nabla \psi  \frac{\p f_{Ma}}{\p \psi}, \label{eq:ftilde}
\end{equation}
where $\gamma$ is the gyrophase, $\psi$ a flux-label and $\vec{\rho}$ the gyrophase vector
\begin{equation}
\vec{\rho}_a = \frac{1}{\Omega_a}\vec{b} \times \vec{v}_\perp,
\end{equation}
with $\vec{v}_\perp$ the velocity perpendicular to the magnetic field, $\Omega_a=Z_a eB/m_a$ the gyrofrequency, $\vec{b}$ the unit vector in the direction of the magnetic field $\vec{B}$; and $\psi$ a flux-label, which acts as our radial coordinate.

\autoref{eq:ftilde} can be massaged a bit to yield (we drop the tilde since we will only consider the gyrophase dependent part of $f_{a1}$ here)
\begin{equation}
f_{a1} = \frac{1}{\Omega_a}(\vec{b}\times \nabla \psi) \cdot  \vec{v}_\perp\frac{\p f_{Ma}}{\p \psi},
\end{equation}
and we introduce the local coordinate system $\vec{v}_\perp = v_2 \hat{e}_2 + v_3 \hat{e}_3$, where $\hat{e}_2$, $\hat{e}_3$ form an orthonormal triplet with $\vec{b}$. There is some freedom in how we align our $\hat{e}_2$, $\hat{e}_3$ coordinates, and we choose these vectors so that $\hat{e}_2$ is entirely in the $(\vec{b}\times \nabla \psi)$ direction. Thus
\begin{equation}
f_{a1} = \frac{1}{\Omega_a}|\vec{b}\times \nabla \psi| v_2 \frac{\p f_{Ma}}{\p \psi}.
\end{equation}

Finally, we evaluate $\frac{\p f_{Ma}}{\p \psi}$. For heavy impurities, the lowest order distribution may vary on the flux-surface in response to a flux-surface variation of the electrostatic potential, in which case, we write
\begin{equation}
f_{Ma} = \eta_a(\psi) \left(\frac{m_a}{2\pi T_a}\right)^{3/2} \exp{\left(-\frac{m_av^2}{2T_a} - \frac{Z_a e \tilde{\Phi}}{T_a}\right)},
\end{equation}
where the \emph{pseudo-density} $\eta_a$ is related to the ordinary density by
\begin{equation}
\eta_a = n_a \e^{Z_a e \tilde{\Phi}/T_a}.
\end{equation}
The gradient thus becomes
\begin{equation}
\frac{\p f_{Ma}}{\p \psi} = f_{Ma}\left(\frac{\d \ln\eta_a}{\d \psi} + \frac{Z_a e}{T_a} \frac{\p \tilde{\Phi}}{\p \psi}  + \frac{Z_a e \tilde{\Phi}}{T_a} \frac{\d \ln T_a}{\d \psi}
 + \left[\frac{m_a v^2}{2T_a} - \frac{3}{2}\right]  \frac{\d \ln T_a}{\d \psi}\right). \label{eq:dpsif0}
\end{equation}
To simplify notation, we will introduce the quantities
\begin{align}
\alpha_{1a} \equiv & \frac{\d \ln\eta_a}{\d \psi} + \frac{Z_a e}{T_a} \frac{\p \tilde{\Phi}}{\p \psi}  + \frac{Z_a e \tilde{\Phi}}{T_a} \frac{\d \ln T_a}{\d \psi}
  - \frac{3}{2}  \frac{\d \ln T_a}{\d \psi} \\
  \alpha_{2a} \equiv & \frac{\d \ln T_a}{\d \psi},
\end{align}
so that $\frac{\p f_{Ma}}{\p \psi} = f_{Ma}(\alpha_{a1} + \alpha_{2a} x_a^2)$,
where
\begin{equation}
x_a^2 \equiv \frac{m_a v^2}{2T_a} \equiv \frac{v^2}{v_{Ta}^2}.
\end{equation}
Thus
\begin{equation}
f_{a1} = \frac{|\vec{b}\times \nabla \psi| }{\Omega_a}v_2 f_{Ma}(\alpha_{1a} + \alpha_{2a} x_a^2).
\end{equation}

\subsection{Radial fluxes}
The classical radial flux of particle and heat is given by
\begin{align}
  \Gamma_a \equiv \lang \vec{\Gamma}_a \cdot \nabla \psi \rang^{\text{C}} \equiv& \lang \frac{\vec{b} \times \nabla \psi}{Z_a e B} \cdot\vec{R}_a \rang,\label{eq:Gamma} \\
  Q_a \equiv \lang \vec{Q}_a \cdot \nabla \psi \rang^{\text{C}} \equiv& \lang \frac{\vec{b} \times \nabla \psi}{Z_a e B} \cdot\vec{G}_a \rang,\label{eq:Q}
\end{align}
where we have introduced the \emph{friction force} and \emph{energy-weighted friction force}
\begin{align}
  \vec{R}_a &= \int \d^3 v m_a \vec{v} C[f_{a}],\\
  \vec{G}_a &= \int \d^3 v \frac{m_a v^2}{2}  m_a \vec{v} C[f_{a}].
\end{align}
Here, $C[f_a]$ is the Fokker-Planck collision operator accounting for the effects of all species acting on $f_a$
\begin{equation}
    C[f_a] = \sum_b C_{ab}\left[f_{a},f_{b}\right].
    \end{equation}
    An important property of the Fokker-Planck operator is that it preserves the gyrophase dependence of $f_a$. Thus, only the gyrophase dependent part of $f_{a}$ will contribute to the friction force in the $\vec{b} \times \nabla\psi$ direction.
    As the gyrophase dependent part of $f_a$ acts as a small correction to $f_{Ma}$, we can linearize the Fokker-Planck operator around $f_{Ma}$ and write
\begin{equation}
  \begin{aligned}
    C[f_a] =& \sum_b C_{ab}\left[f_{Ma} + f_{1a},f_{Mb}+f_{1b}\right] \\
    \approx & \sum_b C_{ab}[f_{Ma},f_{Mb}] + C_{ab}[f_{Ma},f_{b1}] + C_{ab}[f_{a1},f_{Mb}],
      \end{aligned}
    \end{equation}
    where the gyrophase-independent $C_{ab}[f_{Ma},f_{Mb}]$ part will not contribute to the classical transport. With this result, we write the relevant components of $\vec{R}_a$ and $\vec{G}_a$ as
    \begin{align}
  \vec{b} \times \nabla \psi \cdot \vec{R}_a &= m_a  |\vec{b} \times \nabla \psi|\sum_b \int \!\d^3 v \, v_2 \left(C_{ab}[f_{a1},f_{Mb}] + C_{ab}[f_{Ma},f_{b1}]\right),\\
  \vec{b} \times \nabla \psi \cdot \vec{G}_a &= m_a T_a |\vec{b} \times \nabla \psi| \sum_b \int \!\d^3 v\, v_2 x_a^2  \left(C_{ab}[f_{a1},f_{Mb}] + C_{ab}[f_{Ma},f_{b1}] \right).
    \end{align}
    Inserting our expressions for $f_{1}$, we thus get
    \begin{align}
      &\begin{aligned}
        (\vec{b} \times \nabla \psi) \cdot \vec{R}_a = m_a  |\vec{b} \times \nabla \psi|^2\sum_b \int \!\d^3 v \, v_2 &\left( \frac{\alpha_{1a}}{\Omega_a}  C_{ab}[v_2 f_{Ma},f_{Mb}] + \frac{\alpha_{2a}}{\Omega_a} C_{ab}[v_2 x_a^2 f_{Ma} ,f_{Mb}] \right.\\
          &\left.+ \frac{\alpha_{1b}}{\Omega_b}C_{ab}[f_{Ma},v_2 f_{Mb}] + \frac{\alpha_{2b}}{\Omega_b}C_{ab}[f_{Ma},v_2 x_b^2 f_{Mb} ]\right),
      \end{aligned}\\
      &\begin{aligned}
        (\vec{b} \times \nabla \psi) \cdot \vec{G}_a = m_a T_a |\vec{b} \times \nabla \psi|^2 \sum_b \int \!\d^3 v\, v_2 x_a^2  &\left( \frac{\alpha_{1a} }{\Omega_a}C_{ab}[v_2 f_{Ma},f_{Mb}]
          + \frac{\alpha_{2a}}{\Omega_a}C_{ab}[v_2 x_a^2 f_{Ma} ,f_{Mb}]
        \right.\\
        &\left.+ \frac{\alpha_{1b}}{\Omega_b}C_{ab}[f_{Ma},v_2 f_{Ma}]
          + \frac{\alpha_{2b}}{\Omega_b}C_{ab}[f_{Ma},v_2 x_b^2f_{Ma}]
        \right).
      \end{aligned}
    \end{align}
    These friction-force projections can be conveniently expressed in terms of Braginskii matrix elements
    \begin{align}
  M_{ab}^{jk} = \frac{\tau_{ab}}{n_a} \int v_2 L_j^{(3/2)}(x_a^2) C_{ab}\left[\frac{m_a v_2}{T_a} L_k^{(3/2)}(x_a^2) f_{a0},f_{b0}\right] \\
  N_{ab}^{jk} = \frac{\tau_{ab}}{n_a} \int v_2 L_j^{(3/2)}(x_a^2) C_{ab}\left[f_{a0},\frac{m_b v_2}{T_b} L_k^{(3/2)}(x_b^2)  f_{b0}\right],
\end{align}
where
\begin{equation}
\tau_{ab} = \frac{12 \pi^{3/2} \epsilon_0^2}{\sqrt{2} e^4 \ln \Lambda} \frac{m_a^{1/2} T_a^{3/2}}{Z_a^2 Z_b^2 n_b},
\end{equation}
and the relevant Sonine polynomials are
\begin{align}
  L_0^{(3/2)}(x_a^2) = 1 \\
  L_1^{(3/2)}(x_a^2) = \frac{5}{2} - x_a^2,
\end{align}
so that
\begin{align}
  x_a^2 - \frac{5}{2} =& -L_1^{(3/2)}(x_a^2)  \label{eq:qmotiv}\\
  x_a^2  =& \frac{5}{2}L_0^{(3/2)}(x_a^2) -L_1^{(3/2)}(x_a^2)  \\
  1 = &L_0^{(3/2)}(x_a^2).
\end{align}
\autoref{eq:qmotiv} makes it convenient to calculate the conductive heat flux $q_a = Q_a - \frac{5}{2} T_a \Gamma_a$ rather than directly calculating $Q_a$. With this in mind, we write
\begin{align}
      &\begin{aligned}
        (\vec{b} \times \nabla \psi) \cdot \vec{R}_a = m_a  |\vec{b} \times \nabla \psi|^2\sum_b \frac{n_a}{\tau_{ab}}&\left[ \frac{\alpha_{1a}}{\Omega_a}  \frac{T_a}{m_a}M_{ab}^{00} + \frac{\alpha_{2a}}{\Omega_a}\frac{T_a}{m_a} \left(\frac{5}{2}M_{ab}^{00} - M_{ab}^{01}\right)  \right.\\
          &\left.+ \frac{\alpha_{1b}}{\Omega_b}\frac{T_b}{m_b}N_{ab}^{00}+ \frac{\alpha_{2b}}{\Omega_b}\frac{T_b}{m_b}\left(\frac{5}{2} N_{ab}^{00} - N_{ab}^{01}\right)\right],
      \end{aligned}\\
      &\begin{aligned}
        (\vec{b} \times \nabla \psi) \cdot \left(\vec{G}_a - T_a \frac{5}{2}\vec{R}_a\right) = -m_a T_a |\vec{b} \times \nabla \psi|^2 \sum_b \frac{n_a}{\tau_{ab}} &\left[ \frac{\alpha_{1a} }{\Omega_a} \frac{T_a}{m_a}M_{ab}^{10}
          + \frac{\alpha_{2a}}{\Omega_a}\frac{T_a}{m_a}\left(\frac{5}{2}M_{ab}^{10} - M_{ab}^{11}\right)
        \right.\\
        &\left.+ \frac{\alpha_{1b}}{\Omega_b}\frac{T_b}{m_b} N_{ab}^{10}
          + \frac{\alpha_{2b}}{\Omega_b}\frac{T_b}{m_b}\left(\frac{5}{2}N_{ab}^{10} - N_{ab}^{11}\right)
        \right].
      \end{aligned}
\end{align}
The classical particle-flux and heat-flux thus become

\begin{align}
  \hspace*{-18ex}\Gamma_a =&  \frac{m_a}{Z_a  e^2}\lang |\nabla \psi|^2 \sum_b \frac{n_a n_b}{B^2 \tau_{ab} n_b}\left[ \frac{\alpha_{1a} T_a}{Z_a}  M_{ab}^{00} + \frac{\alpha_{2a} T_a}{Z_a} \left(\frac{5}{2}M_{ab}^{00} - M_{ab}^{01}\right) + \frac{\alpha_{1b} T_b}{Z_b}N_{ab}^{00}+ \frac{\alpha_{2b} T_b}{Z_b}\left(\frac{5}{2} N_{ab}^{00} - N_{ab}^{01}\right)\right] \rang \\
  \hspace*{-18ex}q_a =&  -\frac{T_a m_a}{Z_a e^2}\lang |\nabla \psi|^2 \sum_b \frac{n_a n_b}{B^2 \tau_{ab} n_b }\left[ \frac{\alpha_{1a} T_a}{Z_a}  M_{ab}^{10} + \frac{\alpha_{2a} T_a}{Z_a} \left(\frac{5}{2}M_{ab}^{10} - M_{ab}^{11}\right) + \frac{\alpha_{1b} T_b}{Z_b}N_{ab}^{10}+ \frac{\alpha_{2b} T_b}{Z_b}\left(\frac{5}{2} N_{ab}^{10} - N_{ab}^{11}\right)\right] \rang.
\end{align}

The values for the matrix-components can be found in the appendix of Helander+Sigmar. We can simplify the above expressions slightly by noting the symmetry properties
\begin{align}
  M_{ab}^{jk} &= M_{ab}^{kj}, \\
  N_{ab}^{jk} &= \frac{T_a v_{Ta}}{T_b v_{Tb}} N_{ba}^{kj}, \\
  N_{ab}^{j0} &=-M_{ab}^{j0} = -M_{ab}^{0j},  \\
  N_{ab}^{0j} &= \frac{T_a v_{Ta}}{T_b v_{Tb}} N_{ba}^{j0} =-\frac{T_a v_{Ta}}{T_b v_{Tb}} M_{ba}^{j0}  =-\frac{T_a^{3/2} m_b^{1/2}}{T_b^{3/2} m_a^{1/2}} M_{ba}^{0j}, \\
\end{align}
where the last property follows from applying the other 3. Thus, we only need
\begin{align}
  M_{ab}^{00} =& - \frac{1+\frac{m_a}{m_b}}{\left(1+\frac{m_a T_b}{m_b T_a}\right)^{3/2}} = - \frac{\left(1+\frac{m_a}{m_b}\right)\left(1+\frac{m_a T_b}{m_b T_a}\right)}{\left(1+\frac{m_a T_b}{m_b T_a}\right)^{5/2}}\\
  M_{ab}^{01} =& - \frac{3}{2} \frac{1+\frac{m_a}{m_b}}{\left(1+\frac{m_a T_b}{m_b T_a}\right)^{5/2}} \\
  M_{ba}^{01} =& - \frac{3}{2} \frac{1+\frac{m_b}{m_a}}{\left(1+\frac{m_b T_a}{m_a T_b}\right)^{5/2}} =- \frac{3}{2} \frac{\left(1+\frac{m_a}{m_b}\right)\frac{m_b}{m_a}\left(\frac{m_a T_b}{m_b T_a}\right)^{5/2}}{\left(1+\frac{m_a T_b}{m_b T_a}\right)^{5/2}} \\
  M_{ab}^{11} =& - \frac{\frac{13}{4} + 4 \frac{m_a T_b}{m_b T_a} + \frac{15}{2}\left(\frac{m_a T_b}{m_b T_a}\right)^2 }{\left(1+\frac{m_a T_b}{m_b T_a}\right)^{5/2}} \\
  N_{ab}^{11} =& \frac{27}{4}  \frac{\frac{m_a}{m_b}}{\left(1+\frac{m_a T_b}{m_b T_a}\right)^{5/2}}. \\
\end{align}

The property $N_{ab}^{j0} =-M_{ab}^{j0} = -M_{ab}^{0j}$ also means that the radial electric field does not contribute to the classical heat or particle flux.

Writing out the $\alpha_1$ and $\alpha_2$ and employing the relevant symmetry properties, we obtain the particle flux
\begin{equation}
\begin{aligned}
 \hspace*{-10ex} \Gamma_a =  \frac{m_a}{Z_a e^2}\sum_b \frac{1}{\tau_{ab} n_b}&\left[\lang  n_a n_b\frac{ |\nabla \psi|^2}{B^2} \rang M_{ab}^{00} \left(\frac{ T_a}{Z_a}  \frac{\d \ln\eta_a}{\d \psi} -\frac{ T_b}{Z_b}  \frac{\d \ln\eta_b}{\d \psi}\right) \right.\\
    &\left.+    \lang n_a n_b\frac{  |\nabla \psi|^2}{B^2} e\tilde{\Phi} \rang M_{ab}^{00} \left(\frac{\d \ln T_a}{\d \psi} -\frac{\d \ln T_b}{\d \psi}\right) \right.\\
   &\left. + \lang n_a n_b \frac{|\nabla \psi|^2 }{B^2}\rang\left(  \left(M_{ab}^{00} - M_{ab}^{01}\right)\frac{ T_a}{Z_a}\frac{\d \ln T_a}{\d \psi}
    - \left(M_{ab}^{00} - \frac{m_a T_b}{m_b T_a} M_{ab}^{01}\right)\frac{ T_b}{Z_b}\frac{\d \ln T_b}{\d \psi}\right)\right]
\end{aligned}
\end{equation}
and the heat-flux
\begin{equation}
\begin{aligned}
  \hspace*{-10ex} q_a =  -\frac{T_a m_a}{Z_a e^2} \sum_b \frac{1}{\tau_{ab} n_b} &\left[
    \lang n_a n_b\frac{|\nabla \psi|^2}{B^2 } \rang M_{ab}^{01}\left(\frac{T_a}{Z_a}  \frac{\d \ln\eta_a}{\d \psi} - \frac{T_b}{Z_b}\frac{\d \ln\eta_b}{\d \psi}\right) \right.\\
    &\left.+\lang n_a n_b\frac{|\nabla \psi|^2}{B^2 } e \tilde{\Phi} \rang M_{ab}^{01} \left(\frac{\d \ln T_a}{\d \psi}-\frac{\d \ln T_b}{\d \psi}\right) \right.\\
    &\left.+ \lang n_a n_b\frac{|\nabla \psi|^2}{B^2 } \rang \left(\left(M_{ab}^{01} - M_{ab}^{11}\right)\frac{ T_a}{Z_a} \frac{\d \ln T_a}{\d \psi}
    - \left(M_{ab}^{01} + N_{ab}^{11}\right)\frac{T_b}{Z_b}\frac{\d \ln T_b}{\d \psi}\right) \right]
\end{aligned}
\end{equation}

\section{SFINCS implementation}
\textsc{Sfincs} uses normalized quantities (denoted with a hat), where the normalized radial fluxes are defined in terms of the physical fluxes as
\begin{align}
  \hat{\Gamma}_a &= \frac{\bar{R}}{\bar{n}\bar{v}} \frac{1}{Z_a e} \lang B^{-2} \left(\vec{B} \times \nabla \hat{\psi}\right) \cdot \vec{R}_a \rang, \\
  \hat{Q}_a &= \frac{\bar{R}}{\bar{n}\bar{m}\bar{v}^3} \frac{1}{Z_a e} \lang B^{-2} \left(\vec{B} \times \nabla \hat{\psi}\right) \cdot \vec{G}_a \rang,
\end{align}
where $\hat{\psi} = \psi/(\bar{B}\hat{R}^2)$ is just a new choice of radial coordinate rather than a normalization -- see the \textsc{Sfincs} manual for more details. We also define the dimensionless parameters $\alpha = e\bar{\Phi}/\bar{T}$ and $\Delta = \frac{\sqrt{2\bar{m}\bar{T}}}{e\bar{R}\bar{B}}$, and the dimensionless collisionality $\nu_n$
\begin{align}
  \bar{\nu} &\equiv \frac{\sqrt{2}}{12 \pi^{3/2}} \frac{\bar{n} e^4 \ln \Lambda}{\epsilon_0^2 \bar{m}^{1/2} \bar{T}^{3/2}} \\
  \nu_n &\equiv \bar{\nu} \frac{\bar{R} \sqrt{\bar{m}}}{\sqrt{2\bar{T}}} =\frac{1}{12 \pi^{3/2}} \frac{\bar{n} \bar{R} e^4 \ln \Lambda}{\epsilon_0^2 \bar{T}^{2}},
\end{align}
so that
\begin{equation}
\frac{1}{\tau_{ab} n_b} = \frac{Z_a^2 Z_b^2}{\hat{m}_a^{1/2} \hat{T}_a^{3/2}} \frac{\sqrt{2} \bar{T}^{1/2}}{\bar{n} \bar{R} \bar{m}^{1/2}} \nu_n = \frac{Z_a^2 Z_b^2}{\hat{m}_a^{1/2} \hat{T}_a^{3/2}} \frac{\sqrt{2} \bar{T}^{1/2}\bar{m}^{1/2} \bar{B}}{\bar{n} \bar{R} \bar{B} \bar{m}} \nu_n = \frac{Z_a^2 Z_b^2}{\hat{m}_a^{1/2} \hat{T}_a^{3/2}} \frac{e\bar{B}}{\bar{n}\bar{m}} \Delta \nu_n.
\end{equation}
With this, the normalized particle and conductive heat-flux becomes
\begin{equation}
\begin{aligned}
  \hspace*{-10ex} \hat{\Gamma}_a =   \frac{\Delta^2 \nu_n}{2}\frac{Z_a\hat{m}_a^{1/2}}{\hat{T}_a^{3/2}}\sum_b Z_b^2 &\left[
    \lang  \hat{n}_a \hat{n}_b\frac{ |\bar{R}\nabla \hat{\psi}|^2}{\hat{B}^2} \rang M_{ab}^{00} \left(\frac{ \hat{T}_a}{Z_a}  \frac{\d \ln\eta_a}{\d \hat{\psi}} -\frac{ \hat{T}_b}{Z_b}  \frac{\d \ln\eta_b}{\d \hat{\psi}}\right) \right.\\
    &\left.+    \lang \hat{n}_a \hat{n}_b\frac{  |\bar{R}\nabla \hat{\psi}|^2}{\hat{B}^2} \hat{\Phi}_1 \rang \alpha  M_{ab}^{00} \left(\frac{\d \ln T_a}{\d \hat{\psi}} -\frac{\d \ln T_b}{\d \hat{\psi}}\right) \right.\\
   &\left. + \lang \hat{n}_a \hat{n}_b \frac{| \bar{R} \nabla \hat{\psi}|^2 }{\hat{B}^2}\rang\left(  \left(M_{ab}^{00} - M_{ab}^{01}\right)\frac{ \hat{T}_a}{Z_a}\frac{\d \ln T_a}{\d \hat{\psi}}
    - \left(M_{ab}^{00} - \frac{\hat{m}_a \hat{T}_b}{\hat{m}_b \hat{T}_a} M_{ab}^{01}\right)\frac{ \hat{T}_b}{Z_b}\frac{\d \ln T_b}{\d \hat{\psi}}\right)\right]
\end{aligned}
\end{equation}

\begin{equation}
\begin{aligned}
  \hspace*{-10ex} \hat{q}_a =  -\frac{\Delta^2 \nu_n}{\textcolor{red}{4}} \frac{Z_a\hat{m}_a^{1/2}}{\hat{T}_a^{1/2}} \sum_b Z_b^2 &\left[
    \lang \hat{n}_a \hat{n}_b\frac{|\bar{R}\nabla \hat{\psi}|^2}{\hat{B}^2 } \rang M_{ab}^{01}\left(\frac{\hat{T}_a}{Z_a}  \frac{\d \ln\eta_a}{\d \hat{\psi}} - \frac{\hat{T}_b}{Z_b}\frac{\d \ln\eta_b}{\d \hat{\psi}}\right) \right.\\
    &\left.+\lang \hat{n}_a \hat{n}_b\frac{|\bar{R}\nabla \hat{\psi}|^2}{\hat{B}^2 }  \hat{\Phi}_1 \rang \alpha M_{ab}^{01} \left(\frac{\d \ln T_a}{\d \hat{\psi}}-\frac{\d \ln T_b}{\d \hat{\psi}}\right) \right.\\
    &\left.+ \lang \hat{n}_a \hat{n}_b\frac{|\bar{R}\nabla \hat{\psi}|^2}{\hat{B}^2 } \rang \left(\left(M_{ab}^{01} - M_{ab}^{11}\right)\frac{ \hat{T}_a}{Z_a} \frac{\d \ln T_a}{\d \hat{\psi}}
    - \left(M_{ab}^{01} + N_{ab}^{11}\right)\frac{\hat{T}_b}{Z_b}\frac{\d \ln T_b}{\d \hat{\psi}}\right) \right].
\end{aligned}
\end{equation}
Finally, using $\hat{n}_a = \hat{\eta}_a \e^{-Z_a \alpha \hat{\Phi}_1/\hat{T}_a}$, we get
\begin{equation}
\hat{n}_a\hat{n}_b = \hat{\eta}_a \hat{\eta}_b \exp{\left(-\alpha\hat{\Phi}_1\left[\frac{Z_a}{\hat{T}_a} + \frac{Z_b}{\hat{T}_b}\right]\right)},
\end{equation}
where $\hat{\eta}$ is the ``density'' in \textsc{Sfincs}, which is a flux-function. In the code, we define
\begin{align}
  G_{ab}^{(1)} =& \lang \exp{\left(-\alpha\hat{\Phi}_1\left[\frac{Z_a}{\hat{T}_a} + \frac{Z_b}{\hat{T}_b}\right]\right)}\frac{|\bar{R}\nabla \hat{\psi}|^2}{\hat{B}^2 } \rang \\
  G_{ab}^{(2)} =& \lang \exp{\left(-\alpha\hat{\Phi}_1\left[\frac{Z_a}{\hat{T}_a} + \frac{Z_b}{\hat{T}_b}\right]\right)}\frac{|\bar{R}\nabla \hat{\psi}|^2}{\hat{B}^2 } \hat{\Phi}_1 \rang,
\end{align}
so that
\begin{equation}
\begin{aligned}
  \hspace*{-10ex} \hat{\Gamma}_a =   \frac{\Delta^2 \nu_n}{2}\frac{Z_a\hat{m}_a^{1/2} \hat{\eta}_a}{\hat{T}_a^{3/2}}\sum_b Z_b^2 \hat{\eta}_b&\left[
    G_{ab}^{(1)} M_{ab}^{00} \left(\frac{ \hat{T}_a}{Z_a}  \frac{\d \ln\eta_a}{\d \hat{\psi}} -\frac{ \hat{T}_b}{Z_b}  \frac{\d \ln\eta_b}{\d \hat{\psi}}\right) \right.\\
    &\left.+    G_{ab}^{(2)} \alpha  M_{ab}^{00} \left(\frac{\d \ln T_a}{\d \hat{\psi}} -\frac{\d \ln T_b}{\d \hat{\psi}}\right) \right.\\
   &\left. +G_{ab}^{(1)}\left(  \left(M_{ab}^{00} - M_{ab}^{01}\right)\frac{ \hat{T}_a}{Z_a}\frac{\d \ln T_a}{\d \hat{\psi}}
    - \left(M_{ab}^{00} - \frac{\hat{m}_a \hat{T}_b}{\hat{m}_b \hat{T}_a} M_{ab}^{01}\right)\frac{ \hat{T}_b}{Z_b}\frac{\d \ln T_b}{\d \hat{\psi}}\right)\right],
\end{aligned}
\end{equation}

\begin{equation}
\begin{aligned}
  \hspace*{-10ex} \hat{q}_a =  -\frac{\Delta^2 \nu_n}{\textcolor{red}{4}} \frac{Z_a\hat{m}_a^{1/2} \hat{\eta}_a}{\hat{T}_a^{1/2}} \sum_b Z_b^2 \hat{\eta}_b &\left[
    G_{ab}^{(1)}  M_{ab}^{01}\left(\frac{\hat{T}_a}{Z_a}  \frac{\d \ln\eta_a}{\d \hat{\psi}} - \frac{\hat{T}_b}{Z_b}\frac{\d \ln\eta_b}{\d \hat{\psi}}\right) \right.\\
    &\left.+G_{ab}^{(2)} \alpha M_{ab}^{01} \left(\frac{\d \ln T_a}{\d \hat{\psi}}-\frac{\d \ln T_b}{\d \hat{\psi}}\right) \right.\\
    &\left.+ G_{ab}^{(1)} \left(\left(M_{ab}^{01} - M_{ab}^{11}\right)\frac{ \hat{T}_a}{Z_a} \frac{\d \ln T_a}{\d \hat{\psi}}
    - \left(M_{ab}^{01} + N_{ab}^{11}\right)\frac{\hat{T}_b}{Z_b}\frac{\d \ln T_b}{\d \hat{\psi}}\right) \right],
\end{aligned}
\end{equation}
and
\begin{equation}
\hat{Q}_a = \hat{q}_a + \frac{5}{\textcolor{red}{4}} \hat{T}_a \hat{\Gamma}_a.
\end{equation}

\paragraph{Thanks to Sarah Newton} for bringing to my attention that the classical transport can be calculated using the Braginskii matrices.

\end{document}


% !TEX TS-program = pdflatexmk

\documentclass[modern]{aastex61}

\newcommand{\pystan}{\texttt{PyStan}}
\newcommand{\python}{\texttt{Python}}
\newcommand{\RRR}{\texttt{R}}
\newcommand{\stan}{\texttt{Stan}}

\begin{document}

\title{Astrophysics With Gravitational Wave Detections: Data Analysis}
\author{Will M. Farr}
\affil{Birmingham Institute for Gravitational Wave Astrophysics and School of Physics and Astronomy, University of Birmingham}
\email{w.farr@bham.ac.uk}

\begin{abstract}
  This series of three lectures is intended to give you an
  introduction to the data analysis techniques used in gravitational
  wave astrophysics and astrostatistics more generally.
\end{abstract}

\section{Syllabus}

The goal of this course is to introduce you to some of the techniques
used in gravitational wave astrophysics and also to give you some
practice with the \emph{implementation} of statistical analyses.  To
that end, the ``lectures'' will hopefully be very interactive, with
opportunities to ask questions, work with your peers to develop code,
and see in real-time the results of your analyses.  Of course you do
not have to participate to this degree if you do not want;
nevertheless, I think you will get the most out of the school if you
do.

\emph{Before Monday afternoon}, please install a working \stan{}
implemtation \citep{Stan}.  I personally favour
\href{http://mc-stan.org}{\pystan} \citep{PyStan} on top of the
\href{https://www.continuum.io}{Anaconda} \python{} distribution, but
if you are more familiar with \RRR or the command line, go ahead.  If
you are using \pystan{}, you will know your installation is working
when you can run the ``eight schools'' example from the
\href{https://pystan.readthedocs.io/en/latest/getting_started.html}{\pystan{}
  manual}\footnote{For an interesting, intuitive description of
  hierarchical modelling---which will feature in Lecture 3---see
  \href{http://andrewgelman.com/2014/01/21/everything-need-know-bayesian-statistics-learned-eight-schools/}{here}.}.

Each lecture will come with reading material; some I will ask you to
familariase yourself with in advance, and some is just in case you are
interested to go further than I do in the lecture.  Please have a look
(it doesn't have to be extensive---just get familiar with the content,
or have questions about wherever you get stuck) at the material before
the lecture so we can discuss it straight away.

The material for the course, including this document, can be found on
\href{https://github.com/farr/GWDataAnalysisSummerSchool}{GitHub}.

\subsection{Lecture 1: Monday Afternoon.  The Bayesics}

It may seem simple, but this entire lecture will be concerned with the
material in \citet{Hogg2010}.  Please familarise yourself with it
before the lecture; we will be writing code to solve some of the
associated problems during the lecture.

We will spend a bit of time talking about statistical notation and the
notion of conditional versus joint distributions, and then we will
dive in to fitting a line to data!  If you internalise all the
discussion from \citet{Hogg2010} you will be ready to build models
that are better than 90\% of the ``state-of-the-art'' papers on the
arXiv.  To explore the ideas in \citet{Hogg2010}, we will use the
modelling language \stan \citep{Stan}, so I will give a brief tutorial
on the structure of the language (the syntax should be familiar to
those who have programmed in \texttt{C} or similar languages before)
and how it is optimised for describing and solving the sorts of
statistical problems we are going to be talking about in this course.

Further reading (for those interested) includes
\href{http://dan.iel.fm/posts/fitting-a-plane/}{this blog post} by
Daniel Foreman-Mackey, \citet{Kelly2007}, \citet{Lieu2017}, \ldots.

\subsection{Lecture 2: Thursday Morning. Extracting Astrophysical
  Information from Gravitational Waves.}

This lecture will discuss the physics from
\citet{Abbott2017BasicPhysics} and the techniques described in
\citet{Veitch2015} for extracting information about gravitational wave
sources.  Please familarise yourself with these references before the
lecture.

We will explore how the various parameters of a merging compact object
system are encoded in the gravitational waveform
\citep{Abbott2017BasicPhysics}.  We will talk about the noise sources
in interferometric gravitational wave detectors and justify the
likelihood function given in \citet{Veitch2015}.  We will implement
this likelihood function in \stan{}, and use it to estimate the
parameters of GW150914 \citep{Abbott2016GW150914} from
\href{https://losc.ligo.org/events/GW150914/}{actual LIGO data}.

For further reading, consider \citet{Abbott2016PEPaper},
\citet{Abbott2016Wavelet}, \ldots.

\subsection{Lecture 3: Friday Afternoon.  Population Modelling.}

This lecture will deal with three different topics in population
modelling.  First, we will derive the ``fundamental likelihood'' of
population modelling, following
\href{http://dan.iel.fm/posts/histogram1/}{this blog post} by Dan
Foreman-Mackey.  Then we will show how this likelihood generalises to
a \emph{mixture} model used to infer rates of black hole mergers in
\citet{Abbott2016Rate}; we will write some \stan{} code to infer rates
of binary black hole mergers and apply it to real data from that
paper.  Finally, we will discuss \emph{model selection} in a
gravitational waves context, following \citet{Farr2017}.  Please
familiarise yourself with these references before the lecture.

For further reading, consider \citet{Abbott2016RatesSupplement},
\citet{Foreman-Mackey2014}, \citet{Farr2015}, \citet{Loredo1995},
\citet{Farr2011}, \ldots.

\bibliography{syllabus}

\end{document}


\documentclass[a4paper,man,natbib]{apa6}
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
% Common Packages - Delete Unused Ones %
\usepackage{setspace}
\usepackage{amsmath}
\usepackage[cache=false]{minted}
\usepackage{graphicx}
\usepackage{caption}
\graphicspath{ {./images/} }
% End Packages %

\title{Data Visualization Homework 5}
\shorttitle{DAT 332 HW5}
\author{Brandon Hosley \\ UIN: 676399238}
\date{}
\affiliation{Yanhui Guo, Ph.D}
%\abstract{}

\begin{document}
\maketitle
\singlespacing
\raggedbottom

\section{Problem 1}
Please run the following two commands and observe the differences of two graphs. Write the difference and the reasons. (10 points)
\begin{minted}{r}
p <-ggplot(heightweight,aes(x=ageYear,y=heightIn)) +geom_point()
# a.
p + theme(axis.title.x =element_text(colour="red")) + theme_bw()
# b.
p + theme_bw() + theme(axis.title.x =element_text(colour="red",size=12))
\end{minted}
\includegraphics[width=0.45\linewidth]{Q1a}
\includegraphics[width=0.45\linewidth]{Q1b} \\
GGPlot draws in a manner similar to plain CSS; More recent commands (commands later in the code) overwrite older commands. In this example the Black and White theme overwrites the red color command in A. In B the second command overrides the BW theme for the targeted element, the X-axis Title.

\pagebreak

\section{Problem 2}
Use the PlantGrowth data frame to draw following graphs, respectively.  Write down the used functions in ggplot2 (25 points)

\begin{minted}{r}
library(gcookbook)
g <- ggplot(PlantGrowth, aes(x=group, y=weight)) + geom_boxplot()
\end{minted}

\subsection{$($a$)$}
\begin{minted}{r}
g + theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank())
\end{minted}
\includegraphics[width=0.5\linewidth]{Q2a}

\subsection{$($b$)$}
\begin{minted}{r}
g
\end{minted}
\includegraphics[width=0.5\linewidth]{Q2b}

\pagebreak

\subsection{$($c$)$}
\begin{minted}{r}
g + theme(
	panel.grid.minor.x=element_blank(),
	panel.grid.major.x=element_blank(),
	panel.grid.minor.y=element_line(size=0.5),
	panel.grid.major.y=element_line(size=1))
\end{minted}
\includegraphics[width=0.45\linewidth]{Q2c}

\subsection{$($d$)$}
\begin{minted}{r}
g + theme(panel.grid.minor.y=element_blank(), panel.grid.major.y=element_blank())
\end{minted}
\includegraphics[width=0.45\linewidth]{Q2d}

\pagebreak

\section{Problem 3}
Please use the DF1 data frame to draw following graphs, respectively.  Write down the used functions in ggplot2 (40 points)
\begin{minted}{r}
DF1 <- data.frame(x = 1:10, y = 1:10, gp = factor(rep(1:2, each = 5)))
p0 <- ggplot(DF1, aes(x = x, y = y, colour = gp)) + geom_point()
# Formatting Asthetics
p0 <- p0 + theme_light() + theme(
	panel.grid.major=element_blank(),
	panel.grid.minor=element_blank(),
	panel.border=element_blank(),
	legend.key=element_blank())
\end{minted}

\subsection{$($a$)$}
\begin{minted}{r}
p0 + ggtitle("Top") + theme(legend.position="top")
\end{minted}
\includegraphics[width=0.35\linewidth]{Q3a}

\subsection{$($b$)$}
\begin{minted}{r}
p0 + ggtitle("Bottom") + theme(legend.position="bottom")
\end{minted}
\includegraphics[width=0.35\linewidth]{Q3b}

\pagebreak

\subsection{$($c$)$}
\begin{minted}{r}
p0 + ggtitle("Left") + theme(legend.position="left")
\end{minted}
\includegraphics[width=0.3\linewidth]{Q3c}

\subsection{$($d$)$}
\begin{minted}{r}
p0 + ggtitle("None") + theme(legend.position="none")
\end{minted}
\includegraphics[width=0.3\linewidth]{Q3d}

\subsection{$($e$)$}
\begin{minted}{r}
p0 + ggtitle("Inside") + theme(legend.position=c(0.1,0.8))
\end{minted}
\includegraphics[width=0.3\linewidth]{Q3e}

\subsection{$($f$)$}
\begin{minted}{r}
p0 + ggtitle("Default")
\end{minted}
\includegraphics[width=0.3\linewidth]{Q3f}

\pagebreak

\section{Problem 4}
Please use the DF1 data frame to draw following graphs, respectively.  Write down the used functions in ggplot2 (25 points)
\begin{minted}{r}
DF1 <- data.frame(x = 1:10, y = 1:10, gp = factor(rep(1:2, each = 5)))
p0 <- ggplot(DF1, aes(x = x, y = y, colour = gp)) + geom_point()
# Aesthetic Formatting
p0 <- p0 + theme_light() + theme(
    panel.grid.major=element_blank(),
    panel.grid.minor=element_blank(),
    panel.border=element_blank(),
    legend.key=element_blank(),
    legend.position=c(0.5,0.5))
\end{minted}

\subsection{$($a$)$}
\begin{minted}{r}
p0 + ggtitle("Left Justified") + theme(legend.justification="left")
\end{minted}
\includegraphics[width=0.4\linewidth]{Q4a}

\subsection{$($b$)$}
\begin{minted}{r}
p0 + ggtitle("Right Justified") + theme(legend.justification="right")
\end{minted}
\includegraphics[width=0.4\linewidth]{Q4b}

\subsection{$($c$)$}
\begin{minted}{r}
p0 + ggtitle("Center Top Justified") + theme(legend.justification=c(0.5,1))
\end{minted}
\includegraphics[width=0.4\linewidth]{Q4c}

\subsection{$($d$)$}
\begin{minted}{r}
p0 + ggtitle("Bottom Left Justified") + theme(legend.justification=c(0,0))
\end{minted}
\includegraphics[width=0.4\linewidth]{Q4d}

\end{document}

2020-Spr Data Visualization/Homework 5/Homework5.tex


\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{float}
\usepackage{amsmath}
\usepackage{enumerate}
\usepackage{tikz}
\usepackage{tikz-qtree}
\usetikzlibrary{automata,positioning}
\usepackage{tabularx}

\usepackage[hmargin=3cm,vmargin=6.0cm]{geometry}
%\topmargin=0cm
\topmargin=-2cm
\addtolength{\textheight}{6.5cm}
\addtolength{\textwidth}{2.0cm}
%\setlength{\leftmargin}{-5cm}
\setlength{\oddsidemargin}{0.0cm}
\setlength{\evensidemargin}{0.0cm}

%misc libraries goes here
\usepackage{tikz}
\usetikzlibrary{automata,positioning}

\begin{document}

\section*{Student Information}
%Write your full name and id number between the colon and newline
%Put one empty space character after colon and before newline
Full Name : Zeynep Özalp \\
Id Number : 2237691
% Write your answers below the section tags
\section*{Answer 1}

\subsection*{a.}
\begin{enumerate}[(i)]
\item
$G=\{V,\Sigma,R,S\}$\\
$V=\{a,b,S,S_1,S_2\}$\\
$\Sigma=\{a,b\}$\\
$R=\{S->aSa|bS_1, \\
S_1->aS_1a|b \}$
\item
E generates the strings of even length except the empty sting, O generates the strings of odd length. The length of string produced by S is $even+even+3=odd$ or $odd+odd+3=odd$.\\
$G=\{V,\Sigma,R,S\}$\\
$V=\{a,b,S,E,O\}$\\
$\Sigma=\{a,b\}$\\
$R=\{S->aEaEa|bEbEb|aOaOa|bObOb|aaa|bbb, \\
E->aEa|aEb|bEa|bEb|aa|bb, \\
O->aE|Ea|bE|Eb|a|b \}$
\item
$S_1$ creates the string $a^ib^jc^k$ where $i\neq j$ and $S_2$ creates the string $a^ib^jc^k$ where $j\neq k$. For $S_1$, $T_1$ creates the string $a^ib^j$ where $i\neq j$. $T_2$ creates the string $a^ib^j$ where $i=j$. $A_1$ is $a^+$, $B_1$ is $b^+$ and $C_1$ is $c^*$. For $S_2$, $T_3$ creates the string $b^jc^k$ where $j\neq k$. $T_4$ creates the string $b^jc^k$ where $k=j$. $A_2$ is $a^*$, $B_2$ is $b^+$ and $C_2$ is $c^+$. \\
$G=\{V,\Sigma,R,S\}$\\
$V=\{a,b,S,S_1,S_2,T_1,T_2,T_3,T_4,A_1,A_2,B_1,B_2,C_1,C_2\}$\\
$\Sigma=\{a,b\}$\\
$R=\{
S -> S_1 | S_2, \\
S_1 -> T_1C_1, \\
T_1 -> A_1T_2 | T_2B_1, \\
T_2 -> aT_2b | e, \\
A_1 -> aA_1, \\
B_1 -> bB_1, \\
C_1 -> cC_1 | e, \\
S_2 -> A_2T_3, \\
T_3 -> B_2T_4 | T_4C_2, \\
T_4 -> bT_4c | e, \\
B_2 -> bB_2, \\
C_2 -> cC_2, \\
A_2 -> aA_2 | e
\}$
\item
$S_1$ generates all strings, $S_2$ generates a $w_ic$, $S_3$ generates the sequence of $w_ic$'s. $S_4$ generates $w_jc\{sequence of w_ic's\}cw_j^R$.\\
$G=\{V,\Sigma,R,S\}$\\
$V=\{a,b,S,S_1,S_2,S_3,S_4\}$\\
$\Sigma=\{a,b\}$\\
$R=\{S->S_3S_4, \\
S_1->aS_1|bS_1|e, \\
S_2->aS_1c|bS_1c, \\
S_3->S_2S_3|S_2, \\
S_4->aS_4a|bS_4b|cS_3c \}$
\end{enumerate}

\subsection*{b.}
\subsubsection*{PART 1}
$L(G)\subseteq L$, every string generated by G is in L. \\
Use mathematical induction. \\\\
1. Basis step: Derivation in 2 steps.
$$S\Rightarrow aB\Rightarrow ab \in L$$
$$S\Rightarrow bA\Rightarrow ba \in L$$\\
2. Inductive Hypothesis: Assume that for every derivation $S\Rightarrow^* w$ with $n\geq2$ steps, $w\in L$\\\\
3. Inductive Step: Let $S\Rightarrow^*w$ be a derivation with $n+2$ steps, $n\in N$. Since $n+2> 2$, the derivation starts with either $abS,baS,aABB$ or $bBAA$.\\\\
i)$S\Rightarrow aB\Rightarrow abS \Rightarrow^* abw_1$. By ind. hyp., $w_1\in L$,$w_1$ has equal number of a's and b's, so does $abw_1$ and $abw_1 \in L$.\\
ii)$S\Rightarrow bA\Rightarrow baS \Rightarrow^* baw_2$. By ind. hyp., $w_2\in L$,$w_2$ has equal number of a's and b's, so does $baw_2$ and $baw_2 \in L$.\\
iii)$S\Rightarrow aB\Rightarrow abS \Rightarrow abaB\Rightarrow abaABB$. So, the string $aABB$ can be generated by $S$ in two steps and in $L$. \\
iv)$S\Rightarrow bA\Rightarrow baS \Rightarrow babA\Rightarrow babBAA$. So, the string $bBAA$ can be generated by $S$ in two steps and in $L$. \\
Therefore, every string generated by G is in L.
\subsubsection*{PART 2}
$L\subseteq L(G)$, every string in L can be generated by G. \\
$L=\{ab,ba,...\}$
Use mathematical induction. \\\\
1. Basis step: For $|w|=2$, $S \Rightarrow aB \Rightarrow ab$ and $S \Rightarrow bA \Rightarrow ba$. So, $ab \in L(G)$ and $ba \in L(G)$.\\\\
2. Inductive Hypothesis: Assume that all strings $|w| \leq k-1$ in L can be generated by G.\\\\
3. Inductive Step: Suppose $x=a_1a_2a_3...a_k \in L$ with $k>2$, $a_i\in \Sigma$. Now assume that $a_1a_2=ab$. Define $N_a(x)$ as the number of a's in x and
$$d_i=N_a(x)-N_b(x), 0\leq i \leq k$$
$$t=min(i>0, d_i=0)$$

Since we choose $a_1a_2 = ab$, $ d_1 = 1, d_2=0$. Also, $d_k = 0$ since $x \in L$. Thus, there exists a smallest i such that $d_i=-1$ and $d_{i+1}=0$. This value is represented as t. Since t was chosen to be the smallest possible and $d_1>0$, we must have $d_t<0$. Since $d_t<0$ and $d_{t+1}=0$, we must have $a_ta_{t+1} = ba$. Therefore, we have
$$x=aba_3...a_{t-1}baa_{t+2}...a_k$$
Let $y=a_3...a_{t-1}$ and $z=a_{t+2}...a_k$. Clearly, because $x\in L$, y and z have equal number of a's and b's, $y \in L$ and $z \in L$. By inductive hypothesis, since $|y|<k$ and $|z|<k$, $y,z \in L(G)$. This means $S\Rightarrow ^* y$ and $S\Rightarrow ^* z$.
$$S\Rightarrow aB \Rightarrow abS \Rightarrow ^* abyS \Rightarrow  abybA \Rightarrow abybaS \Rightarrow ^* abybaz = x$$
Therefore, $x\in L(G)$ and for all $x\in L \rightarrow x\in L(G)$. This completes the proof that $L(G)$ is the set of all non-empty strings in $\Sigma^*$ that have equal number of a's and b's.

\section*{Answer 2}

\subsection*{a.}
% Sample empty trees for your convenience, edit/use as you wish
$$s=aab$$
$S\Rightarrow aaB \Rightarrow aab$\\
$S\Rightarrow Ab \Rightarrow Aab \Rightarrow aab$\\

\begin{minipage}[c]{\textwidth}
\hfill
\Tree [.S [.a ] [.a ] [.B b ]]
\hspace{0.2\linewidth}
\Tree [.S [.A [.A a ] [.a ] ] [.b ] ]
\hspace{0.3\linewidth}
\end{minipage}

\subsection*{b.}
$L(G)=\{ab,aab,aaab,...\}$\\
G' is an equivalent unambiguous CFG of G.\\
$G'=\{V', \Sigma, R', S\}$, $V'=\{a,b,S,A\}$,\\
$R=\{S\rightarrow Ab, A\rightarrow a|aA\}$

\subsection*{c.}
% Sample empty tree for your convenience, edit/use as you wish
\Tree [.S [.A [.A a ] [.a ] ] [.b ] ]
\section*{Answer 3}

\subsection*{a.}
$L=\{w\in \{a,b\}^*|w=(aa)^i(bbb)^i, i\geq 0\}$
\subsection*{b.}
$M=\{K,\Sigma,\Gamma, \Delta, q_0, q_7\}$\\
$K=\{q_0,q_1,q_2,q_3,q_4,q_5,q_6,q_7\}$, $\Sigma=\{a,b\}, \Gamma=\{Z_0,X,Y\}$\\
$T=\{Z_0,X\}$\\

% Sample empty diagram for your convenience, edit/use as you wish.
\tikzset{every loop/.style={min distance=10mm,looseness=10}}
\begin{tikzpicture}[node distance=3.5cm,on grid,auto]
\node[state,initial] (q_0) {$q_0$};
\node[state] (q_1) [right of=q_0] {$q_1$};
\node[state] (q_2) [right of=q_1] {$q_2$};
\node[state] (q_3) [below of=q_1] {$q_3$};
\node[state] (q_4) [right of=q_2] {$q_4$};
\node[state] (q_5) [below of=q_2] {$q_5$};
\node[state] (q_6) [below of=q_4] {$q_6$};
\node[state,accepting] (q_7) [below right of=q_6] {$q_7$};
\path[->]
  (q_0) edge node {$e,e\rightarrow Z_0$} (q_1)
  (q_1) edge [loop=90 above, swap] node {$b,T\rightarrow XT$} () edge [loop above] node {$a,T\rightarrow XT$} () edge node {$a,T\rightarrow T$} (q_2) edge [swap] node {$b,T\rightarrow T$} (q_3)
  (q_2) edge [loop=90 above, swap] node {$b,X\rightarrow e$} () edge [loop above] node {$a,X\rightarrow e$} () edge node {$e,Z_0\rightarrow Z_0$} (q_4)
  (q_3) edge [loop left] node {$b,X\rightarrow e$} () edge [loop below] node {$a,X\rightarrow e$} () edge node {$e,Z_0\rightarrow Z_0$} (q_5)
  (q_4) edge [loop=90 above, swap] node {$b,T\rightarrow YT$} () edge [loop above] node {$a,T\rightarrow YT$} () edge node {$b,T\rightarrow T$} (q_6)
  (q_5) edge [loop below] node {$b,T\rightarrow YT$} () edge [loop above] node {$a,T\rightarrow YT$} () edge node {$a,T\rightarrow T$} (q_6)
  (q_6) edge [loop below] node {$b,Y\rightarrow e$} () edge [loop right] node {$a,Y\rightarrow e$} () edge node {$e,Z_0 \rightarrow e$} (q_7)
  ;
\end{tikzpicture}
\\\\\\
\subsection*{c.}
\begin{enumerate}[(i)]
\item
$L(M)=\{a^ib^jc^k\in \{a,b,c\}^*|i,j,k\geq 0, i=j$ or $j>k$ or $j<k\}$\\
$M=(K, \{a,b,c\}, \Gamma, \Delta, q_0, \{q_3,q_7\})$\\
$K=\{q_0,q_1,q_2,q_3,q_4,q_5,q_6,q_7,q_8,q_9,q_{10}\}$, $\Gamma=\{Z_0,a,b\}$\\

\begin{tabular}{c} %for alignment purposes
% Sample empty diagram for your convenience, edit/use as you wish.
\tikzset{every loop/.style={min distance=10mm,looseness=10}}
\begin{tikzpicture}[node distance=3.5cm,on grid,auto]
\node[state,initial] (q_0) {$q_0$};
\node[state] (q_1) [right of=q_0] {$q_1$};
\node[state] (q_2) [right of=q_1] {$q_2$};
\node[state,accepting] (q_3) [right of=q_2] {$q_3$};
\node[state] (q_4) [below of=q_1] {$q_4$};
\node[state] (q_5) [right of=q_4] {$q_5$};
\node[state] (q_6) [right of=q_5] {$q_6$};
\node[state,accepting] (q_7) [right of=q_6] {$q_7$};
\node[state] (q_8) [below of=q_5] {$q_8$};
\node[state] (q_9) [below of=q_6] {$q_9$};
\node[state] (q_{10}) [below of=q_0] {$q_{10}$};
\path[->]
  (q_0) edge node {$e,e\rightarrow Z_0$ (1)} (q_1) edge [swap] node {$e,e\rightarrow Z_0$ (7)} (q_{10})
  (q_1) edge [loop above] node {$a,e\rightarrow a$ (2)} () edge node {$e,e\rightarrow e$ (3)} (q_2)
  (q_2) edge [loop above] node {$b,a\rightarrow e$ (4)} () edge node {$e,Z_0\rightarrow e$ (5)} (q_3)
  (q_3) edge [loop above] node {$c,e\rightarrow e$ (6)} ()
  (q_4) edge [loop above] node {$b,e\rightarrow b$ (10)} () edge node {$e,e\rightarrow e$ (11)} (q_5) edge [swap] node {$e,e \rightarrow e$ (16)} (q_8)
  (q_5) edge [loop above] node {$c,b\rightarrow e$ (12)} () edge node {$e,b\rightarrow e$ (13)} (q_6)
  (q_6) edge [loop above] node {$e,b\rightarrow e$ (14)} () edge node {$e,Z_0\rightarrow e$ (15)} (q_7)
  (q_8) edge [loop below] node {$c,b\rightarrow e$ (17)} () edge node {$c,Z_0\rightarrow Z_0$ (18)} (q_9)
  (q_9) edge [loop below] node {$c,Z_0\rightarrow Z_0$ (19)} () edge [swap] node {$e,Z_0\rightarrow e$ (20)} (q_7)
  (q_{10}) edge [loop below] node {$a,e\rightarrow e$ (8)} () edge [swap] node {$e,e\rightarrow e$ (9)} (q_4)
  ;
\end{tikzpicture}
\end{tabular}

\item
% Sample empty table for your convenience, edit/use as you wish.
\begin{tabular}{|llrl|}
\hline
State & Input & Stack & Transition\\
$q_0$ & aabcc & e     &  - \\
$q_{10}$ & aabcc & $Z_0$ & (7) \\
$q_{10}$ & abcc  & $Z_0$     & (8) \\
$q_{10}$ & bcc   & $Z_0$     & (8) \\
$q_4$ & bcc   & $Z_0$     & (9) \\
$q_4$ & cc   & $b,Z_0$     & (10) \\
$q_8$ & cc   & $b,Z_0$     & (16) \\
$q_8$ & c   & $Z_0$     & (17) \\
$q_9$ & e  & $Z_0$     & (18) \\
$q_7$ & e   & e     & (20) \\
$q_7$ & e   & e     & Accepts. \\

\hline
\end{tabular}
% Sample empty table for your convenience, edit/use as you wish.
% You are expected to trace all the possible computations
\begin{tabular}{|llrl|}
\hline
State & Input & Stack & Transition \\
$q_0$ & bac & e & - \\
$q_1$ & bac & $Z_0$ & (1) \\
$q_2$ & bac & $Z_0$ & (3) \\
$q_3$ & bac & $Z_0$ & (5) \\
$q_3$ & bac & $Z_0$ & Rejects. \\
\hline
$q_0$ & bac & e & - \\
$q_{10}$ & bac & $Z_0$ & (7) \\
$q_4$ & bac & $Z_0$ & (9) \\
$q_4$ & ac & $b,Z_0$ & (10) \\
$q_5$ & ac & $b,Z_0$ & (11) \\
$q_5$ & ac & $b,Z_0$ & Rejects. \\
\hline
$q_0$ & bac & e & - \\
$q_{10}$ & bac & $Z_0$ & (7) \\
$q_4$ & bac & $Z_0$ & (9) \\
$q_4$ & ac & $b,Z_0$ & (10) \\
$q_8$ & ac & $b,Z_0$ & (11) \\
$q_8$ & ac & $b,Z_0$ & Rejects. \\
\hline
\end{tabular}
\end{enumerate}

\section*{Answer 4}

\subsection*{a.}
Construct a PDA $M=( \{p,q\} ,\Sigma ,V,\Delta ,p,\{q\})$ such that $L(M)=L(G)$. By Lemma 3.4.1, \\
$\Delta = \\
((p,e,e),(q,E)),\\
((q,e,E),(q,E+T)),\\
((q,e,E),(q,T)),\\
((q,e,T),(q,T\times F)),\\
((q,e,T),(q,F)),\\
((q,e,F),(q,(E))),\\
((q,e,F),(q,a)),\\
((q,a,a),(q,e)),\\
((q,+,+),(q,e)),\\
((q,\times,\times),(q,e)),\\
((q,(,(),(q,e)),\\
((q,),)),(q,e))\\$

\subsection*{b.}
First add $\Delta '$ the transitions ((s',e,e),(s,Z)) and ((f,e,Z),(f',e)) for each $f\in F$. Now, $\Delta '$ consists of these transitions described above and all the transitions in $\Delta$. Next, we replace all the transitions in $\Delta '$ that violates the condition $|\beta|+|\gamma|\leq 1$. \\

Consider any transition $((q,a,\beta),(p,e))$ where $\beta=B_1B_2...B_n$ with $n>1$. It is replaced by new transitions that pop sequentially the symbols $B_1...B_n$. So, we add $\delta '$ these transitions:
$$((q,e,B_1),(q_1,e))$$
$$((q_1,e,B_2),(q_2,e))$$
$$\vdots$$
$$((q_{n-2},e,B_{n-1}),(q_{n-1},e))$$
$$((q_{n-1},e,B_n),(p,e))$$
We repeat these replacements for all transitions $((q,a,\beta),(p,e))$ where $|\beta|>1$ in $\delta '$.\\

Similarly, we replace transitions $((q,a,e),(p,\gamma))$ where $\gamma=G_1G_2...G_m$ and $m>1$ by the new transitions:
$$((q,e,e),(q_1,G_1))$$
$$((q_1,e,e),(q_2,G_2))$$
$$\vdots$$
$$((q_{n-2},e,e),(q_{n-1},G_{n-1}))$$
$$((q_{n-1},e,e),(p,G_n))$$
We repeat these replacements for all transitions $((q,a,e),(p,\gamma))$ where $|\gamma|>1$ in $\delta '$.\\

Now, consider any transition $((q,a,\beta),(p,\gamma))$ where $\beta=B_1B_2...B_n$ with $n>1$ and $\gamma=G_1G_2...G_m$ with $m>1$. We replace these transitions by the new transitions:
$$((q,e,B_1),(q_1,e))$$
$$((q_1,e,B_2),(q_2,e))$$
$$\vdots$$
$$((q_{n-2},e,B_{n-1}),(q_{n-1},e))$$
$$((q_{n-1},e,B_n),(p,e))$$
$$((q_n,e,e),(q_{n+1},G_1))$$
$$((q_{n+1},e,e),(q_{n+2},G_2))$$
$$\vdots$$
$$((q_{n+m-2},e,e),(q_{n+m-1},G_{m-1}))$$
$$((q_{n+m-1},e,e),(p,G_m))$$
It is clear that the resulting pushdown automata is equivalent to the original one.

\section*{Answer 5}

\subsection*{a.}
\begin{enumerate}[(i)]
\item
Let $L_1=\{a^m b^m \in \{a,b\}^* |m\in N\}$ and $L_2=\{b^n a^n \in \{a,b\}^* |n\in N\}$. Grammer for $L_1$ is $G_1=\{\{a,b,S\},\{a,b\}, \{S\rightarrow aSb|e\},S\}$ and grammer for $L_2$ is $G_1=\{\{a,b,S\},\{a,b\}, \{S\rightarrow bSa|e\},S\}$. So, $L_1$ and $L_2$ are context-free languages. By Theorem 3.5.1, their concatenation $L_1L_2$ are also context-free.

\item
$L=\{ba,babaa,babaabaaa,...\}$\\
Let $L'$ be the complement of $L$ and use an algorithm to find the complement of $L$. We can generate strings that is not in $L$ by changing one $a$ or $b$ from the beginning and in order. For $n=1$, we have the string $ba$ in $L$. Change a $b$ to an $a$ or change an $a$ to a $b$ in $ba$, we get $aa$ or $bb$ respectively, which are not in $L$. Concatenating $\Sigma^*$ to these two strings we have the languages $aa\Sigma^*$ and $bb\Sigma^*$. These two languages differ from $L$ by the first $a$ or the first $b$ symbols. However, we may have other strings that differs from $L$ by changing the $n^th$ $a$ to a $b$ and $n^th$ $b$ to an $a$, which are in case, the ones who do not fit the condition of $L$. Thus, one can generate $L'$ by taking the infinite union of such languages.
$$L'=a\Sigma^*\cup baa\Sigma^*\cup babaaa\Sigma^*\cup ...\cup bb\Sigma^*\cup babb\Sigma^*\cup babab\Sigma^*\cup babaabb\Sigma^*\cup ...$$
Since all regular languages are context-free and context-free languages are closed under union by Theorem 3.5.1, $L'$ is context-free.
\end{enumerate}

\subsection*{b.}
\begin{enumerate}[(i)]
\item
Assume L is context-free. Then there exists a pumping length $k$ such that for all $w\in L$ and $|w|>k$ there exists a split $w=uvxyz$, $|vy|>0$, $|vxy|\leq k$ and $yv^nxy^nz\in L$ for all $n\geq 0$. Choose $w=a^{k^2}b^k$. There are three possible splits:\\\\
\textbf{1)} $vxy=a^j$, $j>0$. $uv^2xy^2z=a^{k^2+2j}b^k$. $k^2+2j$ is not less than or equal $k^2$ since $j>0$. \\
\textbf{2)} $vxy=b^j$, $j>0$. $uv^2xy^2z=a^{k^2}b^{k+2j}$. $k^2$ is not less than or equal $(k+2j)^2$ since $j>0$. \\
\textbf{3)} $vxy=a^ib^j$, $0<i+j\leq k$. There are 2 cases:\\
3.1) $v$ and $y$ consist of one symbol, $v=a^l$ and $y=b^m$. If we pump n times, we get $a^{k^2+nl}b^{k+nm}$. $k^2+nl\leq (k+nm)^2$ should hold for all $n,m,l\in N$. Clearly it does not.\\
3.2) One of $v$ or $y$ consist of two symbols, say v, $v=a^ib^j$ and $y=b^m$. When we pump, the order of the new string does not match the order of the languages in $L$ that is first only a's then only b's.\\
For all of these 3 cases, $w\notin L$. Thus, L is not context-free.

\item
Assume L is context-free. Then there exists a pumping length $k$ such that for all $www\in L$ and $|www|>k$ there exists a split $www=uvxyz$, $|vy|>0$, $|vxy|<k$ and $yv^nxy^nz\in L$ for all $n\geq 0$. Choose $www=a^kb^ka^kb^ka^kb^k$. There are three possible splits:\\\\
\textbf{1.} $vxy=a^i$. For n=0:\\
1.1: $yv^nxy^nz=a^{k-i}b^ka^kb^ka^kb^k$ is not in L since to get w we need to split this string in 3 pieces with equal lengths. However, when we do this, the first one ends with an a but the last one ends with a b. So, there no such string in L.\\
1.2: $yv^nxy^nz=a^kb^ka^{k-i}b^ka^kb^k$ is not in L since to get w we need to split this string in 3 pieces with equal lengths. However, when we do this, the first one begins with an a but the middle one begins with a b. So, there no such string in L.\\
1.3: $yv^nxy^nz=a^kb^ka^kb^ka^{k-i}b^k$ is not in L since to get w we need to split this string in 3 pieces with equal lengths. However, when we do this, the first one begins with an a but the last one begins with a b. So, there no such string in L.\\
The same discussion and reasons are valid for case 2 and 3.\\
\textbf{2.} $vxy=a^ib^j$. For n=0:\\
2.1: $yv^nxy^nz=a^{k-i}b^{k-j}a^kb^ka^kb^k$\\
2.2: $yv^nxy^nz=a^kb^ka^{k-i}b^{k-j}a^kb^k$\\
2.3: $yv^nxy^nz=a^kb^ka^kb^ka^{k-i}b^{k-j}$\\
\textbf{3.} $vxy=b^ja^i$. For n=0:\\
3.1: $yv^nxy^nz=a^kb^{k-j}a^{k-i}b^ka^kb^k$\\
3.2: $yv^nxy^nz=a^kb^ka^kb^{k-j}a^{k-i}b^k$\\
Therefore, L is not context-free.

\end{enumerate}

\section*{Answer 6}
% You do not need to explain the following
\begin{enumerate}[(i)]
\item (T/F)? F
\item (T/F)? T
\item (T/F)? T
\item (T/F)? F
\end{enumerate}

% Please do not submit your answers to the ungraded questions.
% Hope you have fun :)
\end{document}


\documentclass[conference]{IEEEtran}
\IEEEoverridecommandlockouts
% The preceding line is only needed to identify funding in the first footnote. If that is unneeded, please comment it out.
\usepackage{cite}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{algorithmic}
\usepackage{graphicx}
\usepackage{textcomp}
\usepackage{xcolor}
\usepackage{tikz}

\usetikzlibrary{matrix}
\def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em
    T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}}

\begin{document}

\title{CPS843 Assignment 2}

\author{\IEEEauthorblockN{Udbhav Prasad}
\IEEEauthorblockA{\textit{500909034}}
}

\maketitle

\begin{abstract}

Second Assignment for Intro to Computer Vision (CPS843).

\end{abstract}

\section*{Part 1 - Problem 1}

Figure 1 contains the original gray scale image.

\begin{figure}[htbp]
    \centering
    \includegraphics[width=8cm, height=4.5cm]{images/eldenring_grayscale.jpg}
    \caption{Original Gray scale Image}
\end{figure}

The more details in the detections there are, the more noise is also present, in prewitt we see that there is no false detections in the sky but other detectors capture that false edges (clouds). However, all the detections have similar edges. Noise is having alot of effect on the edges.

\subsection{Robert Edge Detection}

Figure 2 contains Robert edge detected image.

Here are the vertical/horizontal masks for Robert:

\begin{center}
\begin{tabular}{ |c|c| }
\hline
-1 & 1 \\ \hline
\end{tabular}
\end{center}

\begin{center}
\begin{tabular}{ |c| }
\hline
-1 & 1 \\ \hline
\end{tabular}
\end{center}

Here are the diagonal masks for Robert:

\begin{center}
\begin{tabular}{ |c|c| }
\hline
-1 & 0 \\ \hline
0 & 1 \\ \hline
\end{tabular}
\end{center}

\begin{center}
\begin{tabular}{ |c|c|c| }
\hline
0 & -1 \\ \hline
1 & 0 \\ \hline
\end{tabular}
\end{center}

\begin{figure}[htbp]
    \centering
    \includegraphics[width=8cm, height=4.5cm]{images/eldenring_robert.jpg}
    \caption{Robert Edge Detected Image}
\end{figure}

\subsection{Prewitt Edge Detection}

Figure 3 contains Prewitt edge detected image.

Here are the vertical/horizontal masks for Prewitt:

\begin{center}
\begin{tabular}{ |c|c|c| }
\hline
-1 & 0 & 1 \\ \hline
-1 & 0 & 1 \\ \hline
-1 & 0 & 1 \\ \hline
\end{tabular}
\end{center}

\begin{center}
\begin{tabular}{ |c|c|c| }
\hline
-1 & -1 & -1 \\ \hline
0 & 0 & 0 \\ \hline
1 & 1 & 1 \\ \hline
\end{tabular}
\end{center}

Here are the diagonal masks for Prewitt:

\begin{center}
\begin{tabular}{ |c|c|c| }
\hline
0 & 1 & 1 \\ \hline
-1 & 0 & 1 \\ \hline
-1 & -1 & 0 \\ \hline
\end{tabular}
\end{center}

\begin{center}
\begin{tabular}{ |c|c|c| }
\hline
-1 & -1 & 0 \\ \hline
-1 & 0 & 1 \\ \hline
0 & 1 & 1 \\ \hline
\end{tabular}
\end{center}

\begin{figure}[htbp]
    \centering
    \includegraphics[width=8cm, height=4.5cm]{images/eldenring_prewitt.jpg}
    \caption{Prewitt Edge Detection Image}
\end{figure}

\subsection{Sobel Edge Detection}

Figure 4 contains Sobel edge detected image.

Here are the vertical/horizontal masks for Sobel:

\begin{center}
\begin{tabular}{ |c|c|c| }
\hline
-1 & -2 & -1 \\ \hline
0 & 0 & 0 \\ \hline
1 & 2 & 1 \\ \hline
\end{tabular}
\end{center}

\begin{center}
\begin{tabular}{ |c|c|c| }
\hline
-1 & 0 & 1 \\ \hline
-2 & 0 & 2 \\ \hline
-1 & 0 & 1 \\ \hline
\end{tabular}
\end{center}

Here are the diagonal masks for Sobel:

\begin{center}
\begin{tabular}{ |c|c|c| }
\hline
0 & 1 & 2 \\ \hline
-1 & 0 & 1 \\ \hline
-2 & -1 & 0 \\ \hline
\end{tabular}
\end{center}

\begin{center}
\begin{tabular}{ |c|c|c| }
\hline
-2 & -1 & 0 \\ \hline
-1 & 0 & 1 \\ \hline
0 & 1 & 2 \\ \hline
\end{tabular}
\end{center}

\begin{figure}[htbp]
    \centering
    \includegraphics[width=8cm, height=4.5cm]{images/eldenring_sobel.jpg}
    \caption{Sobel Edge Detection Image}
\end{figure}

\section*{Part 1 - Problem 2}

In figure 5, we show the 1D image and the first and second order derivatives of it. We used zero padding twice to preserve the last two pixel values.

\begin{figure}
\begin{center}
\begin{tabular}{ |c|c|c| }
\hline
f(x) & f'(x) & f''(x)\\ \hline
0 & 0 & 6\\ \hline
0 & 6 & -6\\ \hline
6 & 0 & 0\\ \hline
6 & 0 & 0\\ \hline
6 & 0 & -1\\ \hline
6 & -1 & 0\\ \hline
5 & -1 & 0\\ \hline
4 & -1 & 0\\ \hline
3 & -1 & 0\\ \hline
2 & -1 & 1\\ \hline
1 & 0 & 0\\ \hline
1 & 0 & 0\\ \hline
1 & 0 & 0\\ \hline
1 & 0 & 0\\ \hline
1 & 0 & 5\\ \hline
1 & 5 & -5\\ \hline
6 & 0 & 0\\ \hline
6 & 0 & 0\\ \hline
6 & 0 & 0\\ \hline
6 & 0 & -6\\ \hline
6 & -6 & 6\\ \hline
0 & 0 & N/A\\ \hline
0 & N/A & N/A\\ \hline
\end{tabular}
\caption{1D Image 1st and 2nd order derivatives with zero padding}
\end{center}
\end{figure}

\section*{Part 1 - Problem 3}

\begin{itemize}
  \item \textbf{Step 1:} You blur the image with Gaussian filter.
  \item \textbf{Step 2:} Subtract the original image with the blurred image and obtain the mask.
  \item \textbf{Step 3:} Add the mask to the original image with the mask you obtained multiplied by a constant k value. What you obtain is the sharpened image.
\end{itemize}

With k=1, we obtain this image:

\includegraphics[width=8cm, height=4.5cm]{images/eldenring_sharpen_1.jpg}

With k=5, we obtain this image:

\includegraphics[width=8cm, height=4.5cm]{images/eldenring_sharpen_5.jpg}

With k=1, the image appears to have more sharpened edges but with k=5, it has very large differences in the edges. k=5, the images appears very bold in the image, this is because we add higher intensity of the mask.

\section*{Part 1 - Problem 4}

In figure 6 and figure 7, we add and remove Gaussian noise, the results are very nice. Average filter applies a smoothing/blurring effect on the image. Gaussian filter removes the noise from the image and creates a more even image.

\begin{figure}
    \begin{tabular}{c}
        \includegraphics[width=8cm]{images/eldenring_noise_1.jpg} \\
        (a) Gaussian Noise \\[6pt]
        mu=0 var=0.005 \\[6pt]
        \includegraphics[width=8cm]{images/eldenring_average_1.jpg} \\
        (c) Medium Filter 5x5 \\[6pt]
        \includegraphics[width=8cm]{images/eldenring_g_1.jpg} \\
        (e) Gaussian Filter 10x10 \\[6pt]
    \end{tabular}
    \caption{Problem 4, First Image Noise Removal}
\end{figure}

\begin{figure}
    \begin{tabular}{c}
        \includegraphics[width=8cm]{images/eldenring_noise_2.jpg} \\
        (b) Gaussian Noise \\[6pt]
        mu=0.2 var=0.001 \\[6pt]
        \includegraphics[width=8cm]{images/eldenring_average_2.jpg} \\
        (c) Medium Filter 5x5 \\[6pt]
        \includegraphics[width=8cm]{images/eldenring_g_2.jpg} \\
        (e) Gaussian Filter 10x10 \\[6pt]
    \end{tabular}
    \caption{Problem 4, Second Image Noise Removal}
\end{figure}

\section*{Part 1 - Problem 5}

Equations for First order derivative of f(x, y):

\begin{equation}
\frac{\partial f(x, y)}{\partial x} = f(x+1, y) - f(x, y)
\end{equation}

\begin{equation}
\frac{\partial f(x, y)}{\partial y} = f(x, y+1) - f(x, y)
\end{equation}

Equation for Laplacian of a 2D image:

\begin{equation}
\nabla^2 f = \frac{\partial f^2(x, y)}{\partial x^2} + \frac{\partial f^2(x, y)}{\partial y^2}
\end{equation}

Image is:

\begin{center}
\begin{tabular}{ |c|c|c|c| }
\hline
0 & 2 & 5 & 7 \\ \hline
2 & 5 & 7 & 3\\ \hline
5 & 6 & 3 & 1\\ \hline
5 & 2 & 1 & 0 \\ \hline
\end{tabular}
\end{center}

First order with respect to x:

\begin{center}
\begin{tabular}{ |c|c|c|c| }
\hline
2 & 3 & 2 & -7 \\ \hline
3 & 2 & -4 & -3\\ \hline
1 & -3 & -2 & -1\\ \hline
-3 & -1 & -1 & 0 \\ \hline
\end{tabular}
\end{center}

First order with respect to y:

\begin{center}
\begin{tabular}{ |c|c|c|c| }
\hline
2 & 3 & 2 & -4 \\ \hline
3 & 1 & -4 & -2\\ \hline
0 & -4 & -2 & -1\\ \hline
-5 & -2 & -1 & 0 \\ \hline
\end{tabular}
\end{center}

Mask for Laplacian:

\begin{center}
\begin{tabular}{ |c|c|c| }
\hline
0 & 1 & 0\\ \hline
1 & -4 & 1\\ \hline
0 & 1 & 0\\ \hline
\end{tabular}
\end{center}

Image after Laplacian and Normalization:

\begin{center}
\begin{tabular}{ |c|c|c|c| }
\hline
4/9 & 2/9 & -4/9 & -20/9 \\ \hline
2/9 & -3/9 & -12/9 & 3/9\\ \hline
-7/9 & -9/9 & 3/9 & 2/9\\ \hline
-13/9 & 4/9 & 1/9 & 2/9 \\ \hline
\end{tabular}
\end{center}

Laplacian is done with mask by element wise multiplication then summation then normalization with the neighbourhood of each pixel:

Example for top left pixel: ((0)(0) + (0)(1) + (0)(0) + (0)(1) + (0)(-4) + (2)(1) + (0)(0) + (2)(1) + (5)(0))/9 = 4/9

\section*{Part 2}

Description of image enhancement algorithm:

An overview is that we invert the image then perform the haze removal algorithm then invert the image again to get the output.

The steps:

\begin{itemize}
  \item \textbf{Step 1:} Enhance Low Light Image using Dehazing Algorithm:

  In this step, we invert the image, apply haze removal algorithm (imreadhaze) and invert it again to obtain the enhanced low light image.

  \item \textbf{Step 2:} Improve Results Further Using imreducehaze Optional Parameters:

  In this step, we use same method as step 1 but improve on the method with better parameters: with contrast enhancement and boost per pixel.

  \includegraphics[width=8cm]{images/step2.png}

  \item \textbf{Step 3:} Another Example of Improving Poorly Lit Image:

  In this step, we perform haze reduction algorithm on another image with just improvement of contrast enhancement.

  \includegraphics[width=8cm]{images/step3.png}

  \item \textbf{Step 4:} Reduce Color Distortion by Using Different Color Space:

  We perform the same steps as step 3 but we convert to L*a*b colorspace before the process and convert back to rgb after the process. Here we get better results.

  \item \textbf{Step 5:} Improve Results Using Denoising:

  Low light images have a lot of noise hence enhancing them increases noise. After the process we use imguidedfilter to remove noise from the image.

  \item \textbf{Step 6:} Estimate Illumination Map:

  In this step we compare the image after enhancing with the color map.

\end{itemize}

\section*{Code for Assignment 2}

\includegraphics[width=8cm]{images/a2_code.png}

\begin{thebibliography}{00}
\bibitem{b1} Code for this assignment at ./a2.m
\bibitem{b2} Assignment source code: \href{https://github.com/UdbhavPrasad072300/CPS843-Assignments}
\end{thebibliography}

\end{document}

Assignment 2/CPS843 Assignment 2/a2_CPS843.tex