\documentclass[11 pt]{scrartcl}
\usepackage[header, margin, koma]{tyler}

\newcommand{\hwtitle}{Discussion 0 Recap}

\pagestyle{fancy}
\fancyhf{}
\fancyhead[l]{\hwtitle{}}
\fancyhead[r]{Tyler Zhu}
\cfoot{\thepage}

\begin{document}
\title{\Large \hwtitle{}}
\author{\large Tyler Zhu}
\date{August 27th, 2020}

\maketitle

\section{Sets}
Here's some basic notation you should be familiar with. For this section, let $A = \{1,2\}$ and $B = \{1,2,3,4\}$.
\begin{itemize}
    \ii Cardinality, i.e. size of a set. $|A| = 2$.
    \ii Subsets, i.e. when all members of one set belong to another. $A\subseteq B$.
    \ii Intersection, i.e. everything in common. $A \cap B = \{1,2\}$.
    \ii Union, i.e. everything that appears. $A \cup B = \{1,2,3,4\}$.
    \ii Set difference, i.e. everything in one but not the other. $B\setminus A = \{3,4\}$.
    \ii Cartesian product, i.e. all possible pairs (or tuples) of elements: \\$A\times B = \{(1,1), (1,2), (1,3), (1,4), (2,1), \dots \}$.
    \ii Power set, i.e. the set of all subsets. $\Pcal(A) = \{\emptyset, \{1\}, \{2\}, \{1,2\}\}$. Note that $|\Pcal(A)| = 2^{|A|}$.
\end{itemize}

Some important sets that you'll come across this semester:
\begin{itemize}
    \ii The naturals (which includes 0): $\NN = \{0,1,2,\dots \}$
    \ii The integers: $\ZZ = \{\dots, -2, -1, 0, 1, 2, \dots\}$
    \ii The rationals: $\QQ = \{\frac{a}{b} | a,b\in\ZZ, b\not= 0\}$
    \ii The reals: $\RR$
    \ii The complexes: $\CC$
\end{itemize}

As we saw in discussion, if you want to show that two sets are equal, i.e. $A = B$, you need to show $A \subseteq B$ and $B\subseteq A$. This is the same as showing $x=y$ by proving $x\leq y$ and $y\leq x$.

\section{Propositional Logic}
We work with logical statements, or propositions. Operators operate on propositions. The common ones are
\begin{itemize}
    \ii and, $P\vee Q$: True only if both propositions are True.
    \ii or, $P\wedge Q$: False only if both propositions are False (unlike the English or, which is commonly either or).
    \ii not, $\neg P$: Negates the truth value, i.e. True becomes False and vice versa.
    \ii implies, $P\implies Q \equiv \neg P \wedge Q$: False only if $P$ is True and $Q$ is False.
\end{itemize}

Finally, we have two quantifiers: \emph{for all}, which is $(\forall x\in \RR)$, and \emph{there exists}, which is $(\exists x \in \ZZ)$.

\section{Tips}
\begin{itemize}
    \ii Use DeMorgan's Laws to move negations past $\wedge, \vee$. Negate the clauses and swap between the two.
    \[ \neg(P\wedge Q) \equiv \neg P \vee \neg Q \]
    \[ \neg(P\vee Q) \equiv \neg P \wedge \neg Q \]
    \ii To simplify expressions, distribute operators by following the ``distributive law,'' e.x.
    \[ (P \wedge Q) \vee R \equiv (P \vee R) \wedge (Q \vee R).\]
    The operator linking $P$ and $Q$ remains as the operator linking the two resulting clauses.
    \ii Operators cannot be arbitrarily switched, i.e. $\forall x \exists y P(x,y) \not\equiv \exists y \forall x P(x,y)$. Take $P(x,y) = x < y$ as a counterexample.
\end{itemize}

\end{document}


\chapter{Experimental features}
\label{chap:expfeatures}

Experimental features are currently under active development. They are expected to change rapidly, and they have no full support.

\section{Hierarchical learning algorithms}

A hierarchical learning algorithm (HLA) can be any tree structure of models, where an input is propagated through the tree until a final decision is taken on a leaf node. At every node, a learning algorithm is employed to choose which branch to follow. To understand its utility, try to plot the output values of the \textit{uci\_yacht} dataset: you will see that there is a strong gap between positive and negative values. In this case, it may be better to envision a two-stage architecture:

\begin{itemize}
\item The first level discriminates between positive and negative output values.
\item The second level has two learning algorithms trained only on positive and negative outputs respectively.
\end{itemize}

\noindent Currently, you can implement this with the following code:

\begin{lstlisting}
add_model('NODE', 'Internal node', @ExtremeLearningMachine);
add_model('LEAF1', 'First leaf', @ExtremeLearningMachine);
add_model('LEAF2', 'Second leaf', @ExtremeLearningMachine);

construct_hierarchical_algorithm([2 0 0], {RangeAggregator(0)}, {'NODE', 'LEAF1', 'LEAF2'});

add_dataset('Y', 'Yacht (UCI)', 'uci_yacht');
\end{lstlisting}

\noindent The parameters to the \verb|construct_hierarchical_algorithm| method are:

\begin{itemize}
\item A cell array describing the number of childs of the tree, in a depth-first fashion.
\item A cell array of \verb|Aggregator| objects. Aggregators are used to associate to every original output a particular branch of the node. In our case, \verb|RangeAggregator| discriminates between negative and positive values, sending them to the left and right branches of the node.
\item A cell array containing the ids of the models composing the tree.
\end{itemize}

\noindent We can also add an output script for plotting the tree structure:

\begin{lstlisting}
add_feature(ExecuteOutputScripts('info_hla'));
\end{lstlisting}

\section{Distributed learning algorithms}

Distributed learning algorithms (DLA) represents algorithms that are trained in a fully decentralized way. To use a DLA, add the \verb|InitializeTopology| feature to determine the actual topology of the nodes:

\begin{lstlisting}
add_feature(InitializeTopology(t));
\end{lstlisting}

\noindent where \verb|t| can be any \verb|NetworkTopology| object (see the 'core/classes/NetworkTopologies' folder). If you want every node to have a part of the dataset, add the following flag:

\begin{lstlisting}
add_feature(InitializeTopology(t), 'distribute_data');
\end{lstlisting}

\noindent Distributed algorithms are run inside an SPMD block. You can also have algorithms that only simulate a parallel simulation, in this case add the flag:

\begin{lstlisting}
add_feature(InitializeTopology(t), 'disable_parallel');
\end{lstlisting}

\noindent DLA derive from the basic class \verb|DistributedLearningAlgorithm|, and they must implement the \verb|train_locally| method. In the distributed case, this will be executed inside the SPMD block. Otherwise, it is up to the algorithm to simulate the parallel behavior. Topology is stored inside the \verb|topology| property of the class. To see concrete implementations, see \verb|DataDistributedRVFL| and \verb|SerialDataDistributedRVFL|.


