生成高斯分布矩阵和托普利兹矩阵可以使用python的numpy库和scipy库的函数实现。
首先是生成两个矩阵
import numpy as np
from scipy import linalg
# 生成高斯分布矩阵
A = np.random.normal(loc= 100, scale= 100, size = (200,500))
x = [i for i in range(1,501)]
x.reverse()
y = [i for i in range(500, 1000)]
# 生成托普利兹矩阵
B = linalg.toeplitz(x, y)
print("A = ")
print(A)
print("B = ")
print(B)
生成的结果如图
exercise 1
import numpy as np
from scipy import linalg
A = np.random.normal(loc= 100, scale= 100, size = (200,500))
x = [i for i in range(1,501)]
x.reverse()
y = [i for i in range(500, 1000)]
B = linalg.toeplitz(x, y)
A = np.mat(A)
B = np.mat(B)
print("A = ")
print(A)
print("B = ")
print(B)
print("A + A = ")
print(A + A)
print("A * A's transpose = ")
print(A * np.transpose(A))
print("AB = ")
print(A * B)
x = input("Please input a constant x ")
x = int(x)
I = np.mat(np.eye(500, 500), dtype = int) #生成单位矩阵
print("A(B - xI) = ")
print(A * (B - x * I))
先生成A、B矩阵,然后使用numpy里面定义的矩阵运算方法计算出A+A,A乘A的转置,AB,A(B-xI)
运行结果:
Exercise 2
Bx = b, B^(-1) Bx= B^(-1) b ,x= B^(-1) b
只要求出B的逆和b相乘就可以算出x
import numpy as np
from scipy import linalg
A = np.random.normal(loc= 100, scale= 100, size = (200,500))
x = [i for i in range(1,501)]
y = [i for i in range(1,501)] #之前的B矩阵没有逆,所以换了一个
B = linalg.toeplitz(x, y)
A = np.mat(A)
B = np.mat(B)
print("B = ")
print(B)
b = np.array(range(500))
print('b = ')
print(b)
b.shape = (500,1) # 转换成列向量
print("x = ")
print(B.I * b)
结果如下
x 有500 行,这里就不上全图了
Exercise 3
import numpy as np
from scipy import linalg
A = np.random.normal(loc= 100, scale= 100, size = (200,500))
x = [i for i in range(1,501)]
y = [i for i in range(1,501)]
B = linalg.toeplitz(x, y)
A = np.mat(A)
B = np.mat(B)
A_F = np.linalg.norm(A, 'fro')
print("the Frobenius norm:", A_F)
B_F = np.linalg.norm(B, np.inf)
print("the infinity norm:", B_F)
#find the largest and smallest singular values of B
large = np.linalg.norm(B, 2)
small = np.linalg.norm(B, -2)
print("the largest singular:", large)
print("the smallest singular:", small)
运行结果:
the Frobenius norm: 44756.26340030189
the infinity norm: 125250.0
the largest singular: 87334.52045641867
the smallest singular: 0.5000049348345748
Exercise 4
import time
Z = np.random.normal(loc = 100, scale = 50,size = (200, 200))
num = 0
u_k = np.ones(200)
v_k_norm = 0
v_k = np.zeros(200)
start = time.clock()
while(True):
v_k = np.dot(Z, u_k)
v_k_norm_temp = v_k_norm
v_k_norm = np.linalg.norm(v_k)
u_k = v_k / v_k_norm
num += 1
if(abs(v_k_norm_temp - v_k_norm) < 0.0005):
break;
end = time.clock()
print("The largest eigenvalue:", v_k_norm)
print("The corresponding eigenvector:", u_k)
#How many iterations are needed till convergence
print("iterations number : ", num)
#Optional: use the time.clock() method to compare computation time when varying n.
print("computation time :", end - start)
运行情况
Exercise 5
p = 0.6
C = np.random. binomial(1, p, (200, 200))
#Compute the singular values of C
large = np.linalg.norm(C, 2)
small = np.linalg.norm(C, -2)
print("The smallest singular:", small)
print("The largest singular:", large)
# the relationship between n, p and the largest singular value
print("n * p:", 200*p)
if abs(200*p - large) < 1:
print("the largest singular is nearly equal to n * p")
运行结果
Exercise 6
import numpy as np
def fun_closest(A, z):
B, C = A[A>z], A[A<=z]
top, bot = 0, 0
if(len(B)):
top = np.argmin(B)
else:
return C[np.argmax(C)]
if(len(C)):
bot = np.argmax(C)
else:
return B[top]
if(abs(B[top]-z) < abs(C[bot]-z)):
return B[top]
else:
return C[bot]
A = np.random.normal(loc= 100, scale= 100, size = (200,500))
print("the closest value to 1000 is :", fun_closest(A, 1000))
print("the closest value to 100 is :", fun_closest(A, 100))
print("the closest value to 1 is :", fun_closest(A, 1))
print("the closest value to -1000 is :", fun_closest(A, -1000))
测试情况;