题目如下
代码实现
前三题的代码如下:
import numpy as np
from scipy.linalg import toeplitz
from scipy.sparse.linalg import svds
# Generate matrices A, with random Gaussian entries, B, a Toeplitz matrix, where A 2 Rn×m and B 2 Rm×m,
# for n = 200, m = 500
n = 200
m = 500
A = np.random.normal(0, 1, [n, m])
B = toeplitz(np.random.normal(0, 1, m))
"""Exercise 9.1: Matrix operations
Calculate A + A, AA>; A>A and AB. Write a function that computes A(B − λI) for any λ."""
A_A = np.add(A, A)
AAT = np.matmul(A, np.transpose(A))
ATA = np.matmul(np.transpose(A), A)
AB = np.matmul(A, B)
def fun(lda):
return np.matmul(A, (B - lda * np.identity(m)))
print("Below are A+A, AAT, ATA, AB")
print(A_A)
print(AAT)
print(ATA)
print(AB)
print("For lda=2, result is")
print(fun(2.0))
print("\n####\n")
"""Exercise 9.2: Solving a linear system
Generate a vector b with m entries and solve Bx = b."""
b = np.random.normal(0, 1, m)
x = np.linalg.solve(B, b)
print("b is "+str(b))
print("And x is "+str(x))
print("\n####\n")
"""Exercise 9.3: Norms
Compute the Frobenius norm of A: kAkF and the infinity norm of B: kBk1. Also find the largest and
smallest singular values of B"""
AF = np.linalg.norm(A, ord='fro')
B_INF = np.linalg.norm(B, np.inf)
U, S, VT = svds(B)
B_singular_max, B_singular_min = max(S), min(S)
print("A is ")
print(A)
print("Max singular value is "+str(B_singular_max)+" and min singular value is "+str(B_singular_min))
print("\n####\n")
输出如下:
Below are A+A, AAT, ATA, AB
[[-2.66492207e+00 -1.49876012e+00 2.00194667e+00 ... 6.88724846e-01
-3.40831098e+00 -6.91576946e-01]
[ 1.63296008e+00 3.22684403e-01 -1.89144047e-03 ... 1.83076990e+00
-3.31802590e+00 9.16201166e-01]
[ 1.75877364e+00 3.05972751e-01 8.08349762e-01 ... -9.55688856e-01
3.61101240e-01 -7.13090871e-01]
...
[ 1.33997184e+00 2.27119849e+00 3.59792183e+00 ... 2.01778678e-01
-6.29408881e-01 1.30165867e+00]
[ 2.02176993e+00 -1.94151037e+00 -1.85951773e+00 ... 1.02234975e-01
3.25138082e+00 3.03088772e-01]
[-1.00821852e+00 1.90456708e+00 9.38143901e-01 ... -9.27595989e-01
-7.76512818e-01 2.39483021e+00]]
[[ 4.49305978e+02 -2.29053771e+01 -1.59603117e+01 ... -5.91357295e+01
-1.35699783e+00 8.24689583e+00]
[-2.29053771e+01 5.18908889e+02 2.71560621e+01 ... 4.77307211e+01
3.24210425e+01 5.63168675e+01]
[-1.59603117e+01 2.71560621e+01 4.88911427e+02 ... -1.22549093e+01
2.13182561e+01 2.19720608e+00]
...
[-5.91357295e+01 4.77307211e+01 -1.22549093e+01 ... 4.42767039e+02
-1.11891578e-01 1.47697302e+01]
[-1.35699783e+00 3.24210425e+01 2.13182561e+01 ... -1.11891578e-01
4.23681258e+02 1.38232669e+01]
[ 8.24689583e+00 5.63168675e+01 2.19720608e+00 ... 1.47697302e+01
1.38232669e+01 5.12051371e+02]]
[[219.20012256 -3.20612379 4.3246027 ... -8.46571729 -21.58006919
-13.05689245]
[ -3.20612379 202.48266643 -2.67977549 ... 3.33105725 -34.53833566
-3.90295443]
[ 4.3246027 -2.67977549 217.13114269 ... 16.38075194 -6.15373344
-1.45957956]
...
[ -8.46571729 3.33105725 16.38075194 ... 176.90377695 -2.96049225
-19.8399056 ]
[-21.58006919 -34.53833566 -6.15373344 ... -2.96049225 193.1830672
-8.16717231]
[-13.05689245 -3.90295443 -1.45957956 ... -19.8399056 -8.16717231
191.28936793]]
[[ -1.75100128 -4.51664359 -20.49610688 ... 15.6485837 13.55305409
8.9376733 ]
[ 6.12003807 10.25952047 -17.9046945 ... 59.1307008 12.06137858
-9.91080235]
[ 25.73718843 3.09244542 -20.17783096 ... -13.29777408 0.112725
27.53598967]
...
[ 23.81562351 -18.49531225 5.44819341 ... 5.91362779 -35.13807488
6.35920828]
[-17.50724879 -38.05131258 1.15006618 ... 45.28058997 42.16252686
-24.66810137]
[-10.76791969 2.20370108 -13.99994459 ... -0.48624489 18.46654184
7.25983222]]
For lda=2, result is
[[ 0.91392079 -3.01788347 -22.49805355 ... 14.95985886 16.96136507
9.62925024]
[ 4.48707799 9.93683607 -17.90280306 ... 57.2999309 15.37940448
-10.82700352]
[ 23.97841479 2.78647267 -20.98618072 ... -12.34208522 -0.24837624
28.24908054]
...
[ 22.47565167 -20.76651073 1.85027158 ... 5.71184911 -34.508666
5.05754961]
[-19.52901872 -36.10980221 3.0095839 ... 45.178355 38.91114604
-24.97119014]
[ -9.75970117 0.299134 -14.93808849 ... 0.4413511 19.24305466
4.86500201]]
####
b is [ 1.22083052e+00 -1.65700579e+00 9.63059303e-02 -1.17430549e+00
1.49951110e+00 7.26584678e-01 -4.17270003e-01 7.12865120e-01
-7.83891392e-02 -6.39535285e-01 1.30622553e-01 3.63590454e-01
-1.33990228e+00 -8.34523470e-01 -1.21964855e-01 -1.65009101e+00
-3.46959995e-01 8.65272592e-01 1.18348362e+00 -1.90895548e+00
-2.86590183e-01 -1.53595185e-01 -1.14898115e+00 -1.27306028e+00
-7.67913925e-01 -1.34188844e+00 -1.36358747e-01 7.94255993e-01
2.16212057e+00 -1.04995856e+00 1.45502980e+00 1.70369853e+00
-2.09064969e-01 1.35607937e+00 7.74270679e-01 -1.52029884e+00
7.84937947e-01 -6.76828000e-01 1.71962803e-01 8.33550493e-03
-6.30517493e-01 3.08986877e-02 -1.23903993e+00 2.38859740e-01
-8.16949510e-02 6.11653434e-01 8.66016775e-01 -7.61510181e-01
-1.88575021e+00 -4.25029892e-02 -2.29107390e-01 8.91181590e-01
6.81565497e-01 -5.14479737e-02 -1.80205071e-01 1.97680876e-01
8.64809910e-02 -5.03409004e-01 -7