% PREAMBLE
\documentclass{article}
\usepackage{amsmath}
\usepackage{indentfirst}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{caption}
\usepackage{subcaption}

\hypersetup{
  colorlinks   = true,    % Colours links instead of ugly boxes
  urlcolor     = blue,    % Colour for external hyperlinks
  linkcolor    = blue,    % Colour of internal links
  citecolor    = red      % Colour of citations
}
\title{Beerxels \\
	\large A Personal Project Report}
\author{Jonas Stolle}
\date{\today}
% DOCUMENT
\begin{document}

\begin{titlepage}
\maketitle
\centering
	\includegraphics[width=0.8\linewidth]{caps_pics-circles.jpg}
\end{titlepage}

\section{Abstract}
This report describes the process of creating pixel art using a set of collected bottle caps. The bottle caps are captured digitally, cut out with the help of the Circle Hough Transform and an averaged color is extracted. A given reference image is tiled for the bottle caps to fit and colors are extracted yet again. Optimal pairings between the two sets are computed using the Hungarian Algorithm, with which the correct bottle cap placements are found. Finally, the physical piece is assembled by attaching the bottle caps to a metal sheet using small magnets.

\section{Introduction}

Over the past years a rather large collection of unique bottle caps was collected which invited for a creative application.
The idea of creating pixel art arose, i.e. the tiling of an image with caps such that in each position the color of the cap best matches the color of the underlying part of the image. This results in a downsampled but recognizable version of the reference image. Finding the correct cap placements may be attempted by hand, but the approach of choice is to find a solution via computational means.
\section{Methods}
This section details the process of going from a set of bottle caps to the finished piece.
\subsection{Data Acquisition}
For a start, the bottlecaps and the reference image need to be available in digital form. While the latter is assumed to be given as such, the bottle caps are each captured using a digital camera in consistent lighting conditions. To aid with preprocessing, the background is chosen to be a white sheet of paper on which the bottle caps are placed within a circular black area just slightly larger than the caps themselves.
\begin{figure}[h]
	\begin{center}
	\includegraphics[width=0.5\linewidth]{cap_raw.png}
	\caption{Raw captured image of bottle cap on a whit sheet of paper. Notice the black area within which the cap is positioned.}
	\label{cap:raw}
\end{center}
\end{figure}
\subsection{Preprocessing}
To set up the problem, the colors to eventually be matched need to be extracted from both the bottle caps and the reference image. The process of computing the colors is identical for both cases, but the method of finding the appropriate locations to do so differs.
\subsubsection{Bottle Caps} \label{caps}
Here it is assumed that every image contains only a single bottle cap, which must first be located precisely.
This can be achieved using an implementation of the Circle Hough Transform \cite{DAVIES198837}, which returns a list of circles, parametrized by their center point positions and radii, which are sorted by the confidence of the algorithm.
The goal is to detect the outline of the black area, which  will most likely have the highest confidence, as the high contrast between the black and white parts is favorable for the detection algorithm. Thus the first entry in the list is chosen as a close approximation to the outline of the bottle cap.
\begin{figure}[h]
        \begin{center}
        \includegraphics[width=0.5\linewidth]{cap_hough.png}
	\caption{The same bottle cap image as in \ref{cap:raw}, with the circles returned from the Circle Hough Transform as an overlay. The green circle has the highest algorithmic score and does match the outline of the bottle cap very closely.}
        \label{cap:hough}
\end{center}
\end{figure}

Also note that the algorithmic parameters need to be tuned for good results.
Given the correct circle parameters, the bottle cap is then cut out and its averaged color is computed.
\begin{figure}[h]
        \begin{center}
        \includegraphics[width=0.3\linewidth]{cap_cutout_white_2.png}
	\caption{Cutout of the bottle cap presented in \ref{cap:raw} and \ref{cap:hough}.}
        \label{cap:cut}
\end{center}
\end{figure}

As colors on computers are generally represented in terms of their red, green and blue components (RGB), the average of each component needs to be computed for the final averaged color of the bottle cap.
The reasoning for the average as a metric are its ease of implementation and its rotational symmetry, which allows the caps in the physical piece to be placed in any orientation desired.
The set of all extracted colors is then stored in a vector $\boldsymbol{v}_{caps}$, with each element containing the red, green and blue components.

\subsubsection{Reference Image}
Before any colors can be extracted from the reference image, an appropriate tiling needs to be computed on it, as it determines the positions of the bottle caps in the final piece. In this project the tiling is understood as a repeating pattern of non-overlapping circles covering the reference image.
An important constraint here is the number of available bottlecaps $n_{caps,max}$, which limits the number of circles $n_{caps,piece}$ the tiling may use to cover the image.
\begin{equation}
\label{eqn:max}
n_{caps,piece} \leq n_{caps,max}
\end{equation}
While a quadratic (grid like) tiling would be the easiest to compute, a hexagonal pattern is chosen because of its dense packing and the authors aesthetic preference. Given the centerpoint $(x_c,y_c)$ of a circle and its radius $r$, the rule for finding adjacent circles in the tiling is as follows:
\begin{equation}
\label{eqn:adj}
(x,y)_{adj} =
	\begin{cases}
		\{(x_c \pm 2r,y_c)\} \text{ for horizontal neighbors,} \\
		\{(x_c \pm r, y_c \pm \sqrt{3}r)\} \text{ for vertical neighbors.} \\
	\end{cases}
\end{equation}
Notice that \ref{eqn:adj} depends only on the $r$ chosen.
Let $tile(r):\ r\ \rightarrow \{(x,y)\}$ be a function that covers the plane following \ref{eqn:adj}.
Assuming the reference image lies within a rectangular set of points
\begin{equation}
	D = \{(x,y)\ \vert\ x \in [0,x_{bound}], y \in [0,y_{bound}]\},
\end{equation}
the following optimization problem can be formulated:
\begin{equation}
\label{eqn:tile}
\begin{aligned}
& \ \boldsymbol{\max_{r}}\ \ \ \vert tile(r) \vert \\
& \ \ \textbf{s.t.}\ \ \  \{(x,y)\}\ \ \subseteq\ D  \\
& \ \ \ \ \ \ \ \ \ \vert \{(x,y)\} \vert\ \leq\ n_{caps,max}, \\
\end{aligned}
\end{equation}
where $\vert(\cdot)\vert$ is read as the number of elements in $(\cdot)$.

In practice this is solved by iterating the radius from a starting size of 3 pixels, covering the reference image each time and checking whether for the total number of circles required \ref{eqn:max} holds. The first radius to not violate this constraint is then the optimal solution to \ref{eqn:adj}.
\begin{figure}[h]
        \begin{center}
        \includegraphics[width=0.4\linewidth]{optimal_tiling.png}
        \label{opttile}
	\caption{Visualization of an optimal tiling solution to problem \ref{eqn:tile}, with a $n_{caps,max} = 628$ and $n_{caps,piece} = 621$.}
\end{center}
\end{figure}
Note that in the general case not all bottle caps will be used, but that number will always remain relatively small compared to the size of the entire set of caps.
With the positions of the bottle caps in the refernce image being known, for every circle the averaged color is computed analogously to \ref{caps} and stored in a vector $\boldsymbol{v}_{ref}$.

\subsection{Matching}
Given the averaged colors of all the bottlecaps, the matching process can begin. Concretely pairings need to be found between all elements $v_{caps}$ and $v_{ref}$ of $\boldsymbol{v}_{caps}$ and $\boldsymbol{v}_{ref}$ respectively, such that in each pair the colors are as "similar" as possible. This similarity is computed via the
\begin{equation}
\label{eqn:ssd}
	\text{Sum of Squared Deviations} = \sum_{i = 1}^{3} (v_{caps,i}-v_{ref,i})^2 ,
\end{equation}
where $i = \{1,2,3\}$ corresponds to the RGB components of each color.
This is a Balanced Assignment Problem and can be solved using the Hungarian Algorithm \cite{https://doi.org/10.1002/nav.3800020109}, of which numerous implementations are readily available.
The algorithm returns the optimal pairings w.r.t. \ref{eqn:ssd}, with which the bottle caps can be placed in their corresponding positions.
\subsection{Physical Implementation}
Using the digital version of the piece as a template, the reference image is recreated in a jigsaw like fashion using the bottle caps in the real world. The canvas is chosen to be a magnetic sheet of metal, to which every bottlecap is fixed using small neodymium magnets. This makes the bottle caps removable and leaves the possibility open to change the design in the future.
\section{Results} \label{results}
This section presents the finished piece in both its digital and physical form.

\begin{figure}[h]
	\centering
	\begin{subfigure}[t]{0.3\linewidth}
		\centering
	        \includegraphics[width=\linewidth]{mona-lisa.jpg}
		\caption{A moderately well known painting which is used as the reference image.}
		\label{fig:mona1}
	\end{subfigure}
	\hfill
	\begin{subfigure}[t]{0.3\linewidth}
		\centering
		\includegraphics[width=\linewidth]{mona-lisa-caps-white.jpg}
		\caption{Digital version of the finished piece.}
		\label{fig:mona2}
        \end{subfigure}
	\hfill
        \begin{subfigure}[t]{0.3\linewidth}
		\centering
		\includegraphics[width=\linewidth]{mona-lisa-caps-real.jpg}
		\caption{Physical version of the finished piece.}
		\label{fig:mona3}
        \end{subfigure}
\end{figure}
For increased visual enjoyment, it is advised to view this document from further away or to squint ones eyes slightly.

\section{Discussion} \label{discussion}
Clearly, the quality of the image does detiriorate with every processing step, however the impression of the reference image persists throughout to the final piece.
The bright region at the top left of \ref{fig:mona2} is predominantly made up of caps with strong reflections, which does not translate well to the physical piece \ref{fig:mona3} as they appear noticably dimmer without the reflections present.
Better results will likely be achieved with more accurate capturing of the bottle caps. Possible approaches here could be more diffuse lighting conditions, which would reduce problematic reflections, or the use of a polarized filter, with similar effect.
Another issue encountered were inaccuracies with the circle detection as it did not always work perfectly, especially when the design of the bottle caps featured high contrast circles. In most cases however, the averaged color of a section is still close to that of the full bottle cap, or at least the error is small enough to not be noticable in the result.
Still, a remedy for this would be to manually verify and potentially tune the detected circles manually at the preprocessing stage.
Also note that the set of bottle caps fundamentally dictates the color accuracy of the piece. For \ref{results}, a non-negligible set of red bottle caps was available and most of them were used for the final piece. As a consequence, the color spectrum with respect to the reference, which has almost no red hues, is distorted. Choosing a fitting reference image with a color distribution similar to that of the bottle caps will help with this issue.

Despite its shortcomings, this project is ultimately artistic in nature and the quality of the results lies in the eye of the beholder.
\bibliographystyle{abbrv}
\bibliography{refs}
\end{document}


\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-qtree}
\usepackage{forest}

\author{Lin Jiaying}
\title{Chap.5 Exercise}

\begin{document}

	\maketitle
	\section{Question 5.1 Answer: }
	\begin{tikzpicture}[every internal node/.style={draw,circle}]
		\Tree
		[.1
	     [.3
	      [.2
	       [.7
	        [.5
	         [.6
	          [.4
	           [.x ]]]
	         [.4
	          [.6
	           [.1 ]]]]
	        [.6
	         [.5
	          [.4
	           [.x ]]]
	         [.4 [.x ]]]]]
	       [.4
	        [.5
	         [.7
	          [.2
	           [.x ]]]]
	        [.6
	         [.7
	          [.5
	           [.x ]] ]
	         [.5
	          [.7
	           [.2
	            [.x ]]] ]] ]]
	         [.6
	          [.4
	           [.5
	            [.7
	             [.2
	              [.3
	               [.1 ]]]]]
	           [.3 [.1 [.x ]]
	            [.2
	             [.7
	              [.5
	               [.x ]]]
	            ]]]
	          [.5
	            [.4
	             [.3
	              [.1
	               [.x ]]
	              [.2
	               [.7
	                [.x ]]]]]
	             [.7
	              [.2
	               [.3 [.1 [.x ]]
	                [.4
	                 [.x ]]]]]
	                ]
	           [.7
	           [.2 [.3 [.1 [.x ]] [.4 [.5 [.x ]]]]]
	            [.5
	             [.4 [.3 [.2 [.x ]]]]
	              [.3
	               [.x ]]]]]]

	\end{tikzpicture}

	\section{Question 5.3 Answer:}
	\begin{forest}
	for tree={circle,draw, l sep=20pt}
	[$v_0$(0)
		[$v_1$(1), edge label = {node[midway, left]{1}}
		  [$v_5$(4), edge label = {node[midway, left]{3}}
		   [$v_8$(6), edge label = {node[midway, left]{2}}]]
		[$v_6$(11), edge label = {node[midway, left]{10}} [$7>6$]]]
		[$v_2$(5), edge label = {node[midway, left]{5}}
		 [$v_5$(7), edge label = {node[midway, left]{2}} [$7>6$]]
		 [$v_7$(7), edge label = {node[midway, left]{2}} [$7>6$]]

	]
	[$v_3$(7), edge label = {node[midway, right]{7}} [$7>6$]]
	[$v_4$(4), edge label = {node[midway, right]{4}}
	 [$v_7$(7), edge label = {node[midway, right]{3}} [$7>6$]]]
	];
	\end{forest}
