import numpy as np
from scipy.linalg import toeplitz
A = np.random.normal(0,1,size=(200,500))
B = toeplitz(np.random.random((500)),np.random.random((500)))
[[-1.08139799 1.63626865 -1.19375553 ... 0.54016655 1.04225555
-0.13933849]
[-0.8328577 0.06145153 -0.41176367 ... -0.6699105 0.82843077
-1.96984931]
[ 0.84787184 -0.91498802 0.0050716 ... -1.22836145 -0.20244569
-1.39372778]
...
[-1.47544982 -0.63704547 -0.94399656 ... 0.9433685 -0.45693745
-2.27505334]
[-1.85360841 1.50529954 -0.09037166 ... 1.50275772 0.89649159
-1.42276638]
[-1.71975293 0.03681571 1.29264051 ... -0.45049806 -0.99695241
0.30931893]]
[[0.77901348 0.53469863 0.58073141 ... 0.49282411 0.26355095 0.7060844 ]
[0.49807365 0.77901348 0.53469863 ... 0.73799689 0.49282411 0.26355095]
[0.58240378 0.49807365 0.77901348 ... 0.46331682 0.73799689 0.49282411]
...
[0.37861052 0.91589719 0.19010318 ... 0.77901348 0.53469863 0.58073141]
[0.78174467 0.37861052 0.91589719 ... 0.49807365 0.77901348 0.53469863]
[0.30388698 0.78174467 0.37861052 ... 0.58240378 0.49807365 0.77901348]]
编写代码如下,分别打印出所求结果,定义函数compute计算值。
print(A + A)
print(A.dot(A.T))
print(A.T.dot(A))
print(A.dot(B))
def compute(A, B, lamda):
return A.dot(B - lamda * np.eye(5))
创建向量b并求解。
b = np.random.random(5)
x = np.linalg.solve(B,b)
#x = np.linalg.inv(B).dot(b)
print(x)
调用函数分别计算A的Frobenius范数(各元素平方和开方)和B的无穷范数(行绝对值和最大值),利用svd分解得到B的所有奇异值,分别打印最值。
normFro = np.linalg.norm(A, 'fro')
normInf = np.linalg.norm(B, np.inf)
u, s, vh = np.linalg.svd(B)
print(max(s), min(s))
import numpy as np
A = np.random.normal(size=(5,5))
v = np.ones(5)
epi = 1e-5
delta = 1
k = 0
while 1:
u = v / max(v) #得到规范化向量
p = v
v = A.dot(u)
delta = abs(max(v) - max(p))#比较是否达到精度
k += 1
if delta < epi: break
print(k)
print(max(v)) #最大分量为主特征值
print(v / max(v)) #规范化向量即为主特征值对应的特征向量
对于5*5矩阵,迭代次数大多为100以内,少数收敛速度慢或不收敛则需要100000次以上。
========================== RESTART: D:/python/c.py ==========================
20
2.4852607075818263
[ 1. 0.15200074 0.3943235 0.3377595 -0.07979286]
>>>
========================== RESTART: D:/python/c.py ==========================
203360
1.3469029196104048
[ 0.99642544 -0.1414458 1. -0.5099528 -0.78919265]
>>>
========================== RESTART: D:/python/c.py ==========================
73
2.816940007693461
[ 1. 0.34605648 -0.04584567 -0.42430719 0.33601158]
>>>
========================== RESTART: D:/python/c.py ==========================
47
1.9621261489192796
[ 0.15254321 0.99335278 1. -3.51655808 0.12706556]
>>>
import numpy as np
from scipy.linalg import svd
import random
def get_c(n, p):
C = np.zeros((n,n))
for i in range(n):
for j in range(n):
C[i][j] = 1 if random.random() < p else 0
return C
for i in range(11):
C = get_c(10, 0.1 * i)
u, s, vh = svd(C)
print('p = ', 0.1 * i, 'max singular value is ', max(s))
由以下输出可看出,p越大,最大奇异值越大。
p = 0.0 max singular value is 0.0
p = 0.1 max singular value is 2.243169319983191
p = 0.2 max singular value is 2.668745545242216
p = 0.30000000000000004 max singular value is 3.31429371905275
p = 0.4 max singular value is 5.294757119546733
p = 0.5 max singular value is 6.653159594060823
p = 0.6000000000000001 max singular value is 7.115519446727156
p = 0.7000000000000001 max singular value is 7.426687969549355
p = 0.8 max singular value is 8.626913866418564
p = 0.9 max singular value is 9.180638085275236
p = 1.0 max singular value is 10.000000000000002
>>>
思路:先将A的每个元素减去z,取绝对值,利用argmin取得下标即可。
def get_closest(A, z):
col = np.shape(A)[1]
pos = np.argmin(abs(A - z))
return round(A[pos // col][pos % col], 8)
A = np.random.randint(0, 10, size = (5,4))
print(A)
print(get_closest(A,5.6))
[[5 7 7 9]
[6 5 1 5]
[4 6 8 5]
[6 3 1 5]
[4 6 5 2]]
6
>>>