18.06 Spring 2019 pset 1¶

due Wednesday 2/13 at 10:55am by submitting pdf through Gradescope¶

(Added note: Many have noticed that just printing to pdf with the browser print button may be the easiest).

This problem set is in the form of a Julia notebook (using the Jupyter/IJulia browser-based interface to interactive programming). We will be using the Julia language throughout the term for mathematical and computational explorations. Jupyter (an amalgam of Julia/Python/R) is a language agnostic notbeook interface widely used for simple calculations .

You can run this notebook without installing anything by logging in at JuliaBox. Just download the notebook file (a .ipynb file) by clicking the download icon at the upper right, then drag it onto the JuliaBox dashboard to upload it there. (We will be using Julia version 1.0 or later). (Avoid the json format by downloading the link or at least having the ipynb extension.)

Some of the problems are pencil-and-paper (we just happen to use the notebook to describe them), and some of them require you to run the code in the notebook to see what happens and then explain it. To run the code in an input cell, just click on the cell and then type shift-return; see also the "Help" menu in the notebook. When we say, check, we are asking you to run the code, but not necessarily write anything.

When you submit your pset, you may handwrite or type, but submit clearly labeled PDFs. (An app like Tiny Scanner for your phone makes it easier to scan black-and-white documents into legible PDF files using a cell-phone camera.) For printing a notebook to PDF, you may find that the Jupyter Download-as-PDF or printing Print Preview (in the File menu at the top of the notebook interface) produces a nicer file than directly printing from your browser.

Problem 1 (10 points)¶

Warm up problem. On a clock, 12 unit vectors point to each hour, uniformly spaced. What is the sum of all of these vectors? Why?

In [ ]:

Problem 2 (10 points)¶

Warm up problem. On the neural net, matrix times vector, diagram that we saw in class, if there are p nodes (circles) on the left and q nodes on the right, then how many connections are there in terms of p and q? (In the diagram example there were 15.)

In [ ]:

In the next series of problems we will take a big tour of important ideas, but only in the 2x2 case. Later on we will discuss the generalizations to larger matrices.¶

Problem 3 (10 points)¶

What is the formula for the inverse of the 2x2 matrix $\begin{pmatrix} a & b \\ c & d \end{pmatrix}$? (Answer is on Strang p. 84) On what conditions for a,b,c,d does the inverse exist?

In [ ]:

Problem 4 (10 points)¶

A worked matrix factorization problem is at the end of this notebook.

This is called the 2 x 2 LU factorization: Given $A = \begin{pmatrix} a & b \\ c & d \end{pmatrix}$, find a factorization of $A$ in the form $ \begin{pmatrix} 1 & 0 \\ x & 1 \end{pmatrix}\begin{pmatrix} u & v \\ 0 & w \end{pmatrix}$.

(Write down x,u,v,w in terms of a,b,c,d.) On what conditions for a,b,c,d does the LU factorization exist? (This answer is different from that of Problem 3.)

In [13]:

Out[13]:
\[\left[ \begin{array}{rr}a&b\\c&d\end{array}\right]\]

Problem 5 (10 points)¶

A 2x2 rotation matrix has the form $ \begin{pmatrix} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \end{pmatrix}$. Remembering $\sin^2 \theta + \cos^2 \theta =1$, what is $ \begin{pmatrix} \cos \theta & \sin \theta \\ -\sin \theta & \cos \theta \end{pmatrix}$ $ \begin{pmatrix} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \end{pmatrix}$?


This is called the 2x2 QR factorization: Given $A = \begin{pmatrix} a & b \\ c & d \end{pmatrix}$, find a factorization of $A$ in the form $ \begin{pmatrix} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \end{pmatrix}\begin{pmatrix} u & v \\ 0 & w \end{pmatrix}$. The matrix $Q = \begin{pmatrix} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \end{pmatrix}$ is known as a 2x2 rotation matrix. (Hint1: what are the polar coordinates for the point(a,c)?? You should be able to write a formula for $u,\cos \theta,\sin \theta$ from a and c using only division and square roots. (at least if a and c are not both 0). Hint 2, use what we just mentioned about rotation matrices.)

