$\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}}$
Please find the ipynb
and related files for this exam through the link below.
https://hackmd.io/@jephianlin/S1eKGFAzq
Name:
Student ID #:
Please read the instructions carefully:
import numpy as np
import matplotlib.pyplot as plt
Let
ts = np.linspace(3,8,101)
xs = np.sin(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
Your answer:
ts = np.linspace(3,8,101)
xs = np.sin(ts)
ys = np.log(ts)
X = np.vstack([xs,ys]).T
X.mean(axis=0)
array([-0.16159496, 1.66714919])
Let
ts = np.linspace(3,8,101)
xs = np.sin(ts)
ys = np.log(ts)
Let $x_0,\ldots, x_{100}$ and $y_0,\ldots,y_{100}$ be the values in xs
and ys
, respectively.
Consider the points $(x_i,y_i)$ for $i = 0,\ldots, 100$.
How many of them satisfies $x^2 + 2y < 4$?
Your answer:
ts = np.linspace(3,8,101)
xs = np.sin(ts)
ys = np.log(ts)
np.sum(xs**2 + 2*ys < 4)
59
Let
xs = np.linspace(3,8,101)
ys = 2.9*xs + 2.1 + 0.7*np.cos(xs)
Let $x_0,\ldots, x_{100}$ and $y_0,\ldots,y_{100}$ be the values in xs
and ys
, respectively.
Let $f(x) = 3x + 2$.
Find the mean square error between $y$ and $f(x)$, which is defined as
$$\frac{1}{101}\sum_{i=0}^{100}(f(x_i) - y_i)^2.$$
Your answer:
xs = np.linspace(3,8,101)
ys = 2.9*xs + 2.1 + 0.7*np.cos(xs)
((3*xs + 2 - ys) ** 2).mean()
0.2630775044663041
Let
ts = np.linspace(0, np.pi, 181)
xs = 3 * np.cos(ts)
ys = 2 * np.sin(ts)
Let $x_0,\ldots, x_{180}$ and $y_0,\ldots,y_{180}$ be the values in xs
and ys
, respectively.
Consider the points $(x_i,y_i)$ for $i = 0,\ldots, 180$.
For which $i$, the point $(x_i,y_i)$ is the closest to the point $(5,7)$?
Your answer:
ts = np.linspace(0, np.pi, 181)
xs = 3 * np.cos(ts)
ys = 2 * np.sin(ts)
np.argmin((xs - 5)**2 + (ys - 7)**2)
50
Let
$$ A = \begin{bmatrix} 1 & 1 \\ 1 & 2 \\ 1 & 3 \end{bmatrix} \text{ and } \bb = \begin{bmatrix} 5 \\ 3 \\ 3 \end{bmatrix}. $$Find $\bx\in\mathbb{R}^2$ such that $\|A\bx - \bb\|$ is minimized.
Your answer:
A = np.array([
[1,1],
[1,2],
[1,3]
])
b = np.array([5,3,3])
ATAinv = np.linalg.inv(A.T.dot(A))
x = ATAinv.dot(A.T).dot(b)
x
array([ 5.66666667, -1. ])
Let
$$ A = \begin{bmatrix} 4 & 1 & 1 \\ 1 & 4 & 1 \\ 1 & 1 & 4 \end{bmatrix} \text{ and } \bv = \begin{bmatrix} 1 \\ 0 \\ -1 \end{bmatrix}. $$Is $\bv$ an eigenvector of $A$?
If yes, what is the corresponding eigenvalue?
Your answer:
A = np.array([
[4,1,1],
[1,4,1],
[1,1,4]
])
v = np.array([1,0,-1])
print(A.dot(v))
print(v)
# Observe that A v = 3 v.
# Yes, v is the eigenvector of A and the corresponding eigenvalue is 3.
[ 3 0 -3] [ 1 0 -1]
Let
Q = np.array([[1,1,1],
[-1,1,1],
[0,-2,1]])
Q = Q / np.linalg.norm(Q, axis=0)
Let $\bu_0, \bu_1, \bu_2$ be the columns of Q
.
Find a matrix $A$ such that
$A\bu_0 = 2\bu_0$
$A\bu_1 = 3\bu_1$
$A\bu_2 = 4\bu_2$.
Your answer:
Q = np.array([[1,1,1],
[-1,1,1],
[0,-2,1]])
Q = Q / np.linalg.norm(Q, axis=0)
D = np.diag([2,3,4])
A = Q.dot(D).dot(Q.T)
A
array([[2.83333333, 0.83333333, 0.33333333], [0.83333333, 2.83333333, 0.33333333], [0.33333333, 0.33333333, 3.33333333]])
Let
Q = np.array([[1,2,2],
[-2,1,1],
[0,-5,1]])
Q = Q / np.linalg.norm(Q, axis=0)
D = np.diag([1,1,-1])
A = Q.dot(D).dot(Q.T)
Let $A$ be the same as A
.
What does the action $\bv \mapsto A\bv$ do?
If it is a projection or a reflection, make sure you describe the corresponding line/plane.
Your answer:
# Since the eigenvalues are 1, 1, 0,
# the action is the reflection along the plane spanned by the 0-th and the 1-st columns of Q.
Let
$$ A = \begin{bmatrix} 1 & 2 \\ 1 & 3 \\ 1 & 4 \end{bmatrix}. $$Find the minimum value of $\bx\trans A\trans A\bx$ over all vector $\bx\in\mathbb{R}^2$ of length $1$.
Your answer:
A = np.array([
[1,2],
[1,3],
[1,4]
])
ATA = A.T.dot(A)
vals, vecs = np.linalg.eigh(ATA)
vals.min()
0.18861169915810327
Go to the link provided on the top of this file, and download the file hidden_text.csv
.
Let
arr = np.genfromtxt('hidden_text.csv', delimiter=',')
m,n = arr.shape
U,s,Vh = np.linalg.svd(arr)
Let $\sigma$'s be the values in s
, $\bu_i$'s the columns of U
, and $\bv_i$'s the columns of Vh.T
.
Compute
$$
B = \sum_{\sigma < 300}\sigma_i\bu_i\bv_i\trans
$$
and use plt.imshow(..., cmap='Greys_r')
to visualize the matrix $B$.
What text do you see there?
Your answer:
arr = np.genfromtxt('hidden_text.csv', delimiter=',')
m,n = arr.shape
U,s,Vh = np.linalg.svd(arr)
plt.plot(s) ### check the values in s --> lots of values are above 400
s[s > 300] = 0
Sigma = np.zeros((m,n))
Sigma[np.arange(m), np.arange(m)] = s # m is the smaller one
new_arr = U.dot(Sigma).dot(Vh)
plt.imshow(new_arr, cmap='Greys_r') ### so the answer is HOPE
Run code cell 1 and code cell 2.
It is known that f
is a linear function from $\mathbb{R}^3$ to $\mathbb{R}^3$.
You may modify the line
v = f([1, 2, 3])
in code cell 2 to find out the output of f
with different inputs.
Find a matrix $A$ such that f
$(\bx) = A\bx$ for all $\bx\in\mathbb{R}^3$.
# code cell 1
bit_encoding = {0:'0',1:'1',2:'2',3:'3',4:'4',5:'5',6:'6',7:'7',8:'8',9:'9',10:'A',11:'B',12:'C',13:'D',14:'E',15:'F',16:'G',17:'H',18:'I',19:'J',20:'K',21:'L',22:'M',23:'N',24:'O',25:'P',26:'Q',27:'R',28:'S',29:'T',30:'U',31:'V',32:'W',33:'X',34:'Y',35:'Z',36:'a',37:'b',38:'c',39:'d',40:'e',41:'f',42:'g',43:'h',44:'i',45:'j',46:'k',47:'l',48:'m',49:'n',50:'o',51:'p',52:'q',53:'r',54:'s',55:'t',56:'u',57:'v',58:'w',59:'x',60:'y',61:'z',62:'-',63:'%'}
def get_parameter_encoding(data):
data = bytes(data, encoding='UTF8')
data_len = len(data)
data += b'\0\0\0'
encode_len = (data_len * 8) // 6 + (data_len % 3 > 0)
encode = bytearray(encode_len)
index = 0
read_mode = [((int('00111111', 2), 0), (int('00000000', 2), 0), 0), ((int('11000000', 2), 6), (int('00001111', 2), 2), 1), ((int('11110000', 2), 4), (int('00000011', 2), 4), 1), ((int('11111100', 2), 2), (int('00000000', 2), 0), 1)]
for i in range(encode_len):
r_mod = read_mode[i % 4]
lbits = data[index] & r_mod[0][0]
hbits = data[index + 1] & r_mod[1][0]
encode[i] = ord(bit_encoding[(lbits >> r_mod[0][1]) + (hbits << r_mod[1][1])])
index += r_mod[2]
return encode.decode(encoding='UTF8')
# code cell 2
import requests
result = requests.post('https://python-online-runner.herokuapp.com/api/pyrunner', {
'define': 'AGMPc1YPe4cSobYEA028W02Uia7Bw1IFW4cSof08W028oL6Tr9dRWiLUhe7BWe7BWWNNA0',
'execute': get_parameter_encoding('''
v = f([1, 2, 3])
print(v)
''')
})
print(result.text)
Your answer:
# Use the code to get
# f(1,0,0) --> (0,0,1)
# f(0,1,0) --> (1,0,0)
# f(0,0,1) --> (1,1,0)
# Therefore, A can be
# [0, 1, 1]
# [0, 0, 1]
# [1, 0, 0]
Exam ends here.
Total point = 20 (+2)
Your score: