\section{The Squeeze Theorem And Some Special Limits}\label{sec:TrigLimits}

The previous section could have been titled ``Using Known Limits to Find Unknown Limits.'' By knowing certain limits of functions, we can find limits involving sums, products, powers, etc., of these functions. We further the development of such comparative tools with the Squeeze Theorem, a clever and intuitive way to find the value of some limits.

In this section we aim to compute the limit:
$$\lim_{x\to0} {\sin x\over x}.$$

We start by analyzing the graph of $\ds{y=\frac{\sin x}{x}}$:
$$\includegraphics[width=3.0in]{images/sinx-over-x}$$
Notice that $x=0$ is not in the domain of this function.
Nevertheless, we can look at the limit as $x$ approaches $0$.
From the graph we find that the limit is $1$ (there is an open circle at $x=0$ indicating $0$ is not in the domain).
%We can expand upon the above limit and write it as follows:
%$$\lim_{x\to 0}\frac{\sin nx}{nx}=1.$$
We just convinced you this limit formula holds true based on the graph,
but how does one attempt to prove this limit more formally?
To do this we employ some indirect reasoning embodied in the \dfont{Squeeze Theorem}.

Before stating this theorem formally, suppose we have functions $f$, $g$ and $h$ where $g$ always takes on values between $f$ and $h$; that is, for all $x$ in an interval, $$f(x) \leq g(x) \leq h(x).$$ If $f$ and $h$ have the same limit at $c$, and $g$  is always ``squeezed'' between them, then $g$ must have the same limit as well. That is what the Squeeze Theorem states.

\begin{theorem}{Squeeze Theorem}{sqz}
{Let $f$, $g$ and $h$ be functions on an open interval $I$ containing $a$ such that for all $x$ in $I$, $$f(x)\leq g(x) \leq h(x).$$ If $$\lim_{x\to a} f(x) = L = \lim_{x\to a} h(x),$$ then $$\lim_{x\to a} g(x) = L.$$ \index{limit!Squeeze Theorem}\index{Squeeze Theorem}
}
\end{theorem}

\begin{center}
\begin{tikzpicture} %[default]
      %\diagram{-.5}{6}{-.5}{3}{1}
      \draw[->,ultra thick] (-.5,0)--(6,0) node[right]{$x$};
      \draw[->,ultra thick] (0,-.5)--(0,3) node[above]{$y$};
     % \diagramannotatez
      \draw[red,ultra thick] (-.5,1) to[out=-45,in=185,looseness=2] (4,2) to[out=0,in=135,looseness=1] node [at end,above] {$f(x)$} (6,.5);
      \draw[blue,ultra thick] (-.5,2.5) to[out=25,in=160,looseness=1] (4,2) to[out=10,in=180,looseness=1] node [at end,above] {$h(x)$} (6,3);
      \draw[green,ultra thick] (0.5,3.2) to[out=-45,in=175,looseness=1] (4,2) to[out=0,in=180,looseness=1] node [at end,below] {$g(x)$} (6,2.5);
      \draw[dashed] (4,0) -- node[at start,below] {$a$} (4,2) -- node[at end,left] {$L$} (0,2);
    \end{tikzpicture}
\end{center}

It can take some work to figure out appropriate functions by which to ``squeeze'' the given function of which you are trying to evaluate a limit. However, that is generally the only place work is necessary; the theorem makes the ``evaluating the limit part'' very simple.

We use the Squeeze Theorem in the following example to finally prove that $\ds \lim_{x\to 0} \frac{\sin x}{x} = 1$.\\

\begin{example}{Using the Squeeze Theorem}{ex_limit_sinx_prove}{
Use the Squeeze Theorem to show that $$\ds \lim_{x\to 0} \frac{\sin x}{x} = 1.$$}
\end{example}

\begin{solution}
{We begin by considering the unit circle. Each point on the unit circle has coordinates $(\cos \theta,\sin \theta)$ for some angle $\theta$ as shown in Figure \ref{fig:squeeze_sinx}. Using similar triangles, we can extend the line from the origin through the point to the point $(1,\tan \theta)$, as shown. (Here we are assuming that $0\leq \theta \leq \pi/2$. Later we will show that we can also consider $\theta \leq 0$.)

\mfigure{.7}{The unit circle and related triangles.}{fig:squeeze_sinx}{\begin{tikzpicture}[
>=stealth, scale=2]
\draw [thick] (0,0) node [shift={(10pt,4pt)}] {\scriptsize$\theta$} circle (1);
\draw [<->,thick] (-1.1,0) -- (1.1,0);
\draw [<->,thick] (0,-1.1) -- (0,1.1);
\draw [thick] (0,0) -- (1,.839) node [above] {\scriptsize$(1,\tan \theta)$}-- (1,0) -- (.766,.643);
\fill [black] (1,.839) circle (1.5pt);
\fill [black] (.766,.643) node [left] {\scriptsize$(\cos \theta,\sin \theta)$} circle (1.5pt);
\fill [black] (1,0) node [below right] {\scriptsize$(1,0)$} circle (1.5pt);
\end{tikzpicture}
}

Figure \ref{fig:squeeze_sinx} shows three regions have been constructed in the first quadrant, two triangles and a sector of a circle, which are also drawn below. The area of the large triangle is $\frac12\tan\theta$; the area of the sector is $\theta/2$; the area of the triangle contained inside the sector is $\frac12\sin\theta$. It is then clear from the diagram that we get the inequality in Figure \ref{fig:STpic}.

\begin{figure}[H]
	\centering
	\begin{subfigure}[t]{0.33\textwidth}
\begin{tikzpicture}[>=stealth,scale=3.5]
\fill [draw=black,thick,fill=blue!20] (0,0) node [shift={(10pt,4pt)}] {\scriptsize$\theta$} -- (1,.839) -- node [pos=.5,right] {\scriptsize$\tan \theta$} (1,0) -- cycle;
\draw (.5,0) node [below] {\scriptsize$1$};
\draw [black,dashed,thick] (1,0) arc (0:40:1);
\draw [black,dashed,thick] (1,0) -- (.766,.643);
\end{tikzpicture}
        \label{fig:STpica}
        \caption{$\ds \frac{\tan \theta}{2}$}
    \end{subfigure}%
    \begin{subfigure}[t]{0.33\textwidth}
     \begin{tikzpicture}[ %x=.5\marginparwidth,y=.5\marginparwidth,
     >=stealth,scale=3.5]
     \fill [draw=black,thick,fill=blue!20] (0,0) node [shift={(10pt,4pt)}] {\scriptsize$\theta$} -- (1,0) arc(0:40:1) -- cycle;
     \draw (.5,0) node [below] {\scriptsize$1$};
     \draw [black,dashed,thick] (1,0) arc (0:40:1);
     \draw [black,dashed,thick] (1,0) -- (.766,.643) -- (1,.839) -- cycle;
     \end{tikzpicture}
        \label{fig:STpicb}
        \caption{$\ds \frac{\theta}{2}$}
    \end{subfigure}
\begin{subfigure}[t]{0.33\textwidth}
     \begin{tikzpicture}[ %x=.5\marginparwidth,y=.5\marginparwidth,
     >=stealth,scale=3.5]
     \fill [draw=black,thick,fill=blue!20] (0,0) node [shift={(10pt,4pt)}] {\scriptsize$\theta$} -- (1,0) --(.766,.643) -- cycle;
     \draw [dashed,thick] (.766,0)  -- node [pos=.4,left] {\scriptsize$\sin \theta$}(.766,.643);
     \draw (.766,5pt) -- ++(5pt,0) -- ++(0,-5pt);
     \draw (.5,0) node [below] {\scriptsize$1$};
     %\draw [black,dashed] (1,0) arc (0:40:1);
     \draw [black,dashed,thick] (1,0) arc(0:40:1) --  (1,.839) -- cycle;
     \end{tikzpicture}
        \label{fig:STpicc}
        \caption{$\ds \frac{\sin \theta}{2}$}
    \end{subfigure}
    \caption{Demonstrating that $\ds \frac{\tan \theta}{2}  \geq \frac{\theta}{2} \geq \frac{\sin \theta}{2}$   \label{fig:STpic} }
\end{figure}

%\begin{center}
%\begin{tabular}{ccccc}
%\myincludegraphics{figures/figSqueeze1a} & & \myincludegraphics{figures/figSqueeze1b} & & \myincludegraphics{figures/figSqueeze1c}\\
%$\ds \frac{\tan \theta}{2}$\rule{0pt}{25pt} & $\geq$ & $\ds \frac{\theta}{2}$ & $\geq$ & $\ds \frac{\sin \theta}{2}$
%\end{tabular}
%\end{center}

%$$\frac{\tan\theta}{2} \geq \frac{\theta}{2} \geq \frac{\sin \theta}{2}.$$

Multiply all terms by $\ds\frac{2}{\sin \theta}$, giving $$\frac{1}{\cos\theta} \geq \frac{\theta}{\sin \theta} \geq 1.$$

Taking reciprocals reverses the inequalities, giving $$ \cos \theta \leq \frac{\sin \theta}{\theta} \leq 1.$$ (These inequalities hold for all values of $\theta$ near 0, even negative values, since $\cos (-\theta) = \cos \theta$ and $\sin (-\theta) = -\sin \theta$.)

Now take limits.

$$\lim_{\theta\to 0} \cos \theta \leq \lim_{\theta\to 0} \frac{\sin\theta}{\theta} \leq \lim_{\theta\to 0}  1 $$
$$\cos 0 \leq \lim_{\theta\to 0} \frac{\sin\theta}{\theta} \leq  1 $$
$$1 \leq \lim_{\theta\to 0} \frac{\sin\theta}{\theta} \leq  1 $$

Clearly this means that $\ds \lim_{\theta\to 0} \frac{\sin\theta}{\theta}=1$.\\
}
\end{solution}

Two notes about the previous example are worth mentioning. First, one might be discouraged by this application, thinking ``I would \textit{never} have come up with that on my own. This is too hard!'' Don't be discouraged; within this text we will guide you in your use of the Squeeze Theorem. As one gains mathematical maturity, clever proofs like this are easier and easier to create.

Second, this limit tells us more than just that as $x$ approaches $ 0 $, $\sin(x)/x$ approaches $ 1 $. Both $x$ and $\sin x$ are approaching $ 0 $, but the \textit{ratio} of $x$ and $\sin x$ approaches $ 1 $, meaning that they are approaching $ 0 $ in essentially the same way. Another way of viewing this is: for small $x$, the functions $y=x$ and $y=\sin x$ are essentially indistinguishable.\\

We include this special limit, along with three others, in the following theorem.

\begin{theorem}{Special Limits}{special_limits}{%
\noindent\begin{minipage}[t]{.5\textwidth}
\begin{enumerate}
	\item		$\ds \lim_{x\to 0} \frac{\sin x}{x} = 1$
	\item		$\ds \lim_{x\to 0} \frac{\cos x-1}{x} = 0$
\end{enumerate}
\end{minipage}
\begin{minipage}[t]{.5\textwidth}
\begin{enumerate}\addtocounter{enumi}{2}
	\item		$\ds \lim_{x\to 0} (1+x)^\frac1x = e$
	\item		$\ds \lim_{x\to 0} \frac{e^x-1}{x} = 1$
\end{enumerate}
\end{minipage}
}
\end{theorem}

A short word on how to interpret the latter three limits. We know that as $x$ goes to 0, $\cos x$ goes to 1. So, in the second limit, both the numerator and denominator are approaching 0. However, since the limit is 0, we can interpret this as saying that ``$\cos x$ is approaching 1 faster than $x$ is approaching 0.''

In the third limit, inside the parentheses we have an expression that is approaching 1 (though never equaling 1), and we know that 1 raised to any power is still 1. At the same time, the power is growing toward infinity. What happens to a number near 1 raised to a very large power? In this particular case, the result approaches Euler's number, $e$, approximately $2.718.$

In the fourth limit, we see that as $x\to 0$, $e^x$ approaches 1 ``just as fast'' as $x\to 0$, resulting in a limit of 1.\\

\begin{example}{Limit of Other Trig Functions}{LimitOtherTrigFunctions}
Compute the following limit $\ds\lim_{x\to 0}\frac{\sin 5x\cos x}{x}$.
\end{example}

\begin{solution}
We have
$$\begin{array}{rcl}
\ds{\lim_{x\to 0}\frac{\sin 5x\cos x}{x}} & = & \ds{\lim_{x\to 0}\frac{5\sin 5x\cos x}{5x}}\\
\\
~ & = & \ds{\lim_{x\to 0} 5\cos x\left(\frac{\sin 5x}{5x}\right)}\\
\\
~ & = &  5\cdot (1)\cdot (1)~~=~~5\\
\end{array}$$
since $\cos(0)=1$ and $\ds{\lim_{x\to 0}\frac{\sin 5x}{5x}=1}$.
\end{solution}
Let's do a harder one now.

\begin{example}{Limit of Other Trig Functions}{LimitOtherTrigFunctions2}
Compute the following limit:
$\ds\lim_{x\to 0}\frac{\tan^3 2x}{x^2\sin 7x}$.
\end{example}