In [ ]:

Problem 6 (10 points)¶

The 2x2 singular value decomposition: An SVD has the form $A = \begin{pmatrix} a & b \\ c & d \end{pmatrix}$ = (rotation 1)(diagonal with non-negative entries)(rotation 2) = $ \begin{pmatrix} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \end{pmatrix}$ $ \begin{pmatrix} \sigma_1 & 0 \\ 0 & \sigma_2 \end{pmatrix}$ $ \begin{pmatrix} \cos \phi & \sin \phi \\ -\sin \phi & \cos \phi \end{pmatrix}$. We will see in this class that the SVD is very important in data science, biology, and so many fields.

Compute $A$ if $\theta=\pi/6$ and $\phi=\pi/3$ and $\sigma_1=\sigma_2=1$.

In [ ]:

In [ ]:
# Check your work numerically
 θ = π/6
 ϕ = π/3
 σ₁ = σ₂ = 1
[cos(θ) -sin(θ);sin(θ) cos(θ)] * [1 0;0 1] * [cos(ϕ) sin(ϕ);-sin(ϕ) cos(ϕ)]

Next Problems investigate structured matrices¶

In [ ]:
n = 5
D =  rand(1:9, n, n)  # Create a random n x n matrix with the digits 1 through 9

Put your cursor and click on the cell above and use control-enter to run the cell a few times without moving to another cell. If we asked how many numbers are stored in this n by n matrix you would say $n^2$.

In [3]:
using LinearAlgebra
n = 5
D =  Diagonal(rand(1:9, n))  # Create a random n x n digonal matrix with the digits 1 through 9
Out[3]:
5×5 Diagonal{Int64,Array{Int64,1}}:
 9  ⋅  ⋅  ⋅  ⋅
 ⋅  4  ⋅  ⋅  ⋅
 ⋅  ⋅  3  ⋅  ⋅
 ⋅  ⋅  ⋅  4  ⋅
 ⋅  ⋅  ⋅  ⋅  7

While this is an n by n matrix, the number of parameters is n (which is what Julia stores in memory.)

Problem 7 (10 points): As a function of n, how many parameters are there in an n by n (upper) Bidigonal Matrix?¶

In [119]:
B = Bidiagonal(rand(1:9,n), rand(1:9,n-1), :U)
Out[119]:
5×5 Bidiagonal{Int64,Array{Int64,1}}:
 5  1  ⋅  ⋅  ⋅
 ⋅  2  5  ⋅  ⋅
 ⋅  ⋅  1  4  ⋅
 ⋅  ⋅  ⋅  4  3
 ⋅  ⋅  ⋅  ⋅  6
In [ ]:

Problem 8 (10 points): As a function of n, how many (independent) parameters are there in an n by n Symmetric Tridiagonal Matrix? Why is the answer the same as for the bidiagonal?¶

In [120]:
S = SymTridiagonal(rand(1:9,n), rand(1:9,n-1))
Out[120]:
5×5 SymTridiagonal{Int64,Array{Int64,1}}:
 6  9  ⋅  ⋅  ⋅
 9  8  8  ⋅  ⋅
 ⋅  8  2  6  ⋅
 ⋅  ⋅  6  1  3
 ⋅  ⋅  ⋅  3  6

Problem 9 (10 points) A symmetric "rank 1" matrix has the form $A_{ij} = v_i v_j$ where $v$ is a vector. Why does such a matrix really have only $n$ parameters?¶

In [29]:
#Example
v = [1,2,3,4,5]
[vi*vj for vi∈v, vj∈v]
Out[29]:
5×5 Array{Int64,2}:
 1   2   3   4   5
 2   4   6   8  10
 3   6   9  12  15
 4   8  12  16  20
 5  10  15  20  25
