\documentclass[10pt, a4paper]{report}

\usepackage{nonotation}

%%%%%%%%%%%%%%%%%%%%%%%%% FONTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{bold-extra} % Used for boldface-smallcaps combo in section headers
\usepackage[normalem]{ulem} % For strikethrough text

%%%%%%%%%%%%%%%%%%%%%%%%% HYPERLINKS %%%%%%%%%%%%%%%%%%%%%%%%%

\PassOptionsToPackage{hyphens}{url}
\usepackage{hyperref}

\hypersetup{
    colorlinks,
    linkcolor       = {red!70!black},
    citecolor       = {blue!50!black},
    urlcolor        = {blue!80!black}
}

% Hyperlink dingbat
\usepackage{pifont}
\newcommand{\linkicon}{\ding{226}}

%%%%%%%%%%%%%%%%%%%%%%%%% TIKZ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{tikz}
\usetikzlibrary{arrows.meta}

% Tikz styles used throughout all the figures
% Nodes
\tikzstyle{box}     = [draw, minimum size = 2em]
\tikzstyle{nobox}   = [minimum size = 2em]
% Pins
\tikzstyle{init}    = [pin edge = {-to, thin, black}]

%%%%%%%%%%%%%%%%%%%%%%%%% THEOREMS %%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{amsthm}

\newtheorem{theorem}                    {Theorem}       [chapter]
\newtheorem{lemma}          [theorem]   {Lemma}
\newtheorem{corollary}      [theorem]   {Corollary}
\newtheorem{claim}          [theorem]   {Claim}
\newtheorem{observation}    [theorem]   {Observation}
\newtheorem{proposition}    [theorem]   {Proposition}
\theoremstyle{definition}
\newtheorem{definition}     [theorem]   {Definition}
\newtheorem{exercise}       [theorem]   {Exercise}
\newtheorem{example}        [theorem]   {Example}
\newtheorem{construction}               {Construction}
\theoremstyle{remark}
\newtheorem{solution}       [theorem]   {Solution}

%%%%%%%%%%%%%%%%%%%%%%%%% TODOS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{xcolor}
\usepackage{framed}

\newcounter{todocount}
\definecolor{shadecolor}{rgb}{1, 1, 0}
\newcommand{\todo}[1]{
    \stepcounter{todocount}
    \begin{shaded}
    \textsc{To-do} \arabic{todocount}: #1
    \end{shaded}
}

%%%%%%%%%%%%%%%%%%%%%%%%% OTHER %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Math?lap commands
\usepackage{mathtools}

% Custom chapter title
\newcommand{\mychapter}[2]{
    \setcounter{chapter}{#1}
    \setcounter{section}{0}
    \chapter*{#2}
    \addcontentsline{toc}{chapter}{#2}
}

\newcommand{\upto}{\text{---}} % Range ligature
\renewcommand{\mod}{\mathrel{\textup{mod}}} % Remainder operator
\newcommand{\groupgen}{\mathcal{GG}} % Group generators for standard assumptions
\newcommand{\quadres}{\mathcal{Q}uad} % Quadratic residue group
\newcommand{\keygen}{\textsf{\textup{Keygen}}} % Key generator for public-privte pairs
\newcommand{\C}{\mathcal{C}} % Cipherspace
\newcommand{\G}{\mathcal{G}} % Generally used for generators or groups
\newcommand{\K}{\mathcal{K}} % Keyspace
\newcommand{\M}{\mathcal{M}} % Messagespace
\newcommand{\T}{\mathcal{T}} % Tagspace
\newcommand{\RO}{\mathrm{RO}} % Random oracle
\newcommand{\Gen}{\mathrm{Gen}} % Generic generator
\newcommand{\otp}{\textsc{otp}} % One-Time Pad

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

    \tableofcontents

    \chapter{Introduction}

    Newcomers beware: this course will employ a great deal of math, especially:
    \begin{itemize}
        \item Probability;
        \item Algebra, especially group theory;
        \item Notions of complexity theory and asymptotic analysis;
        \item Some unique mathematical constructs, such as \emph{cryptographic games}
    \end{itemize}

    \subsubsection{Some terminology}

    % Some people might have heard terms similar to ``cryptography'', like ``cryptology'', or ``cryptanalysis''. While they are often deemed interchangeable, there exists indeed a differentiation between the three:

    % \begin{itemize}
    %     \item \emph{Cryptography} denotes the study and betterment of encryption techniques;
    %     \item \emph{Cryptanalysis} denotes the study of encrypted messages, and the ways of breaking the encryption to get to the message;
    %     \item \emph{Cryptology} refers collectively to cryptography and cryptanalisis.
    % \end{itemize}

    % This course has the intended main purpose of fostering better encryption techniques, hence its name.

    While cryptography is almost always associated with the idea of keeping something secret, it is worthwhile to carefully consider other aspects that it influences:

    \begin{itemize}
        \item \emph{Confidentiality}: the two parties using a form of encrypted communication can safely assume that they are communicating privately; this can be further split into:
              \begin{itemize}
                  \item \emph{Secrecy}: the particular aspect of privacy;
                  \item \emph{Authentication}: the particular aspect of identity verification.
              \end{itemize}

        \item \emph{Integrity}: this is about having a guarantee that data is not altered, a property which becomes essential especially in safety-critical scenarios.
    \end{itemize}

    \subsubsection{Principles}

    Modern cryptography systems are usually designed according to \emph{Kerckhoffs's principle}, which states that a secure system  shall only rely on the encryption keys, and not by the secrecy of the underlying algorithm; as Claude Shannon later summed up: \emph{``the enemy knows the scheme''}. The problem of sharing the key between two parties while retaining communication confidentiality thus becomes central in developing a good scheme, and is the main focus of almost every scheme described during the course.

    \part{Mathematical foundations}

    \input{lessons/lesson_1.tex}
    \input{lessons/lesson_2.tex}
    \input{lessons/lesson_3.tex}
    \input{lessons/lesson_4.tex}
    \input{lessons/lesson_5.tex}

    \part{Symmetric schemes}

    \input{lessons/lesson_6.tex}
    \input{lessons/lesson_7.tex}
    \input{lessons/lesson_8.tex}
    \input{lessons/lesson_9.tex}
    \input{lessons/lesson_10.tex}
    \input{lessons/lesson_11.tex}
    \input{lessons/lesson_12.tex}

    \part{Asymmetric schemes}

    \input{lessons/lesson_13.tex}
    \input{lessons/lesson_14.tex}
    \input{lessons/lesson_15.tex}
    \input{lessons/lesson_16.tex}
    \input{lessons/lesson_17.tex}
    \input{lessons/lesson_18.tex}
    \input{lessons/lesson_19.tex}

    \part{Proof-based schemes}

    \input{lessons/lesson_20.tex}
    \input{lessons/lesson_21.tex}
    \input{lessons/lesson_22.tex}
    \input{lessons/lesson_23.tex}
    \input{lessons/lesson_24.tex}
\end{document}


\documentclass[11pt]{article}

\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{multicol}
\usepackage{graphicx}
\usepackage[top=0.8in, bottom=0.8in, left=0.8in, right=0.8in]{geometry}
% Add other packages here %
\usepackage[font=small,labelfont=bf]{caption} % Required for specifying captions to tables and figures

% Put your group number and names in the author field %
\title{\bf Exercise 1.\\ Implementing a first Application in RePast: A Rabbits Grass Simulation.}
\author{Group \textnumero 70: Jelena Banjac, Stratos Triantafyllou}

\begin{document}
\maketitle

\section{Implementation}

This project explores a simple ecosystem made up of rabbits and grass. The rabbits wander around randomly, and the grass grows randomly. When a rabbit bumps into some grass, it eats the grass and gains energy. If the rabbit gains enough energy, it reproduces. If it runs out of energy, it dies.

The grass can be adjusted to grow at different rates and give the rabbits differing amounts of energy. In addition, the reproduction of the rabbits can be adjusted to have different birth thresholds of energy. The model can be used to explore the competitive advantages of these variables, which we will do in this report.

The code contains JavaDocs. Therefore, all following assumptions and implementation remarks can be found in the corresponding parts of the code.

\begin{multicols}{2}
Variables we can change are:
\begin{itemize}
\item \textbf{BirthThreshold} - The amount of energy that a rabbit must reach before reproducing.
\item \textbf{GrassGrowthRate} - The amount of grass that grows at each simulation step.
\item \textbf{GridSize} - The size of the simulation world.
\item \textbf{NumInitGrass} - Number of initial grass cells.
\item \textbf{NumInitRabbits} - Number of initial rabbits.
\item \textbf{RabbitEnergyInit} - Initial energy of a rabbit.
\end{itemize}

\includegraphics[scale=0.25]{grid.png}

\end{multicols}

\subsection{Assumptions}
% Describe the assumptions of your world model and implementation (e.g. is the grass amount bounded in each cell) %
The assumptions of our world model and implementation:
\begin{itemize}
\item The rabbits do not communicate with each other.
\item A rabbit loses 1 point of energy as it moves to the neighbouring cell.
\item A rabbit can stay in the same cell, but it will still lose the 1 point of the energy.
\item 1 unit of grass corresponds to 1 point of energy.
\item On the first tick of the simulation, it can happen that rabbit already has a grass in his cell.
\item When rabbit reproduces, it loses the amount of energy that is specified as initial rabbit energy, that way they cannot reproduce twice during one simulation step.
\item The rabbit does not have parents (i.e. the new rabbit is created from one rabbit only).
\item A newborn rabbit has the same initial energy the same as all rabbits in the beginning of the simulation.
\item We introduce new constraint which describes the maximum amount of the grass in one cell. We did it since the plots didn't look very stable otherwise.
\end{itemize}

\subsection{Implementation Remarks}
% Provide important details about your implementation, such as handling of boundary conditions %
Important details of our implementation are:
\begin{itemize}
\item Assessing whether an adjacent cell is unoccupied so that a rabbit can move onto it, is done according the rabbit's position in the rabbit list. Say that, at a certain simulation step, rabbit B stands in a cell adjacent to the rabbit A. If rabbit B is before rabbit A in the rabbit list, it will move first and the cell will be made available to rabbit A. Otherwise, rabbit A will not be able to move into the cell.
\item Rabbits are represented with a graphical image, otherwise (i.e. image was not found) the rabbit will be represented as a blue rectangle.
\item The strength of grass color represents the quantity of energy it gives (i.e. the darker the color, the higher is the amount of energy the rabbit will receive).
\item Sliders are implemented for all the parameters that are in our interest of testing their influence on the population.
\end{itemize}

\section{Results}
% In this section, you study and describe how different variables (e.g. birth threshold, grass growth rate etc.) or combinations of variables influence the results. Different experiments with diffrent settings are described below with your observations and analysis
It is understandable that the simulation model used in this assignment far from depicts a real-world ecosystem, as mirrored in the assumptions that define it. For instance, a rabbit is considered blind and randomly moving, which means that it might move into a bare cell even thought it might be next to a cell that does contain grass. These assumptions are important in interpreting the results of the following experiments, which, by real-world standards, might look strange.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Scenario 1: Equilibrium}
Most possible parameterizations would result to an equilibrium state; there would be a point in time after which both the amount of grass and the number of rabbits would remain relatively stable. This is mainly related to the grass growth rate, which can be associated with a certain number of surviving rabbits. For example, we can set the growth rate for grass to 200, and number of rabbits at 50.

\begin{multicols}{2}
\subsubsection{Setting}
\textbf{BirthThreshold} = 19 \newline
\textbf{GrassGrowthRate} = 200 \newline
\textbf{GridSize} = 20 \newline
\newline
\textbf{NumInitGrass} = 1000 \newline
\textbf{NumInitRabbits} = 50  \newline
\textbf{RabbitEnergyInit} = 10 \newline
\end{multicols}

\begin{multicols}{2}
\subsubsection{Observations}
% Elaborate on the observed results %

The result of the simulation demonstrates how a growth rate of 200 is able to sustain the rabbit population at approximately 50. Subsequent runs for other values of the initial grass present on the world show that this equilibrium state for the rabbit population is achievable even if we start the simulation with much less grass present.

\includegraphics[scale=0.12]{run2_equilibrium.png}
\captionof{figure}{Amount of grass and number of rabbits in the scenario 1}

\end{multicols}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Scenario 2: Equilibrium, a cyclical perspective}
Having a large number of rabbits in the simulation world creates competition for the available grass. What if that competition did not exist from the beginning? In this scenario, we start the simulation with a single rabbit.

\begin{multicols}{2}

\subsubsection{Setting}
\textbf{BirthThreshold} = 19 \newline
\textbf{GrassGrowthRate} = 25 \newline
\textbf{GridSize} = 20 \newline
\textbf{NumInitGrass} = 150 \newline
\textbf{NumInitRabbits} = 1  \newline
\textbf{RabbitEnergyInit} = 10 \newline

\subsubsection{Observations}
% Elaborate on the observed results %
\includegraphics[scale=0.12]{exp3_updown.png}
\captionof{figure}{Amount of grass and number of rabbits in the scenario 2}

\end{multicols}

At a grass growth rate of 25, we observe some kind of cyclical pattern. The rabbit population starts at 1, therefore the amount of grass rises quickly. However, when there is enough grass available, the rabbit reproduces. As the population of rabbits rises to 4, the amount of grass grows less, until the available cannot sustain the population. After a while, we are back at 1, and the cycle starts all over.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Scenario 3: Overpopulation}
Another experimental attempt would be to test what happens when resources grow too slowly to be able to sustain the population. We opt for a minimum grass growth rate of 1, and a relatively large number of rabbits at 150. To make sure the simulation is not influenced by lack of initial grass, we set it to 1000.

\begin{multicols}{2}

\subsubsection{Setting}
\textbf{BirthThreshold} = 19 \newline
\textbf{GrassGrowthRate} = 1 \newline
\textbf{GridSize} = 20 \newline
\textbf{NumInitGrass} = 1000 \newline
\textbf{NumInitRabbits} = 150  \newline
\textbf{RabbitEnergyInit} = 10 \newline

\subsubsection{Observations}
% Elaborate on the observed results %
\includegraphics[scale=0.12]{run1_rabbits_extinct.png}
\captionof{figure}{Amount of grass and number of rabbits in the scenario 3}

\end{multicols}

We can see that the number of rabbits drops significantly from the beginning, since the rabbits consume a large proportion of the grass, which does not grow back in time to sustain the population. Rabbits eventually go extinct and grass starts growing indefinitely, since it is not consumed.

\end{document}


\documentclass{beamer}

%Include Packages
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[normalem]{ulem}
\usepackage{graphicx}
\usetheme{Warsaw}
\logo{\includegraphics[height=0.8cm]{logo}}

%Top Matter
\title[Monte Carlo Study of 3d Heisenberg Model]{A Monte Carlo Study of the Classical, Isotropic, 3D Heisenberg Model}
\subtitle{Numerical Studies of Stochastic Spin Systems}
\author[Michael Conroy]{Michael Conroy\\
  PHY 471 Capstone Project \\
  Spring 2014 \\
  Professor: Dr. Matthew Enjalran}
\date{May 2, 2014}

\begin{document}
	%Title Frame
	\begin{frame}
	%\frametitle{}
	\titlepage
	\end{frame}

	%Table of Contents Frame
	\begin{frame}
		\frametitle{Table of Contents}
		\tableofcontents
	\end{frame}

	\section{Introduction}

	\subsection{Goal}
  \begin{frame}
    \frametitle{Goal and Purpose}
    	\begin{itemize}
    		\item Simulate the classical, isotropic, 3D Heisenberg Model on the simple cubic lattice
    		\item Utilize the Monte Carlo method with the Metropolis Algorithm
    		\item Compare simulation data to literature data
    		\item Explore numerical analysis approach to the simulation
    	\end{itemize}
  \end{frame}

  \subsection{Brief Overview}
  \subsubsection*{Heisenberg Model}
  \begin{frame}
    \frametitle{Heisenberg Model}
    	\begin{columns}[t]
    	\begin{column}[T]{5cm}
    		\begin{itemize}
    			\item Continuous spin model
    			\item Hamiltonian: \\
    						$\displaystyle H = -J\sum_{<ij>}^{N}\vec{S_i}\cdot\vec{S_j}$, \\ where $\displaystyle |\vec{S_i}| = 1$.
    			\item Applications
    		\end{itemize}
    	\end{column}
    	\begin{column}[T]{6cm}
    		\includegraphics[scale=1.5]{spherical_unit_vector} \includegraphics[scale=0.1]{sc} \\
    		\includegraphics[scale=0.4]{lattice}
    	\end{column}
    	\end{columns}
  \end{frame}
  \subsubsection*{Magnetism and Magnetism in Statistical Mechanics}
  \begin{frame}
    \frametitle{Magnetism and Magnetism in Statistical Mechanics}
    	\begin{itemize}
    		\item Magnetic Domains \\
    					\includegraphics[scale=0.5]{domains}
    		\item Paramagnetism, ferromagnetism, and antiferromagnetism \\
    					\includegraphics[scale=0.3]{ferro_para_anti}
    	\end{itemize}
  \end{frame}
  \subsubsection*{Phase Transitions}
  \begin{frame}
    \frametitle{Phase Transitions}
    	\begin{itemize}
    		\item Critical Temperature: $\beta_c \approx 0.69$ or $T_c \approx 1.45 K$
    		\item Order Parameter
    	\end{itemize}
    \raggedleft \includegraphics[scale=0.14]{c_4_2} \includegraphics[scale=0.14]{m_4_2} \includegraphics[scale=0.14]{e_4_2}
  \end{frame}
  \subsubsection*{Numerical Analysis}
  \begin{frame}
    \frametitle{Numerical Analysis}
    \begin{columns}[t]
    	\begin{column}[T]{4.7cm}
    		\begin{itemize}
    			\item No analytic solutions
    			\item Intractable problems
    			\item Monte Carlo simulations
    			\begin{itemize}
    				\item Importance sampling
    			\end{itemize}
    			\end{itemize}
    	\end{column}
    	\begin{column}[T]{7cm}
  			\includegraphics[scale=0.25]{numerical_analysis}
  		\end{column}
		\end{columns}
  \end{frame}

	\section{Background and Theory}

	\subsection{Statistical Mechanics}
  \begin{frame}
    \frametitle{Statistical Mechanics}
    \framesubtitle{}
    \begin{itemize}
    	\item Canonical Ensemble
    	\item Boltzmann Distribution: $\displaystyle p_\mu = \frac{1}{Z(\beta)}e^{-\beta E(\mu)}$
    	\item Partition Function: $\displaystyle Z(\beta) = \sum_\mu e^{-\beta E(\mu)}$
    	\item Most macroscopic thermodynamic variables of a system can be expressed by the partition function or its derivatives!
    	\item For example, energy, specific heat, entropy, free energy...
    \end{itemize}
  \end{frame}
  \subsection{The Heisenberg Model}
  \begin{frame}
    \frametitle{Calculating the Physical Quantities}
    \framesubtitle{}
    \begin{itemize}
    	\item How do we calculate the required physical quantitites of the Heisenberg Model?
      \item Energy and specific heat:\\
      			$\displaystyle E = -\frac{J}{2}\sum_{<ij>}^{N}\vec{S_i}\cdot\vec{S_j}$ (factor of $\frac{1}{2}$ for double counting) \\
      			$\displaystyle C = k\beta^2(\left< E^2 \right> - \left< E \right>^2)$ \\
    	\item Magnetization: \\
    				$\displaystyle m_{rms} = \sqrt{M_x^2 + M_y^2 + M_z^2}$,\\
    				 where $\displaystyle M_\alpha = \frac{1}{N}\sum_i{\vec{S_{i\alpha}}}$

    \end{itemize}
  \end{frame}
  \subsection{Monte Carlo Method}
  \begin{frame}
    \frametitle{Application of Metropolis Monte Carlo}
    \framesubtitle{}
    \begin{itemize}
    	\item Numerical Analysis
    	\begin{itemize}
    		\item No analytic solution or intractable
    	\end{itemize}
    	\item Pseudo-random number generation
    	\item Monte Carlo Simulation
    	\begin{itemize}
    		\item Importance sampling: must satisfy Markov processes, ergodicity, and detailed balance
    		\item Metropolis importance sampling scheme/algorithm
    		\begin{enumerate}
    			\item Choose initial state
    			\item Choose a site
    			\item Calculate $\Delta E$ if "flip" the spin
    			\item If $\Delta E \le 0$, accept "flip" and go to (7), otherwise (5)
    			\item RNG: $0 < r < 1$
    			\item If $r < \exp(-\beta \Delta E)$, accept "flip"
    			\item Go to next site and go to (3)...
    		\end{enumerate}
    		\item Acceptance Ratio
    	\end{itemize}
    \end{itemize}
  \end{frame}

	\section{Method}
	\subsection{Implementation}
	\subsubsection*{Software}
  \begin{frame}
    \frametitle{Implementation}
    \framesubtitle{Software}
    \begin{columns}[t]
    	\begin{column}[T]{5cm}
    		\begin{itemize}
    			\item KISS: "Keep It Simple Stupid"
    			\item Functional Approach in C
    			\item 3D and 4D Pseudo-arrays of Pointers
    			\item GNU GCC Compiler, Code::Blocks IDE
   			\end{itemize}
      \end{column}
    	\begin{column}[T]{6.5cm}
    		\includegraphics[scale=0.35]{code1}
    	\end{column}
    \end{columns}
  \end{frame}
  \subsubsection*{Hardware}
  \begin{frame}
    \frametitle{Implementation}
    \framesubtitle{Hardware}
    \begin{itemize}
    	\item Workstation\\
    				Lenovo IdeaPad Y580 \\
    				Intel i7-3630QM,  8-thread, 3.4 GHz (max, single core) \\
    				16 GB ram, 256 GB SSD \\
    				GeForce GTX 660M (overclocked to 1 GHz) \\
    				Fedora 20 Linux, Scientific Spin
    	\item Simulation Machines\\
    				Custom Built PCs \\
    				AMD Opteron 6212, 16-thread, 3.2 GHz (max, $\le$ 4 core) \\
    				32GB ram, Fedora and Ubuntu Linux
    \end{itemize}
  \end{frame}

  \section{Results and Discussion}
  \subsection{Data Plots}
  \begin{frame}
    \frametitle{Data Plots}
    \framesubtitle{Energy}
    \center \includegraphics[scale=0.3]{e}
  \end{frame}
  \begin{frame}
  	\frametitle{Data Plots}
    \framesubtitle{Magnetization}
    \center \includegraphics[scale=0.3]{m}
  \end{frame}
  %Specific Heat
  \begin{frame}
  	\frametitle{Data Plots}
    \framesubtitle{Specific Heat}
    \center \includegraphics[scale=0.3]{c}
  \end{frame}
  %Susceptibility
  \begin{frame}
  	\frametitle{Data Plots}
    \framesubtitle{Magnetic Susceptibility}
    \begin{itemize}
    	\item \textbf{Susceptibility calculation not straightforward due to rotational invariance in 3D Heisenberg Model!}
    \end{itemize}
  \end{frame}

  \section{Conclusion}
  \subsection*{Conclusion}
  \begin{frame}
    \frametitle{Conclusion}
    \begin{itemize}
      \item Data matches predicted behavior
      \item Phase transition at critical temperature of $T_c \approx 1.45 K$
    	\item Susceptibility not as straightforward to calculate as in the Ising Model
    	\item Acceptance ratio oddity
    	\item Programmatic concerns
    	\begin{itemize}
    		\item Code in Fortran for readability, debugging, and intrinsic functions (but be careful!)
    		\item Fix arrays and possibly implement structures
    		\item Improve simulation run time! Optimize code!
    	\end{itemize}
    \end{itemize}
  \end{frame}

  \subsection*{Future Work}
  %Frame 10
  \begin{frame}
    \frametitle{Next Steps and Future Work}
    \begin{itemize}
      \item Code improvements
    	\item Susceptibility
    	\begin{itemize}
    		\item Correlation function calculation
    	\end{itemize}
    	\item GPU CUDA implementation
    	\item Spin dynamics
    	\item Magnetic frustration and other lattices
    	\item Possible project: LED Cube visualization of 3D Ising model phase change
    \end{itemize}
  \end{frame}

  \section{Acknowledgments and References}
  \begin{frame}
  	\frametitle{Acknowledgments and References}
  	\begin{itemize}
  		\item \textbf{Acknowledgments} \\
  					Tom Sadowski for his invaluable help with programming in C.
  		\item \textbf{References}
  		\begin{itemize}
  			\item Landau, D. P., & Binder, K. (2009). A guide to Monte Carlo simulations in statistical physics (3rd ed.). Cambridge: Cambridge University Press.
  			\item Newman, M. E., & Barkema, G. T. (1999). Monte Carlo methods in statistical physics. Oxford: Clarendon Press ;.
  			\item Gould, H., & Tobochnik, J. (2010). Statistical and thermal physics: with computer applications. Princeton, N.J.: Princeton University Press.
  			\item Tangirala, Sairam. Monte Carlo Simulation of 1D Heisenberg Model. http://www.scribd.com/doc/50664110/Monte-Carlo-Simulation-of-1D-Heisenberg-Model
  			\item arXiv:hep-lat/9301002
  			\item arXiv:hep-lat/9209017
  		\end{itemize}
  	\end{itemize}
  \end{frame}

\end{document}


\setcounter{framenumber}{346}
\begin{frame}
  \LectureNo{11}
  \maketitle
\end{frame}

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

\section{Rehash}
\begin{frame}{Rehash}

\begin{itemize}

		\item Tableaux for first-order logic work in similar
                  way as propositional tableaux. Underlying is always
                  the idea that an inference is valid iff the premises
                  and negation of conclusion are jointly
                  unsatisfiable.

		\item We have three tiers of tableau systems: simple
                  tableaux, tableaux with identity, and tableaux with
                  identity and functions. We successively add
                  rules for those.

		\item There is also a concept of an associated model,
                  which in first-order logic is a \emph{term model},
                  i.e. a model based on the syntactic strings as the
                  objects in the domain.

		\item First-order tableaux can turn out to be
                  infinite, in which case we need to \emph{prove} that
                  they are open and complete.

		\item The existence of infinite tableaux destroys our
                  hopes for an easy decidability proof.

		\item Alas, first-order logic is actually provably
                  undecidable.

\end{itemize}

\end{frame}

\section{11. Soundness and Completeness}
\subsection{11.1 Overview}

\begin{frame}{11.1 Overview}

	\begin{itemize}

        \item (11.1.1) We're now going to prove soundness and
          completeness for first-order logic. And it's going to be
          \emph{hard}.

          \item (11.1.2) The \emph{idea}, however, is essentially the
            same as in propositional logic:
            \begin{itemize}

            \item Soundness follows from down preservation.

            \item Completeness follows from up preservation.

            \end{itemize}

          \item (11.1.3) Infinite tableaux are not really a problem.

           \item In this lecture version of the proofs, I will ignore
             $=$ and function symbols, hopefully making the proofs
             more perspicuous.

            \item We still need to prove to auxilliary lemmas.

	\end{itemize}

\end{frame}

\subsection{11.2 The Denotation and Locality Lemma}
\begin{frame}{11.2 The Denotation and Locality Lemma}

  \begin{itemize}

  \item (11.2.1) The denotation lemmma says (in a sense) that substitution and
    variable fixing are equivalent:
    \[\mathcal{M},\alpha\vDash (P(x))[x:=t]\text{ iff
      }\mathcal{M},\alpha[x\mapsto\llbracket
      t\rrbracket^\mathcal{M}_\alpha]\vDash P(x)\]
    In words, replacing $x$ with $t$ is the same as setting the value
    of $x$ to the value of $t$.

    \item (11.2.2) \textbf{Lemma} (Denotation Lemma)\textbf{.}
      Consider a formula $\phi$ with precisely one free
            variable, $x$, and an arbitrary ground term $t$. Further,
            let $\mathcal{M}$ be a model and
            $\alpha$ an assignment. Then we have:
            \[\mathcal{M},\alpha\vDash (\phi)[x:=t]\text{ iff
              }\mathcal{M},\alpha[x\mapsto \llbracket
              t\rrbracket^\mathcal{M}_\alpha]\vDash \phi\]

  \end{itemize}

\end{frame}

\begin{frame}{Proof of the Denotation Lemmma (Sketch)}

  \begin{itemize}

  \item We prove this by induction. I sketch the proof, details are in
    the reader.

   \item For the base case, consider $R(t_1, \mathellipsis, t,
     \mathellipsis, t_n)$.

     \item This is of the form $(R(t_1, \mathellipsis,x, \mathellipsis,
       t_n))[x:=t]$.

       \item Note that all of $t_1, \mathellipsis, t_n$ are ground
         terms.

         \item We have: {\small\[\mathcal{M},\alpha\vDash R(t_1,
               \mathellipsis, t,
               \mathellipsis, t_n)\text{ iff }(\llbracket
                t_1\rrbracket^\mathcal{M}_\alpha,\mathellipsis,
                \llbracket
                t\rrbracket^\mathcal{M}_\alpha,\mathellipsis,\llbracket
                t_n\rrbracket^\mathcal{M}_\alpha)\in
                R^\mathcal{M}\]}

            \item Since the $t_i$ are ground terms, their values are
              not affected by changing the value of $x$.

              \item By definition, $\alpha[x\mapsto \llbracket
                t\rrbracket^\mathcal{M}_\alpha](x)=\llbracket
                t\rrbracket^\mathcal{M}_\alpha$.

              \item Together, these two observations give us:
                 {\scriptsize\[\mathcal{M},\alpha[x\mapsto \llbracket
                t\rrbracket^\mathcal{M}_\alpha]\vDash R(t_1,
               \mathellipsis, x,
               \mathellipsis, t_n)\text{ iff }(\llbracket
                t_1\rrbracket^\mathcal{M}_\alpha,\mathellipsis,
                \llbracket
                t\rrbracket^\mathcal{M}_\alpha,\mathellipsis,\llbracket
                t_n\rrbracket^\mathcal{M}_\alpha)\in
                R^\mathcal{M}\]}

  \end{itemize}

\end{frame}

\begin{frame}{Proof Cont'd}

  \begin{itemize}

  \item For the induction step, we assume that the claim holds for
    $\phi$:
    \[\mathcal{M},\alpha\vDash (\phi)[x:=t]\text{ iff
              }\mathcal{M},\alpha[x\mapsto \llbracket
              t\rrbracket^\mathcal{M}_\alpha]\vDash \phi\]

   \item We derive that it holds for $\forall y\phi$:
            \[\mathcal{M},\alpha\vDash (\forall
              y\phi)[x:=t]\text{ iff }
              \mathcal{M},\alpha[x\mapsto \llbracket
              t\rrbracket^\mathcal{M}_\alpha]\vDash \forall
              y\phi\]

    \item Note that $(\forall y\phi)[x:=t]=\forall y(\phi)[x:=t]$.

    \item We know that $\mathcal{M},\alpha\vDash \forall
      y(\phi)[x:=t]$ iff for all $d\in
      D^\mathcal{M}$: \[\mathcal{M},\alpha[y\mapsto d]\vDash
        (\phi)[x:=t]\]

    \item By the induction hypothesis, this is equivalent to:
      \[\mathcal{M},\alpha[y\mapsto d, x\mapsto \llbracket
        t\rrbracket^\mathcal{M}_\alpha]\vDash \phi\]

      \item So we get $\mathcal{M},\alpha[x\mapsto \llbracket
              t\rrbracket^\mathcal{M}_\alpha]\vDash \forall
              y\phi$, as desired.

  \end{itemize}

\end{frame}

\begin{frame}{(11.2.3) Use of the Denotation Lemmma}

  \begin{itemize}
  \item We can use the denotation lemma to show that $\forall
    x\phi\vDash(\phi)[x:=t]$

  \item If everything is $\phi$, then $t$ is $\phi$.

  \item Suppose that $\mathcal{M},\alpha\vDash\forall x\phi$.

    \item It follows that for each $d\in D^\mathcal{M}$, we have
      \[\mathcal{M},\alpha[x\mapsto d]\vDash \phi.\]

    \item Just set the value of $x$ to the value of $t$:
      \[\mathcal{M},\alpha[x\mapsto \llbracket
        t\rrbracket^\mathcal{M}_\alpha]\vDash \phi\]

      \item By the denotation lemma, this gives
        us: \[\mathcal{M},\alpha\vDash(\phi)[x:=t]\] as desired.

    \end{itemize}

\end{frame}

\begin{frame}{(11.2.4) Yet Another Locality Lemma}

  \begin{itemize}

  \item \textbf{Lemma} (Locality)\textbf{.} Let $\mathcal{M}$ and
    $\mathcal{N}$ be models with
              $D^\mathcal{M}=D^\mathcal{N}$. Further, let $\varphi$ be
              a sentence such that for all constants $c$ that occur in
              $\varphi$, $c^\mathcal{M}=c^\mathcal{N}$ and for all
              predicates $R$ that occur in $\varphi$,
              $R^\mathcal{M}=R^\mathcal{N}$. Then for all assignments
              $\alpha$, we have that \[\mathcal{M},\alpha\vDash
                \varphi\text{ iff }\mathcal{N},\alpha\vDash\varphi.\]

              \item The essence of this lemma is that if we changing
                the interpretation of symbols not in the formula,
                doesn't affect the truth-value of the formula.

  \end{itemize}

\end{frame}

\begin{frame}{Proof of the Locality Lemma}

  \begin{itemize}
  \item We prove the claim by induction.
    \item I do the cases for $R(t_1, \mathellipsis, t_n)$ and $\forall
      x\phi$, the rest is exercise.
      \item For the base case, consider an atomic sentence $R(t_1,
        \mathellipsis, t_n)$.
        \item We know that $\mathcal{M},\alpha\vDash
          R(t_1,\mathellipsis, t_n)$ iff \[(\llbracket
                  t_1\rrbracket_\alpha^\mathcal{M}, \mathellipsis,
                  \llbracket t_n\rrbracket_\alpha^\mathcal{M}) \in
                  R^\mathcal{M}.\]
                  \item But since $R^\mathcal{M}=R^\mathcal{N}$ and
                    $t_i^\mathcal{M}=t_i^\mathcal{N}$, this immediately
                    gives us \[(\llbracket
                  t_1\rrbracket_\alpha^\mathcal{N}, \mathellipsis,
                  \llbracket t_n\rrbracket_\alpha^\mathcal{N}) \in
                  R^\mathcal{N},\] which just means that
                $\mathcal{N},\alpha\vDash R(t_1, \mathellipsis, t_n)$.
  \end{itemize}

\end{frame}

\begin{frame}{Proof of Locality (Cont'd)}

  \begin{itemize}
  \item For the induction step, assume the induction hypothesis that
    if $\mathcal{M}$ and
    $\mathcal{N}$ have the same domain and aggree on the
    interpretation of the non-logical
    symbols in $\phi$, then $\mathcal{M},\alpha\vDash \phi$ iff
    $\mathcal{N},\alpha\vDash\phi$.

  \item We know that $\mathcal{M},\alpha\vDash\forall x\phi$ iff for
    all $d\in D^\mathcal{M}$, we have that
    \[\mathcal{M},\alpha[x\mapsto d]\vDash\phi.\]

    \item But since $D^\mathcal{M}=D^\mathcal{N}$ by the induction
      hypothesis, this is equivalent to it being the case that  for each $d\in D^\mathcal{N}$, we
      have  \[\mathcal{N},\alpha[x\mapsto d]\vDash\phi.\]

      \item But that just means that $\mathcal{N},\alpha\vDash \forall
        x\phi$, as desired.

  \end{itemize}

\end{frame}

\subsection{11.3 The Soundness Theorem}

\begin{frame}{11.3 The Soundness Theorem}

  \begin{itemize}
  \item (11.3.1) The idea remains the same as in propositional logic:
    if the premises don't entail the conclusion, the corresponding
    countermodel let's us see that there must be an open branch in the
    corresponding tableau.

  \item (11.3.2) This relies on the down-preservation property:

    \begin{itemize}
    \item If a valuation is faithful to a branch and a rule is
      applied, then the valuation is faithful to at least one new
      branch.
    \item How do we define faithfulness in first-order logic?

      \item A \emph{model} is faithful to a branch $B$ iff for all
        assignments $\alpha$, all formulas on the branch are true.
    \end{itemize}

    \item But, unfortunately, the strict down preservation property
      \emph{fails} in first-order logic:
      \begin{itemize}
      \item If $\exists x\phi$ is on the branch, we get $(\phi)[x:=p]$
        in the branch for some fresh $p$.

      \item But since $p$ wasn't on the branch so far, it's value
        doesn't affect the truth of the formulas on the branch
        (locality).
        \item So, we can have $\exists xP(x)$ is true, but $P(p)$ is false.
      \end{itemize}
  \end{itemize}

\end{frame}

\begin{frame}{(11.3.3) The Soundness Lemma}

\textbf{Lemma} (Soundness Lemma)\textbf{.} Let $B$ be a branch of a (possibly incomplete)
                      tableau and suppose further that $\mathcal{M}$
                      is a model that is faithful to $B$. Then, if a
                      rule is applied to $B$, extending the branch to
                      $B'$, then there \emph{exists} a model
                      $\mathcal{N}$ with
                      $D^\mathcal{M}=D^\mathcal{N}$ and
                      $c^\mathcal{M}=c^\mathcal{N}$ for all
                      $c\in\mathcal{C}$ which occur on $B$, such that
                      $\mathcal{N}$ is faithful to $B'$.

\end{frame}

\begin{frame}{Proof of Soundness Lemma}
  \begin{itemize}
  \item The proof works essentially as in propositional logic: we go
    through the rules one by one.

    \item The propositional rules can be handled just as in
      propositional logic, i.e. we can let $\mathcal{M}=\mathcal{N}$.

    \item The rules for negated quantifiers are easy:
      \begin{center}
                        \begin{prooftree}
                          {
                            line numbering=false,
                            line no sep= 2cm,
                            for tree={s sep'=10mm},
                            single branches=true,
                            close with=\xmark
                          }
                          [\neg \forall x\varphi
                          [\exists x\neg\varphi ]
                          ]
                        \end{prooftree}\hspace{4ex}
                        \begin{prooftree}
                          {
                            line numbering=false,
                            line no sep= 2cm,
                            for tree={s sep'=10mm},
                            single branches=true,
                            close with=\xmark
                          }
                          [\neg \exists x\varphi
                          [\forall x\neg \varphi ]
                          ]
                        \end{prooftree}
                      \end{center}
                     \item  Just note that $\neg\forall
                      x\phi\equi \exists x\neg\phi$ and
                      $\neg\exists x\phi\equi\forall x\neg \phi$.

                    \item So we can let $\mathcal{M}=\mathcal{N}$.

                      \item The interesting cases are the quantifiers.
  \end{itemize}
\end{frame}

\begin{frame}{Soundness ---  Universal Quantifier}

  \begin{itemize}
  \item Suppose that $\mathcal{M}$ is faithful to $B$ and we use

    \begin{center}
                      \begin{prooftree}
                        {
                          line numbering=false,
                          line no sep= 2cm,
                          for tree={s sep'=10mm},
                          single branches=true,
                          close with=\xmark
                        }
                        [\forall x\varphi
                        [{\varphi[x:=a]^\dagger} ]
                        ]
                      \end{prooftree}\quad\raisebox{-3ex}{{\small$\dagger$:
                          $a$ any constant or parameter on the branch}}
                        to extend the branch to $B'$.
                        \end{center}

                        \item Since $\mathcal{M}$ is faithful to $B$
                          and $\forall x\phi\in B$, we have
                          $\mathcal{M},\alpha\vDash\forall x\phi$.

                          \item But we've already observed that
                            $\forall x\phi\vDash (\phi)[x:=t]$, for
                            any ground term $t$, so
                            $\mathcal{M},\alpha\vDash (\phi)[x:=a]$.

                            \item Since $B'=B\cup\{ (\phi)[x:=a]\}$,
                              we get that $\mathcal{M}$ is faithful to
                              $B'$.

  \end{itemize}

\end{frame}

\begin{frame}{Soundness --- Existential Quantifier}

  \begin{itemize}
  \item This is the interesting case.

  \item Suppose $\mathcal{M}$ is faithful to $B$ and we extend $B$ to
    $B'$ using:

    \begin{center}
			\begin{prooftree}
                          {
                            centered,
                            line numbering=false,
                            line no sep= 2cm,
                            for tree={s sep'=10mm},
                            single branches=true,
                            close with=\xmark
                          }
                          [\exists x\varphi
                          [{\varphi[x:=p]^\dagger} ]
                          ]\end{prooftree}
                        \quad\raisebox{-3ex}{{\small$\dagger$: $p$ a fresh parameter}}

                      \end{center}

              \item Since $\mathcal{M}$ is faithful to $B$, we know
                that $\mathcal{M},\alpha\vDash\exists x\phi$.

                \item So, there exists a $d\in D^\mathcal{M}$ such
                  that \[\mathcal{M},\alpha[x\mapsto d]\vDash \phi\]

                  \item So we can change $\mathcal{M}$ to
                    $\mathcal{N}$ by setting $p^\mathcal{N}=d$ and
                    leaving everything else the same.

                    \item By the denotation lemma, we get
                      $\mathcal{N},\alpha\vDash (\phi)[x:=p]$.

                      \item And since $p$ was fresh, for all other
                        formulas in $B$, the condition for the
                        locality lemma are satisfied.

                        \item So, $\mathcal{N},\alpha\vDash \psi$ for
                          all $\psi\in B$.

                          \item Since $B'=B\cup \{(\phi)[x:=p]\}$, we
                            get that $\mathcal{N}$ is faithful to $B'$.

  \end{itemize}

\end{frame}

\begin{frame}{(11.3.4) The Soundness Theorem}

\textbf{Theorem} (Soundness)\textbf{.} If $\Gamma\vdash\phi$, then
$\Gamma\vDash\phi$.

\emph{Proof}:

\begin{itemize}
\item By contrapositive proof.
\item Suppose that $\Gamma\nvDash\phi$.
  \item We get a model $\mathcal{M}$, such that all the members of
    $\Gamma$ are true in $\mathcal{M}$ and $\phi$ is false.
  \item So $\mathcal{M}$ is faithful to $\Gamma\cup\{\neg\phi\}$.
    \item Now everytime we apply a rule to construct the tableaux,
      there will be a mmodel $\mathcal{N}$ that is faithful to at
      least one new branch.
      \item So, for the commplete tableau, there is a branch $B$ and
        such a model
        $\mathcal{N}$ faithful to it.

      \item But that means $B$ can't be closed.
        \item Since if it were, we'd get $R(t_1, \mathellipsis, t_n)$
          both true and false under $\mathcal{N}$.
          \item Hence, $B$ is open, so $\Gamma\nvdash\phi$, as desired.
\end{itemize}

\end{frame}

\subsection{11.4 The Completeness Theorem}

\begin{frame}{11.4 The Completeness Theorem}

  \begin{itemize}

    \item The aim is to prove that the associated model
      $\mathcal{M}_B$ for an open branch $B$ in a complete tableau is
      faithful to $B$.

    \item This will give us completeness as in propositional logic.

      \item \textbf{Lemma} (Completeness Lemma)\textbf{.} Let $B$ be an open
      branch in a complete tableau. Then $\mathcal{M}_B$ defined as follows
		\begin{enumerate}[(i)]

			\item $D^{\mathcal{M}_B}=\{a\in \mathcal{C}\cup Par: a\text{ occurs on }B\}$

			\item $a^{\mathcal{M}_B}=a$ for all $a\in \mathcal{C}\cup Par$

			\item $R^{\mathcal{M}_B}=\{(a_1, \mathellipsis, a_n): R(a_1, \mathellipsis, a_n)\in B\}$

		\end{enumerate}
                is faithful to $B$.

                \item We prove this by \dots surprise \dots induction.

  \end{itemize}

\end{frame}

\begin{frame}{Proof of the Completeness Lemma}

  \begin{itemize}
  \item The prove is again similar as in propositional logic,
    i.e. what we actually prove by induction is that for all
    $\phi\in\mathcal{L}$
    \begin{enumerate}[1.]

                      \item if $\phi\in B$, then
                        $\mathcal{M}_B,\alpha\vDash\phi$

                        \item if $\neg\phi\in B$, then
                          $\mathcal{M}_B,\alpha\nvDash\phi$

                        \end{enumerate}

             \item I will do the base case and the induction steps for
               $\forall x\phi$ and $\neg\forall x\phi$.

               \item For the base case of 1., suppose that
                 $R(t_1,\mathellipsis, t_n)\in B$.

                 \item We know \[\mathcal{M}_B,\alpha\vDash R(t_1,
                            \mathellipsis, t_n)\text{ iff }(\llbracket
                            t_1\rrbracket^{\mathcal{M}_B}_\alpha,
                            \mathellipsis, \llbracket
                            t_1\rrbracket^{\mathcal{M}_B}_\alpha)\in
                            R^{\mathcal{M}_B}.\]

                            \item By definition of $\mathcal{M}_B$, we
                              know:
                              \begin{itemize}
                              \item $\llbracket t_i\rrbracket^\mathcal{M}_\alpha=t_i^{\mathcal{M}_B}=t_i$
                                \item $R^{\mathcal{M}_B}=\{(t_1, \mathellipsis, t_n): R(t_1, \mathellipsis, t_n)\in B\}$
                                \end{itemize}

                                \item So, since $R(t_1,\mathellipsis,
                                  t_n)\in B$, we get $(t_1,
                                  \mathellipsis, t_n)\in
                                  R^{\mathcal{M}_{B}}$

                                  \item This directly gives us
                                    $\mathcal{M}_B,\alpha\vDash R(t_1,
                                    \mathellipsis, t_n)$.

  \end{itemize}

\end{frame}

\begin{frame}{Completeness Lemma --- Base Case}

  \begin{itemize}

  \item  For the base case of 2., suppose that
                            $\neg R(t_1, \mathellipsis, t_n)\in
                            B$.

                            \item Since $B$ is open, we can conclude
                            that $R(t_1,\mathellipsis, t_n)\notin
                            B$.

                            \item $R^{\mathcal{M}_B}=\{(t_i1,
                              \mathellipsis, t_in): R(t_i1,
                              \mathellipsis, t_in)\in B\}$, it follows
                              that \[(t_i1, \mathellipsis, t_in)\notin
                              R^\mathcal{M}.\]

                              \item Since $\llbracket
                                t_ii\rrbracket^\mathcal{M}_\alpha=t_ii^{\mathcal{M}_B}=t_ii$,
                                we get \[(\llbracket
                                t_ii\rrbracket^\mathcal{M},
                                \mathellipsis, \llbracket
                                t_ii\rrbracket^\mathcal{M})\notin
                                R^{\mathcal{M}_B}.\]

                                \item But that just means that
                                  $\mathcal{M}_B,\alpha\nvDash R(t_i1,
                                  \mathellipsis, t_in)$, as desired.

  \end{itemize}

\end{frame}

\begin{frame}{Completeness Lemma --- Universal Quantifier}

  \begin{itemize}
  \item For the induction step, assume the hypothesis that for all
    $\alpha$,
     \begin{enumerate}[1.]

                      \item if $\phi\in B$, then
                        $\mathcal{M}_B,\alpha\vDash\phi$

                        \item if $\neg\phi\in B$, then
                          $\mathcal{M}_B,\alpha\nvDash\phi$

                        \end{enumerate}

                      \item Now suppose that $\forall x\phi\in B$.

                        \item In order to show that
                          $\mathcal{M}_{B},\alpha\vDash\forall x\phi$, we
                          need to show that for all $d\in
                          D^{\mathcal{M}_B}$, we
                          have \[\mathcal{M}_B,\alpha[x\mapsto
                            d]\vDash \phi\]
                          \item Remember that $D^{\mathcal{M}_B}=\{a:
                            a\text{ occurs in }
                            B\}$, so we need to show \[\mathcal{M}_B,\alpha[x\mapsto
                            a]\vDash \phi\]

                              \item Since the tableau is complete, we
                                know that the universal quantifier
                                rule has been applied.

                                \item It follows that $(\phi)[x:=a]\in
                                  B$.

                                  \item By the induction hypothesis,
                                    we get $\mathcal{M}_{B},\alpha\vDash
                                    (\phi)[x:=a]$.

                                    \item By the denotation lemma, we
                                      get $\mathcal{M}_B,\alpha[x\mapsto
                            a]\vDash \phi$, as desired.

  \end{itemize}

\end{frame}

\begin{frame}{The Completeness Lemma --- Negated Universals}

  \begin{itemize}

  \item Now suppose that $\neg\forall x\in B$.

    \item Since the tableau is complete, we know that $\exists
      x\neg\phi$ and $(\neg \phi)[x:=p]$ are on $B$.

    \item We know that $(\neg \phi)[x:=p]=\neg (\phi)[x:=p]$.

      \item So, we can conclude using the induction hypothesis that $\mathcal{M}, \alpha\nvDash
        (\phi)[x:=p]$

        \item By the denotation lemma, we get
          $\mathcal{M},\alpha[x\mapsto \llbracket
          p\rrbracket^\mathcal{M}_\alpha]\nvDash \phi$.

          \item But since $\llbracket
            p\rrbracket^\mathcal{M}_\alpha=p\in D^{\mathcal{M}_B}$,
            this immediately means $\mathcal{M},\alpha\nvDash \forall
            x\phi$, as desired.

            \item This completes our proof.

  \end{itemize}

\end{frame}

\begin{frame}{The Completeness Theorem}

  \textbf{Theorem} (Completeness for First-Order Tableaux)\textbf{.}
  If $\Gamma\vDash\phi$, then $\Gamma\vdash\phi$.

  \emph{Proof}:

  \begin{itemize}
  \item We prove the contrapositive.

  \item So suppose that $\Gamma\nvdash\phi$.

    \item This means, by definition, that the complete tableaux for
      $\Gamma\cup\{\neg\phi\}$ has at least one open branch $B$.

      \item By the completeness lemma, we have that $\mathcal{M}_B$ is
        faithful to this branch.

        \item Since $\Gamma\cup\{\neg\phi\}\subseteq B$, we get that
          $\mathcal{M}_B$ makes the $\Gamma$'s true and $\phi$ false.

        \item But that just means $\Gamma\nvDash\phi$.

          \item We're done.
  \end{itemize}

\end{frame}

\begin{frame}

  \vfill

  \begin{center}
  {\Large \textbf{Q.E.D.}}
\end{center}

\end{frame}

\begin{frame}{Core Ideas (Lecture Version)}

  \begin{itemize}

  \item (

  \end{itemize}

\end{frame}

\begin{frame}

	\begin{center}
	{\huge\bf Thanks!}
	\end{center}

\end{frame}


\documentclass{article}
\title{Chapter 11}
\author{Newton Ni}

\usepackage{bussproofs}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{turnstile}

% Set cardinality: |#1|
\newcommand{\cardinality}[1]{\lvert#1\rvert}

% Set: { #1 }
\newcommand{\set}[1]{\{\ #1\ \}}

% Set comprehension: { #1 | #2 }
\newcommand{\comp}[2]{\set{#1\ \mid\ #2}}

% t₁, t₂, t₃, ...
\newcommand{\term}[1]{\texttt{t\textsubscript{#1}}}

% v₁, v₂, v₃, ...
\newcommand{\val}[1]{\texttt{v\textsubscript{#1}}}

% l₁, l₂, l₃, ...
\newcommand{\lbl}[1]{\texttt{l\textsubscript{#1}}}

% p₁, p₂, p₃, ...
\newcommand{\pat}[1]{\texttt{p\textsubscript{#1}}}

% T₁, T₂, T₃, ...
\newcommand{\ty}[1]{\texttt{T\textsubscript{#1}}}

% x₁, x₂, x₃, ...
\newcommand{\var}[1]{\texttt{x\textsubscript{#1}}}

% T -> T
\newcommand{\fn}[2]{#1$\rightarrow$#2}

\renewcommand{\ss}[2]{#1 \longrightarrow #2}
\renewcommand{\bs}[2]{#1 \Downarrow #2}

% Monospace
\newcommand{\ms}[1]{\texttt{#1}}

\newcommand{\LabelR}[1]{\RightLabel{\textsc{#1}}}

\theoremstyle{remark}
\newtheorem*{case}{Case}

\begin{document}
\maketitle

\section{11.4.1}

    \textit{Show how to formulate ascription as a derived form. Prove that the ``official'' typing}
    \textit{and evaluation rules given here correspond to your definition in a suitable sense.} \\

    We can formulate ascription using the following transformation \ms{Lower}:
    \begin{align*}
        \ms{Lower(t as T)} &= (\lambda \var{} : \ty{}.\ \var{})\ \term{} \\
        \ms{Lower(t)} &= \term{}
    \end{align*}

    We wish to prove the following:
    \begin{align}
        \Gamma \vdash \term{} : \ty{} &\iff \Gamma \vdash \ms{Lower(t)} : \ty{} \\
        \ss{\term{}}{\term{}'} &\iff \ss{\ms{Lower(t)}}{\ms{Lower(\term{}')}}
    \end{align}

    We induct on terms \term{}. For both (1) and (2), all cases except \ms{t = \term{1} as T}
    follow from straightforward induction. For the forward case of (1), we know the following:

    \begin{enumerate}
        \item[1.F.1] $\Gamma \vdash \term{1} :\ \ty{}$
        \item[1.F.2] $\ms{Lower(\term{1} as T)} = (\lambda \var{} : \ty{}.\ \var{})\ \term{1}$
    \end{enumerate}

    The conclusion follows from applying \textsc{T-App} to (1.F.2), after type-checking
    the identity function with \textsc{T-Abs}, \textsc{T-Var}, and (1.F.1) (proof tree omitted). \\

    For the backward case of (1), we know:
    \begin{enumerate}
        \item[1.B.1] $\Gamma \vdash \ms{Lower(\term{1} as T)} : \ty{}$
        \item[1.B.2] $\Gamma \vdash (\lambda \var{} : \ty{}.\ \var{})\ \term{1} : \ty{}$
    \end{enumerate}

    Applying \textsc{T-Abs}, \textsc{T-Var} in reverse (probably need lemma to prove
    this is okay, as there is only one typing judgment each for these syntactic forms)
    yields (1.B.3): $\term{1} : \ty{}$, which is enough to finish with \textsc{T-Ascribe}. \\

    For the forward case of (2), there are two sub-cases: when \term{1} is a value or not.
    When it is a value \val{1}, we apply \textsc{E-Ascribe} to the left-hand side and have
    $\ss{\ms{\val{1} as \ty{}} }{\val{1}}$. On the right-hand side, we want to show that
    $\ss{(\lambda \var{} : \ty{}.\ \var{})\ \val{1}}{\val{1}}$. This follows directly from
    \textsc{E-AppAbs}.

    When \term{1} is not a value, we apply \textsc{E-Ascribe1} to the left-hand side and
    get $\ss{\ms{\term{1} as \ty{}}}{\term{1}'}$. On the right-hand side, since the identity
    function is a value, the conclusion follows from applying \textsc{E-App2} and the determinancy
    of small-step evaluation. \\

    For the backward case of (2), we again distinguish when \term{1} is a value or not,
    and the logic is similar to above. \\

    \textit{Suppose that, instead of the pair of evaluation rules \textsc{E-Ascribe} and \textsc{E-Ascribe1},}
    \textit{we had given an ``eager'' rule that throws away an ascription as soon as it is reached. Can}
    \textit{ascription still be considered as a derived form?} \\

    It depends on the evaluation strategy for the base lambda calculus. For the evaluation rules
    in the textbook, it would no longer be a derived form, as \textsc{E-AscribeEager} would diverge
    from the behavior of \textsc{E-App2} when \term{1} is not a value. But if we changed the evaluation
    strategy to apply functions to their argument eagerly as well, then this would be a derived form.

\section{11.5.2}

    Not sure how to define ``good idea'', but this seems to tangle the evaluation and typing judgments
    (requiring substitions in order to type-check), and require more computation. I suppose errors would
    also be harder to track to their source spans, if the error arises within substituted code?

\section{11.8.2}

    \textit{We can add a simple form of pattern matching to an untyped lambda calculus with records}
    \textit{by adding a new syntactic category of} patterns . . . \textit{Give typing rules for the}
    \textit{new constructs (making any changes to the syntax you feel are necessary in the process).} \\

    \begin{prooftree}
        \LabelR{T-Match}
        \AxiomC{$match(p, \val{1}) = \sigma$}
        \AxiomC{$\Gamma \vdash \sigma\ \term{2} : \ty{}$}
        \BinaryInfC{$\Gamma \vdash match(\pat{}, \val{1})\ \term{2}: \ty{}$}
    \end{prooftree}

    \textit{Sketch a proof of type preservation and progress for the whole calculus.} \\

\section{11.11.1}

    We can define factorial as:
    \begin{align*}
        &\ms{fix} \\
        &\lambda \ms{factorial}: \ms{int} \to \ms{int}. \\
        &\lambda \var{}: \ms{int}. \\
        &\ms{if}\ \var{}\ =\ 0\ \ms{then}\ 1\ \ms{else}\ \ms{factorial}\  (\var{} - 1) * \var{}
    \end{align*}

\section{11.11.2}

    We can rewrite factorial as:
    \begin{align*}
        &\ms{letrec factorial: int $\to$ int} = \\
        &\lambda \var{}: \ms{int}. \\
        &\ms{if}\ \var{}\ =\ 0\ \ms{then}\ 1\ \ms{else}\ \ms{factorial}\  (\var{} - 1) * \var{}
    \end{align*}

\section{11.12.1}

    \textit{Verify that the progress and preservation theorems hold for the simply typed lambda-calculus}
    \textit{with booleans and lists.}

\section{11.12.2}

    \textit{The presentation of lists here includes many type annotations that are not really needed,}
    \textit{in the sense that the typing rules can easily derive the annotations from context. Can} all
    \textit{the type annotations be deleted?} \\

    The type annotation for \ms{nil} cannot be deleted, because there's no argument to derive the type from.
    But all of the other syntactic forms for lists (\ms{cons}, \ms{isnil}, \ms{head}, \ms{tail}) can check
    the type of their argument, so their type annotations are unnecessary.

\end{document}


\documentclass[t]{beamer}
\usetheme{Copenhagen}
\setbeamertemplate{headline}{} % remove toc from headers
\beamertemplatenavigationsymbolsempty

\usepackage{amsmath, array, tikz, bm, pgfplots, tcolorbox, graphicx}
\pgfplotsset{compat = 1.16}
\usepgfplotslibrary{statistics}

\title{Introduction to Probability}
\author{}
\date{}

\AtBeginSection[]
{
  \begin{frame}
    \frametitle{Objectives}
    \tableofcontents[currentsection]
  \end{frame}
}

\begin{document}

\begin{frame}
\maketitle
\end{frame}

\section{Determine the probability of an event}

\begin{frame}{Sample Space}
\begin{tcolorbox}[colframe=green!20!black, colback = green!30!white,title=\textbf{Sample Space}]
The \textbf{sample space} is a listing of all possible outcomes.
\end{tcolorbox}
\vspace{6pt} \pause
\onslide<+->{Common sample spaces:} \\
\begin{itemize}
	\item<+->{\emph{Flipping a coin}: Heads, Tails}
	\item<+->{\emph{Rolling a single die}: 1, 2, 3, 4, 5, 6}
	\item<+->{\emph{Drawing a card from a standard deck}: Ace of spades, ace of hearts, $\dots$, king of diamonds}
\end{itemize}
\end{frame}

\begin{frame}{Venn Diagrams}
\begin{tcolorbox}[colframe=green!20!black, colback = green!30!white,title=\textbf{Venn Diagrams}]
A \textbf{Venn diagram} is a visualization of events and sample spaces.
\end{tcolorbox}
\vspace{8pt} \pause
\begin{center}
\begin{tikzpicture}
\draw (0,0) rectangle (6,4);
\draw [fill=green!60] (2,2.25) ellipse (1.25cm and 1cm);
\node at (6,4) [below left] {$\mathcal{S}$};
\node at (2,2.75) {$\bm{A}$};
\end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}{Playing Cards}
\begin{center}
\includegraphics[scale=0.23]{../Images/Playing_Cards.png}
\end{center}
\end{frame}

\begin{frame}{Probability}
\begin{tcolorbox}[colframe=green!20!black, colback = green!30!white,title=\textbf{Probability}]
\textbf{Probability} is a measure of the likelihood of an event occurring.
\end{tcolorbox}
\vspace{6pt} \pause
\begin{center}
Probability = $\dfrac{\text{number of ways the event can occur}}{\text{total number of outcomes in sample space}}$
\end{center}
\end{frame}

\begin{frame}{Example 1}
Determine the probability of each event.	\newline\\
(a)	\quad	Flipping a coin and landing on heads \newline\\	\pause
1 outcome: Heads	\quad \pause 2 outcomes in sample space \pause
\[P(\text{heads}) = \frac{1}{2}\]
\end{frame}

\begin{frame}{Example 1}
(b)	\quad Rolling a number less than 3 on a single die.	\newline\\	\pause
2 outcomes: 1, 2	\quad	\pause	6 outcomes in sample space \pause
\[P(\text{rolling less than 3 on a single die}) = \frac{2}{6} = \frac{1}{3} \]
\end{frame}

\begin{frame}{Example 1}
(c)	\quad	Drawing a face card from a standard deck.	\newline\\	\pause
Face cards include jacks, queens, and kings. \newline\\	\pause
There are four suits for each card: clubs, diamonds, hearts, and spades.	\newline\\	\pause
Thus, there are 12 total face cards: jack of clubs, jack of diamonds, $\dots$, king of spades	\quad \pause There are 52 total cards	\pause
\begin{align*}
P(\text{drawing a face card}) &= \frac{12}{52} \\[6pt]
P(\text{drawing a face card}) &= \frac{3}{13}
\end{align*}
\end{frame}

\begin{frame}{Example 1}
(d)	\quad The number of students in each class at a college is shown in the table below.
\begin{center}
\begin{tabular}{cccc}
Freshmen	&	Sophomore	&	Junior	&	Senior	\\	\hline
1670		&	2017		&	2975	&	3026
\end{tabular}
\end{center}
Find the probability that a randomly selected student is a sophomore.	\newline\\	\pause

Out of the 9,688 students attending the college, 2,017 of them are sophomores.	\newline\\	\pause

\[P(\text{sophomore}) = \frac{2017}{9688} \]
\end{frame}

\begin{frame}{Example 1}
(e)	\quad The 36 possible sums from rolling 2 dice are shown below.
\begin{center}
\begin{tabular}{c|cccccc}
			&	\textbf{1}	&	\textbf{2}	&	\textbf{3}	&	\textbf{4}	&	\textbf{5}	&	\textbf{6}	\\ \hline
\textbf{1}	&		2		&		3		&		4		&		5		&		6		&		7		\\
\textbf{2}	&		3		&		4		&		5		&		6		&		7		&		8		\\
\textbf{3}	&		4		&		5		&		6		&		7		&		8		&		9		\\
\textbf{4}	&		5		&		6		&		7		&		8		&		9		&		10		\\
\textbf{5}	&		6		&		7		&		8		&		9		&		10		&		11		\\
\textbf{6}	&		7		&		8		&		9		&		10		&		11		&		12
\end{tabular}
\end{center}

What is the probability of rolling a sum of 7?
\begin{align*}
\onslide<2->{P(\text{sum of 7}) &= \frac{6}{36}} \\[8pt]
\onslide<3->{&= \frac{1}{6}}
\end{align*}
\end{frame}

\section{Find the experimental probability of an event}

\begin{frame}{Types of Probability}
In the previous examples, each outcome had an equal chance of being selected. This is referred to as {\color{red}\textbf{classical probability}}.	\newline\\	\pause

There are two other types of probability: experimental and subjective.
\end{frame}

\begin{frame}{Experimental and Subjective Probabilities}
\begin{tcolorbox}[colframe=green!20!black, colback = green!30!white,title=\textbf{Experimental Probability}]
\textbf{Experimental probability} is probability based on events that have actually occurred.
\end{tcolorbox}
\vspace{10pt}	\pause
\begin{tcolorbox}[colframe=green!20!black, colback = green!30!white,title=\textbf{Subjective Probability}]
\textbf{Subjective probability} is probability based on opinion.
\end{tcolorbox}
\end{frame}

\begin{frame}{Example 2}
Flip a coin 10 times and state the experimental probability of landing on tails.
\end{frame}

\begin{frame}{Law of Large Numbers}
As the number of events increases, the experimental probability of an event will approach the classical (a.k.a. \textit{theoretical}) probability.
\end{frame}

\begin{frame}{Rules of Probability}
Rules of Probability Club:	\newline\\ \pause
\begin{itemize}
	\item<+-> Each probability must be a value between 0 and 1, inclusive.
	\begin{itemize}
		\item<+-> A probability of 0 is an \textit{impossible event}.
		\item<+-> A probability of 1 is a \textit{certain event}.
	\end{itemize}
	\item<+-> The sum of all possible probabilities of a sample space must equal 1.
\end{itemize}
\end{frame}

\end{document}


% COSC 4P03 Project
% sudoku generator
% Taras Mychaskiw

\section{Results}

A sample of $100,000$ ($10$ sets of $10,000$) $9$x$9$ sudoku puzzles were generated using each of the generation strategies discussed above.
During each of these, all three sudoku solvers were run concurrently to check formity of a puzzle. Once one solver got the answer,
it would return the answer and the other two would stop calculating. The "winner" (the solver that took the least amount of time) of each
of these was stored for an additional comparison of the solvers. All the results below (to do with generation) are are an average over
the $10$ runs, each of which calculated the average over generating $10,000$ sudoku puzzles.\\
While larger sudoku puzzles could be generated, for the purposes of this experiment, they were not needed. Presumably, the same results
would occur for any size sudoku puzzle, so the most basic and most well known size sudoku puzzles were used.

\subsection{Comparison of Solving Strategies}

    \subsubsection{Solving Hard Puzzles}
    To compare the solving strategies, each of them were used to solve all of Peter Norvig's Top 95 sudoku puzzles\cite{norvig}.
    These puzzles are considered some of the hardest ever found. Each of the solvers solved each puzzle, and checked the formity
    of each puzzle as well.
    \begin{table}[H]
    \begin{center}\begin{tabular}{l|r|r}
        \hline
        Solver                  &   Time to Solve   &   Time to Check Formity   \\  \hline
        %Backtracking            &   $593475$ms              &   $1407571$ms    \\
        %Constraint Propagation  &   $243$ms                 &   $351$ms        \\
        %Exact Cover             &   $166$ms                 &   $99$ms         \\
        Backtracking            &   $6000$                  &   $14200$         \\
        Constraint Propagation  &   $2.45$                  &   $3.55$          \\
        Exact Cover             &   $1.67$                  &   $1$             \\
        \hline
    \end{tabular}\end{center}
    \caption{Results of each of the solvers solving and checking the formity of Peter Norvig's Top 95 sudoku puzzles. Each of the actual
    times have been divided by the lowest time taken found, which was $99$ms on the computer used.}
    \label{tab:top95}
    \end{table}
    Seems that exact cover and dancing links is far ahead of the competition. Constraint propagation didn't do too badly. Backtracking
    is just not as good as the other two. Something to note about the formity time for exact cover - it's less than the time to took
    to solve the same puzzles. Seems counter intuitive, but it has to do with how the solving was actually implemented. Solving a puzzle
    would return the solved version of the puzzle, while formity checking only returned an integer. In exact cover, post processing is
    required to convert the matrix back into a sudoku, but this is not required in checking the formity. All that matters for formity
    is that a new solution was found (and if it was), the actual solution is not important.
    %\end{Solving Hard Puzzles}

    \subsubsection{Use in Generation}
    Something surprising happened during generation - backtracking still won. It seems that, from the data above, backtracking would
    win very few times if at all. It was excepted that exact cover would be about $60\%$ of the wins, and constraint propagation would
    be about $35\%$. However, this was not the case. Figure~\ref{fig:solvergen} should the results of the winners during the generation.
    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.70]{solvers.png}
        \caption{Results of how many times each solver won during each of the generation of sudoku puzzles.}
        \label{fig:solvergen}
    \end{figure}
    As expected, exact cover does win about $60\%$ of the time. However backtracking and constraint propagation are backwards from what was expected
    (note this was \textit{not} an implementation issue!). In fact, constraint propagation won so few times that it doesn't even appear in the graph.
    So why did backtracking do much better than expected? For top down generation, it actually makes sense. Backtracking requires absolutely
    no pre- or post-processing of the data. It just loops through the sudoku, skipping clues already given in the puzzle. During top down generation,
    most of the puzzle is already given, so backtracking has very few calculations to actually do. Meanwhile, constraint propagation and exact
    cover are still pre-processing the puzzle. Backtracking would win while the puzzle is fairly full still, then later exact cover would outperform,
    which is the type of results seen for top down generation.\\
    What about bottom up and deduction generation? Each of those start with an empty sudoku puzzle. Backtracking is still
    a major contender. The difference between the results in the previous section and now is that the formity checks are on puzzles that are
    not well formed, they usually have more than one solution. Each of the solving strategies is a depth first search, so if there are many
    solutions, they would find two similar ones quickly and return that there are multiple solutions. The difference between backtracking
    and the others is again the lack of additional processing. On a near empty sudoku, basically any values you throw into a cell will lead
    to a solution with very little backtracking actually required. So, the same reasoning applies to these generation strategies as top down.
    While exact cover is still pre-processing, backtracking has gotten a head start, and may end up finding two very similar solutions very
    quickly and actually winning.\\
    Constraint propagation didn't do anywhere near as well as expected. This is likely due to the unexpected backtracking results. Backtracking
    does exceptionally well in near full and near empty puzzles while checking for formity. In all other cases, exact cover would simply outperform
    constraint propagation. Given these facts, it makes sense that constraint propagation simply fails to show any results. If one were to redo
    the experiment without using exact cover, my personal estimate is that constraint propagation would take exact cover's position and win
    most of the time against backtracking.
    %\end{Use in Generation}
%\end{Comparison of Solving Strategies}

\subsection{Comparison of Generation Strategies}
As mentioned, $100,000$ sudoku puzzles were generated using each of the three generators. Figure~\ref{fig:generators} shows how each of
the generators performed on average.
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.70]{generators.png}
    \caption{Average results of each of the three sudoku puzzle generators.}
    \label{fig:generators}
\end{figure}
Figure~\ref{fig:generators} shows that, on average, the bottom up generation strategy created puzzles with a fewer clues than the other
generation methods, but it also took more time. The average time and number of clues for bottom up generation are skewed however by outliers.
Occasionally, bottom up would "get stuck" on a particular problem. New clues were added, but a unique solution was never obtained,
at least not until a large number of clues was added. This meant that a clue would be added, the solvers would run, no solution was found and the
clue was removed. These types of puzzles took ages to generate, and had many clues compared to all the other bottom up puzzles generated.
Of the $100,000$ generated, about $100$ experienced this problem. These outliers had about double the clues of all the other puzzles, and took
anywhere from $10$ to $100$ times the amount of time to create. Bottom up, even including these outliers, was the most successful generator tested.
It produced puzzles with fewer clues more often than the others, and actually produced the only $19$ clue puzzle found during the testing.\\
Top down and deduction generation seem to be about on par, except for the fact that deduction generation usually produced puzzles with fewer
clues than top down generation in about the same amount of time.\\
Looking back at Figure~\ref{fig:solvergen}, we can see why each generation strategy took as long as they did. Deduction and top down generation
each had to check the formity of about the same number of puzzles - the total width of the bar. Bottom up generation had to check formity about
three times as many puzzles, so it took much longer than the others.
%\end{Comparison of Generation Strategies}

%\end{Results}


\documentclass[11pt]{article}
\usepackage[
top    = 2.50cm,% presumably you don't want it to be 0pt as well?
bottom = 2.50cm,
left   = 2cm,
right  = 2cm,
marginparsep = 0pt,
marginparwidth=0pt,
]{geometry}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{fancyhdr}
\usepackage[siunitx]{circuitikz}[european resistors	]
\usepackage{siunitx}
\usepackage{caption}
\ctikzset{resistor = european}
\usepackage{tikz}
\usepackage{pgfplots}                %Tikz and GeoGebra integration
\usepackage{multicol}
\pagestyle{fancy}
\fancyhead[l]{Capacitors - Abridged edition}
\fancyhead[r]{Giorgio Grigolo \textcopyright}
\fancyfoot[c]{ }
\fancyfoot[r]{\thepage}

\begin{document}
	\section{Capacitance definition: }

\begin{center}
		\textbf{Capacitance} is the ratio of the change in an electric charge in a system to the corresponding change in its electric potential.
\end{center}

	\section{Capacitor symbol: }

	\begin{center}
		\begin{circuitikz}
			\draw (2,0) to [C,l_=$C_1$,*-*] (4,0);
		\end{circuitikz}
	\end{center}

	\section{Capacitance equation: }
	\[Q = CV\]
	\begin{center}
		Where $C$ is capacitance (\si{\farad}), $Q$ is the charge (\si{\coulomb}) stored in the and V is the potential difference (\si{\volt}) between the plates.
	\end{center}

	\section{Capacitance equation (given area and distance of plates): }
	\[C = \varepsilon\frac{A}{d}\]
	\begin{center}
		Where $C$ is \textbf{capacitance} (\si{\farad}), $\varepsilon$ is \textbf{absolute permittivity} ($\varepsilon_0 \, \varepsilon_r$), $A$ is the \textbf{common area} (\si{\meter\squared}) of overlap and $d$ the \textbf{seperation} (\si{meter}) of the plates.
	\end{center}

	\section{Charging capacitor: }

	\begin{multicols}{3}

		{\begin{circuitikz}[scale=0.7]
				\draw(0,0) to[battery, l=6<\volt>] (4,0)
				to[resistor, l=6<\volt>] (4,4)
				to[capacitor, l=0<\volt>] (0,4) -- (0,0);
			\end{circuitikz}
			\captionof{figure}{Uncharged capacitor}}

		{\begin{circuitikz}[scale=0.7] \draw
				(0,0) to[battery, l=6<\volt>] (4,0)
				to[resistor, l=2<\volt>] (4,4)
				to[capacitor, l=4<\volt>] (0,4) -- (0,0);
			\end{circuitikz}
			\captionof{figure}{Some time after charging}}

		{\begin{circuitikz}[scale=0.7] \draw
				(0,0) to[battery, l=6<\volt>] (4,0)
				to[resistor, l=0<\volt>] (4,4)
				to[capacitor, l=6<\volt>] (0,4) -- (0,0);
			\end{circuitikz}
			\captionof{figure}{Fully charged capacitor}}

	\end{multicols}
	\section{Charge of capacitor at any given second: }
	\begin{equation}
 		Q=It\tag{\si{\coulomb}}
	\end{equation}

	\section{Dis/charging time formula: }
\begin{equation}
		\tau = RC\tag{\si{\second}}
\end{equation}

	\section{Charging graphs: }

	\begin{multicols}{3}
		\begin{tikzpicture}[scale=0.7]
			\begin{axis}[
				x=1cm,y=1cm,
				axis lines=middle,
				ymajorgrids=true,
				xmajorgrids=true,
				xmin=0,
				xmax=7,
				ymin=0,
				x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
				y label style={at={(axis description cs:-0.1,0.5)},rotate=90,anchor=south},
				xlabel=$t$ time,
				ylabel=$q$ charge,
				ymax=7,
				xtick={0,1,...,6},
				ytick={0,1,...,6},]
			\end{axis}
			\draw[domain=0:7,samples=1000] plot ({\x},{(6*(1-(e^(-\x))))});
		\end{tikzpicture}

		\begin{tikzpicture}[scale=0.7]
			\begin{axis}[
				x=1cm,y=1cm,
				axis lines=middle,
				ymajorgrids=true,
				xmajorgrids=true,
				xmin=0,
				xmax=7,
				ymin=0,
				x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
				y label style={at={(axis description cs:-0.1,0.5)},rotate=90,anchor=south},
				xlabel=$t$ time,
				ylabel=$v$ volts,
				ymax=7,
				xtick={0,1,...,6},
				ytick={0,1,...,6},]
			\end{axis}
			\draw[domain=0:7,samples=1000] plot ({\x},{(6*(1-(e^(-\x))))});
		\end{tikzpicture}

		\begin{tikzpicture}[scale=0.7]
			\begin{axis}[
				x=1cm,y=1cm,
				axis lines=middle,
				ymajorgrids=true,
				xmajorgrids=true,
				xmin=0,
				xmax=7,
				ymin=0,
				x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
				y label style={at={(axis description cs:-0.1,0.5)},rotate=90,anchor=south},
				xlabel=$t$ time,
				ylabel=$q$ charge,
				ymax=7,
				xtick={0,1,...,6},
				ytick={0,1,...,6},]
			\end{axis}
			\draw[domain=0:7,samples=1000] plot ({\x},{(6*(e^(-0.8*\x)))});
		\end{tikzpicture}

	\end{multicols}

	\section{Discharging graphs: }

	\begin{multicols}{3}
		{\begin{tikzpicture}[scale=0.7]
				\begin{axis}[
					x=1cm,y=1cm,
					axis lines=middle,
					ymajorgrids=true,
					xmajorgrids=true,
					xmin=0,
					xmax=7,
					ymin=0,
					x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
					y label style={at={(axis description cs:-0.1,0.5)},rotate=90,anchor=south},
					xlabel=$t$ time,
					ylabel=$q$ charge,
					ymax=7,
					xtick={0,1,...,6},
					ytick={0,1,...,6},]
				\end{axis}
				\draw[domain=0:7,samples=1000] plot ({\x},{(6*(e^(-0.8*\x)))});
			\end{tikzpicture}

			\begin{center}
				\captionof*{figure}{\qquad \qquad Gradient: $\frac{\delta q}{\delta t} = I $}
		\end{center}}

		{\begin{tikzpicture}[scale=0.7]
				\begin{axis}[
					x=1cm,y=1cm,
					axis lines=middle,
					ymajorgrids=true,
					xmajorgrids=true,
					xmin=0,
					xmax=7,
					ymin=0,
					x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
					y label style={at={(axis description cs:-0.1,0.5)},rotate=90,anchor=south},
					xlabel=$t$ time,
					ylabel=$v$ volts,
					ymax=7,
					xtick={0,1,...,6},
					ytick={0,1,...,6},]
				\end{axis}
				\draw[domain=0:7,samples=1000] plot ({\x},{(6*(e^(-0.8*\x)))});
			\end{tikzpicture}
			\begin{center}
				\captionof*{figure}{\qquad \qquad Gradient: $\frac{\delta V}{\delta t}$}
		\end{center}}

		{\begin{tikzpicture}[scale=0.7]
				\begin{axis}[
					x=1cm,y=1cm,
					axis lines=middle,
					ymajorgrids=true,
					xmajorgrids=true,
					xmin=0,
					xmax=7,
					ymin=-7,
					x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
					y label style={at={(axis description cs:-0.1,0.5)},rotate=90,anchor=south},
					xlabel=$t$ time,
					ylabel=$q$ charge,
					ymax=0,
					xtick={0,1,...,6},
					ytick={0,-1,...,-7},]
				\end{axis}
				\draw[domain=0:7,samples=1000] plot ({\x},{1+(6*(1-(e^(-\x))))});
			\end{tikzpicture}
			\begin{center}
				\captionof*{figure}{\qquad \qquad Gradient: $\frac{\delta I}{\delta t}$}
		\end{center}}
	\end{multicols}

	\section{Dis/charging capacitor circuit: }

\begin{multicols}{2}
		\begin{center}
		{\begin{circuitikz}
				\draw (0,0) to  (0,-3) -- (8,-3)  -- (8,-1)  to [short, *-, l^=B] (8,-1);
				\draw (0,0) -- (0,2) to [battery] (8,2) -- (8,1) to [short, *-, l^=A] (8,1);
				\draw (0,0) to  [capacitor] (4,0) to [vR] (7 ,0) to [short, *-] (8,0);
				\draw (0,0) to (1,0) to  (1,-1.5) [voltmeter] (1,-1.5) to (3,-1.5) -- (3,0);
			\end{circuitikz}}
	\end{center}\begin{center}
	~\\~\\~\\~\\
		A: Charging at 63\% every $\tau$.\\
  		B: Discharging at 37\% every $\tau$.
\end{center}
\end{multicols}

\end{document}

Physics/8. Electrostatics and Capacitors/Capacitors/capacitors.tex


%
%==> Section 1: What is TikZ?
%
\section{
  What is Ti$k$Z?
}

%
%==> What is TikZ
%
\begin{frame}
  \frametitle{
    What is Ti$k$Z?
  }
  \begin{enumerate}
  \item
    A recursive acronym for \textcolor{blue}{ \tt Ti$k$Z ist kein Zeichenprogramm} (German for ``Ti$k$Z is not a drawing program").
  \item
    More specifically, Ti$k$Z is a package for creating pictures.
  \item
    Create anything from rectangles, circles to Koch snowflakes, 3D graphs and animations.
  \item
    Ti$k$Z works very well with Beamer (they were written by the same person!). Muck thanks and gratitude to Till Tantau.
  \end{enumerate}

\end{frame}

%
%==> An image by Ben Cot\'e
%
\begin{frame}
  \frametitle{
    An image by Ben Cot\'e
}

  {
    \tiny
    \begin{center}
      \input{./tex/src/image_by_ben.tex}
    \end{center}
  }

\end{frame}

%
%==> Ben Cot\'e's source code
%
\begin{frame}[fragile]
  \frametitle{
    Ben Cot\'e's source code
  }

  {
    \tiny
    \lstinputlisting{./tex/src/image_by_ben.tex}
  }

\end{frame}

%
%==> Preliminaries
%
\begin{frame}[fragile]
  \frametitle{
    Preliminaries
  }

  \begin{enumerate}
  \item
    You need to add

    \begin{lstlisting}
      \usepackage{tikz}
    \end{lstlisting}

    to your document preamble. Other commands to put in preamble will be discussed later.
  \item
    When creating a picture use the {\tt tikzpicture} environment. E.g.

  \begin{lstlisting}
    \begin{tikzpicture}
      %
      %... code
      %
    \end{tikzpicture}
  \end{lstlisting}
  \end{enumerate}

\end{frame}


\documentclass[../main]{subfiles}
\begin{document}

\chapter{Introduction to Cryptography}

\section{Secret key cryptography}
We will see a first example of encryption scheme where only the sender (Alice) and the addressee (Bob) can read the plaintext with a shared key. An attacker $A$ can read the ciphertext inside the channel.
\begin{figure}[h]
    \centering
    \includegraphics[width=0.5\textwidth]{images/secret_key_cryptography.png}
    \caption{Symmetric encryption scheme.}
\end{figure}
\newline
In general, an encryption scheme is defined with a triple of algorithms $(Gen, Enc, Dec)$ where:
$$Gen: 1 \rightarrow{} \mathcal{K} \quad\quad Enc: \mathcal{M} \times \mathcal{K} \rightarrow{} \mathcal{C} \quad\quad Dec: \mathcal{C} \times \mathcal{K} \rightarrow{} \mathcal{M}$$
We can assume that a scheme is correct when $Dec(Enc(m, k), k) = m$.
%----------------------------------------------------------------------------------
\section{Kerchoff’s principle}
We must assume that in every scenario the attacker $A$ know the encryption scheme and all its components, the only thing that he doesn't know is the key $k$.
We should also remember that the knowledge of $A$ can change over time and that changing the key is easy meanwhile changing the encryption scheme is much more complicated.
%----------------------------------------------------------------------------------
\section{Possible attack scenarios}
The adversary knows how the encryption scheme is defined but we are interesting in how he performs its attack and how it can act in the channel. We will study four possible attack scenarios:
\begin{itemize}
    \item \textit{Ciphertext-only attack}, the adversary $A$ knows only a certain number of cryptograms $c_1, \dotsc, c_m$ so he interferes with the communication in a passive way.
    \item \textit{Known-Plaintext Attack}, the adversary $A$ knows a certain number of pairs $(m_1, c_1), \dotsc{},(m_k, c_k)$, where $c_i$ is the cryptogram corresponding to $m_i$, it is a passive attack.
    \item \textit{Chosen-Plaintext Attack}, the adversary $A$ begins to play an active role, he can compute $Enc_k(m) = Enc(m, k)$ for messages of his own choice.
    \item \textit{Chosen-Ciphertext Attack}, the adversary $A$ participates in an even more active way in the communication, having access to an “oracle” $Dec_k(\cdot{})$ for decryption but obviously without having access to $k$.
\end{itemize}

\end{document}


\documentclass[titlepage]{article}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{color}
\usepackage[dvipsnames]{xcolor}
\usepackage{gensymb}
\usepackage[linguistics]{forest}
\usepackage{amsmath}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{mindmap}
\pagestyle{empty}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\definecolor{magenta}{HTML}{F10088}
\definecolor{plum}{HTML}{95298A}
\definecolor{tan}{HTML}{DC9C7B}
\definecolor{aquamarine}{HTML}{00B5BC}
\makeatletter
\setlength{\@fptop}{0pt}
\makeatother

\makeatletter
\newcommand*{\rom}[1]{\expandafter\@slowromancap\romannumeral #1@}
\makeatother

\lstset{frame=tb,
  language=Java,
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\small\ttfamily},
  numbers=none,
  numberstyle=\tiny\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=3
}

\title{ECE408 - Applied Parallel Programming \\ Final Report}
\author{Neil Singh (ngsingh2) \\ Patrick McMahon (pfmcmah2) \\ Matthew Krikorian (krikorn2)}

\begin{document}

\maketitle

\section*{Milestone 1}
Milestone 1 of this project was mostly setting up our test environment and preparing it to start developing the later parts of this project.

\subsection*{Running Time of m1.1.py}
For m1.1.py, we had an accuracy of 0.8673, and an elapsed running time of 9.76 seconds.

\subsection*{Running Time of m1.2.py}
For m1.2.py, we had an accuracy of 0.8673, and an elapsed running time of 3.19 seconds.

\subsection*{Most Time Consuming Kernels}
Some of the most time consuming kernels when running the nvprof profile were the\textit{implicit\_convolve\_sgemm} kernel, taking up 36.96\% of the execution time, the \textit{activation\_fw\_4d\_kernel} kernel, taking up 14.33\% of the execution time, and the \textit{pooling\_fw\_4d\_kernel} kernel, which took up 10.69\% of the programs execution time.

\newpage

\section*{Milestone 2}
In this milestone of the project, we were tasked with writing sequential convolution code. Using the skeleton from our textbook, and the corresponding data accesses, we successfully implemented the function below.

\begin{figure}[h!]
\begin{lstlisting}[language=C++]
{
    const int B = x.shape_[0];
    const int M = y.shape_[1];
    const int C = x.shape_[1];
    const int H = x.shape_[2];
    const int W = x.shape_[3];
    const int K = k.shape_[3];

    int H_out = H - K + 1;
    int W_out = W - K + 1;

    for (int b = 0; b < B; ++b)
    {
      for (int m = 0; m < M; m++)
      {
        for (int h = 0; h < H_out; h++)
        {
          for (int w = 0; w < W_out; w++)
          {
            y[b][m][h][w] = 0;

            for (int c = 0; c < C; c++)
            {
              for (int p = 0; p < K; p++)
              {
                for (int q = 0; q < K; q++)
                {
                  y[b][m][h][w] += x[b][c][h + p][w + q] * k[m][c][p][q];
                }
              }
            }
          }
        }
      }
    }
}
\end{lstlisting}
\caption{Our CPU implementation of the convolutional layer in MxNet}
\end{figure}

\newpage

\subsection*{Our Results For Respective Models}

\begin{figure}[h!]
\includegraphics[width=\linewidth]{ece-408-high-10000.png}
\caption{Accuracy and elapsed time for high correctness and sample size 10000}
\label{fig:flowFree}
\end{figure}

\begin{figure}[h!]
\includegraphics[width=\linewidth]{ece-408-low-10000.png}
\caption{Accuracy and elapsed time for low correctness and sample size 10000}
\label{fig:flowFree}
\end{figure}

\newpage
\section*{Milestone 3}
In this milestone we were tasked with writing a baseline paralllel convolution kernel that had to pass several baseline tests. This implementation is very sub-optimal, as it uses a TILE\_WIDTH of 1.
\begin{figure}[h!]
\begin{lstlisting}[language=C++]
{
    const int H_out = H - K + 1;
    const int W_out = W - K + 1;

    #define y4d(i3,i2,i1,i0) y[(i3) * (M * H_out * W_out) + (i2)*(H_out * W_out) + (i1)*(W_out) + i0]
    #define x4d(i3,i2,i1,i0) x[(i3) * (C * H * W) + (i2)*(H * W) + (i1)*(W) + i0]
    #define k4d(i3,i2,i1,i0) k[(i3) * (C * K * K) + (i2)*(K * K) + (i1)*(K) + i0]

    int W_grid = ceil(W_out / (float)TILE_WIDTH);
    int H_grid = ceil(H_out / (float)TILE_WIDTH);
    int n, m, h, w, c, p, q;
    n = blockIdx.x;
    m = blockIdx.y;
    h = blockIdx.z / W_grid + threadIdx.y;
    w = blockIdx.z % W_grid + threadIdx.x;

    float acc = 0;
    for (c = 0; c < C; c++) {
        for (p = 0; p < K; p++) {
            for (q = 0; q < K; q++) {
                if (h+p < H && w+q < W)
                    acc += x4d(n, c, h+p, w+q) * k4d(m, c, p, q);
            }
        }
    }
    y4d(n, m, h, w) = acc;

    #undef y4d
    #undef x4d
    #undef k4d
}
\end{lstlisting}
\caption{Our GPU implementation of the convolutional layer in MxNet}
\end{figure}

\newpage
\subsection*{Nvprof GPU Profile}
\begin{figure}[h!]
\includegraphics[width=\linewidth]{nvprof.png}
\caption{Nvprof GPU profile for our forward kernel}
\label{fig:flowFree}
\end{figure}

\newpage

\section*{Optimized Layer}

\subsection*{Initial Optimization}
We knew right off the bat that the first thing we were going to need to optimize was the way our implementation handled tiling. Our current implementation was giving us a slightly incorrect accuracy for any TILE\_SIZE above 1, so we first had to correct that to speedup our kernel. Below is the corrected kernel from part 2, which led to an average Op time of about 450ms instead of the 14s we got with our implementation where TILE\_SIZE = 1. This new implementation had TILE\_SIZE = 8. This was due in part to the correction of all the floating point operations we had.

\begin{figure}[h!]
\begin{lstlisting}[language=C++]
{
    int W_grid = (int) ceil(W_out / TILE_WIDTH * 1.0);
//  int H_grid = ceil(H_out / TILE_WIDTH * 1.0);
    int n, m, h, w, c, p, q;
    n = blockIdx.x;
    m = blockIdx.y;
    h = (blockIdx.z / W_grid) * TILE_WIDTH + threadIdx.y;
    w = (blockIdx.z % W_grid) * TILE_WIDTH + threadIdx.x;
    float acc = 0.0;
    for (c = 0; c < C; c++) {
      for (p = 0; p < K; p++) {
        for (q = 0; q < K; q++) {
//        if (h+p < H && w+q < W)
            acc += x4d(n, c, h+p, w+q) * k4d(m, c, p, q);
          }
        }
    }
    y4d(n, m, h, w) = acc;}
\end{lstlisting}
\caption{Our first improved GPU implementation with TILE\_SIZE = 8}
\end{figure}

\newpage
\subsection*{Further Optimization}
After we made this initial optimization, we knew that a running time of about 450ms was still too poor to really say we made any real optimizations to the kernel. After this point, we chose to optimize the shared memory and tiling implementation of our kernel, to improve on the DRAM accesses our code was making. Rather than taking variables from shared memory, our implementation was making calls to global memory every time it wanted to make an operation, which is highly inefficient. To correct this, we allocated shared memory for both the inputs and the weight matrix, to allow for much faster accesses to speed up our implementation. In our new implementation, we load the filter W into shared memory, then all the threads work together to load the input into a shared memory array. Then part of the sum is computed and put into Y, and then the calculation moves onto the next channel where the threads perform the next subset of convolution. We thought this optimization would be fruitful because we were now going to use shared memory to handle our operations, which would severely decrease our running time by reducing the latency to global memory accesses. With this new and improved implementation, we were able to reduce our running time all the way down to 151ms (200ms when we run Nvprof). Below is our implementation.

\newpage

\subsection*{Optimization Code}
\begin{figure}[h!]
\begin{lstlisting}[language=C++]
for(c = 0; c < C; c++)
    {
      if((h0 < K) && (w0 < K))
      {
        W_shared[h0*K+w0] = k4d(m, c, h0, w0);
      }

      //__syncthreads();

      int itemp = h0temp;
      for(i = h; i < h_base + X_tile_width; i += TILE_WIDTH, itemp += x_tile_tile_width)
      {
        for(j = w; j < w_base + X_tile_width; j += TILE_WIDTH)
        {
            X_shared[itemp + (j - w_base)] = x4d(n, c, i, j);
        }
      }

      __syncthreads();

      int ptemp = h0temp;
      for(p = 0; p < k2; p += K, ptemp += X_tile_width)
      {
        for(q = 0; q < K; q++)
        {
            acc += X_shared[ptemp + (w0 + q)] * W_shared[p+q];
        }
      }
      __syncthreads();

    }

y4d(n, m, h, w) = acc;
}
\end{lstlisting}
\caption{Our second improved GPU implementation with TILE\_SIZE = 8}
\end{figure}

\newpage

\subsection*{Nvprof GPU profile of Optimized Kernel}
\begin{figure}[h!]
\includegraphics[width=\linewidth]{rai2.png}
\caption{Nvprof GPU profile for our optimized forward kernel}
\label{fig:flowFree}
\end{figure}

\newpage
\subsection*{Summary}
\noindent
From the profiler, we can see a heavy increase in runtime speed from the running time of our previous implementation in milestone 3, showing that the shared memory and tiling improvements we implemented were truly worthwhile, and effective in making our forward convolution kernel much faster than it previously was. This implementation was influenced by the implementation in the textbook, which served as a great model for implementing a tiling kernel that utilized the benefits of shared memory to achieve peak performance.

\section*{Team Contributions}
We all met up together and finished the sequential code for the convolutional layer in person. We also met up and wrote the parallel code together after
meeting up in person, as well as crafting this report together for submission. Then for the final optimizations, we met up in person to speak about the implementation we were going to pursue, and used an online repository to assist each other in the implementation of our final kernel. We then documented our respective contributions in the report.

\end{document}