题目:
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, A(A^T),(A^T)A and AB. Write a function that computes A(B−λI) for any λ.
import numpy as np
from scipy.linalg import toeplitz
def calculate(A, B, num):
l = len(B)
m = np.eye(l)
print(A * (B - num * m))
#create the matrix A and B
A = np.random.randn(200, 500)
A = np.mat(A)
temp1 = np.random.rand(500)
temp2 = np.random.rand(500)
B = toeplitz(temp1, temp2)
B = np.mat(B)
print(A)
print(B)
print(A + A) #A+A
print(A * (A.T)) #A*A^T
print(A.T * A) #A^T*B
print(A * B) #A+B
m = float(input("Input a number"))
calculate(A, B, 6)
Exercise 9.2: Solving a linear system Generate a vector b with m entries and solve Bx = b.
import numpy as np
from scipy.linalg import toeplitz
A = np.random.randn(200, 500)
A = np.mat(A)
temp1 = np.random.rand(500)
temp2 = np.random.rand(500)
B = toeplitz(temp1, temp2)
B = np.mat(B)
b = np.random.rand(500)
x = np.linalg.solve(B, b)
print(x)
Exercise 9.3: Norms Compute the Frobenius norm of A: ||A||k and the infinity norm of B: ||B||∞. Also find the largest and smallest singular values of B.
import numpy as np
from scipy.linalg import toeplitz
def Frobenius(A):
sum = 0
for i in range(200):
for j in range(500):
sum += A[i, j] ** 2
return np.sqrt(sum)
def Infinity(B):
max = 0
for i in range(500):
sum = 0
for j in range(500):
sum += np.fabs(B[i, j])
if sum > max:
max = sum
return sum
def Find_min(B):
min = B[0, 0]
for i in range(500):
for j in range(500):
if min > B[i, j]:
min = B[i, j]
return min
A = np.random.randn(200, 500)
A = np.mat(A)
temp1 = np.random.rand(500)
temp2 = np.random.rand(500)
B = toeplitz(temp1, temp2)
B = np.mat(B)
'''
print(np.linalg.norm(A,"fro")) #F范数
print(np.linalg.norm(B, np.inf)) #无穷范数
print(np.min(B)) #矩阵中最小值
'''
print(Frobenius(A))
print(Infinity(B))
print(Find_min(B))
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.
import numpy as np
from scipy.linalg import toeplitz
Z = np.random.randn(200, 200)
Z = np.mat(Z)
u = np.random.rand(200)
u = np.mat(u)
u = u.T
v = u
k = 0
eig = 1
last = eig + 1
while np.fabs(last - eig) > 1e-4:
last = eig
v = Z * u
eig = np.max(v)
u = v * (1 / eig)
k += 1
a,b=np.linalg.eig(Z)
print(k)
print(eig)
print(u)
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?
import numpy as np
import scipy.linalg
def solve(n, p):
C = np.eye(n)
for i in range(n):
for j in range(n):
if np.random.rand() < p:
C[i][j] = 1
else:
C[i][j] = 0
a, b, c = scipy.linalg.svd(C)
print("n = " + str(n))
print("p = " + str(p))
print("singular = " + str(max(b)))
for i in range(10, 100):
for j in range(10):
solve(i, float(j/10))
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
def find_closest():
A = np.random.rand(200)
z = np.random.rand()
min = 10000.0
num = 0
print(A)
print(z)
for i in A:
if np.fabs(z-i) < min:
min = np.fabs(z-i)
num = i
return num
print(find_closest())