In [160]:
v = rand(1:9,n)
println(v)
[vi*vj for vi∈v, vj∈v]
[4, 1, 9, 3, 7]
Out[160]:
5×5 Array{Int64,2}:
 16  4  36  12  28
  4  1   9   3   7
 36  9  81  27  63
 12  3  27   9  21
 28  7  63  21  49

Problem 10 (10 points) A general "rank 1" matrix has the form $A_{ij}=v_iw_j$ where $v$ and $w$ are vectors of size $m$ and $n$ respectively. Think of a reason why this matrix really has only $m+n-1$ parameters.¶

(If it helps, you can assume "generically" that the (1,1) entry is not 0.)

In [166]:
v = rand(1:9,3)
w = rand(1:9,4)
println(v)
println(w)
[vi*wj for vi∈v, wj∈w]
[9, 5, 6]
[2, 2, 1, 1]
Out[166]:
3×4 Array{Int64,2}:
 18  18  9  9
 10  10  5  5
 12  12  6  6
In [ ]:

Sample 2x2 Matrix Factorization Problem¶

A 2x2 symmetric $A$ has the form $A = \begin{pmatrix} x & y \\ y & z \end{pmatrix}$. Find a factorization of $A$ in the form $ \begin{pmatrix} 1 & 0 \\ a & 1 \end{pmatrix} \begin{pmatrix} d_1 & 0 \\ 0 & d_2 \end{pmatrix} \begin{pmatrix} 1 & a \\ 0 & 1 \end{pmatrix}$. (Solve for $a,d_1,d_2$ in terms of x,y, and z). When does this factorization not exist?

Solution: we can multiply out $A = \begin{pmatrix} x & y \\ y & z \end{pmatrix}$=$ \begin{pmatrix} 1 & 0 \\ a & 1 \end{pmatrix} \begin{pmatrix} d_1 & 0 \\ 0 & d_2 \end{pmatrix} \begin{pmatrix} 1 & a \\ 0 & 1 \end{pmatrix}$ to get $ \begin{pmatrix} 1 & 0 \\ a & 1 \end{pmatrix} \begin{pmatrix} d_1 & d_1 a \\ 0 & d_2 \end{pmatrix} = \begin{pmatrix} d_1 & d_1 a \\ d_1 a & d_1 a^2 + d_2 \end{pmatrix}. $
We immediately see from the (1,1) entry that $d_1=x$.
From the (1,2) or (2,1) entry we see that $d_1a=y$ or $a=y/d_1=y/x$. There is one entry left, the (2,2) entry which tells us that $z=d_1 a^2 + d_2$ or $d_2 = z-d_1 a^2 = z - y^2/x$.

In summary $a=y/x, d_1=x$ and $d_2 = z-y^2/x$. We have thus concluded that the following factorization always holds unless x=0:
$\begin{pmatrix} x & y \\ y & z \end{pmatrix} = \begin{pmatrix} 1 & 0 \\ y/x & 1 \end{pmatrix} \begin{pmatrix} x & 0 \\ 0 & z-y^2/x \end{pmatrix} \begin{pmatrix} 1 & y/x \\ 0 & 1 \end{pmatrix}$

While not at all required by this problem, if you like you can check your work with some numbers. This won't be possible during an exam, but it's nice to do right now.

In [39]:
x,y,z = rand(3)
a = y/x
d₁ = x
d₂ = z - y^2/x
display([x y;y z])
display( [1 0;a 1] * [d₁ 0;0 d₂] * [1 a;0 1] )
2×2 Array{Float64,2}:
 0.804518  0.603452
 0.603452  0.496319
2×2 Array{Float64,2}:
 0.804518  0.603452
 0.603452  0.496319

You can use [control enter] a few times in the cell above to test as many cases that would convince you that you are right.

In [ ]: