% No 'submit' option for the problems by themselves.
\documentclass{cos302}
% Use the 'submit' option when you submit your solutions.
%\documentclass[submit]{cos302}
% Put in your full name and email address.
\name{Your Name}
\email{email@princeton.edu}
\discussants{Marie Curie \\ Leonhard Euler}
% You don't need to change these.
\course{COS302-S21}
\assignment{Assignment \#1}
\duedate{6:00pm Friday 12 February 2021}
\dropboxurl{https://www.gradescope.com/courses/214271/assignments/981350}
\begin{document}
% IMPORTANT: Uncomment the \documentclass command above with the [submit] option and put your name where it says ``your name''.
\begin{problem}[10pts]
In COS 302 you'll typeset your assignments in \LaTeX, which is the way that almost all documents are composed in mathematical fields.
It's a good thing to learn and \LaTeX{} can produce beautiful documents.
The first part of this assignment is going to get you started with it.
There are a variety of ways to write and compile \LaTeX, from web-based tools like \href{https://www.overleaf.com/}{Overleaf}, to various GUI tools for Mac, Linux and Windows, to plain text editors like Emacs.
If you aren't an experienced user of \LaTeX, with tools already installed on your computer, we \textbf{strongly} recommend that you use \href{https://www.overleaf.com/}{Overleaf} for this course.
\href{https://www.overleaf.com/}{Overleaf} runs in your browser so you don't have to worry about Mac vs Linux vs Windows vs Chromebook.
To make this as easy as possible, we'll post links to templates of each homework to get you going.
Just use the ``copy project'' option in the menu to the left.
We have \href{https://youtu.be/kqLbTYdYcPc}{posted a screencast} to walk you through it.
There are two files in the project.
One of them, \texttt{cos302.cls} is a formatting (``class'') file that you should not have to worry about or edit.
The other, \texttt{hw1.tex}, is the file you'll actually edit.
When you typeset in \LaTeX, you're basically just writing markup code.
You then compile it and get a nice looking PDF file.
Note that this compilation process just produces a PDF; it does not submit it for grading automatically or anything like that.
To submit the PDF, you'll need to upload via \href{https://gradescope.com}{Gradescope}.
A \href{https://www.youtube.com/watch?v=KMPoby5g_nE}{screencast} is available to walk you through the process of submitting the homework.
To start out, look at the very top of the file where you see:
\begin{lstlisting}[style=latex]
% No 'submit' option for the problems by themselves.
\documentclass{cos302}
% Use the 'submit' option when you submit your solutions.
% \documentclass[submit]{cos302}
\end{lstlisting}
Lines that start with the ``\%'' symbol are comments.
\LaTeX{} commands start with backslashes and arguments are usually in curly braces~$\{\ldots\}$ with options in square brackets~$[\ldots]$.
The \texttt{\textbackslash documentclass} command starts the document and declares what kind of thing we're writing.
Here the document class is \texttt{cos302} because we're using a custom class for assignments; in scientific documents it would be \texttt{article} 90\% of the time.
In any case, here there is one possible option, \texttt{submit}.
You can turn that on by commenting out the first \texttt{\textbackslash documentclass} and uncommenting the second one that has the \texttt{[submit]} option active.
Do that and compile the document.
What happened?
Explain what happened by replacing the commented text below that looks like this:
\begin{lstlisting}[style=latex]
\end{problem}
% UNCOMMENT AND PUT PROBLEM ANSWER HERE.
\end{lstlisting}
\end{problem}
% UNCOMMENT AND PUT PROBLEM ANSWER HERE.
\newpage
\begin{problem}[10pts]
Now, hopefully you discovered in the previous problem that when the document compiled with the \texttt{[submit]} option, it put a name, email and the course info in the top left corner, and a ``discussants'' section in the top right.
You won't be surprised to hear that you should change these in the code above (the ``preamble'' is what this part before the \texttt{\textbackslash begin\{document\}} is called) to be your actual name and email address.
The ``discussants'' section is a place to list the people that you discussed the problems with (consistent with the collaboration policy, of course).
\begin{lstlisting}[style=latex]
% Put in your full name and email address.
\name{Your Name}
\email{email@princeton.edu}
\discussants{Marie Curie \\ Leonhard Euler}
\end{lstlisting}
The double backslash is a way to insert a line break if you want one.
You really do need to change this on each assignment and use the \texttt{[submit]} option, so that your name appears at the top.
(You get this problem right by putting your name and email in the document.)
\end{problem}
\newpage
\begin{problem}[10pts]
The reason mathematicians, computer scientists, and other technical folks like \LaTeX{} is because it typesets equations so nicely.
This problem will give you a little bit of practice with that.
Good written communication is important.
There are lots of tutorials online for typesetting mathematics with \LaTeX, but \href{https://en.wikibooks.org/wiki/LaTeX/Mathematics}{this} is a good starting point.
Use the \texttt{equation} (or equivalent) environment to typeset the following:
\begin{enumerate}[(A)]
\item \href{https://en.wikipedia.org/wiki/Euler\%27s_formula}{Euler's formula}
\item \href{https://en.wikipedia.org/wiki/Exponential_function}{the Taylor series of the exponential function}
\item \href{http://mathworld.wolfram.com/NormalEquation.html}{the normal equations} with matrix $\bm{A}$, $\bm{x}$, and $\bm{b}$.
\end{enumerate}
Don't worry if you don't know what these are yet.
Just find their forms on the links above and complete the equations below.
Soon you'll be on your way to being a \LaTeX{} typesetting pro.
\end{problem}
(A)
\begin{equation}
e^{ix} = ???
\end{equation}
(B)
\begin{equation}
e^x = ???
\end{equation}
(C)
\begin{equation}
???
\end{equation}
\newpage
\begin{problem}[18pts]
Now let's use \LaTeX{} to do some problems.
Multiply these matrices, if possible.
If not possible, say so.
\begin{enumerate}[(A)]
\item
\begin{equation*}
\begin{bmatrix}
1 & 2 \\
4 & 5 \\
7 & 8
\end{bmatrix}
\begin{bmatrix}
1 & 1 & 0\\
0 & 1 & 1\\
1 & 0 & 1
\end{bmatrix}
\end{equation*}
\item
\begin{equation*}
\begin{bmatrix}
1 & 2 & 3\\
4 & 5 & 6\\
7 & 8 & 9
\end{bmatrix}
\begin{bmatrix}
1 & 1 & 0\\
0 & 1 & 1\\
1 & 0 & 1
\end{bmatrix}
\end{equation*}
\item
\begin{equation*}
\begin{bmatrix}
1 & 1 & 0\\
0 & 1 & 1\\
1 & 0 & 1
\end{bmatrix}
\begin{bmatrix}
1 & 2 & 3\\
4 & 5 & 6\\
7 & 8 & 9
\end{bmatrix}
\end{equation*}
\item
\begin{equation*}
\begin{bmatrix}
1 & 2 & 1 & 2\\
4 & 1 & -1 & -4
\end{bmatrix}
\begin{bmatrix}
0 & 3\\
1 & -1\\
2 & 1\\
5 & 2
\end{bmatrix}
\end{equation*}
\item Did you notice anything interesting about (B) vs (C)? Do they have the same answer?
\end{enumerate}
\end{problem}
(A)
\begin{equation*} % Including the asterisk causes it to not include an equation number on the right.
\begin{bmatrix} % A nice environment for pretty matrices.
1 & 2 \\ % Ampersands split up the columns and the \\ go to the next row.
4 & 5 \\
7 & 8
\end{bmatrix}
\begin{bmatrix}
1 & 1 & 0\\
0 & 1 & 1\\
1 & 0 & 1
\end{bmatrix}
=
???
\end{equation*}
(B)
\begin{equation*}
\begin{bmatrix}
1 & 2 & 3\\
4 & 5 & 6\\
7 & 8 & 9
\end{bmatrix}
\begin{bmatrix}
1 & 1 & 0\\
0 & 1 & 1\\
1 & 0 & 1
\end{bmatrix}
=
???
\end{equation*}
(C)
\begin{equation*}
\begin{bmatrix}
1 & 1 & 0\\
0 & 1 & 1\\
1 & 0 & 1
\end{bmatrix}
\begin{bmatrix}
1 & 2 & 3\\
4 & 5 & 6\\
7 & 8 & 9
\end{bmatrix}
=
???
\end{equation*}
(D)
\begin{equation*}
\begin{bmatrix}
1 & 2 & 1 & 2\\
4 & 1 & -1 & -4
\end{bmatrix}
\begin{bmatrix}
0 & 3\\
1 & -1\\
2 & 1\\
5 & 2
\end{bmatrix}
=
???
\end{equation*}
(E)
\newpage
\begin{problem}[10pts]
The other fantastic browser-based tool we'll use in this course is Google's \href{https://colab.research.google.com/}{Colaboratory}.
Colaboratory is an environment for running Python notebooks that is \emph{free} and runs in the cloud.
So that means you don't have to worry at all about setting up a local environment.
This is based on an open source project called \href{https://jupyter.org/}{Jupyter}.
Colab/Jupyter notebooks are a way to write code in the browser, create visualizations, interact with data, and write documentation all in one place.
They are extremely common in the workflows of machine learning practitioners and data scientists, as they make it easy to iterate on things and see the results.
Much like \LaTeX, it's a good investment to learn how to use Python notebooks.
Typically, you would set up Jupyter on your own computer, but in this course we'll take advantage of the cloud and use Colab.
I suggest going and checking out the \href{https://colab.research.google.com/notebooks/welcome.ipynb}{Welcome Notebook} and watching the video there to get an idea as to what Colab is about, along with pointers to resources that you might find interesting for using it to do machine learning.
A more comprehensive \href{https://colab.research.google.com/notebooks/basic_features_overview.ipynb}{Overview Notebook} is also available and worth taking a look at.
The goal if this problem in the assignment is to get you started using Colab.
\begin{enumerate}[(A)]
\item Go to \href{https://colab.research.google.com/}{colab.research.com} and select ``New Python 3 Notebook''.\footnote{If you know and care about this kind of thing: we're going to use Python 3 exclusively as \href{https://www.python.org/doc/sunset-python-2/}{Python 2 has now entered sunset}.}
\item Rename the notebook by clicking on the filename in the upper left corner.
Give it a name like\\ \texttt{COS 302 HW1 - Firstname Lastname.ipynb}.
\item Make the first cell into a text cell via \texttt{Ctrl-m m} or insert a text cell and move it to the top.
As mentioned in the video, these text cells can be easily formatted using \href{https://www.markdownguide.org/basic-syntax}{Markdown}.
Put the notebook title into that first cell using something like:
\begin{lstlisting}[style=markdown]
# COS 302 HW1 - Firstname Lastname
\end{lstlisting}
Use \texttt{Shift-Enter} to execute the cell and render it.
\end{enumerate}
Keep this notebook open for the next problem.
\end{problem}
\newpage
\begin{problem}[15pts]
Now that you have a Colab notebook going, it's time to write a little bit of code.
\begin{enumerate}[(A)]
\item Start out by making a text cell with a level-two header identifying what problem you're working on:
\begin{lstlisting}[style=markdown]
## Problem 6
\end{lstlisting}
This is something you should generally do in the class to make it clear which parts of the notebook correspond to which parts of the assignment.
\item We're assuming that you've written some code before, but perhaps not Python specifically, so in this problem we'll hit some of the basics of the syntax.
(Python is great, but this isn't going to be a comprehensive Python course.)
Let's start out in the most basic way with a ``Hello world'' code cell:
\begin{lstlisting}[style=python]
print("hello world!")
\end{lstlisting}
Do a \texttt{Shift-Enter} or hit the play button on the left to execute this.
\item The key thing that can be weird at first with Python is that blocks are delineated by indentation rather than, e.g., curly braces.
That means that in Python whitespace matters in a way that it doesn't for languages like C and Java.
In a code cell, write an \texttt{if/else} statement that looks something like this:
\begin{lstlisting}[style=python]
course_name = "COS 302"
if course_name == "COS 302":
print("This is super fun!")
else:
print("Fun, but I wish I was in COS 302.")
\end{lstlisting}
Here we use the equals sign to assign a string to the variable \texttt{course\_name} and then a conditional that compares that variable to a string.
The equality comparison is done with a double-equals; the single vs.\ double equals thing here matters as the single equals does assignment and not comparison.
The \texttt{if} statement doesn't need to be in parentheses, although this is permitted.
The conditional statement ends in a colon and then the next line is indented to indicate what code should run if the statement is true.
It does not matter how many spaces there are, but the block all has to have the same indentation.
\item Create and execute code cells for each of the following and figure out which work and which don't:
\begin{lstlisting}[style=python]
if course_name == "COS 302":
print("This is super fun!")
print("I love Python.")
else:
print("Fun, but I wish I was in COS 302.")
\end{lstlisting}
\begin{lstlisting}[style=python]
if course_name == "COS 302":
print("This is super fun!")
print("I love Python.")
else:
print("Fun, but I wish I was in COS 302.")
\end{lstlisting}
\begin{lstlisting}[style=python]
if course_name == "COS 302":
print("This is super fun!")
print("I love Python.")
else:
print("Fun, but I wish I was in COS 302.")
\end{lstlisting}
\end{enumerate}
\end{problem}
\newpage
\begin{problem}[15pts]
Outside of the basic language, most interesting functionality in Python happens in packages.
Some of these are distributed with Python, some are commonly installed by default in comprehensive distributions like \href{https://www.anaconda.com/distribution/}{Anaconda}, and others need to be installed using tools like \href{https://pip.pypa.io/en/stable/}{\texttt{pip}}.
Part of the reason we're using Colab is so we don't have to worry about all that and you can just \texttt{import} things and get to work.
Start a new problem in the notebook using a level-two heading in a text cell, as described previously.
\begin{enumerate}[(A)]
\item For basic scalar math, the included \href{https://docs.python.org/3/library/math.html}{\texttt{math} package} will get you going.
Figure out how to write a loop that implements a \href{https://math.stackexchange.com/questions/2341017/natural-logarithm-in-a-chaotic-recursive-function-reaching-stable-points}{chaotic map} defined by~${f(n + 1) = \ln|f(n)|}$, where~$f(0)$ is any real number excluding~$1$ and~$e$.
Complete the \texttt{TODO}s below in a fresh code cell.
\begin{lstlisting}[style=python]
import math
num_iters = 10
f = # TODO
for ii in range(num_iters):
f = # TODO
print(f)
\end{lstlisting}
Here we're doing two new syntactual things: using an \href{https://docs.python.org/3/reference/import.html}{\texttt{import} statement}, and using a \href{https://docs.python.org/3/tutorial/controlflow.html}{\texttt{for} loop}.
The \texttt{import} is fairly self-explanatory, and the \texttt{for} loop has the same colon-and-whitespace structure as the conditional from the previous problem.
However, now you're looping with \texttt{ii} taking values from \texttt{0} to \texttt{num\_iters-1}.
The \href{https://docs.python.org/3/library/stdtypes.html#range}{\texttt{range}} function here returns a sequence of integers that you can iterate over.
\item For this course, and for most non-trivial mathematical computations in Python, we'll use \href{https://numpy.org/}{NumPy}.
NumPy lets you do things fast with vectors and matrices of floating point numbers.
Let's rewrite this code using NumPy.
Create a new code cell and fill in the \texttt{TODO}s:
\begin{lstlisting}[style=python]
import numpy as np
num_iters = 100
f_values = np.empty(num_iters)
f_values[0] = # TODO
for ii in range(1, num_iters):
f_values[ii] = # TODO
print(f_values)
\end{lstlisting}
The ``\texttt{as np}'' part of the import just lets you write \texttt{np.foo} rather than \texttt{numpy.foo} and is pretty conventional.
Another differences is that we're pre-allocating the memory for the values by creating a NumPy array (vector) of size \texttt{num\_iters}; you'll often also see code that does this with \href{https://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html}{\texttt{np.zeros}}, which allocates a zero array and is about as fast.
Finally, because we're operating on pre-allocated memory, we're \href{https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html}{indexing into the array} using square brackets.
\item One of the things we often want to be able to do is visualize data.
\href{https://matplotlib.org/}{Matplotlib} is not the only package out there for doings this in Python, but it is powerful and popular, so we'll use it in this course.
It also integrates nicely with Colab notebooks, so well in fact that the notebooks have special features to make figures easy to embed.
Turn on this integration with:
\begin{lstlisting}[style=python]
%matplotlib inline
\end{lstlisting}
Then import the package and display the values you've generated via:
\begin{lstlisting}[style=python]
import matplotlib.pyplot as plt
plt.plot(f_values, '.')
\end{lstlisting}
\end{enumerate}
\end{problem}
\newpage
\begin{problem}[10pts]
For the last problem, you learn how to turn in the Colab part of the homework assignment.
There will be two aspects to this: attaching a PDF to the \LaTeX{} file, and giving us a read-only link to your notebook.
We've made a \href{https://youtu.be/cDKZ-gJrdHk}{screencast} to help you walk through this.
\begin{enumerate}[(A)]
\item Make sure you have run all of the cells in your notebook so that we can see the output.
If we can't see the output in the PDF, you won't receive credit for the problem.
\item Close the table of contents on the left side so we can see your work clearly.
\item In \emph{File >> Print}, print to a PDF file and save it to your computer.
\item Insert the PDF into this document.
In Overleaf, that means uploading the PDF to the project; if you're editing \LaTeX{} locally then you'll probably need to put it in the same directory as this file.
At the end of this \texttt{.tex} file, you'll see the following lines:
\begin{lstlisting}[style=latex]
%\includepdf[pages=-]{mynotebook.pdf}
\end{document}
\end{lstlisting}
You should uncomment the \texttt{includepdf} command and change the name of the PDF to the one that you just downloaded.
Note that you probably don't want any spaces in the PDF file name.
If this worked, after you compile you should see your notebook at the end of this document.
\item We'd also like to be able to run your code ourselves.
As a side effect, you'll get to learn a little bit about version control.
For that, we'd like a link to what you consider your final version.
Go to the \emph{File} menu in the notebook (not the browser itself) and select \emph{Save and pin revision}.
It's helpful to rename the revision so that we know exactly which one you want graded, so open \emph{File >> Revision history} and then click the little three vertical dots next to your pinned revision and rename the file to something like ``Submitted''.
Now close the revision history and click the \emph{Share} button in the upper right.
Make sure you have \emph{anyone with a link can view} selected, copy the URL and then paste it below, replacing the dummy URL.
\bigskip
My notebook URL:
{\small \url{https://colab.research.google.com/XXXXXXXXXXXXXXXXXXXXXXX}}
\bigskip
You probably realize that you can continue to edit your notebook after the deadline.
Don't do this, as we'll be comparing your PDF to the notebook URL.
\end{enumerate}
\end{problem}
\begin{problem}[2pts]
Approximately how many hours did this assignment take you to complete?
\end{problem}
\subsection*{Changelog}
\begin{itemize}
\item 31 January 2021 -- Initial S21 version.
\end{itemize}
% \includepdf[pages=-]{mynotebook.pdf}
\end{document}