You may find the ipynb
file of this exam at
http://www.math.nsysu.edu.tw/~chlin/2021SMath599/LinearA-exam.ipynb
Name:
Student ID #:
Please read the instructions carefully:
import numpy as np
import matplotlib.pyplot as plt
Let
grid = np.meshgrid(np.linspace(0,10,101),np.linspace(0,10,101))
xs = grid[0].ravel()
ys = grid[1].ravel()
vs = np.vstack([xs,ys])
The columns of vs
contain 10201 points $(x_i,y_i)$.
How many of these points satisfy $2x + 3y > 12.01$?
Your answer:
grid = np.meshgrid(np.linspace(0,10,101),np.linspace(0,10,101))
xs = grid[0].ravel()
ys = grid[1].ravel()
vs = np.vstack([xs,ys])
r = np.array([2,3])
np.sum(r.dot(vs) > 12.01)
8940
Let
x = np.linspace(0,10,101)
y = 2.1*x + 1.1 + 0.5*np.sin(x)
The variable x
stores 101 values $x_i$'s.
The variable y
stores 101 values $y_i$'s.
Let $f(x) = 2x + 1$.
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:
x = np.linspace(0,10,101)
y = 2.1*x + 1.1 + 0.5*np.sin(x)
diff = y - 2*x - 1
np.mean(diff**2)
0.6563453624224954
Let
grid = np.meshgrid(np.linspace(-5,5,101),np.linspace(-5,5,101))
c0 = grid[0].ravel()
c1 = grid[1].ravel()
cs = np.vstack([c0,c1])
A = np.array([[1,1],
[-1,1],
[0,-2]])
pts = A.dot(cs)
Let ${\bf b} = (1,1,2)^\top$ and ${\bf p}_i$'s the columns of pts
.
Find
$$\min_{i}\|{\bf b} - {\bf p}_i\|.$$
That is, the shortest distance between ${\bf b}$ and a point ${\bf p}_i$.
Your answer:
grid = np.meshgrid(np.linspace(-5,5,101),np.linspace(-5,5,101))
c0 = grid[0].ravel()
c1 = grid[1].ravel()
cs = np.vstack([c0,c1])
A = np.array([[1,1],
[-1,1],
[0,-2]])
pts = A.dot(cs)
b = np.array([1,1,2])
diff = b[:,np.newaxis] - pts
dist = np.linalg.norm(diff, axis=0)
dist.min()
2.3108440016582685
Let
$$A = \begin{bmatrix} 1 & 1 \\ -1 & 1 \\ 0 & -2 \end{bmatrix} \text{ and } {\bf b} = \begin{bmatrix} 1 \\ 1 \\ 2 \end{bmatrix}.$$
Solve $A{\bf x} = {\bf b}$.
If there is no solution for ${\bf x}$, then find ${\bf x}$ that minimizes $\|A{\bf x } - {\bf b}\|$.
Your answer:
A = np.array([[1,1],
[-1,1],
[0,-2]])
b = np.array([1,1,2])
ATAinv = np.linalg.inv(A.T.dot(A))
x = ATAinv.dot(A.T).dot(b)
print('x =', x)
print('Ax =', A.dot(x))
print('dist =', np.linalg.norm(A.dot(x) - b))
x = [ 0. -0.33333333] Ax = [-0.33333333 -0.33333333 0.66666667] dist = 2.3094010767585034
Let
A = np.arange(1,5)[:,np.newaxis] ** np.arange(3)
b = np.array([3,2,3,2])
Find the projection of b
onto the column space of A
.
Your answer:
A = np.arange(1,5)[:,np.newaxis] ** np.arange(3)
b = np.array([3,2,3,2])
ATAinv = np.linalg.inv(A.T.dot(A))
proj = A.dot(ATAinv).dot(A.T).dot(b)
proj
array([2.8, 2.6, 2.4, 2.2])
Download the file
http://www.math.nsysu.edu.tw/~chlin/2021SMath599/LinearA-text.csv
Let
arr = np.genfromtxt('LinearA-text.csv', delimiter=',')
vals,vecs = np.linalg.eigh(arr)
Let $\lambda_i$'s be the values in vals
and ${\bf u}_i$'s the columns of vecs
.
Compute
$$ B = \sum_{\lambda_i < 300}\lambda_i{\bf u}_i{\bf u}_i^\top$$
and use plt.imshow(..., cmap='Greys_r)
to visualize the matrix $B$.
What text do you see there?
Your answer:
arr = np.genfromtxt('LinearA-text.csv', delimiter=',')
vals,vecs = np.linalg.eigh(arr)
new_vals = vals.copy()
mask = (vals > 300)
new_vals[mask] = 0
D = np.diag(new_vals)
Q = vecs
new_arr = Q.dot(D).dot(Q.T)
plt.imshow(new_arr, cmap='Greys_r')
The answer is "BOOK".
Let
Q = np.array([[1,1,1],
[-1,1,1],
[0,-2,1]])
Q = Q / np.linalg.norm(Q, axis=0)
Let ${\bf u}_0, {\bf u}_1, {\bf u}_2$ be the columns of Q
.
Let ${\bf v} = (2,1,1)^\top$.
Find $c_0,c_1,c_2$ such that ${\bf v} = c_0{\bf u}_0 + c_1{\bf u}_1 + c_2{\bf u}_2$.
Your answer:
Q = np.array([[1,1,1],
[-1,1,1],
[0,-2,1]])
Q = Q / np.linalg.norm(Q, axis=0)
v = np.array([2,1,1])
Q.T.dot(v)
array([0.70710678, 0.40824829, 2.30940108])
Let
Q = np.array([[1,1,1],
[-1,1,1],
[0,-2,1]])
Q = Q / np.linalg.norm(Q, axis=0)
Let ${\bf u}_0, {\bf u}_1, {\bf u}_2$ be the columns of Q
.
Find a matrix $A$ such that
$A{\bf u}_0 = {\bf u}_0$
$A{\bf u}_1 = {\bf u}_1$
$A{\bf u}_2 = {\bf 0}$.
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([1,1,0])
A = Q.dot(D).dot(Q.T)
A
array([[ 0.66666667, -0.33333333, -0.33333333], [-0.33333333, 0.66666667, -0.33333333], [-0.33333333, -0.33333333, 0.66666667]])
Let
k = 3
J = np.ones((k,k))
v = np.ones(k)
Is v
an eigenvector of J
?
If yes, what is the corresponding eigenvalue?
Your answer:
k = 3
J = np.ones((k,k))
v = np.ones(k)
print(v)
print(J.dot(v))
print(J.dot(v) / v)
[1. 1. 1.] [3. 3. 3.] [3. 3. 3.]
The asnwer is 3.
Let
$$A = \begin{bmatrix} -2 & -2 \\ -1 & -1 \\ 0 & 0 \\ 1 & 1 \\ 2 & 2 \end{bmatrix}.$$
Find a vector ${\bf x}$ of length $1$ that maximizes ${\bf x}^\top A^\top A{\bf x}$.
Your answer:
A = np.array([[-2,-2],
[-1,-1],
[0,0],
[1,1],
[2,2]])
vals,vecs = np.linalg.eigh(A.T.dot(A))
vecs[:,-1]
array([0.70710678, 0.70710678])
Download the file
http://www.math.nsysu.edu.tw/~chlin/2021SMath599/LinearA_T.py
Run
from LinearA_T import f
Thus, f
is a function taking an array of shape (3,)
and return another.
For example, you may run.
v = np.array([1,2,3])
f(v)
Suppose that $f=$ f
is a linear function from $\mathbb{R}^3$ to $\mathbb{R}^3$.
Find a matrix $A$ such that $A{\bf v} = f({\bf v})$ for all ${\bf v}\in\mathbb{R}^3$.
Your answer:
from LinearA_T import f
e0,e1,e2 = np.eye(3)
A = np.vstack([f(e0), f(e1), f(e2)]).T
A
array([[1., 1., 1.], [0., 1., 1.], [0., 0., 1.]])
Exam ends here.
Total point = 20 (+2)
Your score: