Calculate A + A, AA>; A>A and AB. Write a function that computes A(B - λI) for any λ.
Generate a vector b with m entries and solve Bx = b.
smallest singular values of B.
Optional: use the time.clock() method to compare computation time when varying n.
relationship between n, p and the largest singular value?
particular, use brackets and argmin.
代码
import time
import numpy as np
from scipy import linalg
from scipy.linalg import svdvals
from scipy.linalg import toeplitz
A=np.random.randn(200,500)
z=list(range(0,500))
B=toeplitz(z)
'''
exercise 1
print(A+A)
print(np.dot(A,A.T))
print(np.dot(A.T,A))
print(np.dot(A,B))
def compute(A,B,y):
I=np.eye(500)
print(np.dot(A,(B-y*I)))
compute(A,B,2)
'''
'''
exercise 2
b = np.arange(500,0,-1)
x = b/B
print(x)
'''
'''
exercise 3
b = svdvals(B)
print(linalg.norm(A, 'fro'))
print(linalg.norm(B, np.inf))
print("最大奇异值:",max(b))
print("最小奇异值:",min(b))
'''
'''
exercise 4
def ex4():
Z = np.mat(np.random.rand(200,200))
v = np.mat(np.ones(200)) #初始向量
n = 0 #迭代次数
v = v.T
x = v
t_ = 0
ei = linalg.norm(x,ord=np.inf) #特征值
time_ = time.clock()
while np.abs(t_-ei)>1e-4:
t_ = ei
x = Z * v
ei = linalg.norm(x,ord=np.inf)
v = x/ei
n = n+1
time_ = time.clock()-time_
return n, time_, v, ei
result = ex4()
print("iterations:" , result[0])
print("Time: " ,result[1])
print("corresponding eigenvector: " , result[2])
print("eigenvalue: " ,result[3])
'''
'''
exercise 5
def Singular(n,p):
C=np.random.binomial(1,p,[n,n])
b=svdvals(C)
return max(b)
print(Singular(10,0.1))
print(Singular(100,0.1))
print(Singular(1000,0.1))
print("\n")
print(Singular(100,0.1))
print(Singular(100,0.2))
print(Singular(100,0.3)) #最大奇异值很接近np
'''
'''
exercise 6
def exercise6(x):
a = min(A[A>x])
b = max(A[A<=x])
x = b if abs(a-x) > abs(b-x) else a
print(x)
exercise6(0.5)
'''