原题矩阵规模太大了,这里为了方便截图检验,设置n = 2,m = 5
Exercise 9.1:
代码:
import numpy as np
from scipy.linalg import toeplitz
def compute(A, B, lamda):
return np.dot(A, B - lamda * np.eye(B.shape[1]))
A = np.random.randn(2,5)
x = np.random.randn(1,5)
y = np.random.randn(1,5)
B = toeplitz(x, y)
print("Matrix A:")
print(A)
print("Matrix B:")
print(B)
############
print("A+A:")
print(A + A)
print("AAT:")
print(np.dot(A, A.T))
print("ATA:")
print(np.dot(A.T, A))
print("AB:")
print(np.dot(A, B))
print("A(B-I):")
print(compute(A, B, 1))
结果:
Matrix A:
[[-1.39612299 1.96146785 0.09048303 1.27988762 1.105836 ]
[-0.46515137 -1.04407125 0.5779924 0.1160498 2.09477522]]
Matrix B:
[[ 0.14963695 1.11204777 0.61889539 -0.57087541 -0.18088727]
[-0.75591586 0.14963695 1.11204777 0.61889539 -0.57087541]
[ 0.25316756 -0.75591586 0.14963695 1.11204777 0.61889539]
[-1.90517251 0.25316756 -0.75591586 0.14963695 1.11204777]
[ 0.56573786 -1.90517251 0.25316756 -0.75591586 0.14963695]]
A+A:
[[-2.79224597 3.9229357 0.18096606 2.55977524 2.21167201]
[-0.93030275 -2.0881425 1.1559848 0.2320996 4.18955043]]
AAT:
[[8.66568828 1.11880339]
[1.11880339 6.04207656]]
ATA:
[[ 2.16552519 -2.25279917 -0.39517939 -1.84086125 -2.51827063]
[-2.25279917 4.9374409 -0.4259857 2.38929416 -0.01803282]
[-0.39517939 -0.4259857 0.34226239 0.18288401 1.31082355]
[-1.84086125 2.38929416 0.18288401 1.65157988 1.65844405]
[-2.51827063 -0.01803282 1.31082355 1.65844405 5.61095648]]
AB:
[[-3.48150228 -3.11022727 0.64320593 1.46717664 0.77755674]
[ 1.82995388 -5.07194391 -0.91984288 -1.30398071 1.48040008]]
A(B-I):
[[-2.08537929 -5.07169512 0.5527229 0.18728902 -0.32827926]
[ 2.29510526 -4.02787266 -1.49783528 -1.42003051 -0.61437514]]
都是比较基本的运算调用。
Exercise 9.2:
代码:
import numpy as np
from scipy.linalg import toeplitz
x = np.random.randn(1,5)
y = np.random.randn(1,5)
B = toeplitz(x, y)
print("Matrix B:")
print(B)
b = np.random.randn(1,5).T
print("vector b:")
print(b)
print("solve Bx = b,x is")
print(np.linalg.solve(B, b))
结果:
Matrix B:
[[ 0.15356159 1.95578204 -0.39315247 -0.58093584 1.33742878]
[ 0.12695822 0.15356159 1.95578204 -0.39315247 -0.58093584]
[-1.65568047 0.12695822 0.15356159 1.95578204 -0.39315247]
[-0.25629207 -1.65568047 0.12695822 0.15356159 1.95578204]
[-1.46854658 -0.25629207 -1.65568047 0.12695822 0.15356159]]
vector b:
[[ 0.22128892]
[ 1.74450946]
[ 0.09007106]
[-0.22109333]
[ 0.36026202]]
solve Bx = b,x is
[[-1.17357604]
[ 0.12608276]
[ 0.71273766]
[-1.03671225]
[-0.12496665]]
调用了solve方法解线性方程组。
Exercise 9.3:
代码:
import numpy as np
from scipy.linalg import toeplitz
A = np.random.randn(2,5)
x = np.random.randn(1,5)
y = np.random.randn(1,5)
B = toeplitz(x, y)
print("Matrix A:")
print(A)
print("Matrix B:")
print(B)
############
print("the Frobenius norm of A:")
print(np.linalg.norm(A, ord = 'fro'))
print("the infinity norm of B:")
print(np.linalg.norm(B, ord = np.inf))
print("the largest singular value of B:")
print(np.linalg.cond(B, 2))
print("the smallest singular value of B:")
print(np.linalg.cond(B, -2))
结果:
Matrix A:
[[-0.34183582 0.02747836 -2.04670733 0.2444222 -0.07283119]
[ 1.85484887 0.34124865 0.91227828 -0.96930431 -0.3298667 ]]
Matrix B:
[[ 8.59026858e-02 5.82853753e-01 -1.22317476e+00 3.94876866e-01
6.18022893e-04]
[ 1.32168827e+00 8.59026858e-02 5.82853753e-01 -1.22317476e+00
3.94876866e-01]
[ 5.44494671e-01 1.32168827e+00 8.59026858e-02 5.82853753e-01
-1.22317476e+00]
[ 9.89831759e-02 5.44494671e-01 1.32168827e+00 8.59026858e-02
5.82853753e-01]
[ 1.31068730e+00 9.89831759e-02 5.44494671e-01 1.32168827e+00
8.59026858e-02]]
the Frobenius norm of A:
3.1319632480498907
the infinity norm of B:
3.7581141461500844
the largest singular value of B:
2.963944039156744
the smallest singular value of B:
0.3373882862796915
通过关于numpy.linalg.cond文档说明可以知道关于几个参数的用法。
Exercise 9.4:
代码:
import numpy as np
import time
def power_iteration(A, emerson = 1e-5):
x_k = np.random.randn(A.shape[1]).T
eig_val = np.max(x_k)
iterations = 0
s_time = time.clock()
while True:
y_k = x_k/eig_val
x_k1 = np.dot(A, y_k)
eig_val = np.max(x_k1)
if np.linalg.norm(x_k - x_k1) < emerson or iterations > 1e5:
break
iterations += 1
x_k = x_k1
e_time = time.clock()
if iterations > 1e5:
print("the result is not convergence.")
else :
print("the largest eigenvalue of Z:")
print(eig_val)
print("the corresponding eigen vector of Z:")
print(y_k)
print("the itrations are:")
print(iterations)
return e_time - s_time
Z = np.random.randn(2,2)
print("2X2 Matrix Z:")
print(Z)
tim1 = power_iteration(Z)
Z = np.random.randn(3,3)
print("3X3 Matrix Z:")
print(Z)
tim2 = power_iteration(Z)
print("the differece of tow sizes of matrix using power iteration:")
print(tim2 - tim1)
############
结果:
2X2 Matrix Z:
[[ 1.15460099 -0.635185 ]
[ 0.18734087 0.3969566 ]]
the largest eigenvalue of Z:
0.9323504802842806
the corresponding eigen vector of Z:
[1. 0.34989887]
the itrations are:
26
3X3 Matrix Z:
[[ 0.56276902 0.28389285 -0.0534136 ]
[ 0.7729379 0.15439234 1.26399685]
[ 2.45134567 0.736767 -0.90305878]]
the largest eigenvalue of Z:
1.3021521711443254
the corresponding eigen vector of Z:
[0.3330787 1. 0.70436147]
the itrations are:
143
the differece of tow sizes of matrix using power iteration:
0.0011395646328154182
对于这样得到的矩阵幂迭代法并不总是会收敛,所以有时要试个几次。
对于算出来的最大特征值我用numpy提供的方法试验过了,相差一个判停标准的大小,所以基本可以证明正确性。
Exercise 9.5:
代码:
import numpy as np
n, p = 5, 0.8
print("n:{} p:{}".format(n, p))
C = np.array(np.random.binomial(1, p, n*n)).reshape(n, n)
print("The C:")
print(C)
_, vals, _ = np.linalg.svd(C)
print("The singular values of C:\n{}".format(vals))
print("The largest singular value:\n{}".format(np.max(vals)))
print("The n*p is:\n{}".format(n*p))
结果:
n:5 p:0.8
The C:
[[1 1 0 1 1]
[0 0 1 1 1]
[1 1 1 1 1]
[0 0 1 1 0]
[1 1 1 1 1]]
The singular values of C:
[4.00637949e+00 1.50345894e+00 6.59708279e-01 5.03308663e-01
5.67223420e-19]
The largest singular value:
4.006379486076943
The n*p is:
4.0
通过svd分解计算奇异值,可以看到最大奇异值约等于n*p
Exercise 9.6:
代码:
import numpy as np
def nearest_neighbor(A, z):
index = np.argmin(np.abs(A-z))
axi0 = index // A.shape[1]
axi1 = index % A.shape[1]
return A[axi0][axi1]
z = np.random.randn()
A = np.random.randn(2,5)
print("the matrix A:\n{}".format(A))
print("the value z:\n{}".format(z))
print("the nearest neighbor:\n{}".format(nearest_neighbor(A, z)))
结果:
the matrix A:
[[ 1.37603443 -0.81334734 -0.99669634 0.89441934 1.09195437]
[-1.40090431 0.52012356 1.96160828 -0.41486128 -0.87353392]]
the value z:
-0.24677498776151868
the nearest neighbor:
-0.41486128459295607
只要将A中每一个元素减去z再求绝对值,最后再调用argmin找到最小那个元素的下标,通过A的size进行转换,即可得到这个元素。