$\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/B18Gakhxn
Name:
Student ID #:
Please read the instructions carefully:
import numpy as np
import matplotlib.pyplot as plt
Let
ts = np.linspace(2,8,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
Your answer:
ts = np.linspace(2,8,101)
xs = np.arctan(ts)
ys = np.log(ts)
X = np.vstack([xs,ys]).T
X.mean(axis=0)
array([1.3451008 , 1.53998402])
Let
A = np.ones((5,5)) + 2 * np.eye(5)
v = np.ones((5,))
Is v
an eigenvector of A
? If yes, what is the corresponding eigenvalue?
Your answer:
A = np.ones((5,5)) + 2 * 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: 7.0
Let
$$ A = \begin{bmatrix} 1 & 2 \\ 2 & 2 \end{bmatrix}. $$Find
$$ \min_{\|\bx\| = 1} \bx\trans A\bx $$and the $\bx$ that achieves this minimum value.
Your answer:
A = np.array([[1,2],
[2,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: -0.5615494261324032 achieved by: [-0.78764736 0.61612631]
Let
B = np.array([[0,1,2],
[1,0,1],
[2,1,0]])
C = np.ones((10,)) + 2 * 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:
B = np.array([[0,1,2],
[1,0,1],
[2,1,0]])
C = np.ones((10,)) + 2 * np.eye(10)
A = np.kron(B,C)
mask = (A != 0)
np.sum(1 / A[mask])
466.66666666666663
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 = 2\}$.
Your answer:
X = np.arange(500).reshape(250,2)
y = np.arange(250) % 5
X[y == 2].mean(axis=0)
array([249., 250.])
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 $1.5$.
Your answer:
xs = np.linspace(-2,2,100)
ys = np.sin(xs) + 1
X = np.vstack([xs,ys]).T
mask = (np.linalg.norm(X, axis=1) < 1.5)
mask.sum()
48
Let
$$ A = \begin{bmatrix} 1 & 1 \\ 1 & 2 \\ 1 & 3 \\ 1 & 4 \end{bmatrix} \text{ and } \by = \begin{bmatrix} 3 \\ 3 \\ 3 \\ 4 \end{bmatrix}. $$Find the vector $\bc$ such that $\|A\bc - \by\|$ is minimized.
Your answer:
A = np.array([[1,1],
[1,2],
[1,3],
[1,4]])
y = np.array([3,3,3,4])
ATAinv = np.linalg.inv(A.T.dot(A))
c = ATAinv.dot(A.T).dot(y)
c
array([2.5, 0.3])
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} 3 \\ 4 \\ 5 \end{bmatrix}$.
Your answer:
Q = np.array([[1,2,2],
[-2,1,1],
[0,-5,1]])
Q = Q / np.linalg.norm(Q, axis=0)
v_beta = np.array([3,4,5])
Q.dot(v_beta)
array([ 6.88471718, 0.08825662, -1.61024226])
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:
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,:]
array([[ 5, 6, 7, 8], [ 6, 7, 8, 9], [ 7, 8, 9, 10]])
Let
p = np.array([0.9] + [0.1 / 99] * 99)
and $p_i$ the entries of p
. Find
Your answer:
p = np.array([0.9] + [0.1 / 99] * 99)
np.sum(p * (1 - p))
0.18989898989898993
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:
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
array([0.21428571, 0.57142857, 0.21428571])
Exam ends here.
Total point = 20 (+2)
Your score: