\documentclass{memoir}
\usepackage{notestemplate}

%\logo{~/School-Work/Auxiliary-Files/resources/png/logo.png}
%\institute{Rice University}
%\faculty{Faculty of Whatever Sciences}
%\department{Department of Mathematics}
%\title{Class Notes}
%\subtitle{Based on MATH xxx}
%\author{\textit{Author}\\Gabriel \textsc{Gress}}
%\supervisor{Linus \textsc{Torvalds}}
%\context{Well, I was bored...}
%\date{\today}

\begin{document}

% \maketitle

% Notes taken on ??

Recall the definition of a field.
\begin{defn}
	A \textbf{field} is a commutative ring \(F\) with multiplicative identity \(1_F\) in which every nonzero element has a multiplicative inverse.
\end{defn}
Furthermore, recall that the \textbf{characteristic} of a field \(F\), denoted \(\textrm{char}(F)\), is the smallest positive integer \(n\) such that
\begin{align*}
	1_F + 1_F + \ldots_n + 1_F = 0_F
\end{align*}
if such an \(n \in \N\) exists. Otherwise, we say that \(\textrm{char}(F)=0\).

\begin{prop}
	For a field \(F\), we have that \(\textrm{char}(F)=0\) or \(\textrm{char}(F) = p\) for a prime integer \(p\). If \(\textrm{char}(F) = p\), then \(p\cdot \alpha  = \alpha + \ldots_p + \alpha  = 0_F\) for all \(\alpha  \in F\).
\end{prop}
We often refer to fields with prime characteristics as \textbf{fields of positive characteristic}.\\

Some fields of characteristic zero include \(\Q\), \(\R\), and \(\C\). Any field of the form \(\Z / p\Z := \mathbb{F}_p\) is a field of characteristic \(p\).

\section{Subfields}
\label{sec:subfields}

\begin{defn}
	A \textbf{subfield} of a field \(F\) is a nonempty subset \(S\) containing \(1_F\) that is a subring under the addition and multiplication of \(F\), and so that \(S\) is closed under taking multiplicative inverse.\\

	The \textbf{prime subfield} of a field \(F\) is the subfield generated by the multiplicative identity \(1_F\) of \(F\), that is, it is the smallest subfield of \(F\) containing \(1_F\).
\end{defn}

\begin{prop}
	The prime subfield of a field \(F\) is either \(\Q\) if \(\textrm{char}(F) = 0\), or \(\mathbb{F}_p\) if \(\textrm{char}(F) = p\).
\end{prop}

\begin{defn}
	A \textbf{homomorphism \(\Phi:F_1\to F_2\) between fields \(F_1\) and \(F_2\)} is a unital ring homomorphism: \(\forall x,y \in F_1\)
	\begin{align*}
		\varphi (x+y) = \varphi (x) + \varphi (y)\\
		\varphi (xy) = \varphi (x) \varphi (y), \quad \varphi (1_{F_1}) = 1_{F_2}
	\end{align*}
\end{defn}
Notice that either \(F \cong \textrm{Im}(\varphi )\) or \(0 \cong \textrm{Im}(\varphi )\). This follows from the fact that the only ideals of \(F\) are \(0\) and \(F\).

A lot of fields are better viewed via a ring homomorphism. We can quotient out a ring \(R\) by any maximal ideal \(I\) of \(R\) to get an object isomorphic to a field.
\begin{exmp}
	Consider the principal ideal domain \(\Q[x]\). For any irreducible polynomial \(p(x)\), we have that
	\begin{align*}
		\Q[x] / (p(x))
	\end{align*}
	is a field, where \((p(x))\) denotes the root of \(p(x)\). We can in fact see that this space is equivalent to \(\Q\) but including the roots of \(x^2-2\), namely \(\sqrt{2} \). One can construct a unital isomorphism so that
	\begin{align*}
		\Q[x] / (x^2-2) \cong \Q(\sqrt{2} )
	\end{align*}
\end{exmp}

\end{document}

Abstract Algebra - Introductory/Algebra II/Notes/source/Lecture14 - IntroField.tex


