Generate matrices A, with random Gaussian entries, B, a Toeplitz matrix, where A∈Rn∗m A ∈ R n ∗ m and B∈Rm∗m B ∈ R m ∗ m , for n=200 n = 200 , m=500 m = 500 .
import numpy as np
import scipy.linalg as sy
import time
# Prepare
print("Prepare")
n = 200
m = 500
A = np.asmatrix(np.random.normal(0, 1, (n, m)))
B = sy.toeplitz(range(m))
print("A:")
print(A)
print("B:")
print(B)
Exercise 9.1: Matrix operations
Calculate A+A,AAT,ATA A + A , A A T , A T A and AB A B . Write a function that computes A(B−λI) A ( B − λ I ) for any λ λ .
# Exe 9.1
print("Exe 9.1")
print('A+A=:')
print(A + A)
print("A*A'=:")
print(A * A.T)
print("A'*A=:")
print(A.T * A)
def cal(lamb_da):
return A * (B - lamb_da * np.eye(m, m))
lam = input("input lambda:")
print(cal(int(lam)))
Exercise 9.2: Solving a linear system
Generate a vector b with m entries and solve Bx=b B x = b .
# Exe 9.2
print("Exe 9.2")
b = np.asmatrix(np.random.random_integers(0, m, (m, 1)))
print(np.linalg.solve(B, b))
Exercise 9.3: Norms
Compute the Frobenius norm of A:||A||F A : | | A | | F and the in nity norm of B:||B||∞ B : | | B | | ∞ . Also nd the largest and smallest singular values of B.
# Exe 9.3
print("Exe 9.3")
U, S, VT = np.linalg.svd(B)
print(np.linalg.norm(A))
print(np.linalg.norm(B, np.inf))
print(S.max(), S.min())
Exercise 9.4: Power iteration
Generate a matrix Z,n∗n Z , n ∗ n , with Gaussian entries, and use the power iteration to nd the largest eigenvalue and corresponding eigenvector of Z Z . How many iterations are needed till convergence?
Optional: use the time.clock()
method to compare computation time when varying n.
# Exe 9.4
print("Exe 9.4")
while n < 2400:
Z = np.asmatrix(np.random.normal(10, 0.1, (n, n)))
beg = time.clock()
eigvals, eigvts = np.linalg.eig(Z)
end = time.clock()
eigvts = eigvts.T
iter = zip(eigvals, eigvts)
maximum = eigvals[0], eigvts[0]
for item in iter:
if item[0] > maximum[0]: maximum = item
print("largest eigenvalue: ", maximum[0])
print("corresponding eigenvector: ", maximum[1])
print("n: " + str(n) + " time: " + str(end - beg))
n += 200
Exercise 9.5: Singular values
Generate an n∗n 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?
# Exe 9.5
print("Exe 9.5")
for i in range(10):
n = np.random.randint(200, 600)
p = np.random.random()
C = np.asmatrix(np.random.binomial(1, p, (n, n)))
print("n: ", n, "p: ", p, "largest: ", n * p, sy.svd(C)[1].max())
结论:p ≈ largest singular value
Exercise 9.6: Nearest neighbor
Write a function that takes a value z and an array A and nds 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 nd this value manually. In particular, use brackets and argmin.
# Exe 9.6
print("Exe 9.6")
for i in range(10):
n = np.random.randint(200, 600)
A = np.random.normal(n, 12, (n, n))
z = n
A = np.fabs(A - np.asarray([np.ones(n) * z for j in range(n)]))
index = np.argmin(A)
print(z, A[index // n, index % n] + z)