\documentclass{beamer}

\usetheme{uhh}
\showtotalframenumber
\showuhhlogoeachframe
\showsections

\usepackage{amsmath}
\usepackage{graphicx}
\DeclareMathOperator*{\argmin}{arg\,min}

\usepackage{listings}
\lstset{
  language=python
  }

\title{Part 04: Implementing Word2Vec in Tensorflow}
\author{Fabian Barteld, Benjamin Milde}
\date[20.06.2016]{June 20, 2016}

\AtBeginSection[]
{
   %%%%% section title
   % This is how it would look like in Beamer:
   % \begin{frame}
   %     \frametitle{Overview}
   %     \tableofcontents[sections={2-3},currentsection,sectionstyle=show/hide,subsectionstyle=hide]
   % \end{frame}
  \begin{frame}[plain]
  \begin{tikzpicture}[overlay]
    \relax%
    \fill[blueuhh,opacity=1] (-10,-10)
    rectangle(\the\paperwidth,\the\paperheight);
  \end{tikzpicture}
   \begin{tikzpicture}[overlay]
    \relax%
    \fill[white,opacity=1] (-5,-1.2)
    rectangle(\the\paperwidth,0.5) node[pos=0.5,black]{\LARGE\insertsectionhead};
  \end{tikzpicture}
  \end{frame}

  %%%% add subsection to show navigation dots
  \subsection{}
}

\begin{document}

\maketitle

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

\section{Introduction}

\begin{frame}[fragile]
\frametitle{Training embeddings}
  \begin{itemize}
	\item We will now implement Word2Vec in Tensorflow
	\item (Slides smiliar to https://www.tensorflow.org/tutorials/word2vec)
	\includegraphics[width=0.7\textwidth]{04_skipgram_vs_cbow}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Main concepts I}
  \begin{itemize}

    \item  Neural probabilistic language models are traditionally trained using the maximum likelihood (ML) principle (where $w_t$ is the target word and $h$ is the context):

	 \end{itemize}

\begin{center}
\includegraphics[width=0.5\textwidth]{04_w2v_01}
\end{center}

\end{frame}

\begin{frame}
  \frametitle{Main concepts II}
  \begin{itemize}

    \item  Neural probabilistic language models are traditionally trained using the maximum likelihood (ML) principle (where $w_t$ is the target word and $h$ is the context):

	 \end{itemize}

$$P(w_t | h) = \text{softmax}(\text{score}(w_t, h)) = $$
	$$ \frac{\ exp\{\text{score}(w_t, h) \} } {\sum_\text{Word w' in Vocab} \ exp\{ \text{score}(w', h) \}}$$

\end{frame}

\begin{frame}
  \frametitle{Main concepts III}

    \begin{itemize}

	\item We train this model by maximizing its log-likelihood on the training set, i.e. by maximizing:

	$$  J_\text{ML} = \log P(w_t | h) \ = $$
	$$  \text{score}(w_t, h) - \log \left( \sum_\text{Word w' in Vocab} \exp { \text{score}(w', h) } \right). $$

	\item However this is very expensive, because we need to compute and normalize each probability using the score for all other $V$ words $w'$ in the current context $h$, at every training step.
	\end{itemize}

\end{frame}

\begin{frame}
  \frametitle{Main concepts IV - NCE}

    \begin{itemize}
    \item Noise Contrastive Estimation (NCE)
	\item For feature learning in word2vec we do not need a full probabilistic model. Instead, we train to discriminate the real target words $w_t$ from $k$ imaginary (noise) words w~:

	\end{itemize}

	\begin{center}
	\includegraphics[width=0.6\textwidth]{04_w2v_02}
	\end{center}

\end{frame}

\begin{frame}
  \frametitle{Main concepts V - NCE}

  \begin{itemize}
\item Mathematically, the objective is to maximize:

$$J_\text{NCE} = \log Q_\theta(D=1 |w_t, h) + k \mathop{\mathbb{E}}\limits_{\tilde w \sim P_\text{noise}} \left[ \log Q_\theta(D = 0 |\tilde w, h) \right]$$

\item discriminate the real target words \(w_t\) from \(k\) imaginary (noise) words \(\tilde w\)
\item where $Q_\theta(D=1 | w, h)$ is the binary logistic regression probability
\item under the model of seeing the word $w$ in the context $h$ and assigning the label 1 for datapoint $D$, calculated in terms of the learned embedding vectors $\theta$

\end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Impl. I - Tensorflow W2V}
    \begin{itemize}
    	\item In practice we approximate the expectation by drawing k contrastive words from the noise distribution (i.e. we compute a Monte Carlo average)
   $$J_\text{NCE} \approx \log Q_\theta(D=1 |w_t, h) +  \sum_{i=1, w \sim P_\text{noise}}^{k}  \left[ \log Q_\theta(D = 0 |\tilde w, h) \right]$$
   \item Now we can choose $k \neq |V|$, in practice 5-10 for small datasets, 2-5 for large datasets
   \item Negative sampling, as in the word2vec paper, is a variant of NCE and uses a specific distribution (uniform raised to the power of 3/4)

 \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Impl. II - Tensorflow W2V}
  \begin{lstlisting}

loss = tf.reduce_mean(
  tf.nn.nce_loss(weights=nce_weights,
                 biases=nce_biases,
                 labels=train_labels,
                 inputs=embed,
                 num_sampled=num_sampled,
                 num_classes=vocabulary_size))
    \end{lstlisting}

  \begin{itemize}
  	\item We can use the NCE loss op of Tensorflow to construct a variant of word2vec. Internally, nce\_weights also uses embedding\_lookup and does a form of negative sampling  directly in Tensorflow.
  \end{itemize}