\documentclass[11pt,a4paper,fleqn]{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[colorlinks=true, citecolor=blue, linkcolor=blue, filecolor=blue,urlcolor=blue]{hyperref}
\hypersetup{
     colorlinks   = true,
     citecolor    = gray
}
\usepackage{wrapfig}

\usepackage{caption}
\captionsetup{format=plain, indent=5pt, font=footnotesize, labelfont=bf}

\setkomafont{disposition}{\scshape\bfseries}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{bbm}
\usepackage[table,xcdraw]{xcolor}
\usepackage{mathtools}
\usepackage{fontawesome}
% \usepackage{epsfig}
% \usepackage{grffile}
%\usepackage{times}
%\usepackage{babel}
\usepackage{tikz}
\usepackage{paralist}
\usepackage{color}
\usepackage[top=3cm, bottom=2.5cm, left=2.5cm, right=3cm]{geometry}
%\setlength{\mathindent}{1ex}

% PGF
\usepackage{pgfplots}
\usepackage{pgf}
\usepackage{siunitx}
\usepackage{xfrac}
\usepackage{calculator}
\usepackage{calculus}
\usepackage{eurosym}
\usepackage{booktabs}
%\sisetup{per-mode=fraction,%
%	fraction-function=\sfrac}

%\newcommand{\eur}[1]{\EUR{#1}\si{\per\kilo\meter}}
\pgfplotsset{
  compat=newest,
  every axis/.append style={small, minor tick num=3}
}

%\usepackage[backend=biber,style=alphabetic,url=false,doi=false]{biblatex}
%\addbibresource{sheet01_biber.bib}
% \addbibresource{/home/coroa/papers/refs.bib}

\newcommand{\id}{\mathbbm{1}}
\newcommand{\NN}{{\mathbbm{N}}}
\newcommand{\ZZ}{{\mathbbm{Z}}}
\newcommand{\RR}{{\mathbbm{R}}}
\newcommand{\CC}{{\mathbbm{C}}}
\renewcommand{\vec}[1]{{\boldsymbol{#1}}}

\renewcommand{\i}{\mathrm{i}}

\newcommand{\expect}[1]{\langle\,#1\,\rangle}
\newcommand{\e}[1]{\ensuremath{\,\mathrm{#1}}}

\renewcommand{\O}{\mc{O}}
\newcommand{\veps}{\varepsilon}
\newcommand{\ud}[1]{\textup{d}#1\,}

\newcommand{\unclear}[1]{\color{green}#1}
\newcommand{\problem}[1]{\color{red}#1}
\newcommand{\rd}[1]{\num[round-mode=places,round-precision=1]{#1}}

%\DeclareSIUnit{\euro}{\EUR}
\DeclareSIUnit{\dollar}{\$}
\newcommand{\eur}{\text{\EUR{}}}

\usepackage{palatino}
\usepackage{mathpazo}
\setlength\parindent{0pt}
\usepackage{xcolor}
\usepackage{framed}
\definecolor{shadecolor}{rgb}{.9,.9,.9}

%=====================================================================
%=====================================================================
\begin{document}

\begin{flushright}
 \textbf{Energy System Modelling }\\
 {\small Karlsruhe Institute of Technology}\\
 {\small Institute for Automation and Applied Informatics}\\
 {\small Summer Term 2020}\\
\end{flushright}

 \vspace{-0.5em}
 \hrulefill
 \vspace{0.3em}

\begin{center}
 \textbf{\textsc{\Large Tutorial V: Investment and Large Power Systems}}\\
 \small Will be worked on in the exercise session on Friday, 26 June 2020.\\[1.5em]
\end{center}

\vspace{-0.5em}
\hrulefill
\vspace{0.8em}

\vspace{1em}

%=============== ======================================================
\paragraph{Problem V.1 (analytical) -- investment in generators and transmission lines \faGroup}~\\
%=====================================================================

Two generators are connected to the grid by a single transmission
line (with no electrical demand on their side of the transmission line). A single company owns both the generators and the transmission line. Generator 1 has a linear cost curve $C_1(g_1) = 5 g_1$ [\euro/h] and a capacity of 300~MW and Generator 2 has a linear cost curve $C_2(g_2) = 10 g_2$ [\euro/h] and a capacity of 900~MW. The transmission line has a capacity $K$ of 1000~MW. Suppose the demand in the grid is always high enough to absorb the
generation from the two generators and that the market price of
electricity $\pi$ is never below 15 \euro/MWh and averages 20
\euro/MWh.

\begin{enumerate}[(a)]
 \item Determine the full set of equations (objective function and
       constraints) for the generators to optimise their dispatch to
       maximise total economic welfare.
 \item What is the optimal dispatch?
 \item What are the values of the KKT multipliers for all the constraints in terms of $\pi$?
 \item A new turbo-boosting technology can increase the capacity of Generator 1 from 300~MW to 350~MW.  At what annualised capital cost would it be efficient to invest in this new technology?
 \item A new high temperature conductor technology can increase the capacity of the transmission line by 200~MW. At what annualised capital cost would it be efficient to invest in this new technology?
\end{enumerate}

\newpage
%=============== ======================================================
\paragraph{Problem V.2 (anal./prog.) -- duration curves and generation investment \faGroup}~\\
%=====================================================================

Let us suppose that demand is inelastic. The demand-duration curve is given by $D=1000-1000z$, where $z\in [0,1]$ represents the probability of time the load spends above a certain value. Suppose that there is a choice between coal and gas generation plants with a variable cost of 2 and 12~\euro/MWh, together with load-shedding at 1012\euro/MWh. The fixed costs of coal and gas generation are 15 and 10~\euro/MWh, respectively.

\begin{enumerate}[(a)]
 \item Describe the concept of a screening curve and how it helps to determine generation investment, given a demand-duration curve.
 \item Plot the screening curve and find the intersections of the generation technologies.
 \item Compute the long-term equilibrium power plant investment (optimal mix of generation) using PyPSA.
 \item Plot the resulting price duration curve and the generation dispatch. Comment!
 \item Demonstrate that the zero-profit condition is fulfilled.
 \item While it can be shown that generators recover their cost in theory, name reasons why this might not be the case in reality.
\end{enumerate}

\newpage

%=============== ======================================================
\paragraph{Problem V.3 (programming) -- synthetic fuels \faHome}~\\
Diesel can be produced via Fischer-Tropsch process from hydrogen and carbon dioxide. The necessary CO$_2$ can be captured from the atmosphere with direct air capture (DAC) and than be stored. Model the production of diesel via Fischer-Tropsch-process in PyPSA. You can find a notebook filled out partially in your tutorial repository and on Github.
\begin{enumerate}[(a)]
	\item  read the demand data (given in MWh) for electricity and transport. Plot the time series. How do you explain the shape of the transport demand?
	\item What is the meaning of \texttt{e\_cyclic}, \texttt{e\_inital} and \texttt{e\_min\_pu} for the component \texttt{store} in the PyPSA syntax?
	\item Complete the jupyter notebook to build a PyPSA model including the transport and electricity demand. Set as snapshots the given demand time period. Add a bus and a store for hydrogen, gas and CO$_2$. Electricity can be generated with
an Open-Cycle-gas turbine (OCGT) and for transport diesel cars are used. Assume that the amount of emitted CO$_2$ to the atmosphere is equal to the amount of used gas in the OCGT/ used diesel in the car. The whole required diesel should be also produced via Fischer-Tropsch during the investigated period. Why do we need to track the CO$_2$ emissions for this problem?
	\item Run an investment optimisation of the problem
		\begin{enumerate}[(i)]
		\item Show that the total amount of carbon stays constant in every time step
		\item Plot the energy difference in the stores
		\item What are the total system costs?
		\end{enumerate}
	\item The allowed amount of CO$_2$ in the atmosphere should be zero in the last time step. Add this as an extra constraint to the model and run again the investment optimisation. How do the total system costs change? How much CO2 storage capacity is needed?
	\item Add electric cars to the model and run again an investment optimisation. How large is the share of electric cars to satisfy the transport demand?
	\item Add a wind generator with the given time series in the data folder. Optimise the investment. How large is the share of electric cars now? How do you explain the result?
\end{enumerate}

% Please add the following required packages to your document preamble:
% \usepackage[table,xcdraw]{xcolor}
% If you use beamer only pass "xcolor=table" option, i.e. \documentclass[xcolor=table]{beamer}
\begin{table}[]
	\begin{tabular}{llllll}
		\hline
		\rowcolor[HTML]{C0C0C0}
		\textbf{technology} & \textbf{\begin{tabular}[c]{@{}l@{}}capital cost\\ {[}EUR/MW{]}\end{tabular}} & \textbf{\begin{tabular}[c]{@{}l@{}}marginal cost\\ {[}EUR/MWh{]}\end{tabular}} & \textbf{\begin{tabular}[c]{@{}l@{}}efficiency\\ {[}per unit{]}\end{tabular}} & \textbf{\begin{tabular}[c]{@{}l@{}}energy \\ in the storage at the beginning\\ {[}TWh{]}\end{tabular}} & \textbf{\begin{tabular}[c]{@{}l@{}}optimise\\ capacity\end{tabular}} \\ \hline
		onwind              & 1500                                                                         & 0                                                                              & -                                                                            & -                                                                                & yes                                                                  \\ \hline
		\rowcolor[HTML]{EFEFEF}
		Fischer-Tropsch     & 145                                                                          & -                                                                              & 1                                                                            & -                                                                                & yes                                                                  \\ \hline
		gas storage         & -                                                                            & 23                                                                             & -                                                                            & 6                                                                                & no                                                                   \\ \hline
		\rowcolor[HTML]{EFEFEF}
		H2 storage          & -                                                                            & 213                                                                            & -                                                                            & 1                                                                                & no                                                                   \\ \hline
		CO2 storage         & 20                                                                           & -                                                                              & -                                                                            & -                                                                                & yes                                                                  \\ \hline
		\rowcolor[HTML]{EFEFEF}
		electric car        & -                                                                            & -                                                                              & 0.9                                                                          & -                                                                                & yes                                                                  \\ \hline
		DAC                 & -                                                                            & 100                                                                            & 1                                                                            & -                                                                                & yes                                                                  \\ \hline
		\rowcolor[HTML]{EFEFEF}
		diesel car          & -                                                                            & -                                                                              & 0.35                                                                         & -                                                                                & yes                                                                  \\ \hline
		OCGT                & 2000                                                                         & 2                                                                              & 0.5                                                                          & -                                                                                & yes                                                                  \\ \hline
	\end{tabular}
\end{table}
%=====================================================================
%=============== ======================================================
%\paragraph{Problem V.4 \normalsize (network clustering).}~\\
%=====================================================================

\end{document}


\documentclass[12pt]{amsart}

\usepackage[margin=.8in]{geometry}

\usepackage{amsmath}

\usepackage{enumerate}
\usepackage{todonotes}
\usepackage{hyperref}
\newtheorem{thm}{Theorem}
\newtheorem{cor}{Corollary}
%\newtheorem{rmk}[thm]{Remark}
\newenvironment{sol}{\paragraph{{\bf Solution:}}}{\hfill$\square$}
\newenvironment{act}{\paragraph{{\bf Activity:}}}{\hfill$\square$}
\usepackage{bm}

\theoremstyle{definition}
\newtheorem*{defn}{Definition}
\newtheorem*{ex}{Example}
\newtheorem*{nonex}{Non-example}
\newtheorem*{rmk}{Remark}

\usepackage{mathtools}

\usepackage{tikz}

\newcommand{\Q}{\mathbb{Q}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\Z}{\mathbb{Z}}

\title{What to Expect for Final Exam
}

\begin{document}
MTH 210 - Winter 2020 \hfill Dr. Keough\\

\maketitle

\vspace{-.3in}

The purpose of the final exam is to give you an opportunity to demonstrate all that you have learned in the course.

\section{Grading}
The final exam is out of 100 points. There are 10 questions worth 10 points each. To give you some idea of what points mean, consider an ``S" on a quiz to be at least an 8 out of 10. If you score an 85 or better, a $+$ will be added to your final grade. If you score a 50 or less on the final exam a $-$ will be added to your grade. You also can add a $+$ to your grade if you complete the proof portfolio at the next highest level.\footnote{If you get two +'s your grade goes up by 2/3 of a letter grade, that is, we'll say a ``B$++$" is an A$-$. Also $+$/$-$'s cancel so a ``B$+-$" is a B.} There are no revisions on the final exam and limited feedback will be given. See the syllabus at \url{http://gvsu.edu/s/1kE} for how grades are computed and please ask if you are not sure.

\section{Timing and Resource Use}
Please read the following carefully:
\begin{itemize}
\item The exam will be posted on Blackboard on Monday, April 20 at 10AM. You should upload a single PDF of your answers by Wednesday, April 22, 12PM (the end of our final exam time).
\item There is no time limit on the exam, but my intention is that it will not take you more time than if we took the exam in class, so plan to spend at most 2 hours. You may want to take advantage of the take home nature of this exam by reading the exam and answering the questions you know for sure and then giving yourself time to walk away. You do not have to take the exam in one sitting.
\item The exam is open book and notes.  You may use a calculator or Desmos (or other graphing technology) if you'd like to graph something, but otherwise no technology is allowed. You should not use Blackboard or other internet resources, and no collaboration is allowed.
\end{itemize}

\section{How to Study}

While you are allowed to use your book and your notes on the exam, it's still a good idea to prepare for the exam. Here are some recommendations for how to study:
\begin{itemize}
\item Work on the given proofs in (10) - take advantage of being able to talk through those proofs with a classmate.
\item Organize your notes! Make a summary sheet for each of the 10 questions of information you'd like to be able to find easily for that question.
\item Go through the self quiz questions on Blackboard. Look through your notes for ones you can not answer and ask me if you can't find them.
\item Practice!  Look at the extra practice questions on Blackboard, try synthesis activities again (without looking at your solutions), and do exercises on worksheets.  Use the book to find extra exercises on things you are struggling with.
\item Write the test! Write yourself a practice exam that you think I would write.  Then take the practice exam.
\end{itemize}
\newpage

\section{Material}
The exam is cumulative, covering the sections we covered in Chapters 1-7 of the text. There will be one question about each of the following topics:

\begin{enumerate}
\item \emph{\bf Definitions and Notation:} Be able to know and apply the important definitions from the class, including even integer, odd integer, divides, congruence, rational number, irrational number, function, injection, surjection, bijection, relation, and equivalence relation.
\item \emph{\bf Logic:} This section includes understanding the meanings of and, or, conditional statements, and biconditional statements. You may be asked for a truth table, to determine if conditional statements are true or false based on given information, or to explain logical equivalences. You may be asked to negate a statement.
\item \emph{\bf Proof Techniques - what to assume:} This question will be similar to Section 3, Question 1 on Exam 1.
\item \emph{\bf Proof Techniques - which to use:} This question will be similar to Section 3 on Exam 2.
\item \emph{\bf Counterexamples:} You'll be given a statement, asked to disprove it, and explain why this shows the statement is false. Similar to Skill P3.
\item \emph{\bf Proof Critique:} Similar to Skill P4, and the ``proof evaluation" exercises on the synthesis. You'll be given a proof and asked to determine if it is a valid proof and for any writing guideline violations. You do not need to rewrite the proof. There's lots of examples of these, typically exercise 18 or 19 in the sections in Chapter 3, Exercise 18 in Section 4.1, Exercise 17 in 5.3, Exercise 17 in Section 6.3, Exercise 16 in Section 6.2.
\item \emph{\bf Sets:} Properly use the notation $\subset$, $\subseteq$, $\in$, $=$, $\not\subset$, $\not\subseteq$, $\notin$, $=$. Be able to read and write in both roster and set builder notation. Given sets $A$ and $B$ and a universal set $U$ be able to find $A\cap B$, $A\cup B$, $A-B$, $A^c$, $\mathcal{P}(A)$ (the power set of $A$), $A\times B$, $|A|$ (the cardinality of $A$, and combinations of these things. For sets $A$ and $B$, know how to prove $A\subseteq B$, $A\subset B$, $A=B$, $A\neq B$, and $A\cap B = \emptyset$ ($A$ and $B$ disjoint).
\item \emph{\bf Functions:} You may be asked for examples and non-examples for functions, injections, surjections, and bijections. You may be asked to use the terms domain, codomain, range, image, and preimage, and be able to use them in sentences about a given function. Know how to explain a function is an injection, surjection, or bijection, or not, using the formal definitions.
\item \emph{\bf Equivalence Relations:} You will either be given a relation and asked to determine and explain if it is reflexive, symmetric, and transitive or asked to give an example that is some combination of the properties.
\item \emph{\bf Proof:} I will ask you to prove one of the following two things:
	\begin{enumerate}
	\item Define $g:\R\times\R \to\R$ by $g(a,b) = a$. Then $g$ is a surjection and $g$ is not an injection.
	\item For $(w,x)$ and $(y,z)$ in $\Z\times \Z$ define $(w,x)\sim (y,z)$ if and only if $w\leq y$ and $x\leq z$. Then $\sim$ is reflexive and transitive, but not symmetric.
	\end{enumerate}

\end{enumerate}
\vspace{.5in}

\emph{There is lots of opportunity for partial credit here. In particular, if you are not sure how to answer the question given, please explain what you do know. On each question, for up to 7 out of 10 points on that question you can provide some evidence that you understand the topics related to that given question. As an example, if you wrote a practice test you could give that with your solution, or you could give your summary sheet.}

\end{document}


\documentclass{beamer}

\mode<presentation> {
\usetheme{Madrid}
}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{wrapfig}

\title[CNNs]{Computer Vision with CNN}

\author{Behnia - Heydari}
\institute[AUT]
{
Amirkabir University of Technology \\
\medskip
\textit{}
}
\date{May 21, 2019}

\begin{document}

\begin{frame}
\titlepage
\end{frame}

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

\section{Motivation}

\begin{frame}
\frametitle{Cameras Everywhere}
\begin{columns}[c]

\column{.4\textwidth}
\textbf{Smartphones}
\begin{itemize}
\item Exploding number of sensors vs. humans

\end{itemize}
\column{.6\textwidth}

\begin{figure}
	\includegraphics[height=180pt]{Pics/samsung.jpg}
\end{figure}
\end{columns}
\end{frame}

\begin{frame}
\frametitle{ConvNets Everywhere}
\centering
\includegraphics[width=0.9\linewidth]{Pics/ex1.PNG}
\end{frame}
\begin{frame}
\frametitle{ConvNets Everywhere}
\centering
\includegraphics[width=0.9\linewidth]{Pics/ex2.PNG}
\end{frame}
\begin{frame}
\frametitle{ConvNets Everywhere}
\centering
\includegraphics[width=0.9\linewidth]{Pics/ex3.PNG}
\end{frame}
\begin{frame}
\frametitle{ConvNets Everywhere}
\centering
\includegraphics[width=0.9\linewidth]{Pics/ex4.PNG}
\end{frame}
\begin{frame}
\frametitle{ConvNets Everywhere}
\centering
\includegraphics[width=0.9\linewidth]{Pics/ex5.PNG}
\end{frame}

\begin{frame}
	\frametitle{Neat Dataset}
	\begin{figure}
		\includegraphics[width=\linewidth]{Pics/image_net.jpeg}
	\end{figure}
\end{frame}

\section{ImageNet Challenge}
\begin{frame}
\frametitle{Role of CNN}
\begin{figure}
	\includegraphics[width=\linewidth]{Pics/imagenet_acc.png}
\end{figure}
\end{frame}

\section{From Cat's Brain to ResNet (Classification)}
\begin{frame}
\frametitle{Cat's Brain}
\begin{columns}[c]
	\column{.4\textwidth}
	\textbf{Types of cells:}
\begin{itemize}
	\item simple cells
	\item complex cells
	\item hypercomplex cells
\end{itemize}
\column{.6\textwidth}

\begin{figure}
	\includegraphics[width=\linewidth]{Pics/cats.jpg}
\end{figure}
\end{columns}
\end{frame}

\begin{frame}
\frametitle{CNN}

	\begin{figure}
		\includegraphics[width=\linewidth]{Pics/cexample.png}
	\end{figure}

\end{frame}
\begin{frame}
\frametitle{Convolution}

\begin{figure}
	\includegraphics[width=\linewidth]{Pics/cnlayer.png}
	\caption{Convolution Layer}
\end{figure}

\end{frame}

\begin{frame}
\frametitle{Complexity of Features}

\begin{figure}
	\includegraphics[width=\linewidth]{Pics/hlayer.png}
	\caption{complexity of features in each layer}
\end{figure}
\end{frame}

\begin{frame}
\frametitle{Complexity of Features}

\begin{figure}
	\includegraphics[width=.8\linewidth]{Pics/ex7.PNG}
	\caption{complexity of each layer output}
\end{figure}

\end{frame}

\begin{frame}
\frametitle{Sliding a Filter}
\begin{figure}
	\includegraphics[width=.5\linewidth]{Pics/clook.png}
	\caption{filtering example}
\end{figure}
\end{frame}

\begin{frame}
\frametitle{Sliding a Filter}
\begin{figure}
	\includegraphics[width=.5\linewidth]{Pics/clook2.png}
	\caption{filtering example}
\end{figure}
\end{frame}

\begin{frame}
\frametitle{Sliding a Filter}
\begin{figure}
	\includegraphics[width=.5\linewidth]{Pics/clook3.png}
	\caption{filtering example}
\end{figure}
\end{frame}

\begin{frame}
\frametitle{Sliding a Filter}
 {\color{red}
 What if using a 3*3 filter with stride 3 ?!
	}
\begin{figure}
	\includegraphics[width=.5\linewidth]{Pics/clook4.png}
	\caption{filtering example}
\end{figure}

\end{frame}

\begin{frame}
\frametitle{Padding}
\begin{figure}
	\includegraphics[width=.4\linewidth]{Pics/padding.png}
	\caption{Padding: control output size}
\end{figure}
\end{frame}

\begin{frame}
\frametitle{Summary}
\begin{itemize}
	\item Accept a volume of size N x N
	\item requires 4 hyper parameters:
	\begin{itemize}
		\item Filter's spatial extent F
		\item Filter's Stride S
		\item Amount of zero padding P
		\item Number of kernels ($\times depth \times$)
	\end{itemize}

     \item Produces a volume of size $M \times M$
     \begin{itemize}
     	\item M = $ \frac{(N - F + 2p)}{S} + 1 $
     	\item Number of parameters : $ (F \times F \times D + b) \times k $
     \end{itemize}
\end{itemize}

\end{frame}

\begin{frame}
\frametitle{CNN}

\begin{figure}
	\includegraphics[width=\linewidth]{Pics/cexample.png}
	\caption{Fully Connected Layer}
\end{figure}

\end{frame}

\begin{frame}
\frametitle{Pooling}

\begin{figure}
	\includegraphics[width=\linewidth]{Pics/pooling.png}
	\caption{Max-pooling}
\end{figure}

\end{frame}

\begin{frame}
\frametitle{CNN}

\begin{figure}
	\includegraphics[width=\linewidth]{Pics/cexample.png}
	\caption{Fully Connected Layer}
\end{figure}

\end{frame}

\begin{frame}
\frametitle{Comparing}

\begin{figure}
	\includegraphics[width=\linewidth]{Pics/comaprecnn.png}
	\caption{Comparing Structures}
\end{figure}

\end{frame}

\begin{frame}
\frametitle{AlexNet}

\begin{figure}
	\includegraphics[width=\linewidth]{Pics/alexnet.png}
	\caption{AlexNet architecture}
\end{figure}

\end{frame}

\begin{frame}
\frametitle{AlexNet}

\begin{figure}
	\includegraphics[width=\linewidth]{Pics/alexnetarch.jpg}
	\caption{AlexNet architecture}
\end{figure}

\end{frame}

\begin{frame}
\frametitle{Comparing}

\begin{figure}
	\includegraphics[width=\linewidth]{Pics/compare2.png}
	\caption{Comparing Structures}
\end{figure}

\end{frame}

\begin{frame}
\frametitle{VGG}
{\color{red} Deeper Networks, Smaller Filters}
\begin{figure}
	\includegraphics[width=.6\linewidth]{Pics/VGG.png}
	\caption{VGG Structures}
\end{figure}

\end{frame}
\begin{frame}
\frametitle{Comparing}

\begin{figure}
	\includegraphics[width=\linewidth]{Pics/compare2.png}
	\caption{Comparing Structures}
\end{figure}

\end{frame}

\begin{frame}[fragile]
\frametitle{GoogLeNet}
{\color{red} Make deeper networks computationally inexpensive}
\begin{figure}
	\includegraphics[height=0.3\textheight]{Pics/googlenet.png}
\end{figure}
\begin{figure}
	\includegraphics[height=0.4\textheight]{Pics/inception.jpg}
\end{figure}
\end{frame}

\begin{frame}
\frametitle{Comparing}

\begin{figure}
	\includegraphics[width=\linewidth]{Pics/com3.png}
	\caption{Comparing Structures}
\end{figure}

\end{frame}

\begin{frame}
\frametitle{ResNet}

\begin{figure}
	\includegraphics[width=\linewidth]{Pics/plain.png}
\end{figure}

\end{frame}

\begin{frame}
\frametitle{ResNet}

\begin{figure}
	\includegraphics[width=.6\linewidth]{Pics/resnet.png}
	\caption{ResNet Architecture}
\end{figure}

\end{frame}
\begin{frame}

\frametitle{Comparing}

\begin{figure}
	\includegraphics[width=\linewidth]{Pics/ccomplex.png}
	\caption{Comparison}
\end{figure}

\end{frame}

\begin{frame}
\frametitle{Example}
\begin{figure}

	\includegraphics[width=\linewidth]{Pics/cnnex.png}
\end{figure}

\end{frame}

\section{Localization}
\begin{frame}
	\frametitle{Computer Vision Tasks}

	\begin{figure}
		\includegraphics[width=\linewidth]{Pics/classloc.PNG}

	\end{figure}

\end{frame}
\begin{frame}
	\frametitle{Classification + Localization}
	\begin{figure}
		\includegraphics[width=\linewidth]{Pics/loc2.PNG}
	\end{figure}

\end{frame}
\begin{frame}
	\frametitle{Example}

	\begin{figure}
		\includegraphics[width=\linewidth]{Pics/humanpose.PNG}
		\caption{Body pose estimation}
	\end{figure}

\section{Object Detection}
\end{frame}
\begin{frame}
	\frametitle{Fancier Task}

	\begin{figure}
		\includegraphics[width=\linewidth]{Pics/detect1.PNG}

	\end{figure}

\end{frame}
\begin{frame}
	\frametitle{Object Detection}
	\begin{figure}
		\includegraphics[width=\linewidth]{Pics/objectdetect.PNG}
		\caption{{\color{red}New approach is needed}}
	\end{figure}

\end{frame}
\begin{frame}
	\frametitle{Object Detection}

	\begin{figure}
		\includegraphics[width=\linewidth]{Pics/objectdetect2.PNG}

	\end{figure}

\end{frame}
\begin{frame}
	\frametitle{RCNN}

	\begin{figure}
		\includegraphics[width=\linewidth]{Pics/rcnn.PNG}

	\end{figure}

\end{frame}
\begin{frame}
	\frametitle{Fast RCNN}

	\begin{figure}
		\includegraphics[width=\linewidth]{Pics/fastrcnn.PNG}

	\end{figure}

\end{frame}
\begin{frame}
	\frametitle{Comparing}

	\begin{figure}
		\includegraphics[width=\linewidth]{Pics/crcnn.PNG}

	\end{figure}

\end{frame}
\begin{frame}
\frametitle{Even Faster}
\begin{figure}
	\includegraphics[width=\linewidth]{Pics/FasterRCNN.png}
\end{figure}
\end{frame}

\begin{frame}
\frametitle{Even Faster}

\begin{figure}
	\includegraphics[width=\linewidth]{Pics/fastercompare.png}

\end{figure}

\end{frame}

\begin{frame}
	\frametitle{Yolo}
	\begin{figure}
		\includegraphics[width= .8\linewidth]{Pics/yolo.PNG}
	\end{figure}
\end{frame}

\begin{frame}
\frametitle{References}
	References:
	\begin{itemize}
		\item
		Krizhevsky, Alex \& Sutskever, Ilya \& E. Hinton, Geoffrey. (2012). ImageNet Classification with Deep Convolutional Neural Networks. Neural Information Processing Systems. 25. 10.1145/3065386.
		\item
		Very Deep Convolutional Networks for Large-Scale Image Recognition
		Karen Simonyan, Andrew Zisserman
		(Submitted on 4 Sep 2014 (v1), last revised 10 Apr 2015 (this version, v6))
		\item
		Going Deeper with Convolutions
		Christian Szegedy, Wei Liu, Yangqing Jia, Pierre Sermanet, Scott Reed, Dragomir Anguelov, Dumitru Erhan, Vincent Vanhoucke, Andrew Rabinovich
		(Submitted on 17 Sep 2014)
		\item Stanford CS231n 2017
	\end{itemize}
\end{frame}

\begin{frame}
\Huge
\centering
Any Question?
\end{frame}

\end{document}


\documentclass[12pt, fullpage,letterpaper]{article}

\usepackage[margin=1in]{geometry}
\usepackage{url}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{xspace}
\usepackage{graphicx}

\newcommand{\semester}{Spring 2022}
\newcommand{\assignmentId}{0}
\newcommand{\releaseDate}{10 Jan, 2022}
\newcommand{\dueDate}{11:59pm, 21 Jan, 2022}

\newcommand{\bx}{{\bf x}}
\newcommand{\bw}{{\bf w}}

\title{CS 6190: Probabilistic Machine Learning \semester}
\author{Homework \assignmentId}
\date{Handed out: \releaseDate\\
  Due: \dueDate}

\begin{document}
\maketitle

\input{emacscomm}
\footnotesize
	\begin{itemize}
		\item You are welcome to talk to other members of the class about
		the homework. I am more concerned that you understand the
		underlying concepts. However, you should write down your own
		solution. Please keep the class collaboration policy in mind.

		\item Feel free discuss the homework with the instructor or the TAs.

		\item Your written solutions should be brief and clear. You need to
		show your work, not just the final answer, but you do \emph{not}
		need to write it in gory detail. Your assignment should be {\bf no
			more than 10 pages}. Every extra page will cost a point.

		\item Handwritten solutions will not be accepted.

		\item The homework is due by \textbf{midnight of the due date}. Please submit
		the homework on Canvas.
	\end{itemize}

\section*{Warm up[100 points + 5 bonus]}
\label{sec:q1}

%independency, conditional distribution, expectation, variance, basic properties
%gradient calcualtion, logistic function, second derivatives
%
\begin{enumerate}
%\item~[5 points] We use sets to represent events. For example, toss a fair coin $5$ times, and the event can be represented by the set of ``Heads" or ``Tails" after each tossing. Let a specific event $A$ be ``at least one head after tossing $5$ times". Calculate the probability that event $A$ happens, i.e., $p(A)$.

\item~[2 points] Given two events $A$ and $B$, prove that
\begin{align}
&p(A \cup B) \le p(A) + p(B) \nonumber \\
&p(A \cap B) \le p(A), p(A \cap B) \le p(B) \nonumber
\end{align}
When does the equality hold?
\item~[2 points] Let $\{A_1, \ldots, A_n\}$ be a collection of events. Show that
\[
p(\cup_{i=1}^n A_i) \le \sum_{i=1}^n p(A_i).
\]
When does the equality hold? (Hint: induction)
%\item~[5 points] Given three events $A$, $B$ and $C$, show that
%\[
%p(A\cap B\cap C) = p(A|B\cap C)p(B|C)p(C)
%\]
\item~[14 points]  We use $\EE(\cdot)$ and $\VV(\cdot)$ to denote a random variable's mean (or expectation) and variance, respectively. Given two discrete random variables $X$ and $Y$, where $X \in \{0, 1\}$ and $Y \in \{0,1\}$. The joint probability $p(X,Y)$ is given in as follows:
\begin{table}[h]
        \centering
        \begin{tabular}{ccc}
        \hline\hline
         & $Y=0$ & $Y=1$ \\ \hline
         $X=0$ & $3/10$ & $1/10$ \\ \hline
         $X=1$  & $2/10$ & $4/10$ \\ \hline\hline
        \end{tabular}
        %\caption{Training data for the alien invasion problem.}\label{tb-alien-train}
        \end{table}

        \begin{enumerate}
            \item~[10 points] Calculate the following distributions and statistics.
            \begin{enumerate}
            \item the the marginal distributions $p(X)$ and $p(Y)$
            \item the conditional distributions $p(X|Y)$ and $p(Y|X)$
            \item $\EE(X)$, $\EE(Y)$, $\VV(X)$, $\VV(Y)$
            \item  $\EE(Y|X=0)$, $\EE(Y|X=1)$,  $\VV(Y|X=0)$, $\VV(Y|X=1)$
            \item  the covariance between $X$ and $Y$
            \end{enumerate}
            \item~[2 points] Are $X$ and $Y$ independent? Why?
            \item~[2 points] When $X$ is not assigned a specific value, are $\EE(Y|X)$ and $\VV(Y|X)$ still constant? Why?
        \end{enumerate}
\item~[9 points] Assume a random variable $X$ follows a standard normal distribution, \ie $X \sim \N(X|0, 1)$. Let $Y = e^{-X^2}$. Calculate the mean and variance of $Y$.
\begin{enumerate}
	\item $\EE(Y)$
	\item $\VV(Y)$
	\item $\text{cov}(X, Y)$
\end{enumerate}

\item~[8 points] Derive the probability density functions of the following transformed random variables.
\begin{enumerate}
	\item $X \sim \N(X|0, 1)$ and $Y = X^3$.
	%\item $\x =[x_1, x_2]^\top \sim \N(\x|\0, [1, -1/2;1/2,1])$ and $y =3 x_1 + 2x_2$.
	\item $\left[\begin{array}{c}X_1 \\ X_2\end{array}\right] \sim \N\big(\left[\begin{array}{c}X_1 \\ X_2\end{array}\right]|\left[\begin{array}{c}0 \\ 0 \end{array}\right], \left[\begin{array}{cc}1 & -1/2 \\ -1/2 & 1\end{array}\right]\big)$ and $\left[\begin{array}{c}Y_1 \\ Y_2\end{array}\right] = \left[\begin{array}{cc}1 & 1/2\\-1/3&1\end{array}\right]\left[\begin{array}{c}X_1 \\ X_2\end{array}\right]$.
\end{enumerate}

\item~[10 points]  Given two random variables $X$ and $Y$, show that
\begin{enumerate}
\item $\EE(\EE(Y|X)) = \EE(Y)$
\item
$\VV(Y) = \EE(\VV(Y|X)) + \VV(\EE(Y|X))$
\end{enumerate}
(Hints: using definition.)

%\item~[20 points]  Let us go back to the coin tossing example. Suppose we toss a coin for $n$ times, \textit{independently}. Each toss we have $\frac{1}{2}$ chance to obtain the head. Let us denote the total number of heads by $c(n)$. Derive the following statistics. You don't need to give the numerical values. You only need to provide the formula.
%\begin{enumerate}
%\item $\EE(c(1))$, $\VV(c(1))$
%\item $\EE(c(10))$, $\VV(c(10))$
%\item $\EE(c(n))$, $\VV(c(n))$
%\end{enumerate}
%What can you conclude from comparing the expectations and variances with different choices of $n$?

\item~[9 points] Given a logistic function, $f(\x) = 1/(1+\exp(-\a^\top \x))$ ($\x$ is a vector),
\begin{enumerate}
\item derive $\frac{\d f(\x)}{\d \x}$
\item derive $\frac{\d^2 f(\x)}{\d \x^2}$, \ie the Hessian matrix
\item show that $-f(\x)$ is convex
\end{enumerate}
Note that $0 \le f(\x) \le 1$.

\item ~[10 points] Derive the convex conjugate for the following functions
\begin{enumerate}
\item $f(x) = -\log(x)$
\item $f(\x) = \x^\top \A^{-1} \x$ where $\A \succ 0$
\end{enumerate}

\item~[20 points] Derive the (partial) gradient of the following functions. Note that bold small letters represent vectors, bold captial letters matrices, and non-bold letters just scalars.
\begin{enumerate}
	\item $f(\x) = \x^\top \A \x$, derive $\frac{\partial f}{\partial \x}$
	\item $f(\x)  =\left(\I + \x\x^\top\right)^{-1} \x$, derive $\frac{\partial f}{\partial \alpha}$
	\item $f(\x) = \log |\K + \alpha \I|$, where $|\cdot|$ means the determinant. Derive $\frac{\partial f}{\partial \sigma}$
	\item $f(\bmu, \bSigma) = \log\big(\N(\a|\A\bmu, \S\bSigma\S^\top)\big)$, derive $\frac{\partial f}{\partial \bmu}$ and $\frac{\partial f}{\partial \bSigma}$,
	\item $f(\bSigma) = \log\big(\N(\a|\b, \K\otimes \bSigma)\big)$ where $\otimes$ is the Kronecker product (Hint: check Minka's notes).
\end{enumerate}
\item~[2 points] Given the multivariate Gaussian probability density, $$p(\x|\bmu, \bSigma) = |2\pi \bSigma|^{-\frac{1}{2}}\exp\left(- (\x-\bmu)^\top\bSigma^{-1}(\x-\bmu)\right).$$ Show that the density function achieves the maximum when $\x = \bmu$.
\item~[5 points] Show that $$\int \exp(-\frac{1}{2\sigma^2}x^2) \d x = \sqrt{2\pi \sigma^2}.$$ Note that this is about  how the normalization constant of the Gaussian density is obtained. Hint: consider its square and use double integral.
\item~[5 points] The gamma function is defined as $$\Gamma(x) = \int_0^\infty u^{x-1}e^{-u} \d u.$$ Show that $\Gamma(1) = 1$ and $\Gamma(x+1) = x\Gamma(x)$. Hint: using integral by parts.
\item~[2 points] By using Jensen's inequality with $f(x) = \log(x)$, show that for any collection of positive numbers $\{x_1, \ldots, x_N\}$,
$$\frac{1}{N}\sum_{n=1}^N x_n \ge \left(\prod_{n=1}^N x_n\right)^{\frac{1}{N}}.$$
\item~[2 points] Given two probability density functions $p(\x)$ and $q(\x)$, show that $$\int p(\x)\log\frac{p(\x)}{q(\x)}\d \x \ge 0.$$
\item~[\textbf{Bonus}][5 points] Show that for any square matrix $\X \succ 0$, $\log\det\X$ is concave to $\X$.
\end{enumerate}

\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:


\lab{Introduction to Python}{Introduction to Python}
\label{lab:Python_Intro}
\objective{Python is a powerful, general-purpose programming language.
It can be used interactively, allowing for very rapid development.
Python has many powerful scientific computing tools, making it an ideal language for applied and computational mathematics.
In this introductory lab we introduce Python syntax, data types, functions, and control flow tools.
These Python basics are an essential part of almost every problem you will solve and almost every program you will write.
}

\section*{Getting Started} % ==================================================

Python is quickly gaining momentum as a fundamental tool in scientific
computing. Python should be already installed in Linux and MacOSX
operating systems. Under Windows it is possible to install Windows
subsystem for Linux and thus proceeding as in a Linux
environment. Alternatively, it is possible to install Python via
Anaconda.
% It is simple, readable, and easy to learn.
\emph{Anaconda} is a free distribution service by Continuum Analytics, Inc., that includes the cross-platform Python \emph{interpreter} (the software that actually executes Python code) and many Python libraries that are commonly used for applied and computational mathematics.
To install Python via Anaconda, go
to \url{https://www.anaconda.com/download/}, download the installer
for \textbf{Python 3.8.5} corresponding to your operating system, and
follow the on-screen instructions.
%Python 2.7 is still popular in the scientific community (as of 2018), but more and more libraries are moving support to Python 3.
%Note that Python 2.7 is syntactically different than Python 3, and
%support for Python 2 will be phased out by the end of 2020. It will
%definitely not be supported in this course.
Installing Python via Anaconda you may end up installing Python twice in
your system and you may end up loosing track of which installation you
are using, hence we do not recommend this way. For more information on
installing Python and various libraries, see
Appendix \ref{pythoninstall}.

\subsection*{Running Python} % ------------------------------------------------

Python files are saved with a \texttt{.py} extension.
For beginners, we strongly recommend using a simple text editor for writing Python files.
However, many free IDEs (Integrated Development Environments---large applications that facilitate code development with some sophisticated tools) are also compatible with Python.
For now, the simpler the coding environment, the better.
% \footnote{Some of the more popular text editors are described in Appendix \ref{pythoninstall}.}

A plain Python file looks similar to the following code.

\begin{lstlisting}
# filename.py
"""This is the file header.
The header contains basic information about the file.
"""

if __name__ == "__main__":
    pass                            # 'pass' is a temporary placeholder.
\end{lstlisting}

The \li{#} character creates a single-line \emph{comment}.
Comments are ignored by the interpreter and serve as annotations for the accompanying source code.
A pair of three quotes, \li{""" """} or \li{''' '''}, creates a multi-line string literal, which may also be used as a multi-line comment.
% Comments on the very first line can be used to give the interpreter special instructions, which we will discuss briefly in Volume 3.
% For now, we put the name of the file on the first line.
A triple-quoted string literal at the top of the file serves as the \emph{header} for the file.
The header typically identifies the author and includes instructions on using the file.
Executable Python code comes after the header.
% Finally, each statement under the \li{if __name__ == "__main__"} clause is executed when the file is run.
% Any ``orphan'' statements that are not placed under this clause are also executed when the file is run, but it is generally considered bad practice to leave any extra statements that are outside of function definitions and not under this clause.

\begin{problem} % Run a file that prints "Hello, world!"
Open the file named \texttt{python\_intro.py} (or create the file in a text editor if you don't have it).
Add your information to the header at the top, then add the following code.

\begin{lstlisting}
if __name__ == "__main__":
    print("Hello, world!")          # Indent with four spaces (NOT a tab).
\end{lstlisting}

Open a command prompt (\emph{Terminal} on Linux or Mac and \emph{Command Prompt} or \emph{GitBash} on Windows) and navigate to the directory where the new file is saved.
Use the command \li{ls} (or \li{DIR} on Windows) to list the files and folders in the current directory, \li{pwd} (\li{CD ,} on Windows) to print the working directory, and \li{cd} to change directories.
% Below, the \li{\$} character indicates that the command is being run from the command line.

\begin{lstlisting}[language=bash]
$ pwd                               # Print the working directory.
/Users/Guest
$ ls                                # List the files and folders here.
Desktop     Documents       Downloads       Pictures        Music
$ cd Documents                      # Navigate to a different folder.
$ pwd
/Users/Guest/Documents
$ ls                                # Check to see that the file is here.
python_intro.py
\end{lstlisting}

Now the Python file can be executed with the following command:

\begin{lstlisting}
$ python python_intro.py
\end{lstlisting}

If $\ $ \li{ Hello, world\! } $\ $ is displayed on the screen, you have just successfully executed your first Python program!
\label{prob:helloWorld}
\end{problem}

\subsection*{IPython} % -------------------------------------------------------

Python can be run interactively using several interfaces.
The most basic of these is the Python interpreter.
In this and subsequent labs, the triple brackets \li{>>>} indicate that the given code is being executed one line at a time via the Python interpreter.

\begin{lstlisting}
$ python                            # Start the Python interpreter.
>>> print("This is plain Python.")  # Execute some code.
<<This is plain Python.>>
\end{lstlisting}

There are, however, more useful interfaces.
Chief among these is \emph{IPython},\footnote{See \url{https://ipython.org/}.} which is included with the Anaconda distribution.
To execute a script in IPython, use the \li{run} command.

\begin{lstlisting}
>>> exit()                          # Exit the Python interpreter.
$ ipython                           # Start IPython.

<g<In [1]:>g> print("This is IPython!")   # Execute some code.
<<This is IPython!>>

<g<In [2]:>g> <p<%run>p> python_intro.py         # Run a particular Python script.
Hello, world!
\end{lstlisting}

One of the biggest advantages of IPython is that it supports \emph{object introspection}, whereas the regular Python interpreter does not.
Object introspection quickly reveals all methods and attributes associated with an object.
IPython also has a built-in \li{help()} function that provides interactive help.

\begin{lstlisting}
# A list is a basic Python data structure. To see the methods associated with
# a list, type the object name (list), followed by a period, and press tab.
<g<In [1]:>g> list.   # Press 'tab'.
              <<append()  count()   insert()  remove()
              clear()   extend()  mro()     reverse()
              copy()    index()   pop()     sort()>>

# To learn more about a specific method, use a '?' and hit 'Enter'.
<g<In [1]:>g> list.append?
<<Docstring: L.append(object) -> None -- append object to end
Type:      method_descriptor>>

<g<In [2]:>g> help()                      # Start IPython's interactive help utility.

<<help>>> list                          # Get documentation on the list class.
<<Help on class list in module __builtin__:

class list(object)
 |  list() -> new empty list
 |  >># ...                           # Press 'q' to exit the info screen.

<<help>>> quit                          # End the interactive help session.
\end{lstlisting}

\begin{info}
Use IPython side-by-side with a text editor to test syntax and small code snippets quickly.
Testing small pieces of code in IPython \textbf{before} putting it into a program reveals errors and greatly speeds up the coding process.
Consult the internet with questions; \href{http://stackoverflow.com/}{stackoverflow.com} is a particularly valuable resource for answering common programming questions.

The best way to learn a new coding language is by actually writing code.
Follow along with the examples in the yellow code boxes in this lab by executing them in an IPython console.
Avoid copy and paste for now; your fingers need to learn the language as well.
\end{info}

\section*{Python Basics} % ====================================================

\subsection*{Arithmetic} % ----------------------------------------------------

Python can be used as a calculator with the regular \li{+}, \li{-}, \li{*}, and \li{/} operators.
Use \li{**} for exponentiation and \li{\%} for modular division.

\begin{lstlisting}
>>> 3**2 + 2*5                      # Python obeys the order of operations.
19

>>> 13 % 3                          # The modulo operator % calculates the
1                                   # remainder: 13 = (3*4) + 1.
\end{lstlisting}

In most Python interpreters, the underscore character \li{\_} is a variable with the value of the previous command's output, like the ANS button on many calculators.

\begin{lstlisting}
>>> 12 * 3
36
>>> _ / 4
9.0
\end{lstlisting}

Data comparisons like \li{<} and \li{>} act as expected.
The \li{==} operator checks for numerical equality and the \li{<=} and \li{>=} operators correspond to $\le$ and $\ge$, respectively.
To connect multiple boolean expressions, use the operators \li{and}, \li{or}, and \li{not}.%
\footnote{In many other programming languages, the \lif{and}, \lif{or}, and \lif{not} operators are written as \lif{&&}, \lif{||}, and \lif{!}, respectively. Python's convention is much more readable and does not require parentheses.}
% The boolean values \li{True} and \li{False} (capitalized) act similar to the integers $1$ and $0$, respectively: 0 evaluates to \li{False} and nonzero values evaluate to \li{True}.

\begin{lstlisting}
>>> 3 > 2.99
<<True>>
>>> 1.0 <= 1 or 2 > 3
<<True>>
>>> 7 == 7 and not 4 < 4
<<True>>

>>> True and True and True and True and True and False
<<False>>
>>> False or False or False or False or False or True
<<True>>
>>> True or not True
<<True>>
\end{lstlisting}

\subsection*{Variables} % -----------------------------------------------------

Variables are used to temporarily store data.
A \textbf{single} equals sign \li{=} assigns one or more values (on the right) to one or more variable names (on the left).
A \textbf{double} equals sign \li{==} is a comparison operator that returns \li{True} or \li{False}, as in the previous code block.

Unlike many programming languages, Python does not require a variable's data type to be specified upon initialization.
Because of this, Python is called a \emph{dynamically typed} language.

\begin{lstlisting}
>>> x = 12                          # Initialize x with the integer 12.
>>> y = 2 * 6                       # Initialize y with the integer 2*6 = 12.
>>> x == y                          # Compare the two variable values.
True

>>> x, y = 2, 4                     # Give both x and y new values in one line.
>>> x == y
False
\end{lstlisting}

\subsection*{Functions} % -----------------------------------------------------

To define a function, use the \li{def} keyword followed by the function name, a parenthesized list of parameters, and a colon.
Then indent the function body using exactly \textbf{four} spaces.

\begin{lstlisting}
>>> def add(x, y):
...    return x + y                 # Indent with four spaces.
\end{lstlisting}

\begin{warn} % Python is picky about whitespace.
Many other languages use the curly braces \li{\{\}} to delimit blocks, but Python uses whitespace indentation.
In fact, whitespace is essentially the only thing that Python is particularly picky about compared to other languages: \textbf{mixing tabs and spaces confuses the interpreter and causes problems}.
Most text editors have a setting to set the indentation type to spaces so you can use the tab key on your keyboard to insert four spaces (sometimes called \emph{soft tabs}).
For consistency, \textbf{never} use tabs; \textbf{always} use spaces.
\end{warn}

Functions are defined with \emph{parameters} and called with \emph{arguments}, though the terms are often used interchangeably.
Below, \li{width} and \li{height} are parameters for the function \li{area()}.
The values \li{2} and \li{5} are the arguments that are passed when calling the function.

\begin{lstlisting}
>>> def area(width, height):        # Define the function.
...     return width * height
...
>>> area(2, 5)                      # Call the function.
10
\end{lstlisting}

Python functions can also return multiple values.
% Simply separate the values by commas after the \li{return} statement.

\begin{lstlisting}
>>> def arithmetic(a, b):
...     return a - b, a * b         # Separate return values with commas.
...
>>> x, y = arithmetic(5, 2)         # Unpack the returns into two variables.
>>> print(x, y)
3 10
\end{lstlisting}

The keyword \li{lambda} is a shortcut for creating one-line functions.
For example, the polynomials $f(x) = 6x^3 + 4x^2 - x + 3$ and $g(x,y,z) = x + y^2 - z^3$ can be defined as functions in one line each.

\begin{lstlisting}
# Define the polynomials the usual way using 'def'.
>>> def f(x):
...     return 6*x**3 + 4*x**2 - x + 3
>>> def g(x, y, z):
...     return x + y**2 - z**3

# Equivalently, define the polynomials quickly using 'lambda'.
>>> f = lambda x: 6*x**3 + 4*x**2 - x + 3
>>> g = lambda x, y, z: x + y**2 - z**3
\end{lstlisting}

\begin{info}
Documentation is important in every programming language.
Every function should have a \emph{docstring}---a string literal in triple quotes just under the function declaration---that describes the purpose of the function, the expected inputs and return values, and any other notes that are important to the user.
Short docstrings are acceptable for very simple functions, but more complicated functions require careful and detailed explanations.

\begin{lstlisting}
>>> def add(x, y):
...     """Return the sum of the two inputs."""
...     return x + y

>>> def area(width, height):
...     """Return the area of the rectangle with the specified width
...     and height.
...     """
...     return width * height
...
>>> def arithmetic(a, b):
...     """Return the difference and the product of the two inputs."""
...     return a - b, a * b
\end{lstlisting}

Lambda functions cannot have custom docstrings, so the \li{lambda} keyword should be only be used as a shortcut for very simple or intuitive functions that need no additional labeling.
% See \url{https://docs.python.org/3/tutorial/controlflow.html#documentation-strings} and \url{https://www.python.org/dev/peps/pep-0257/} for standard docstring conventions.
\end{info}

\begin{problem} % write a very basic function.
The volume of a sphere with radius $r$ is $V = \frac{4}{3}\pi r^3$.
In your Python file from Problem \ref{prob:helloWorld}, define a function called \li{sphere_volume()} that accepts a single parameter $r$.
Return the volume of the sphere of radius $r$, using $3.14159$ as an approximation for $\pi$ (for now).
Also write an appropriate docstring for your function.
% Try keeping the body of the function down to a single line of code.

To test your function, call it under the \li{if __name__ == "__main__"} clause and print the returned value.
Run your file to see if your answer is what you expect it to be.
\end{problem}

\begin{warn} % print() versus return.
The \li{return} statement instantly ends the function call and passes the return value to the function caller.
However, functions are not required to have a return statement.
A function without a return statement implicitly returns the Python constant \li{None}, which is similar to the special value \li{null} of many other languages.
Calling \li{print()} at the end of a function does \textbf{not} cause a function to return any values.

\begin{lstlisting}
>>> def oops(i):
...     """Increment i (but forget to return anything)."""
...     print(i + 1)
...
>>> def increment(i):
...     """Increment i."""
...     return i + 1
...
>>> x = oops(1999)                  # x contains 'None' since oops()
2000                                # doesn't have a return statement.
>>> y = increment(1999)             # However, y contains a value.
>>> print(x, y)
None 2000
\end{lstlisting}
%
If you have any intention of using the results of a function, use a \li{return} statement.
\end{warn}

It is also possible to specify \emph{default values} for a function's parameters.
In the following example, the function \li{pad()} has three parameters, and the value of \li{c} defaults to 0.
If it is not specified in the function call, the variable \li{c} will contain the value 0 when the function is executed.

\begin{lstlisting}
>>> def pad(a, b, c=0):
...     """Print the arguments, plus an zero if c is not specified."""
...     print(a, b, c)
...
>>> pad(1, 2, 3)                # Specify each parameter.
1 2 3
>>> pad(1, 2)                   # Specify only non-default parameters.
1 2 0
\end{lstlisting}

Arguments are passed to functions based on position or name, and positional arguments must be defined before named arguments.
For example, \li{a} and \li{b} must come before \li{c} in the function definition of \li{pad()}.
Examine the following code blocks demonstrating how positional and named arguments are used to call a function.

\begin{lstlisting}
# Try defining printer with a named argument before a positional argument.
>>> def pad(c=0, a, b):
...     print(a, b, c)
...
<<SyntaxError: non-default argument follows default argument>>
\end{lstlisting} % page break...
\begin{lstlisting}
# Correctly define pad() with the named argument after positional arguments.
>>> def pad(a, b, c=0):
...     """Print the arguments, plus an zero if c is not specified."""
...     print(a, b, c)
...

# Call pad() with 3 positional arguments.
>>> pad(2, 4, 6)
2 4 6

# Call pad() with 3 named arguments. Note the change in order.
>>> pad(b=3, c=5, a=7)
7 3 5

# Call pad() with 2 named arguments, excluding c.
>>> pad(b=1, a=2)
2 1 0

# Call pad() with 1 positional argument and 2 named arguments.
>>> pad(1, c=2, b=3)
1 3 2
\end{lstlisting}

\begin{problem}
The built-in \li{print()} function has the useful keyword arguments \li{sep} and \li{end}.
It accepts any number of positional arguments and prints them out with \li{sep} inserted between values (defaulting to a space), then prints \li{end} (defaulting to the \emph{newline character} \li{'\\n'}).
% \begin{lstlisting}
% >>> print(1, 2, 3, sep=', ', end='!!!\n')
% 1, 2, 3!!!
% \end{lstlisting}

Write a function called \li{isolate()} that accepts five arguments.
Print the first three separated by 5 spaces, then print the rest with a single space between each output.
For example,
\begin{lstlisting}
>>> isolate(1, 2, 3, 4, 5)
1     2     3 4 5
\end{lstlisting}
\end{problem}

\begin{warn} % Previous versions of print().
In previous versions of Python, \li{print()} was a \emph{statement} (like \li{return}), not a function, and could therefore be executed without parentheses.
However, it lacked keyword arguments like \li{sep} and \li{end}.
If you are using Python 2.7, include the following line at the top of the file to turn the \li{print} statement into the new \li{print()} function.
\begin{lstlisting}
>>> from __future__ import print_function
\end{lstlisting}
\end{warn}

\section*{Data Types and Structures} % ========================================

\subsection*{Numerical Types} % -----------------------------------------------

Python has four numerical data types: \li{int}, \li{long},
\li{float}, and \li{complex}.
Each stores a different kind of number.
The built-in function \li{type()} identifies an object's data type.

\begin{lstlisting}
>>> type(3)                         # Numbers without periods are integers.
<<int>>

>>> type(3.0)                       # Floats have periods (3. is also a float).
<<float>>
\end{lstlisting}

Python has two types of division: integer and float.
The \li{/} operator performs float division (true fractional division), and the \li{//} operator performs integer division, which rounds the result down to the next integer.
If both operands for \li{//} are integers, the result will be an \li{int}.
If one or both operands are floats, the result will be a \li{float}.
Regular division with \li{/} always returns a \li{float}.

\begin{lstlisting}
>>> 15 / 4                          # Float division performs as expected.
3.75
>>> 15 // 4                         # Integer division rounds the result down.
3
>>> 15. // 4
3.0
\end{lstlisting}

\begin{warn} % Beware of integer division in Python 2.
In previous versions of Python, using \li{/} with two integers performed integer division, even in cases where the division was not even.
This can result in some incredibly subtle and frustrating errors.
If you are using Python 2.7, always include a \li{.} on the operands or cast at least one as a float when you want float division.
\begin{lstlisting}
# PYTHON 2.7
>>> 15 / 4                          # The answer should be 3.75, but the
3                                   # interpreter does integer division!

>>> 15. / float(4)                  # 15. and float(4) are both floats, so
3.75                                # the interpreter does float division.
\end{lstlisting}
Alternatively, including the following line at the top of the file redefines the \li{/} and \li{//} operators so they are handled the same way as in Python 3.
\begin{lstlisting}
>>> from __future__ import division
\end{lstlisting}
\end{warn}

Python also supports complex numbers computations by pairing two numbers as the real and imaginary parts.
Use the letter $j$, not $i$, for the imaginary part.
\begin{lstlisting}
>>> x = complex(2,3)                # Create a complex number this way...
>>> y = 4 + 5j	                    # ...or this way, using j (not i).
>>> x.real                          # Access the real part of x.
2.0
>>> y.imag                          # Access the imaginary part of y.
5.0
\end{lstlisting}

\subsection*{Strings} % -------------------------------------------------------

In Python, strings are created with either single or double quotes.
To concatenate two or more strings, use the \li{+} operator between string variables or literals.
\begin{lstlisting}
>>> str1 = "Hello"
>>> str2 = 'world'
>>> my_string = str1 + " " + str2 + '!'
>>> my_string
<<'Hello world!'>>
\end{lstlisting}

Parts of a string can be accessed using \emph{slicing}, indicated by square brackets \li{[ ]}.
Slicing syntax is \li{[start:stop:step]}.
The parameters \li{start} and \li{stop} default to the beginning and end of the string, respectively.
The parameter \li{step} defaults to 1.

\begin{lstlisting}
>>> my_string = "Hello world!"
>>> my_string[4]                # Indexing begins at 0.
<<'o'>>
>>> my_string[-1]               # Negative indices count backward from the end.
<<'!'>>

# Slice from the 0th to the 5th character (not including the 5th character).
>>> my_string[:5]
<<'Hello'>>

# Slice from the 6th character to the end.
>>> my_string[6:]
<<'world!'>>

# Slice from the 3rd to the 8th character (not including the 8th character).
>>> my_string[3:8]
<<'lo wo'>>

# Get every other character in the string.
>>> my_string[::2]
<<'Hlowrd'>>
\end{lstlisting}

\begin{problem} % Slicing with strings.
Write two new functions, called \li{first_half()} and \li{backward()}.
\begin{enumerate}
\item \li{first_half()} should accept a parameter and return the first half of it, excluding the middle character if there is an odd number of characters.
\\
(Hint: the built-in function \li{len()} returns the length of the input.)
\item The \li{backward()} function should accept a parameter and reverse the order of its characters using slicing, then return the reversed string.
\\
(Hint: The \li{step} parameter used in slicing can be negative.)
\end{enumerate}
Use IPython to quickly test your syntax for each function.
\end{problem}

\subsection*{Lists} % ---------------------------------------------------------

A Python \li{list} is created by enclosing comma-separated values with square brackets \li{[ ]}.
Entries of a list do \textbf{not} have to be of the same type.
Access entries in a list with the same indexing or slicing operations used with strings.

\begin{lstlisting}
>>> my_list = ["Hello", 93.8, "world", 10]
>>> my_list[0]
<<'Hello'>>
>>> my_list[-2]
<<'world'>>
>>> my_list[:2]
<<['Hello', 93.8]>>
\end{lstlisting}

Common list methods (functions) include \li{append()}, \li{insert()}, \li{remove()}, and \li{pop()}.
Consult IPython for details on each of these methods using object introspection.
\begin{lstlisting}
>>> my_list = [1, 2]                # Create a simple list of two integers.
>>> my_list.append(4)               # Append the integer 4 to the end.
>>> my_list.insert(2, 3)            # Insert 3 at location 2.
>>> my_list
[1, 2, 3, 4]
>>> my_list.remove(3)               # Remove 3 from the list.
>>> my_list.pop()                   # Remove (and return) the last entry.
4
>>> my_list
[1, 2]
\end{lstlisting}

Slicing is also very useful for replacing values in a list.

\begin{lstlisting}
>>> my_list = [10, 20, 30, 40, 50]
>>> my_list[0] = -1
>>> my_list[3:] = [8, 9]
>>> print(my_list)
[-1, 20, 30, 8, 9]
\end{lstlisting}

The \li{in} operator quickly checks if a given value is in a list (or another iterable, including strings).

\begin{lstlisting}
>>> my_list = [1, 2, 3, 4, 5]
>>> 2 in my_list
<<True>>
>>> 6 in my_list
<<False>>
>>> 'a' in "xylophone"              # 'in' also works on strings.
<<False>>
\end{lstlisting}

\subsection*{Tuples} % --------------------------------------------------------

A Python \li{tuple} is an ordered collection of elements, created by enclosing comma-separated values with parentheses \li{(} and \li{)}.
Tuples are similar to lists, but they are much more rigid, have less built-in operations, and cannot be altered after creation.
Lists are therefore preferable for managing dynamic ordered collections of objects.

When multiple objects are returned by a function, they are returned as a tuple.
For example, recall that the \li{arithmetic()} function returns two values.

\begin{lstlisting}
>>> x, y = arithmetic(5,2)                      # Get each value individually,
>>> print(x, y)
3 10
>>> both = arithmetic(5,2)                      # or get them both as a tuple.
>>> print(both)
(3, 10)
\end{lstlisting}

\begin{problem} % Simple list operations.
Write a function called \li{list_ops()}.
Define a list with the entries \li{"bear"}, \li{"ant"}, \li{"cat"}, and \li{"dog"}, in that order.
Then perform the following operations on the list:
\begin{enumerate}
\item Append \li{"eagle"}.
\item Replace the entry at index 2 with \li{"fox"}.
\item Remove (or pop) the entry at index 1.
\item Sort the list in reverse alphabetical order.
\item Replace \li{"eagle"} with \li{"hawk"}.
\\(Hint: the list's \li{index()} method may be helpful.)
\item Add the string \li{"hunter"} to the last entry in the list.
\end{enumerate}
Return the resulting list.

Work out (on paper) what the result should be, then check that your function returns the correct list.
Consider printing the list at each step to see the intermediate results.
\end{problem}

\subsection*{Sets} % ----------------------------------------------------------

A Python \li{set} is an unordered collection of distinct objects.
Objects can be added to or removed from a set after its creation.
Initialize a set with curly braces \li{\{ \}}, separating the values by commas, or use \li{set()} to create an empty set.
Like mathematical sets, Python sets have operations like union, intersection,
difference, and symmetric difference.

\begin{lstlisting}
# Initialize some sets. Note that repeats are not added.
>>> gym_members = {"Doe, John", "Doe, John", "Smith, Jane", "Brown, Bob"}
>>> print(gym_members)
<<{'Doe, John', 'Brown, Bob', 'Smith, Jane'}>>

>>> gym_members.add("Lytle, Josh")      # Add an object to the set.
>>> gym_members.discard("Doe, John")    # Delete an object from the set.
>>> print(gym_members)
<<{'Lytle, Josh', 'Brown, Bob', 'Smith, Jane'}>>

>>> gym_members.intersection({"Lytle, Josh", "Henriksen, Ian", "Webb, Jared"})
<<{'Lytle, Josh'}>>
>>> gym_members.difference({"Brown, Bob", "Sharp, Sarah"})
<<{'Lytle, Josh', 'Smith, Jane'}>>
\end{lstlisting}

\subsection*{Dictionaries} % --------------------------------------------------

Like a set, a Python \li{dict} (dictionary) is an unordered data type.
A dictionary stores key-value pairs, called \emph{items}.
The values of a dictionary are indexed by its keys.
Dictionaries are initialized with curly braces, colons, and commas.
Use \li{dict()} or \li{\{\}} to create an empty dictionary.

\begin{lstlisting}
>>> my_dictionary = {"business": 4121, "math": 2061, "visual arts": 7321}
>>> print(my_dictionary["math"])
2061

# Add a value indexed by 'science' and delete the 'business' keypair.
>>> my_dictionary["science"] = 6284
>>> my_dictionary.pop("business")       # Use 'pop' or 'popitem' to remove.
4121
>>> print(my_dictionary)
<<{'math': 2061, 'visual arts': 7321, 'science': 6284}>>

# Display the keys and values.
>>> my_dictionary.keys()
<<dict_keys(['math', 'visual arts', 'science'])>>
>>> my_dictionary.values()
<<dict_values([2061, 7321, 6284])>>
\end{lstlisting}

As far as data access goes, lists are like dictionaries whose keys are the integers $0,1,\ldots,n-1$, where $n$ is the number of items in the list.
The keys of a dictionary need not be integers, but they must be \emph{immutable}, which means that they must be objects that cannot be modified after creation.
We will discuss mutability more thoroughly in the Standard Library lab.

\subsection*{Type Casting} % --------------------------------------------------

The names of each of Python's data types can be used as functions to cast a value as that type.
This is particularly useful for converting between integers and floats.

\begin{lstlisting}
# Cast numerical values as different kinds of numerical values.
>>> x = int(3.0)
>>> y = float(3)
>>> z = complex(3)
>>> print(x, y, z)
3 3.0 (3+0j)

# Cast a list as a set and vice versa.
>>> set([1, 2, 3, 4, 4])
<<{1, 2, 3, 4}>>
>>> list({'a', 'a', 'b', 'b', 'c'})
<<['a', 'c', 'b']>>

# Cast other objects as strings.
>>> str(['a', str(1), 'b', float(2)])
<<"['a', '1', 'b', 2.0]" >>
>>> str(list(set([complex(float(3))])))
<<'[(3+0j)]'>>
\end{lstlisting}

\section*{Control Flow Tools} % ===============================================

Control flow blocks dictate the order in which code is executed.
Python supports the usual control flow statements including \li{if} statements, \li{while} loops and \li{for} loops.

\subsection*{The If Statement} % ----------------------------------------------

An \li{if} statement executes the indented code \textbf{if} (and \text{only if}) the given condition holds.
The \li{elif} statement is short for ``else if'' and can be used multiple times following an if statement, or not at all.
The \li{else} keyword may be used at most once at the end of a series of \li{if}/\li{elif} statements.

\begin{lstlisting}
>>> food = "bagel"
>>> if food == "apple":             # As with functions, the colon denotes
...     print("72 calories")        # the start of each code block.
... elif food == "banana" or food == "carrot":
...     print("105 calories")
... else:
...     print("calorie count unavailable")
...
calorie count unavailable
\end{lstlisting}

\begin{problem} % If statements and Pig Latin.
Write a function called \li{pig_latin()}.
Accept a parameter \li{word}, translate it into Pig Latin, then return the translation.
Specifically, if \li{word} starts with a vowel, add ``hay'' to the end; if \li{word} starts with a consonant, take the first character of \li{word}, move it to the end, and add ``ay''.
\\
(Hint: use the \li{in} operator to check if the first letter is a vowel.)
\end{problem}

\subsection*{The While Loop} % ------------------------------------------------

A \li{while} loop executes an indented block of code \textbf{while} the given condition holds.

\begin{lstlisting}
>>> i = 0
>>> while i < 10:
...     print(i, end=' ')           # Print a space instead of a newline.
...     i += 1                      # Shortcut syntax for i = i+1.
...
0 1 2 3 4 5 6 7 8 9
\end{lstlisting}

There are two additional useful statements to use inside of loops:
\begin{enumerate}
\item \li{break} manually exits the loop, regardless of which iteration the loop is on or if the termination condition is met.
\item \li{continue} skips the current iteration and returns to the top of the loop block if the termination condition is still not met.
\end{enumerate}

\begin{lstlisting}
>>> i = 0
>>> while True:
...    print(i, end=' ')
...    i += 1
...    if i >= 10:
...        break                    # Exit the loop.
...
0 1 2 3 4 5 6 7 8 9

>>> i = 0
>>> while i < 10:
...     i += 1
...     if i % 3 == 0:
...         continue                # Skip multiples of 3.
...     print(i, end=' ')
1 2 4 5 7 8 10
\end{lstlisting}

\subsection*{The For Loop} % --------------------------------------------------

A \li{for} loop iterates over the items in any \emph{iterable}.
Iterables include (but are not limited to) strings, lists, sets, and dictionaries.

\begin{lstlisting}
>>> colors = ["red", "green", "blue", "yellow"]
>>> for entry in colors:
...    print(entry + "!")
...
red!
green!
blue!
yellow!
\end{lstlisting}

The \li{break} and \li{continue} statements also work in for loops, but a \li{continue} in a for loop will automatically increment the index or item, whereas a \li{continue} in a while loop makes no automatic changes to any variable.

\begin{lstlisting}
>>> for word in ["It", "definitely", "looks", "pretty", "bad", "today"]:
...     if word == "definitely":
...         continue
...     elif word == "bad":
...         break
...     print(word, end=' ')
...
<<It looks pretty>>
\end{lstlisting}

In addition, Python has some very useful built-in functions that can be used in conjunction with the \li{for} statement:
\begin{enumerate}
\item \li{range(start, stop, step)}: Produces a sequence of integers, following slicing syntax.
If only one argument is specified, it produces a sequence of integers from 0 to the argument, incrementing by one.
This function is used \textbf{very} often.
\item \li{zip()}: Joins multiple sequences so they can be iterated over simultaneously.
\item \li{enumerate()}: Yields both a count and a value from the sequence. Typically used to get both the index of an item and the actual item simultaneously.
\item \li{reversed()}: Reverses the order of the iteration.
\item \li{sorted()}: Returns a new list of sorted items that can then be used for iteration.
\end{enumerate}
Each of these functions except for \li{sorted()} returns an \emph{iterator}, an object that is built specifically for looping but not for creating actual lists.
To put the items of the sequence in a collection, use \li{list()}, \li{set()}, or \li{tuple()}.

\begin{lstlisting}
# Strings and lists are both iterables.
>>> vowels = "aeiou"
>>> colors = ["red", "yellow", "white", "blue", "purple"]

# Iterate by index.
>>> for i in range(5):
...     print(i, vowels[i], colors[i])
...
0 a red
1 e yellow
2 i white
3 o blue
4 u purple

# Iterate through both sequences at once.
>>> for letter, word in zip(vowels, colors):
...     print(letter, word)
...
a red
e yellow
i white
o blue
u purple

# Get the index and the item simultaneously.
>>> for i, color in enumerate(colors):  #
...     print(i, color)
...
0 red
1 yellow
2 white
3 blue
4 purple

# Iterate through the list in sorted (alphabetical) order.
>>> for item in sorted(colors):
...     print(item, end=' ')
...
blue purple red white yellow

# Iterate through the list backward.
>>> for item in reversed(colors):
...     print(item, end=' ')
...
purple blue white yellow red

# range() arguments follow slicing syntax.
>>> list(range(10))                 # Integers from 0 to 10, exclusive.
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> list(range(4, 8))               # Integers from 4 to 8, exclusive.
[4, 5, 6, 7]

>>> set(range(2, 20, 3))            # Every third integer from 2 to 20.
{2, 5, 8, 11, 14, 17}
\end{lstlisting}

\begin{problem} % large palindromic number (Project Euler #4)
This problem originates from \url{https://projecteuler.net}, an excellent resource for math-related coding problems.

A palindromic number reads the same both ways.
The largest palindrome made from the product of two 2-digit numbers is $9009 = 91 \times 99.$
Write a function called \li{palindrome()} that finds and returns the largest palindromic number made from the product of two 3-digit numbers.
\end{problem}

\subsection*{List Comprehension} % --------------------------------------------

A \emph{list comprehension} uses for loop syntax between square brackets to create a list.
This is a powerful, efficient way to build lists.
The code is concise and runs quickly.

\begin{lstlisting}
>>> [float(n) for n in range(5)]
[0.0, 1.0, 2.0, 3.0, 4.0]
\end{lstlisting}

List comprehensions can be thought of as ``inverted loops'', meaning that the body of the loop comes before the looping condition.
The following loop and list comprehension produce the same list, but the list comprehension takes only about two-thirds the time to execute.

\begin{lstlisting}
>>> loop_output = []
>>> for i in range(5):
...    loop_output.append(i**2)
...
>>> list_output = [i**2 for i in range(5)]
\end{lstlisting}

Tuple, set, and dictionary comprehensions can be done in the same way as list comprehensions by using the appropriate style of brackets on the end.

\begin{lstlisting}
>>> colors = ["red", "blue", "yellow"]
>>> {c[0]:c for c in colors}
<<{'y': 'yellow', 'r': 'red', 'b': 'blue'}>>

>>> {"bright " + c for c in colors}
<<{'bright blue', 'bright red', 'bright yellow'}>>
\end{lstlisting}

\begin{problem} % Alternating harmonic series.
The alternating harmonic series is defined as follows.
\[\sum_{n=1}^\infty \frac{(-1)^{(n+1)}}{n} = 1 - \frac{1}{2} + \frac{1}{3} - \frac{1}{4} + \frac{1}{5} - \ldots = \ln(2)\]
Write a function called \li{alt_harmonic()} that accepts an integer $n$.
Use a list comprehension to quickly compute the first $n$ terms of this series (be careful not to compute only $n-1$ terms).
The sum of the first 500,000 terms of this series approximates $\ln(2)$ to five decimal places.
\\(Hint: consider using Python's built-in \li{sum()} function.)
\end{problem}

\newpage

\section*{Additional Material} % ==============================================

\subsection*{Further Reading} % -----------------------------------------------

Refer back to this and other introductory labs often as you continue getting used to Python syntax and data types.
As you continue your study of Python, we strongly recommend the following readings.
\begin{itemize}
\item The official Python tutorial: \url{http://docs.python.org/3.6/tutorial/introduction.html} (especially chapters 3, 4, and 5).
\item Section 1.2 of the SciPy lecture notes: \url{http://scipy-lectures.github.io/}.
\item PEP8 - Python style guide: \url{http://www.python.org/dev/peps/pep-0008/}.
\end{itemize}

\subsection*{Generalized Function Input} % ------------------------------------

On rare occasion, it is necessary to define a function without knowing exactly what the parameters will be like or how many there will be.
This is usually done by defining the function with the parameters \li{*args} and \li{**kwargs}.
Here \li{*args} is a list of the positional arguments and \li{**kwargs} is a dictionary mapping the keywords to their argument.
This is the most general form of a function definition.

\begin{lstlisting}
>>> def report(*args, **kwargs):
...     for i, arg in enumerate(args):
...         print("Argument " + str(i) + ":", arg)
...     for key in kwargs:
...         print("Keyword", key, "-->", kwargs[key])
...
>>> report("TK", 421, exceptional=False, missing=True)
<<Argument 0: TK
Argument 1: 421
Keyword missing --> True
Keyword exceptional --> False>>
\end{lstlisting}

See \url{https://docs.python.org/3.6/tutorial/controlflow.html} for more on this topic.

\subsection*{Function Decorators} % -------------------------------------------

A \emph{function decorator} is a special function that ``wraps'' other functions.
It takes in a function as input and returns a new function that pre-processes the inputs or post-processes the outputs of the original function.

\begin{lstlisting}
>>> def typewriter(func):
...     """Decorator for printing the type of output a function returns"""
...     def wrapper(*args, **kwargs):
...         output = func(*args, **kwargs)      # Call the decorated function.
...         print("output type:", type(output)) # Process before finishing.
...         return output                       # Return the function output.
...     return wrapper
\end{lstlisting}

The outer function, \li{typewriter()}, returns the new function \li{wrapper()}.
Since \li{wrapper()} accepts \li{*args} and \li{**kwargs} as arguments, the input function \li{func()} could accepts any number of positional or keyword arguments.

Apply a decorator to a function by tagging the function's definition with an @ symbol and the decorator name.

\begin{lstlisting}
>>> @typewriter
... def combine(a, b, c):
...     return a*b // c
\end{lstlisting}

Placing the tag above the definition is equivalent to adding the following line of code after the function definition:

\begin{lstlisting}
>>> combine = typewriter(combine)
\end{lstlisting}

Now calling \li{combine()} actually calls \li{wrapper()}, which then calls the original \li{combine()}.

\begin{lstlisting}
>>> combine(3, 4, 6)
<<output type: <class 'int'>>>
2
>>> combine(3.0, 4, 6)
<<output type: <class 'float'>>>
2.0
\end{lstlisting}

Function decorators can also customized with arguments.
This requires another level of nesting: the outermost function must define and return a decorator that defines and returns a wrapper.

\begin{lstlisting}
>>> def repeat(times):
...     """Decorator for calling a function several times."""
...     def decorator(func):
...         def wrapper(*args, **kwargs):
...             for _ in range(times):
...                 output = func(*args, **kwargs)
...             return output
...         return wrapper
...     return decorator
...
>>> @repeat(3)
... def hello_world():
...     print("Hello, world!")
...
>>> hello_world()
Hello, world!
Hello, world!
Hello, world!
\end{lstlisting}

See \url{https://www.python.org/dev/peps/pep-0318/} for more details.

\begin{comment} % Overkill, since they don't know how to import yet.
Decorating a function can overwrite the original function's docstring.
To remedy this problem, the \li{functools} module includes a decorator for wrappers.

\begin{lstlisting}
>>> from functools import wraps

>>> def typewriter(func):
...     """Decorator for printing the type of output a function returns"""
...     @wraps(func)                            # Add the @wraps tag.
...     def wrapper(*args, **kwargs):
...         output = func(*args, **kwargs)
...         print("output type:", type(output))
...         return output
...     return wrapper
...
>>> def repeat(times=2):
...     """Decorator for calling a function several times."""
...     def decorator(func):
...         @wraps(func)                        # Add the @wraps tag.
...         def wrapper(*args, **kwargs):
...             for _ in range(times):
...                 output = func(*args, **kwargs)
...             return output
...         return wrapper
...     return decorator
\end{lstlisting}
\end{comment}


\documentclass[acmlarge, screen, nonacm]{acmart}
\usepackage[utf8]{inputenc}
\usepackage{float}
\usepackage{setspace}
\usepackage{pgfplots}
\usepackage{graphicx}
\usepackage{fancyvrb}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{hyperref}
  \hypersetup{colorlinks=true,allcolors=blue!40!black}
\setlength{\topskip}{6pt}
\setlength{\parindent}{0pt} % indent first line
\setlength{\parskip}{6pt} % before par

\title{Three phase commit consistency evaluation for git repository replication}

\author{Ilya Moskvitin}
\email{TODO: put your email address here}

\author{Kirill Chernyavskiy}
\email{g4s8.public@gmail.com}

\acmBooktitle{none}
\acmConference{none}
\editor{none}

\begin{document}

\begin{abstract}
  For git repository replication distributed system we need to use atomic
  commit implementation based on git reference transaction hooks (todo: add reference to previous report).
  There are three most popular protocols: two-phase commit (2PC), three-phase commit protocols (3PC) family,
  and Paxos commit protocol. 2PC protocol is not really fault tolerant, since it may turn storage
  replicas into inconsistent state in case of transaction manager (TM) failure. Paxos-commit is a fault-tolerant
  version of transaction commit protocol. But it pays for fault-tolerance by sending much more messages via network
  for distributing the resource manager (RM) votes: \texttt{3N - 3} messages for 2PC vs
  \texttt{(N - 1)(2F + 3)} messages for Paxos commit co-located faster version (where N is number of nodes,
  and F is a number of tolerated faults)
  (todo: add references  J. Gray and L. Lamport, “Consensus on transaction commit).
  Different versions of 3PC protocols family claims to fix possible inconsistency by appending
  a new state ``prepared-to-commit'', and by allowing the
  quorum of replicas to not block on the transaction in case if some RM failure.
  In this report we evaluate 3PC and compare it with Paxos-commit to analyze which one is better for
  git repository replication system.
\end{abstract}

\maketitle

\section{Problem}

3PC was introduced by (todo: add authors and initial publication). The motivation of creating 3PC was to fix
a problem with 2PC, which can not recover from some kind of failures. For instance, if a TM receives a ``prepared''
message from all RMs, decides to commit, and sends a ``commit'' message to one of the RM (let's say RM1),
and the crashes, the secondary TM can be started in another network region which can't access RM1 to continue the transaction,
and this secondary TM will not be able to access RM1 and decides to abort the transaction by sending ``abort'' message
to RMs except RM1, then RMs will have different state after the transaction.
The 3PC protocol fixes this problem by introducing new kind of message: ``prepared to commit''. If TM fails before sending
this message, the secondary TM will abort the transaction if at least one RM is not in ``prepared to commit'' state
(todo: describe more verbosely).

Let's define a metrics to compare these two algorithms:
\begin{enumerate}
  \item Message delays - the number of total messages round-trips between nodes
  \item Message count - the number of messages sent in the system to commit the transaction
  \item Message model - what kind of message model does the algorithm support: synchronous, partially
    synchronous or asynchronous
  \item Fault tolerance - the number of nodes that may fail and keep the system working
  \item Complexity - the number of modules on deployments requires for transaction management
\end{enumerate}

Paxos commit protocol with co-located acceptor nodes (acceptors are deployed to same node as a RM),
has two versions: just a ``Paxos commit'' and ``Faster Paxos commit''; the difference between them
is in balance between message delays and message count: faster version has fewer message delays
but requires more messages to be sent. The formulas for Paxos commit were taken from the
consensus-on-transaction-commit (todo: add cite) paper. Paxos commit requires 5 message delays,
and $N(N+3)-3$ messages to commit the transaction; faster version requires 4 delays, and
$(N-1)(2F+3)$ messages; both versions works in partially-synchronous (P-S) network model,
can tolerate $F$ number of failures, this number should be less than $(N/2)+1$ and it affects
messages count; and it requires 4 components for deployment: RM, TM, and one Paxos instance per
RM deployment (co-located version), where each deployment consists of one proposer and $N$ acceptors.

The comparison of these three protocols
are presented at~figure~\ref{fig:compare-3pc-pc-fpc}, where $N$ is a number of nodes, and $F$ is a number of
failures that system can tolerate.

\begin{figure}
  \begin{tabular}{r | l | l | l | l | l}
    Protocol & Delays & Count & Model & Fault tolerance & Complexity \\
    \hline
    PC  & 5 & $N(F+3)-3$    & P-S & $F>N/2+1$ & 4   \\
    \hline
    FPC & 4 & $(N-1)(2F+3)$ & --- & --- & --- \\
    \hline
    3PC & 6 & $5N-1$ & P-S & depends on impl. & todo \\
    \hline
  \end{tabular}
  \caption{
    Comparing 3PC with Paxos-commit and Faster Paxos-commit
  }\label{fig:compare-3pc-pc-fpc}
\end{figure}

To calculate the message delays and count let's describe all steps required to complete transaction successfully in
positive case scenario for generic 3PC protocol; Assuming we have $N$ resource managers (RM) and transaction is
started when any received an initial data which can be applied to the RM, and the system has the only transaction
manager (TM):
\begin{enumerate}
  \item RM initiates the transaction by sending ``prepared'' message to TM ($1$ message)
  \item TM sends ``prepare'' command to other RMs ($N-1$ message)
  \item RMs who received the command replies with ``prepared'' message to TM ($N-1$ messages)
  \item TM sends ``prepare to commit'' command to all RMs ($N$ messages)
  \item All RMs replies with ``prepared to commit'' message ($N$ messages)
  \item TM sends a ``commit'' command ($N$ messages)
\end{enumerate}

So to commit the transaction 3PC requires 6 message delays and $5N-1$ messages.

\section{Related works}

(todo: describe 3PC papers, paxos-commit about 3PC).

\section{Solution}

3PC could be used as a distributed commit protocol for git transactions. (todo: add diagrams).
\begin{itemize}
  \item Client pushes new changes to coordinator node
  \item Coordinator sends these changes to all RMs
  \item Each RM starts applying these changes and calls reference-transaction hook process with the result (prepared
    in case if transaction was validated and can be applied; aborted otherwise).
  \item Hook calls RM process with a decision and waits for the signal from RM.
  \item (todo: explain 3PC here)
  \item RM notifies the hook process to continue with decided by TM status
  \item Hook exits with specified status: 0 - for success, 1 - for abort
  \item Git finish transaction if the hook exited with 0 status, or cancel it on abort; after that it
    calls hook again to notify completion
  \item Hook notify RM process about transaction finish
  \item RM notifies TM coordinator about finish transaction
\end{itemize}

To evaluate the 3PC protocol in this workflow, we've created a $TLA_{+}$ model and checker
(todo: create the model, add to appendix).

It shows that 3PC in git repository transaction could be broken by these paths: (todo: describe paths).

\section{Conclusion}

(todo: analyze the result of paths and show the risks how it affects git repository consistency.)

\end{document}