Numpy 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 λ.
#9-1
#! usr/bin/python
# -*- coding: UTF-8 -*-
import numpy as np
from scipy.linalg import toeplitz
A = np.random.randn(200,500)
B = toeplitz(range(0,500))
def cal(a,b,k):
temp = int(k) * np.eye(500,dtype=int)
print('A(B-λI)=',np.dot(a,b-temp))
print('矩阵A')
print(A)
print('矩阵A+矩阵A')
print(A + A)
print('矩阵A*矩阵A的转置')
print(np.dot(A , A.T))
print('矩阵A的转置*矩阵A')
print(np.dot(A.T , A))
print('矩阵A*矩阵B')
print(np.dot(A , B))
k = input('请输入λ,计算A(B-λI)')
cal(A,B,k)
Exercise 9.2: Solving a linear system Generate a vector b with m entries and solve Bx = b.
#9-2
#! usr/bin/python
# -*- coding: UTF-8 -*-
import numpy as np
from scipy.linalg import toeplitz
A = np.random.randn(200,500)
B = toeplitz(range(0,500))
b = np.random.rand(500)
print('Solve Bx = b')
print(np.linalg.solve(B,b))
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.
#9-3
#! usr/bin/python
# -*- coding: UTF-8 -*-
import numpy as np
from scipy.linalg import toeplitz
A = np.random.randn(200,500)
B = toeplitz(range(0,500))
print('A`s Frobeniums norm = ',np.linalg.norm(A,'fro'))
print('B`s infinity norm = ',np.linalg.norm(B,np.inf))
print('B`s the largest singular value = ',np.linalg.norm(B,2))
print('B`s the smallest singular value = ',np.linalg.norm(B,-2))
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.
#9-4
import numpy as np
from scipy.linalg import toeplitz
import time
n = int(input('Please input "n".'))
Z = np.random.randn(n, n)
Z = np.mat(Z)
A = np.random.randn(200,500)
B = toeplitz(range(0,500))
u = np.random.rand(n)
u = np.mat(u)
u = u.T
v = u
num = 0
norm = 1
now = norm + 1
begin = time.clock()
while True:
now = norm
v = Z * u
norm = np.max(v)
u = v * (1 / norm)
num += 1
if np.fabs(now - norm) < 0.0001:
break
end = time.clock()
print("Iterate times: ",num)
print("The largest eigenvalue: ", norm)
print("Eigenvector: ",u)
print("Time cost: ", end - begin,'Second')
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?
#9-5
import numpy as np
from scipy.linalg import toeplitz
A = np.random.randn(200,500)
B = toeplitz(range(0,500))
p = 0.5
C = np.random.binomial(1,p,(200,200))
print('The smallest singular:', np.linalg.norm(C,2))
print('The largest singular:', np.linalg.norm(C, -2))
print('n * p = ', 200*p)
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-6
import numpy as np
from scipy.linalg import toeplitz
A = np.random.randn(200,500)
B = toeplitz(range(0,500))
def Nearest(A,n):
Min = min(A[A>n])
Max = max(A[A<=n])
if abs(Min-n) > abs(Max-n):
return Max
else:
return Min
print(Nearest(A,0))