Generate matrices A, with random Gaussian entries, B, a Toeplitz matrix, where A ∈ R n×m and B ∈ R m×m , for n = 200, m = 500.
Exercise 9.1:
Matrix operations Calculate A + A, AA > , A > A and AB. Write a function that computes A(B − λI) for any λ.
solution:
import numpy
import random
from scipy.linalg import toeplitz
import scipy
#####9.1
A= numpy.random.randint(0, 10, (200,500))
c = numpy.random.randint(0, 10, (500, 1))
r = numpy.random.randint(0, 10, (1, 500))
B = toeplitz(c, r)
print('A:', A)
print('B:', B)
#A+A
print(A + A)
#AA(T)
print(numpy.dot(A, A.T))
#A(T)A
print(numpy.dot(A.T, A))
#AB
print(numpy.dot(A,B))
#A(B − λI)
def fun1(A, B, tmp):
return numpy.dot(A, B - tmp* numpy.ones(dtype=int, shape = B.shape))
print(fun1(A, B, 1))
Exercise 9.2:
Solving a linear system Generate a vector b with m entries and solve Bx = b.
solution:
#####9.2
b = numpy.random.randint(0, 10, 500)
print(b)
BI = numpy.mat(B).I
print(BI)
print(numpy.dot(b, BI))
Exercise 9.4:
Power iteration 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.
solution:
#####9.4 power_iteration
n = input('input n: ')
n = int(n)
Z = numpy.random.randint(0, 10, (n, n))
def power_iteration(Z):
time = 0
b = numpy.random.randint(Z.shape[1])
while True:
b1 = numpy.dot(Z, b)
norm = numpy.linalg.norm(b1)
b = b1 / norm
norm2 = numpy.linalg.norm(b)
if abs(norm - norm2) / norm <= 0.00001:
break
time += 1
print('iteration time:', time)
print(b)
power_iteration(Z)
Exercise 9.3:
Norms Compute the Frobenius norm of A: kAk F and the infinity norm of B: kBk ∞ . Also find the largest and smallest singular values of B.
solution:
#####9.3
print(numpy.linalg.norm(A))
print(numpy.linalg.norm(B, ord=numpy.inf))
print('largest:', numpy.linalg.norm(B, ord=2))
print('smallest:', numpy.linalg.norm(B, ord=-2))
Exercise 9.5:
Singular values 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?
solution:
#####9.5 Singular values
n = int(input('input n: '))
#C = numpy.zeros((n,n))
C = []
p = float(input('input P: '))
for i in range(0, n):
tmp = []
for j in range(0, n):
if random.uniform(0, 1) > p:
tmp.append(0)
else:
tmp.append(1)
C.append(tmp)
U, sigma, VT = scipy.linalg.svd(C)
print(sigma)
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.#####9.6Nearest neighbor
AA = numpy.random.randint(0, 10, 10)
print(AA,len(AA))
print(AA)
z = int(input('input z: '))
BB = [abs(i - z) for i in AA]
print(BB)
print('num', AA[numpy.argmin(B)])