Generate matrices
A
, with random Gaussian entries,
B
, a Toeplitz matrix, where
A
2
R
n
×
m
and
B
2
R
m
×
m
,
Compute the Frobenius norm of A : k A k F and the infinity norm of B : k B k 1 . Also find the largest and
Generate a matrix Z , n × n , with Gaussian entries, and use the power iteration to find the largest
eigenvalue and corresponding eigenvector of Z . How many iterations are needed till convergence?
Generate an n × n matrix, denoted by C , where each entry is 1 with probability p and 0 otherwise. Use
the linear algebra library of Scipy to compute the singular values of C . What can you say about the
Write a function that takes a value z and an array A and finds the element in A that is closest to z . The
Exercise 9.3: Norms
for n = 200, m = 500.
Calculate A + A, AA>; A>A and AB. Write a function that computes A(B - λI) for any λ.
Generate a vector b with m entries and solve Bx = b.
Compute the Frobenius norm of A : k A k F and the infinity norm of B : k B k 1 . Also find the largest and
smallest singular values of B.
Generate a matrix Z , n × n , with Gaussian entries, and use the power iteration to find the largest
eigenvalue and corresponding eigenvector of Z . How many iterations are needed till convergence?
Optional: use the time.clock() method to compare computation time when varying n.
Generate an n × n matrix, denoted by C , where each entry is 1 with probability p and 0 otherwise. Use
the linear algebra library of Scipy to compute the singular values of C . What can you say about the
relationship between n, p and the largest singular value?
Write a function that takes a value z and an array A and finds the element in A that is closest to z . The
function should return the closest value, not index.
particular, use brackets and argmin
Code:
import numpy as np
import math
from scipy import linalg
import random
import time
n = 200
m = 500
num = n * m
a = []
for i in range(num):
a.append(random.gauss(0, 1))
A = np.mat(a)
A = A.reshape(n, m)
c = []
r = []
for i in range(m):
c.append(random.gauss(0, 1))
r.append(random.gauss(0, 1))
B = linalg.toeplitz(c, r)
B = np.mat(B)
Exercise 9.1: Matrix operations
res1 = A + A
res2 = np.dot(A, (A.T))
res3 = A.T
res4 = np.dot(A, B)
I = np.eye(m)
print(res1)
print(res2)
print(res3)
print(res4)
def solution1(inp):
B1 = B - inp * I
return np.dot(A, B1)
Exercise 9.2: Solving a linear system
b = []
for i in range(m):
b.append(random.gauss(0, 1))
res5 = np.dot(b, (B.I))
print(res5)
Exercise 9.3: Norms
A_norm = 0
for i in range(n):
for j in range(m):
A_norm = A_norm + A[i, j] * A[i, j]
A_norm = math.sqrt(A_norm)
B_norm = 0
B_max = 0
for i in range(m):
B_max = 0
for j in range(m):
B_max = B_max + B[i, j]
if B_max > B_norm:
B_norm = B_max
print(A_norm)
print(B_norm)
P, D, Q = linalg.svd(B)
B_max_svd = max(D)
B_min_svd = min(D)
print(B_max_svd)
print(B_min_svd)
Exercise 9.4: Power iteration
z = []
for i in range(n):
for j in range(n):
z.append(random.gauss(0, 1))
Z = np.mat(z)
Z = Z.reshape(n, n)
def power_iteration(Z):
v_0 = np.ones((1, n))
v_0 = np.mat(v_0)
v_0 = v_0.T
v_1 = np.ones((1, n))
v_1 = np.mat(v_1)
v_1 = v_1.T
m_1 = 1
m_2 = 0
counter = 0
while(abs(m_2 - m_1) > 10 ** -4):
m_2 = m_1
u_1 = Z * v_0
m_1 = max(u_1)
v_1 = u_1 / m_1
v_0 = v_1
counter = counter + 1
return counter, m_1, v_1
counter, Z_value, Z_vector = power_iteration(Z)
print(counter)
print(Z_value)
print(Z_vector)
Exercise 9.5: Singular values
def Singular_values(p):
C = []
for i in range(n):
for i in range(n):
if(p > random.random()):
C.append(1)
else:
C.append(0)
C = np.mat(C)
C = C.reshape(n, n)
U, sigma, V_T = linalg.svd(C)
C_max_sigma = max(sigma)
print(p)
print(sigma)
print(C_max_sigma)
Exercise 9.6: Nearest neighbor
def Nearest_neighbor(z, A):
return A[np.argmin(np.abs(A - z))]