Generate matrices A, with random Gaussian entries, B, a Toeplitz matrix, where A 2 Rn×m and B 2 Rm×m,
for n = 200, m = 500.
Exercise 9.1: Matrix operations
Calculate A + A, AAT; ATA and AB. Write a function that computes A(B − λI) for any λ.
import numpy as np
import scipy.linalg as lp
n = 200
m = 500
A = np.random.normal(size=(n,m))
B = lp.toeplitz(list(range(0,m)))
print('A + A')
print(A + A)
print()
print('AAT')
print(np.dot(A,A.T))
print('ATA')
print(np.dot(A.T,A))
print()
print("AB")
print(np.dot(A,B))
print()
def n1(A,B,k):
t = np.eye(m)
print(np.dot(A , B - k * t))
n1(A,B,5)
Exercise 9.2: Solving a linear system
Generate a vector b with m entries and solve Bx = b .
import numpy as np
import scipy.linalg as lp
n = 200
m = 500
A = np.random.normal(size=(n,m))
B = lp.toeplitz(list(range(0,m)))
b = np.random.normal(size=m)
print(np.linalg.solve(B,b))
Exercise 9.3: NormsCompute 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.
import numpy as np
import scipy.linalg as lp
n = 200
m = 500
A = np.random.normal(size=(n,m))
B = lp.toeplitz(list(range(0,m)))
print(lp.norm(A, 'fro'))
print(lp.norm(B, np.inf))
temp = lp.svdvals(B)
print('Max:')
print(max(temp))
print('Min:')
print(min(temp))
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.
import numpy as np
import scipy.linalg as lp
import time
n = 200
m = 500
iteration = 0
max_svd = 0
me = np.random.randn(n)
Z = np.random.normal(size=(n, n))
begin = time.clock()
while 1:
n = max_svd
v = Z.dot(me)
max_svd = np.max(v)
me = np.true_divide(v, max_svd)
iteration += 1
if(abs(max_svd - n) < 0.001):
break
end = time.clock()
print('Used Time: ', end = '')
print(end - begin)
print(max_svd)
print(me)
print(iteration)
Exercise 9.5: Singular valuesGenerate 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?
import numpy as np
import scipy.linalg as lp
import math
n = 200
m = 500
p = 0.5
C = np.random.binomial(1, p, (n, n))
U, svd, Vh = lp.svd(C)
print("Max SVD:\n\t", end = '')
print(max(svd))
print("n * p:\n\t", end = '')
print(n * p)
print("They are almost equal.")
Exercise 9.6: Nearest neighbor
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.
Hint: Use the built-in functionality of Numpy rather than writing code to find this value manually. In
particular, use brackets and argmin .
import numpy as np
import scipy.linalg as lp
n = 200
m = 500
A = np.random.normal(size=(n,m))
B = lp.toeplitz(list(range(0,m)))
def n6(A, z):
return A[np.argmin([abs(A[i] - z) for i in range(len(A))])]
print(n6([np.random.normal(0, 10) for i in range(1000)], 1))