.\\
	So the shortest path we found is $v_0 \rightarrow v_1 \rightarrow v_5 \rightarrow v_8$(1 + 3 + 2 = 6).\\

	\section{Question 5.4 Answer:}
	The lower bound = 5 + 7 + 8 + 6 + 10 = 36.\\
	\begin{forest}
		for tree={draw, l sep=20pt}
		[All Solution\\
		Lower Bound:36, align=center, base=bottom
		 [All solution with 5-3\\
		 Lower Bound:36, align=center, base=bottom
		   [All solution with 3-4\\
		   Lower Bound:36, align=center, base=bottom
		    [All solution with 4-1\\
		    Lower Bound:36, align=center, base=bottom
		    [All solution with 2-5\\
		     Lower Bound:36, align=center, base=bottom
		    [All solution with 1-2\\
		    Lower Bound:36, align=center, base=bottom]
		    [All solution without 1-2\\
		    No solution, align=center, base=bottom]]
		    [All solution without 2-5\\
		    Lower Bound:43, align=center, base=bottom]]
		    [All solution without 4-1\\
		    Lower Bound:130, align=center, base=bottom]]
		    [All solution without 3-4\\
		    Lower Bound:80, align=center, base=bottom]]
		 [All solution without 5-3\\
		 Lower Bound:88, align=center, base=bottom]
		]
	\end{forest}
	Solution: $5\to3\to4\to1\to2\to5$.
\end{document}


% -----------------------------------------------------------------------------------
% Section   :
% -----------------------------------------------------------------------------------

\section*{Abstract}

In this paper we study the Count-min Sketch problem and explore the feasibility of impelementing it in the programming language Haskell. We benchmark its \textit{time-complexity} against naive methods.\newline

\section*{Introduction}

Modern applications now must handle massive amount of data. In this setting, many existing data structures that were suitable for main memory no longer suffice; therefore we are motivated to find new  data structures that are relatively small, even sub-linear in the size of the input. Furthermore, it often suffices to provide approximate responses to various queries.
These so called “sketches”  approximate massive data sets. We implement one such data structure called the Count-Min Sketch that has found wide applications from machine learning to distributed computing, just to name a few. \newline

First we introduce the most common problem in modern data processing: count tracking. In this problem, there are a large number of items, and an associated frequency for each item which changes over time. The query specifies an
item and the response to the query is the frequency of the item. Here is an example.
Suppose a high traffic website needs to track statistics on the queries used
to search the site. \newline

This problem can be solved exactly using traditional data structures: ie a hash table [4], and update and Estimate involve standard hash methods. However, the amount of memory used by the data structure can become very large over
time as more items are added. Because its size is large, accessing such a data structure becomes slow. Practically speaking, as the table grows it must be periodically resized, this affects real-time processing. Finally, in
distributed applications, if the entire frequency distribution has to be communicated, this may be a prohibitive overhead. \newline

But note in this setting we can generally tolerate some imprecision, that is we may tradeoff precision for an algorithm with lower time or space complexity. For instance, in presenting statistics on customer buying patterns, an uncertainty of one percent or less is not considered significant. Such sketch data structures can accurately summarize arbitrary data distributions with a small and fixed memory footprint that can often fit within a cache, thus leading to fast processing of updates and quick communication across sites.


\chapter{AND, OR, NOT gates}
%\ref{sec:background}.

\section{Aim}
%\label{sec:objectives}
	To verify and interpret the logic and truth table for AND, OR, NOT gates using Resistor Transistor Logic (RTL), Diode Resistance Logic (DRL) and Transitor Logic (TL)

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

