import numpy as np
from scipy.linalg import toeplitzimport
A = np.random.normal(100,20,200*500).reshape([200,500])
print("A=")
print(A)
B = toeplitz(range(1,501))
print("\nB=")
print(B)
import numpy as np
from scipy.linalg import toeplitz
def fun(a,b,i):
return np.dot(a,b-i*np.eye(500))
A = np.random.normal(100,20,200*500).reshape([200,500])
print("A=")
print(A)
B = toeplitz(range(1,501))
print("\nB=")
print(B)
print("\nA+A=")
print(A+A)
print("\nA*AT=")
print(A.dot(A.T))
print("\nAT*A=")
print((A.T).dot(A))
print("\nA*B=")
print(A.dot(B))
print("\nA*(B-3*I)=")
print(fun(A,B,3))
import numpy as np
from scipy.linalg import toeplitz
B = toeplitz(range(1,501))
print("\nB=")
print(B)
b = np.random.normal(100,20,500)
print("\nb=")
print(b)
print("\nx=")
x = np.linalg.solve(B, b)
print(x)
import numpy as np
from scipy.linalg import toeplitz
A = np.random.normal(100,20,200*500).reshape([200,500])
print("A=")
print(A)
print("\n|A|F=")
print(np.linalg.norm(A,ord='fro'))
B = toeplitz(range(1,501))
print("\nB=")
print(B)
print("\n|B|=")
print(np.linalg.norm(B,ord=np.inf))
l = np.linalg.eigvals(B)
print("\nLargest=")
print(np.max(l))
print("\nSmallest=")
print(np.min(l))
#伪算法
#假设初始值x0=零向量
#循环迭代条件可以设置为本次计算出来的y与上一次最大值的差距
#Given x0
#for i=1,2,3……until converge do
# yi=Ax(i-1)
# xi=yi/||yi||2
#end for
#计算后的x为特征向量
#y1的最大值为最大特征值
#num为迭代次数
#time为迭代时间
import numpy as np
import time
n=100
Z = np.random.normal(10,1,n*n).reshape([n,n])
print("Z=")
print(Z)
x=np.ones(n)
y1=np.zeros(n)
y2=np.ones(n)
t=time.clock()
num=0
while abs(np.max(y1-y2))>0.00001:
num=num+1
y2 = y1
y1 = np.dot(Z,x)
x = y1/np.linalg.norm(y1, ord=2)
t1=time.clock()
print("\ntotal time=")
print(t1-t)
print("\ntotal number=")
print(num)
print("\nlargest eigenvalue=")
print(np.max(y1))
print("\n corresponding eigenvector of Z=")
print(x)
n=100时
n=1000时
import numpy as np
for n in (10,50,100):
for p in (0.2,0.5,0.8):
print("n = %d, p = %.3f" % (n,p))
C = np.random.binomial(1,p,(n,n))
print(np.max(np.linalg.eigvals(C)))
可以看到结果随着n与p成倍数增长
import numpy as np
def closest(A,z):
B = A - z*np.ones(len(A))
idx = np.argmin(np.abs(B))
return A[idx]
A=[3,23,13,24]
z=10
print(closest(A,z))