% Created 2022-01-04 Tue 00:56
% Intended LaTeX compiler: pdflatex
\documentclass[presentation]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{awesomebox}
\usepackage{booktabs}
\usepackage{placeins}
\usepackage{siunitx}
\usepackage{minted}
\usetheme[progressbar=frametitle]{metropolis}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\usepackage{spot}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta}
\pgfplotsset{compat=1.16}
\newcommand{\gv}[1]{\ensuremath{\mbox{\boldmath$ #1 $}}}
\newcommand{\bv}[1]{\ensuremath{\mathbf{#1}}}
\newcommand{\norm}[1]{\left\lVert#1\right\rVert}
\newcommand{\abs}[1]{\left\lvert#1\right\rvert}
\newcommand{\bigqm}[1][1]{\text{\larger[#1]{\text{?}}}}
\newcommand{\order}[1]{\mathcal O \left( #1 \right)} % order of magnitude
\definecolor{scarlet}{rgb}{1.0, 0.13, 0.0}
\definecolor{shamrockgreen}{rgb}{0.0, 0.62, 0.38}
\definecolor{royalblue}{rgb}{0.25, 0.41, 0.88}
\definecolor{metropolisorange}{RGB}{235,129,27}
\definecolor{metropolisblue}{RGB}{35,55,59}
\usetheme{default}
\author{\emph{Tejaswin Parthasarathy}, Mattia Gazzola}
\date{\today}
\title{Elastica : Coordinate/Frame transformations}
\subtitle{ME447: Comp. Design \& Dyn. of Soft Syst}
\hypersetup{
 pdfauthor={\emph{Tejaswin Parthasarathy}, Mattia Gazzola},
 pdftitle={Elastica : Coordinate/Frame transformations},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 28.0.50 (Org mode 9.4.6)},
 pdflang={English}}
\begin{document}

\maketitle
\tikzset{>=latex}

\section{Coordinate/Frame transformations}
\label{sec:org2014f66}
\begin{frame}[label={sec:org2abc02b}]{Motivation}
\[ \gv{x}_{\mathcal{L}} = \bv{Q}\gv{x} \]
\[ \scalebox{5}{\textbf{?}} \]
\[ \frac{\partial \bv{d}_j}{\partial t} = \left( \bv{Q}^T
   \omega_{\mathcal{L}}\right) \times \bv{d}_j \]
\[ \scalebox{5}{\textbf{?}} \]
\end{frame}

\begin{frame}[label={sec:orge08581d}]{Motivation}
\begin{columns}
\begin{column}{0.7\columnwidth}
\begin{itemize}
\item To convert between arbitrary spaces, e.g. world space and other spaces (in
graphics) or Eulerian frame to Lagrangian frame in physics
\item To convert between coordinates that are more ``natural'' to the system under
observation---e.g. complex numbers can be naturally represented in polar,
rather than cartesian coordinates
\end{itemize}
\begin{itemize}
\item Transformations can be applied to points, vectors etc.
\end{itemize}
\end{column}
\begin{column}{0.5\columnwidth}
\begin{center}
	\begin{tikzpicture}
	\begin{axis}[
		width=1\textwidth,
		height=0.8\textheight,
		xmin=-1.5,
		xmax=4.5,
		ymin=-4.5,
		ymax=4.5,
		axis equal,
		axis lines=middle,
		grid=major,
		major grid style={line width=.2pt,draw=gray!50},
		xlabel=$\Re(z)$,
		ylabel=$\Im(z)$,
		disabledatascaling]
		% https://tex.stackexchange.com/questions/27279/how-to-make-an-arrow-bigger-and-change-its-color-in-tikz/27287#27287
		\addplot [arrows={-latex[scale=4]}, thick, color=metropolisorange] coordinates { (0,0) (2,3) } node [right] {$2 + 3i$};
		\addplot [arrows={-latex[scale=4]}, thick, color=metropolisblue] coordinates { (0,0) (3,-2) } node [below] {$3 - 2i$};
		\addplot [black, mark = *] coordinates {( 1, -3)} node [below] {$1 - 3i$};
	\end{axis}
	\end{tikzpicture}
\end{center}
\end{column}
\end{columns}
\end{frame}

\begin{frame}[label={sec:org58e37d1}]{Introduction}
\begin{block}{Cartesian coordinates}
\begin{itemize}
\item Coordinate: \((x, y, z)\)
\item Frame : \(\hat{\gv{e}_x}, \hat{\gv{e}_y}, \hat{\gv{e}_z}\)
\end{itemize}
\end{block}
\begin{columns}
\begin{column}{0.6\columnwidth}
\begin{center}
	\begin{tikzpicture}
	\begin{axis}[
		width=1\textwidth,
		height=0.8\textheight,
		xmin=-3.5,
		xmax=3.5,
		ymin=-3.5,
		ymax=3.5,
		axis equal,
		axis lines=middle,
		grid=both,
		grid style={line width=.1pt, draw=gray!20},
		major grid style={line width=.2pt,draw=gray!50},
		minor tick num=3,
		xlabel=$x$,
		ylabel=$y$,
		disabledatascaling]
		% https://tex.stackexchange.com/questions/27279/how-to-make-an-arrow-bigger-and-change-its-color-in-tikz/27287#27287
		% Mark origin
		\addplot [scarlet, mark = *] coordinates {( 0, 0)} node [above right]{$(0,0)$};

		% Mark (2,3) and projection
		\addplot [metropolisorange, mark = *] coordinates {(2, 3)} node [above]{$(2,3)$};
		\addplot [metropolisorange, thick, dashed] coordinates {(2, 3) (2, 0)};
		\addplot [metropolisorange, thick, dashed] coordinates {(2, 3) (0, 3)};

		% Mark (-1.5,-2.5) and projection
		\addplot [metropolisblue, mark = *] coordinates {(-1.5,-2.5)} node [below]{$(-1.5,-2.5)$};
		\addplot [metropolisblue, thick, dashed] coordinates {(-1.5,-2.5) (-1.5, 0)};
		\addplot [metropolisblue, thick, dashed] coordinates {(-1.5,-2.5) (0, -2.5)};

		% Mark (-3,-1) and projection
		\addplot [royalblue, mark = *] coordinates {(-3,1)} node [above right]{$(-3,1)$};
		\addplot [royalblue, thick, dashed] coordinates {(-3,1) (-3, 0)};
		\addplot [royalblue, thick, dashed] coordinates {(-3,1) (0, 1)};

	\end{axis}
	\end{tikzpicture}
\end{center}
\end{column}
\end{columns}
\end{frame}

\begin{frame}[label={sec:org6cc5b22}]{Introduction}
\begin{columns}
\begin{column}{0.4\columnwidth}
\begin{block}{Cylindrical coordinates}
\begin{itemize}
\item Coordinate: \((\rho, \phi, z)\)
\item Frame : \(\hat{\gv{e}_\rho}, \hat{\gv{e}_\phi}, \hat{\gv{e}_z}\)
\end{itemize}
% https://tex.stackexchange.com/a/159452
\tdplotsetmaincoords{60}{110}
\begin{tikzpicture}[tdplot_main_coords, scale=2.8]
	\tikzstyle{every node}=[font=\small]
	\draw[thick,-latex] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};
	\draw[thick,-latex] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};
	\draw[thick,-latex] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};
	\draw [thick](0,0,0) circle (0.5);
	\draw [thick](0,0,0.8) circle (0.5);
	\draw [thick](0.22,-0.45,0) -- (0.22,-0.45,0.8) node[midway, left]{$\rho=\rho_1$};
	\draw [thick](-0.22,0.45,0) -- (-0.22,0.45,0.8);
	\filldraw[fill=metropolisorange, nearly transparent] (-0.6,-0.6,0.8) -- (0.6,-0.6,0.8) --  (0.6,0.6,0.8) -- (-0.6,0.6,0.8) -- (-0.6,-0.6,0.8);
	\filldraw[fill=blue, nearly transparent] (0,0,0.8) -- (0.45,0.6,0.8) --  (0.45,0.6,0) -- (0,0,0) -- (0,0,0.8);
	\filldraw [color=metropolisblue](0.3,0.4,0.8) circle (0.03) ;
	\draw (-0.6,0.6,0.8) node[anchor=south]{$z=z_1$};
	\draw (0.6,0.8,0) node[anchor=south west]{$\phi=\phi_1$};
	\draw [color=metropolisorange](0.3,0.7,0.8) node[above right] { $(\rho_1,\phi_1,z_1)$};
	\draw[thick,-latex](0.3,0.4,0.8) -- (0.48,0.64,0.8) node[anchor=north] {$\hat{\gv{e}_\rho}$};
	\draw[thick,-latex](0.3,0.4,0.8) -- (0.12,0.52,0.8) node[anchor=north west]{$\hat{\gv{e}_\phi}$};
	\draw[thick,-latex](0.3,0.4,0.8) -- (0.3,0.4,1.1) node[anchor=north west]{$\hat{\gv{e}_z}$};
	\draw [thick,->](0.8,0,0) arc (0:53.14:0.8);
	% \draw (0.8,0.3,0) node[anchor=north] {$\phi_1$};
	\draw[thick,-latex,metropolisblue](0,0,0) -- (0.3,0.4,0);
	\draw (0.20,0.12,0) node[anchor=north] {$\rho_1$};
	\draw [thick,metropolisblue,-latex] (0.3,0.4,0)--(0.3,0.4,0.8);
	%\draw[ultra thick](0.1,0,4) -- (-0.1,0,4) node[anchor=south west] {$z_1$};
\end{tikzpicture}
\end{block}
\end{column}

\begin{column}{0.4\columnwidth}
\begin{block}{Spherical coordinates}
\begin{itemize}
\item Coordinate: \((r, \theta, \phi)\)
\item Frame : \(\hat{\gv{e}_r}, \hat{\gv{e}_\theta}, \hat{\gv{e}_\phi}\)
\end{itemize}

% 3D axis with spherical coordinates
% https://wiki.physik.uzh.ch/cms/latex:example_spherical_coordinates
\tdplotsetmaincoords{60}{110}
\begin{tikzpicture}[scale=3,tdplot_main_coords]

	% variables
	\def\rvec{1.2}
	\def\thetavec{40}
	\def\phivec{60}

	\def\rvecplus{0.3}
	\def\thetavecplus{8}
	\def\phivecplus{15}
	% axes
	\coordinate (O) at (0,0,0);
	\draw[thick,->] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};
	\draw[thick,->] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};
	\draw[thick,->] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};

	% vectors
	\tdplotsetcoord{P}{\rvec}{\thetavec}{\phivec}
	\draw[-stealth,metropolisorange,thick] (O)  -- (P) node[above left] {$(r, \theta, \phi)$};
	\draw[dashed,metropolisorange]   (O)  -- (Pxy);
	\draw[dashed,metropolisorange]   (P)  -- (Pxy);
	% \draw[dashed,metropolisorange]   (Py) -- (Pxy);

	% coordinate axes
	\tdplotsetcoord{Pr}{\rvec + \rvecplus}{\thetavec}{\phivec}
	\tdplotsetcoord{Pt}{\rvec}{\thetavec + \thetavecplus}{\phivec}
	\tdplotsetcoord{Pp}{\rvec}{\thetavec}{\phivec + \phivecplus}
	\draw[thick,->] (P) -- (Pr) node[above]{$\hat{\gv{e}_r}$};
	\draw[thick,->] (P) -- (Pt) node[below right]{$\hat{\gv{e}_\theta}$};
	\draw[thick,->] (P) -- (Pp) node[right]{$\hat{\gv{e}_\phi}$};
	% arcs
	\tdplotdrawarc[->]{(O)}{0.2}{0}{\phivec}
	{anchor=north}{$\phi$}
	\tdplotsetthetaplanecoords{\phivec}
	\tdplotdrawarc[->,tdplot_rotated_coords]{(0,0,0)}{0.5}{0}{\thetavec}
	{anchor=south west}{$\theta$}

\end{tikzpicture}
\end{block}
\end{column}
\end{columns}
\end{frame}

\begin{frame}[label={sec:org8cc743c}]{Introduction}
\begin{block}{Choice of coordinate system}
\begin{itemize}
\item We represent entities (points, vectors etc.) in the \emph{cartesian coordinate
system}, considering only Euclidean geometry
\end{itemize}
\end{block}
\begin{block}{Why?}
\begin{itemize}
\item To use certain \alert{affine} transformations
\end{itemize}
\end{block}
\begin{definition}[Affine transformations]
\begin{itemize}
\item any function between \emph{affine spaces} which preserves points, straight lines and planes
\item Examples: translation, scaling, similarity transformation,
reflection, rotation, etc. and their compositions
\end{itemize}
\end{definition}
\end{frame}
\begin{frame}[label={sec:org06d8877}]{Affine transformations}
\begin{theorem}[Affine \(\leftrightarrow\) Linear]
If \(\mathcal{X}\) and \(\mathcal{Y}\) are affine spaces, then every affine transformation
\(f\colon \mathcal{X}\to \mathcal{Y}\) is of the form \(\gv{x}\mapsto
	\bv{M}\gv{x}+\gv{b}\) where \(\bv{M}\) is a linear transformation on the
space \(\mathcal{X}\),  \(\gv{x}\) is a vector in \(\mathcal{X}\), and \(\gv{b}\) is a vector in \(\mathcal{Y}\).
\end{theorem}
\end{frame}

\begin{frame}[label={sec:org5f676bc}]{Affine transformations : Examples}
\begin{block}{Translations ( \(\bv{M} = \bv{I}\) and \(\gv{b} \neq \gv{0}\) )}
\begin{figure}[htbp]
\centering
\includegraphics[width=0.4\textwidth]{images/translate.pdf}
\caption{Translation of entities, Wikimedia, CC4.0}
\end{figure}
\end{block}
\begin{block}{Translation}
\end{block}
\end{frame}
\begin{frame}[label={sec:orgb4d6341}]{Linear transformations}
\begin{block}{Difference b/w linear and affine trans.}
\begin{itemize}
\item Most of our interest lies in modeling soft filaments, for which we will be
using only \emph{linear transformations}
\item In linear transformations, \(\gv{b} \equiv \gv{0}\) in vector space
\(\mathcal{Y}\)
\item We lose the ability to translate entities using linear transformations (
zero must map to zero by definition)
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[label={sec:orgd21a687}]{But what are linear transformations?}
\begin{itemize}
\item We loosely define a linear transformation \(\gv{x} \to \bv{M}\gv{x}\) by a \emph{matrix}
( \(\bv{M}\) ) that acts on the vector \(\gv{x} \in \mathcal{X}\), about
\(\gv{0} \in \mathcal{X}\)
\item Check out the Wikipedia page on \href{https://en.wikipedia.org/wiki/Matrix\_(mathematics)}{matrices} and \href{https://en.wikipedia.org/wiki/Rotation\_matrix}{matrix classes} to see why they
(matrices and linear transformations) are considered important.
\end{itemize}
\end{frame}
\begin{frame}[label={sec:orgd9da2c2}]{Linear transformations : Examples\footnote{All examples from Wikipedia found in \href{https://en.wikipedia.org/wiki/Affine\_transformation\#Image\_transformation}{article ``Affine transformation''
under section ``Image transformation''} and assume origin at the center of image}}
\begin{columns}
\begin{column}{0.5\columnwidth}
\begin{block}{Identity}
\[ \bv{M} = \begin{bmatrix}1&0&0\\0&1&0\\0&0&1\end{bmatrix} \]
\begin{center}
\includegraphics[height=0.8\textwidth]{images/ch_id.pdf}
\end{center}
\end{block}
\end{column}
\begin{column}{0.5\columnwidth}
\begin{block}{Reflection}
\[ \bv{M} =\begin{bmatrix}-1&0&0\\0&1&0\\0&0&1\end{bmatrix} \]
\begin{center}
\includegraphics[height=0.8\textwidth]{images/ch_ref.pdf}
\end{center}
\end{block}
\end{column}
\end{columns}
\end{frame}

\begin{frame}[label={sec:orgf7bd85c}]{Linear transformations : Examples}
\begin{columns}
\begin{column}{0.5\columnwidth}
\begin{block}{Scale}
\[ \bv{M} =\begin{bmatrix}c_{x}=2&0&0\\0&c_{y}=1&0\\0&0&1\end{bmatrix} \]
\begin{center}
\includegraphics[height=0.8\textwidth]{images/ch_sc.pdf}
\end{center}
\end{block}
\end{column}
\begin{column}{0.5\columnwidth}
\begin{block}{Shear}
\[ \bv{M} =\begin{bmatrix}1&c_{x}=0.5&0\\c_{y}=0&1&0\\0&0&1\end{bmatrix}\]
\begin{center}
\includegraphics[height=0.8\textwidth]{images/ch_sh.pdf}
\end{center}
\end{block}
\end{column}
\end{columns}
\end{frame}

\begin{frame}[label={sec:orgb427323}]{Linear transformations : Examples}
\begin{block}{Rotation}
\begin{center}
\spot<2>{\( \bv{M} =\begin{bmatrix}\cos(\theta )&\sin(\theta )&0\\-\sin(\theta
 )&\cos(\theta )&0\\0&0&1\end{bmatrix} \text{with } \theta = \frac{\pi}{6}\)}
\end{center}

\begin{center}
\includegraphics[height=0.5\textwidth]{images/ch_rot.pdf}
\end{center}
\end{block}
\end{frame}
\begin{frame}[label={sec:orge64dab6}]{Rotations (includes reflections)}
\begin{itemize}
\item Generates new unit vectors, fundamentally changing the directions
(eigenvectors) of further transformations
\item Does not scale the entity under consideration ( \(\abs{\lambda} \equiv  1\), more on this later\ldots{})
\end{itemize}
\end{frame}
\begin{frame}[label={sec:orgce04e9d}]{Frame rotations in two--dimensions}
Consider rotation in two dimensions:

% 3D axis with spherical coordinates
\tdplotsetmaincoords{0}{0}
\begin{center}
\begin{tikzpicture}[scale=3,tdplot_main_coords]

	% variables
	\def\rvec{0.8}
	\def\thetavec{90}
	\def\phivec{36.86}

	% variables
	\def\alphavec{20}

	% axes
	\coordinate (O) at (0,0,0);
	% \draw[thick,->] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};
	% \draw[thick,->] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};
	\draw[thick,<->] (1,0,0) node[anchor=north east]{$x$} -- (0,0,0) --
	(0,1,0) node[anchor=north west]{$y$};

	% vectors
	\tdplotsetcoord{P}{\rvec}{\thetavec}{\phivec}
	% Draw point at P
	\filldraw [color=metropolisblue](P) circle (0.02) ;
	\draw[-stealth,metropolisblue,thick] (O)  -- (P) node[above right] {$p$};
	\draw[metropolisblue]   (P)  -- (Px) node[below] {$p_x$};
	\draw[metropolisblue]   (P)  -- (Py) node[left] {$p_y$};

	% coordinate axes xdash and ydash
	\tdplotsetcoord{xdash}{1}{90}{\alphavec}
	\tdplotsetcoord{ydash}{1}{90}{\alphavec + 90}
	%\draw[->,metropolisorange,dashed,thick] (O)  -- (xdash) node[above right] {$x^{\prime}$};
	%\draw[->,metropolisorange,dashed,thick] (O)  -- (ydash) node[above right] {$y^{\prime}$};
	\draw[thick,dashed,metropolisorange,<->,rotate=\alphavec] (1,0,0) node[above right]{$x^{\prime}$}-- (O) --
	(0,1,0) node[above right]{$y^{\prime}$};
	% \draw[thick,dashed,metropolisorange,<->] (ydash) node[above right]{$x^{\prime}$}-- (O) --
	% (xdash) node[above right]{$y^{\prime}$};

	% Points on x and y dash
	\tdplotsetcoord{xdashmeet}{\rvec*cos(\phivec-\alphavec)}{90}{\alphavec}
	\tdplotsetcoord{ydashmeet}{\rvec*sin(\phivec-\alphavec)}{90}{\alphavec + 90}
	\draw[metropolisorange]   (P)  -- (xdashmeet) node[below right] {$p_x^{\prime}$};
	\draw[metropolisorange]   (P)  -- (ydashmeet) node[below left] {$p_y^{\prime}$};

	% arcs
	\tdplotdrawarc[->]{(O)}{0.4}{0}{\alphavec}
	{anchor=west}{$\alpha$}
	% \tdplotsetthetaplanecoords{\phivec}
	% \tdplotdrawarc[->,tdplot_rotated_coords]{(0,0,0)}{0.5}{0}{\thetavec}
	% {anchor=south west}{$\theta$}
\end{tikzpicture}
\end{center}

\[ \begin{bmatrix} x^\prime \\ y^\prime \end{bmatrix}
   = \underbrace{\begin{bmatrix}\cos(\alpha )&\sin(\alpha )\\ -\sin(\alpha
   )&\cos(\alpha )\end{bmatrix}}_{\bv{R}} \begin{bmatrix} x\\ y\end{bmatrix}\]
\note{:B\_note:
Derive this thing in class.}
\end{frame}
\begin{frame}[label={sec:org4f755f0}]{Inverse rotations in two--dimensions}
Now consider the same picture, but we want to obtain \([x,y]^T\) from \([
   x^\prime, y^\prime ]^T\) (the other way around).

\begin{itemize}
\item Physically, this is just a rotation of \(- \alpha\) counter-clockwise (or
\(\alpha\) clockwise). That means
\end{itemize}
\[ \begin{bmatrix} x\\ y\end{bmatrix}
   = \begin{bmatrix}\cos(\alpha )& -\sin(\alpha )\\ \sin(\alpha
   )& \cos(\alpha )\end{bmatrix}  \begin{bmatrix} x^\prime \\ y^\prime
   \end{bmatrix} \]
\begin{itemize}
\item Mathematically, if \(\gv{x}^\prime= \bv{R} \gv{x}\), then we know \(\gv{x}= \bv{R}^{-1} \gv{x}^\prime\), provided \(\bv{R}^{-1}\) exists
(which does).
\item Then notice that
\end{itemize}
\[ \bv{R}^{-1} = \begin{bmatrix}\cos(\alpha )& -\sin(\alpha )\\ \sin(\alpha
   )& \cos(\alpha )\end{bmatrix}  = \bv{R}^T ! \]
\begin{itemize}
\item We will see later why holds for \alert{all} rotation matrices\ldots{}
\end{itemize}
\end{frame}
\begin{frame}[label={sec:org13ee560}]{Frame rotations in three--dimensions}
\begin{figure}[htbp]
\centering
\includegraphics[width=0.45\textwidth]{images/cartesian_rot_3D.pdf}
\caption{Rotation in three dimensions, Wikimedia CC1.0}
\end{figure}
is a natural extension of 2D results\ldots{}
\end{frame}
\begin{frame}[label={sec:org5fbbcf1}]{Beware!}
\begin{block}{Be wary about alias (passive) or alibi (active) transformations}
\end{block}
\begin{columns}
\begin{column}{0.55\columnwidth}
\begin{definition}[Alias transformations]
Involves rotation of the coordinate system or frame
(change in basis)
\end{definition}
\begin{definition}[Alibi transformations]
Involves rotation of the entities within the same
frame (change in entity)
\end{definition}
\end{column}
\begin{column}{0.5\columnwidth}
\begin{figure}[htbp]
\centering
\includegraphics[width=1.00\textwidth]{images/alias_alibi.png}
\caption{Alias-Alibi transformations, Wikimedia CC3.0}
\end{figure}
\end{column}
\end{columns}
Both are equally valid ways of representing rotations---in this class
however, we focus on alias transformations.
\note{:B\_note:
\begin{itemize}
\item Affirm that the entity does not matter. Show this for a vector or a point.
Beauty of affine transformations.
\item To change the formulas for passive rotations (or find reverse active
rotation), transpose the matrices (then each matrix transforms the initial
coordinates of a vector remaining fixed to the coordinates of the same
vector measured in the rotated reference system; same rotation axis, same
angles, but now the coordinate system rotates, rather than the vector).
\end{itemize}}
\end{frame}
\begin{frame}[label={sec:org8a0963e}]{Difference in perspectives\footnote{\href{https://iopscience.iop.org/book/978-0-7503-1454-1}{Rotation, Reflection, and Frame Changes---Orthogonal tensors in computational engineering mechanics, RM Brannon, IOP Publishing 2018}}}
\begin{quotation} %% :B\_quotation:
`` Analyzing rotation demands awareness of your desired perspective. You can rotate an object, while you stay still, or you can keep the object
fixed while you rotate yourself. It is important to be aware of which of these
perspectives applies for your problem of interest. The distinction between
these fundamentally different transformations goes beyond one being the
same as the other with an opposite rotation angle. ''
\end{quotation}
\end{frame}
\begin{frame}[label={sec:orgaa868b6}]{Frame rotation as a change in basis}
\begin{block}{More concretely}
If \(\mathcal{B}\) and \(\mathcal{B}^\prime\) are two (different) bases
\(\in \mathbb{R}^n\)
\begin{itemize}
\item Alibi : Change in entity \([\gv{p}]_{\mathcal{B}} \to
      [\gv{p}^\prime]_{\mathcal{B}}\) given by
\end{itemize}
\[ [\gv{p}^\prime]_{\mathcal{B}} = [\bv{M}]_{\mathcal{B} \to \mathcal{B}}
  [\gv{p}]_{\mathcal{B}} \]
\begin{itemize}
\item Alias : Change in basis \([\gv{p}]_{\mathcal{B}} \to
      [\gv{p}]_{\mathcal{B}^\prime}\)
\end{itemize}
\[ [\gv{p}]_{\mathcal{B}^\prime} = [\bv{M}]_{\mathcal{B} \to \mathcal{B}^\prime}
  [\gv{p}]_{\mathcal{B}} \]
\begin{itemize}
\item In our soft filament framework, \(\mathcal{B}^\prime \equiv \mathcal{L}\) and  \(\mathcal{B} \equiv\) lab frame. \(\bv{Q}\) is then the
basis transformation matrix (corresponding to pure rotation of the
orthonormal bases)
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[label={sec:orge9eb036}]{Frame rotation---example}
\begin{columns}
\begin{column}{0.5\columnwidth}
\tdplotsetmaincoords{60}{100}
\begin{center}
 \begin{tikzpicture}[scale=2, tdplot_main_coords]
 \draw[thick,->, color=scarlet] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};
 \draw[thick,->, color=shamrockgreen] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};
 \draw[thick,->, color=royalblue] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};
 \end{tikzpicture}
\end{center}
\end{column}
\begin{column}{0.5\columnwidth}
\tdplotsetmaincoords{60}{100}
\begin{center}
 \begin{tikzpicture}[scale=2, tdplot_main_coords]
 \draw[dashed,->,line width= 1.1pt] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};
 \draw[dashed,->,line width= 1.1pt] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};
 \draw[dashed,->,line width= 1.1pt] (0,0,0) -- (0,0,1) node[anchor=south west]{$z$};

 \coordinate (Shift) at (0,0,0);
 \tdplotsetrotatedcoords{0}{0}{90}
 \tdplotsetrotatedcoordsorigin{(Shift)}

 \draw[thick,color=scarlet,tdplot_rotated_coords,->] (0,0,0)
 -- (1,0,0) node[anchor=south east]{$x'$};
 \draw[thick,color=shamrockgreen,tdplot_rotated_coords,->] (0,0,0)
 -- (0,1,0) node[anchor=west]{$y'$};
 \draw[thick,color=royalblue,tdplot_rotated_coords,->] (0,0,0)
 -- (0,0,1) node[anchor=south east]{$z'$};
 \end{tikzpicture}
\end{center}
\end{column}
\end{columns}
\begin{itemize}
\item Represent \((x-y-z)\) axis with a basis \(\mathcal{E}\) of unit vectors \(\hat{\gv{e}_1}, \hat{\gv{e}_2}, \hat{\gv{e}_3}\)
\item Represent \((x'-y'-z')\) axis with a basis \(\mathcal{D}\) of unit vectors \(\hat{\gv{d}_1}, \hat{\gv{d}_2}, \hat{\gv{d}_3}\)
\item \(\mathcal{E} \to \mathcal{D}\)?
\item Note : rotation of \(\ang{90}\) about an invariant \(z' = z\) axis
\end{itemize}
\end{frame}
\begin{frame}[label={sec:org417c9b3}]{Frame rotation---example contd.}
\[ {\begin{bmatrix} x^\prime \\ y^\prime \\ z^\prime\end{bmatrix}} =
  \spot{[\bv{M}]_{\mathcal{E} \to \mathcal{D}}}
  {\begin{bmatrix} x \\ y \\ z \end{bmatrix}}
  \]
\begin{itemize}
\item We begin by noticing that \(\begin{bmatrix} x^\prime , y^\prime , z^\prime
     \end{bmatrix} = \begin{bmatrix} y , -x , z \end{bmatrix}\) (from figure). Then
\end{itemize}
\[ {\begin{bmatrix} x^\prime \\ y^\prime \\ z^\prime\end{bmatrix}} =
  {\begin{bmatrix} 0 & 1 & 0 \\ -1 & 0 & 0 \\ 0 & 0 & 1 \end{bmatrix}}
  {\begin{bmatrix} x \\ y \\ z \end{bmatrix}}
  \]
\[\Rightarrow {\begin{bmatrix} x^\prime \\ y^\prime \\ z^\prime\end{bmatrix}} =
  {\begin{bmatrix} \cos(\ang{90}) & \sin(\ang{90}) & 0 \\ -\sin(\ang{90}) &
  \cos(\ang{90}) & 0 \\ 0 & 0 & 1 \end{bmatrix}}
  {\begin{bmatrix} x \\ y \\ z \end{bmatrix}}
  \]
\end{frame}
\begin{frame}[label={sec:orgb10cae1}]{Generalizing frame rotations as a basis change}
\begin{itemize}
\item But also notice with the given bases that
\end{itemize}
\[{\begin{bmatrix} x^\prime \\ y^\prime \\ z^\prime\end{bmatrix}_{\mathcal{D}}} =
  \spot<2>{
  \underbrace{\begin{bmatrix}
  \hat{\gv{d}}_1 \cdot \hat{\gv{e}}_1 & \hat{\gv{d}}_1 \cdot
  \hat{\gv{e}}_2 & \hat{\gv{d}}_1 \cdot \hat{\gv{e}}_3 \\
  \hat{\gv{d}}_2 \cdot \hat{\gv{e}}_1 & \hat{\gv{d}}_2 \cdot
  \hat{\gv{e}}_2 & \hat{\gv{d}}_2 \cdot \hat{\gv{e}}_3 \\
  \hat{\gv{d}}_3 \cdot \hat{\gv{e}}_1 & \hat{\gv{d}}_3 \cdot
  \hat{\gv{e}}_2 & \hat{\gv{d}}_3 \cdot \hat{\gv{e}}_3
  \end{bmatrix}}_{[\bv{M}]_{\mathcal{E} \to \mathcal{D}}, \text{ independent of
  } \mathbf{x}}
  }
  {\begin{bmatrix} x \\ y \\ z \end{bmatrix}_{\mathcal{E}}}
  \]
\begin{block}<2->{Soft filament framework}
\begin{itemize}
\item Describe lab frame, \(\mathcal{E}\), by natural bases \(\hat{i}, \hat{j}, \hat{k}\).
\item Describe material (Lagrangian) frame, \(\mathcal{D}\), by orthonormal
vectors \(\hat{\gv{d}_1}, \hat{\gv{d}_2}, \hat{\gv{d}_3}\) (coordinates wrt
natural bases). Then
\end{itemize}
\[{\begin{bmatrix} x_{\mathcal{L}} \\ y_{\mathcal{L}} \\ z_{\mathcal{L}} \end{bmatrix}_{\mathcal{D}}} =
	 \underbrace{\begin{bmatrix}
	 \mbox{------}~\hat{\gv{d}}_1~\mbox{------} \\
	 \mbox{------}~\hat{\gv{d}}_2~\mbox{------} \\
	 \mbox{------}~\hat{\gv{d}}_3~\mbox{------} \\
	 \end{bmatrix}}_{\bv{Q}}
	 {\begin{bmatrix} x \\ y \\ z \end{bmatrix}_{\mathcal{E}}}
   \]
\end{block}
\note{:B\_note:
\begin{itemize}
\item Derive the \(\gv{d} \cdot \gv{e}\) relations in class.
\end{itemize}}
\end{frame}
\begin{frame}[label={sec:orgdd5b0fd}]{Generalizing frame rotations as a basis change}
 Taking it one step further we arrive at the conclusion,
\[
	\underbrace{\begin{bmatrix}
	\mbox{|} & \mbox{|}& \mbox{|}\\
	\hat{\gv{d}_1} & \hat{\gv{d}_2} & \hat{\gv{d}_3} \\
	\mbox{|} & \mbox{|}& \mbox{|}\\
	\end{bmatrix}}_{\bv{Q}^{-1} = \bv{Q}^T}
	{\begin{bmatrix} x_{\mathcal{L}} \\ y_{\mathcal{L}} \\ z_{\mathcal{L}}
	\end{bmatrix}}
	=
	{\begin{bmatrix}1 & 0 & 0 \\ 0 & 1 & 0 \\0 & 0& 1\end{bmatrix}}
	{\begin{bmatrix} x \\ y \\ z \end{bmatrix}}
  \]

\[
  \Rightarrow x_{\mathcal{L}}\hat{\gv{d}_1} + y_{\mathcal{L}}\hat{\gv{d}_2} +
  z_{\mathcal{L}}\hat{\gv{d}_3} = x\hat{i} + y\hat{j} + z\hat{k} = \gv{x} !
  \]
\note{:B\_note:
\begin{itemize}
\item Again iterate that this is a passive (alias) transformation and so this is
the expected result.
\end{itemize}}
\end{frame}
\begin{frame}[label={sec:org8125b97}]{Implementation of rotation as bases change}
\begin{itemize}
\item We have seen that the action of frame rotation matrices correspond to a
bases change operation
\item Let's implement these operations in our framework
\[ R_{x}(\theta)={\begin{bmatrix}1&0&0\\0&\cos \theta &\sin \theta
     \\0&-\sin \theta &\cos \theta \\\end{bmatrix}}\]

\[ R_{y}(\theta)={\begin{bmatrix}\cos \theta & 0 & -\sin \theta\\
	 0&1&0 \\ \sin\theta & 0 & \cos \theta \\\end{bmatrix}} \]

\[R_{z}(\theta)={\begin{bmatrix}\cos \theta &\sin \theta &0\\-\sin
	 \theta &\cos\theta &0\\0&0&1\\\end{bmatrix}} \]
\item \alert{ACTIVITY}
\end{itemize}
\end{frame}
\begin{frame}[label={sec:org3409e2c}]{But what about arbitrary rotations?}
\begin{itemize}
\item Rotations about arbitrary axes with arbitrary angles?
\end{itemize}
\begin{columns}
\begin{column}{0.6\columnwidth}
\begin{itemize}
\item Can we do compositions?
\begin{itemize}
\item \alert{Yes}, but not that intutive (means of rotation, intrinsic/extrinsic)
\item Not commutative (order matters) usually
\end{itemize}
\end{itemize}
\end{column}
\begin{column}{0.3\columnwidth}
\begin{center}
\includegraphics[width=0.80\textwidth]{images/rotated_cube.jpeg}
\end{center}
\end{column}
\end{columns}
\begin{itemize}
\item Becomes even more complicated when we have frames depending on one another
\begin{itemize}
\item But not a bad idea---robotics\footnote{\href{https://www.mecademic.com/resources/Euler-angles/Euler-angles}{Mecademic Euler rotations}}
\end{itemize}
\item \alert{Idea}: If we know the linear bases transformation, we don't need to worry
about compositions etc.
\end{itemize}
\note{:B\_note:
\begin{itemize}
\item Mention that some means of rotation like quarternions are better suited,
but require more math and understanding.
\item Mention Euler axis angle, euler roataions, quarternions
\end{itemize}}
\end{frame}
\begin{frame}[label={sec:orgff6e7fb}]{Let's reconsider what we know}
\begin{itemize}
\item We know why \(\gv{x}_{\mathcal{L}} = \bv{Q}\gv{x}\)
\item We then need the \alert{action} of \(\bv{Q}\) on \(\gv{x}\)
\item But\ldots{}
\begin{itemize}
\item Do we know \(\bv{Q}\) ?
\begin{itemize}
\item We need the basis \(\hat{\gv{d}}_j\)
\end{itemize}
\item Do we know \(\hat{\gv{d}}_j\)?
\begin{itemize}
\item \alert{No}
\end{itemize}
\end{itemize}
\item We seek ways to obtain this basis \(\gv{d}\) and hence \(\bv{Q}\).
\item We will see that we require some properties on \(\gv{d}\) to make \(\bv{Q}\) effect a rotation.
\end{itemize}
\end{frame}
\begin{frame}[label={sec:org94db160}]{Obtaining \(\gv{d}, \bv{Q}\) : Properties}
\[\bv{Q} =
	 {\begin{bmatrix}
	 \mbox{------}~\hat{\gv{d}}_1~\mbox{------} \\
	 \mbox{------}~\hat{\gv{d}}_2~\mbox{------} \\
	 \mbox{------}~\hat{\gv{d}}_3~\mbox{------} \\
	 \end{bmatrix}}
   \]
\begin{columns}
\begin{column}{0.47\columnwidth}
\begin{block}{\(\bv{Q}\)}
\begin{itemize}
\item Rows are unit vectors
\item Real, orthogonal matrix ( \(\bv{Q^T}\bv{Q} = \bv{Q}\bv{Q^T} = \bv{I}\) )
\item Eigenvalues are \(\lambda = {1, e^{\pm j \theta}}\)
\item Determinant \(= \prod_{i} \lambda_i = 1\)
\end{itemize}
\end{block}
\end{column}
\begin{column}{0.50\columnwidth}
\begin{block}{\(\hat{\gv{d}}\)}
\begin{itemize}
\item \(\norm{\hat{\gv{d}_1}} = \norm{\hat{\gv{d}_2}} = 1\)
\item \(\hat{\gv{d}_1} \cdot \hat{\gv{d}_2} = 0\)
\item \(\hat{\gv{d}_1} \times \hat{\gv{d}_2} = \hat{\gv{d}_3}\)
\item \(\therefore\) They form an orthonormal basis
\end{itemize}
\end{block}
\end{column}
\end{columns}
\note{:B\_note:
\begin{itemize}
\item Motivate orthogonality by saying that the natural bases is orthogonal,
and so we want to preserve this in rotation (all axes rotates equally).
This also makes R\^{}-1 = R\^{}T
\item By Gram-Schmidt theorem, we can always find an orthonormal bases given a
span of vectors
\item Euler's rotation theorem: Express any roation as a single rotation about
an axis. Eigenvalues represent this. 1--> rotation axes. 2,3 are
orthogonal axes that simply has a rotation.
\item Motivate determinant by volume. It tells expansino of a volume: 1 means
volume is preserved. Formulae for parallelopiped : \(u \cdot (v \times w)\). They are symmetric relations.
\end{itemize}}
\end{frame}
\begin{frame}[label={sec:org14cea7a}]{Obtaining \(\gv{d}, \bv{Q}\) : Options\footnote{Wikimedia, CC3.0 license}}
\begin{columns}
\begin{column}{0.6\columnwidth}
\begin{itemize}
\item We only need the \alert{action} of \(\bv{Q}\) on \(\gv{x}\)
\item Some means/formalisms to achieve these are
\begin{itemize}
\item Rotation matrices (gives \(\bv{Q}\) explicitly )
\item \(\spot<2>{\text{Euler axes and angle } \gv{r} = \theta \hat{\gv{e}} }\)
\item Euler rotations (precession, nutation, rotation)
\item Quaternions (\(w, \gv{r}\))
\end{itemize}
\item (dis)Advantages are spread equally, although some are more equal than the others*
\end{itemize}
\end{column}
\begin{column}{0.4\columnwidth}
\tdplotsetmaincoords{60}{125}

% https://tex.stackexchange.com/a/36607
\newcommand{\AxisRotator}{%
	\tikz [x=0.8ex,y=1.0ex,line width=.2ex,-stealth,rotate=50] \draw (0,0) arc (-150:150:1 and 1);%
}
\begin{center}
	\begin{tikzpicture}[scale=1.8,tdplot_main_coords]

	% variables
	\def\rvec{1.8}
	\def\thetavec{40}
	\def\phivec{60}

	% axes
	\coordinate (O) at (0,0,0);
	\draw[thick,->] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};
	\draw[thick,->] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};
	\draw[thick,->] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};

	% vectors
	\tdplotsetcoord{P}{\rvec}{\thetavec}{\phivec}
	\draw[-stealth,metropolisorange,thick] (O)  -- (P) node [near end] {\AxisRotator}
	node [pos=0.6, below right] {$\theta$} node [above] {$\hat{\gv{e}}$};

	% Draw the ball
	% https://tex.stackexchange.com/questions/54193/how-to-draw-a-shaded-sphere
	\shade [ball color=black!20] (0,0) circle [radius=1.5ex];

	\end{tikzpicture}
\end{center}

\begin{figure}[htbp]
\centering
\includegraphics[width=0.45\textwidth]{images/euler_rot.pdf}
\caption{Euler rotations}
\end{figure}
\end{column}
\end{columns}
\end{frame}

\begin{frame}[label={sec:orga30eb1e}]{Rotations about fixed axis : Euler axis-angle}
\begin{block}{Single rotation about an axis---\(\hat{\gv{e}}\) vector}
\begin{itemize}
\item Axis : unit vector which remains unchanged by the rotation
\item Note : Only two dofs, by normality condition
\item Unique, for any given rotation, except for the sign
\end{itemize}
\end{block}
\begin{block}{Rotation through scalar \(\theta\)}
\begin{itemize}
\item Unique, sign determined by axis \(\hat{\gv{e}}\)
\end{itemize}
\end{block}
\begin{columns}
\begin{column}{0.6\columnwidth}
\begin{block}{\(\hat{\gv{r}} = \theta \hat{\gv{e}}\)}
\begin{itemize}
\item Called \emph{Rotation vector} or \emph{Euler vector}
\end{itemize}
\end{block}
\end{column}
\begin{column}{0.3\columnwidth}
\tdplotsetmaincoords{60}{125}

% https://tex.stackexchange.com/a/36607
\newcommand{\AxisRotator}{%
	\tikz [x=0.8ex,y=1.0ex,line width=.2ex,-stealth,rotate=50] \draw (0,0) arc (-160:170:3 and 3);%
}
\begin{center}
	\begin{tikzpicture}[scale=3,tdplot_main_coords]

	% variables
	\def\rvec{1.8}
	\def\thetavec{40}
	\def\phivec{60}

	% axes
	\coordinate (O) at (0,0,0);

	% vectors
	\tdplotsetcoord{P}{\rvec}{\thetavec}{\phivec}
	\draw[-stealth,metropolisorange,ultra thick] (O)  -- (P)
	node [pos=0.5, right, color=royalblue] {$\theta$} node [above right] {$\hat{\gv{r}}=\theta \hat{\gv{e}}$};
	\tdplotsetcoord{Phalf}{\rvec*0.6}{\thetavec}{\phivec}
	\draw[-stealth,metropolisblue,ultra thick] (O)  -- (Phalf)
	node [pos=0.9, color=royalblue] {\AxisRotator} node [pos=0, left] {$\hat{\gv{e}}$};

	\end{tikzpicture}
\end{center}
\end{column}
\end{columns}
\end{frame}

\begin{frame}[label={sec:orgbfa0e7b}]{Rotations about fixed axis : Euler axis-angle}
\begin{block}{Advantages}
\begin{itemize}
\item Easy to understand/code up
\item Convenient while dealing with rigid body motions
\item Conversion to rotation matrices straightforward (and is so for all other
representations as well)
\end{itemize}
\end{block}
\begin{block}{Disadvantages}
\begin{itemize}
\item Combining successive rotations not straightforward (and breaks vector addition)
\item Corner cases when dealing with \(\theta = 0\) and signs of \(\hat{\gv{e}}\)
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[label={sec:org6f063c7}]{Rotation using Euler angles : Rodrigues formula}
\begin{block}{Rodrigues formula}
\begin{itemize}
\item Named after \href{https://en.wikipedia.org/wiki/Olinde\_Rodrigues}{Olinde Rodrigues}
\item Is an efficient algorithm to rotate a vector in space, given \(\theta\)
and \(\hat{\gv{e}}\).
\item Gives the exponential map that effects a transformation from the
axis-angle representation (our case!) to rotation matrices
\item Basically gives us \(\bv{Q}\) given \(\theta \hat{\gv{e}}\).
\end{itemize}
\[ \mathbf {R}(\theta, \hat{\gv{e}}) =\mathbf {I} +(\sin \theta )\mathbf {K} +(1-\cos \theta )\mathbf {K} ^{2} \]
where \(\mathbf{K}\) is the cross product matrix, discussed last class \(\mathbf{K}\gv{v} = \hat{\gv{e}} \times \gv{v}\)
\end{block}
\end{frame}
\begin{frame}[label={sec:org7d7ffd7}]{Rotation matrix from Rodrigues formula : structure and intuition}
 \[ \mathbf {R}(\theta, \hat{\gv{e}}) =\mathbf {I} +(\sin \theta )\mathbf {K} +(1-\cos \theta )\mathbf {K} ^{2} \]
We need \(\bv{R}\) above to satisfy properties of a rotation matrix. Let's verify:
\begin{itemize}
\item \(\bv{R}(0, \hat{\gv{e}}) = \bv{I}\)
\item \(\bv{R}(\theta, \hat{\gv{e}})\bv{R}(\phi, \hat{\gv{e}}) =
     \bv{R}(\theta+\phi, \hat{\gv{e}})\)
\item \(\bv{R}\bv{R}^T = \bv{R}^T\bv{R} = \bv{I}\)
\end{itemize}
We note that this operator always exists and is unique for given axis-angle
(hence its form).
\note{:B\_note:
\begin{itemize}
\item Use k\^{}4 = -k\^{}2 in the rr\^{}T thing
\item Use k\^{}T = -k.
\end{itemize}}
\end{frame}
\begin{frame}[label={sec:orgf768bc6},fragile]{Simplification using sympy}
 \begin{minted}[]{python}
import sympy as sp
from sympy.simplify.fu import TR8, TR9, TR10i
x, y, k = sp.symbols('x y k')
expr_x = 1 + sp.sin(x)*k + (1-sp.cos(x))*k**2
expr_y = 1 + sp.sin(y)*k + (1-sp.cos(y))*k**2
expr_n = sp.fu(expr_x * expr_y,
             measure=lambda x: -x.count_ops())
# expr = TR8(expr_x * expr_y)
# print(TR9(expr))
print(expr_n)
\end{minted}

\begin{verbatim}
k**4*cos(x)*cos(y) - 2*k**4*cos(x/2 - y/2)*cos(x/2 + y/2) + k**4 + 2*k**3*sin(x/2 + y/2)*cos(x/2 - y/2) - k**3*sin(x + y) + k**2*sin(x)*sin(y) - 2*k**2*cos(x/2 - y/2)*cos(x/2 + y/2) + 2*k**2 + 2*k*sin(x/2 + y/2)*cos(x/2 - y/2) + 1
\end{verbatim}
\end{frame}

\begin{frame}[label={sec:org158adf6}]{Rotation using Euler angles : Rodrigues formula (geometry)}
But where did it come from?
\alert{GEOMETRY} (part I)
\begin{figure}[htbp]
\centering
\includegraphics[width=0.45\textwidth]{images/rodrigues.pdf}
\caption{Geometrical construction for deriving the Rodrigues rotation formula}
\end{figure}
\[ \gv{v}_{\mathrm {rot} } =\cos \theta \,\gv{v} +(1-\cos \theta
)(\gv {k} \cdot \gv {v} )\gv {k} +\sin \theta \,\gv {k} \times
\gv {v} \]
\end{frame}
\begin{frame}[label={sec:orgf41877c}]{Rotation using Euler angles : Rodrigues formula (geometry)}
 \alert{GEOMETRY} (part II)
 Given
\[ \gv {v}_{\mathrm {rot} } =\cos \theta \,\gv {v} +(1-\cos \theta
  )(\hat{\gv {k}} \cdot {\gv{v}} ) \hat{\gv{k}} +\sin \theta \,\hat{\gv{k}} \times
  \gv {v} \]

With \(\mathbf{K}\gv{v} = \hat{\gv{k}} \times \gv{v}\), we have \(\mathbf{K}\left(
  \mathbf{K} \gv{v}\right) =  \hat{\gv{k}} \times \hat{\gv{k}} \times \gv{v}
  = (\hat{\gv{k}} \cdot \gv{v}) \hat{\gv{k}} - (\hat{\gv{k}} \cdot \hat{\gv{k}})
  \hat{v}\) (Using vector triple product).

Substitute in the original equation,
\[  \gv{v}_{\mathrm {rot} }=\gv {v} +(\sin \theta )\gv {K} \gv {v}
  +(1-\cos \theta )\gv {K} ^{2}\gv {v} \,,\quad \|\gv {K} \|_{2}=1
  \]

Now factor \(\gv{v}\) from the equation to get \(\gv{v}_{\mathrm {rot} } = \mathbf{R}\gv{v}\)
\end{frame}
\begin{frame}[label={sec:org1e92dc2}]{Digression: ODEs}
\begin{itemize}
\item To further understand the Rodrigues rotation formula (and how it relates
to solving \(\frac{\partial \bv{d}_j}{\partial t} = \omega \times \bv{d}_j\) ), we digress a bit and deal with ordinary differential equations (and
their solutions)
\item The next lecture also deals with the same issues (time-stepping and
numerical stability) and the fundamentals are the same.
\end{itemize}
\begin{example}<1->[Solve the following ODE]
\[ \frac{dx}{dt} = 2x \]
\end{example}
\begin{alertblock}<2->{We can't!}
\begin{itemize}
\item Uniqueness and existence?
\item Initial conditions?
\end{itemize}
\end{alertblock}
\end{frame}
\begin{frame}[label={sec:orgde876ed}]{Digression: simple ODEs}
\begin{example}[Solve the following ODE]
\[ \frac{dx}{dt} = 2x \quad x(0) = 1 \]
\end{example}
\begin{block}{We can!}
\[ x(t) = e^{2t}\]
\end{block}
\end{frame}
\begin{frame}[label={sec:orgc3d3da6}]{Digression: system of simple ODEs}
\begin{example}[Solve the following ODE]
\[ \begin{bmatrix}\dot{x} \\ \dot{y} \end{bmatrix} =
	 \begin{bmatrix} 5 & 0 \\ 0 & 3 \end{bmatrix} \cdot
	 \begin{bmatrix}{x} \\ {y} \end{bmatrix} \quad x(0) = 1, y(0) = 2\]
\end{example}
\begin{block}{We can solve this too}
\[ x(t) = e^{5t} \quad y(t) = 2e^{3t} \]

More importantly,
\[ \gv{x}(t) = e^{\bv{A}t}\gv{x}(0)\]
\end{block}
\note{:B\_note:
\begin{itemize}
\item Explain matrix exponential to these dudes
\end{itemize}}
\end{frame}
\begin{frame}[label={sec:orgc731e45}]{Digression: changing it up a bit}
\begin{example}[Solve the following ODE]
\[ \begin{bmatrix}\dot{x} \\ \dot{y} \end{bmatrix} =
	 \begin{bmatrix} 0 & 1 \\ -1 & 0 \end{bmatrix} \cdot
	 \begin{bmatrix}{x} \\ {y} \end{bmatrix} \quad x(0) = 1, y(0) = 0\]
\end{example}
\begin{block}<2->{We can solve this too!}
\[ x(t) = cos{t} \quad y(t) = \sin{t} \]
\alert{OR}
\[ x(\theta) = cos{\theta} \quad y(\theta) = \sin{\theta} \]
\end{block}
\begin{alertblock}<3->{Matrix exponential}
We still retain \[ \gv{x}(\theta) = e^{\bv{A}\theta}\gv{x}(0) \]
\end{alertblock}
\note{:B\_note:
\begin{itemize}
\item Use Hamiltonian. That is dy/dx = x/y and then integrate
\end{itemize}}
\end{frame}
\begin{frame}[label={sec:org0e52f93}]{Digression: changing it up a bit}
\begin{example}[Solve the following ODE]
\[ \begin{bmatrix}\dot{x} \\ \dot{y} \end{bmatrix} =
	 \begin{bmatrix} 0 & 1 \\ -1 & 0 \end{bmatrix} \cdot
	 \begin{bmatrix}{x} \\ {y} \end{bmatrix} \quad x(0) = 1, y(0) = 0\]
\end{example}
\begin{block}{What changed the solutions from exponentials to trigonometric terms?}
\begin{itemize}
\item The skew-symmetry of the matrix!
\item More importantly, a skew-symmetric matrix has a pair of imaginary
eigenvalues \(\pm j \theta\)
\item We know \(\mathrm{Re}{(e^{j \theta})} = \cos(\theta)\), which is exactly
what we see\ldots{}
\item \alert{IDEA} : Matrix exponentials can also be used to perform rotations!
\item Then, can you connect it back to why \(\frac{\partial \bv{d}_j}{\partial t} = \omega \times \bv{d}_j\) performs a rotation?
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[label={sec:org0d9b74a}]{Rotation using Euler angles : Rodrigues formula (algebra)}
With \(\mathbf{K}\gv{v} = \hat{\gv{k}} \times \gv{v}\), we have
\[ \bv{R}=\exp(\theta \bv {K} )=\sum _{k=0}^{\infty }{\frac {(\theta \bv
{K} )^{k}}{k!}}= \bv{I} + \theta \bv {K} + {\frac {1}{2!}}(\theta \bv {K}
)^{2} + {\frac {1}{3!}}(\theta \bv {K} )^{3} + \cdots  \]

Because of skew-symmetry and orthogonality, by Cayley-Hamilton theorem we have, \(\mathbf {K} ^{3}=-\mathbf {K}, \mathbf {K}^{4}=-\mathbf{K}^2,\mathbf
  {K}^{5}=\mathbf{K},\mathbf{K}^{6}=\mathbf{K}^2\).

With this cyclic pattern continuing for \(k \to \infty\), we have
\(\bv{R}=\bv{I}+\left(\theta -{\frac {\theta ^{3}}{3!}}+{\frac {\theta
  ^{5}}{5!}}-\cdots \right)\mathbf{K} +\left({\frac {\theta ^{2}}{2!}}-{\frac
  {\theta ^{4}}{4!}} + {\frac {\theta ^{6}}{6!}}-\cdots \right) \mathbf{K} ^{2}\)

or equivalently

\(\mathbf {R} =\mathbf {I} +(\sin \theta )\mathbf {K} +(1-\cos \theta )\mathbf {K} ^{2}\)
\end{frame}
\begin{frame}[label={sec:org47dfbc0}]{Rotation : Rodrigues formula implementation}
\begin{itemize}
\item We have seen how the matrix exponential can give rise to rotation.
\item Let's implement this operation in our framework
\end{itemize}
\[ \mathbf {R} =\mathbf {I} +(\sin \theta )\mathbf {K} +(1-\cos \theta )\mathbf {K} ^{2} \]
, \(\bv{K}\) being the now-familiar skew-symmetric matrix having vector elements
\[ \mathbf {K} = \begin{bmatrix}\,\,0&\!-k_{3}&\,\,\,k_{2}\\\,\,\,k_{3}&0&\!-k_{1}\\\!-k_{2}&\,\,k_{1}&\,\,0\end{bmatrix}
   \]
\begin{itemize}
\item \alert{ACTIVITY}
\end{itemize}
\end{frame}
\begin{frame}[label={sec:org583d339}]{Rotation : Rodrigues formula IRL}
\begin{itemize}
\item In mechanics, frame rotations are omnipresent
\item One familiar real life example is when an elastic rod experiences a
torsional force
\# \#+ATTR\_LATEX: :width 0.45\textwidth
\end{itemize}

\begin{center}
\begin{tikzpicture}[
	declare function={squarex(\t) =
	(\t < 0.25) ? 1 : (
	(\t < 0.5) ? (1 - (\t-0.25) * 8) : (
	(\t < 0.75) ? -1 : (-1 + (\t - 0.75) * 8)
	)
	;},
	declare function={squarey(\t) =
	(\t < 0.25) ? (-1 + \t*8) : (
	(\t < 0.5) ? 1 : (
	(\t < 0.75) ? (1 - (\t - 0.5) * 8) : -1
	)
	;}
	]
	\begin{axis}[
	axis equal image,
	z buffer=sort,
	hide axis,
	domain=0:1, y domain = 0:10, samples y = 25,
	ylabel =y, xlabel=x,
	clip=false
	]

	\addplot3 [
	z buffer=none, domain=0:330, samples y=1,
	ultra thick, metropolisblue, -latex] (
	-0.5,
	{sin(x)*1.5},
	{cos(x)*1.5}
	);

	\addplot3 [surf, shader=flat, fill=metropolisorange!70, draw=metropolisblue] (
	y,
	{squarex(x) * cos(y*9) - squarey(x) * sin(y*9)},
	{squarex(x) * sin(y*9) + squarey(x) * cos(y*9)}
	);

	\addplot3 [z buffer=auto, fill=metropolisorange!70, draw=black,table/row sep=\\] table[row sep=\\] {
	x y z\\10 -1 -1\\10 1 -1\\10 1 1\\10 -1 1\\10 -1 -1\\};

	\addplot3 [
	z buffer=auto, domain=-60:270, samples y=1,
	ultra thick, metropolisblue, latex-] (
	10.5,
	{sin(x)*1.5},
	{cos(x)*1.5}
	);
	\end{axis}
\end{tikzpicture}
\end{center}

\begin{itemize}
\item Let's see this in our framework too\ldots{}
\item \alert{DEMO}
\item It was possible to code the frames up that way (linearly), because of the
spatial rate of change of the frame angle.
\item \alert{Curvature}
\end{itemize}
\end{frame}
\begin{frame}[label={sec:orgb765551}]{Rodrigues formula : The inverse operator}
\begin{block}{What about the inverse operation?}
Given the rotation matrix \(\bv{R}\)
\begin{itemize}
\item Identify \(\theta\)
\item Identify \(\hat{\gv{e}}\), the axis
\end{itemize}
\end{block}
\begin{block}{Is it useful?}
Very. Especially when:
\begin{itemize}
\item computing the angle by which two frames differ (especially in graphics)
\item there are governing equations that need differences of, rather than
angles themselves (invariance principles)
\end{itemize}
\end{block}
\begin{block}{How to do it?}
Using the \alert{matrix logarithm} \(\log(\cdot)\) operator, which relies on properties of the
rotation matrix\ldots{}
\end{block}
\end{frame}
\begin{frame}[label={sec:orgf127c07}]{Rodrigues formula : The logarithm operator}
\begin{block}{Formula for \(\theta\)}
\[ \theta = \arccos\left( \frac{\text{Tr}(\mathbf{R}) - 1}{2}\right) \]
Why?
\begin{itemize}
\item trace of a matrix is invariant and \(= \sum \lambda_i\).
\item For a rotation matrix, \(\lambda = {1, e^{\pm j \theta}}\)
\item \(\therefore\) \(\sum \lambda_i = 1 + 2 \cos(\theta)\)
\end{itemize}
\end{block}
\begin{block}{Once \(\theta\) is known, we can find \(\hat{\gv{e}}\)\ldots{}}
\end{block}
\end{frame}
\begin{frame}[label={sec:org51a55e5}]{Rodrigues formula : The logarithm operator}
\begin{block}{Finding \(\hat{\gv{e}}\)\ldots{}}
 Use properties of \(\mathbf{R}\)
\[ \mathbf {R} =\mathbf {I} +(\sin \theta )\mathbf {K} +(1-\cos \theta )\mathbf {K} ^{2} \]
Transposing, using \(\mathbf{K}^T = -\mathbf{K}\) and \((\mathbf{K}^2)^{T} = \mathbf{K}^2\)
\[ \mathbf {R}^T =\mathbf {I} - (\sin \theta )\mathbf {K} +(1-\cos \theta )\mathbf {K} ^{2} \]
Subtracting both the equations,
\[ \mathbf{K} = \left( \frac{\mathbf {R} - \mathbf {R}^T}{2 \sin \theta} \right) \]
\end{block}
\end{frame}
\begin{frame}[label={sec:org5309be5}]{Summary}
In this lecture, we
\begin{itemize}
\item understood basic linear/affine transformations relevant in mechanics
\item investigated rotations, and interpreted them as basis-change
transformations
\item saw properties on \(\gv{d}, \bv{Q}\) that linked them back to rotation
\item learnt (+ implemented) a couple (more importantly, the Rodrigues formula)
of ways to rotate basis frames
\end{itemize}
\end{frame}
\begin{frame}[label={sec:org6fd743a}]{Temporal/Spatial rates of change}
What about temporal/spatial changes in rotation? i.e. given a frame, and
given its \emph{change}, how do we predict nearby frames (in time and space)?

\alert{DEMO}
\end{frame}
\end{document}


% Created 2016-03-08 Tue 19:26
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fixltx2e}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{float}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{marvosym}
\usepackage{wasysym}
\usepackage{amssymb}
\usepackage{hyperref}
\tolerance=1000
\usepackage{color}
\usepackage{minted}
\usepackage{color}
\usepackage{minted}
\usepackage{parskip}
\author{Tammo Rukat}
\date{March 10, 2016}
\title{The Hamming Machine}
\hypersetup{
  pdfkeywords={},
  pdfsubject={},
  pdfcreator={Emacs 24.5.1 (Org mode 8.2.10)}}
\begin{document}

\maketitle
\setcounter{tocdepth}{1}
\tableofcontents

\section*{Basics}
\label{sec-1}
\begin{itemize}
\item Based on the hamming distance between two binary vectors ${h(\mathbf{x},\mathbf{u})}$ we construct a probability distribution of ${\mathbf{x}}$ given ${\mathbf{u}}$ with a dispersion parameter ${\lambda}$: $$ p(\mathbf{x}|\mathbf{u}) \propto \exp\left[ -\lambda h(\mathbf{x},\mathbf{u}) \right] $$
\item We assume a set of ${M}$ binary vectors ${\mathbf{u}_{1\ldots M}}$, that we call \textbf{codes}.
\item Each observations ${\mathbf{x} }$ is generated from a subset of these codes: $$ p(\mathbf{x}|\mathbf{U},\mathbf{s},\lambda) \propto \prod\limits_m p(\mathbf{x}|\mathbf{u}_m,\lambda)^{s_m} = \prod\limits_d \exp\left[ \sum_m s_m \lambda h(x_d,u_{md}) \right]$$
\item We find the normalised probability to be sigmoidal: $$ p(x_d = 1|\mathbf{s}, \mathbf{U}, \lambda) = \frac{1}{1+\exp\left[ \lambda \sum_m s_m (2u_{md} - 1) \right]} $$
\end{itemize}

\section*{Sigmoid Belief Net}
\label{sec-2}
\begin{itemize}
\item $$ p(x_d = 1|\mathbf{s}, \mathbf{U}, \lambda) =\frac{1}{1+\exp\left[ \lambda \sum_m s_m (2u_{md} - 1) \right]} $$
\end{itemize}

\includegraphics[width=.9\linewidth]{figures/hm0_1.png}

\section*{Inference}
\label{sec-3}
\begin{itemize}
\item Gibbs sampling for ${\mathbf{u}}$ and ${\mathbf{s}}$ with uninformative priors.
\item Metropolis Hastings for $\lambda$ with inverse gamma prior.
\item Precomputing quantities of the form $$ \log(1+e^{-\lambda m}); \,\,\,\, \text{for} \, m \in \{-M,\ldots,-1,0,1,\ldots,M\} $$ speeds up the computation.
\end{itemize}

\section*{Multilayer Hamming Machine}
\label{sec-4}
\includegraphics[width=.9\linewidth]{figures/hm2.png}
\begin{itemize}
\item The lower layer acts like a prior on the peripheral layer.
\end{itemize}

\section*{Toy Example}
\label{sec-5}
\url{figures/a4_10_5.gif}

\section*{Sparsity priors on ${\mathbf{u}}$}
\label{sec-6}
\begin{enumerate}
\item Independent Bernoulli prior on every single code unit ${u_{md}}$
\item Bernoulli prior controlling the number of 1s in every code.
\end{enumerate}
E.g. \emph{step-exp prior}

\includegraphics[width=.9\linewidth]{figures/prior.png}
$$  p(u = 1) = \tfrac{1}{2} \mathrm{H}( 1 - q ) + \tfrac{1}{2} \mathrm{H}(q-1) e^{-a(q-1)} $$

\subsection*{Effect of sparsity prior}
\label{sec-6-1}
\url{figures/a4_10_5_sparse.gif}

\section*{Example -- mnist}
\label{sec-7}
\subsection*{Sampling}
\label{sec-7-1}
\includegraphics[width=.9\linewidth]{figures/mnist_sampler.png}
\subsection*{Reconstructions}
\label{sec-7-2}
\includegraphics[width=.9\linewidth]{figures/recon_1.png}
\includegraphics[width=.9\linewidth]{figures/recon_2.png}
\subsection*{Patterns}
\label{sec-7-3}
\includegraphics[width=.9\linewidth]{figures/snb_1_1.png}
\subsection*{Patterns}
\label{sec-7-4}
\includegraphics[width=.9\linewidth]{figures/snb_0_1.png}
% Emacs 24.5.1 (Org mode 8.2.10)
\end{document}


\addcontentsline{toc}{chapter}{Appendices}

% The \appendix command resets the chapter counter, and changes the chapter numbering scheme to capital letters.
%\chapter{Appendices}
\appendix
\chapter{DDPG Algorithm using a Custom Policy Architecture}
\label{app:ddpgfm}
\begin{center}
 \begin{algorithm}[!htbp]
%    \SetAlgoLined
    \DontPrintSemicolon
    \caption{DDPG algorithm}
    \label{alg:ddpg_alg}

    Randomly initialize actor and critic networks $f_{\theta^{\pi}}$ $Q_{\theta^Q}$\;
    Initialize target network $f_{\theta^{\pi}}$ and $Q_{\theta^{Q}}$ with weights
    $\theta^{Q'} \leftarrow \theta^Q$, $\theta^{\pi'} \leftarrow \theta^{\pi}$\;
    Initialize policy model $g$ with elements in $\mathcal{A}$\;
    Initialize replay buffer B\;
    \For{episode = 1 \KwTo $M$}{
    	Initialize a random process $\mathcal{N}$ for action exploration\;
	Receive initial observation state $\mathbf{s}_1$ from environment\;
	\For{t = 1 \KwTo $T$}{
		Select action $\mathbf{a}_t = \pi_{\theta(\mathbf{s}_t)}$ according current policy and exploration method\;
		Send action $\mathbf{a}_t$ and observe reward  $r_t$ and new state $\mathbf{s}_{t + 1}$\;
		Store transition $(\mathbf{s}_t, \mathbf{a}_t, r_t, \,mathbf{s}_{t+1})$ in B\;
		Sample a random minibatch of N transitions from B\;
		Set $y_i = r_i + \gamma \cdot Q_{\theta^{Q'}}(\mathbf{s}_{i+1}, \pi_{\theta'}(\mathbf{s}_{i+1}))$\;
		Update the critic by minimizing the loss: \;
		$L(\theta^Q) = \frac{1}{N}\sum_{i}[y_i - Q_{\theta^{Q}}(\mathbf{s_i}, \mathbf{a}_i)]^2$\;
		Update the actor using the sampled gradient:\;
		$\nabla_{\theta^{\pi}}f_{\theta^{\pi}}|_{\mathbf{s}_i} \approx \frac{1}{N}\sum_i \nabla_{\mathbf{a}}Q_{\theta^{Q}}(\mathbf{s}, \mathbf{\hat{a}}) |_{\mathbf{\hat{a}}=f_{\theta^{\pi}}(\mathbf{s}_i)} \cdot \nabla_{\theta^{\pi}}f_{\theta^{\pi}}(\mathbf{s}) |_{\mathbf{s}_i}$\;
		Update the target networks:\;
		$\theta^{Q'} \leftarrow \tau\theta^Q + (1 - \tau)\theta^{Q'}$\;
		$\theta^{\pi'} \leftarrow \tau\theta^{\pi} + (1 - \tau)\theta^{\pi'}$\;
	}
    }
  \end{algorithm}
\end{center}

The pseudocode presented above is based in the work presented by Dulac-Arnold et al. in \cite{Dulac-Arnold2015}

\chapter{Transition probability Matrix algorithm}
\label{app:trans_prob_alg}

\begin{center}
 \begin{algorithm}[!htbp]
%    \SetAlgoLined
    \DontPrintSemicolon
    \caption{Transition Probability Matrix(R)}
    \label{alg:transition_matrix}

    \KwInput{$R$: Initial User Rating Matrix}
    \KwResult{$\mathcal{P}$: Transition Probability Matrix}
    %$\textbf{e} \leftarrow trigger\_candidate(\textbf{x})$\;
    $S \leftarrow ComputeCosineSimilarityMatrix(R)$\;
    \For{$i \leftarrow 1$ \KwTo $m$}{
        $sum \leftarrow \sum^{m}_{j=1} S_{ij}$\;
    \For{$j \leftarrow 1$ \KwTo $m$}{
    \tcc{user walks through a direct neighborith probability $\beta$ or jumps to an arbitrary item with uniform probability}
    $\mathcal{P}_{ij} \leftarrow \beta S_{ij} / sum + (1 - \beta) / m$\;
    }
    }
    \KwRet $\mathcal{P}$
  \end{algorithm}
%\end{minipage}
\end{center}

\chapter{Recommender System Simulated Environment}
\label{app:simulated_env}

\begin{center}
 \begin{algorithm}[!htbp]
    \SetAlgoVlined
    \DontPrintSemicolon
    \caption{Simulated environment}
    \label{alg:simulated_env}

    \KwInput{$R$: Ratings Matrix; $\mathcal{P}$: Transition Prob. Matrix; $\mathcal{I}$: Items set\;}
%    \KwResult{$\textbf{s}(\textbf{e}, \textbf{a}; \textbf{x}, \boldsymbol{\lambda})$}

    $u_{random} \leftarrow selectRandomUser(R)$ \textit{\# selects an active user}\;
    $state \leftarrow selectLikelyItem(\mathcal{I}, u_{random})$ \textit{\# initial guided exploration}\;
    $term \leftarrow False$\;
    \While{$not$ $term$}{
    	$action \leftarrow waitForAgentAction()$\;
	\tcc{probability of choosing action given current state}
	$\rho_{action} = \leftarrow computeTransition(\mathcal{P}, state, action)$\;
	$\mathit{rating} \leftarrow getUserRating(u_{random}, action)$\;
	\tcc{get the mean and std dev of the transition probability distr. for a given state}
	$\mu, \sigma \leftarrow transProbDistribution(\mathcal{P}, state)$\;
	$\mathit{threshold} \leftarrow \mu$\;
	$is\_chosen \leftarrow \rho_{action} > \mathit{threshold}$ \textbf{and} $\mathit{rating} > 0$\;
	\eIf{$is\_chosen$}{
		$\rho_{end} \leftarrow 0.1$\;
		$reward \leftarrow rating$\;
	}{
		$\rho_{end} \leftarrow 0.2$\;
		$reward \leftarrow 0$\;
		$action \leftarrow selectRandomItem(u_{random})$\;
	}
	$state \leftarrow action$\;
	\If{$random.uniform() < \rho_{end}$}{
		$term \leftarrow True$\;
	}
	$sendSignalToAgent(state, reward, term)$\;
    }
  \end{algorithm}
\end{center}

\chapter{Hyperparameter configuration for models}
\label{app:hyperparameter}

\begin{table}[!htbp]
%\begin{center}
\centering
\begin{tabular}{ |l|c|c|c| }
  \hline
%  \multicolumn{2}{|c|}{100K Movielens} & 1M Movielens \\
  \textbf{Hyperparameter}& \textbf{DRL-kNN-CB} & \textbf{DRL-kNN-CF} & \textbf{DRL-FM} \\
  \hline
  Hidden Layer 1 Size & 400 & 1,000 & 400 \\ \hline
  Hidden Layer 2 Size & 300 & 1,000 & 400 \\ \hline
  Replay Memory size & \multicolumn{3}{c|}{50,000} \\ \hline
  $k-NN/Top-K$ & \multicolumn{3}{c|}{5\% of action set size} \\ \hline
  $\mu$ $L_2$ reg. & \multicolumn{3}{c|}{0} \\ \hline
  $\mathcal{Q}$ $L_2$ reg. & \multicolumn{3}{c|}{0.01} \\ \hline
  $\nabla \mu$ learning rate & \multicolumn{3}{c|}{0.0001} \\ \hline
  $\nabla \mathcal{Q}$ learning rate & \multicolumn{3}{c|}{0.001} \\ \hline
  $\tau$ & \multicolumn{3}{c|}{0.001} \\
  $\gamma$ & \multicolumn{3}{c|}{0.99} \\
  \hline
\end{tabular}
%\end{center}
\caption{Hyperparameter and network settings for DRL models}
\label{table:hyperparameters}
\end{table}

\chapter{Implementation notes and Installation Requirements}
\label{app:implementation}

\section{System Dependencies}

The following packages are needed to execute the project models

\begin{itemize}
\item Python 2.7
\item Virtualenv
\item OpenAI gym (included in this project)
\item Tensorflow
\item Jupyter notebook
\item Graphlab Create API 2.1 (with Academic License)
\end{itemize}

\section{Setup}

To setup the environment, the following commands need to be executed:

\begin{lstlisting}[language=bash]
$ git clone https://github.com/santteegt/ucl-drl-msc
$ cd ucl-drl-msc
$ git submodule update --init --recursive
$ mkdir .venv
$ virtualenv --system-site-packages --python=python2.7 .venv/
$ source .venv/bin/activate

(venv)$ cd gym
(venv)$ pip install -e .
# installation for Mac OS X. For other platforms,
# refer to
# https://www.tensorflow.org/versions/r0.9/get_started/os_setup.html
(venv)$ export TF_BINARY_URL=
https://storage.googleapis.com/tensorflow/mac/tensorflow-0.9.0-py2-none-any.whl
(venv)$ pip install --upgrade $TF_BINARY_URL
(venv)$ pip install pymongo pandas gensim fastFM matplotlib scikit-learn scipy
(venv)$ pip install --upgrade --no-cache-dir
https://get.graphlab.com/GraphLab-Create/2.1/hernan.toral.15@ucl.ac.uk/
55E9-2088-3AF8-120F-D171-1AAB-95A3-B077/GraphLab-Create-License.tar.gz
\end{lstlisting}

\section{Datasets}

Movielens datasets files were pre-processed for running the models. They can be downloaded from the following URL: \url{https://mega.nz/#F!oFYFmZyQ!UERNs4e3Jvvkml2zNj2ngA}. Then, you can follow the instructions below:

\begin{itemize}
\item Extract and copy the contents of \textit{FMmodel.zip} to the \textit{data} folder
\item To use the Movielens-100k dataset, extract the contents of the \textit{data-movielens100k.zip} file into the \textit{data} folder
\item To use the Movielens-1M dataset, extract the contents of the \textit{data-movielens1m.zip} file into the 'textit{data} folder
\end{itemize}

\section{Running the environment}

To make the running process more easier, executable files for each experiment are provided under the `bin` folder (make sure to activate the virtualenv before running the exec file):

\begin{enumerate}
\item \textbf{DRL-kNN-CB}: \textit{run-v2.sh} runs the model for the Movielens 100k dataset, while \textit{run-v0.sh} runs the model for the Movielens 1M dataset
\item \textbf{DRL-kNN-CF}: \textit{run-v1.sh} runs the model for the Movielens 100k dataset.
\item \textbf{DRL-FM}: \textit{run-v3.sh} runs the model for the Movielens 100k dataset.
\item \textbf{Random agent}: \textit{run-v1-random.sh} and \textit{run-v2-random.sh} runs the DDPG algorithm using a random agent
\end{enumerate}

Human readable rendering of recommendations made while running the model can be seen using the following command:

\begin{lstlisting}[language=bash]
(venv)$ cd ucl-drl-msc
(venv)$ tail -F run.log
\end{lstlisting}

Reward logs obtained by an agent while running the model can be read using the following command:

\begin{lstlisting}[language=bash]
(venv)$ cd ucl-drl-msc
(venv)$ tail -F ddpg-results/experiment1/output.log
\end{lstlisting}

Additionally, to visualize the model parameters during training, you can run the Tensorflow dashboard using the following command:

\begin{lstlisting}[language=bash]
(venv)$ cd ucl-drl-msc
(venv)$ python src/dashboard.py --exdir ddpg-results/
\end{lstlisting}

Finally, to stop the training process, it is necessary to run the following command:

\begin{lstlisting}[language=bash]
(venv)$ cd ucl-drl-msc/bin
(venv)$ sh stop.sh
\end{lstlisting}

\section{License}

The Project code is mostly developed under the Apache2 license, except for the module developed using Graphlab Create which is based on an Academic License provided by Turi.

%\chapter{Colophon}
%\label{appendixlabel3}
%\textit{This is a description of the tools you used to make your thesis. It helps people make future documents, reminds you, and looks good.}
%
%\textit{(example)} This document was set in the Times Roman typeface using \LaTeX\ and Bib\TeX , composed with a text editor.
 % description of document, e.g. type faces, TeX used, TeXmaker, packages and things used for figures. Like a computational details section.
% e.g. http://tex.stackexchange.com/questions/63468/what-is-best-way-to-mention-that-a-document-has-been-typeset-with-tex#63503

% Side note:
%http://tex.stackexchange.com/questions/1319/showcase-of-beautiful-typography-done-in-tex-friends


\documentclass[twocolumn,prl,nobalancelastpage,aps,10pt]{revtex4-1}
%\documentclass[rmp,preprint]{revtex4-1}
\usepackage{graphicx,bm,times,amsmath}
\usepackage{float} %useful to structure the figures if they're not going where you'd like them to. When beginning a figure environment with "\begin{figure}" add An H to the end to make the placement more forceful like "\begin{figure}[H]"
\usepackage{gensymb} %has some extra symbols like the degree symbol
\graphicspath{{figures/}}

\begin{document}

\title{Investigating computational numerical integration using Simpson's rule for Fresnel diffraction}
\author{Francis Taylor}
\affiliation{Level 5 Laboratory, School of Physics, University of Bristol.}

\date{\today}

\begin{abstract}
The diffraction patterns produced by monochromatic light passing through an aperture were modelled in one and two dimensions using Fresnel's diffraction equation, and evaluated using Simpson's $\frac{1}{3}$ rule for numerical integration. The effect of changing the distance from the aperture to the screen the patterns were projected onto, and changing the size and shape of the aperture were investigated. Suggestions for improvements to the model were made.
\end{abstract}

\maketitle

\section{INTRODUCTION \& THEORY}

Fresnel's diffraction equation for near-field diffraction is an approximate form of the more complex, and more widely applicable, Kirchoff diffraction equation \cite{Born1999}. It is used to model the diffraction pattern of light when a monochromatic light ray passes through a small aperture in an opaque screen \cite{Hecht2002}. Kirchoff's diffraction equation simplifies to the Fresnel diffraction equation when all terms of third- and higher-order can be neglected. This gives \cite{Born1999}:

\begin{equation}
\begin{aligned}
E(x,y,z) = \frac{e^{ikz}}{i\lambda z} \iint E(x', y')\exp{\{\frac{ik}{2z}[(x-x')^{2}\\
														  	  \qquad+(y-y')^{2}]\}}dx'dy',
\end{aligned}
\label{LongFresnel}
\end{equation}

which is the general form of the Fresnel diffraction equation. Here, $E$ is the electric field observed on a screen and is proportional to the intensity of light on the screen, $x$ and $y$ are the Cartesian coordinates on the screen, $z$ is the distance from the aperture to the screen, $x'$ and $y'$ are the width and height of the aperture respectively, and $k$ is the wave-number. By taking $E(x',y')$ to be zero everywhere apart from the aperture, where it has a constant value of $E_{0}$, we can simplify (\ref{LongFresnel}) to:

\begin{equation}
\begin{aligned}
E(x,y,z) = \frac{kE_{0}}{2\pi z} \iint_{aperture} \exp{\{\frac{ik}{2z}[(x-x')^{2}\\
														 \qquad+(y-y')^{2}]\}}dx'dy'.
\end{aligned}
\label{ShortFresnel}
\end{equation}

From this, the observed diffraction intensity can be obtained from the square of the magnitude of $E$:

\begin{equation}
I(x,y,z) = \epsilon_{0} c E(x,y,z)E^{*}(x,y,z),
\label{Intensity}
\end{equation}

where $\epsilon_{0}$ is the permittivity of free space, $c$ is the speed of light in vacuo, and $E^{*}$ is the complex conjugate of $E$.

\subsection{Simpson's Rule}
The analytical solution of the Fresnel equation is impossible for most aperture geometries, therefore it is usually calculated numerically \cite{Sears1948}. One method is to use Simpson's Rule, which approximates a given function $f(x)$ by a series of evenly spaced quadratics. The general form of this is \cite{Hazewinkel1988}

\begin{equation}
\begin{aligned}
\int_{a}^{b} f(x)dx \approx \frac{h}{3}[f(x_{0}) + 4f(x_{1}) +2f(x_{2}) + 4f(x_{3})\\
										\qquad + 2f(x_{4}) + \cdots + 4f(x_{n-1}) + f(x_{n})],
\end{aligned}
\label{Simpson}
\end{equation}

where $n$ is the number of equally space divisions and the spacing $h = \frac{b-a}{n}$. The error between Simpson's approximation and the actual function scales with $\mathcal{O}(h^{5})$, so when $h$ is close to 0, there is an infinitesimal difference of a constant multiplied by $h^{5}$  \cite{Atkinson1989a, DeBruijn1970}. In this simulation, a function was written to perform a Fresnel integral using Simpson's $\frac{1}{3}$ rule, by iterating over the range $x'_{1}$ to $x'_{2}$, which are the physical limits of the aperture, for each value $x$ on the screen.

\section{RESULTS \& DISCUSSION}

The diffraction pattern of a $\lambda = 1 \times 10^{-6}$m wave passing through an aperture of horizontal size $2\times10^{-5}$m, projected onto a screen of width $0.01$m and distance $0.02$m from the aperture, was modelled using (\ref{Simpson}) over 100 equally spaced intervals. This produced a large central intensity peak, with several smaller secondary peaks on either side, as shown by figure \ref{1D_Default}. This suggests that with these parameters, a single bright spot would be seen on the screen, and this is supported by the 2-dimensional intensity map (figure \ref{2D_Default}) that uses the same parameters and a square aperture.

\begin{figure}[!ht]
	\includegraphics*[width=0.96\linewidth,clip]{1D_Default}
	\caption{An intensity graph vs horizontal screen coordinate for the diffraction pattern, using the default parameters.} \label{1D_Default}
\end{figure}

\begin{figure}
\includegraphics*[width=0.96\linewidth,clip]{2D_Default}
\caption{A 2-dimensional intensity map for the diffraction pattern on the screen, using the default parameters.} \label{2D_Default}
\end{figure}

The parameters were then adjusted, decreasing the distance to the screen from the aperture such that $z = 5\times10^{-5}$m, and the screen size was adjusted to $1\times10^{-4}$m in both $x$ and $y$ directions in order to see the near-field features. The 1-dimensional intensity plot produced (figure \ref{1D_smallZ}) shows two large symmetrical peaks, with a region of lower intensity between them. The 2-dimensional plot (figure \ref{2D_smallZ}) confirms this, with four bright spots containing a slightly dimmer central cross. Decreasing $z$ further to the width of the aperture, $2\times10^{-5}$m produces interesting results. The same two peaks are observed, however there are several secondary peaks between these with slightly less intensity.

\begin{figure}[!ht]
	\includegraphics*[width=0.96\linewidth,clip]{1D_z5e-5}
	\caption{An intensity graph vs horizontal screen coordinate for the diffraction pattern, using $z = 5\times10^{-5}$m, and a square screen of length $1\times10^{-4}$m.} \label{1D_smallZ}
\end{figure}

\begin{figure}
	\includegraphics*[width=0.96\linewidth,clip]{2D_z5e-5}
	\caption{A 2-dimensional intensity map for the diffraction pattern on the screen, using $z = 5\times10^{-5}$m, and a square screen of length $1\times10^{-4}$m.} \label{2D_smallZ}
\end{figure}

\begin{figure}
	\includegraphics*[width=0.96\linewidth,clip]{1D_z2e-5}
	\caption{An intensity graph vs horizontal screen coordinate for the diffraction pattern, using $z = 2\times10^{-5}$m, and a square screen of length $1\times10^{-4}$m.} \label{1D_smallerZ}
\end{figure}

\begin{figure}
	\includegraphics*[width=0.96\linewidth,clip]{2D_z2e-5}
	\caption{A 2-dimensional intensity map for the diffraction pattern on the screen, using $z = 2\times10^{-5}$m, and a square screen of length $1\times10^{-4}$m.} \label{2D_smallerZ}
\end{figure}

The resolution is not very good when using $N=100$, as is used in figures \ref{1D_smallerZ} and \ref{2D_smallerZ}, and so N was increased to 500 , which produced the results seen in figures \ref{1D_smallerZ_highN} and \ref{2D_smallerZ_highN}. $N$ cannot go much higher than 500 without needing large amounts of RAM to perform the calculation, and so it has been limited to 500. The intensity peaks are more distinguished than in figures \ref{1D_smallerZ} and \ref{2D_smallerZ}, and the secondary fringes seen at the edges figures \ref{1D_smallerZ} and \ref{2D_smallerZ} not present in figures \ref{1D_smallerZ_highN} and \ref{2D_smallerZ_highN}. A larger value of $N$ was needed to see these features because the spacing between them was very small, and so a greater resolution is needed to see them clearly.

\begin{figure}[!ht]
	\includegraphics*[width=0.96\linewidth,clip]{1D_z2e-5_N500}
	\caption{An intensity graph vs horizontal screen coordinate for the diffraction pattern, using $z = 2\times10^{-5}$m, and a square screen of length $1\times10^{-4}$m. $N=500$} \label{1D_smallerZ_highN}
\end{figure}

\begin{figure}
	\includegraphics*[width=0.96\linewidth,clip]{2D_z2e-5_N500}
	\caption{A 2-dimensional intensity map for the diffraction pattern on the screen, using $z = 2\times10^{-5}$m, and a square screen of length $1\times10^{-4}$m. $N=500$} \label{2D_smallerZ_highN}
\end{figure}

Conversely, increasing $z$ causing less features to be seen in the diffraction pattern, and it becomes a large diffuse point of light, as shown by figures \ref{1D_z20cm} and \ref{2D_z20cm}. Here $N$ has been set back to 100.

\begin{figure}[!ht]
	\includegraphics*[width=0.96\linewidth,clip]{1D_z20cm}
	\caption{An intensity graph vs horizontal screen coordinate for the diffraction pattern, using $z = 0.2$m, and a square screen of length $0.01$m.} \label{1D_z20cm}
\end{figure}

\begin{figure}
	\includegraphics*[width=0.96\linewidth,clip]{2D_z20cm}
	\caption{A 2-dimensional intensity map for the diffraction pattern on the screen, using $z = 0.2$m, and a square screen of length $0.01$m.} \label{2D_z20cm}
\end{figure}

The effect of changing the size and shape of the aperture was also investigated. Figures \ref{1D_WIDTH5E-5} and \ref{2D_WIDTH5E-5} show the effect of a square aperture of width  $5\times10^{-5}$m. All other parameters are the same as in figures \ref{1D_Default} and \ref{2D_Default}. The intensity peak for the larger aperture is much thinner, but the comparative intensity is higher, as less light is being diffracted because the wavelength is much smaller than the aperture \cite{Hecht2002}. There is less spatial resolution with $N=100$, so the 1-dimensional peak is not as smooth and less-well defined as is the 2-dimensional intensity map, with a small, pixelated spot at the centre and faint secondary fringes extending along the $x$ and $y$ planes from the centre. Decreasing the aperture width to the size of the wavelength, $1\times10^{-6}$m, has a similar effect as increasing $d>>\text{screen size}$, whereby the diffraction pattern becomes a diffuse point, seen in figures \ref{1D_WIDTH1E-6} and \ref{2D_WIDTH1E-6}.

\begin{figure}[!ht]
	\includegraphics*[width=0.96\linewidth,clip]{1D_WIDTH5E-5}
	\caption{An intensity graph vs horizontal screen coordinate for the diffraction pattern, with an aperture width of $5\times10^{-5}$m.} \label{1D_WIDTH5E-5}
\end{figure}

\begin{figure}
	\includegraphics*[width=0.96\linewidth,clip]{2D_WIDTH5E-5}
	\caption{A 2-dimensional intensity map for the diffraction pattern on the screen, with a square aperture of width of $5\times10^{-5}$m.} \label{2D_WIDTH5E-5}
\end{figure}

\begin{figure}
	\includegraphics*[width=0.96\linewidth,clip]{1D_WIDTH1E-6}
	\caption{An intensity graph vs horizontal screen coordinate for the diffraction pattern, with an aperture width of $1\times10^{-6}$m.} \label{1D_WIDTH1E-6}
\end{figure}

\begin{figure}
	\includegraphics*[width=0.96\linewidth,clip]{2D_WIDTH1E-6}
	\caption{A 2-dimensional intensity map for the diffraction pattern on the screen, with a square aperture of width of $1\times10^{-6}$m.} \label{2D_WIDTH1E-6}
\end{figure}

Changing the aperture to a rectangle (for the 2-D plot) produces a more rectangular interference pattern. For the plot shown in figure \ref{2D_Rectangular}, $x'=2\times10^{-5}$ and $y'=1\times10^{-5}$.
\begin{figure}[!ht]
	\includegraphics*[width=0.96\linewidth,clip]{2D_Rectangular}
	\caption{A 2-dimensional intensity map for the diffraction pattern on the screen, with a rectangular aperture.} \label{2D_Rectangular}
\end{figure}

\section{IMPROVEMENTS TO CODE}
The program could have been improved by allowing the use of different, non-rectangular apertures, such as a circular aperture. Fresnel's approximation breaks down at very low distances from the aperture to the screen, so the code could have been extended to compare the plots with the Rayleigh-Sommerfeld approximation, which is more accurate at low z \cite{DeBruijn1970}. If physical components such as RAM were not an issue, the number of points on the screen that could be evaluated would be much higher --- $N=1,000,000$ requires roughly 7.3TiB of RAM for the 2-dimensional integral, and likely a very long computation time! However, increasing N would mean that a much better spatial resolution would be possible, allowing the evaluation and analysis of very near-field patterns that aren't possible in this simulation.

\section{CONCLUSIONS}

Simpson's rule has been shown to be valuable and effective in evaluating integrals, specifically Fresnel's approximation for diffraction of light, producing intensity plots similar to those that would be observed in the laboratory. The method could be improved by increasing $N$ such that a greater spatial resolution is possible, allowing the simulation of very near-field patterns that cannot be done due to restrictions on computational resources.

\section{REFERENCES}

\bibliographystyle{report}
\bibliography{C:/Users/Francis/Documents/Bibtex/Coding-Report-3}

\end{document}


% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
  \usepackage{unicode-math}
  \defaultfontfeatures{Scale=MatchLowercase}
  \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
  \usepackage[]{microtype}
  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
  \IfFileExists{parskip.sty}{%
    \usepackage{parskip}
  }{% else
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
  \KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\hypersetup{
  hidelinks,
  pdfcreator={LaTeX via pandoc}}
\urlstyle{same} % disable monospaced font for URLs
\usepackage[margin=1in]{geometry}
\usepackage{graphicx,grffile}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
% Set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering

\author{}
\date{\vspace{-2.5em}}

\begin{document}

\startappendices

\hypertarget{chap:appendix_a}{%
\section{Backpropagation with Binary
Cross-Entropy}\label{chap:appendix_a}}

Let's consider a simple binary classification task. It is common to use
a network with a single logistic output with the binary cross-entropy
loss function and for the sake of simplicity, let's assume that there is
only one hidden layer. \[
\begin{aligned}
BCE=-\sum_{i=1}^{n o u t}\left(y_i \log \left(\hat{y}_i \right)+\left(1-y_i\right) \log \left(1-\hat{y}_i\right)\right)
\end{aligned}
\]

Where \(y\) is the ground truth and \(\hat{y}\) is the output of the
network. After having the loss function, let's continue with the forward
pass.

\[
\begin{aligned}
a_{k} &= h_{k-1} w_{k} + b_k \\
h_k &= f(a_{k})
\end{aligned}
\]

Where, \(w_k\) is the weight, \(b_{k}\) is the bias term, \(h_k\) is the
output of the layer (which means that \(h_0 = X\) and \(h_2 = \hat{y}\))
and f is the non linear function. Please note that for last layer
logistic function is used whereas for hidden layer reLU is used as non
linear functions.\\
We can compute the derivative of the weights by using the chain rule.

\[
\begin{aligned}
\frac{\partial BCE}{\partial w_{2}}=\frac{\partial BCE}{\partial \hat{y}} \frac{\partial \hat{y}}{\partial a_{2}} \frac{\partial a_{2}}{\partial w_{2}}
\end{aligned}
\]

Computing each factor in the term, we have: \[
\begin{aligned}
\frac{\partial BCE}{\partial \hat{y}} &=\frac{-y}{\hat{y}}+\frac{1-y}{1-\hat{y}} \\
&=\frac{\hat{y}-y}{\hat{y}\left(1-\hat{y}\right)} \\
\frac{\partial \hat{y}}{\partial a_{2}} &=\hat{y}\left(1-\hat{y}\right) \\
\frac{\partial a_{2}}{\partial w_{2}} &=h_{1}^T
\end{aligned}
\] Which gives us: \[
\frac{\partial BCE}{\partial w_{2}}=h_{1}^T\left(\hat{y}-y\right)
\] Derivative of the \(w_1\) concerning loss function can be calculated
as the following:

\[
\begin{aligned}
\frac{\partial BCE}{\partial w_{1}}=\frac{\partial BCE}{\partial h_1} \frac{\partial h_1}{\partial a_{1}} \frac{\partial a_{1}}{\partial w_{1}}
\end{aligned}
\] Compute each factor in the term again, we have:

\[
\begin{aligned}
\frac{\partial BCE}{\partial h_1} &= \frac{\partial BCE}{\partial \hat{y}} \frac{\partial \hat{y}}{\partial a_{2}} \frac{\partial a_{2}}{\partial h_{1}}  \\
&= \left(\hat{y}-y\right) w_{2}^T \\
\frac{\partial h_1}{\partial a_{1}} &=f'(a_1) \\
\frac{\partial a_{1}}{\partial h_{1}} &=X^T
\end{aligned}
\] Which gives us: \[
\begin{aligned}
\frac{\partial BCE}{\partial w_{1}}= \left(X\right)^T\left(\hat{y}-y\right)\left(w_{2}^T\right) \odot f'(a_1)
\end{aligned}
\] Where \(\odot\) is element-wise multiplication, similarly, bias terms
can be calculated by following:

\[
\begin{aligned}
\frac{\partial BCE}{\partial b_{2}}&=\frac{\partial BCE}{\partial \hat{y}} \frac{\partial \hat{y}}{\partial a_{2}} \frac{\partial a_{2}}{\partial b_{2}} \\
&= \left(\hat{y}-y\right)
\end{aligned}
\]

\[
\begin{aligned}
\frac{\partial BCE}{\partial b_{1}}&=\frac{\partial BCE}{\partial h_1} \frac{\partial h_1}{\partial a_{1}} \frac{\partial a_{1}}{\partial b_{1}} \\
&= \left(\hat{y}-y\right)\left(w_{2}^T\right) \odot f'(a_1)
\end{aligned}
\] After having all these results, we can update the parameters (weights
and biases) using gradient descent and its variants.

\hypertarget{hidden-representation-of-digits-in-parity-problem}{%
\section{Hidden Representation of Digits in Parity
Problem}\label{hidden-representation-of-digits-in-parity-problem}}

On of the interesting question is: Does the network learn the digits
individually when we train the network and make the necessary processes
(summation, division) or does it memorize the way data is fed? This
question can be answered by observing the hidden representation. If the
network learns the digits well, we need to observe a well separation as
in MNIST task. For this purpose hidden representation of the networks
trained with BP and DFA are plotted in two dimensional space by using
t-SNE \cite{vanDerMaaten2008} implementation of sklearn
\cite{scikit-learn}.\\
Hidden representation of single image in \(k=3\) case is obtained as the
following way; we know that after we flatten the images, particular
parts of each images are multiplied corresponding parts of the weight
matrix. By getting these parts and performing multiplication for each
digit will gives us the hidden representation of the individual digit
that is trained in \(k=3\) case. Process is visualized in
\ref{fig:HiddenRepProcess}

\begin{figure}

{\centering \includegraphics[width=1\linewidth]{../figures/hidden_rep_process}

}

\caption{Process of Hidden Representation}\label{fig:HiddenRepProcess}
\end{figure}

\hypertarget{reproducibility}{%
\section{Reproducibility}\label{reproducibility}}

TODO: Write down the library versions, refer to the github repository
for the experiments, give information about the hardware. Explain the
stochastic behaviour of data.

\end{document}


\subsection{The fundamental theorem of external calculus}


\subsection{Introduction}

Weak stationary processes can be decomposed to a deterministic and a stochastic component.


\par
\section{Data Structure}
\label{section:DenseMtx:dataStructure}
\par
\par
The {\tt DenseMtx} structure has the following fields.
\begin{itemize}
\item
{\tt int type} : type of entries, {\tt SPOOLES\_REAL}
or {\tt SPOOLES\_COMPLEX}.
\item
{\tt int rowid} : object's row id, default value is {\tt -1}.
\item
{\tt int colid} : object's column id, default value is {\tt -1}.
\item
{\tt int nrow} : number of rows
\item
{\tt int ncol} : number of columns
\item
{\tt int inc1} : row increment,
   difference in addresses between entries in the same column
\item
{\tt int inc2} : column increment,
   difference in addresses between entries in the same row
\item
{\tt int *rowind} :
pointer to the base address of the {\tt int} vector
that contains row indices.
\item
{\tt int *colind} :
pointer to the base address of the {\tt int} vector
that contains column indices.
\item
{\tt double *entries} :
pointer to the base address of the {\tt double} vector that
contains the entries.
\item
{\tt DV wrkDV} :
object that manages the owned working storage.
\item
{\tt DenseMtx *next} :
link to a next object in a singly linked list.
\end{itemize}
\par
One can query the type of entries via two macros.
\begin{itemize}
\item
{\tt DENSEMTX\_IS\_REAL(mtx)} returns {\tt 1} if the matrix has
real entries, and {\tt 0} otherwise.
\item
{\tt DENSEMTX\_IS\_COMPLEX(mtx)} returns {\tt 1} if the matrix has
complex entries, and {\tt 0} otherwise.
\end{itemize}