\begin{solution}
Recall that the $\tan^3(2x)$ means that $\tan(2x)$ is being raised to the third power.
$$\begin{array}{rcll}
\ds{\lim_{x\to 0}\frac{\tan^3(2x)}{x^2\sin(7x)}}&=& \ds{\lim_{x\to 0}\frac{(\sin(2x))^3}{x^2\sin(7x)\cos^3(2x)}}
& \mbox{Rewrite in terms of $\sin$ and $\cos$}\\
\\
~&=&\ds{\lim_{x\to 0}\frac{(2x)^3\left(\frac{\sin(2x)}{2x}\right)^3}{x^2(7x)\left(\frac{\sin(7x)}{7x}\right)\cos^3(2x)}}
& \mbox{Make sine terms look like: $\ds{\frac{\sin\theta}{\theta}}$}\\
\\
~&=&\ds{\lim_{x\to 0}\frac{8x^3(1)^3}{7x^3(1)(1^3)}}
& \mbox{Replace $\ds{\lim_{x\to 0}\frac{\sin nx}{nx}}$ with $1$. Also, $\cos(0)=1$.}\\
\\
~&=&\ds{\lim_{x\to 0}\frac{8}{7}}&\mbox{Cancel $x^3$'s.}\\
\\
~&=&\ds{\frac{8}{7}}.\\
\end{array}$$
\end{solution}

\begin{example}{Applying the Squeeze Theorem}{Applying the Squeeze Theorem}
Compute the following limit:
$\ds\lim_{x\to 0^+}x^3\cos\left(\frac{1}{\sqrt{x}}\right)$.
\end{example}

\begin{solution}
We use the \ifont{Squeeze Theorem} to evaluate this limit.
We know that $\cos\alpha$ satisfies $-1\leq\cos\alpha\leq 1$ for any choice of $\alpha$.
Therefore we can write:
$$-1\leq\cos\left(\frac{1}{\sqrt{x}}\right)\leq 1$$
Since $x\to 0^+$ implies $x>0$, multiplying by $x^3$ gives:
$$-x^3\leq x^3\cos\left(\frac{1}{\sqrt{x}}\right)\leq x^3.$$
$$\lim_{x\to 0^+}(-x^3)\leq \lim_{x\to 0^+}\left(x^3\cos\left(\frac{1}{\sqrt{x}}\right)\right)\leq\lim_{x\to 0^+} x^3.$$
But using our rules we know that
$$\lim_{x\to 0^+}(-x^3)=0,\qquad\qquad \lim_{x\to 0^+} x^3=0$$
and the Squeeze Theorem says that the only way this can happen is if
$$\lim_{x\to 0^+}x^3\cos\left(\frac{1}{\sqrt{x}}\right)=0.$$
\end{solution}

%\begin{theorem}{Squeeze Theorem}{SqueezeTheorem}
%Suppose that $g(x) \le f(x) \le h(x)$ for all $x$ close to $a$ but not
%equal to $a$. If $\ds\lim_{x\to a}g(x)=L=\lim_{x
%\to a}h(x)$, then $\ds\lim_{x\to a}f(x)=L$.
%\end{theorem}
%
%This theorem can be proved using the official definition of limit. We
%won't prove it here, but point out that it is easy to understand and
%believe graphically. The condition says that $f(x)$ is trapped between
%$g(x)$ below and $h(x)$ above, and that at $x=a$, both $g$ and $h$
%approach the same value. This means the situation looks something like
%Figure~\ref{fig:squeeze}.
%
%For example, imagine the blue curve is $f(x)=x^2\sin(\pi/x)$, the
%upper (red) and lower (green) curves are $h(x)=x^2$ and $g(x)=-x^2$. Since the sine function
%is always between $-1$ and $1$, $-x^2\le x^2\sin(\pi/x)\le x^2$, and
%it is easy to see that $\lim_{x\to0}-x^2=0=\lim_{x\to0}x^2$.
%It is not so easy to see directly (i.e. algebraically) that
%$\lim_{x\to0}x^2\sin(\pi/x)=0$, because the $\pi/x$ prevents us from
%simply plugging in $x=0$. The squeeze theorem makes this ``hard
%limit'' as easy as the trivial limits involving $x^2$.

%\figure[!ht]
%\centerline{\vbox{\beginpicture
%\normalgraphs
%%\ninepoint
%\setcoordinatesystem units <4truecm,4truecm>
%\setplotarea x from -1 to 1, y from -1 to 1
%\axis left shiftedto x=0 /
%\axis bottom shiftedto y=0 /
%\setquadratic
%\plot -1.000 0.000 -0.980 0.060 -0.961 0.118 -0.941 0.173 -0.922 0.224
%-0.902 0.272 -0.882 0.317 -0.863 0.357 -0.843 0.392 -0.824 0.423
%-0.804 0.448 -0.784 0.468 -0.765 0.481 -0.745 0.488 -0.726 0.488
%-0.706 0.481 -0.686 0.467 -0.667 0.445 -0.647 0.415 -0.628 0.377
%-0.608 0.332 -0.588 0.280 -0.569 0.223 -0.549 0.161 -0.530 0.096
%-0.510 0.032 -0.490 -0.030 -0.471 -0.084 -0.451 -0.128 -0.432 -0.156
%-0.412 -0.165 -0.392 -0.152 -0.373 -0.117 -0.353 -0.063 -0.334 -0.001
%-0.314 0.054 -0.294 0.082 -0.275 0.068 -0.255 0.016 -0.236 -0.039
%-0.216 -0.043 -0.196 0.011 -0.177 0.028 -0.157 -0.022 -0.138 0.014
%-0.118 -0.014 -0.098 -0.005 -0.079 -0.005 -0.059 -0.001 -0.040 0.001
%-0.020 0.000 0 0
%0.020 0.000 0.040 -0.001 0.059 0.001 0.079 0.005 0.098 0.005
%0.118 0.014 0.138 -0.014 0.157 0.022 0.177 -0.028 0.196 -0.011
%0.216 0.043 0.236 0.039 0.255 -0.016 0.275 -0.068 0.294 -0.082
%0.314 -0.054 0.334 0.001 0.353 0.063 0.373 0.117 0.392 0.152
%0.412 0.165 0.432 0.156 0.451 0.128 0.471 0.084 0.490 0.030
%0.510 -0.032 0.530 -0.096 0.549 -0.161 0.569 -0.223 0.588 -0.280
%0.608 -0.332 0.628 -0.377 0.647 -0.415 0.667 -0.445 0.686 -0.467
%0.706 -0.481 0.726 -0.488 0.745 -0.488 0.765 -0.481 0.784 -0.468
%0.804 -0.448 0.824 -0.423 0.843 -0.392 0.863 -0.357 0.882 -0.317
%0.902 -0.272 0.922 -0.224 0.941 -0.173 0.961 -0.118 0.980 -0.060
%1.000 0.000 /
%\plot -1 1 0 0 1 1 /
%\plot -1 -1 0 0 1 -1 /
%\endpicture}}
%\caption{The squeeze theorem. \label{fig:squeeze}}
%\endfigure

%\begin{figure}[!ht]
%$$\includegraphics[width=3.0in]{images2/squeeze-theorem}$$
%\caption{The squeeze theorem. \label{fig:squeeze}}
%\end{figure}
%
%To compute $\ds\lim_{x\to0}
%(\sin x)/x$, we will find two simpler functions $g$ and $h$ so that
%$g(x)\le (\sin x)/x\le h(x)$, and so that
%$\lim_{x\to0}g(x)=\lim_{x\to0}h(x)$. Not too surprisingly, this will
%require some trigonometry and geometry. Referring to
%figure~\ref{fig:hard limit}, $x$ is the measure of the angle in
%radians. Since the circle has radius 1, the coordinates of point $A$
%are $(\cos x,\sin x)$, and the area of the small triangle is
%$(\cos x\sin x)/2$. This triangle is completely contained within the
%circular wedge-shaped region bordered by two lines and the circle from
%$(1,0)$ to point $A$. Comparing the areas of the triangle and the
%wedge we see
%$(\cos x\sin x)/2 \le x/2$, since the area of a circular region with
%angle $\theta$ and radius $r$ is $\theta r^2/2$. With a little algebra
%this turns into $(\sin x)/x \le 1/\cos x$, giving us the $h$ we seek.
%
%\figure[!ht]
%\centerline{\vbox{\beginpicture
%\normalgraphs
%%\ninepoint
%\setcoordinatesystem units <4truecm,4truecm>
%\setplotarea x from 0 to 1, y from 0 to 1
%\circulararc 90 degrees from 1 0 center at 0 0
%\axis left ticks numbered from 0 to 1 by 1 /
%\axis bottom ticks numbered from 1 to 1 by 1 /
%\putrule from  0.8660254040 0 to 0.8660254040 0.5
%\putrule from  1 0 to 1 .577
%\plot 0 0 1 .577 /
%\put {$x$} [bl] <5pt,2.5pt> at 0.1 0
%\put {$A$} [r] <-6pt,2pt> at 0.8660254040 0.5
%\put {$B$} [bl] <3pt,3pt> at 1  .577
%\circulararc 30 degrees from 0.1 0 center at 0 0
%\endpicture}}
%\caption{Visualizing $\sin x / x$. \label{fig:hard limit}}
%\endfigure
%
%To find $g$, we note that the circular wedge is completely contained
%inside the larger triangle. The height of the triangle, from $(1,0)$
%to point $B$, is $\tan x$, so comparing areas we get
%$x/2 \le (\tan x)/2 = \sin x / (2\cos x)$. With a little algebra this
%becomes $\cos x \le (\sin x)/x$. So now we have
%$$ \cos x \le {\sin x\over x}\le {1\over\cos x}.$$
%Finally, the two limits $\lim_{x\to0}\cos x$ and $\lim_{x\to0}1/\cos x$
%are easy, because $\cos(0)=1$. By the squeeze theorem,
%$\lim_{x\to0} (\sin x)/x = 1$ as well.
%
%Using the above, we can compute a similar limit:
%$$\lim_{x\to0}{\cos x - 1\over x}.$$
%This limit is just as hard as $\sin x/x$, but closely related to it,
%so that we don't have to do a similar calculation; instead we can do a
%bit of tricky algebra.
%$${\cos x - 1\over x}={\cos x - 1\over x}{\cos x+1\over\cos x+1}
%={\cos^2 x - 1\over x(\cos x+1)}={-\sin^2 x\over x(\cos x+1)}=
%-{\sin x\over x}{\sin x\over \cos x + 1}.$$
%To compute the desired limit it is sufficient to compute the limits of
%the two final fractions, as $x$ goes to 0. The first of these is the
%hard limit we've just done, namely 1. The second turns out to be
%simple, because the denominator presents no problem:
%$$\lim_{x\to0}{\sin x\over \cos x + 1}={\sin 0\over \cos 0+1}=
%{0\over 2}  = 0.$$
%Thus,
%$$\lim_{x\to0}{\cos x - 1\over x}=0.$$

When solving problems using the Squeeze Theorem it is also helpful to have the following theorem.

\begin{theorem}{Monotone Limits}{MonotoneLimits}\label{MonotoneLimits}
If $f(x)\leq g(x)$ when $x$ is near $a$ (except possibly at $a$) and the limits of $f$ and $g$ both exist as $x$ approaches $a$, then $\ds\lim_{x\to a}f(x)\leq\lim_{x\to a}g(x)$.
\end{theorem}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Opensolutionfile{solutions}[ex]
\section*{Exercises for \ref{sec:TrigLimits}}

\begin{enumialphparenastyle}

%%%%%%%%%%
\begin{ex}
Compute the following limits.
\begin{enumerate}
	\item	$\ds\lim_{x\to 0} {\sin (5x)\over x}$
	\item	$\ds\lim_{x\to 0 } {\sin(7x)\over\sin (2x)}$
	\item	$\ds\lim_{x\to 0 } {\cot (4x) \over\csc (3x)}$
	\item	$\ds\lim_{x\to 0 } {\tan x\over x}$
	\item	$\ds\lim_{x\to \pi/4} {\sin x-\cos x \over\cos (2x)}$
\end{enumerate}
\begin{sol}
\begin{enumerate}
	\item	$5$
	\item	$7/2$
	\item	$3/4$
	\item	$1$
	\item	$-\sqrt2/2$
\end{enumerate}
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
For all $x\geq 0$, $4x-9 \leq f(x) \leq x^2 - 4x +7$. Find $\ds\lim_{x\to4}f(x)$.
\begin{sol}
 $7$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
For all $x$, $2x \leq g(x) \leq x^4 - x^2 +2$. Find $\ds\lim_{x\to1}g(x)$.
\begin{sol}
 $2$
\end{sol}
\end{ex}

% % % % % % % % % % %
\begin{ex}
{Use the Squeeze Theorem where appropriate, to evaluate the given limit.}

\begin{enumerate}
\item {$\ds \lim_{x\to0} x\sin\left(\frac{1}{x}\right)$}

\item {$\ds \lim_{x\to0} \sin x\cos\left(\frac{1}{x^2}\right)$}

\item {$\ds \lim_{x\to0} x\sin\left(\frac{1}{x}\right)$}

\item {$\ds \lim_{x\to3^+} f(x)$, where $6x-9\leq f(x) \leq x^2$ on $[0,3]$.}

\end{enumerate}

\begin{sol}
\begin{enumerate}
\item {$0$}
\item {$0$}
\item {$0$}
\item {$9$}
\end{enumerate}
\end{sol}

\end{ex}
% % % % % % % % % % % %

%%%%%%%%%%
\begin{ex}
Use the Squeeze Theorem to show that $\ds\lim_{x\to0} x^4 \cos(2/x)=0$.
\end{ex}

%%%%%%%%%%
\begin{ex}
Find the value of $\lim_{x\to\infty}\dfrac{3x+\sin x}{x+\cos x}$. Justify your steps carefully.
\begin{sol}
	$ 3 $
\end{sol}
\end{ex}

% % % % % % % % % % %
\begin{ex}
{\noindent The following exercises}
{ challenge your understanding of limits but can be evaluated using the knowledge gained in this section.}
\begin{enumerate}
\item {$\ds \lim_{x\to0}\frac{\sin 3x}{x}$}

\item {$\ds \lim_{x\to0}\frac{\sin 5x}{8x}$}

\item {$\ds \lim_{x\to0}\frac{\ln (1+x)}{x}$}

\item {$\ds \lim_{x\to0}\frac{\sin x}{x}$, where $x$ is measured in degrees, not radians.}

\end{enumerate}

\begin{sol}
\begin{enumerate}
\item {$3$}
\item {$5/8$}
\item {$1$}
\item {$\pi/180$}
\end{enumerate}
\end{sol}

\end{ex}
% % % % % % % % % % % %

\end{enumialphparenastyle}


\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{parskip}
\usepackage{svg}
\usepackage[utf8]{inputenc}
\usepackage{helvet}
\renewcommand{\familydefault}{\sfdefault}
\usepackage{geometry}
\usepackage[document]{ragged2e}
\geometry{letterpaper, portrait, top=1in, bottom=1in, left=1.5in, right=1.5in}

\title{Math 301}

\begin{document}

\section{Set Notation}
\begin{align*}
    \mathbb{N} &= natural\ numbers = \{0, 1, 2 \cdots\} \\
    \mathbb{Z} &= integers = \{ \cdots -2, -1, 0, 1, 2 \cdots \}\\
    \mathbb{Q} &= rational\ numbers\ (can\ be\ expressed\ with\ fractions\ of\ two\ integers) \\
    \mathbb{R} &= real\ numbers\ (rational\ numbers\ and\ irrational\ numbers)
\end{align*}

A is an element of B
\begin{gather*}
    A \in B
\end{gather*}

A is a subset of b
\begin{gather*}
    A \subseteq B
\end{gather*}

The cardinality of A
\begin{gather*}
    |A|
\end{gather*}

Complement of A
\begin{gather*}
    A^C \\
    \overline{A}
\end{gather*}

The intersection of A and B
\begin{gather*}
    A \cap B
\end{gather*}

The union of A and B
\begin{gather*}
    A \cup B
\end{gather*}

The symmetric difference of A and B
\begin{gather*}
    A \oplus B = (A \cup B) - (A \cap B)
\end{gather*}

\section{Cartesian Products}

The Cartesian product of A and B
\begin{gather*}
    A \times B = {(a, b) | a \in A, b \in B}
\end{gather*}

\begin{gather*}
    |A \times B| = |A| \times |B|
\end{gather*}

The power set of A is the set of all subsets of A.
\begin{gather*}
    \mathcal{P}(A)
\end{gather*}

\begin{gather*}
    |\mathcal{P}(A)| = 2^{|A|}
\end{gather*}

\section{The Rule of Products}
Given the number of possibilities for two independent events denoted $|A|$ and $|B|$, the number of possible combinations of both events is $|A| \times |B|$. You can also say "the number of ways to do $A$ \textbf{AND} $B$ is $|A| \times |B|$."

\section{The Law of Addition}

The basic law of addition
\begin{gather*}
    |A| = |A_1| + |A_2| + \cdots + |A_n| = \sum_{k=1}^{n} |A_k|
\end{gather*}

Given the number of possibilities for two mutually exclusive events denoted $|A|$ and $|B|$, the sum of possible events is $|A| + |B|$. "The number of ways to do $A$ \textbf{OR} $B$ is $|A| + |B|$."

Partition of a set
\begin{gather*}
    A_1 \cup A_2 \cup A_3 \cup \cdots = A \\
    A_i \cap A_j = \emptyset\ where\ i \neq j\
\end{gather*}

Law of inclusion-exclusion for two sets

\begin{gather*}
    |A_1 \cup A_2| = |A_1| + |A_2| - |A_1 \cap A_2|
\end{gather*}

\begin{gather*}
\end{gather*}

\section{Permutation}
The total number of ordered sets of $k$ elements taken from a set of $n$ elements
\begin{gather*}
    P(n,k) = \frac{n!}{(n-k)!}
\end{gather*}

\section{Combination}
The total number of unordered sets of $k$ elements taken from a set of $n$ elements
\begin{gather*}
    \binom{n}{k} = \frac{n!}{(n-k)!k!}
\end{gather*}

Binomial theorem---Allows you to expand $(x+y)^n$.
\begin{gather*}
    (x + y)^n = \sum_{k=0}^{n} \binom{n}{k}x^{n-k}y^k
\end{gather*}

\section{Logical operator}

Logical conjunction (AND)
\begin{gather*}
    p \land q \\
    \begin{tabular}{c|c|c}
        p & q & $p \land q$ \\
        \hline
        0 & 0 & 0 \\
        0 & 1 & 0 \\
        1 & 0 & 0 \\
        1 & 1 & 1
    \end{tabular}
\end{gather*}

Logical disjunction (OR)
\begin{gather*}
    p \lor q \\
    \begin{tabular}{c|c|c}
        p & q & $p \lor q$ \\
        \hline
        0 & 0 & 0 \\
        0 & 1 & 1 \\
        1 & 0 & 1 \\
        1 & 1 & 1
    \end{tabular}
\end{gather*}

Logical negation
\begin{gather*}
    \neg p \\
    \begin{tabular}{c|c}
        p & $\neg p$ \\
        \hline
        0 & 1 \\
        1 & 0
    \end{tabular}
\end{gather*}

Conditional statement (if $p$ is true then $q$ is also true)
\begin{gather*}
    p \rightarrow q \\
    \begin{tabular}{c|c|c}
        p & q & $p \rightarrow q$ \\
        \hline
        0 & 0 & 1 \\
        0 & 1 & 1 \\
        1 & 0 & 0 \\
        1 & 1 & 1
    \end{tabular}
\end{gather*}

Converse
\begin{gather*}
    p \leftarrow q \\
    or \\
    q \rightarrow p
\end{gather*}

Contrapositive
\begin{gather*}
    \neg q \rightarrow \neg p \\
    \begin{tabular}{c|c|c}
        p & q & $\neg q \rightarrow \neg p$ \\
        \hline
        0 & 0 & 1 \\
        0 & 1 & 1 \\
        1 & 0 & 0 \\
        1 & 1 & 1
    \end{tabular}
\end{gather*}

Biconditional (XNOR)
\begin{gather*}
    p \leftrightarrow q \\
    \begin{tabular}{c|c|c}
        p & q & $p \leftrightarrow q$ \\
        \hline
        0 & 0 & 1 \\
        0 & 1 & 0 \\
        1 & 0 & 0 \\
        1 & 1 & 1
    \end{tabular}
\end{gather*}

Sheffer stroke (NAND)
\begin{gather*}
    p | q \\
    \begin{tabular}{c|c|c}
        p & q & $p | q$ \\
        \hline
        0 & 0 & 1 \\
        0 & 1 & 1 \\
        1 & 0 & 1 \\
        1 & 1 & 0
    \end{tabular}
\end{gather*}

Order of precedence of propositions
\begin{enumerate}
    \item Parentheses
    \item Negation
    \item Conjunction
    \item Disjunction
    \item Conditional statement
    \item Biconditional
\end{enumerate}

\section{Equivalence and implication}

Tautology: An expression that is true in all cases

Contradiction: An expression that is false for all cases

Equivalence ($r \leftrightarrow s$ is a tautology)
\begin{gather*}
    r \Leftrightarrow s
\end{gather*}

Implication (r implies s)
\begin{gather*}
    r \Rightarrow s
\end{gather*}

\section{Laws of logic}

Duality principle: Each law of logic can be used to derive a second law by switching the symbols $\land$ with $\lor$, $1$ with $0$ and visa versa.

Commutative laws
\begin{gather*}
    p \lor q \Leftrightarrow q \lor p \\
    p \land q \Leftrightarrow q \land p
\end{gather*}

Associative laws
\begin{gather*}
    (p \lor q) \lor r \Leftrightarrow p \lor (q \lor r) \\
    (p \land q) \land r \Leftrightarrow p \land (q \land r)
\end{gather*}

Distributive laws
\begin{gather*}
    p \land (q \lor r) \Leftrightarrow (p \land q) \lor (p \land r) \\
    p \lor (q \land r) \Leftrightarrow (p \lor q) \land (p \lor r) \\
\end{gather*}

Identity laws
\begin{gather*}
    p \lor 0 \Leftrightarrow p \\
    p \land 1 \Leftrightarrow p
\end{gather*}

Negation laws
\begin{gather*}
    p \land \neg p \Leftrightarrow 0 \\
    p \lor \neg p \Leftrightarrow 1
\end{gather*}

Idempotent laws
\begin{gather*}
    p \lor p \Leftrightarrow p \\
    p \land p \Leftrightarrow p
\end{gather*}

Null laws
\begin{gather*}
    p \land 0 \Leftrightarrow 0 \\
    p \lor 1 \Leftrightarrow 1
\end{gather*}

Absorption laws
\begin{gather*}
    p \land (p \lor q) \Leftrightarrow p \\
    p \lor (p \land q) \Leftrightarrow p
\end{gather*}

DeMorgan's laws
\begin{gather*}
    \neg (p \lor q) \Leftrightarrow (\neg p) \land (\neg q) \\
    \neg (p \land q) \Leftrightarrow (\neg p) \lor (\neg q)
\end{gather*}

Involution law
\begin{gather*}
    \neg (\neg p) \Leftrightarrow p
\end{gather*}

Detachment
\begin{gather*}
    (p \rightarrow q) \land p \Rightarrow q
\end{gather*}

Indirect reasoning
\begin{gather*}
    (p \rightarrow q) \land \neg q \Rightarrow \neg p
\end{gather*}

Disjunctive addition
\begin{gather*}
    p \Rightarrow (p \lor q)
\end{gather*}

Conjunctive simplification
\begin{gather*}
    (p \land q) \Rightarrow p \\
    (p \land q) \Rightarrow q
\end{gather*}

Disjunctive simplification
\begin{gather*}
    (p \lor q) \land \neg p \Rightarrow q \\
    (p \lor q) \land \neg q \Rightarrow p
\end{gather*}

Chain rule
\begin{gather*}
    (p \rightarrow q) \land (q \rightarrow r) \Rightarrow (p \rightarrow r)
\end{gather*}

Conditional equivalence
\begin{gather*}
    p \rightarrow q \Leftrightarrow \neg p \lor q
\end{gather*}

Biconditional equivalences
\begin{gather*}
    (p \leftrightarrow q) \Leftrightarrow (p \rightarrow q) \land (q \rightarrow p) \Leftrightarrow (p \land q) \lor (\neg p \land \neg q)
\end{gather*}

Contrapositive
\begin{gather*}
    (p \rightarrow q) \Leftrightarrow (\neg q \rightarrow \neg p)
\end{gather*}

\section{Propositions over a Universe}

If $p$ is a proposition over $U$, the truth set of $p$ is $T_p = \{a \in U | p(a)\ is\ true\}$.

\section{Mathematical induction}

Mathematical induction is a way to prove a proposition over natural numbers.

\begin{enumerate}
    \item Prove the basis of the statement, $P(n)$ where $n = 0$.
    \item Assume the statement is true for $n - 1$. Prove $P(n-1) \Rightarrow P(n)$.
\end{enumerate}

Stronk induction

\begin{enumerate}
    \item Prove the bases of the statement, $P(m)$, for an arbitrary $m > 0$.
    \item Assume the statement is true for $k$ where $m \leq k < n$. Prove $P(k) \Rightarrow n$.
\end{enumerate}

\section{Quantifiers}

The existential quantifier states that there exists an $n$ such that $p(n)$ is true.
\begin{gather*}
    (\exists n)(p(n))
\end{gather*}

The universal quantifier states that for all $n$ in $U$, $p(n)$ is true.
\begin{gather*}
    (\forall n)(p(n))
\end{gather*}

Negation of quantified propositions
\begin{gather*}
    \neg ((\forall n)(p(n))) \Leftrightarrow (\exists n)(\neg p(n)) \\
    \neg ((\exists n)(p(n))) \Leftrightarrow (\forall n)(\neg p(n))
\end{gather*}

Multiple quantifiers of the same type can be arranged in any order, but mixed quantifiers cannot be exchanged.

\section{Proofs for sets}

You can prove set propositions using Venn diagrams, truth tables (true indicating that $x \in A$ or with definitions.

To prove that $A \subseteq B$, show that $x \in A$ and $x \in B$. \\
To prove that $A = B$, show that $A \subseteq B$ and $B \subseteq A$.

\section{Laws of set theory}

Commutative laws
\begin{gather*}
    A \cup B = B \cup A \\
    A \cap B = B \cap A
\end{gather*}

Associative laws
\begin{gather*}
    A \cup (B \cup C) = (A \cup B) \cup C \\
    A \cap (B \cap C) = (A \cap B) \cap C
\end{gather*}

Distributive laws
\begin{gather*}
    A \cap (B \cup C) = (A \cap B) \cup (A \cap C) \\
    A \cup (B \cap C) = (A \cup B) \cap (A \cup C)
\end{gather*}

Identity laws
\begin{gather*}
    A \cup \emptyset = A \\
    A \cap U = A
\end{gather*}

Complement laws
\begin{gather*}
    A \cup \overline{A} = U \\
    A \cap \overline{A} = \emptyset
\end{gather*}

Idempotent laws
\begin{gather*}
    A \cup A = A \\
    A \cap A = A
\end{gather*}

Null laws
\begin{gather*}
    A \cup U = U \\
    A \cap \emptyset = \emptyset
\end{gather*}

Absorption laws
\begin{gather*}
    A \cup (A \cap B) = A \\
    A \cap (A \cup B) = A
\end{gather*}

DeMorgan's laws
\begin{gather*}
    \overline{A \cup B} = \overline{A} \cap \overline{B} \\
    \overline{A \cap B} = \overline{A} \cup \overline{B}
\end{gather*}

Involution law
\begin{gather*}
    \overline{\overline{A}} = A
\end{gather*}

\section{Relations}
Relation: Any subset of $A \times B$

Divides: Let $a,b \in \mathbb{Z}, a \neq 0$. $a | b$ ($a$ divides $b$) if and only if there exists an integer $k$ such that $ak = b$.

Relation notation: If $s$ is a relation from set $A$ into set $B$, the fact that $(x, y) \in s$ can be written $xsy$.

Composition of relations: Let $r$ be a relation from set $A$ into set $B$, and let $s$ be a relation from set $B$ into set $C$. The composition of $r$ with $s$, written $rs$, is the set of pairs $(a,c) \in A \times C$, where $(a, c) \in rs$ if and only if there exists $b \in B$ such that $(a,b) \in r$ and $(b,c) \in s$.

Graphs can be used to visualize relations, by having the arrows pointing from vertex $a$ to vertex $b$ if $arb$.

\section{Properties of relations}

Reflexive relation: $ara$ for all $a \in A$

Antisymmetric relation: if $arb$ and $a \neq b$, then $bra$ is true

Symmetric relation: if $arb$, then $bra$ is true

Transitive relation: if $arb$ and $brc$, then $arc$

A partial ordering on $A$ is a relation on set $A$ that is reflexive, antisymmetric and transitive.

A equivalence relation is a relation that is reflexive, symmetric and transitive.

Hasse diagram: A graph can be used to represent a partial ordered set. The reflexive property is implied in every element and loops are not drawn. The antisymmetry property is described by putting the first element go below the second. By the transitive property, edges connecting from one element to a second element and another connecting the second to a third means a third connection can be made from the first to the third.

Congruence modulo
\begin{gather*}
    a = b \pmod{n} \Leftrightarrow n | (a - b)
\end{gather*}

Equivalence class (Set of all elements that are equal to a given element under a relation)
\begin{gather*}
    a \in A,\ r\text{ is an equivalence relation}\\
    c(a) = \{b \in A | arb\}
\end{gather*}

\section{Matrices of relations}

Adjacency matrix: Let $A = \{a_1, a_2,..., a_m\}$ and $B = \{b_1, b_2,..., b_n\}$. Let $r$ be a relation from $A$ into $B$. Then $r$ can be represented by the $m \times n$ matrix $R$ defined by
\begin{gather*}
    R_ij =
    \begin{cases}
        1 & \text{if}\ a_i r b_j \\
        0 & \text{otherwise}
    \end{cases}
\end{gather*}

Composition as matrix multiplication: If $R_1$ and $R_2$ are the adjacency matrices of $r_1$ and $r_2$ respectively, then the product $R_1 R_2$ using Boolean arithmetic represents the composition $r_1 r_2$.

\section{Transitive closure}

Transitive closure: Let $A$ be a set and $r$ be a relation on $A$. The transitive closure of $r$, $r^+$, is the smallest transitive relation that contains $r$ as a subset.

If $r$ is a relation on a set $A$ and $|A| = n$, then the transitive closure of $r$ is the union of the first $n$ powers of $r$.
\begin{gather*}
    r^+ = r \cup r^2 \cup r^3 \cup \cdots \cup r^n
\end{gather*}

Matrix math can be used to find $R^+$.
\begin{gather*}
    R^+ = R + R^2 + \cdots + R^n
\end{gather*}

\section{Functions}

A function from set $A$ into set $B$ is a relation from $A$ into $B$ such that each element of $A$ is related to exactly one element of $B$.

\begin{gather*}
    f: A \rightarrow B
\end{gather*}

Set $A$ is the domain and set $B$ is the codomain.

If $f(a) = b$, the image of $a$ is b.

Range: The union of all images of a function's domain. It is a subset of the codomain.

A function can have the domain of a Cartesian product, in which case it is denoted $C: A \times B \rightarrow C$ defined by $C(a, b)$

\section{Properties of functions}

Injective function (one-to-one function): Distinct elements in domain map to distinct elements in codomain
\begin{gather*}
    \forall a, b \in A, \\
    f(a) = f(b) \Rightarrow a = b\ and \\
    a \neq b \Rightarrow f(a) \neq f(b)
\end{gather*}

Surjective function (onto function): Its range is equal to its codomain

Bijective function (one-to-one, onto function): Is both injective and surjective.

Countable set: A set that has the same cardinality as a subset of natural numbers.

Pigeonhole principle: Let $f$ be a function from a finite set $X$ into a finite set $Y$. If $n \geq 1$ and $|X| > n|Y|$, then there exists an element of $Y$ that is the image under $f$ of at least $n + 1$ elements of $X$.

\section{Function composition}

Equality of functions
\begin{gather*}
    function\ f = function\ g \Leftrightarrow (\forall x)_A(f(x) = g(x))
\end{gather*}

Two functions with different domains cannot be equal, even if the functions have the same equation. Also, two functions can be equal even if they have different equations.

Composition of functions
\begin{gather*}
    g(f(x)) = (g \circ f)(x)
\end{gather*}

Associativity of functions
\begin{gather*}
    h \circ (g \circ f) = (h \circ g) \circ f
\end{gather*}

Composition of injections and surjections
\begin{itemize}
    \item If $f: A \rightarrow B$ and $g: B \rightarrow C$ are injections, then $g \circ f: A \rightarrow C$ is an injection.
    \item If $f: A \rightarrow B$ and $g: B \rightarrow C$ are surjections, then $g \circ f: A \rightarrow C$ is a surjection.
\end{itemize}

The identity function on $A$ is a function from $A$ onto $A$, such that \\
$(\forall a)_A(i(a) = a)$.

Inverse function: Let $f: A \rightarrow B$ and $g: B \rightarrow A$. If $g \circ f = i_A$ and $f \circ g = i_B$, then $g = f^{-1}$.

$f^{-1}$ exists if and only if $f$ is a bijection.

\section{Recursion}

Telescoping form: The expanded recursive form for $f(n)$

Iteration: Starting with the basis, $f(0)$, and working your way up to $f(n)$.

Recursive definition of the binomial coefficient
\begin{gather*}
    \binom{n}{0} = 1 \\
    \binom{n}{n} = 1 \\
    \binom{n}{k} = \binom{n-1}{k} + \binom{n-1}{k-1}, n > k > 0
\end{gather*}

Recursive polynomial expression
\begin{gather*}
    p(0) \in \mathbb{Z} \\
    p(n) = p(n-1)x + a, a \in \mathbb{Z}
\end{gather*}

Fibonacci sequence
\begin{gather*}
    F_0 = 1, F_1 = 1 \\
    F_k = F_{k-2} + F_{k-1}, k \geq 2
\end{gather*}

Closed form expression: An expression that does not have a runaway number of operations. For example, the number of operations in $\sum_{k=1}^{n} k$ grows indefinitely with $n$, but $(n(n+1)) / 2$ has 3 operations no matter what $n$ is.

Sequence/discrete function: maps natural numbers into a certain set

\section{Solving recurrence relations}

Homogeneous recurrence relation: $S(k) + C_1 S(k-1) + \cdots + C_n S(k-n) = 0$

Characteristic equation: $a^n + C_1 a^{n-1} + \cdots + C_{n-1} a + C_n = 0$

Solving homogeneous finite order linear relations
\begin{enumerate}
    \item Use the characteristic equation to solve for $a$, the roots.
    \item Write the general solution of the recurrence relation and replace $a_n$. The general solution is $S(k) = b_1 a_1^k + b_2 a_2^k + \cdots + b_n a_n^k$. If $a_j$ a double root, then $b_j a_j^k$ is replaced with $(b_{j0}+b_{j1} k)a_j^k$.
    \item Find $b_n$ using the given initial conditions $S(n)$ and substitute those into the general solution.
\end{enumerate}

Solving nonhomogeneous finite order linear relations
\begin{enumerate}
    \item Write the associated homogeneous relation by changing the right-hand side to $0$ and solve for $a$. The solution for the associated homogeneous relation is $S^{(h)}(k)$.
    \item Use the table to find the form of $S^{(p)}(k)$, given the form of the right-hand side. \\
    \begin{tabular}{c|c}
            Right-hand side, $f(x)$ & $S^{(p)}(k)$ \\
            $q$ & $d$ \\
            $q_0 + q_q k$ & $d_0 + d_1 k$ \\
            $qa^k$ & $da^k$
        \end{tabular}
    \item Substitute $S^{(p)}(k)$ into the recurrence relation to find the unknown coefficients.
    \item Add the homogeneous relation and the unknown coefficients, \\
    $S^{(h)}(k) + S^{(k)}$ and use the initial conditions to solve for $b$.
\end{enumerate}

\section{Generating function}

Generating function of a sequence $S$ with terms $S_n$
\begin{gather*}
    G(S;z) = \sum_{n=0}^{\infty} S_n z^n = S_0+S_1 z+S_2 z^2+S_3 z^3+\cdots
\end{gather*}

Solving a recurrence relation using generating functions
To solve $S(n) - 2S(n-1) - 3S(n-2) = 0, n \geq 2$, with $S(0) = 3$ and $S(1) = 1$
\begin{enumerate}
    \item Translate the recurrence relation into an equation about generating functions. \\
        Let $V(n) = S(n) - 2S (n - 1) - 3S (n - 2), n \geq 2$, with $V(0) = 0$ and $V(1) = 0$.
        \begin{equation*}
        G(V;z) = 0 + 0z +\sum_{n=2}^{\infty}  (S(n) - 2S (n - 1) - 3S (n - 2)) z^n= 0
        \end{equation*}
    \item Solve for the generating function of the unknown sequence, \\
    $G(S;z) = \sum_{n=0}^{\infty} S_n z^n$.
    \begin{equation*}
        0 =\sum_{n=2}^{\infty} {S(n) z^n-2} \left(\sum_{n=2}^{\infty} S(n-1) z^n\right)-3\left(\sum_{n=2}^{\infty} S(n-2) z^n\right)
    \end{equation*}
    The three sums can be written as
    \begin{equation*}
        \begin{split}
            \sum_{n=2}^{\infty} S_n z^n &=\sum_{n=0}^{\infty} S_n z^n - S(0)-S(1)z\\
            &= G(S;z)-3-z
        \end{split}
    \end{equation*}
    \begin{equation*}
        \begin{split}
            \sum_{n=2}^{\infty} S(n-1) z^n &=z\left(\sum_{n=2}^{\infty} S(n-1) z^{n-1}\right)\\
            & =z\left(\sum_{n=1}^{\infty} S(n) z^n\right)\\
            & = z\left(\sum_{n=0}^{\infty} S(n) z^n-S(0)\right)\\
            &= z(G(S;z)-3)
        \end{split}
    \end{equation*}
    \begin{equation*}
        \begin{split}
            \sum_{n=2}^{\infty} S(n-2) z^n  & = z^2\left(\sum_{n=2}^{\infty} S(n-2) z^{n-2}\right)\\
            & =z^2G(S;z)
        \end{split}
    \end{equation*}
    Therefore
    \begin{equation*}
        \begin{split}
            &(G(S;z)-3-z)-2z(G(S;z)-3)-3z^2G(S;z)=0\\
            &G(S;z)=\frac{3-5z}{1-2z-3z^2}
        \end{split}
    \end{equation*}
    \item Determine the sequence whose generating function is the one from Step 2. Note that $S(n) = ba^n, G(S;z) = \frac{b}{1-az}$ \\
    Apply partial fractions decomposition to get
    \begin{equation*}
        G(S;z)= \frac{1}{1-3z}+ \frac{2}{1+z}
    \end{equation*}
    \begin{equation*}
        S(n)=3^n + 2(-1)^n
    \end{equation*}
\end{enumerate}

\section{Strategies for proofs}

\begin{itemize}
    \item Draw or write a few examples and notice patterns
    \item Find the extreme cases, such as an empty set and a set with the maximum cardinality
    \item Consider the contrapositive
    \item If the statement is one of the following, it's probably solved through induction: it has a "$n \in \mathbb{N}$" variable, it has a lot of numbers, and/or you can start with a basis case and build upon that with inductive reasoning
    \item If it's not solved using induction, try to approach a direct proof and then a proof by contradiction and decide which is the simplest one
    \item For complex proofs that branch out into multiple scenarios, break it down by explaining the various cases
    \item Read back the proof to make sure a classmate would understand it and that all variables are defined
\end{itemize}

\section{Graphs}

Graph: $G = (V, E)$, where $V =$ nonempty set of vertices, and $E \subseteq V \times V$

Incident = connecting ("$X$ is incident to vertices $a$ and $b$")

Adjacent vertices = Vertex pair connected by an edge

The edges in an undirected graph have no direction, while the edges in a directed graph have direction.

Multigraph: A graph that is permitted to have two or more edges connecting the same vertices.

Complete undirected graph: Each vertices are connected to one another, denoted $K_n$. $K_n$ has ${n \choose 2}$ edges.

Path length: The number of edges in an edge list.

\begin{center}
\resizebox{\textwidth}{!}{
    \begin{tabular}{ c | c | c | c | c }
    & Open/Closed & A sequence of edges & Distinct edges & Distinct vertices \\
    \hline
    Walk & Open or closed & \checkmark & & \\
    Trail & Open or closed & \checkmark & & \\
    Circuit & Closed & \checkmark & \checkmark & \\
    Path & Open or closed & \checkmark & \checkmark & \checkmark\\
    Cycle & Closed & \checkmark & \checkmark & \checkmark
    \end{tabular}
}
\end{center}

Degree
\begin{itemize}
    \item The degree is the number of edges connected to the vertex.
    \item The outdegree is the number of edges that initiate at a vertex and indegree is the number of edges that terminate at a vertex.
    \item The sum of degrees of a graph is $2|E|$
\end{itemize}

Regular graph: A graph where each vertex has the same number of degrees. A $n$-regular graph is one where each vertex is $n$ degrees.

Subgraph: A subgraph is a graph $G$ formed from a subset of the vertices and edges of $G$. All edges of the subgraph must connect a vertex pair.

Induced subgraphs contain a subset of vertices from the original graph, and all edges connecting pairs of vertices in that subset.

\begin{figure}[htb]
    \caption{A graph and an induced subgraph}
    \centering
    \includegraphics[width=0.6\textwidth]{InducedSubgraph_900.png}
\end{figure}

Spanning subgraphs contain all vertices of the original graph.

\begin{figure}[htb]
    \caption{A graph and a spanning subgraph}
    \centering
    \includegraphics[width=0.6\textwidth]{fig-subgraphs.png}
\end{figure}

Isomorphic graph

\begin{itemize}
    \item A graph $(V', E')$ is isomorphic to $(V, E)$ if there exists a bijection $f: V \rightarrow V'$ such that $(v_i, v_j) \in E$ iff $(f(v_i), f(v_j)) \in E'$
    \item Basically, two isomorphic graphs have the same structure but may have different labels for the vertices
    \item Check if two graphs are isomorphic by mapping corresponding vertices of $V$ to those of $E$
\end{itemize}

Tournament graph: A directed graph that has zero loops and there is only one edge between any two vertices.

Complete/round-robin tournament graph: A tournament graph where all pairs of distinct vertices are connected by one edge.

Single-elimination tournament graph: A tournament graph where: one vertex called the champion has no edge terminating at it, every other vertex is the terminal vertex of exactly one edge, and there is a path from the champion vertex to every other vertex.

\begin{figure}[htb]
    \caption{A single-elimination tournament graph}
    \centering
    \includegraphics[width=0.4\textwidth]{fig-mlb-1983-9-1.png}
\end{figure}

Bipartite graph: A graph is bipartite if its vertices can be divided into two disjoint and independent sets. Visually, there exists a pair of sets of where a line between the pair cuts all edges.

\begin{figure}[htb]
    \caption{A bipartite graph and its two vertex sets}
    \centering
    \includesvg[width=0.4\textwidth]{Simple-bipartite-graph.svg}
\end{figure}

Degree sequence: A non-increasing sequence of its vertex degrees.

\begin{figure}[htb!]
    \caption{The degree sequence of this graph is $(4, 3, 2, 2, 1)$}
    \centering
    \includegraphics[width=0.5\textwidth]{fig-degrees-example-9-1.png}
\end{figure}

Graphic sequence: A degree sequence is graphic if there exists an undirected graph that has the same degree sequence. As an example, $(3, 3, 1)$ is not a graphic sequence.

The complement of a graph $G = (V, E)$ is $\bar{G} = (V, K - E)$.

Eulerian path
\begin{itemize}
    \item A trail that visits every edge in a graph exactly once
    \item A graph has an Eulerian path iff it has exactly 0 or 2 vertices with odd degrees
    \item Eulerian circuit: A closed Eulerian path
    \item A graph has an Eulerian circuit iff the degrees of all vertices are even
\end{itemize}

Hamilton path
\begin{itemize}
    \item A path that visits each vertex exactly once
    \item Hamilton cycle/Hamiltonian graph: A Hamilton path that is a cycle
\end{itemize}

Distance
\begin{itemize}
    \item Distance: Number of edges in a shortest path connecting a pair of vertices
    \item Eccentricity: Greatest distance between a given vertex and any other vertex
    \item Center: The vertex with the minimum eccentricity
    \item Radius: Minimum eccentricity of any vertex
    \item Diameter: Maximum eccentricity of any vertex
\end{itemize}

\section{Trees}

Tree
\begin{itemize}
    \item An undirected graph that is connected and has no cycles
    \item A disconnected graph that has no cycles is a forest
\end{itemize}

Dijkstra's algorithm: An algorithm for finding the shortest path(s) between all or a pair of vertices in a graph.

Image credits: Al Doerr, Ken Levasseur - Applied Discrete Structures

\end{document}


\documentclass[a4paper,10pt]{amsart}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{eucal}
\usepackage{amscd}
\usepackage{url}
\usepackage[named]{algo}

\newcommand{\Z}{\mathbb{Z}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\I}{\mathbb{I}}
\newcommand{\C}{\mathbb{C}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\Pee}{\mathbb{P}}
\newcommand{\EuO}{\mathcal{O}}
\newcommand{\Qbar}{\overline{\mathbb{Q}}}

\newcommand{\ljk}[2]{\left(\frac{#1}{#2}\right)}
\newcommand{\modulo}[1]{\;\left(\mbox{mod}\;#1\right)}
\newcommand{\fr}{\mathfrak}

\def\notdivides{\mathrel{\kern-3pt\not\!\kern4.5pt\bigm|}}
\def\nmid{\notdivides}
\def\nsubseteq{\mathrel{\kern-3pt\not\!\kern2.5pt\subseteq}}

\newtheorem{theorem}{Theorem}[section]
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{definition}[theorem]{Definition}

\newenvironment{example}[1][Example]{\begin{trivlist}
\item[\hskip \labelsep {\bfseries #1}]}{\end{trivlist}}

\parindent=0pt
\parskip 4pt plus 2pt minus 2pt

\email{W.B.Hart@warwick.ac.uk}

\title{Computation of quadratic forms}
\author{William Hart}

\begin{document}
\maketitle

\section{Introduction}

\begin{definition}
Given an integer $n \geq 1$ an \emph{$n$-ary quadratic form} is an expression
\begin{equation}\label{qf}
Q(x_1, x_2, \ldots, x_n) = \sum_{i,j=1}^{\infty} a_{ij}x_ix_j,
\end{equation}
for coefficients $a_{ij}$ in some commutative ring $R$ and variables $x_1, x_2, \ldots, x_n$.
\end{definition}

We say that a quadratic form is \emph{integral} if the coefficients $a_{ij}$ are rational integers.

If we write $A = (a_{ij})$ for the matrix of coefficients and $X = (x_1, x_2, \ldots, x_n)^T$ for the column vector of variables, then we can write (\ref{qf}) as
\begin{equation}
Q = X^TAX.
\end{equation}

A quadratic form in two variables is called a \emph{binary quadratic form}. If we write $x, y$ for the variables, it has the form
\begin{equation}\label{abc}
Q(x, y) = ax^2 + bxy + cy^2.
\end{equation}

We will often use the notation $Q := (a, b, c)$ to denote the binary quadratic form (\ref{abc}).

\begin{definition}
The \emph{discriminant} of a binary quadratic form $(a, b, c)$ is the expression $d = b^2 - 4ac$.
\end{definition}

We observe that
\begin{equation}
4aQ(x, y) = 4a^2x^2 + 4abxy + 4acy^2 = (2ax + by)^2 - dy^2,
\end{equation}
where $d$ is the discriminant of $Q := (a, b, c)$.

From this we see that $(a, b, c)$ is always nonnegative if $d < 0$ and $a > 0$. In this case we call the quadratic form a \emph{positive definite} binary quadratic form. If $a < 0$ and $d < 0$ we say that it is a \emph{negative definite} binary quadratic form. If $d > 0$ the form is called an \emph{indefinite} binary quadratic form.

\section{Positive definite binary quadratic forms}

In this section we restrict our attention to integral binary quadratic forms. We also restrict to forms which are \emph{primitive}, i.e. forms $Q := (a, b, c)$ such that $a, b, c$ do not share a common factor.

We now introduce the concept of equivalent forms.

\begin{definition}
We say that binary quadratic forms $P(x, y)$ and $Q(x, y)$ are \emph{equivalent} if there is an $M \in$ GL$_2(\mathbb{Z})$ such that
\begin{equation}\label{equiv}
P(x, y) = Q(M(x, y)^T)
\end{equation}
\end{definition}

Matrices in GL$_2(\mathbb{Z})$ have determinant $\pm 1$. We say that forms are \emph{properly equivalent} if the matrix $M$ in (\ref{equiv}) has determinant $1$, otherwise they are \emph{improperly equivalent}.

We see immediately that equivalent forms $P(x, y)$ and $Q(x, y)$ represent precisely the same set of integers as $x, y$ vary over $\mathbb{Z}$ because the matrix $M$, and hence the equivalence of forms, is invertible.

It will turn out that there are only finitely many GL$_2(\mathbb{Z})$ orbits of positive definite binary quadratic forms.

\begin{lemma}
If two binary quadratic forms $P(x, y) := (a, b, c)$ and $Q(x, y) := (a', b', c')$ are properly equivalent they have the same discriminant.
\end{lemma}

\textbf{Proof:}
If two forms are properly equivalent then $Q(x, y) = P(M(x, y)^T)$ for some $M \in$ SL$_2(\mathbb{Z})$. But this group is generated by $T = \left(\begin{array}{cc}1 & 1 \\ 0 & 1 \end{array}\right)$ and $S = \left(\begin{array}{cc}0 & -1 \\ 1 & 0 \end{array}\right)$.

The transformation $T$ sends $(a, b, c)$ to $(a, b + 2a, a + b + c)$ and its inverse sends $(a, b, c)$ to $(a, b - 2a, a - b + c)$, whereas the transformation $S$ sends $(a, b, c)$ to $(c, -b, a)$ (note that $S^4 = 1$ so that $S^{-1} = S^3$). In both cases we see that the discriminant is preserved. $\qed$

Legendre introduced a concept of reduced binary quadratic form which we now explore. This allows us to find a ``smallest'' such form in each GL$_2(\mathbb{Z})$ orbit by a process known as reduction.

\begin{definition}A positive definite binary quadratic form is said to be \emph{reduced} if
\begin{equation}\label{reduced}
|b| \leq a \leq c, \;\mbox{and}\; b \geq 0 \;\mbox{if either}\; |b| = a \;\mbox{or}\; a = c.
\end{equation}
\end{definition}

We now proceed to show that there is a unique reduced form in each GL$_2(\mathbb{Z})$ orbit. We require some intermediate results.

\begin{lemma}
Let $Q(x, y) := (a, b, c)$ be a reduced positive definite binary quadratic form with discriminant $d \neq -3$. The minimum value taken by $Q(x, y)$ as $x, y$ not both zero range over the integers is $a$. If $a = c$ this value is taken at $(x, y) = (\pm 1, 0)$ and $(0, \pm 1)$, otherwise it is only taken at $(\pm 1, 0)$.
\end{lemma}

\textbf{Proof:} As $Q$ is reduced we have that $|b| \leq a \leq c$. Thus
\begin{equation}
Q(x, y) \geq (a - |b| + c)\,\mbox{min}(x^2, y^2).
\end{equation}

If neither $x$ or $y$ are zero then $Q(x, y) \geq a - |b| + c \geq a$. Similarly if $x = 0$ or $y = 0$ then $Q(x, y) \geq a$.

If $a = c$ then $|b| \neq a$ otherwise $Q$ is not primitive (if $a = b = c = 1$ then $d = -3$).

Thus if $xy \neq 0$ then $Q(x, y) > a$. So the only time $Q(x, y) = a$ is if $x = 0$ or $y = 0$. In the first case we have $(x, y) = (0, \pm 1)$ and in the second case $(x, y) = (\pm 1, 0)$.

The other possibility is that $a < c$. In this case, if $xy \neq 0$ then $Q(x, y) \geq (a - |b|) + c > a$. Clearly the only remaining possibility is $(x, y) = (\pm 1, 0)$. $\qed$

In the case where $d = -3$ it is clear that the form $Q(x, y) = x^2 + xy + y^2$ is an exception. In this case we see that the minimum value is $1$, taken at $(x, y) = (\pm 1, 0)$, $(0, \pm 1)$, $(1, -1)$ and $(-1, 1)$.

\begin{theorem}
If $P(x, y) := (a, b, c)$, $Q(x, y) = (a', b', c')$ are properly equivalent reduced positive definite binary quadratic forms then $P = Q$.
\end{theorem}

\textbf{Proof:}
We assume for the moment that $d \neq -3$.

As $P$ and $Q$ are properly equivalent there exist integers $\alpha, \beta, \gamma, \delta$ with $\alpha\delta - \beta\gamma = 1$ such that $Q(x, y) = P(\alpha x + \beta y, \gamma x + \delta y)$.

By the previous theorem, $P$ and $Q$ have the same minimum value, thus $a' = a$.

If $a = c$ then $P$ achieves its minimum value at $(\pm 1, 0)$ and $(0, \pm 1)$. Therefore $a = Q(1, 0) = P(\alpha, \gamma)$. Thus $(\alpha, \gamma) = (\pm 1, 0)$, or $(0, \pm 1)$.

In the first case, $(\beta, \delta) = (r, \pm 1)$ for some integer $r$. Thus $Q(x, y) = P(\pm x + ry, \pm y)$. Thus $b' = b \pm 2ar$. By the fact that $Q$ is reduced, we see that $r = 0$, $b' = b$ and $c = c'$. Thus the forms are equal.

In the second case $(\beta, \delta) = (\mp 1, r)$. Thus $Q(x, y) = P(\mp y, \pm x + ry)$. Thus $b' = -b \pm 2cr$. Again we see that $r = 0$ and $b' = -b$. Here we must have $a = c = c'$ and so $b, b' \geq 0$. This means that $b' = 0$ and the forms are equal.

When $a < c$ we have that $P$ achieves its minimum at $(\pm 1, 0)$. The argument here is the same as for the first case above.

Finally, when $d = -3$ we have $-3 = b^2 - 4ac \leq a^2 - 4a^2 = -3a^2$. Thus $a = 1$. But this implies that $b = \pm 1$ and thus $c = 1$. But the form is reduced, thus $b = 1$. So we see there is only one reduced form of discriminant $d = -3$ and the result follows. $\qed$

\begin{theorem}
Every positive definite binary quadratic form is properly equivalent to a unique reduced binary quadratic form.
\end{theorem}

\textbf{Proof:} We have already shown uniqueness, thus it suffices to show that every form $P(x, y) := (a, b, c)$ is properly equivalent to a reduced one.

Let $Q := (a', b', c')$ be a form properly equivalent to $P$ with minimum positive $a'$. As the discriminants of $P$ and $Q$ are the same we must have $c' > 0$.

The transformation $U = \left(\begin{array}{cc}1 & u \\ 0 & 1 \end{array}\right)$ sends $Q$ to $Q' = (a'', b'', c'') = (a', b' + 2ua', c'')$. We can therefore choose $u$ so that $|b''| \leq a''$.

But the transformation $S$ above sends $(a'', b'', c'')$ to $(c'', -b'', a'')$ and therefore $a'' = a' \leq c''$ by the choice of $a'$.

Thus every class contains a form $(a, b, c)$ with $|b| \leq a \leq c$.

This form is already reduced unless $b < 0$ and either $a = -b$ or $a = c$. In both cases $(a, -b, c)$ is reduced.

In the first case $(a, -a, c)$ is sent to $(a, a, c)$ by the transformation $T$ above and in the second case $(a, b, a)$ is sent to $(a, -b, a)$ by the transformation $S$ above.

Therefore in both cases $(a, b, c)$ is properly equivalent to $(a, -b, c)$ and the theorem is proved. $\qed$

\begin{theorem}\label{ineq}
If $P(x, y) := (a, b, c)$ is a positive definite reduced form of discriminant $d$ then $|b| \leq a \leq \sqrt{|d|/3}$ and $c \leq (1-d)/4$.
\end{theorem}

\textbf{Proof:}
As the form is reduced, we have $-d = 4ac - b^2 \geq 4a^2 - a^2 = 3a^2$, and the first part of the result follows.

For the second part we note that
$$c = \frac{b^2 - d}{4a} \leq \frac{a^2 - d}{4a}.$$

For constant $d$ this expression has negative derivative with respect to $a$ in the interval $[1, \sqrt{|d|}]$ and is therefore decreasing. It therefore achieves its maximum value at $a = 1$ which gives the required result.$\qed$

The average value of $h(d)$, the number of reduced forms of discriminant $d$, is known to be asymptotic to $C\pi^{-1}\sqrt{|d|}$ where $C \approx 0.8815$ and in fact $h(d)$ is unconditionally bounded above by $O(\sqrt{|d|}\log{|d|})$ (see \cite{cohen}).

Our first algorithm for computing all the primitive reduced binary quadratic forms for a disciminant $d < 0$ is essentially that of \cite{buchmann}.

\begin{algorithm}{ReducedForms1}{
   \qcomment{Compute primitive reduced binary quadratic forms for $d < 0$}}
A $\qlet \sqrt{-d/3}$ \\
$h \qlet 0$ \\
$L \qlet \emptyset$ \\
\qfor $a \qlet 1$ \qto A \\
\qfor $b \in \Z$ such that $-a < b \leq a$ and $b^2 \equiv d \pmod{4a}$ \\
$c \qlet (b^2 - d)/4a$ \\
\qif $(a, b, c)$ is primitive and reduced \\
\qthen $L \qlet L \cup \{(a, b, c)\}$ \\
$h$ \qlet $h + 1$ \qfi \qrof \qrof \\
\qreturn $L$, $h$
\end{algorithm}

The algorithm makes use of the following observation:
if $P := (a, b, c)$ is a reduced form, then as $d = b^2 - 4ac$
we have $b^2 \equiv d \pmod{4a}$.

On the other hand, if $b$ is a square root of $d$ modulo $4a$ in the range $-a < b \leq a$ then $b^2 = d + 4ac$ for some integer $c$, i.e. $d = b^2 - 4ac$.

\textbf{Performance improvements}

\begin{itemize}
\item Most of the run time is in finding square roots modulo $4a$. This can be optimised by using the Tonelli-Shanks algorithm to compute square roots modulo each prime $p$ dividing $4a$ and Hensel lifting the roots to square roots modulo each $p^k \;|\; 4a$. These roots can then be combined using the Chinese Remainder Theorem.

\item In order to compute the square roots modulo $4a$, the factorisation of $4a$ must be known. It is more efficient to compute the factorisations of all the values $4a$ in advance by sieving. This can be done by sieving by all primes $p$ up to $\sqrt{A}$ and by every power $p^k \leq A$ of such such a prime.

\item One can precompute square roots of $d$ modulo all powers of primes $p^k \leq A$ for odd primes $p$ and modulo all $2^k \leq 4A$. Having been precomputed, these roots can be combined using chinese remaindering to obtain square roots modulo $4a$ for each $a$ in turn.

\item In order to compute $c$ we must first compute $b^2 - d$. This can have the value $4|d|/3$ which may cause an overflow, depending on how integers are stored. However, if $d$ is stored as a C long and $b^2 - d$ and $4a$ as C unsigned longs, then no overflow occurs when computing $c = (b^2 - d)/4a$.
\end{itemize}

\textbf{Analysis}

\begin{itemize}
\item Sieving requires computing primes up to $(-d/3)^{1/4}$. There can be at most one prime which exceeds this bound which divides any value $4a$, and it does so with multiplicity at most 1. Thus after dividing through by the highest power of each prime dividing $4a$, we are left with either 1 or a prime $p$.

\item Clearly finding all the necessary primes can be completed in time $\tilde{O}(|d|^{1/4})$.

\item Sieving the interval $[1, A]$ for prime factors can be done in time $\tilde{O}(|d|^{1/2})$.

\item Computing square roots modulo $p$ can be done in polynomial time (assuming we can find a quadratic non-residue modulo $p$). Similarly lifting these to roots modulo prime powers can be done in polynomial time, for $p \;\notdivides\; d$.

\item If $p \;|\; d$ and $p \;|\; 4a$, then the number of square roots of $d$ modulo $4a$ is bounded by $4a/p$. Thus, computation of roots modulo values $4a$ divisible by primes dividing $d$ can also be done in time $\tilde{O}(|d|^{1/2})$.

\item Finally, searching for primitive, reduced forms from the roots that are computed takes time $\tilde{O}(|d|^{1/2})$.
\end{itemize}

We see that the following theorem holds:

\begin{theorem}Assuming the Generalised Riemann Hypothesis (required to guarantee efficiently finding a quadratic nonresidue in Tonelli-Shanks), we can find all primitive, reduced binary quadratic forms of discriminant $d < 0$ in time $\tilde{O}(|d|^{1/2})$. We must compute $\tilde{O}(|d|^{1/4})$ primes.
\end{theorem}

The algorithm above is quite complicated to implement. In FLINT we only implement the first two of the three performance improvements, and instead of sieving with all powers of the primes $p$, we simply sieve with the primes $p$ and then determine the highest power of $p$ dividing each value $4a$ that it divides.

There is an alternative algorithm which is slightly easier to implement and is slightly faster in practice, but it requires the computation of a greater number of primes. This makes it less practical for larger discriminants.

The formulation is suggested by Algorithm 5.3.5 of \cite{cohen}.

\begin{algorithm}{ReducedForms2}{
   \qcomment{Compute primitive reduced binary quadratic forms for $d < 0$}}
B $\qlet \sqrt{-d/3}$ \\
$h \qlet 0$ \\
$L \qlet \emptyset$ \\
\qfor $b \qlet 1$ \qto B \\
\qfor $a, c \in \N$ such that $ac = (b^2 - d)/4$ \\
\qif $(a, b, c)$ is primitive and reduced \\
\qthen $L \qlet L \cup \{(a, b, c)\}$ \\
$h$ \qlet $h + 1$ \qfi \\
\qif $(a, -b, c)$ is primitive and reduced \\
\qthen $L \qlet L \cup \{(a, -b, c)\}$ \\
$h$ \qlet $h + 1$ \qfi \qrof \qrof \\
\qreturn $L$, $h$
\end{algorithm}

This algorithm relies on being able to factor the expressions $(b^2 - d)/4$.

\textbf{Performance improvements}

\begin{itemize}
\item Most of the run time is in factorisation of values $(b^2 - d)/4$. This can be optimised by computing all the prime power factors in advance and then iterating through all possible exponents to find factors $a$. We then compute $c = (b^2 - d)/(4a)$ for each such factor $a$.

\item We can use (quadratic) sieving to find all the prime factors of the values $(b^2 - d)/4$ in advance. Suppose that an odd prime $p$ divides $(b^2 - d)/4$. Then $b^2 \equiv d \pmod{p}$. We can use the Tonelli-Shanks algorithm to find square roots $r_0, r_1 \pmod{p}$. We can then mark off all values in the range $[1, B]$ congruent to $r_0$ or $r_1 \pmod{p}$.

\item Once we know for which values of $b$ the expression $(b^2 - d)/4$ is divisible by a given prime $p$, we can simply compute the maximum power of $p$ dividing each such value $(b^2 - d)/4$. This turns out to be faster in practice than sieving with higher powers of $p$.

\item We are factoring values $(b^2 - d)/4$. In order to do so, we must first compute $b^2 - d$. Thus the same overflow considerations occur as for the first algorithm.
\end{itemize}

\textbf{Analysis}

\begin{itemize}
\item The largest value $(b^2 - d)/4$ can take is $-d/3$. Thus sieving requires computing primes up to $(-d/3)^{1/2}$.

\item Clearly finding all the necessary primes can be completed in time $\tilde{O}(|d|^{1/2})$.

\item Quadratically sieving the interval $[1, B]$ for prime factors can be done in time $\tilde{O}(|d|^{1/2})$ once the square roots are known.

\item Computing square roots modulo $p$ can be done in polynomial time (assuming we can find a quadratic non-residue modulo $p$).

\item Finally, searching for primitive, reduced forms from the triples $(a, b, c)$ that are computed takes time $\tilde{O}(|d|^{1/2})$.
\end{itemize}

We clearly get the same time complexity as for the previous algorithm. However, we now need $\tilde{O}(|d|^{1/2})$ primes. The advantage is that the algorithm is slightly easier to implement (in FLINT we implement all the listed performance improvements).

The version of the second algorithm in FLINT is about 50\% faster than the first algorithm. Of course, this may be due to not implementing all the performance improvements listed for the first case.

As we shall see later, computation of the class number $h$, only, can be completed much faster than computation of the list of reduced forms $L$.

\begin{thebibliography}{99}

\bibitem{buchmann} J. Buchmann, U. Vollmer \emph{Binary Quadratic Forms: An Algorithmic Approach}, Springer, 2007.

\bibitem{cohen} H. Cohen, \emph{A Course in Computational Algebraic Number Theory}, Springer, 1996.

\end{thebibliography}
\end{document}


\section{Statistics}\label{section:statistics}

Robust predictive modeling and statistical inference are crucial for successful analyses in particle physics.
This section will be a very brief tour of these concepts and some of their applications, which will be necessary to understand the analysis that was performed.

\subsection{Likelihood Ratio}\label{subsection:likelihood_ratio}

\subsubsection{Wilk's Theorem}\label{subsection:wilks_theorem}

In brief, Wilk's theorem~\cite{Wilks:1938dza} states that for two functions in a nested family of functions, $f\left(x\,\middle|\,\vec{\theta}_{a}\right)$ and $f\left(x\,\middle|\,\vec{\theta}_{b}\right)$, with the number of parameters $a < b$, in the limit of large sample size the likelihood ratio test statistic,%
\footnote{As likelihoods are not p.d.f.s and so not required to be normalized, it is seen that $L\left(\vec{\theta}\right) = f\left(\vec{\theta}\middle|x\right)$.}
\begin{equation}
 t_{\vec{\theta}} = -2 \log \frac{\displaystyle L\left(\vec{\theta}_{a}\right)}{\displaystyle L\left(\vec{\theta}_{b}\right)},
\end{equation}
is asymptotically distributed as a $\chi^2$ random variable with $d=b-a$ degrees of freedom,
\begin{equation}
 \lim_{\mathrm{samples}\to \infty} t_{\vec{\theta}} \sim \chi^2_{d}.\label{eq:wilks_theorem}
\end{equation}
This asymptotic approximation to the empirical distribution of the test statistic allows for efficient computation of a \pvalue{} (that approximates the \pvalue{} under the empirical distribution).

The one-sided (one-tailed) \pvalue{} --- given the null hypothesis that $t_{\vec{\theta}}$ is $\chi_{d}^{2}$ distributed --- is then the value of the complementary cumulative distribution function (CCDF) of the $\chi^2_{d}$ distribution evaluated at the observed $t_{\vec{\theta}}$,
\begin{equation}
 p\textrm{-value} = \textrm{CCDF}_{\chi^2_{d}}\left(x=t_{\vec{\theta}}\right).
\end{equation}
That is to say, the \pvalue{} is the probability, given $t_{\vec{\theta}} \sim \chi^2_{d}$, to observe a $t_{\vec{\theta}}$ value greater than or equal to that which was observed.

\subsubsection{Profile Likelihood Ratio}

The \gls{MLE} $\hat{\theta}$ is the value of the parameter $\theta$ that maximizes the likelihood function given the observed data.
So for parameter of interest $\mu$ and nuisance parameters $\vec{\theta}$, the maximum likelihood estimators are $\hat{\mu}$ and $\hat{\vec{\theta}}$.
Likewise, the conditional maximum likelihood estimator $\hat{\hat{\theta}}$ is the value of the parameter $\theta$ that maximizes the likelihood for a given value of another parameter $\mu$.
So for a specific value of the parameter of interest $\mu$, the conditional maximum likelihood estimator for nuisance parameters $\vec{\theta}$ is $\hat{\hat{\vec{\theta}}}$.
As $\hat{\hat{\vec{\theta}}}$ is a function of the given $\mu$ then it is seen that the likelihood function's dependence on the nuisance parameters $\vec{\theta}$ can be ``profiled'' vs. $\mu$ and removed.
Given this, the profile likelihood ratio,
\begin{equation}
 \lambda\left(\mu\right) = \frac{L\left(\mu, \hat{\hat{\vec{\theta}}}\right)}{L\left(\hat{\mu}, \hat{\vec{\theta}}\right)}\,,
 \label{eq:profile_likelihood_ratio}
\end{equation}
can be constructed and used as a test statistic to indicate the compatibility of a possible value $\mu$ with the MLE $\hat{\mu}$ --- a function of the observed data.
Given that the negative log likelihood is usually what is actually calculated --- for numerical reasons --- the test statistic that is generally used from the profile likelihood ratio is
\begin{equation}
 q_{\mu} = -2 \ln \lambda\left(\mu\right) = 2 \left(\ln L\left(\hat{\mu}, \hat{\vec{\theta}}\right) - \ln L\left(\mu, \hat{\hat{\vec{\theta}}}\right)\right)\,,
 \label{eq:q_mu}
\end{equation}
as it is seen by Wilk's theorem, \Cref{eq:wilks_theorem}, to be distributed according to a $\chi^{2}$ distribution with one degree of freedom.
This results in a \pvalue{} for $q_{\mu}$ of
\begin{equation}
 p = \int\limits_{q_{\mu}}^{\infty} f_{\chi^{2}}\left(t\middle|1\right)\,dt\,.
 \label{eq:q_mu_pvalue}
\end{equation}

\subsection{Intervals and limits}\label{section:intervals_and_limits}

In addition to point estimates that determine an estimator, $\hat{\theta}$, of a parameter $\theta$, interval estimates give statistical precision to the measured value.
A common example of such an interval estimate is the set of points bounded by the point estimate and the estimated standard deviation: $\left[\hat{\theta} - \sigma_{\hat{\theta}}, \hat{\theta} + \sigma_{\hat{\theta}}\right]$.
The following is a short discussion of the construction, interpretation, and use of these intervals in the frequentist and Bayesian paradigms.

\subsubsection{Frequentist Confidence Intervals}

In the frequentist paradigm, a $1-\alpha$ confidence level (CL) confidence interval (CI) is an interval estimate that covers the true value of the parameter, $\theta$, $1-\alpha$ of the time it is constructed.
So the $95\%$ confidence level confidence interval covers the true value $95\%$ of the time it is constructed.
The method for constructing confidence intervals is called the ``Neyman Construction''~\cite{Neyman:1937uhy}, and results from inverting hypothesis tests.
This confidence interval construction can be described as a random variable that is the set of parameter points, $\left\{\vec{\theta}\right\}$, where the null hypothesis of each parameter point $\theta$ is accepted, $p\left(t > k_{\alpha}\middle| \theta\right) < \alpha$,

\begin{equation}
 \mathrm{CI}_{1-\alpha} = \left\{\vec{\theta}\,\middle| \,p\left(t > k_{\alpha}\middle| \vec{\theta}\right) < \alpha\right\}\,.
 \label{eq:confidence_interval}
\end{equation}

By construction, a hypothesis test of size $\alpha$ should accept the null hypothesis, given that the null is true, $(1-\alpha)$ of the time~\cite{Cranmer:2015nia}.

It is very important to take care in interpreting the meaning of the confidence interval, as it is often misunderstood and misused in analysis.
The confidence interval is constructed from the observed data%
\footnote{The data are a random variable in the frequentist paradigm.}
and so is a random variable and reflects information regarding the constructed estimator --- not the true parameter.
The confidence interval does \emph{not} give the interval in which there is a $1-\alpha$ probability of finding the true parameter value.
This is manifestly Bayesian and in fact is the interpretation of a Bayesian credible interval.
Keeping the definition of frequentist probability tightly in mind, the confidence interval should be interpreted as an interval of parameter values that $1-\alpha$ \emph{of the times is constructed} contains the true parameter value.
Given this, in the frequentist paradigm one is \emph{unable} to make any statement on the probability that the true parameter value is contained in any specific confidence interval beyond the tautology that the true parameter is either contained in it or it is not.
Any misuse of this result is not from a failing of the paradigm, but a misplaced desire of the analyst to have different questions answered than were asked.

In terms of computing a confidence interval, from observations that are governed by $\theta$ a test statistic, $t$, that is an estimator of $\theta$ is constructed.
For each value of the parameter to be tested, there exists an interval $\left[t_{1}, t_{2}\right]$ such that the probability of $t \in \left[t_{1}, t_{2}\right]$ is
\begin{equation}
 p\left(t_{1} < t < t_{2}\middle|\theta\right) = \int\limits_{t_{1}}^{t_{2}} f\left(t\middle|\,\theta\right)\,dt = 1-\alpha\,.
 \label{eq:confidence_interval_coverage}
\end{equation}
This interval represents a constant line segment in the $\left(t, \theta\right)$ parameter space plane at the given value of $\theta$.
By repeating this procedure for every value of $\theta$ to be tested, a band of line segments --- a ``confidence belt'' --- is created that is bound between the curves $\theta\left(t_{1}\right)$ and $\theta\left(t_{2}\right)$, as shown in the example in \Cref{fig:confidence_belt}.
Then, for any given observed value of the test statistic, $t'$, a boundary at $t = t'$ can be drawn in the plane that intersects the confidence belt at the points $\left(t', \theta_{2}\right)$ and $\left(t', \theta_{1}\right)$.
This resulting range of parameter values $\left[\theta_{1}, \theta_{2}\right]$ is the confidence interval~\cite{Cranmer:2015nia,PDG2018:Ch39}.

\begin{figure}[htbp]
 \centering
 \includegraphics[width=0.6\linewidth]{preface/confidence_belt.eps}
 \caption[Neyman construction of a confidence interval using a confidence belt.]{%
  Example sketch of the construction of a confidence belt showing an observation in red intersecting the belt and the corresponding confidence interval as the parameter values bounded between the two blue dashed lines.}\label{fig:confidence_belt}
\end{figure}

The conditions of coverage from \Cref{eq:confidence_interval_coverage} do not uniquely specify $t_{1}$ and $t_{2}$, which allows for analysis specific choices to be made.
If central intervals are chosen, then the probabilities excluded below $t_{1}$ and $t_{2}$ are both $\alpha/2$.
In the event that only an upper (or lower) limit is of interest, as is common in searches for new physics where no excess has been observed, then the probability excluded below $t_{1}$ (or above $t_{2}$) is zero.
Alternatively, if the test statistic used is the profile likelihood ratio test statistic,
\[
 q_{\theta} = -2 \ln\lambda\left(\theta\right) = -2 \ln\frac{L\left(\theta, \hat{\hat{\phi}}\,\right)}{L\left(\hat{\theta},\hat{\phi}\right)}\,,
\]
profiling determines the allowed range $\left[q_{\theta, 1}, q_{\theta, 2}\right]$.
It is seen from \Cref{eq:q_mu} and \Cref{eq:q_mu_pvalue} that for an observed $q_{\theta}$, $q_{\mathrm{obs}}$, to satisfy \Cref{eq:confidence_interval} the resulting confidence belt are the values $q_{\theta} < q_{\mathrm{obs}}$, and the resulting confidence interval the range of $\theta$ that enforce this.
Using such a test statistic results in the Feldman-Cousins confidence intervals~\cite{Feldman:1997qc}.

As the confidence interval can be a difficult concept to describe, a simple illustrative example follows.
Consider $n$ observations $\vec{x} = \left\{x_{1}, \cdots, x_{n}\right\}$ that are drawn from a Normal distribution with unknown mean $\theta$ and width $\sigma_{\theta}$.
This results in a sample mean $\hat{\theta}$ and standard deviation $\sigma_{\hat{\theta}}$.
To construct a $95\%$ confidence level central confidence interval for $\theta$, the test statistic $t = \left(\hat{\theta} - \theta\right)/\sigma_{\hat{\theta}}$ can be used such that ${p\left(t_{1} < t < t_{2}\middle|\theta\right) = 0.95}$, where $t_{1}$ and $t_{2}$ are respectively the $2.5$th percentile and $97.5$th percentile%
\footnote{$t_{1} = \mathrm{CDF}^{-1}\left(\alpha/2\right)$ and $t_{2} = \mathrm{CDF}^{-1}\left(1 - \alpha/2\right)$.}
of the Student's $t$-distribution for $n-1$ degrees of freedom, mean $\mu=\hat{\theta}$ and standard deviation $\sigma=\sigma_{\hat{\theta}}$.
Transforming the Student's $t$-distribution by $t' = \left(t-\hat{\theta}\right)/\sigma_{\hat{\theta}}$ to have $\mu=0, \sigma=1$ simplifies to ${p\left(-d < t' < d\middle|\theta\right) = 0.95}$.
Transforming to parameter space, ${p\left(\hat{\theta} - d\, \sigma_{\hat{\theta}} < \theta < \hat{\theta} + d\, \sigma_{\hat{\theta}}\right)  = 0.95}$, this gives a confidence interval of $\left[\hat{\theta} - d\, \sigma_{\hat{\theta}}, \hat{\theta} + d\, \sigma_{\hat{\theta}}\right]$.
Confidence intervals following this example construction are simulated and shown in \Cref{fig:confidence_intervals}.

\begin{figure}[htbp]
 \centering
 \includegraphics[width=\linewidth]{preface/confidence_intervals.eps}
 \caption[Simulation of 100 $95\%$ confidence level confidence intervals]{%
  An example of 100 point estimates and associated $95\%$ confidence level confidence intervals of parameter value $\theta$.
  Each measurement is the result of the same number of samples from a Normal distribution.
  Confidence intervals that do not include the true value $\theta$ (dashed blue line) are colored red.}
 \label{fig:confidence_intervals}
\end{figure}

\subsubsection{Bayesian Credible Intervals}

In the Bayesian paradigm, a $1-\alpha$ credibility level (CL) credible interval (CI)%
\footnote{CL and CI are used for abbreviations for both the frequentist and Bayesian intervals.
 It will be made clear to the reader from context which paradigm is being considered.}
is an interval estimate where there is a $1-\alpha$ probability of containing the true parameter value --- which is a random variable.
As a result, it is simply the interval of the posterior predictive distribution $\left[\theta_{1}, \theta_{2}\right]$ that when integrated over gives a probability of $1-\alpha$,

\begin{equation}
 p\left(\theta_{1} < \theta < \theta_{2}\middle|\vec{x}\right) = \int\limits_{\theta_{1}}^{\theta_{2}} p\left(\theta\middle|\,\vec{x}\right)\,d\theta = 1-\alpha\,.
 \label{eq:credible_interval_coverage}
\end{equation}

As in the frequentist paradigm, there are different ways to select the credible interval range.
One can choose the shortest interval,%
\footnote{For a unimodal distribution this interval is known as the highest posterior density interval (HPD).}
the interval where probabilities excluded below $\theta_{1}$ and above $\theta_{2}$ are both $\alpha/2$ (this interval includes the median), the interval centered at the mean of the posterior (if the mean exists), or the intervals corresponding to upper (or lower) limits which reduce \Cref{eq:credible_interval_coverage} to the CDF (or CCDF) of $\theta$.

As a final word on interval estimates, it is worth remembering that the frequentist and Bayesian paradigms address different questions and so make different statements with their intervals.
\begin{itemize}
 \item Frequentist: When a confidence interval is constructed on future data, the constructed interval will contain the true parameter value with a probability (frequency) of $1-\alpha$.
 \item Bayesian: Given the observed data, there is a $1-\alpha$ probability that the true parameter value is contained by the constructed credible interval.
\end{itemize}


\section{Dictionary Learning}

Adapt the dictionary to signal characteristics. Objective: $(\mathbf{U}^\star, \mathbf{Z}^\star) \in \argmin_\mathbf{U,Z} \| \mathbf{X} - \mathbf{U} \cdot \mathbf{Z} \|_F^2$ not jointly convex but convex in 1 argument.

\textbf{Matrix Factorization by Iter Greedy Minimization}
\begin{inparaenum}[\color{red} 1.]
  \item Coding step: $\mathbf{Z}^{t+1} \in \argmin_\mathbf{Z} \| \mathbf{X} - \mathbf{U}^t \mathbf{Z} \|_F^2$ subject to $\mathbf{Z}$ being sparse ($\mathbf{z}_n^{t+1}\in \argmin_\mathbf{z}\|\mathbf{z}\|_0$ s.t.$\|\mathbf{x}_n - \mathbf{U}^t\mathbf{z}\|_2 \le \sigma \|\mathbf{x}_n\|_2$)
  \item Dict update step: $\mathbf{U}^{t+1} \in \argmin_\mathbf{U} \| \mathbf{X} - \mathbf{UZ}^{t+1} \|_F^2$, subj to $\forall l\in [L]:\|\mathbf{u}_l\|_2 = 1$. (set $\mathbf{U} = [\mathbf{u}_1^t\cdots \mathbf{u}_l\cdots \mathbf{u}_L^t],~ \min_{u_l}\|\mathbf{X} - \mathbf{U}\mathbf{Z}^{t+1}\|_F^2 = \min_{u_l}\|\mathbf{R}_l^t - \mathbf{u}_l(\mathbf{z}_l^{t+1})^\top\|_F^2$ with $\mathbf{R}_l^t = \tilde{\mathbf{U}}\Sigma\tilde{\mathbf{V}}^\top$ by $\mathbf{u}^*_l=\tilde{\mathbf{u}}_1$)
\end{inparaenum}


\section*{Exercises}

\begin{ex}
  Consider the sequence of numbers defined by the recurrence
  \begin{equation*}
    \begin{array}{l@{~}c@{~}l}
      a_0 &=& 0, \\
      a_1 &=& 1, \\
      a_{n+2} &=& 2a_{n+1} + 3a_n, \quad\mbox{for all $n\geq 0$.} \\
    \end{array}
  \end{equation*}
  Find the first 5 members of this sequence. Then solve the recurrence
  and find $a_{20}$.
\end{ex}

\begin{ex}
  Consider the sequence of numbers defined by the recurrence
  \begin{equation*}
    \begin{array}{l@{~}c@{~}l}
      b_0 &=& 1, \\
      b_1 &=& 2, \\
      b_2 &=& 3, \\
      b_{n+3} &=& 2b_{n+2} + b_{n+1} - 2b_n, \quad\mbox{for all $n\geq 0$.} \\
    \end{array}
  \end{equation*}
  Find the first 5 members of this sequence. Then solve the recurrence
  and find $b_{20}$.
\end{ex}

\begin{ex}
  Consider two sequences of numbers $c_0,c_1,c_2,\ldots$ and
  $d_0,d_1,d_2,\ldots$, defined by the following mutual recurrence
  relation.
  \begin{equation*}
    \begin{array}{l@{~}c@{~}l@{\quad}l@{~}c@{~}l}
      c_0 &=& 0,             & d_0 = 1 \\
      c_{n+1} &=& 4c_n - 3d_n, & d_{n+1} = 2c_n - d_n, \quad\mbox{for all $n\geq 0$.} \\
    \end{array}
  \end{equation*}
  Find the first 5 members of both sequences. Then solve the
  recurrence. Hint: first find a matrix $A$ such that
  \begin{equation*}
    \begin{mymatrix}{c} c_{n+1} \\ d_{n+1} \end{mymatrix}
    = A \begin{mymatrix}{c} c_n \\ d_n \end{mymatrix}.
  \end{equation*}
\end{ex}


\Lecture{Jayalal Sharma}{Sept 19, 2020}{08}{Live lecture discussion}{Anshu and Narasimha Sai }{$\alpha$}{JS}

\paragraph{Bijection from Euler's problem to Binary Trees} As we have already established a bijection from set of balanced parenthesisations to set of full binary trees and established that number of full binary trees with $n$ internal nodes is the catlan number $C_n$, in this section, let's establish a bijection from the \emph{Euler's Problem} to set of full binary trees to establish that the solution to \emph{Euler's problem} is also catlan number $C_n$.

Lets recall \emph{Euler's problem} first. Consider a convex polygon with $n+2$ edges. Euler's problem is the number of ways of triangulating it (partition the polygon into triangles) by drawing non-crossing diagonals. (Refer fig. \ref{fig:Euler's-polygon}). We know that number of non-crossing diagonals in a polygon of $n+2$ edges is $n-1$ (proof follows from a simple induction) and from those $n-1$ non-crossing diagonals, we have our polygon partitioned into $n$ triangles. Let's associate each of the triangles with a vertex (green dots in the fig. \ref{fig:Euler's-polygon}). Observe that if two triangles share an edge, it must be one of the diagonals (no two triangles can share an edge because of non-crossing diagonals). Now, let's connect the vertices whose corresponding triangles share an edge. Any edge connecting two of these vertices crosses a diagonal.
\begin{figure}[h!]
    \centering
    \includegraphics[width=0.4\linewidth]{images/polygon.png}
    \caption{Partitioning a polygon into triangles by non-crossing diagonals. Observe that green dots in each triangle associates the triangle with a vertex}
    \label{fig:Euler's-polygon}
\end{figure}
Now, consider a polygon edge $e$. For every polygon edge surrounding a vertex (other than $e$), add an open-edge originating from that vertex (see fig. \ref{fig:tree-in-polygon}). We arrive at the following claim.
\begin{claim}
	If we remove the underlying triangles (which are formed with polygon edges and diagonals), from fig. \ref{fig:tree-in-polygon}, the 		resulting graph obtained (see fig. \ref{fig:tree}) is a full binary tree with the vertices as internal nodes.
\end{claim}
\begin{figure}[h!]
    \centering
    \includegraphics[width=0.4\linewidth]{images/polygon-tree.png}
    \caption{Polygon with vertices connected to form a tree}
    \label{fig:tree-in-polygon}
\end{figure}
\begin{figure}[h!]
    \centering
    \includegraphics[width=0.4\linewidth]{images/tree.png}
    \caption{Tree formed by connecting vertices}
    \label{fig:tree}
\end{figure}
\begin{proof}
	We observe that degree of every vertex other than the vertex surrounded by edge $e$ is 2. This vertex will act as root to our full binary tree. All other vertices have degree 3 because each vertex is surrounded by a triangle and if a side is a diagonal, it will be connected to vertex which is surrounded by triangle that shares the diagonal and if the side is a polygon edge, then there will be an open edge corresponding to it originating from the vertex. Therefore the resulting graph formed is a full binary tree with our vertices as $n$ internal nodes and vertices corresponding to open edges are $n+1$ leaves (because there are $n+2$ edges and one edge is under consideration). This completes the description of bijection.
\end{proof}

We leave it as an exercise to the reader to prove that the mapping defined above is indeed a bijection.

\paragraph{Bijection from binary trees to full binary trees}
In this section we are interested in connection between binary and full binary trees. Recall that a full binary tree is one in which each node has either 0 or two children. On the other hand, when we say binary tree then it only means that each node can have at most two children. We want to find a bijection between set of binary trees with $n$ internal nodes and set of full binary trees with certain number of internal nodes.

First of all lets try to see how to convert a given binary tree into a full binary tree so that we can reverse the process, i.e. recover the original (binary) tree back from the full binary tree without ambiguity.

Here is the first attempt:

\noindent \underline{Attempt 1:} First natural approach can be to add a leaf node to all non-full (internal nodes having only one child) nodes, as shown in figure~\ref{fig:bt-fbt-attempt1}
\begin{figure}[h!]
    \centering
    \includegraphics[width=0.7\linewidth]{images/binary-to-full-binary-1.png}
    \caption{Binary to full binary tree attempt1: adding a child node to each non full node}
    \label{fig:bt-fbt-attempt1}
\end{figure}

But notice that this transformation is not injective. For example, it can be observed that  both the trees in figure~\ref{fig:bt-fbt-attempt1-issue} map to same full binary tree.
\begin{figure}[h!]
    \centering
    \includegraphics[width=0.7\linewidth]{images/binary-to-full-binary-2.png}
    \caption{Two different binary trees that map to same full binary tree}
    \label{fig:bt-fbt-attempt1-issue}
\end{figure}

\noindent\underline{Attempt 2(correct)}
Lets try a slightly different approach. Given a binary tree, do the following:
\begin{itemize}
    \item to each leaf node, add two children
    \item to each internal node having only one child, add another child
\end{itemize}
Figure~\ref{fig:binary-to-full-solution} shows the full binary tree constructed in this way for the same binary tree as in Figure~\ref{fig:bt-fbt-attempt1}.
\begin{figure}[h!]
    \centering
    \includegraphics[width=0.5\linewidth]{images/full-binary-3.png}
    \caption{Full binary tree for the (non-full) binary tree given in fig~\ref{fig:bt-fbt-attempt1}. Notice that all the leaf nodes are added during transformation}
    \label{fig:binary-to-full-solution}
\end{figure}
We can see that this solution addresses the issue in the first attempt. Intuitively because of following argument: in the previous attempt the problem was that given a full binary tree, it was hard to decide if a leaf node was originally present in the binary tree or added during transformation. Now, in the current solution, this issue does not arise, because for any leaf node originally present in the binary tree, we add two new leaves as its children. Thus, it can be observed that all the leaf nodes (and only these nodes) are added during transformation.

To see that this translation is well-defined, we can see that the transformed tree is full binary tree by construction itself. Surjectivity is also easy to prove. To recover a binary tree from any given full binary tree, simply remove all the leaf nodes. We discussed injection informally. To give a formal argument, we first need to identify how to characterize two different binary trees? One of the hint as given during the discussion is to assign address to the nodes in the form of binary string, where 0-1 represents left or right child.

Here we argued the bijection only intuitively and there are many things to be worked out formally. For example, proof for injection is not formally argued. Also, to argue surjection, we need to fix the number of nodes in full binary tree. Once we figure out this number, the argument for transformation being  well-defined also need to take that into account.

Writing a complete formal proof of bijection is left as homework exercise.

\paragraph{Bijection between plane trees and full binary trees}
A plane tree is a rooted tree with an ordering among the children. A plane tree can have more than two children. Figure~\ref{fig:plane-tree-1} shows a plane tree.

\begin{figure}[h!]
    \centering
    \includegraphics[width=0.8\linewidth]{images/plane-tree.png}
    \caption{An example of plane trees and its transformation to a binary tree}
    \label{fig:plane-tree-1}
\end{figure}

We are interested in studying the connection between plane trees and binary trees. The number of plane trees with $n$ nodes is equal to the number of binary trees with $n$ nodes. Thus, there is bijection between set of plane trees with $n$ nodes and the set of  binary trees with $n$ nodes.

Here we define the bijection function.

\noindent\underline{The Bijection:} Given any plane tree, do the following
\begin{itemize}
    \item For each node in the tree,
    \begin{itemize}
        \item add its first child in plane tree as its left child in binary tree
        \item add its immediate sibling on right as its right child in binary tree.
    \end{itemize} child in the binary tree.
\end{itemize}
By following the above rule, we get a binary tree from given plane tree.

Observe that in the binary tree thus obtained, root node has only one child, while in general, in a binary tree the root can have both its children. Hence, we won't include the root as part of the binary tree.

Writing formal argument for all the properties is left as homework excercise.


%!TEX root = ../notes.tex
\section{March 16, 2022}
\subsection{Addition on Elliptic Curves \emph{continued}}
We said last time that we had some rules:
\begin{itemize}
    \item $P + \cO = \cO + P = P$.
    \item If $x_1 = x_2$ and $y_1 = -y_2$, we have
          \[P_1 + P_2 = \cO\]
    \item
          In other cases, we need to calculate the slope of the line.
          \begin{center}
              \includegraphics[width=0.5\textwidth]{images/ec_two_intersection.png}
          \end{center}
          If $P_1\neq P_2$, the slope of $L$ is
          \[\lambda = \frac{y_2-y_1}{x_2-x_1}\]
          \begin{center}
              \includegraphics[width=0.5\textwidth]{images/ec_tangent.png}
          \end{center}
          If $P_1 = P_2$, then the slope would be the tangent:
          \begin{align*}
              y^2           & = x^3 + ax + b      \\
              2y\cdot dy    & = (3x^2+a)\cdot dx  \\
              \frac{dy}{dx} & = \frac{3x^2+a}{2y}
          \end{align*}
          so we have
          \[\lambda = \frac{3x^2+a}{2y}\]

          We want to solve systems
          \[\begin{cases}
                  y^2 = x^3 + ax + b \\
                  y = y_1 + \lambda(x-x_1)
              \end{cases}\]
          which gives us
          \begin{align*}
              [y_1 + \lambda(x - x_1)]^2 & = x^3 + ax + b                                       \\
              0                          & = x^3 - \boxed{\lambda^2}x^2 + (a + 2\lambda_1 x_1)x
              + b-y_1 - \lambda x_1^2                                                           \\
                                         & = (x-x_1)(x-x_2)(x-x_3)                              \\
                                         & = x^3 - \boxed{(x_1+x_2+x_3)}x^2 +
              (x_1x_3 + x_2x_3 + x_1x_2)x - x_1x_2x_3
          \end{align*}
          With some working out (taking the coefficient of $x^2$), \[\boxed{x_3 = \lambda^2 - x_1 - x_2}\] and $-y_3 = y_1 + \lambda(x_3 - x_1)$ so we have \[\boxed{y_3 = -y_1 - \lambda(x_3 - x_1)}\] are our points by addition where
          \[\lambda = \frac{y_2 - y_1}{x_2 - x_1}\]
          or when $P_1 = P_2$,
          \[\lambda = \frac{3x_1^2 + a}{2y_1}.\]
\end{itemize}

\subsection{Elliptic Curves over Finite Fields}
Our definition stays the same, except $x, y$ are elements of $\ZZ/p\ZZ$. How do we add points? We could do it geometrically, but setting this up is outside the scope of this class\dots

For addition, we use the same formulas that we've derived for $x_3$ and $y_3$, and they still make perfect sense mod $p$. \emph{For whatever reasonable notion of geometry we have over $\ZZ/p\ZZ$, they work with these formulas.}

\begin{example}
    We take elliptic curve
    \[y^2 = x^3 + x + 2\quad \text{ over }\ZZ/5\ZZ.\]
    How do we find elements in this elliptic curve? We can try them all.
    \begin{itemize}
        \item If $x = 0$, $y^2 = 2$, of which there are no solutions.
        \item If $x = 1$, $y^2 = 4$, of which $y = 2, 3$ are solutions.
        \item If $x = 2$, $y^2 = 2$ again, of which there are no solutions.
        \item If $x = 3$, $y^2 = 2$, of which there are no solutions.
        \item If $x = 4$, $y^2 = 0$, so $y = 0$ is one solution.
    \end{itemize}
    We have $(1,2), (1,3), (4,0), \cO$ are the elements of this elliptic curve. We have these
    \[\begin{array}{c|cccc}
            +     & \cO   & (1,2) & (4,0) & (1,3) \\ \hline
            \cO   & \cO   & (1,2) & (4,0) & (1,3) \\
            (1,2) & (1,2) & (4,0) & (1,3) & \cO   \\
            (4,0) & (4,0) & (1,3) & \cO   & (1,2) \\
            (1,3) & (1,3) & \cO   & (1,2) & (4,0)
        \end{array}\]
\end{example}
Let's implement this:
\begin{lstlisting}[language=Python]
O = "the point O"
def add(P1, P2, a, p):
    if P1 == O:
        return P2
    if P2 == O:
        return P1
    x1, y1 = P1
    x2, y2 = P2
    if x1 == x2 and (y1 + y2) % p == 0:
        return O
    if P1 == P2:
        lam = (3 * x1**2 + a) * ext_gcd(2 * y1, p)[0] % p
    else:
        lam = (y2 - y1) * ext_gcd(x2 - x1, p)[0] % p
    x3 = (lam**2 - x1 - x2) % p
    y3 = (lam * (x1 - x3) - y1) % p
    return x3, y3
\end{lstlisting}