\documentclass[t]{beamer}
\usetheme{Copenhagen}
\usepackage{amsmath, tikz, pgfplots, array, graphicx}
\tikzset{>=stealth}
\pgfplotsset{compat=newest}
\pgfplotsset{every tick label/.append style={font=\scriptsize}}
\setbeamertemplate{headline}{} % remove toc from headers
\beamertemplatenavigationsymbolsempty
\everymath{\displaystyle}

\title{Graphs of Tangent, Cotangent, Secant, and Cosecant}
\date{}

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

\begin{document}

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

\section{Determine the amplitude, period, phase shift, and vertical shift of the tangent and cotangent graphs.}

\begin{frame}{Tangent and Cotangent Graphs}
Recall that $\tan = \frac{y}{x}$. \\[20pt]    \pause

Since $x$- and $y$-coordinates can be positive, negative, or zero, the graphs of tangent and cotangent functions pose some interesting behavior; in particular, when the $x$-coordinate is 0.
\end{frame}

\begin{frame}[c]{Vertical Asymptotes}
    A \alert{vertical asymptote} is a vertical line that the graph will get infinitely close to, but never cross.
\end{frame}

\begin{frame}[c]{Tangent Graph}
\begin{center}
    \begin{tikzpicture}
    \begin{axis}[
    axis lines = middle,
    xmin = -2, xmax = 6.5,
    ymin = -3.5, ymax = 3.5,
    grid, domain=0:8.5,
    xtick = {-1.57, 0, 1.57, 3.14, 4.71, 6.29},
    xticklabels = {$-90^\circ$, 0, $90^\circ$, $180^\circ$, $270^\circ$, $360^\circ$},
    xlabel style={at=(current axis.right of origin), anchor=west},
    ytick = {-3,-2,...,3},
    ylabel style={at=(current axis.above origin), anchor=south}
    ]
    \addplot [color=blue, line width = 1, smooth, domain=-0.45*pi:0.45*pi] {tan(deg(x))};
    \addplot [color=blue, line width = 1, smooth, domain=0.55*pi:1.45*pi] {tan(deg(x))};
    \addplot [color=blue, line width = 1, smooth, domain=1.55*pi:2*pi] {tan(deg(x))};
    \addplot [color=violet, line width = 1.5, dashed] coordinates {(-1.57,-3.5) (-1.57,3.5)};
    \addplot [color=violet, line width = 1.5, dashed] coordinates {(1.57,-3.5) (1.57,3.5)};
    \addplot [color=violet, line width = 1.5, dashed] coordinates {(4.71,-3.5) (4.71,3.5)};
    \end{axis}
    \end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}[c]{Cotangent Graphs}
\begin{center}
    \begin{tikzpicture}
    \begin{axis}[
    axis lines = middle,
    xmin = -2, xmax = 6.5,
    ymin = -3.5, ymax = 3.5,
    grid, domain=0:8.5,
    xtick = {-1.57, 0, 1.57, 3.14, 4.71, 6.29},
    xticklabels = {$-90^\circ$, 0, $90^\circ$, $180^\circ$, $270^\circ$, $360^\circ$},
    xlabel style={at=(current axis.right of origin), anchor=west},
    ytick = {-3,-2,...,3},
    ylabel style={at=(current axis.above origin), anchor=south}
    ]
    \addplot [color=blue, line width = 1, smooth, domain=-0.65*pi:-0.05, <->, >=stealth] {cot(deg(x))};
    \addplot [color=blue, line width = 1, smooth, domain=0.05*pi:0.95*pi] {cot(deg(x))};
    \addplot [color=blue, line width = 1, smooth, domain=1.05*pi:1.95*pi] {cot(deg(x))};
    \addplot [color=violet, line width = 1.5, dashed] coordinates {(0,-3.5) (0,3.5)};
    \addplot [color=violet, line width = 1.5, dashed] coordinates {(3.14,-3.5) (3.14,3.5)};
    \addplot [color=violet, line width = 1.5, dashed] coordinates {(6.29,-3.5) (6.29,3.5)};
    \end{axis}
    \end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}{Amplitude?}
    The graphs of tangent and cotangent functions do not stop going up or down. Thus, they have neither a maximum point nor a minimum point. \newline\\ \pause

    In other words, \emph{tangents and cotangents have no amplitude}.
\end{frame}

\begin{frame}{Period of Tangent and Cotangent}
    Tangents and cotangents complete one full cycle between asymptotes. Notice for each graph, that period is $180^\circ, \text{ or } \pi$ radians. \newline\\ \pause

    Like the graphs of sine and cosine, multiplying the inputs, $x$, by a positive value other than 1 will affect the period of the graphs for tangent and cotangent.    \newline\\ \pause

    Instead of dividing $360^\circ$ (or $2\pi$) by that value, for tangent and cotangent divide $180^\circ$ (or $\pi$ radians).
\end{frame}

\begin{frame}{Shifts}
Determining phase shift and vertical shift follow the same procedures as that for sine and cosine.
\end{frame}

\begin{frame}{Example 1}
Determine the amplitude, period, phase shift, and vertical shift of each of the following.    \newline\\
(a) \quad $y = 2\tan \left(x - 45^\circ \right)$    \newline\\  \pause

Amplitude: None \newline\\  \pause
Period: $\frac{180^\circ}{1} = 180^\circ$
\end{frame}

\begin{frame}{Example 1 \quad $y = 2\tan \left(x - 45^\circ \right)$}
Phase Shift:
\begin{align*}
    \onslide<2->{x - 45 &= 0} \\
    \onslide<3->{x &= 45}
\end{align*}     \pause
\onslide<4->{Phase Shift: $45^\circ$ right}   \newline\\
\onslide<5->{Vertical Shift: 0 (or none)}
\end{frame}

\begin{frame}{Example 1}
(b) \quad $y = -\frac{1}{3}\cot x + 1$    \newline\\  \pause
Amplitude: None \newline\\ \pause
Period: $\frac{180^\circ}{1} = 180^\circ$   \newline\\  \pause
Phase Shift: 0 (or none) \newline\\ \pause
Vertical Shift: Up 1
\end{frame}

\begin{frame}{Example 1}
(c) \quad $y = 1.5\tan\left(2x + 120^\circ\right) - 5$  \newline\\ \pause
Amplitude: None \newline\\  \pause
Period: $\frac{180^\circ}{2} = 90^\circ$
\end{frame}

\begin{frame}{Example 1 \quad $y = 1.5\tan\left(2x + 120^\circ\right) - 5$}
Phase Shift:
\begin{align*}
    \onslide<2->{2x + 120 &= 0}   \\
    \onslide<3->{2x &= -120} \\
    \onslide<4->{x &= -60}
\end{align*}
\onslide<5->{Phase Shift: $60^\circ$ left}    \newline\\
\onslide<6->{Vertical Shift: 5 down}
\end{frame}

\section{Determine the amplitude, period, phase shift, and vertical shift of the secant and cosecant graphs.}

\begin{frame}[c]{Secant Graph}
\begin{center}
    \begin{tikzpicture}
    \begin{axis}[
    axis lines = middle,
    xmin = -2, xmax = 6.5,
    ymin = -3.5, ymax = 3.5,
    grid, domain=0:8.5,
    xtick = {-1.57, 0, 1.57, 3.14, 4.71, 6.29},
    xticklabels = {$-90^\circ$, 0, $90^\circ$, $180^\circ$, $270^\circ$, $360^\circ$},
    xlabel style={at=(current axis.right of origin), anchor=west},
    ytick = {-3,-2,...,3},
    ylabel style={at=(current axis.above origin), anchor=south}
    ]
    \addplot [color=blue, line width = 1, smooth, domain=-0.45*pi:0.45*pi] {sec(deg(x))};
    \addplot [color=blue, line width = 1, smooth, domain=0.55*pi:1.45*pi] {sec(deg(x))};
    \addplot [color=blue, line width = 1, smooth, domain=1.55*pi:2*pi] {sec(deg(x))};
    \addplot [color=violet, line width = 1.5, dashed] coordinates {(-1.57,-3.5) (-1.57,3.5)};
    \addplot [color=violet, line width = 1.5, dashed] coordinates {(1.57,-3.5) (1.57,3.5)};
    \addplot [color=violet, line width = 1.5, dashed] coordinates {(4.71,-3.5) (4.71,3.5)};
    \end{axis}
    \end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}{Cosecant Graph}
\begin{center}
    \begin{tikzpicture}
    \begin{axis}[
    axis lines = middle,
    xmin = -2, xmax = 6.5,
    ymin = -3.5, ymax = 3.5,
    grid, domain=0:8.5,
    xtick = {-1.57, 0, 1.57, 3.14, 4.71, 6.29},
    xticklabels = {$-90^\circ$, 0, $90^\circ$, $180^\circ$, $270^\circ$, $360^\circ$},
    xlabel style={at=(current axis.right of origin), anchor=west},
    ytick = {-3,-2,...,3},
    ylabel style={at=(current axis.above origin), anchor=south}
    ]
    \addplot [color=blue, line width = 1, smooth, domain=-0.65*pi:-0.05, <->, >=stealth] {cosec(deg(x))};
    \addplot [color=blue, line width = 1, smooth, domain=0.05*pi:0.95*pi] {cosec(deg(x))};
    \addplot [color=blue, line width = 1, smooth, domain=1.05*pi:1.95*pi] {cosec(deg(x))};
    \addplot [color=violet, line width = 1.5, dashed] coordinates {(0,-3.5) (0,3.5)};
    \addplot [color=violet, line width = 1.5, dashed] coordinates {(3.14,-3.5) (3.14,3.5)};
    \addplot [color=violet, line width = 1.5, dashed] coordinates {(6.29,-3.5) (6.29,3.5)};
    \end{axis}
    \end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}{Relationship to Sine and Cosine}
    If we graph $y = \cos x$ in the same plane as $y = \sec x$, we see some interesting features:
    \begin{center}
    \begin{tikzpicture}
    \begin{axis}[
    axis lines = middle,
    xmin = -2, xmax = 6.5,
    ymin = -3.5, ymax = 3.5,
    grid, domain=0:8.5,
    xtick = {-1.57, 0, 1.57, 3.14, 4.71, 6.29},
    xticklabels = {$-90^\circ$, 0, $90^\circ$, $180^\circ$, $270^\circ$, $360^\circ$},
    xlabel style={at=(current axis.right of origin), anchor=west},
    ytick = {-3,-2,...,3},
    ylabel style={at=(current axis.above origin), anchor=south}
    ]
    \addplot [color=blue, line width = 1, smooth, domain=-0.45*pi:0.45*pi] {sec(deg(x))};
    \addplot [color=blue, line width = 1, smooth, domain=0.55*pi:1.45*pi] {sec(deg(x))};
    \addplot [color=blue, line width = 1, smooth, domain=1.55*pi:2*pi] {sec(deg(x))};
    \addplot [color=violet, line width = 1.5, dashed] coordinates {(-1.57,-3.5) (-1.57,3.5)};
    \addplot [color=violet, line width = 1.5, dashed] coordinates {(1.57,-3.5) (1.57,3.5)};
    \addplot [color=violet, line width = 1.5, dashed] coordinates {(4.71,-3.5) (4.71,3.5)};
    \addplot [color=red, domain=-1.57:6.29, line width=1] {cos(deg(x))};
    \end{axis}
    \end{tikzpicture}
    \end{center}