\end{frame}

 \begin{frame}[fragile]
  \frametitle{Impl. III - Tensorflow W2V}
  The embeddings matrix is a variable that we want to optimize:
  \begin{lstlisting}
embeddings = tf.Variable(
tf.random_uniform([vocabulary_size,
embedding_size], -1.0, 1.0))
\end{lstlisting}
\end{frame}

 \begin{frame}[fragile]
 \frametitle{Impl. IIII - Tensorflow W2V}
We also need variables for the nce\_loss:

\begin{footnotesize}
\begin{lstlisting}
nce_weights = tf.Variable(
  tf.truncated_normal([vocabulary_size, embedding_size],
stddev=1.0 / math.sqrt(embedding_size)))
nce_biases = tf.Variable(tf.zeros([vocabulary_size]))
\end{lstlisting}
\end{footnotesize}
\end{frame}

 \begin{frame}[fragile]
  \frametitle{Impl. IV - embedding\_lookup:}

  \begin{footnotesize}
 \begin{lstlisting}
embed = tf.nn.embedding_lookup(embeddings, train_inputs)
\end{lstlisting}
\end{footnotesize}

e.g. If your list of sentences is: $\big[[0, 1], [0, 3]\big]$ (sentence 1 is $[0, 1]$, sentence 2 is $[0, 3]$, the function will compute a tensor of embeddings, which will be of shape $(2, 2, \text{embedding\_size})$ and will look like:
\\
$$[[\text{embedding0, embedding1}], [\text{embedding0, embedding3}]]$$

\end{frame}

 \begin{frame}[fragile]

  \frametitle{Exercise 1 - simple version}
   \begin{itemize}
   		\item Lets put it together: We can use tf.nn.embedding\_lookup for the input projection and tf.nn.nce\_loss for the loss (no other layers needed!).
		\item For simplicity, lets also implement CBOW and Skipgram with a window size of 1.
		\item E.g. for "the quick brown fox jumped over the lazy dog"
		\item (context, target) pairs: ([the, brown], quick), ([quick, fox], brown), ([brown, jumped], fox)
		\item We can simplify to: (the, quick), (brown, quick), (quick, brown), (fox, brown), ... \textbf{CBOW}
		\item or (quick, the), (quick, brown), (brown, quick), (brown, fox), ... \textbf{Skip-gram}
	\end{itemize}
\end{frame}

 \begin{frame}[fragile]

  \frametitle{Exercise 2 - advanced version}
   \begin{itemize}
		\item Lets try to make a version that does not use tf.nn.nce\_loss, as easy as that makes our lives!
		\item We can also do the negative sampling on the host and code up a linear regression as in the previous tutorials
		\item Host will assign labels (1 for true context pairs, 0 for noise pairs)
		\item You have to change the code in the get\_batch function and the inputs to your model and adapt your model accordingly
	\end{itemize}
\end{frame}

 \begin{frame}[fragile]

 \frametitle{Hints}
  \begin{itemize}
		\item Hint1: The negative samples need a second embedding matrix
		\item Hint2: For the loss, to get the logits, use the dot product between embedding pairs.
		\item Hint3: There is no tf.dot(), but you can combine tf.reduce\_sum(x,1) and tf.multiply(a,b).
		\item Hint4: Readable pure Python code with comments: , or if you're feeling masochistic the original uncommented word2vec C impl at:
	\end{itemize}

\end{frame}

 \begin{frame}[fragile]

 \frametitle{Tensorboard}
  \begin{itemize}
		\item Visualize loss, embeddings and much more in your browser
		\item You need to add a few lines of code to tell Tensorboard what to log
		\item Make sure train\_summary\_dir is a new directory for every new experiment!
	\end{itemize}

	\begin{tiny}
\begin{lstlisting}
 loss_summary = tf.summary.scalar('loss', loss)
 train_summary_op = tf.summary.merge_all()
 summary_writer = tf.summary.FileWriter(train_summary_dir, sess.graph)
\end{lstlisting}
\end{tiny}

\end{frame}

\begin{frame}[fragile]

 \frametitle{Tensorboard}
  \begin{itemize}
		\item You need to regularly call the train\_summary\_op in training
		\item Not as often as the training step, because it will otherwise slowdown your training if you have more complex summaries
	\end{itemize}

\begin{tiny}
\begin{lstlisting}
if current_step % 100==0 and current_step != 0:
	summary_str = sess.run(train_summary_op, feed_dict=feed_dict)
	summary_writer.add_summary(summary_str, current_step)
\end{lstlisting}
\end{tiny}

\end{frame}

\begin{frame}[fragile]
 \frametitle{Tensorboard - running it}
 \begin{tiny}
 \begin{lstlisting}
python3 -m tensorflow.tensorboard  --logdir=w2v_summaries_1499773534
--host=127.0.0.1
\end{lstlisting}
\end{tiny}
 \includegraphics[width=0.5\textwidth]{04_loss}
\end{frame}

\begin{frame}[fragile]
 \frametitle{Tensorboard - embeddings}
   \begin{itemize}
		\item Possible to nicely visualize embeddigs, see \url{https://www.tensorflow.org/get_started/embedding_viz}
		\item Also checkout \url{http://projector.tensorflow.org/}, live demo of pretrained embeddings
		\includegraphics[width=0.5\textwidth]{04_embed_viz.png}
	\end{itemize}
\end{frame}

%\begin{frame}[fragile]
%  \frametitle{Layer based APIs vs. Graphs based}
%
%Disadvantage: Difficult to express structures like these:
%
%    \includegraphics[angle=-90,width=0.75\textwidth]{graph_example}
%
%Increasing evidence that these kind of deeply connected networks are very useful.
%
%\end{frame}
%
%\begin{frame}[fragile]
%  \frametitle{Layer based APIs vs. Graphs based}
%  \begin{itemize}
%		\item  Since Tensorflow uses computation graphs, the declaration of the model allows for a higher expressivity
%		\item  Has a steeper learning curve in the beginning
%		\item  In the newer versions of tensorflow, you can also mix layer-like APIs with the computation graph
%		\item  We will focus on not using any short cuts, as this has a higher learning effect and only make use of standard ops in the beginning
%  \end{itemize}
%\end{frame}
%
%\begin{frame}[fragile]
%\frametitle{First steps - Lets open spyder}
%
%\includegraphics[width=1.0\textwidth]{spyder}
%
%\end{frame}
%
%\begin{frame}[fragile]
%\frametitle{First steps - Necessary imports}
%
%\begin{lstlisting}
%import numpy as np
%import tensorflow as tf
%\end{lstlisting}
%
%\begin{itemize}
% 	\item Outside of graph computations, we usually store data in Numpy arrays.
% 	\item Numpy arrays are the main objects to transfer data to inputs of the graph and from outputs of the graph.
% 	\item Numpy arrays are also an abstraction for (homogeneous) multidimensional arrays.
%\end{itemize}
%
%\end{frame}
%
%\begin{frame}[fragile]
%\frametitle{Generating some random data}
%
%\begin{lstlisting}
%#some random test data
%a_data = np.random.rand(256)
%b_data = np.random.rand(256)
%\end{lstlisting}
%
%\begin{itemize}
%	\item Now a and b contain vectors of length 256 with random floats. E.g. print(a\_data) returns:
%\end{itemize}
%
%\begin{lstlisting}
%[ 0.54976368  0.87790201  0.96528541 ...,
% 0.05281365  0.48556404  0.46848266]
%  \end{lstlisting}
%
%\end{frame}
%
%
%\begin{frame}[fragile]
%\frametitle{Declare the computation graph}
%
%\begin{lstlisting}
%#construct the graph
%a = tf.placeholder(tf.float32, [256])
%b = tf.placeholder(tf.float32, [256])
%
%x = a+b
%\end{lstlisting}
%
%\begin{itemize}
%\item The placeholders can later be used to input data to the computation graph
%\item The operation x = a+b does not immediatly add something, it creates a graph.
%\item In fact, print(x) returns:
%\end{itemize}
%
%\begin{lstlisting}
%Tensor("add:0", shape=(256,), dtype=float32)
%\end{lstlisting}
%
%\end{frame}
%
%\begin{frame}[fragile]
%\frametitle{A session on a computation device}
%
%\begin{lstlisting}
%with tf.device('/cpu'):
%    with tf.Session() as sess:
%       x_data = sess.run(x, {a: a_data, b: b_data})
%       print(x_data)
%\end{lstlisting}
%
%\begin{itemize}
%\item This fills the inputs a and b with a\_data and b\_data (our random data), runs the computation graph and retrieves the results of x in x\_data
%\item Obviously not terrible useful as is, but you could run the operation easily on a gpu by changing tf.device('/cpu') to  tf.device('/gpu:1'). Copying data to and from the GPU is handled automatically for you.
%\end{itemize}
%
%\end{frame}
%
%\begin{frame}[fragile]
%\frametitle{Small warm up exercise!}
%
%\begin{itemize}
%	\item We change a and b to random matrices:
%\end{itemize}
%
%\begin{lstlisting}
%a = np.random.rand(256, 128)
%b = np.random.rand(128, 512)
%\end{lstlisting}
%
%\begin{itemize}
%	\item Calculate the resulting matrix of shape (256, 512) in TensorFlow.
%\end{itemize}
%
%\end{frame}

%\section{Simple Optimization}
%
%% https://medium.com/@saxenarohan97/intro-to-tensorflow-solving-a-simple-regression-problem-e87b42fd4845
%% https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/2_BasicModels/linear_regression.py
%\begin{frame}
%  \frametitle{Linear Regression}
%
%  \begin{itemize}
%  \item Given: $(x_1, y_1)$, \ldots, $(x_n,y_n)$
%  \item Goal: find $w$ and $b$ such that:
%    \begin{displaymath}
%      \argmin_{w, b} \frac{\sum^n_{i=1} (\hat{y}_i - y_i)^2}{n}
%    \end{displaymath}
%    where $\hat{y}_i = wx_i + b$.
%  \end{itemize}
%
%\end{frame}
%
%\begin{frame}[fragile]
%  \frametitle{Define model parameters}
%  Model: $\hat{y}_i = wx_i + b$
%
%\begin{lstlisting}
%w = tf.Variable(np.random.randn(), name="weight")
%b = tf.Variable(np.random.randn(), name="bias")
%\end{lstlisting}
%\end{frame}
%
%\begin{frame}[fragile]
%  \frametitle{Define the model}
%
%  \begin{displaymath}
%    \begin{pmatrix} \hat{y}_1\\\vdots\\\hat{y}_n\end{pmatrix} =
%    \begin{pmatrix} w\\\vdots\\w\end{pmatrix} *
%    \begin{pmatrix} \hat{x}_1\\\vdots\\\hat{x}_n\end{pmatrix} +
%    \begin{pmatrix} b\\\vdots\\b\end{pmatrix}
%  \end{displaymath}
%
%\begin{lstlisting}
%yhat = tf.add(tf.multiply(X, w), b)
%\end{lstlisting}
%
%{\footnotesize The scalars $w$ and $b$ are converted into vectors of the same
%  length as X (broadcast); \url{https://www.tensorflow.org/performance/xla/broadcasting}}
%
%\end{frame}
%
%\begin{frame}[fragile]
%  \frametitle{Define the loss}
%
%\begin{lstlisting}
%loss = tf.reduce_mean(tf.square(y - yhat))
%\end{lstlisting}
%
%\end{frame}
%
%
%\begin{frame}[fragile]
%  \frametitle{Optimization}
%
%\begin{lstlisting}
%epochs = 10
%optimizer = tf.train.GradientDescentOptimizer(
%    learning_rate).minimize(loss)
%
%with tf.Session() as sess:
%    ## initalize parameters
%    sess.run(tf.global_variables_initializer())
%
%    for i in list(range(epochs)):
%        ## run one epoch
%        sess.run(optimizer)
%        ## print result and loss
%        print(sess.run(yhat) + ' ' + sess.run(loss))
%\end{lstlisting}
%
%\end{frame}
%
%\begin{frame}[fragile]
%  \frametitle{Hands on: Simple optimization}
%
%  \begin{enumerate}
%  \item Do a linear regression to learn $y = 2x + 1$
%  \item Do a multiple linear regression with Boston housing prices
%  \end{enumerate}
%
%\begin{lstlisting}
%from sklearn.datasets import load_boston
%from sklearn.preprocessing import scale
%
%total_X, total_Y = load_boston(True)
%total_x = scale(total_x)
%\end{lstlisting}
%\end{frame}
%
%% https://www.tensorflow.org/tutorials/wide
%% https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/2_BasicModels/logistic_regression.py
%\begin{frame}
%  \frametitle{Logistic regression}
%
%  TODO
%  tf.sigmoid
%
%\end{frame}
%
%\begin{frame}
%  \frametitle{Hands on: Regression model}
%
%  TODO: more complex example
%\end{frame}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-engine: luatex
%%% End:


% declare document class and geometry
\documentclass[12pt]{article} % use larger type; default would be 10pt
\usepackage[margin=1in]{geometry} % handle page geometry

\input{../header2.tex}

\title{Phys 220A -- Classical Mechanics -- Lec14}
\author{UCLA, Fall 2014}
\date{\formatdate{25}{11}{2014}} % Activate to display a given date or no date (if empty),
         % otherwise the current date is printed

\begin{document}
\setlength{\unitlength}{1mm}
\maketitle

\section{More on rigid bodies}

For an angular momentum $\v \omega$ we have kinetic energy
\begin{eqn}
T = \frac{1}{2} \sum_{ij} \omega_i I_{ij} \omega_j
\end{eqn}
and angular momentum
\begin{eqn}
L_i = \sum_j I_{ij} \omega_j.
\end{eqn}
[One or both of these are independent of frame?] Recall the Euler equations
\begin{eqn}
I \vd \omega = \v \omega \times (I \v \omega),
\end{eqn}
or in the frame of the principal axes
\begin{align}
I_1 \od{\omega_1}{t} &= (I_2 - I_3) \omega_2 \omega_3, \\
I_2 \od{\omega_2}{t} &= (I_3 - I_1) \omega_3 \omega_1, \\
I_3 \od{\omega_3}{t} &= (I_1 - I_2) \omega_1 \omega_2.
\end{align}

\subsection{Stability of solutions}

Suppose without loss of generality that $I_1 < I_2 < I_3$. Then we can write our solutions as
\begin{align}
\omega_1 &= \omega + \eta_1, \\
\omega_2 &= 0 + \eta_2, \\
\omega_3 &= 0 + \eta_3.
\end{align}
For some reason we have a 0th order case
\begin{eqn}
\od{\omega}{t} = 0
\qquad \implies \qquad
\omega = \text{const}
\end{eqn}
and a 1st order case
\begin{eqn}
\od{\eta_1}{t} = 0
\end{eqn}
in which case we can set $\eta_1 = 0$ (can be absorbed into $\omega$ [what?]) Then we can write ODEs for $\eta_2, \eta_3$,
\begin{eqn}
\od{\eta_2}{t} = J_2 \eta_3, \qquad
\od{\eta_3}{t} = J_3 \eta_2
\end{eqn}
where
\begin{eqn}
J_2 = \frac{I_3 - I_1}{I_2} \, \omega, \qquad
J_3 = \frac{I_1 - I_2}{I_3} \, \omega.
\end{eqn}
So in general we can write
\begin{eqn}
\pmat{\dot \eta_2 \\ \dot \eta_3} = \pmat{0 & J_2 \\ J_3 & 0} \pmat{\eta_2 \\ \eta_3},
\end{eqn}
which has solutions
\begin{eqn}
\eta_i(t) = e^{\lambda t} \eta_i(0), \quad i = 2,3
\end{eqn}
where $\lambda$ is determined by
\begin{eqn}
0 = \det \pmat{\lambda & -J_2 \\ -J_3 & \lambda} = \lambda^2 - J_2 J_3 \qquad
\implies \qquad
\lambda = \pm \sqrt{J_2 J_3}.
\end{eqn}
So we have
\begin{eqn}
\lambda =
\begin{cases}
\pm i \sqrt{\abs{J_2 J_3}}, & J_2 J_3 < 0 \\
\pm \sqrt{\abs{J_2 J_3}}, & J_2 J_3 > 0
\end{cases}
\end{eqn}
which is stable in the first case and unstable in the second case. In our case $I_1 < I_2 < I_3$ so $J_2 > 0$ and $J_3 < 0$ so it's stable. In other cases it can be unstable.

\subsection{Geometrical (Poinsot) Picture of Conserved Quantities}

[stuff about intersections of ellipsoids and spheres]

[something about why choice of Euler angles is nice]

\subsection{Example: Heavy symmetric top}

[missed last hour]

\end{document}


\documentclass[12pt]{cdblatex}
\usepackage{eqtns}

\begin{document}

\section*{PhysRevD.67.084023 equation (20)}

\begin{cadabra}
   from shared import *
   import cdblib

   jsonfile = 'momentum.json'
   cdblib.create (jsonfile)

   defG2GBar = cdblib.get ('defG2GBar','gamma.json')

   # --------------------------------------------------------------------------
   # Momentum constraint pt.1

   Mom := D_{j}{K^{i j} - g^{i j} trK}.                                       # cdb(Mom.101,Mom)

   defDgD := D_{a}{g_{b c}} -> 0.
   defDgU := D_{a}{g^{b c}} -> 0.

   defDtrK   := D_{a}{trK} -> \partial_{a}{trK}.
   defDexp   := D_{a}{\exp(-4\phi)} -> -4\exp(-4\phi) \partial_{a}{\phi}.

   distribute   (Mom)                                                         # cdb(Mom.102,Mom)
   product_rule (Mom)                                                         # cdb(Mom.103,Mom)
   substitute   (Mom, defDgU)                                                 # cdb(Mom.104,Mom)

   defK2ABarU := K^{i j} -> \exp(-4\phi) ABar^{i j} + (1/3) g^{i j} trK.

   substitute   (Mom, defK2ABarU)                                             # cdb(Mom.105,Mom)
   distribute   (Mom)                                                         # cdb(Mom.106,Mom)
   product_rule (Mom)                                                         # cdb(Mom.107,Mom)
   substitute   (Mom, defDtrK)                                                # cdb(Mom.108,Mom)
   substitute   (Mom, defDgU)                                                 # cdb(Mom.109,Mom)
   substitute   (Mom, defDexp)                                                # cdb(Mom.110,Mom)

\end{cadabra}

\clearpage

\begin{dgroup*}[spread=5pt]
   \begin{dmath*}
      {\cal D}^{j}
         = \Cdb*{Mom.101}
         = \Cdb*{Mom.102}
         = \Cdb*{Mom.103}
         = \Cdb*{Mom.104}
         = \Cdb*{Mom.105}
         = \Cdb*{Mom.106}
         = \Cdb*{Mom.107}
         = \Cdb*{Mom.108}
         = \Cdb*{Mom.109}
         = \Cdb*{Mom.110}
   \end{dmath*}
\end{dgroup*}

\clearpage

\begin{cadabra}
   # --------------------------------------------------------------------------
   # Momentum constraint pt.2

   confMom := \exp(4\phi) @(Mom).

   defG2GBarU := g^{i j} -> \exp(-4\phi) gBar^{i j}.

   distribute   (confMom)                                                     # cdb(confMom.101,confMom)
   substitute   (confMom, defG2GBarU)                                         # cdb(confMom.102,confMom)
   map_sympy    (confMom, "simplify")                                         # cdb(confMom.103,confMom)

   defDAabU  := D_{a}{ABar^{b c}} ->  \partial_{a}{ABar^{b c}}
                                    + \Gamma^{b}_{i a} ABar^{i c}
                                    + \Gamma^{c}_{i a} ABar^{b i}.

   substitute     (confMom, defDAabU)                                         # cdb(confMom.104,confMom)
   substitute     (confMom, defG2GBar)                                        # cdb(confMom.105,confMom)
   distribute     (confMom)                                                   # cdb(confMom.106,confMom)
   confMom = product_sort (confMom)                                           # cdb(confMom.107,confMom)
   rename_dummies (confMom)                                                   # cdb(confMom.108,confMom)
   canonicalise   (confMom)                                                   # cdb(confMom.109,confMom)
   substitute     (confMom, $gBar^{i}_{i} -> 3$)                              # cdb(confMom.110,confMom)
   substitute     (confMom, $gBar_{i j} ABar^{i j} -> 0$)                     # cdb(confMom.111,confMom)
   substitute     (confMom, $gBar_{a i} gBar^{i b} -> gBar_{a}^{b}$)          # cdb(confMom.112,confMom)
   substitute     (confMom, $GammaBar^{b}_{a b} -> 0$)                        # cdb(confMom.113,confMom) # follows from det gBar = 1
   eliminate_kronecker (confMom)                                              # cdb(confMom.114,confMom)
   rename_dummies (confMom)
   canonicalise   (confMom)                                                   # cdb(confMom.115,confMom)

   cdblib.put ('confMom',confMom,jsonfile)
\end{cadabra}

\clearpage

\begin{dgroup*}[spread=5pt]
   \begin{dmath*}
      \exp(4\phi) {\cal D}^{j}
         = \Cdb*{confMom.101}
         = \Cdb*{confMom.102}
         = \Cdb*{confMom.103}
         = \Cdb*{confMom.104}
         = \Cdb*[\hskip3.0cm\hfill]{confMom.105}
         = \Cdb*[\hskip2.0cm\hfill]{confMom.106}
         = \Cdb*[\hskip2.0cm\hfill]{confMom.107}
         = \Cdb*[\hskip2.0cm\hfill]{confMom.108}
         = \Cdb*[\hskip2.5cm\hfill]{confMom.109}
         = \Cdb*{confMom.110}
         = \Cdb*{confMom.111}
   \end{dmath*}
\end{dgroup*}

\clearpage

\begin{dgroup*}[spread=5pt]
   \begin{dmath*}
      \exp(4\phi) {\cal D}^{j}
         = \Cdb*{confMom.112}
         = \Cdb*{confMom.113}
         = \Cdb*{confMom.114}
         = \Cdb*{confMom.115}
   \end{dmath*}
\end{dgroup*}

\clearpage

\begin{cadabra}
   tmpA := @(confMom).                                              # cdb(confMom.201,tmpA)
   tmpB := @(confMom).

   X^{b c}_{a}::Weight(label=numX).

   Xbca := \partial_{a}{ABar^{b c}}.                                # cdb(confMom.202,Xbca)

   foo := \partial_{a}{ABar^{b c}} -> X^{b c}_{a}.
   bah := X^{b c}_{a} -> \partial_{a}{ABar^{b c}}.

   substitute  (tmpA, foo)                                          # cdb(confMom.203,tmpA)
   substitute  (tmpB, foo)                                          # cdb(confMom.204,tmpB)
   drop_weight (tmpA, $numX=1$)                                     # cdb(confMom.205,tmpA)
   keep_weight (tmpB, $numX=1$)                                     # cdb(confMom.206,tmpB)
   substitute  (tmpB, bah)                                          # cdb(confMom.207,tmpB)

   tmpC := - @(tmpA).                                               # cdb(confMom.208,tmpC)

   defMomSub := @(tmpB) -> @(tmpC).                                 # cdb(confMom.209,defMomSub)

   cdblib.put ('defMomSub',defMomSub,jsonfile)
\end{cadabra}

\clearpage

\begin{dgroup*}
   \begin{dmath*} 0 =  \Cdb*{confMom.201} \end{dmath*}
   \begin{dmath*} 0 =  \Cdb*{confMom.203} \end{dmath*}
   \begin{dmath*}
      \cdb{confMom.202} = \Cdb*{confMom.206}
                        = \Cdb*{confMom.208}
   \end{dmath*}
   \begin{dmath*} \Cdb*{confMom.209} \end{dmath*}
\end{dgroup*}

\clearpage

\begin{cadabra}
   # --------------------------------------------------------------------------
   # Check against prd67.

   foo := @(confMom).                                   # cdb(prd67.eq20.lcb,foo)
   bah  = cdblib.get('prd67.eq20.rhs','prd67.json')     # cdb(prd67.eq20.prd,bah)

   diff := @(foo) - @(bah).

   distribute     (diff)
   diff = product_sort (diff)
   rename_dummies (diff)
   map_sympy      (diff, "simplify")
   canonicalise   (diff)                                # cdb(prd67.eq20.chk,diff)
\end{cadabra}

% \clearpage

\begin{dgroup*}
   \begin{dmath*} \cdb*{prd67.eq20.lcb} \end{dmath*}
   \begin{dmath*} \cdb*{prd67.eq20.prd} \end{dmath*}
   \begin{dmath*} \cdb*{prd67.eq20.chk} \end{dmath*}
\end{dgroup*}

\end{document}


% !TeX spellcheck = en_US
\chapter{Classes and Objects}
\section{Constructors}

It is an example of \textbf{divide and conquer} strategy.

\section{Quick sort}
The quicksort algorithm has a worst-case running time of $\Theta(n^{2})$ on an input array of n numbers.

Applies \textbf{divide and conquer} strategy.

\section{Heap sort}


\section{Broadcast time}\label{sec:startnodetime}

Files \code{\{low,high\}-density-time.ipynb} contain the analysis for the
broadcast time needed to reach the 98th percentile of the coverage. The analysis
has been done fixing all the factors (also \(R\)) and by just varying the
position of the starting user. Configurations used are
``StartNodePositionLowDensityTime'' and ``StartNodePositionHighDensityTime''.

We get the results shown in \tableref{table:startnodetimeresults}.

\begin{table}[hbt]
	\centering
	\begin{tabular}{lcccc}
		\multicolumn{5}{c}{Low density (98th percentile broadcast time)}\\
		\toprule
		Start Node Pos\@. & Mean & Std\@. Dev\@. & Min\@. & Max\@. \\
		\midrule
		Center & \(35.166667s\) & \(1.533158s\) & \(31s\) & \(40s\) \\[16pt]
		Border & \(53.7s\) & \(1.914554s\) & \(50s\) & \(58s\) \\[16pt]
		Corner & \(65.1s\) & \(2.186952s\) & \(61s\) & \(70s\) \\
		\bottomrule
	\end{tabular}\\[16pt]
	\begin{tabular}{lcccc}
		\multicolumn{5}{c}{High density (98th percentile broadcast time)}\\
		\toprule
		Start Node Pos\@. & Mean & Std\@. Dev\@. & Min\@. & Max\@. \\
		\midrule
		Center & \(32.533333s\) & \(2.161311s\) & \(30s\) & \(39s\) \\[16pt]
		Border & \(48.666667s\) & \(2.039833s\) & \(45s\) & \(53s\) \\[16pt]
		Corner & \(57.7s\) & \(1.803254s\) & \(55s\) & \(62s\) \\
		\bottomrule
	\end{tabular}
	\caption{With the starting node at the border or the corner, we get an
	higher broadcast time compared to the case where the starting node is at
	the center}\label{table:startnodetimeresults}
\end{table}

As we can see, as the starting node moves near to the border/corner, we get
higher values for both the mean broadcast time. This can be explained by the
fact that, when the starting node is in the corner, the message need to traverse
the entire length of the floorplan in both its dimensions.  When, instead, it
starts from the center, since it is sent out radially by the user, the message
just needs to cover half of the side of the floorplan in each dimension. The
standard deviation is higher in the corner for the low density scenario but it
is lower in the high density scenario: a higher number of users leads to a lower
variance.

So, if possibile, also for the broadcast time, is better to set the starting
node near the center or, at least, avoid borders and corners of the floorplan.


\section{Traps}
It is inevitable that as adventurers explore castle ruins and age-old dungeons, they will run into traps the villains have set for them. These traps will vary from mildly annoying to outright deadly. So how will adventurers avoid them?

The answer is simple: by a game mechanic. By default, characters cannot detect any traps other than the most obvious ones \Parentheses{interpretation up to the GM/DM}, and cannot disarm any traps, meaning that once they detected a trap in the first place, their only course of action is to try to walk around them and not step into them, if possible. But with a certain feat - called \SoCalled{Security} in the main archmodule - not only their ability to detect traps improve, but they also gain the ability to disarm traps.

When disarming traps, a d20 dice \Parentheses{or the sum of four d5 dices, or five d4 dices} must be rolled - or alternatively, for videogames, a random number between 1and 20 must be generated. When the received number is smaller than the character's dexterity, the disarming is a success, and the character can put the disarmed trap into their inventory if they want, potentially rearming it at will. If the disarming attempt is a failure, the trap activates and does its damage to the one who attempted to disarm it.

Naturally, at DM discretion, constant numbers can be added to or detracted from the aforementioned randomly generated number - or it can be even multiplied or divided, to simulate trap that are harder or easier to disarm.


\chapter{NAND, NOR gates}
%\ref{sec:background}.

\section{Aim}
%\label{sec:objectives}
	To verify and interpret the logic and truth table for NAND, NOR gates using Resistor Transistor Logic (RTL)

\section{Apparatus}
%\label{sec:objectives}
	\begin{itemize}
		\tightlist
		\item Kit for realization of gates
		\item Connecting Leads
	\end{itemize}

\section{Circuits}

\section{Theory}
	Logic gates are the basic building blocks of any digital system. Logic gates are electronic circuits having one or more than one input and only one output. The relationship between the input and the output is based on a certain logic. Based on this, logic gates are named as:
	\begin{enumerate}
		\tightlist
		\item AND gate
		\item OR gate
		\item NOT gate
		\item NAND gate
		\item NOT gate
		\item Ex-OR gate
		\item Ex-NOR gate
	\end{enumerate}

	\subsection{NAND gate}
	This is a NOT-AND gate which is equal to an AND gate followed by a NOT gate. The outputs of all NAND gates are high if any of the inputs are low. The symbol is an AND gate with a small circle on the output. The small circle represents inversion.
	\begin{align*}
		Y &= \overline{A . B}
	\end{align*}
	A simple 2-input logic NAND gate can be constructed using RTL (Resistor-transistor-logic) switches connected together as shown in Figure \ref{fig:nand_circuit} with the inputs connected directly to the transistor bases. Either transistor must be cut-off or “OFF” for an output at Q.
	\begin{figure}[ht]
		\centering
		\subfloat[Symbol]
		{
			\begin{circuitikz} \draw
				(0,0) node[nand port] (myand1) {}
				(myand1.in 1) node[anchor=east] {A}
				(myand1.in 2) node[anchor=east] {B}
				(myand1.out) node[anchor=west] {Y}
				;
			\end{circuitikz}
			\label{fig:nand_symbol}
		}
		\hfill
		\subfloat[Truth Table]
		{
			\begin{tabular}{|c|c|c|}
				\hline
				\multicolumn{2}{|c|}{Input} & Output \\
				\hline
				$A$ & $B$ & $Y=\overline{A.B}$ \\
				\hline
				0 & 0 & 1 \\
				\hline
				0 & 1 & 1 \\
				\hline
				1 & 0 & 1 \\
				\hline
				1 & 1 & 0 \\
				\hline
			\end{tabular}
			\label{fig:nand_table}
		}
		\hfill
		\subfloat[RTL Design]{
			\includegraphics[width=0.25\textwidth,valign=c]{img/exp2/fig1}
			\label{fig:nand_circuit}
		}
		\caption{\textit{NAND gate}}
	\end{figure}

	\subsection{NOR gate}
		This is a NOT-OR gate which is equal to an OR gate followed by a NOT gate. The outputs of all NOR gates are low if any of the inputs are high. The symbol is an OR gate with a small circle on the output. The small circle represents inversion.
		\begin{align*}
			Y &= \overline{A + B}
		\end{align*}
		A simple 2-input logic NOR gate can be constructed using RTL (Resistor-transistor-logic) switches connected together as shown in Figure \ref{fig:nor_circuit} with the inputs connected directly to the transistor bases. Both transistors must be cut-off or “OFF” for an output at Q.
		\begin{figure}[ht]
			\centering
			\subfloat[Symbol]
			{
				\begin{circuitikz} \draw
					(0,0) node[nor port] (myand1) {}
					(myand1.in 1) node[anchor=east] {A}
					(myand1.in 2) node[anchor=east] {B}
					(myand1.out) node[anchor=west] {Y}
					;
				\end{circuitikz}
				\label{fig:nor_symbol}
			}
			\hfill
			\subfloat[Truth Table]
			{
				\begin{tabular}{|c|c|c|}
					\hline
					\multicolumn{2}{|c|}{Input} & Output \\
					\hline
					$A$ & $B$ & $Y=\overline{A+B}$ \\
					\hline
					0 & 0 & 1 \\
					\hline
					0 & 1 & 0 \\
					\hline
					1 & 0 & 0 \\
					\hline
					1 & 1 & 0 \\
					\hline
				\end{tabular}
				\label{fig:nor_table}
			}
			\hfill
			\subfloat[RTL Design]{
				\includegraphics[width=0.25\textwidth,valign=c]{img/exp2/fig2}
				\label{fig:nor_circuit}
			}
			\caption{\textit{NOR gate}}
		\end{figure}

\section{Procedure}
	\subsection{NAND gate}
		\begin{figure}[ht]
			\centering
			\subfloat[Simulator 1]{\includegraphics[width=0.45\textwidth,valign=c]{img/exp2/fig3}
				\label{fig:nand_sim:1}}
			\hfill
			\subfloat[Simulator 2]{\includegraphics[width=0.4\textwidth,valign=c]{img/exp2/fig4}
				\label{fig:nand_sim:2}}
			\caption{\textit{Simulator for realizing circuit for NAND gate}}
		\end{figure}
		\subsubsection{Simulator 1}
			\begin{enumerate}
				\tightlist
				\item Connect the supply(+5V) to the circuit.
				\item Press the switches for inputs "A" and "B".
				\item The bulb glows if any one or both the switches are OFF else it won't glow.
				\item Repeat step-2 and step-3 for all state of inputs.
			\end{enumerate}
		\subsubsection{Simulator 2}
			\begin{enumerate}
				\tightlist
				\item Enter the Boolean input "A" and "B".
				\item Enter the Boolean output for your corresponding inputs.
				\item Click on "Check" Button to verify your output.
			\end{enumerate}

	\subsection{NOR gate}
		\begin{figure}[ht]
			\centering
			\subfloat[Simulator 1]{\includegraphics[width=0.45\textwidth,valign=c]{img/exp2/fig5}
				\label{fig:nor_sim:1}}
			\hfill
			\subfloat[Simulator 2]{\includegraphics[width=0.4\textwidth,valign=c]{img/exp2/fig6}
				\label{fig:nor_sim:2}}
			\caption{\textit{Simulator for realizing circuit for NOR gate}}
		\end{figure}
		\subsubsection{Simulator 1}
			\begin{enumerate}
				\tightlist
				\item Connect the supply(+5V) to the circuit.
				\item Press the switches for inputs "A" and "B".
				\item The bulb glows if both the switches are OFF else it won't glow.
				\item Repeat step-2 and step-3 for all state of inputs.
			\end{enumerate}
		\subsubsection{Simulator 2}
			\begin{enumerate}
				\tightlist
				\item Enter the Boolean input "A" and "B".
				\item Enter the Boolean output for your corresponding inputs.
				\item Click on "Check" Button to verify your output.
			\end{enumerate}

\section{Observations}
	\subsection{NAND gate}
			\begin{figure}[ht]
				\centering
				\subfloat[Either of the Inputs OFF, LED is ON]{\includegraphics[width=0.45\textwidth,valign=c]{img/exp2/fig7}
					\label{fig:nand_obs:1}}
				\hfill
				\subfloat[Both Inputs ON, LED is OFF]{\includegraphics[width=0.45\textwidth,valign=c]{img/exp2/fig8}
					\label{fig:nand_obs:2}}
				\caption{\textit{Observations for different Input Values}}
			\end{figure}
			\begin{figure}[h]
				\centering
				\includegraphics[width=0.85\linewidth]{img/exp2/fig9}
				\caption{\textit{Observations for verification of Truth Table of the NAND gate}}
				\label{fig:nand_obs_2}
			\end{figure}

\pagebreak
	\subsection{NOR gate}
			\begin{figure}[ht]
				\centering
				\subfloat[Either of the Inputs ON, LED is OFF]{\includegraphics[width=0.45\textwidth,valign=c]{img/exp2/fig10}
					\label{fig:nor_obs:1}}
				\hfill
				\subfloat[Both Inputs OFF, LED is ON]{\includegraphics[width=0.45\textwidth,valign=c]{img/exp2/fig11}
					\label{fig:nor_obs:2}}
				\caption{\textit{Observations for different Input Values}}
			\end{figure}
			\begin{figure}[h]
				\centering
				\includegraphics[width=0.85\linewidth]{img/exp2/fig12}
				\caption{\textit{Observations for verification of Truth Table of the NOR gate}}
				\label{fig:nor_obs_2}
			\end{figure}

\section{Precautions}
	\begin{enumerate}
		\tightlist
		\item Make the connections when power supply is OFF.
		\item Ensure that the connections are tight.
		\item Change the status of inputs only when power supply is OFF.
	\end{enumerate}