Math599 2023S¶

$\newcommand{\trans}{^\top} \newcommand{\adj}{^{\rm adj}} \newcommand{\cof}{^{\rm cof}} \newcommand{\inp}[2]{\left\langle#1,#2\right\rangle} \newcommand{\dunion}{\mathbin{\dot\cup}} \newcommand{\bzero}{\mathbf{0}} \newcommand{\bone}{\mathbf{1}} \newcommand{\ba}{\mathbf{a}} \newcommand{\bb}{\mathbf{b}} \newcommand{\bc}{\mathbf{c}} \newcommand{\bd}{\mathbf{d}} \newcommand{\be}{\mathbf{e}} \newcommand{\bh}{\mathbf{h}} \newcommand{\bp}{\mathbf{p}} \newcommand{\bq}{\mathbf{q}} \newcommand{\br}{\mathbf{r}} \newcommand{\bx}{\mathbf{x}} \newcommand{\by}{\mathbf{y}} \newcommand{\bz}{\mathbf{z}} \newcommand{\bu}{\mathbf{u}} \newcommand{\bv}{\mathbf{v}} \newcommand{\bw}{\mathbf{w}} \newcommand{\tr}{\operatorname{tr}} \newcommand{\nul}{\operatorname{null}} \newcommand{\rank}{\operatorname{rank}} %\newcommand{\ker}{\operatorname{ker}} \newcommand{\range}{\operatorname{range}} \newcommand{\Col}{\operatorname{Col}} \newcommand{\Row}{\operatorname{Row}} \newcommand{\spec}{\operatorname{spec}} \newcommand{\vspan}{\operatorname{span}} \newcommand{\Vol}{\operatorname{Vol}} \newcommand{\sgn}{\operatorname{sgn}} \newcommand{\idmap}{\operatorname{id}} \newcommand{\am}{\operatorname{am}} \newcommand{\gm}{\operatorname{gm}} \newcommand{\mult}{\operatorname{mult}} \newcommand{\iner}{\operatorname{iner}}$

LA exam¶

Please find the ipynb and related files for this exam through the link below.
https://hackmd.io/@jephianlin/Hk8Mpk2e3

Name:

Student ID #:

Please read the instructions carefully:

  1. Write your name and Student ID # first.
  2. Write down your answers on the exam paper; no need to write down the code.
  3. You are allowed to use the internet, but you are NOT allowed to communicate with others in any form.
  4. Do NOT use your cell phone, use the computer instead.
  5. Different problems might use same variable names. Make sure you use the right one to answer the problem.
  6. If the answer is too long, write two digits after the decimal point.

In [1]:
import numpy as np
import matplotlib.pyplot as plt
Problem 1 [2pt]¶

Let

ts = np.linspace(3,9,101)
xs = np.arctan(ts)
ys = np.log(ts)
X = np.vstack([xs,ys]).T

Let $\bv_0,\ldots,\bv_{100}$ be the rows of X .
Find the center

$$ \frac{1}{101}\sum_{i = 0}^{100}\bv_i. $$

Your answer:

In [2]:
ts = np.linspace(3,9,101)
xs = np.arctan(ts)
ys = np.log(ts)
X = np.vstack([xs,ys]).T

X.mean(axis=0)
Out[2]:
array([1.38998296, 1.74554336])
Problem 2 [2pt]¶

Let

A = np.ones((5,5)) + 3 * np.eye(5)
v = np.ones((5,))

Is v an eigenvector of A ? If yes, what is the corresponding eigenvalue?

Your answer:

In [3]:
A = np.ones((5,5)) + 3 * np.eye(5)
v = np.ones((5,))

print(np.isclose(np.abs(A.dot(v).dot(v)), 
                 np.linalg.norm(A.dot(v)) * np.linalg.norm(v)))
print("eigenvalue:", A.dot(v)[0] / v[0])
True
eigenvalue: 8.0
Problem 3 [2pt]¶

Let

$$ A = \begin{bmatrix} 1 & 3 \\ 3 & 2 \end{bmatrix}. $$

Find

$$ \min_{\|\bx\| = 1} \bx\trans A\bx $$

and the $\bx$ that achieves this minimum value.

Your answer:

In [4]:
A = np.array([[1,3],
              [3,2]])

vs = np.random.randn(2,10000)
vs = vs / np.linalg.norm(vs, axis=0)
Avs = A.dot(vs)
quotients = np.sum(vs * Avs, axis=0)

i_min = quotients.argmin()
print("min value:", quotients[i_min])
print("achieved by:", vs[:,i_min])
min value: -1.541381106027751
achieved by: [-0.76291543  0.6464983 ]
Problem 4 [2pt]¶

Let

B = np.array([[0,1,2],
              [1,0,1],
              [2,1,0]])
C = np.ones((10,)) + 3 * np.eye(10)
A = np.kron(B,C)

and $a_{i,j}$ the $i,j$-entry of A . Find the sum of $\frac{1}{a_{i,j}}$ for all $a_{i,j} \neq 0$ in A .

Your answer:

In [5]:
B = np.array([[0,1,2],
              [1,0,1],
              [2,1,0]])
C = np.ones((10,)) + 3 * np.eye(10)
A = np.kron(B,C)

mask = (A != 0)
np.sum(1 / A[mask])
Out[5]:
462.5
Problem 5 [2pt]¶

Let

X = np.arange(500).reshape(250,2)
y = np.arange(250) % 5

Let $\bp_i$ be the rows of X and $y_i$ the entries of y . Find the center of $\{\bp_i: y_i = 3\}$.

Your answer:

In [6]:
X = np.arange(500).reshape(250,2)
y = np.arange(250) % 5

X[y == 3].mean(axis=0)
Out[6]:
array([251., 252.])
Problem 6 [2pt]¶

Let

xs = np.linspace(-2,2,100)
ys = np.sin(xs) + 1
X = np.vstack([xs,ys]).T

Find the number of rows of X whose norm is less than $2.5$.

Your answer:

In [7]:
xs = np.linspace(-2,2,100)
ys = np.sin(xs) + 1
X = np.vstack([xs,ys]).T

mask = (np.linalg.norm(X, axis=1) < 2.5)
mask.sum()
Out[7]:
87
Problem 7 [2pt]¶

Let

$$ A = \begin{bmatrix} 1 & 1 \\ 1 & 2 \\ 1 & 3 \\ 1 & 4 \end{bmatrix} \text{ and } \by = \begin{bmatrix} 3 \\ 4 \\ 4 \\ 4 \end{bmatrix}. $$

Find the vector $\bc$ such that $\|A\bc - \by\|$ is minimized.

Your answer:

In [8]:
A = np.array([[1,1],
              [1,2],
              [1,3],
              [1,4]])
y = np.array([3,4,4,4])

ATAinv = np.linalg.inv(A.T.dot(A))
c = ATAinv.dot(A.T).dot(y)
c
Out[8]:
array([3. , 0.3])
Problem 8 [2pt]¶

Let

Q = np.array([[1,2,2], 
              [-2,1,1], 
              [0,-5,1]])
Q = Q / np.linalg.norm(Q, axis=0)

Let $\beta = \{\bu_0, \bu_1, \bu_2\}$ be the columns of Q . Find the vector $\bv$ with $[\bv]_\beta = \begin{bmatrix} 5 \\ 4 \\ 3 \end{bmatrix}$.

Your answer:

In [9]:
Q = np.array([[1,2,2], 
              [-2,1,1], 
              [0,-5,1]])
Q = Q / np.linalg.norm(Q, axis=0)

v_beta = np.array([5,4,3])
Q.dot(v_beta)
Out[9]:
array([ 6.14615121, -2.51709434, -2.42673885])
Problem 9 [2pt]¶

Let

x = np.array([1,2,3])
y = np.array([4,5,6,7])

Write one line of the code to generate

array([[ 5,  6,  7,  8],
       [ 6,  7,  8,  9],
       [ 7,  8,  9, 10]])

from x and y by broadcasting.

Your answer:

In [10]:
x = np.array([1,2,3])
y = np.array([4,5,6,7])

line = r"x[:,np.newaxis] + y[np.newaxis,:]"
print(line)
eval(line)
x[:,np.newaxis] + y[np.newaxis,:]
Out[10]:
array([[ 5,  6,  7,  8],
       [ 6,  7,  8,  9],
       [ 7,  8,  9, 10]])
Problem 10 [2pt]¶

Let

p = np.array([0.8] + [0.2 / 99] * 99)

and $p_i$ the entries of p . Find

$$ \sum_{i = 0}^{99} p_i (1 - p_i). $$

Your answer:

In [11]:
p = np.array([0.8] + [0.2 / 99] * 99)

np.sum(p * (1 - p))
Out[11]:
0.3595959595959597
Problem 11 [extra 2pt]¶

Let

$$ M = \begin{bmatrix} 0.2 & 0.3 & 0 \\ 0.8 & 0.4 & 0.8 \\ 0 & 0.3 & 0.2 \end{bmatrix} \text{ and } \bx = \begin{bmatrix} 1 \\ 0 \\ 0 \end{bmatrix}. $$

Find $\lim_{t\to\infty} M^t \bx$.

Your answer:

In [12]:
M = np.array([[0.2,0.3,0],
              [0.8,0.4,0.8],
              [0,0.3,0.2]])
x = np.array([1,0,0])

for _ in range(10000):
    x = M.dot(x)
    
x
Out[12]:
array([0.21428571, 0.57142857, 0.21428571])

Exam ends here.
Total point = 20 (+2)

Your score: