import numpy
from scipy import linalg
import time
A = numpy.random.normal(size=(200, 500))
B = linalg.toeplitz(list(range(0,500)))
'''
1
def ex1(A,B,X):
I = numpy.eye(500)
print(numpy.dot(A,B-X*I))
print(A+A)
print("\n\n")
print(numpy.dot(A,A.T))
print("\n\n")
print(numpy.dot(A.T,A))
print("\n\n")
print(numpy.dot(A,B))
print("\n\n")
ex1(A,B,3)
'''
'''
2
b = numpy.arange(500,0,-1)
x = numpy.linalg.solve(B,b)
print(x)
'''
'''
3
print(linalg.norm(A, 'fro'))
print(linalg.norm(B, numpy.inf))
b = linalg.svdvals(B)
print(max(b))
print(min(b))
'''
'''
4
def ex4(n):
Z = numpy.random.normal(0, 1, n*n).reshape(n, n) # Z矩阵
v = numpy.array(list(range(1, n+1))[::-1]) # 初始向量
a, tempa = linalg.norm(v,numpy.inf), 0 # 特征值
x = 0 # 记录次数
t = time.clock() # 记录时间
while True:
tempa = a
a = linalg.norm(y, numpy.inf)
y = numpy.dot(Z, v)
v = y / a
x += 1
if abs(tempa - a) < 0.00001:# 判停条件
break
t = time.clock() - t
return x, t, v, a
result = ex4(10)
print("次数为:" + str(result[0]))
print("时间为:" + str(result[1]))
print("特征向量为:" + str(result[2]))
print("特征值为:" + str(result[3]))
'''
'''
5
def ex5(n, p):
return max(linalg.svdvals(numpy.random.binomial(1, p, [n,n])))
print(ex5(10, 0.3))
print(ex5(100, 0.3))
print(ex5(1000, 0.3))
#越来越接近np
'''
'''
6
a = min(A[A>0.5])
b = max(A[A<=0.5])
x = b if abs(a-0.5) > abs(b-0.5) else a
print(x)
'''
python作业之numpy and scipy
最新推荐文章于 2020-06-04 21:05:19 发布