\section{Circuits}

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

	\subsection{AND gate}
	The AND gate is an electronic circuit that gives a high output $(1)$ only if all its inputs are high. A dot $(.)$ is used to show the AND operation i.e. $A.B$ or can be written as $AB$.
	\begin{align*}
		Y &= A . B
	\end{align*}
	A simple 2-input logic AND gate can be constructed using RTL (Resistor-Transistor-Logic) switches connected together as shown below with the inputs connected directly to the transistor bases. Both transistors must be saturated “ON” for an output at $Q$.
	\begin{figure}[ht]
		\centering
		\subfloat[Symbol]
		{
			\begin{circuitikz} \draw
				(0,0) node[and port] (myand1) {}
				(myand1.in 1) node[anchor=east] {A}
				(myand1.in 2) node[anchor=east] {B}
				(myand1.out) node[anchor=west] {Y}
				;
			\end{circuitikz}
			\label{fig:and_symbol}
		}
		\hfill
		\subfloat[Truth Table]
		{
			\begin{tabular}{|c|c|c|}
				\hline
				\multicolumn{2}{|c|}{Input} & Output \\
				\hline
				$A$ & $B$ & $Y=A.B$ \\
				\hline
				0 & 0 & 0 \\
				\hline
				0 & 1 & 0 \\
				\hline
				1 & 0 & 0 \\
				\hline
				1 & 1 & 1 \\
				\hline
			\end{tabular}
			\label{fig:and_table}
		}
		\hfill
		\subfloat[RTL Design]{
			\includegraphics[width=0.25\textwidth,valign=c]{img/exp1/fig1}
			\label{fig:and_circuit}
		}
		\caption{\textit{AND gate}}
	\end{figure}

	\subsection{OR gate}
		The OR gate is an electronic circuit that gives a high output $(1)$ if one or more of its inputs are high. A plus $(+)$ is used to show the OR operation.
		\begin{align*}
			Y &= A + B
		\end{align*}
		OR gate can be realized by DRL (Diode-Resistance-Logic) or by TTL (Transistor-Transistor-Logic). Presently, we will learn how to implement the OR gate using DRL (Diode-Resistance-Logic). To realise OR gate, we will use a diode at every input of the OR gate. The anode part of diode is connected with input while the cathode part is joined together and a resistor, connected with the cathode is grounded. In this case, we have taken two inputs which can be seen in the circuit below.

		When both the inputs are at logic $0$ or low state then the diodes $D_1$ and $D_2$ become reverse biased. Since the anode terminal of diode is at lower voltage level than the cathode terminal, so diode will act as open circuit so there is no voltage across resistor and hence output voltage is same as ground. When either of the diodes is at logic $1$ or high state then the diode corresponding to that input is forward bias. Since this time anode is at high voltage than cathode therefore current will flow through forward biased diode and this current then appears on resistor causing high voltage at output terminal also. Hence at output we get high or logic $1$ or $+5V$. So, if any or both inputs are high, the output will be high or $1$.
		\begin{figure}[ht]
			\centering
			\subfloat[Symbol]
			{
				\begin{circuitikz} \draw
					(0,0) node[or port] (myand1) {}
					(myand1.in 1) node[anchor=east] {A}
					(myand1.in 2) node[anchor=east] {B}
					(myand1.out) node[anchor=west] {Y}
					;
				\end{circuitikz}
				\label{fig:or_symbol}
			}
			\hfill
			\subfloat[Truth Table]
			{
				\begin{tabular}{|c|c|c|}
					\hline
					\multicolumn{2}{|c|}{Input} & Output \\
					\hline
					$A$ & $B$ & $Y=A+B$ \\
					\hline
					0 & 0 & 0 \\
					\hline
					0 & 1 & 1 \\
					\hline
					1 & 0 & 1 \\
					\hline
					1 & 1 & 1 \\
					\hline
				\end{tabular}
				\label{fig:or_table}
			}
			\hfill
			\subfloat[DRL Design]{
				\includegraphics[width=0.25\textwidth,valign=c]{img/exp1/fig2}
				\label{fig:or_circuit}
			}
			\caption{\textit{OR gate}}
		\end{figure}

	\subsection{NOT gate}
	The NOT gate is an electronic circuit that produces an inverted version of the input at its output. It is also known as an inverter. If the input variable is $A$, the inverted output is known as $NOT A$. This is also shown as $A'$ or $A$ with a bar over the top, as shown at the outputs.
	\begin{align*}
		Y &= \overline{A}
	\end{align*}
	NOT gate can be realized through transistor.The input is connected through resistor $R_2$ to the transistor’s base. When no voltage is present on the input, the transistor turns off. When the transistor is off, no current flows through the collector-emitter path. Thus, current from the supply voltage ($V_{cc}$) flows through resistor $R_1$ to the output. In this way, the circuit’s output is high when its input is low.

	When voltage is present at the input, the transistor turns on, allowing current to flow through the collector-emitter circuit directly to ground. This ground path creates a shortcut that bypasses the output, which causes the output to go low.

	In this way, the output is high when the input is low and low when the input is high.
	\begin{figure}[ht]
		\centering
		\subfloat[Symbol]
		{
			\begin{circuitikz} \draw
				(0,0) node[not port] (mynot) {}
				(mynot.in) node[anchor=east] {A}
				(mynot.out) node[anchor=west] {Y}
				;
			\end{circuitikz}
			\label{fig:not_symbol}
		}
		\hfill
		\subfloat[Truth Table]
		{
			\begin{tabular}{|c|c|}
				\hline
				Input & Output \\
				\hline
				$A$ & $Y = \overline{A}$ \\
				\hline
				0 & 1 \\
				\hline
				1 & 0 \\
				\hline
			\end{tabular}
			\label{fig:not_table}
		}
		\hfill
		\subfloat[TL Design]{
			\includegraphics[width=0.25\textwidth,valign=c]{img/exp1/fig3}
			\label{fig:not_circuit}
		}
		\caption{\textit{NOT gate}}
	\end{figure}

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

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

	\subsection{NOT gate}
		\begin{figure}[ht]
			\centering
			\subfloat[Simulator 1]{\includegraphics[width=0.45\textwidth,valign=c]{img/exp1/fig8}
				\label{fig:not_sim:1}}
			\hfill
			\subfloat[Simulator 2]{\includegraphics[width=0.4\textwidth,valign=c]{img/exp1/fig9}
				\label{fig:not_sim:2}}
			\caption{\textit{Simulator for realizing circuit for NOT gate}}
		\end{figure}
		\subsubsection{Simulator 1}
			\begin{enumerate}
				\tightlist
				\item Connect the supply(+5V) to the circuit.
				\item Press the switch 1 to connect battery to the circuit.
				\item Press the switch 2 for input "A".
				\item The bulb glows if switch 2 is OFF else it won't glow.
			\end{enumerate}
		\subsubsection{Simulator 2}
			\begin{enumerate}
				\tightlist
				\item Enter the Boolean input "A".
				\item Enter the Boolean output for your corresponding input.
				\item Click on "Check" Button to verify your output.
			\end{enumerate}

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

\pagebreak
	\subsection{OR gate}
			\begin{figure}[ht]
				\centering
				\subfloat[Either of the Inputs ON, LED is ON]{\includegraphics[width=0.45\textwidth,valign=c]{img/exp1/fig13}
					\label{fig:or_obs:1}}
				\hfill
				\subfloat[Both Inputs OFF, LED is OFF]{\includegraphics[width=0.45\textwidth,valign=c]{img/exp1/fig14}
					\label{fig:or_obs:2}}
				\caption{\textit{Observations for different Input Values}}
			\end{figure}
			\begin{figure}[h]
				\centering
				\includegraphics[width=0.85\linewidth]{img/exp1/fig15}
				\caption{\textit{Observations for verification of Truth Table of the OR gate}}
				\label{fig:or_obs_2}
			\end{figure}

\pagebreak
	\subsection{NOT gate}
			\begin{figure}[ht]
				\centering
				\subfloat[Input is ON, Output is OFF]{\includegraphics[width=0.45\textwidth,valign=c]{img/exp1/fig16}
					\label{fig:not_obs:1}}
				\hfill
				\subfloat[Input is OFF, Output is ON]{\includegraphics[width=0.45\textwidth,valign=c]{img/exp1/fig17}
					\label{fig:not_obs:2}}
				\caption{\textit{Observations for different Input Values}}
			\end{figure}
			\begin{figure}[h]
				\centering
				\includegraphics[width=0.85\linewidth]{img/exp1/fig18}
				\caption{\textit{Observations for verification of Truth Table of the NOT gate}}
				\label{fig:not_obs_2}
			\end{figure}

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


\documentclass{article}
%\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsmath, hyperref}
\usepackage{fancyhdr}
\usepackage[title]{appendix}
\pagestyle{fancy}
\fancyhf{}
\rhead{Math - 6373}
\lhead{Project 2}
\chead{Autoencoders}
\rfoot{Page \thepage}
\lfoot{A. Radillo}
\usepackage{graphicx}
\usepackage{xcolor}
\title{Autoencoders training for handwritten digits classification \\ {\large Project 2 - Math-6373 - Prof. Azencott}}
\author{Adrian Radillo - PSID: 1328335}
\newcommand{\bi}{\begin{itemize}}
\newcommand{\ei}{\end{itemize}}
\makeatletter
\g@addto@macro\@floatboxreset\centering
\makeatother