\end{frame}

\begin{frame}{Cosine and Secant}
Notice that when $\cos x$ is at a maximum, we get a ``smile" on the secant graph, and when $\cos x$ is at a minimum, we get a ``frown" on the secant graph. \newline\\ \pause

Also, whenever $y=\cos x$ crosses the $x$-axis, there is a vertical asymptote for $y=\sec x$ (why?)   \newline\\ \pause

The same logic applies with $y=\csc x$ and $y=\sin x$.
\end{frame}

\begin{frame}{Sine and Cosecant}
    \begin{center}
    \begin{tikzpicture}
    \begin{axis}[
    axis lines = middle,
    xmin = -2, xmax = 6.5,
    ymin = -3.5, ymax = 3.5,
    grid, domain=0:8.5,
    xtick = {-1.57, 0, 1.57, 3.14, 4.71, 6.29},
    xticklabels = {$-90^\circ$, 0, $90^\circ$, $180^\circ$, $270^\circ$, $360^\circ$},
    xlabel style={at=(current axis.right of origin), anchor=west},
    ytick = {-3,-2,...,3},
    ylabel style={at=(current axis.above origin), anchor=south}
    ]
    \addplot [color=blue, line width = 1, smooth, domain=-0.95*pi:-0.05*pi] {cosec(deg(x))};
    \addplot [color=blue, line width = 1, smooth, domain=0:0.95*pi] {cosec(deg(x))};
    \addplot [color=blue, line width = 1, smooth, domain=1.05*pi:1.95*pi] {cosec(deg(x))};
    \addplot [color=blue, line width = 1, smooth, domain=2.05*pi:2.95*pi] {cosec(deg(x))};
    \addplot [color=violet, line width = 1.5, dashed] coordinates {(0,-3.5) (0,3.5)};
    \addplot [color=violet, line width = 1.5, dashed] coordinates {(3.14,-3.5) (3.14,3.5)};
    \addplot [color=violet, line width = 1.5, dashed] coordinates {(6.28,-3.5) (6.28,3.5)};
    \addplot [color=red, domain=-1.57:6.29, line width=1] {sin(deg(x))};
    \end{axis}
    \end{tikzpicture}
    \end{center}
\end{frame}

\begin{frame}{Properties}
Once again, since there are no maximum nor minimum points, secant and cosecant do not have an amplitude.   \newline\\ \pause

The period of the graphs of secant and cosecant can be found by determining how long it takes one full smile and one full frown to appear. \newline\\ \pause

From the graphs, we can see that it is $360^\circ$, or $2\pi$ radians (just like sine and cosine).  \newline\\ \pause

Multiplying the inputs by a positive value other than 1 changes the period. \newline\\ \pause

Phase shifts and vertical shifts are calculated in the same way as the other four trig functions.
\end{frame}

\begin{frame}{Example 2}
the amplitude, period, phase shift, and vertical shift for each of the following.   \newline\\
(a) \quad $y = 2\sec \left(x - 45^\circ \right)$    \newline\\  \pause
Amplitude: None \newline\\  \pause
Period: $\frac{360^\circ}{1} = 360^\circ$
\end{frame}

\begin{frame}{Example 2 \quad $y = 2\sec \left(x - 45^\circ \right)$}
Phase Shift:
\begin{align*}
    \onslide<2->{x - 45 &= 0} \\
    \onslide<3->{x &= 45} \\
\end{align*}
\onslide<4->{Phase Shift: $45^\circ$ right} \newline\\ \pause
\onslide<5->{Vertical Shift: 0 (or none)}
\end{frame}

\begin{frame}{Example 2}
(b) \quad $y = -\frac{1}{3}\csc x + 1$  \newline\\  \pause
Amplitude: None \newline\\ \pause
Period: $\frac{360^\circ}{1} = 360^\circ$   \newline\\  \pause
Phase Shift: None   \newline\\  \pause
Vertical Shift: Up 1
\end{frame}

\begin{frame}{Example 3}
(c) \quad $y = 1.5\csc\left(2x + 120^\circ\right) - 5$  \newline\\  \pause
Amplitude: None \newline\\  \pause
Period: $\frac{360^\circ}{2} = 180^\circ$
\end{frame}

\begin{frame}{Example 3 \quad }$y = 1.5\csc\left(2x + 120^\circ\right) - 5$
Phase Shift:
\begin{align*}
    \onslide<2->{2x + 120 &= 0} \\
    \onslide<3->{2x &= -120} \\
    \onslide<4->{x &= -60}  \\
\end{align*}
\onslide<5->{Phase Shift: $60^\circ$ left} \newline\\
\onslide<6->{Vertical Shift: 5 down}
\end{frame}

\begin{frame}[c]{Summary}
\begin{center}
\setlength{\extrarowheight}{11pt}
\scalebox{0.8}{
\begin{tabular}{|c|c|c|c|c|}
    \hline
    &   \textbf{Amplitude}  &   \textbf{Period} &   \textbf{Phase Shift}  &   \textbf{Vertical Shift} \\[6pt] \hline
    $y=A\tan(Bx-C)+D$   &   None    &   $\dfrac{180^\circ}{B} \text{ or } \dfrac{\pi}{B}$   &
    $\dfrac{C}{B}$  &   $D$ \\[11pt]  \hline

    $y=A\cot(Bx-C)+D$   &   None    &   $\dfrac{180^\circ}{B} \text{ or } \dfrac{\pi}{B}$   &
    $\dfrac{C}{B}$  &   $D$ \\[11pt]  \hline

    $y=A\sec(Bx-C)+D$   &   None    &   $\dfrac{360^\circ}{B} \text{ or } \dfrac{2\pi}{B}$   &
    $\dfrac{C}{B}$  &   $D$ \\[11pt]  \hline

    $y=A\csc(Bx-C)+D$   &   None    &   $\dfrac{360^\circ}{B} \text{ or } \dfrac{2\pi}{B}$   &
    $\dfrac{C}{B}$  &   $D$ \\[11pt]  \hline
\end{tabular}}
\end{center}
\end{frame}

\end{document}


\documentclass[10pt]{report}

\usepackage{geometry}
\geometry{
	a4paper,
	margin=1in,
	footskip=0.25in
}

\usepackage{enumerate} % for enumerate counter
\usepackage{subcaption} % for subfigures
\usepackage{amsthm} % for QED
\usepackage{mathtools} % for delimiter

\usepackage{listings} % for code
\lstset{
	language=R,
	basicstyle=\footnotesize\ttfamily,
	numbers=none,
	stepnumber=1,
	numbersep=8pt,
	showspaces=false,
	showstringspaces=false,
	showtabs=false,
	frame=single,
	tabsize=2,
	captionpos=t,
	breaklines=true,
	breakatwhitespace=false
}

\usepackage{float} % for figure [H]
\usepackage{booktabs} % for tabular
\usepackage{caption} % for \caption*
\usepackage[export]{adjustbox} % for valign=t
\usepackage{array} % for column type m
\usepackage{verbatim}
\usepackage{graphicx}
%\graphicspath{ {imgs/} }

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead[L]{\hwAuther}
\fancyhead[C]{\courseNo}
\fancyhead[R]{\hwNo}

\usepackage{amssymb}
\usepackage{amsmath}

%Cover
\newcommand{\courseTitle}{Introduction to Mathematical Modeling}
\newcommand{\courseNo}{Math 380}
\newcommand{\hwAuther}{Zhihao Ai}