\usepackage{tabularx,ragged2e,booktabs,caption}
\newcolumntype{C}[1]{>{\Centering}m{#1}}
\renewcommand\tabularxcolumn[1]{C{#1}}

\usepackage{listings}
\usepackage{color} %red, green, blue, yellow, cyan, magenta, black, white
\definecolor{mygreen}{RGB}{28,172,0} % color values Red, Green, Blue
\definecolor{mylilas}{RGB}{170,55,241}
\newcommand{\lp}{\left(}
\newcommand{\rp}{\right)}
%\usepackage{mdframed}
\begin{document}

\lstset{language=Matlab,%
    %basicstyle=\color{red},
    breaklines=true,%
    morekeywords={matlab2tikz},
    keywordstyle=\color{blue},%
    morekeywords=[2]{1}, keywordstyle=[2]{\color{black}},
    identifierstyle=\color{black},%
    stringstyle=\color{mylilas},
    commentstyle=\color{mygreen},%
    showstringspaces=false,%without this there will be a symbol in the places where there is a space
    numbers=left,%
    numberstyle={\tiny \color{black}},% size of the numbers
    numbersep=9pt, % this defines how far the numbers are from the text
    emph=[1]{for,end,break},emphstyle=[1]\color{red}, %some words to emphasise
    %emph=[2]{word1,word2}, emphstyle=[2]{style},
}

\maketitle
\section{Database}
I use the task and data as in project 1. This is a set of
60,000 14x14 grayscale images of handwritten digits for the training set and
10,000 images for the test set, all taken from the MNIST database and pre-processed by myself
in project 1.

\section{Autoencoder architecture}
\begin{itemize}
\item The input and output layers have $14\times14=196$ units. The range of each one of these units
is $[0,1]$ as the initial pixel intensities ranging from 0 to 255 were rescaled by 1/255.
\item My tentative values for $h$ are $50,100,150$ for the $h<n_1$ case, and $h=250,300,350$ for the $h>n_1$ case.
\end{itemize}

\section{First experiment}
\begin{itemize}
\item My ST is MATLAB, and more specifically, the Neural Network Toolbox from the R2015b version. I created custom neural networks for the three small autoencoders with the function create\_NN() presented in appendix~\ref{app:create}.
\item In figure~\ref{fig:trainFcn} are short and non-exhaustive descriptions of the options available in this toolbox.
\begin{description}
\item[learning] A neural network object has a property called trainFcn. This object property can be set to many
different values, which correspond to a variety of learning algorithms. The backpropagation gradient descent
algorithm corresponds to the name `traingd'. However, in order to update the weights after the presentation of a whole batch I believe that the name `trainb' is more appropriate. But I really struggled to find clear documentation on this
point. Below is a list of all the other options offered by the ST:
\begin{figure}
\centering
\fbox{\includegraphics[width=\textwidth]{trainFcn.png}}
\caption{Existing built-in training functions in my ST. Taken from~\cite{Demuth2006}}\label{fig:trainFcn}
\end{figure}
\item[initialization] I used the `rands' initFcn property value for both the weights and the biases. This samples
independent values from the uniform distribution over the interval $[-1,1]$ for each weight and bias.
\begin{figure}
\centering
\fbox{\includegraphics[width=\textwidth]{initFcn.png}}
\caption{Existing built-in initializing functions in my ST. Taken from~\cite{Demuth2006}. This picture only concerns the inputWeights property but `rands' exists as well for the layerWeights and biases properties.}
\end{figure}
\item[batch learning] The ideal batch learning option that would have suited my needs in the MATLAB Neural Network Toolbox is the `trainingOptions' function called for a convolutional neural network object with the stochastic gradient descent with momentum (`sgdm') solver in the R2016b release of MATLAB. However, my version of MATLAB didn't have this option,
so I set out to produce my training batches myself (as in project 1).

I produced 3,000 batches containing 500 cases each, taken from the 60,000 cases in the training set. Consecutive
batches were constructed with an overlap of 100 cases, and the whole training set was used 20 times in order to
produce the batches. Each sweep through the training set was preceded by a shuffling of its elements order.
I refer you to the make\_batches.m function in the appendix of my project 1 to see the source code of this function.
In appendix~\ref{app:train_diabolo} I show how the train() function from my ST was called sequentially for each batch
in order to train my autoencoder.
\item[step size] The step size is controlled by the `lr' property of trainFcn property. The ST offered a learning algorithm
based on gradient descent with adapting learning rate (`traingda'), but I was not sure that this corresponded to
the equations seen in class. The `traingd' learning function, that I used, uses a fixed learning rate.
Since I called the train() function sequentially on each batch, I only set the number of Epochs in the training properties to 1, for each iteration. Then, in between iterations, I wasn't sure whether I should change the learning rate manually, or
if the training algorithm would do it for me. This is why there are a lot of commented lines in appendix~\ref{app:train_diabolo}.
\end{description}
\end{itemize}
\section{Training `small' autoencoders}
\subsection{Implementation of the standard retro-propagation rule}
Below is a list of mathematical notation that I used to derive the retro-propagation rule by hand.
The final result is expressed in equations~\eqref{one} and~\eqref{two}.
\begin{itemize}
\item Matrix of weights between input and hidden layer:
\[
W=\left(w_{ij}^{(1)}\right)_{ij}, \quad 1\leq i\leq n_2; \quad1\leq j \leq n_1+1,
\]
where $w_{in_1+1}^{(1)}$ is always the threshold of unit $i$.
\item Matrix of weights between hidden and output layer:
\[
W=\left(w_{ij}^{(2)}\right)_{ij}, \quad 1\leq i\leq n_3; \quad1\leq j \leq n_2+1,
\]
where $w_{in_2+1}^{(1)}$ is always the threshold of unit $i$.
\item The total number of cases in the training set or in the batch is denoted by $M$.
\item The state of input unit $i$ under presentation of case $m$ is denoted $x_i$.
\item The state of hidden unit $j$ under presentation of case $m$ is denoted $h_j(W,m)$.
\item The state of unit $k$ on the output layer under presentation of case $m$ is denoted $o_k(W,m)$.
Hence, the output units states are: $o_1(W,m),\ldots,o_{n_3}(W,m)$.
\item Linear output of hidden unit $i$:
\[
A_i^{(1)}(W,m)=\sum_{j=1}^{n_1+1}x_jw_{ij}^{(1)}
\]
\item Logistic function is denoted $\sigma$. We have:
\[
\sigma(v)=\frac{1}{1+e^{-v}}\qquad \sigma'(v)=\frac{e^{-v}}{\lp1+e^{-v}\rp^2}
\]
\item Output of unit $j$ in hidden layer, under presentation of case $m$:
\[
h_j(W,m)=\sigma\lp A_j^{(1)}(W,m) \rp
\]
\item Linear output of the output layer unit $i$:
\[
A_i^{(2)}(W,m)=\sum_{j=1}^{n_2+1}h_j(W,m)w_{ij}^{(2)}
\]
\item Output of unit $i$ in output layer, under presentation of case $m$:
\[
o_i(W,m)=\sigma\lp A_i^{(2)}(W,m) \rp
\]
\item Denote by $\delta_{x,y}$ the Kronecker delta which is 1 only when $x=y$ and zero otherwise.
\item Also, define $\iota$ to be the function that maps each case $m$ to the output unit index $\iota(m)$
which codes for the correct label of this case. Hence, if case 36 represents a 4, then unit $o_5$ will code
for it in $\text{OUT}_{36}$ and therefore $\iota(36)=5$.
\item We denote the MSE function by the letter $f$:
\begin{align}
f(W)&=\frac{1}{M}\sum_{m=1}^{M}\left |\left| \widehat{\text{OUT}}_m-\text{OUT}_m \right |\right |_2^2\\
	&=\frac{1}{M}\sum_{m=1}^M \sum_{k=1}^{n_3}\lp o_{k}(W,m)-\delta_{k,\iota(m)}\rp^2
\end{align}
\item The update rule for the weights is:
\[
\Delta W_n=-\text{grad} \lp f(W)\rp\cdot \frac{\gamma}{\epsilon_n}
\]
\end{itemize}
The formula for the partial derivative of the MSE with respect to a weight from the H-OUT layer, $w^{(2)}_{ij}$, is:
\begin{equation}
\frac{\partial f}{\partial w^{(2)}_{ij}}(W)=\frac{2}{M}\sum_{m=1}^M\sigma'\left(A_i^{(2)}(W,m)\right)h_j(W,m)\left[o_i(W,m)-\delta_{\iota(m),i}\right] \label{one}
\end{equation}
And when it is with respect to a weight from the IN-to-H layer, $w^{(1)}_{ij}$, we get:
\begin{equation}
\frac{\partial f}{\partial w^{(1)}_{ij}}(W)=\frac{2}{M}
\sum_{m=1}^M\left[\sigma'\left(A_i^{(1)}(W,m)\right)x_j
\sum_{k=1}^{n_3}\sigma'\left(A_k^{(2)}(W,m)\right) w^{(2)}_{ki}\left[o_k(W,m)-\delta_{\iota(m),k}\right] \right]\label{two}
\end{equation}

\section{Second experiment: `large' autoencoders with sparsity}
I trained the `large autoencoders' with the code presented in appendix~\ref{app:sparse}
\subsection{Results}
The following table was generated with the script presented in appendix~\ref{app:msemat} after training the 9 autoencoders.\vspace{0.8cm}\\
{\centering
\begin{tabular}{l | c  cccccccc}
MSE& $h=50$&$h=100$&$h=150$&
$h=250$&$h=300$&$h=350$&
$h=250$&$h=300$&$h=350$\\
table& &&&
$ \rho=0.05$&$\rho=0.05$&$ \rho=0.05$&
$ \rho=0.15$&$\rho=0.15$&$\rho=0.15$\\
\hline
training & 0.3485  &  0.3313  &  0.3385  &  0.0742  &  0.0744  &  0.0731  &  0.0720  &  0.0680  &  0.0647\\
test &  0.3495   & 0.3314  &  0.3367   & 0.0735 &   0.0737  &  0.0725   & 0.0714 &   0.0674  &  0.0638
\end{tabular}}
\section{Detailed analysis of hidden layer structure and efficiency}
I did not have time to do this part of the homework.
\bibliographystyle{plain}
\bibliography{auto}
\begin{appendices}
\section{MATLAB code}
\subsection{Custom network with NN Toolbox in MATLAB}
\label{app:create}
\lstinputlisting{create_NN.m}
\subsection{Training the `diabolo'-autoencoders}
\label{app:train_diabolo}
\lstinputlisting{train_autoencoder.m}
\subsection{Training autoencoders with sparsity constraint}
\label{app:sparse}
\lstinputlisting{train_sparse.m}
\begin{center}
\line(1,0){250}
\end{center}
\lstinputlisting{train_sparse_UH.m}
\subsection{Generating matrix of MSE on training and test sets for all autoencoders}
\label{app:msemat}
\lstinputlisting{mse_perf_small.m}
\end{appendices}
\end{document}


\documentclass[a4paper]{article}

\def\npart{II}

\def\ntitle{Algebraic Geometry}
\def\nlecturer{I.\ Grojnowski}

\def\nterm{Lent}
\def\nyear{2018}

\input{header}

\DeclareMathOperator{\Cl}{Cl}
\renewcommand*{\P}{\mathbb{P}}
\let\div\relax
\AtBeginDocument{DeclareMathOperator{\div}{div}}

\begin{document}

\input{titlepage}

\tableofcontents

\section{Algebraic curves}

\begin{eg}
  \(E^0 = \{y^2 = (x - \lambda_1)(x - \lambda_2)(x - \lambda_3)\}, \lambda_1\lambda_2\lambda_3 \neq 0, \lambda_i \neq \lambda_j\) for \(i \neq j\). Let \(E = \cl{E^0} \subseteq \P^2\). The closure is
  \[
    y^2t = (x - \lambda_1z)(x - \lambda_2z)(x - \lambda_3z).
  \]
  Each point on the other chart when \(z = 0\). Unique extra point \(x = z = 0, y \neq 0\), \(P_\infty = [0:1:0]\). We will compute \(L(nP_\infty)\). To begin, let's compute \(\div(x), div(y)\).
  \begin{align*}
    x &= 0 \text{ when } y = \pm \sqrt{-\lambda_1\lambda_2\lambda_3} = \pm c \\
    x &= \infty \text{ at } P_\infty
  \end{align*}
  At \(y = \pm c, \frac{\p f}{\p y} (0, \pm c) \neq 0\) so \(x\) is a local parameter so
  \[
    \div(x) = [0: c: 1] + [0: -c: 1] + n_\infty P_\infty.
  \]
  Let \(\deg \div(x) = 0\), so \(n_\infty = -2\). Similarly,
  \[
    \div(y) = \sum_{i = 1}^3 [\lambda_i: 0: -1] - 3 P_\infty.
  \]
  Hence \(x \in L(2 P_\infty), y \in L(3 P_\infty)\).

  Example \(xy \in L(5P_\infty)\), Claim \(L(P_\infty) = k\). Granting claim, get \(\dim L(nP_\infty) \leq n\) for \(n \geq 1\) by proposition v. But
  \begin{align*}
    1, x &\in L(2P_\infty) \\
    1, x, y &\in L(3P_\infty) \\
    1, x, y, x^2 &\in L(4P_\infty) \\
    1, x, y, x^2, xy &\in L(5P_\infty)
  \end{align*}
  Claim that these are linearly independent, so \(\dim L(nP_\infty) = n\) for \(n \leq 5\): this is obvious in \(k(x) \supseteq L(nP_\infty)\).
  \[
    1, x, y, x^2, xy, x^3, y^2 \in L(6P_\infty)
  \]
  but these are not linearly independent by our general result, or more importantly, \(y^2 = (x - \lambda_1)(x - \lambda_2)(x - \lambda_3)\) is the linear relation between them.
  \begin{ex}\leavevmode
    \begin{enumerate}
    \item Show that \(xx^i, x^iy\) are linearly independent and hence
    \item \(\dim L(nP_\infty) = n\) for all \(n \geq 1\).
    \end{enumerate}
  \end{ex}

  Observe how this compares with \(\P^1\), when \(\dim L(nP) = n + 1\) for \(n \geq 0\).

  Proof of claim that \(L(P_\infty) = k\): if not, \(L(P_\infty) = k + kt\) for some \(t \in k(E)\), then
  \[
    t^n \in L(nP_\infty) \setminus L((n - 1)P_\infty)
  \]
  so \(1, t, \dots, t^n \in L(nP_\infty)\) and are linearly independent so must be a basis (as \(\dim L(nP_\infty) \leq n + 1\)). But \(x \in L(2P_\infty), y \in L(3P_\infty)\), so \(x = a't^2 + b't + c = (at + b)^2 + d\) by completing the square if \(\ch k \neq 2\). Replace \(t\) with \(at + b\), so \(x = t^2 + d\) and \(y = g_3(t)\) for some cubic polynomial in \(t\). But that
  \[
    g_3(t)^2 = \prod_{i = 1}^3 (t^2 - (\lambda_i - d)).
  \]
  Now \(\lambda_i\)'s are distinct, so \(\lambda_i - d\) are distinct, so RHS is not a square in \(k[t]\), absurd.
\end{eg}

Given \(D \in \div X\) where \(X\) is a smooth projective curve, \(\ell(D) = \dim L(D) \geq 1\), put \(m = \ell (D) - 1\). Choose a basis \(f_0, \dots, f_m\) of \(L(D)\). Get a rational map \(\alpha_D = [f_0: \dots: f_n], X \to \P^m = \P(L(D)^*)\). As \(X\) is a smooth projective curve, \(\alpha_D\) extends to a morphism \(\alpha_D: X \to \P^m\). Notice that if \(D' = D + \div(g)\), \(f_0g, \dots, f_mg\) is a basis of \(L(D')\) but
\[
  [f_0g: \dots: f_mg] = [f_0: \dots: f_m]
\]
so \(\alpha_D: X \to \P^m = \P(L(D)^*)\) equals to \(\alpha_{D'}: X \to \P^m = \P(L(D')^*)\), i.e.\ the map only depends on the class of \(D\) in \(\Cl(X)\).

\begin{eg}
  \(X = \P^1, D = n(\alpha), \ell(D) = n + 1\). Choose a basis \(1, t, \dots, t^n\) of \(L(n(\infty))\), then
  \[
    \alpha_D(t) = [1:t: \dots: t^n]: \P^1 \to \P^n.
  \]
  i.e.
  \[
    \alpha_D[x_0: x_1] = [1: \frac{x_1}{x_0}: \dots: \frac{x_1^n}{x_0^n}] = [x_0^n: x_0^{n - 1}x_1: \dots: x_1^n].
  \]
\end{eg}

\begin{definition}[Embedding]\index{embedding}
  An \emph{embedding} is a morphism \(\alpha: X \to Y\) with the property that \(X\) is isomorphic to \(\alpha(X)\) via \(\alpha\).
\end{definition}

\begin{ex}\leavevmode
  \begin{enumerate}
  \item Show that the above \(\alpha_{n(\infty)}: \P^1 \to \P^n\) is an embedding.
  \item Show that the morphism
    \begin{align*}
      alpha: \P^1 &\to \P^2 \\
      t &\mapsto [1: t^2: t^3]
    \end{align*}
    is injective but \emph{not} an embedding.
  \end{enumerate}
\end{ex}

\begin{ex}
  Let \(X = E\) be elliptical curve, \(D = nP_\infty\). Show
  \begin{align*}
    \alpha_{P_\infty}: E &\to \P^0 \\
    \alpha_{2P_\infty}: E &\to \P^1 \\
    (x, y) &\mapsto x \\
    \alpha_{3P_\infty}: E &\to P^2 \\
    (x, y) &\mapsto (x, y)
  \end{align*}

  basis of \(L(2P_\infty)\) \(1, x\), basis of \(L(3P_\infty)\) \(1, x, y\).

  Notice that \(\alpha_{3P_\infty}\)  is an embedding \(E \subseteq \R^2\) but \(\alpha_{2P_\infty}, \alpha_{P_\infty}\) are not.
\end{ex}

\begin{theorem}[Embedding criteria]
  \(\alpha_D: X \to \P^m\) is an embedding if and only if for all \(p, q \in X\), \(\ell(D - p - q) = \ell(D) - 2\). When this happens, \(X\) is a curve in \(\P^m\) of degree \(\deg D\).
\end{theorem}

We have not yet defined the degree of a curve \(X \subseteq \P^m\). Suppose \(X \subseteq \P V, \dim V = m + 1\), \(X\) smooth. Let \(H \subseteq \P V\) be a hyperplane and suppose \(X \nsubseteq H\). Then \(X \cap H\) is a closed subvariety of \(X\), so a finite set of points, and we define
\[
  [X \cap H] \in \div X
\]
to be these points ``counted with multiplicity''. Define \(\deg X\) ot be \(\deg [X \cap H]\) which will depend on \(X \subseteq \P^m\) but no other choices.

\begin{eg}
  Let \(H = \{x_0 = 0\}\) for some linear function \(x_0 \in V^*\). If \(p \in X \cap H\), want to define \(v_p(x_0)\). But \(x_0 \in k(X)\). Pick \(x_1 \in V^*\) wit \(x_1(p) \neq 0\). Now \(\frac{x_0}{x_1} \in k(X)\). Then \(v_p(\frac{x_0}{x_1}\) makes sense, and
  \[
    v_p(\frac{x_0}{x_1'}) = v_p(\frac{x_0}{x_1}) + v_p(\frac{x_1}{x_1'}).
  \]
  If \(x_1'\) is another function in \(V^*\) with \(x_1'(p) \neq 0\) but then the last term is \(0\). So define
  \[
    n_p = v_p(\frac{x_0}{x_1})
  \]
  which is independent of choice of \(x_1\).
\end{eg}

\begin{definition}
  Let \([H \cap X] = \sum_{p \in H \cap X} n_pP \in \div (X), [H \cap X] \geq 0\). Define
  \[
    \deg X = \deg [H \cap X].
  \]
\end{definition}

If we pick \(H' = \{x_0' = 0\}, X \nsubseteq H'\).
\[
  v_p(\frac{x_0}{x_1'}) = v_p(\frac{x_0}{x_1}) + v_p(\frac{x_1}{x_1'})
\]
so \([H \cap X] = [H' \cap X] + \div (\frac{x_0}{x_0'})\), i.e.\ \([H \cap X] = [H' \cap X]\) are equal in \(\Cl(X)\).

\begin{lemma}
  Let \(X = Z(F) \subseteq \P^2\) where \(F\) is a homogeneous polynomial of degree \(d\), then
  \[
    \deg Z(F) = \deg F.
  \]
\end{lemma}
\printindex
\end{document}