\newcommand{\hwNo}{HW \#1}
\newcommand{\hwDate}{Due on 01/23}

\title{
	\courseTitle\\
	\hwNo\\
	\hwDate
}
\author{\hwAuther}
\date{}
%

%Custom
%\everymath{\displaystyle}
\setlength\parindent{0pt}

%Custom commands
\newcommand{\ds}{\displaystyle}
\newcommand{\ts}{\textstyle}

\newcolumntype{N}{>$ c <$}
\newcolumntype{M}[1]{>{\centering\arraybackslash $}m{#1}<{$}}

\newcommand{\abs}[1] {\left| #1 \right|}

\DeclarePairedDelimiter\autoparen{(}{)}
\newcommand{\pa}[1]{\autoparen*{#1}}

\newcommand{\var} {\text{var}}

\newcommand{\m}[1] {\mathbf{#1}}

\begin{document}

\maketitle

\section*{Section 1.1}
\begin{enumerate}
	\item[3.]
	By examining the following sequences, write a difference equation to represent the change during the $n$th interval as a function of the previous term in the sequence.

	\begin{enumerate}
		\item [b.]
		\{2, 4, 16, 256\}
		\begin{align*}
			a_{n+1} &= a_n^2, \quad n=0,1,2,3,\dots\\
			a_0 &= 2
		\end{align*}
		where $a_n$ is the $n$-th term of the sequence.

		\item [c.]
		\{1, 2, 5, 11, 23\}
		\begin{align*}
			a_{n+1} &= 2 a_n + 1, \quad n=1,2,3,\dots\\
			a_0 &= 1\\
			a_1 &= 2
		\end{align*}
		where $a_n$ is the $n$-th term of the sequence.
	\end{enumerate}

	\item [10.]
	Your grandparents have an annuity. The value of the annuity increases each month by an automatic deposit of 1\% interest on the previous month's balance. Your grandparents withdraw \$1000 at the beginning of each month for living expenses. Currently, they have \$50,000 in the annuity. Model the annuity with a dynamical system. Will the annuity run out of money? When? \textit{Hint}: What value will $a_n$ have when the annuity is depleted?

	Denote the value of the annuity after $n$ months as $a_n$. Assuming the value of the annuity increases each month before the grandparents withdraw \$1000, we have
	\begin{align*}
		a_{n+1} &= a_n + 0.01 a_n - 1000, \quad n=0,1,2,3,\dots\\
		a_0 &= 50000
	\end{align*}
	where $a_n$ represents the value of the annuity after $n$ months. Since $a_{70}$ is the first negative term, the annuity will run out of money after 70 months.

	\item [12.]
	Your current credit card balance is \$12,000 with a current rate of 19.9\% per year. Interest is charged monthly. Determine what monthly payment $p$ will pay off the card in

	\begin{enumerate}
		\item [12a.]
		Two years, assuming no new charges.

		Denote the credit card balance after $n$ months as $a_n$. A rate of 19.9\% per year is equal to 1.658\% per month. Assuming the interest is charged before the monthly payment is made, we have
		\begin{align*}
			a_{n+1} &= a_n +  0.01658 a_n - p, \quad n=0,1,2,3,\dots\\
			a_0 &= 12000
		\end{align*}
		To make $a_{24}$ less than or equal to 0, meaning the amount left to be paid is 0, $p$ is computed to be approximately \$610.16.

		\item [13a.]
		Two years, assuming each month you charge \$105.

		Assume \$105 is charged before monthly interset. We have
		\begin{align*}
			a_{n+1} &= 1.01658 (a_n + 105) - p, \quad n=0,1,2,3,\dots\\
			a_0 &= 12000
		\end{align*}
		The monthly payment $p$ is computed to be approximately \$716.91.
	\end{enumerate}
\end{enumerate}

\section*{Section 1.2}
\begin{enumerate}
	\item [2.]
	The following data represent the U.S. population from 1790 to 2010. Find a dynamical system model that fits the data fairly well. Test your model by plotting the predictions of the model against the data.

	Let $p_n$ denote the U.S. population $10 n$ years after 1790. Plot $p_n$ against $n$, $\Delta p_n$ against $p_n$, and $\Delta p_n$ against $(4\times 10^8  - p_n) p_n$ assuming the carrying capacity is 400 millions:
	\begin{figure}[H]
		\centering
		\begin{subfigure}[b]{.3\linewidth}
			\caption{$p_n$ vs $n$}
			\includegraphics[width=\linewidth]{s1_2/p-n.png}
		\end{subfigure}
		\begin{subfigure}[b]{.3\linewidth}
			\caption{$\Delta p_n$ vs $p_n$}
			\includegraphics[width=\linewidth]{s1_2/dp-p.png}
		\end{subfigure}
		\begin{subfigure}[b]{.3\linewidth}
			\caption{$\Delta p_n$ vs $(4\times 10^8  - p_n) p_n$}
			\includegraphics[width=\linewidth]{s1_2/dp-(c-p)p.png}
		\end{subfigure}
	\end{figure}
	Plot (a) shows that the population is not linear correlated with the year, so we turn to the first differences. There is roughly a linear relationship in (b) but it unreasonably predicts a non-stop increase of population. Taking into account the carrying capacity, we have figure (c) where the least square estimate of the slope of the regression line is $5.9145\times 10^{-10}$. Thus, we propose the model to be
	\begin{align*}
		p_{n+1} &= p_n + 5.9145\times 10^{-10} (4\times 10^8 - p_n) p_n, \quad n=0,1,2,3,\dots\\
		p_0 &= 3929000
	\end{align*}
	The predictions of the model against the data is shown below:
	\begin{figure}[H]
		\centering
		\includegraphics[width=0.5\linewidth]{s1_2/data-pred.png}
	\end{figure}

	\item [3.]
	Sociologists recognize a phenomenon called \textit{social diffusion}, which is the spreading of a piece of information, a technological innovation, or a cultural fad among a population. The members of the population can be divided into two classes: those who have the information and those who do not. In a fixed population whose size is known, it is reasonable to assume that the rate of diffusion is proportional to the number who have the information times the number yet to receive it. If $a_n$ denotes the number of people who have the information in a population of $N$ people after $n$ days, formulate a dynamical system to approximate the change in the number of people in the population who have the information.

	According to the assumption on the rate of diffusion,
	\[
	\Delta a_n = k (N - a_n) a_n
	\]
	where $k$ is the estimated proportionality constant. Thus, we have the dynamical system
	\begin{align*}
		a_{n+1} &= a_n + k (N - a_n) a_n, \quad n=0,1,2,3,\dots\\
		a_0 &= c
	\end{align*}
	where $c$ is the initial number of people who have the information.

	\item [9.]
	The data in the table show the speed $n$ (in increments of 5 mph) of an automobile and the associated distance $a_n$ in feet required to stop it once the brakes are applied. For instance $n=6$ (representing $6\times 5 = 30$ mph) requires a stopping distance of $a_6 = 47$ ft.
	\begin{enumerate}
		\item
		Calculate and plot the change $\delta a_n$ versus $n$. Does the graph reasonably approximate a linear relationship?
		\begin{figure}[H]
			\centering
			\includegraphics[width=0.4\linewidth]{s1_2/da-n.png}
		\end{figure}
		It reasonably approximates a linear relationship.

		\item
		Based on your conclusions in part (a), find a difference equation model for the stopping distance data. Test your model by plotting the errors in the predicted values against $n$. Discuss the appropriateness of the model.

		Since $\Delta a$ is proportional to $n$, and the least square estimate of the proportionality constant is approximately 3.1395, we have $\Delta a = 3.1395 n$ and the following model
		\begin{align*}
			a_{n+1} &= a_n + 3.1395 n, \quad n=1,2,3,\dots\\
			a_1 &= 3
		\end{align*}
		The plots of predicted values and the errors are shown below:
		\begin{figure}[H]
			\centering
			\begin{subfigure}[b]{.4\linewidth}
				\caption{True and Predicted}
				\includegraphics[width=\linewidth]{s1_2/9-data-pred.png}
			\end{subfigure}
			\begin{subfigure}[b]{.4\linewidth}
				\caption{Error}
				\includegraphics[width=\linewidth]{s1_2/9-error.png}
			\end{subfigure}
		\end{figure}
		The errors are small so the model is appropriate.
	\end{enumerate}
\end{enumerate}

\end{document}


\documentclass[11pt]{article}

\usepackage{common}
\usepackage{amsmath}
\usepackage{amssymb}

\title{HW1: Classification}
\author{Emily Tseng \\ et397@cornell.edu}
\begin{document}

\maketitle{}
\section{Introduction}

Given a sentence from a movie review, e.g. ``\textit{Never inspires more than an interested detachment.}'', how might we classify its sentiment, in this case \textit{negative}?

In this assignment, we use the implementation of a handful of basic statistical models for this task to familiarize ourselves with core concepts and technologies in modern NLP. Specifically, we explored the use of the following models:

\begin{itemize}
  \item \textbf{Multinomial Naive Bayes (MNB)}, as outlined in \cite{wang2012baselines}
  \item \textbf{Logistic Regression (LR)}
  \item \textbf{Continuous Bag of Words (CBOW)}, as outlined in \cite{mikolov2013efficient}
  \item \textbf{Convolutional Neural Network (CNN)}, as outlined in \cite{kim2014convolutional}
  \item \textbf{CNN*}, a variant of the CNN using pretrained word embeddings
\end{itemize}

As a dataset, we used the Stanford Sentiment Treebank (SST-2) \citep{socher2013recursive}, which provides a corpus of sentences from movie reviews labeled as \textit{positive}, \textit{negative}, or \textit{neutral}. For this assignment, we removed neutral samples and formulated the task as a binary classification problem between positive and negative sentiment.

\section{Problem Description}

Given a sentence represented as a feature vector $\boldx$, we can find its predicted class $y \in \{-1, 1\}$, where -1 indicates negative sentiment and 1 indicates positive sentiment, via:
\begin{align}
  y = \sigma(\boldW\boldx + \boldb)
\end{align}

Where $\sigma$ is an activation function, $\boldW$ is a matrix of weights, and $\boldb$ is a vector of biases. $\boldx$ itself is generated using a feature function $\phi$, which can take a number of forms. We elaborate on the specific structures used per model in the subsequent section.

\section{Model and Algorithms}

\subsection{Multinomial Naive Bayes (MNB) over unigrams}

As formulated in \cite{wang2012baselines}, we consider the linear model where a prediction for test case $k$ is expressed as:
\begin{align}
  y^{(k)} = sign(\boldW^{T}\boldx^{(k)} + b)
\end{align}

We consider as features the unigrams within vocabulary $\mcV$, and define $\boldf^{(i)} \in \mathbb{R}^{|\mcV|}$ as the feature count vector for training case $i$ with label $y^{(i)} \in \{-1, 1\}$. $\boldf^{(i)}_j$ is thus the number of occurrences of feature $j$ in training sample $i$. We additionally consider the log-count ratio $r$ of the two count vectors $\boldp$ and $\boldq$, given smoothing parameter $\alpha$:
\begin{align}
  \boldp &= \alpha + \sum_{i:y^{(i)}=1}{\boldf^{(i)}} \\
  \boldq &= \alpha + \sum_{i:y^{(i)}=-1}{\boldf^{(i)}} \\
  \boldr &= log(\frac{\boldp / ||\boldp||_1}{\boldq / ||\boldq||_1})
\end{align}

In this implementation, we consider \textit{binarized count vectors}, in which $\hat{\boldf}_j$ is 1 if feature $j$ occurs at least once in the sentence, and 0 otherwise. We additionally consider our weights $\boldW$ as a vector of log-count ratios, and use as a bias the log-ratio of the number of positive to negative samples. Formally, we define as parameters the following, where $N+$ is the number of positive samples and $N-$ is the number of negative samples:

\begin{align}
  \boldx^{(k)} &= \hat{\boldf}^{(k)}, \hat{\boldf}^{(k)} = \textbf{1}\{f^{(k)} > 0\} \\
  \boldW^T &= \hat{\boldr} = log(\frac{\hat{\boldp} / ||\hat{\boldp}||_1}{\hat{\boldq} / ||\hat{\boldq}||_1}) \\
  \hat{\boldp} &= \alpha + \sum_{i:y^{(i)}=1}{\hat{\boldf}^{(i)}} \\
  \hat{\boldq} &= \alpha + \sum_{i:y^{(i)}=-1}{\hat{\boldf}^{(i)}} \\
  b &= log(\frac{N+}{N-})
\end{align}

\subsection{Logistic Regression}

Recalling (1), we express this model as a single-layer, fully-connected neural network using a sigmoid activation $\sigma$ to learn weight matrix $\boldW$ and bias vector $\boldb$. Here, $\boldx^{(i)}$ represents an input sentence as binarized counts of words in vocabulary $\mcV$, as in (6). The outputs $\boldy$ are thus 1x2 vectors representing the distribution over the two classes, and we take the argmax of $\boldy$ as the prediction. Our implementation randomly initializes all model parameters, uses Pytorch's built-in CrossEntropy loss function and standard stochastic gradient descent (SGD) optimizer, and backpropagates loss at each batch.

\subsection{CBOW}

The Continuous Bag of Words (CBOW) model as described in \citep{mikolov2013efficient} represents an input sentence $\boldx^{(i)}$ of length $n$ as a $k$-dimensional embedding vector $\bolde$ produced by averaging the individual $k$-dimensional embeddings for each word in the sentence. Formally:
\begin{align}
  \bolde = \frac{1}{n} \sum_{i=1}^{n} \bolde_i
\end{align}

This embedding is the input to a single-layer, fully-connected neural network using a softmax activation to learn a weight matrix and bias vector, as before. We take the argmax of the output of this linear layer as the prediction. As before, our implementation randomly initializes all model parameters, including the embeddings $\mcE$.

\subsection{CNN}

The Convolutional Neural Network (CNN) approach to sentence classification as described in \cite{kim2014convolutional} represents input sentences as the concatenation of fixed-length embeddings. In other words, for an input sentence of length $n$:
\begin{align}
  \boldx_{1:n} = x_1 \oplus x_2 \oplus ... \oplus x_{n-1} \oplus x_n
\end{align}

This input is put through a convolutional layer consisting of a set of \textit{filters}. A filter generates a new feature thus:
\begin{align}
  c_i = f(\boldw\boldx_{i:i+h-1} + b)
\end{align}

where $f$ is a non-linear activation function such as the rectified linear unit (ReLU), $\boldw$ is the filter, $h$ is the window size for that convolutional layer, and $b$ is a bias term. Filters are applied over every possible \textit{window} (or \textit{kernel}) of the input to generate a feature map:
\begin{align}
  \boldc = [c_1, c_2, ... c_{n-h+1}]
\end{align}

A max-pooling over time operation is then applied over the feature map, which outputs the $c_i$ with the highest value as the value for that feature.

In our implementation, we follow \cite{kim2014convolutional} and use the ReLU nonlinearity, filters with window sizes 3, 4 and 5, a dropout rate of $p=0.5$ to regularize against overfitting during training, and a softmax activation function over the final output. The argmax of the output of this final layer is our prediction. Of note, in our implementation we padded inputs to a minimum length of 5 to ensure the provided window sizes would work.

\subsection{CNN*}

The CNN model above is initialized with random values as the initial word embeddings. We also experimented with a variant that initialized with pretrained word embeddings, but otherwise used the same structure as the CNN described above.

\section{Experiments}

As depicted in Table \ref{tab:results}, our results show that when the learned models (LR, CBOW, CNN and CNN*) are trained for 20 epochs with a learning rate of $5e^{-2}$, MNB performs best, with a test-set accuracy of 0.82.

\begin{table}[h]
  \centering
  \begin{tabular}{llr}
   \toprule
   Model &  & Test Acc. \\
   \midrule
   \textsc{MNB} & & 0.82\\
   \textsc{LR} & & 0.71\\
   \textsc{CBOW} & & 0.77 \\
   \textsc{CNN} & & 0.70 \\
   \textsc{CNN*} & & 0.78 \\
   \bottomrule
  \end{tabular}
  \caption{\label{tab:results} Results from each model. LR, CBOW, CNN and CNN* models run with $lr=5e^{-2}$, $epochs=20$.}
  \end{table}

% CBOW 5e-2, NLLLoss, Adam: 0.77

Of note, the CBOW experiments used Pytorch's built-in NLLLoss loss function and the Adam optimizer, while the CNN and CNN* experiments used NLLLoss and the Adadelta optimizer per \cite{kim2014convolutional}. The LR experiments used Pytorch's built-in CrossEntropy and the Adam optimizer.

Examination of the losses per epoch shows the learned models are in fact learning, e.g. the losses per epoch decrease over training time (Figure \ref{fig:losses}). From this we infer the low accuracies for the learned models are due to the training parameters used, and do not accurately speak to their performance relative to MNB.

\begin{figure}[h]
  \centering
  \includegraphics[width=4in]{losses.png}
  \caption{\label{fig:losses} Losses over time for the four learned models. All models trained with $lr=5e^{-2}$, $epochs=20$.}
\end{figure}

Still, we can draw conclusions from the relative differences in performance between the learned models: for instance, CNN* significantly outperforms CNN, as expected.

\section{Conclusion}

This assignment explored the use of 5 simple models for text classification, as a way to gain familiarity with the tools to be used in this course.

We implemented one count-based model, the Multinomial Naive Bayes (MNB), and four learned models, Logistic Regression (LR), Continuous Bag-of-Words (CBOW), Convolutional Neural Network (CNN) and a variant of the CNN initialized with pretrained word embeddings (CNN*). As shown in Table \ref{tab:results}, MNB performed best in our set of experiments, with a test-set accuracy of 0.82. Trained for 20 epochs with a learning rate of $5e^{-2}$, the four learned models (LR, CBOW, CNN, CNN*) did not perform as well as the MNB standard, but the relative accuracies achieved by these models highlight the differences between them. Notably, CNN* significantly outperformed CNN, as expected.

Further work might investigate additional hyperparameter tuning schemes to refine the learned models, for example by using learning rate annealing techniques. Further work should also pursue the use of novel representations of words and sentences as inputs to these models, for instance larger pretrained word embeddings derived from different sources (e.g. BERT), the use of parse trees and other semantic information, or even simply additional hand-tuned features, such as bigram and trigram frequencies.

\bibliographystyle{apalike}
\bibliography{writeup}

\end{document}


%%==============================
%% Section 4.01.01: DE $\implies$ IE (Temporary Title)
%%==============================

\documentclass[../dissertation.tex]{subfiles}

\begin{document}
\subsection{Jost Solutions Solve the Integral Equations}\label{subsec4:DEtoIE}

In this subsection we prove that Jost solutions also solve the
corresponding integral equation, as respectively
defined in Definitions \ref{dfn4:DEsoln} and \ref{dfn4:IEsoln}. We do
so in two phases: first for $\lambda \ne 0$ (Lemma \ref{lma4:DEtoIE1}), and then
for $\lambda = 0$ (Lemma \ref{lma4:DEtoIE2}).

\begin{lma}\label{lma4:DEtoIE1}
	A Jost solution $M$ satisfying Definition \ref{dfn4:DEsoln} also solves the
	associated integral equation \ref{eq4:MPluseq} in the sense of Definition
	\ref{dfn4:IEsoln} whenever $\lambda \in \mathbb R \sm \{0\}$.
\end{lma}
\begin{proof}
	Proposition \ref{prop3:BndryRelProp} implies
	$\wh{M^-} = e^{-2\xi} \wh{M^+}$. Moreover, $\wh{M^+}$ is a tempered
	distribution by the hypothesis of Definition \ref{dfn4:DEsoln} which means
	that $e^{2\xi} \wh{M^-}$ is also tempered. Taking the distribution Fourier
	transform of both sides of \eqref{eq4:LinSpecProb} we consequently find
	\begin{align}\label{eq3:DEtoIEsymbol}
		\wh{uM^+}
			&= \xi \wh{M^+} - \zeta\left( \wh{M^+} - \wh{M^-} \right) \\
			&= \left(\xi - \zeta(1-e^{-2\xi})\right) \wh{M^+} \nonumber \\
			&= p(\xi) \wh{M^+} \nonumber
	\end{align}
	in the sense of distributions in $\mathcal D'(\mathbb R)$. To avoid the
	zeros of the symbol $p$, we rewrite \eqref{eq3:DEtoIEsymbol} as
	\begin{align}\label{eq3:peps}
		p(\xi - i\varepsilon) \wh{M^+}
			= [p(\xi - i\varepsilon) - p(\xi)] \wh{M^+} + \wh{uM^+}
	\end{align}
	for $0< \varepsilon \ll 1$ and introduce the approximate Green's function
	\begin{align}\label{eq4:approxGF}
		G_L^\varepsilon(x; \lambda)
			= \frac{1}{2\pi}
				\int_{\mathbb R}
					e^{ix\xi} \frac{1}{p(\xi - i \varepsilon)}
				\,d\xi.
	\end{align}
	Using the contour shift
	$\mathbb R-i\varepsilon \mapsto \mathbb R + i \sign(x) \pi$
	in our work from Section \ref{sec1:GreensFunctions} to prove
	\eqref{eq1:GLrep} shows that
	\begin{align}\label{eq3:GLeps}
		G_L^\varepsilon(x; \lambda)
			=
				\begin{cases}
					K^+(x; \lambda)
						+ i
						\big[
							\alpha(\lambda)
							+ \beta(\lambda) e^{i\lambda x}
						\big] e^{-\varepsilon x} \, \chi_L(x)
						& \lambda \ne 0 \\
					K^+(x; \lambda)
						+ i
						\left[
							\frac{2}{3} + i x
						\right] e^{-\varepsilon x} \, \chi_L(x)
						& \lambda = 0
				\end{cases}
	\end{align}
	where $K^+$ is as defined in Theorem \ref{thm1:GFRep}
	\[
		K^+(x)
			= \frac{e^{-\pi|x|}}{2\pi}
				\int_{\mathbb R}
					e^{ix\xi}
					\frac{1}{p(\xi)+i \sign(x) \pi}
				\, \mathrm{d}\xi.
	\]
	An immediate consequence of \eqref{eq1:GLrep} and \eqref{eq3:GLeps} is that
	\begin{align*}
		G_L^+(x; \lambda) - G_L^\varepsilon(x; \lambda)
			&= i
				\Big[
					\alpha(\lambda)
					+ \beta(\lambda) e^{ix\lambda}
				\Big]
				\big(1-e^{-\varepsilon x}\big)
				\chi_{(0, \infty)}.
	\end{align*}
	Hence, we see from \eqref{eq2:polesumbnd} that
	\begin{align}\label{eq3:GLminusGeps}
		\big| \left(G_L^+ - G_L^\varepsilon\right)*f(x) \big|
			\lesssim \int_{-\infty}^x
					\left[ 1 -e^{-\varepsilon(x - x')}  \right] |f(x')|
				\, \mathrm{d}x'.
	\end{align}
	for real $\lambda\ne0$.

	The distribution identity $\wh 1 = 2\pi\delta_0$ (where $\delta_0$ denotes a
	Dirac delta-function centered at $\xi=0$) allows us ``subtract 1'' from both
	sides of \eqref{eq3:peps} to obtain
	\begin{align*}
		&p(\xi -i \varepsilon)(\wh{M^+-1})(\xi) + 2\pi \, p(\xi-i\varepsilon)\,\delta_0 \\
		&\qquad\qquad= [p(\xi-i\varepsilon) - p(\xi)]
						(\wh{M^+-1})(\xi)
						+ 2\pi\,p(\xi-i\varepsilon)\,\delta_0 + \wh{uM^+}
	\end{align*}
	Dividing both sides by $p(\xi-i\xi)$\textemdash{}since it has no zeros
	for $\xi\in \mathbb R$\textemdash{}we have
	\begin{align}\label{eq3:Mfouriered}
		(\wh{M^+ -1})(\xi)
			= \frac{p(\xi-i\varepsilon) - p(\xi)}{p(\xi - i\varepsilon)}(\wh{M^+-1})(\xi)
				+ \frac{1}{p(\xi - i\varepsilon)} \wh{uM^+}.
	\end{align}
	We therefore see from \eqref{eq3:Mfouriered} that in order to verify that $M^+$ satisfies
	definition \ref{dfn4:IEsoln}, it suffices to prove that the following two limits
	\begin{subequations}
		\label{eq3:suff}
		\begin{align}
			\label{eq3:suff1}
			\lim_{\varepsilon \searrow 0} \mathcal F^{-1}
				 	\left[\frac{1}{p(\xi - i\varepsilon)} \wh{uM^+}\right](x)
				 &= G_L^+*(uM)(x)
		\end{align}
		and
		\begin{align}
			\label{eq3:suff2}
				\lim_{\varepsilon \searrow 0} \mathcal F^{-1}
						\left[
							\frac{p(\xi-i\varepsilon) - p(\xi)}{p(\xi-i\varepsilon)}
							(\wh{M^+-1})(\xi)
						\right]
						(x)
					&= 0
		\end{align}
	\end{subequations}
	hold for \textit{a.e.} $x$.

	Since $uM^+ \in L^1(\mathbb R)$, \eqref{eq3:suff1} follows from estimate
	\eqref{eq3:GLminusGeps} and the Dominated Convergence Theorem.

	To verify \eqref{eq3:suff2}, first note that
	\begin{align*}
		\frac{p(\xi - i\varepsilon) - p(\xi)}{p(\xi - i \varepsilon)}
			= \frac{-i \varepsilon + \zeta e^{-2\xi}\left(1-e^{2i\varepsilon}\right)}
					{p(\xi-i\varepsilon)},
	\end{align*}
	which means it suffices to prove
	\begin{subequations}
		\label{eq3:secondlims}
		\begin{align} \label{eq3:secondlims1}
			\lim_{\varepsilon \searrow 0}
				\varepsilon \mathcal F^{-1}
					\left[
						\frac{1}{p(\xi-i\varepsilon)} (\wh{M^+-1})(\xi)
					\right](x)
			= 0
		\end{align}
		and
		\begin{align} \label{eq3:secondlims2}
			\lim_{\varepsilon \searrow 0}
				(1-e^{2i\varepsilon}) \mathcal F^{-1}
					\left[
						\frac{e^{-2\xi}}{p(\xi-i\varepsilon)} (\wh{M^+-1})(\xi)
					\right](x)
			= 0.
		\end{align}
	\end{subequations}

	From the definition of $G_L^\varepsilon$ and \eqref{eq3:GLeps} we see that
	\begin{align*}
		\mathcal F^{-1}
				\left[
					\frac{1}{p(\xi-i\varepsilon)} (\wh{M^+-1})(\xi)
				\right](x)
			&= G_L^\varepsilon * (M^+-1)
	\end{align*}
	and
	\begin{align*}
		G_L^\varepsilon * (M^+-1)(x)
			&= i \alpha(\lambda) \,
				\int_{-\infty}^x
					e^{-\varepsilon(x - x')} \big(M^+(x')-1\big)
				\, \mathrm{d}x' \\
			&\quad + i \beta(\lambda) \,
					\int_{-\infty}^x
						e^{i\lambda (x-x')} e^{-\varepsilon(x-x')}\big(M^+(x')-1\big)
					\, \mathrm{d}x' \\
			&\quad + \left(
						\int_{-\infty}^x K^+(x- x') + \int_{x}^\infty K^+(x - x')
					\right)
					\big(M^+(x')-1\big) \, \mathrm{d}x'
	\end{align*}
	Thus, since $e^{i\lambda (x-x')}$ is a unitary phase (\textit{i.e.} has complex
	modulus 1), in order to verify \eqref{eq3:secondlims1}, we need to show that
	\begin{subequations}
		\label{eq3:thirdlims}
		\begin{align}
			\label{eq3:thirdlims1}
			\lim_{\varepsilon\searrow 0} \varepsilon
				\int_{-\infty}^x
					e^{-\varepsilon(x-x')} |M^+(x') - 1|
				\, \mathrm{d}x' = 0
		\end{align}
		and
		\begin{align}
			\label{eq3:thirdlims2}
			\lim_{\varepsilon\searrow 0} \varepsilon
					\int_{\mathbb R} K^+(x- x') \big(M^+(x')-1\big) \, \mathrm{d}x'
				= 0.
		\end{align}
	\end{subequations}
	To prove \eqref{eq3:thirdlims1}, we choose an arbitrary $\varepsilon'>0$,
	split the integral $\int_{-\infty}^x$ into $\int_{-\infty}^{x-L} + \int_{x-L}^x$,
	and use the fact that $M^+(x) \to 0$ as $x \to -\infty$ to choose $L>0$
	sufficiently large that $|M^+(x')  - 1| < \varepsilon'/2$ for $x' < x-L$. Then,
	since
	\[
		\int_{-\infty}^{x-L} e^{-\varepsilon (x-x')} \,dx'
			= \int_{-\infty}^{-L} e^{\varepsilon t} \, \mathrm{d}t
			< \int_{-\infty}^0 e^{\varepsilon t} \, \mathrm{d}t
			= \frac{1}{\varepsilon},
	\]
	where $t = x'-x$, we have
	\begin{align} \label{eq3:DEtoIEthirdlim1v1}
		\varepsilon \int_{-\infty}^{x-L} e^{-\varepsilon(x-x')} |M(x') - 1| \,dx'
			< \frac{\varepsilon'}{2} \, \varepsilon \, \int_{-\infty}^{x-L} e^{-\varepsilon(x-x')} \, \mathrm{d}x'
			< \frac{\varepsilon'}{2}.
	\end{align}
	Now, since $M^+$ is continuous, $M^+-1$ is bounded by $\varepsilon'/2$ on $(\infty, x-L)$,
	and $[x-L, x]$ is compact, there exists a constant $C_x > 0$ depending only on $x$
	so that $\sup_{x'\leq x-L}|M^+(x')-1| \leq C_x$.
	Set $\delta':= \frac{\varepsilon'}{2 L C_x}$.
	For all $\varepsilon < \delta'$, we have
	\begin{align} \label{eq3:DEtoIEthirdlim1v2}
		\varepsilon \int_{x-L}^{x} e^{-\varepsilon(x-x')} |M(x') - 1| \,dx'
			\leq \varepsilon \, C_x \int_{-L}^0 e^{\varepsilon x'} \, \mathrm{d}x'
			< \frac{\varepsilon'}{2},
	\end{align}
	as $e^{\varepsilon x'} \leq 1$ for $x' \leq 0$ implies
	$\int_{-L}^0 e^{\varepsilon x'} \, \mathrm{d}x'\leq L$. Limit \eqref{eq3:thirdlims1} follows
	from \eqref{eq3:DEtoIEthirdlim1v1} and \eqref{eq3:DEtoIEthirdlim1v2}.

	Since we proved that $K^+\in \mathcal S(\mathbb R)$ in Section \ref{sec1:AsympK}
	and therefore in $L^1(\mathbb R)$, limit \eqref{eq3:secondlims2} is an
	immediate consequence of Definition \ref{dfn4:DEsoln}(ii) which states
	that $M^+ \in L^\infty(\mathbb R)$.

	Lastly, to complete the proof that $M^+$ satisfies Definition \ref{dfn4:IEsoln},
	we now verify limit \eqref{eq3:secondlims2}. To do so, observe that
	it suffices by the Taylor expansion of $1-e^{2i\varepsilon}$
	to verify the (slightly) simpler limit
	\begin{align}\label{eq3:simpler}
	 	\lim_{\varepsilon \searrow 0}
				\varepsilon \mathcal F^{-1}
					\left[
						\frac{e^{-2\xi}}{p(\xi-i\varepsilon)} (\wh{M^+-1})(\xi)
					\right](x)
			= 0,
	\end{align}

	We use Proposition
	\ref{prop3:BndryRelProp} and Definition \ref{dfn4:DEsoln}(v) to rewrite
	\eqref{eq3:simpler} as
	\begin{align}
		% &\lim_{\varepsilon \searrow 0}
		% 		\varepsilon \, \mathcal F^{-1}
		% 			\left[
		% 				\frac{e^{-2\xi}}{p(\xi-i\varepsilon)} (\wh{M^+-1})(\xi)
		% 			\right](x) \\
		% &\qquad\qquad= \lim_{\varepsilon \searrow 0}
		&\lim_{\varepsilon \searrow 0}
				\varepsilon \, \mathcal F^{-1}
					\left[
						\frac{1}{p(\xi-i\varepsilon)} (\wh{M^--1})(\xi)
					\right](x)
					\\
		&\qquad\qquad= \lim_{\varepsilon \searrow 0}
				\varepsilon \, \mathcal F^{-1}
					\left[
						\frac{1}{p(\xi-i\varepsilon)} (\wh{M_c^--1})(\xi)
					\right](x)
				\nonumber \\
		&\qquad\qquad\quad+ \lim_{\varepsilon \searrow 0}
				\varepsilon \, \mathcal F^{-1}
					\left[
						\frac{1}{p(\xi-i\varepsilon)} (\wh{M_s^-})(\xi)
					\right](x)
				\nonumber
	\end{align}
	An analogous argument to the one employed to verify \eqref{eq3:secondlims1}
	shows
	\[
		\lim_{\varepsilon \searrow 0}
				\varepsilon \, \mathcal F^{-1}
					\left[
						\frac{1}{p(\xi-i\varepsilon)} (\wh{M_c^--1})(\xi)
					\right](x)
			= 0.
	\]
	%%========================================
	%% Begin thievery from Prof. Perry's notes
	%% Rewrite this section later.
	%%========================================
	To analyze the second right-hand term, we again appeal to the representation
	\eqref{eq3:GLeps}. The ``pole terms'' in \eqref{eq3:GLeps} give
	two terms which can be estimated by
	\[
		\varepsilon \int_{-\infty}^x e^{-\varepsilon(x-x')} |M_s^-(x')| \, \mathrm{d}x'
	\]
	which is $\mathcal O\left(\varepsilon^{1/2}\right)$ by the Schwartz inequality.
	To control the integrals involving $K^\pm$, we again use the $L^2$
	bound on $M_s^-$ to show that the integrals
	\[
		\int_{\mathbb R} |K_\pm(x-x')| |M_s^-(x')| \, \mathrm{d}x
	\]
	converge, and hence the corresponding terms are
	$\mathcal O\left(\varepsilon\right)$.
\end{proof}

We now finish our proof that Jost solutions solve the associated integral
equation by considering the case where $\lambda = 0$.

\begin{lma}\label{lma4:DEtoIE2}
	Let $\lambda =0$ and suppose $M$ is a Jost solution in accordance
	with Definition \ref{dfn4:DEsoln}. Then $M$ is a solution for
	\ref{eq4:MPluseq} as specified in Definition \ref{dfn4:IEsoln}.
\end{lma}
\begin{proof}
	As in the proof of Lemma \ref{lma4:DEtoIE1}, we begin with the distribution
	identity $p(\xi;\lambda) \widehat{M^+} = \widehat{u\,M^+}$, which may be rewritten as
	\[
		p(\xi,\lambda) \widehat{M^+-1} = \widehat{u\,M^+},
	\]
	since $p(0;\lambda)=0$. Mimicking our proof of Lemma \ref{lma4:DEtoIE1}, we write
	\[
		p(\xi-i\varepsilon) \widehat{M^+-1}
			= \left[ p(\xi-i\varepsilon) - p(\xi) \right] \widehat{M^+-1}
				+ \widehat{u\,M^+},
	\]
	where here and in what follows we write $p(\xi)$ for $p(\xi; \lambda = 0)$
	since $\lambda = 0$ is fixed throughout. Dividing we get
	\begin{equation}
		\label{Jost.half.pre-int}
		\widehat{M^+-1}
			= \frac{p(\xi-i\varepsilon) - p(\xi)}{p(\xi-i\varepsilon)}
					\widehat{M^+-1}
				+ \frac{\widehat{u\,M^+}}{p(\xi-i\varepsilon)}.
	\end{equation}
	We wish to show that, on taking inverse Fourier transforms and taking
	$\varepsilon \searrow 0$, we obtain
	\[
		M^+(x)-1 = G_L*(u\,M^+).
	\]
	Recalling the definition the approximate Green's function
	\[
		G_L^\varepsilon(x)
			= \frac{1}{2\pi} \int \frac{e^{ix\xi}}{p(\xi-i\varepsilon)} \, \mathrm{d}\xi
	\]
	from Equation \ref{eq4:approxGF} in the proof of Lemma \ref{lma4:DEtoIE1},
	we note that
	\begin{equation}
		\label{GL.eps}
		 G_L^\varepsilon(x)
		 	=
		 		i \left(\frac{2}{3} + i x \right)
		 		e^{-\varepsilon x} \chi_L(x)
		 		+ e^{-\pi|x|} k(x),
	\end{equation}
	where $k(x)$ is as defined in Remark \ref{rmk1:littlek}.

	Thus the inverse Fourier transform of the second right-hand term in
	\eqref{Jost.half.pre-int} is given by
	\begin{align*}
	\mathcal F^{-1} \left( \frac{\widehat{uM}}{p(\xi-i\varepsilon)} \right)(x)
		&=
			i
			\int_{-\infty}^x
				\left( \frac{2}{3} + i (x-x') \right)
				e^{-\varepsilon(x-x')} u(x') M(x') \, \mathrm{d}x' \\
		&\quad + \int_{\mathbb R} e^{-\pi|x-x'|} k(x-x')  u(x') M(x') \, \mathrm{d}x'.
	\end{align*}
	It follows by dominated convergence that this expression approaches
	$G_L*(uM)(x)$ pointwise as $\varepsilon \searrow 0$ as
	$u\in L^{2,4}(\mathbb R)$ implies
	$u \in L^{1,1}(\mathbb R) \cap L^2(\mathbb R)$.

	It remains to show that the first term vanishes pointwise as
	$\varepsilon \searrow 0$. We write
	\begin{align*}
		\mathcal F^{-1}
				\left(
					\frac{p(\xi-i\varepsilon) - p(\xi)}
						{p(\xi-i\varepsilon)} \widehat{M^+-1}
				\right)
			&=	i\varepsilon G_L^\varepsilon * (M^+ -1) \\
			&\quad-
				\frac{1}{2} \left(e^{2i\varepsilon}-1 \right)
				G_L^\varepsilon*(M^- - 1)
	\end{align*}
	where we used $\widehat{M^-} = e^{-2\xi} \widehat{M^+}$. The goal is to use
	the asymptotic behavior of $M^+$ and $M^-$ as $x \to -\infty$ to show that
	these terms vanish as $\varepsilon \searrow 0$. Due to the linear growth
	of the Green's function we need a more stringent rate of decay for
	$M^+-1$ and $M^- - 1$ as $x \to -\infty$ to control convolution with the pole
	term in $G_L^\varepsilon$.

	First we consider
	\begin{align*}
		i\varepsilon \, G_L^\varepsilon*(M^+ - 1) (x)
			&=
				-\varepsilon
				\int_{-\infty}^x
					\left( \frac{2}{3} + i(x-x') \right)
					e^{-\varepsilon x} \left(M^+(x') - 1\right)
				\, \mathrm{d}x' \\
			&\quad
				+ i\varepsilon \int e^{-\pi|x-x'|} k(x-x') \big(M^+(x')-1\big) \, \mathrm{d}x'
	\end{align*}
	We use equation \eqref{GL.eps} and asymptotic condition (a) from
	property \ref{itm:asymp} of Definition \ref{dfn4:DEsoln}.
	The second right-hand integral is bounded by $\varepsilon$ times
	\[
		\int_{\mathbb R} \inn{x-x'}^{-2} |k(x-x')| \inn{x'} \, \mathrm{d}x'
			\lesssim
				\inn{x}
				\int_{\mathbb R}
					\inn{x-x'}^{-1} |k(x-x')| \,dx'
			\lesssim \inn{x} \nm{k}_{L^2}
	\]
	and so goes to zero pointwise as $\varepsilon \searrow 0$.
	Let $H(x) = \inn{x}\big(M^+(x)-1\big)$. The first right-hand integral is
	bounded by
	\begin{align*}
	\varepsilon
		\int_{-\infty}^x
			\inn{x-x'} e^{-\varepsilon (x-x')} \inn{x'}^{-1}
			\left|H(x') \right|
		\, \mathrm{d}x'
		&\lesssim
			\varepsilon
			\int_{-\infty}^x
				e^{-\varepsilon (x-x')} \left| H(x') \right|
			\, \mathrm{d}x'
			\\
		&=	\int_0^{\infty} e^{-\Xi} \left| H(x-\Xi/\varepsilon) \right| \, \mathrm{d}\Xi
	\end{align*}
	which goes to $0$ as $\varepsilon \searrow 0$ by dominated convergence since
	$\lim_{x \to -\infty} H(x) = 0$, where we used the substitution
	$\Xi = \varepsilon(x-x')$ in the above integral.

	We seek to carry out an analogous estimate for the term involving $M^-$.
	We will use equation \eqref{GL.eps} and asymptotic condition (b) from
	property \ref{itm:asymp} of Definition \ref{dfn4:DEsoln}. Since
	$e^{2i\varepsilon}-1$ is of order $\varepsilon$, it suffices to show that
	$\varepsilon \left| G_L^\varepsilon*(M^- -1) (x) \right| = o(1)$
	as $\varepsilon \searrow 0$, where we use the ``little oh'' notation
	$f = o(g)$ to indicate that $\lim{y\to a} \frac{f(y)}{g(y)} = 0$ (in this
	case $y=\varepsilon$ and $a = 0$).
	We have
	\begin{align*}
		\varepsilon \left| (G_L^\varepsilon * (M_1))(x) \right|
			&\lesssim
				\varepsilon
				\int_{-\infty}^x
					\inn{x-x'} e^{-\varepsilon x'} \inn{x'}^{-1-\upsilon}
				\, \mathrm{d}x'
				\\
			&\lesssim
				\int_{-\infty}^x
					\varepsilon e^{-\varepsilon(x-x')} \inn{x'}^{-\upsilon}
				\, \mathrm{d}x'
				\\
			&=
				\int_0{\infty}
					e^{-\Xi} \inn{x-\Xi/\varepsilon}^{-\upsilon}
				\, \mathrm{d}\Xi
	\end{align*}
	which goes to zero as $\varepsilon \searrow 0$ by dominated convergence.
	We leave the second term, involving $k(x-x')$, as an exercise to the
	reader.

	Finally
	\begin{align*}
		\varepsilon \left| (G_L^\varepsilon (M_2))(x) \right|
			&\lesssim
				\varepsilon
				\int_{-\infty}^x
					e^{-\varepsilon (x-x')} \inn{x-x'}
					\inn{x'}^{-1-\upsilon} g(x')
				\, \mathrm{d}x',
		\end{align*}
	where $g \in L^2$. By the Cauchy-Schwarz inequality and the
	fact that $\inn{x-x'} \inn{x'}^{-1}$ is bounded for $x' < x < 0$ we again
	get an $\varepsilon^{\frac{1}{2}}$ estimate which suffices for the purpose.

	We conclude that, for $u \in L^{1,2+\upsilon} \cap L^{2,2} \supset X$,
	the Jost solution $M$ the satisfying asymptotic conditions \ref{itm:asymp}
	from Definition \ref{dfn4:DEsoln} solves the corresponding integral equation.
\end{proof}

\end{document}


\documentclass[notitlepage, 10pt]{article}
\title{Newton's Method as a Formal Recurrance}
%\title{Newton's Method Applied Symbolically to Quadratics}
%\title{Newton's Recurrence}
\author{{\sc
	Carl Edquist,
	Sam Lachterman,
	Brendan Younger,
	Hal Canary} \\
{\small University of Wisconsin--Madison}}
%\date{April 28, 2004}
\usepackage{amssymb, amsthm, amsmath, amscd}
\usepackage{fullpage}
\usepackage{hyperref}
\usepackage{graphicx}
\hypersetup{pdfstartview=FitH,pdfauthor={SSL},
pdftitle={Newton's Method Applied Symbolically to Quadratics}}

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}
\newtheorem{conjecture}{Conjecture}

\newcommand{\R}{\mathbb{R}}
\newcommand{\Cstar}{\widetilde{\mathbb{C}}}
\newcommand{\onto}{\rightarrow}
\newcommand{\binomial}[2]{\genfrac{(}{)}{0pt}{}{ #1 }{ #2 }}
\newcommand{\qbinomial}[2]{\genfrac{[}{]}{0pt}{}{ #1 }{ #2 }_q }

\begin{document}

\bibliographystyle{plain} % Entries are ordered alphabetically;
%\bibliographystyle{unsrt} % Entries are not ordered alphabetically,
			  %  but in the order they are first referenced.
%\bibliographystyle{abbrv} % The bibliography looks the same as for
			  %  plain style except that first names and
			  %  names of journals and months are abbreviated;
%\bibliographystyle{alpha} % The bibliography looks the same as for
			  %  plain style except that the reference
			  %  markers are not just 1,2,3... but are
			  %  based on authors' initials and publication year;
\bibliographystyle{plain} % Entries are ordered alphabetically;

\maketitle

%% \begin{abstract}
%% Iterating Newton's method symbolically for the general quadratic
%% $ax^2+bx+c$ yields a rational function $\frac{P_n(x)}{Q_n(x)}$, the
%% numerator and denominator of which are polynomials with highly
%% composite coefficients. In particular, the coefficients have no prime
%% factors greater than $2^n$ after $n$ iterations.
%% \end{abstract}

\section*{Non-commuting Algebra}

We have defined $P_n$ and $Q_n$ with $P_0(x)=x$ and $Q_0(x)=1$.  If we
instead let $P_0(x)=x$ and $Q_0(x)=y$, it can be verified that we get
slightly different formula:

\[
P_n(x,y) = a^{2^n-1}x^{2^n}~+~\sum\limits_{k=0}^{(2^n-2)}
\sum\limits_{i=1}^{~(2^n-k-1)~}
(-1)^{i} \binomial{2^n }{ k} \binomial{2^n-k-i-1}{i-1}
a^{k+i-1}~ b^{2^n-k-2i} ~c^i ~x^k y^{2^n-k}
\]
\[
Q_n(x,y) = \sum\limits_{k=0}^{(2^n-1)}
\sum\limits_{i=0}^{~(2^n-k-1)~} (-1)^i \binomial{2^n}{k}
\binomial{2^n-k-i-1}{i}
a^{k+i} ~b^{2^n-k-2i-1} ~c^i ~x^k y^{2^n-k}
\]

But suppose that $x$ and $y$ do not commute, but rather satisfy the
formula $yx=qxy$.  What happens?  If we define
\[ \qbinomial{n }{ k} = \prod_{i=1}^{n-k}
\frac{ 1-q^{i+k} }{ 1-q^i }\]
then the q-version of the binomial formula is: \cite{qbin}
\[
(x + y)^n = \sum_{k=0}^{n} \qbinomial{n }{ k} x^k y^{n-k}.
\]

\begin{conjecture}
If $yx=qxy$ and if $P_n(x,y)$ and $P_n(x,y)$ are defined by
\begin{eqnarray*}
& P_0     = x \qquad Q_0 = y \\
& P_{n+1} = a P_n^2 - c Q_n^2  \qquad
Q_{n+1} = a P_n Q_n + a Q_n P_n + b Q_n Q_n .
\end{eqnarray*}
then
\[
P_n(x,y) = a^{2^n-1}x^{2^n}~+~\sum\limits_{k=0}^{(2^n-2)}
\sum\limits_{i=1}^{~(2^n-k-1)~}  (-1)^{i}
\qbinomial{2^n }{ k} \binomial{2^n-k-i-1}{i-1}
a^{k+i-1}~ b^{2^n-k-2i} ~c^i ~x^k y^{2^n-k}
\]
\[
Q_n(x,y) = \sum\limits_{k=0}^{(2^n-1)}
\sum\limits_{i=0}^{~(2^n-k-1)~} (-1)^i
\qbinomial{2^n }{ k} \binomial{2^n-k-i-1}{i}
a^{k+i} ~b^{2^n-k-2i-1} ~c^i ~x^k y^{2^n-k}.
\]
\end{conjecture}

It is not clear to us that the proof we provided for Theorem
(\ref{thm:maintheorem}) is aplicable to this more general conjecture.

%% \begin{thebibliography}{9}
%% \bibitem{qbin}
%% {\sc M.~P.~Schutzenberger}.
%% ``Une interpretation de certaines solutions de l'equation
%% fonctionnelle: $F(x + y) = F(x)F(y)$.''
%% \emph{C.~R.~Acad.~Sci.~Paris}, 236 (1953), 352-353.
%% \end{thebibliography}

\bibliography{newton-with-a-vengance}
\end{document}


\documentclass{amsart}

\usepackage{amsmath, amsthm, amssymb, amsfonts}
% Notation for sheaves.
\usepackage{mathtools, mathrsfs}
% Identity à la Hatcher.
\usepackage{dsfont}
% Enumerate with letters.
\usepackage{enumerate}
\usepackage{hyperref}

\newtheorem{theorem}{Theorem}[section]
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{proposition}[theorem]{Proposition}

\theoremstyle{definition}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{example}[theorem]{Example}
\newtheorem{xca}[theorem]{Exercise}

\theoremstyle{remark}
\newtheorem{remark}[theorem]{Remark}

\numberwithin{equation}{section}

\setlength\parindent{0pt}

\begin{document}

\title{Number Field Sieves}

\author{Federico Bongiorno}

\date{24\textsuperscript{th} of August 2020}

\begin{abstract}
Algorithms, development, outcomes and improvements of the project undertaken with \texttt{Haskell.org} for the \texttt{arithmoi} library under supervision of Andrew Lelechenko.
\end{abstract}

\maketitle

\section*{Introduction}

Decomposing integers into prime factors has always been at the core of arithmetic. The ancient Greeks were probably the first ones to study numbers for their own sake and they developed algorithms to effectively deduce properties about
them. Among these are the sieve of Eratosthenes and the algorithm of Euclid. Despite these advances, the fastest algorithm to factor a given integer was trial division. In the XVII century, Pierre de Fermat described an alternative approach to factor integers, however the algorithm was still based on trial and error. During the last century, plenty of algorithms to factor integers arose. Among them
are the Elliptic Curve Factorisation, the Quadratic Sieve and the General Number
Field Sieve. Despite the fact that these algorithms require modern mathematical
techniques, the latter two are still based on Fermat’s idea and they are the fastest classical algorithms. Indeed, on the 28\textsuperscript{th} of February 2020 , the General Number Field Sieve was used to factor the largest number to date: a $250$-digit integer. On the other hand, only the Elliptic Curve Factorisation algorithm was implemented in Haskell. As an initial step, this project developed the Quadratic Sieve in Haskell.

\section{Algorithms}

\subsection*{Quadratic Sieve}

Suppose $n$ is an integer. The quadratic sieve attempts to factor $n$ by finding two integers $x$ and $y$ such that $$x ^ 2 - y ^ 2 = n$$ Then $(x - y)(x + y) = n$ is, in at least a half of the cases, a non-trivial factorisation of $n$.

\begin{example}
Let $n = 21$. Note that $$ 5 ^ 2 - 2 ^ 2 = 21$$ so that $$ 7 \cdot 3 = (5 + 2)\cdot (5 - 2) = 21.$$
\end{example}

To find such integers we compute $y_i = x_i ^ 2 - n$ as $x_i$ runs through the sieving interval and we look for numbers which decompose completely into prime factors, which are less than a fixed bound. These are called smooth numbers. If we can find enough smooth numbers, then we are guaranteed to find a product of $y_i$'s which is a square. Indeed, this problem reduces to a linear system of equations over $\mathbb{F}_2$, the finite field with two elements. The factorisation of a smooth number will form the column of a matrix after reduction modulo $2$. Finally by taking a suitable product of the $x_i$ 's and $y_i$'s, the end goal is achieved. \\

The version developed here presents three improvements. Firstly, the sieve uses the multiple polynomials Montgomery method. Instead of mapping $x$ using the polynomial $x^2 - n$, we can use $f (x) = ax^2 + 2bx + c$ where $a$, $b$ and $c$ are integers such that $b ^ 2 - ac = n$. Then, by completing the square, we get $$(ax + b) ^ 2 - n =  af(x)$$ which reduces the computations to the standard case by replacing $ax +b$ with $x$ and $a f(x)$ with $y$. The advantage of this method is that the numbers generated by $f$ can be made small, and so more likely to be smooth. Another improvement is to sieve with approximate logarithms. Instead of dividing by a prime, we subtract the approximate logarithm of that prime. This is faster than division. A number is then checked for being smooth if, after sieving, the corresponding logged value falls below a given threshold. A third improvement is given by considering a number to be smooth if, after dividing by all the primes in the factor base, the number remaining is itself prime. This may help to find more smooth numbers.

\subsection*{Wiedemann Algorithm}

Given a singular square matrix $A$ with coefficients in $\mathbb{F}_2$, the aim of this algorithm is to find a non-zero $v$ such that $Av = 0$.
The idea of the algorithm is to estimate the minimal polynomial of $A$ and then use it to infer a solution. This can be achieved as follows. Suppose $p(x)$ is the minimal polynomial of $A$. Because $A$ is singular, $$p(x) = x^k q(x) $$
for some $q(x)$ and some $k \geq 1$. Hence $$ A^k q(A) = p(A) = 0$$ gives a non-trivial solution. To estimate $p(x)$, we pick random vectors $u$ and $w$ and compute $$u^T A ^ j w$$ as $j$ runs in a fixed interval. Using this data, a variation of Euclid's algorithm for polynomials (Berlekamp-Massey algorithm) gives an estimate of $p(x)$.

\section{Development}

\subsection*{Weeks 1 - 4}
In the first four weeks, I developed a working version of the quadratic sieve employing Gaussian elimination in the linear algebra stage. The sieve was not dividing by higher prime powers and, as a result, it needed a much larger factor base. The algorithm was tested and benchmarked against $n_{30}$ (30-digit integer, see \S \ref{sec:outcomes}). It took \texttt{600 s} to factor and used almost \texttt{2 GB} of memory. Most of the time was consumed in the linear algebra step. \\

\emph{Difficulties}. The main difficulty came from using Haskell's mutable vectors. It was also difficult to mix mutable and immutable types while writing the code for the sieve.\\

\emph{What I learnt}. I learnt to write code in Haskell using mutable vectors and many engineering tools. These include Git, GitHub and profiling tools. I also developed a better understanding of Haskell's laziness.\\

\emph{Code}. \url{https://github.com/Bodigrim/arithmoi/pull/202}

\subsection*{Weeks 5 - 8}
In the second four weeks, I developed the linear algebra routine to solve sparse binary matrices. The quadratic sieve improved considerably, taking around \texttt{60 s} to factor $n_{30}$. This algorithm also uses a fraction of the memory used in Gaussian elimination. \\

\emph{Difficulties}. I had problems using random numbers. Because of laziness, if used naively, random numbers may produce the same output throughout the program. It was difficult to use type parameters to implement vectors of given length. GHC expected to know at each step if the parameter was fixed or could vary. I hadn't observed this syntax in any other programming language. Linking the two algorithms together was also not straightforward since rows and columns had to be indexed in the same fashion. \\

\emph{What I learnt}. I was not aware of this linear algebra algorithm or, more in general, of the counterintuitive idea to estimate minimal polynomials to solve linear systems. I also learnt about sized vectors and how to operate with generalised abstract data types. I had a better understanding of data structures and when to use which.\\

\emph{Code}. \url{https://github.com/Bodigrim/arithmoi/pull/208}

\subsection*{Week 9}
I then spent the week after, implementing the multiple polynomial variant. This is an ingenious way to find more smooth numbers in a given interval. This was successful and it cut time and memory significantly. It was taking around \texttt{15 s} and \texttt{300 MB}. \\

\emph{Difficulties}. Choosing $a$, the leading coefficient of the polynomial is quite easy. Choosing how $a$ varies as multiple sieving blocks are run is much harder. It is desirable to make sure that $a$ is not too far from a fixed values but it has to change at every block. I suspect that the best way to change $a$ is by using random numbers. \\

\emph{What I learnt}. I learnt about Haskell's applicative functors and monads while implementing this variant.

\subsection*{Week 10}
At this point I realised it was necessary to sieve by higher prime powers. I only changed a few lines of code and it instantly improved the algorithm. It was then taking \texttt{6 s}. I also sieved using approximate logarithms. This approach did not require dividing by higher prime powers as the sieving was approximate. Instead, trial division is used on numbers which fall below a certain threshold after the sieve. Its performance was even better than the previous one taking only \texttt{1 s}. \\

\emph{Difficulties}. Log sieving was actually slower at first. The logarithm of an integer was computed as a double and then floored. Taking logarithm to double precision was the problem and this was solved by calling a function previously developed by Andrew.\\

\emph{What I learnt}. A further speed up was obtained by using an unboxed vector for sieving rather than a boxed one. I learnt about the difference between these two types.\\

\emph{Code}. \url{https://github.com/Bodigrim/arithmoi/pull/210}

\subsection*{Week 11}
I then developed the large prime variation, a technique allowing numbers to be considered smooth even if they have one prime factor outside of the factor base. This variant further improved performance. Factoring $n_{30}$ took \texttt{0.6 s} and \texttt{1 MB} of memory. \\

\emph{Difficulties}. At first this variant was very inefficient as I was only picking up a couple of more smooth numbers while investing many resources in finding them. This was due to misunderstanding of the algorithm which was pointed out to me by Andrew. \\

\emph{What I learnt}. I became familiar with using maps and better understood how they work.

\subsection*{Week 12}
In the last week, I polished and tested the code. I also wrote the documentation and the report.\\

\emph{Difficulties}. When testing extensively, I realised there were a few bugs that I hadn't noticed before. It took a surprising amount of time to find them.\\

\emph{What I learnt}. I learnt better testing practices. These includes testing for edge cases and writing tests covering different facets of the algorithms. For instance, after developing the linear algebra routine, I only tested for correctness of the solution. However, in the context of integer factorisation, it is also important for the algorithm to find distinct solutions. This aspect was left untested and the uncovering the related inefficiency later required several hours of laborious debugging. \\

\emph{Code}. \url{https://github.com/Bodigrim/arithmoi/pull/211}

\section{Outcomes}
\label{sec:outcomes}

The outcomes of the project are working versions of the quadratic sieve algorithm to factor an integer and the Wiedemann algorithm to solve sparse binary matrices. Here below are the main files in their final versions:

\begin{itemize}
\item \url{https://github.com/Bodigrim/arithmoi/blob/master/Math/NumberTheory/Primes/Factorisation/QuadraticSieve.hs}
\item \url{https://github.com/Bodigrim/arithmoi/blob/master/Math/NumberTheory/Primes/Factorisation/LinearAlgebra.hs}
\item \url{https://github.com/Bodigrim/arithmoi/blob/master/test-suite/Math/NumberTheory/Primes/QuadraticSieveTests.hs}
\item \url{https://github.com/Bodigrim/arithmoi/blob/master/test-suite/Math/NumberTheory/Primes/LinearAlgebraTests.hs}
\end{itemize}

Here are some performance examples on my machine:\\

\textbf{Factoring a 30-digit number $n_{30}$}\\
Time: \texttt{0.599 s} \\
Memory: \texttt{1 MB} \\*

\textbf{Factoring a 40-digit number $n_{40}$}\\
Time: \texttt{14.213 s} \\
Memory: \texttt{14 MB} \\*

\textbf{Factoring a 50-digit number $n_{50}$}\\
Time: \texttt{216.649 s} \\
Memory: \texttt{82 MB} \\*

\textbf{Factoring a 60-digit number $n_{60}$}\\
Time: \texttt{1746.459 s} \\
Memory: \texttt{427 MB} \\*

\textbf{Solving a matrix of size 1000 and density 0.005}\\
Time: \texttt{1.140 s} \\
Memory: \texttt{0 MB} \\*

\textbf{Solving a matrix of size 10000 and density 0.001}\\
Time: \texttt{99.484 s} \\
Memory: \texttt{6 MB} \\*

\textbf{Solving a matrix of size 20000 and density 0.0005}\\
Time: \texttt{485.538 s} \\
Memory: \texttt{61 MB} \\*

\section{Improvements}

Future improvements are needed, particularly in the linear algebra routine. First of all, experimental evidence suggests that when the matrix is very singular, the solutions output by the algorithm are often the same. This can be problematic. Indeed, given a solution of the matrix, one can infer a factorisation in only a half of the cases. It is desirable to make the solution vary as the initial random seed varies. Another improvement to be carried out is the block variant of the Wiedemann. This improvement may speed up the classical Wiedemann algorithm by up to 64 times.

\section{Conclusion}

This project started with the ambitious aim to code efficient versions of both the Quadratic and General Number Field Sieves. It then became clear that writing an efficient version of the Quadratic Sieve alone required much more work than initially thought. Indeed, the algorithm was quite slow at first and several performance improvements were required. Nonetheless, I am satisfied about the result achieved and the copious amount of knowledge assimilated during the course of the project. I am looking forward to further improve the algorithm in the coming months.

\end{document}