Generate matrices A, withrandom Gaussian entries, B, a Toeplitz matrix, where A ∈ Rn×m and B ∈ Rm×m, for n = 200, m = 500.
Exercise 9.1: Matrixoperations
Calculate A + A, AA⊤, A⊤A and AB. Write a function that computes A(B − λI) forany λ.
输入样例
100
样例结果
input n
100
[[ 0.25492905 0.23925104 -1.98625118 ... 3.12361183 -1.76108268
-1.03045014]
[ 0.99610086 -2.19372995 1.99450751 ... 1.62936487 -0.79844217
-1.01313104]
[ 1.02039602 -1.18865333 0.90970572 ... -0.00643825 0.37541486
-2.6062939 ]
...
[ 2.49890466 -0.03042841 -0.40941104 ... 2.83135408 1.56049787
-1.75867723]
[ 1.77857431 3.6898885 -1.06631876 ... -1.28950938 -3.34071184
-0.20809047]
[ 1.02772208 -1.27415573 -1.98211949 ... -2.66795933 0.94557247
0.39738567]]
[[497.10157725 29.33997367 11.89869076 ... -39.2352458 -28.08448026
-16.66887871]
[ 29.33997367 516.0630948 10.41121829 ... 25.35670707 -16.97177249
-23.11154664]
[ 11.89869076 10.41121829 465.19591596 ... -36.1198177 -1.008844
-27.82534697]
...
[-39.2352458 25.35670707 -36.1198177 ... 482.91736413 20.5736587
-14.02801916]
[-28.08448026 -16.97177249 -1.008844 ... 20.5736587 557.17793535
27.50731586]
[-16.66887871 -23.11154664 -27.82534697 ... -14.02801916 27.50731586
462.0287267 ]]
[[217.98650996 12.01549371 -1.81752272 ... -12.78807957 3.16064157
8.67172505]
[ 12.01549371 225.30981342 9.86966004 ... -0.67555045 -1.59276503
7.04849011]
[ -1.81752272 9.86966004 176.43297755 ... -5.20047595 14.58406069
-3.65044271]
...
[-12.78807957 -0.67555045 -5.20047595 ... 207.4008876 12.04331292
15.13019973]
[ 3.16064157 -1.59276503 14.58406069 ... 12.04331292 161.20307071
26.59891546]
[ 8.67172505 7.04849011 -3.65044271 ... 15.13019973 26.59891546
209.15421597]]
[[-2968.75941953 -2951.42722365 -2933.85577673 ... -5556.61871045
-5570.90444446 -5586.95126115]
[ 2687.82633874 2680.12814943 2670.23623017 ... 1647.79975151
1658.30561489 1668.01303611]
[-3585.77473724 -3559.94286365 -3535.29964339 ... -8799.9897419
-8822.57034043 -8844.7755241 ]
...
[-2167.51025683 -2161.4846763 -2155.48952416 ... 405.74213491
402.41363838 400.64563972]
[-7925.52176622 -7896.19366971 -7863.17568469 ... -5825.44670401
-5849.44742391 -5876.78885564]
[ 9772.60466705 9735.48683747 9697.09485215 ... 9263.76595799
9300.56855152 9338.31671751]]
[[-2981.5058721 -2963.38977575 -2834.54321794 ... -5712.79930194
-5482.85031043 -5535.42875429]
[ 2638.02129558 2789.81464714 2570.51085476 ... 1566.33150826
1698.22772352 1718.66958831]
[-3636.7945383 -3500.51019699 -3580.78492929 ... -8799.66782921
-8841.34108365 -8714.46082924]
...
[-2292.45548961 -2159.96325601 -2135.01897228 ... 264.17443092
324.38874494 488.57950099]
[-8014.4504819 -8080.68809487 -7809.85974688 ... -5760.97123503
-5682.41183203 -5866.38433235]
[ 9721.21856286 9799.19462415 9796.20082673 ... 9397.16392435
9253.28992805 9318.44743384]]
代码实现
import numpy as np
A = np.random.randn(200,500)
B = np.zeros((500,500))
C = np.zeros((500,500))
n = input("input n \n")
for i in range(0,500):
for j in range(0,500):
if i==0:
B[i][j]=j+1
if j==0:
B[i][j]=i+1
else:
if i>=j:
B[i][j]=B[i-j][0]
else:
B[i][j]=B[0][j-i]
for i in range(0,500):
for j in range(0,500):
if i==j:
C[i][j]=n
res_1 = A + A
res_2 = np.dot(A,A.transpose())
res_3 = np.dot(A.transpose(),A)
res_4 = np.dot(A,B)
res_5 = np.dot(A,(B-C))
print(res_1)
print(res_2)
print(res_3)
print(res_4)
print(res_5)
Exercise 9.2: Solving a linear system
Generate a vector b with mentries and solve Bx = b.
样例结果
x= [[ 8.34559935e-02]
[-1.84147550e-01]
[-1.05216680e-01]
[ 2.38800669e-01]
[-8.28615179e-02]
[ 3.61193104e-01]
[-4.45423501e-01]
[-1.35173495e-01]
[ 6.09494125e-01]
[-4.80077298e-01]
[ 3.29950112e-01]
[-4.14958640e-01]
[ 3.16616561e-01]
[-7.51664965e-02]
[ 1.51916913e-01]
[-4.03210726e-01]
[ 8.92947689e-02]
[ 5.26897948e-01]
[-6.16379359e-01]
[ 2.08842675e-01]
[-1.07060824e-01]
[ 2.17467580e-01]
[-1.66095536e-02]
[-2.55367202e-01]
[ 2.06886917e-01]
[ 3.53684786e-02]
[ 2.27446762e-01]
[-3.93807802e-01]
[ 4.59129164e-02]
[-6.72456530e-02]
[ 5.25469225e-01]
[-7.37078550e-01]
[ 4.35221198e-01]
[ 9.20155706e-02]
[-6.94621527e-02]
[-1.79338551e-01]
[-1.27424314e-01]
[ 1.16619655e-01]
[ 1.74149962e-01]
[ 4.54203642e-02]
[-7.16545186e-02]
[-4.31852899e-01]
[ 7.34156101e-01]
[-4.03685085e-01]
[-1.18011844e-01]
[ 1.50847021e-01]
[-2.22778242e-01]
[ 2.38117117e-01]
[ 2.90521359e-01]
[-5.82160008e-01]
[ 2.38669341e-01]
[ 3.31672850e-01]
[-6.45536733e-01]
[ 5.68920365e-01]
[-3.23782171e-01]
[ 1.57215032e-01]
[ 1.27013666e-01]
[-9.00145957e-02]
[-2.29290467e-01]
[ 2.22098850e-01]
[-1.19433020e-01]
[-1.91330397e-01]
[ 3.58191134e-01]
[-1.98690801e-01]
[-2.69714817e-02]
[ 4.04991269e-01]
[-5.07232204e-01]
[ 2.75826290e-01]
[-1.94707577e-01]
[ 2.81194028e-01]
[-2.62985699e-02]
[-3.11719924e-01]
[-3.49547695e-02]
[ 4.19862396e-01]
[-2.72425910e-01]
[-1.85272247e-02]
[ 3.99369635e-01]
[-5.39964024e-01]
[ 3.56416325e-01]
[-4.62717345e-01]
[ 7.22801876e-01]
[-7.65389295e-01]
[ 7.72949540e-01]
[-6.46372969e-01]
[ 3.21245808e-01]
[-1.59407527e-01]
[ 1.99854992e-01]
[-3.95641322e-02]
[-3.37475819e-01]
[ 5.15962135e-01]
[-6.42357283e-01]
[ 6.66217134e-01]
[-5.64741175e-01]
[ 3.26223884e-01]
[ 2.70030684e-01]
[-3.82309935e-01]
[ 2.14708519e-01]
[-1.89896840e-01]
[-3.29821144e-01]
[ 4.05792796e-01]
[ 1.06086499e-01]
[-1.31467740e-01]
[-1.70459142e-01]
[ 4.04866655e-01]
[-3.62953563e-01]
[ 4.43021167e-02]
[ 4.11584772e-01]
[-4.39571880e-01]
[ 1.75695807e-01]
[-1.13816943e-01]
[ 1.77936937e-01]
[-4.38126179e-01]
[ 1.46660064e-01]
[ 3.93872145e-01]
[-8.27819909e-02]
[-4.01875853e-01]
[ 2.86679346e-01]
[-2.36906349e-01]
[ 5.62543362e-01]
[-4.83403971e-01]
[ 5.61321843e-02]
[ 1.83067574e-01]
[-2.29046358e-02]
[-3.81521836e-01]
[ 4.51834916e-01]
[-3.86659474e-01]
[ 2.69395756e-01]
[-8.42283052e-02]
[ 5.04553135e-02]
[ 3.51851272e-01]
[-7.26952959e-01]
[ 5.83419718e-01]
[-4.13391983e-01]
[ 2.48918661e-01]
[ 2.29856832e-01]
[-3.02926772e-01]
[-3.62752746e-01]
[ 4.00435333e-01]
[ 5.29232042e-02]
[-1.22067175e-01]
[ 9.09945531e-02]
[-1.66264653e-01]
[ 9.23163391e-02]
[-6.81396456e-03]
[ 4.14225755e-01]
[-2.94523645e-01]
[-3.88799007e-01]
[ 4.62103445e-01]
[-6.63221933e-02]
[-1.19887025e-01]
[ 1.02216061e-01]
[-4.39030717e-01]
[ 3.72177725e-01]
[ 3.18769836e-01]
[-4.88558130e-01]
[ 9.02108609e-02]
[ 1.27080832e-04]
[ 3.60026869e-01]
[-3.39144470e-01]
[-7.52131660e-02]
[ 2.32187303e-01]
[-1.75593875e-01]
[-1.03410914e-01]
[ 3.88736022e-01]
[-4.03879555e-01]
[ 4.77356751e-01]
[-3.01255981e-01]
[-3.80579114e-01]
[ 7.34248653e-01]
[-2.25040021e-01]
[-4.81945652e-01]
[ 3.10681057e-01]
[ 4.64571053e-01]
[-6.28670824e-01]
[ 2.21569530e-02]
[ 2.15389878e-01]
[ 1.93473475e-01]
[-2.43842107e-01]
[-1.37873571e-01]
[ 1.20200249e-01]
[ 1.16553973e-01]
[-1.36284792e-01]
[ 1.66330161e-01]
[-1.11419831e-01]
[ 1.92658197e-01]
[ 6.57110486e-03]
[-3.01866787e-01]
[ 1.82473580e-01]
[-2.64987495e-01]
[ 3.45321160e-01]
[-3.09240988e-01]
[ 1.33960265e-01]
[-7.81492375e-02]
[ 2.15875840e-01]
[ 2.86054122e-02]
[-3.17512728e-01]
[ 2.62134381e-01]
[-1.97039252e-01]
[ 1.19289634e-02]
[ 2.75734203e-01]
[-4.76397098e-02]
[-2.79887432e-01]
[ 3.61766050e-01]
[-2.78526045e-01]
[-1.72145444e-01]
[ 6.27631305e-01]
[-5.58907569e-01]
[ 5.89293065e-02]
[ 9.14401894e-02]
[ 2.61608242e-01]
[-2.66915108e-01]
[-4.44390763e-02]
[-1.23150806e-01]
[ 2.22662818e-01]
[ 6.32052460e-02]
[-9.71216172e-03]
[-8.79564343e-02]
[-1.01944916e-01]
[ 4.02967664e-01]
[-6.47554377e-01]
[ 6.38357924e-01]
[-3.05374054e-01]
[-1.38076979e-01]
[ 1.05168498e-01]
[ 3.97267617e-01]
[-3.96264915e-01]
[ 4.48281181e-02]
[-2.22862596e-01]
[ 1.09344182e-01]
[ 4.43018978e-01]
[-4.64679415e-01]
[ 1.42262443e-01]
[ 1.10327577e-01]
[-2.64022453e-01]
[ 3.27853659e-01]
[-1.93284004e-01]
[ 1.55274242e-01]
[-2.09814130e-01]
[-2.13672781e-01]
[ 4.24597156e-01]
[ 8.08912195e-02]
[-1.71812265e-01]
[-2.89931707e-01]
[ 4.63749854e-01]
[-1.25509575e-01]
[-2.72193729e-01]
[-2.68369069e-02]
[ 2.65616649e-01]
[-9.83989391e-02]
[ 9.46798612e-02]
[-2.59852153e-02]
[-1.49430526e-01]
[ 1.16118304e-01]
[ 1.93273221e-01]
[-1.48173003e-01]
[ 9.36635114e-02]
[-3.83976164e-01]
[ 2.46498955e-01]
[ 1.53478280e-01]
[-4.03415337e-01]
[ 2.76743035e-01]
[ 2.22226291e-01]
[-1.64240752e-01]
[-4.40253163e-01]
[ 3.99668852e-01]
[ 4.82014754e-02]
[ 1.75574862e-01]
[-2.30672219e-01]
[-3.24456787e-02]
[-3.62904835e-01]
[ 6.13400317e-01]
[-2.20835151e-01]
[-5.44488314e-02]
[ 1.46665363e-02]
[-1.42919263e-02]
[ 9.26625144e-02]
[-1.39764451e-01]
[-1.81767131e-01]
[ 3.56835961e-01]
[-1.36186882e-01]
[ 1.08827016e-01]
[-6.52437560e-02]
[-3.16979832e-02]
[-8.51227877e-02]
[ 2.78903330e-01]
[-8.93996755e-02]
[-3.94409092e-01]
[ 6.96196117e-01]
[-8.40432688e-01]
[ 5.62904334e-01]
[-1.77707836e-01]
[ 2.74438431e-01]
[-9.79662880e-02]
[-2.23726436e-01]
[-1.13204908e-02]
[ 6.00267052e-02]
[ 3.60568231e-02]
[ 7.75294951e-03]
[ 2.67711629e-01]
[-3.92684854e-01]
[ 3.94335654e-01]
[-1.19287430e-01]
[-2.47033916e-01]
[ 1.76772570e-01]
[-4.51513879e-01]
[ 5.56585270e-01]
[-1.77922158e-01]
[-3.82020340e-02]
[ 2.56232688e-02]
[ 2.00051703e-01]
[ 4.13440867e-02]
[-5.12434683e-01]
[ 4.88874944e-01]
[-3.62580327e-01]
[ 1.88331626e-01]
[ 7.44348311e-02]
[-3.21932825e-01]
[ 2.04789946e-01]
[ 2.17712514e-01]
[-4.55148413e-02]
[-7.92044100e-03]
[-5.92181532e-01]
[ 8.65385461e-01]
[-6.73814779e-01]
[ 4.37877425e-01]
[-2.87210111e-01]
[ 3.10865718e-02]
[ 2.70632243e-01]
[-2.26927803e-01]
[ 1.22344876e-02]
[-6.99719097e-02]
[-7.68293708e-02]
[ 6.41160471e-01]
[-8.86151645e-01]
[ 6.05917014e-01]
[-3.28296444e-01]
[ 4.13690925e-01]
[-2.18803165e-01]
[-4.59454186e-01]
[ 6.93952754e-01]
[-3.00239616e-01]
[-6.95195176e-02]
[ 1.60753641e-01]
[-1.36383513e-01]
[ 1.14609218e-01]
[-2.13078709e-02]
[-3.04207299e-01]
[ 2.21108764e-01]
[ 4.91115735e-01]
[-8.77625645e-01]
[ 7.95131045e-01]
[-3.92170945e-01]
[ 2.57796756e-02]
[ 9.78281444e-03]
[-1.05304649e-01]
[ 2.80584204e-01]
[-2.71143030e-01]
[ 1.33212841e-01]
[-1.20549891e-02]
[-3.19682269e-01]
[ 5.23647780e-01]
[-4.50021216e-01]
[ 2.45933292e-01]
[ 2.85081395e-01]
[-4.63970297e-01]
[-6.31085095e-02]
[ 2.81840540e-01]
[ 2.24538411e-01]
[-4.88299093e-01]
[-9.00847927e-02]
[ 4.46814350e-01]
[-2.04349858e-01]
[ 2.83225413e-01]
[-2.72634941e-01]
[-1.94632537e-01]
[ 5.15796396e-01]
[-5.74948370e-01]
[ 6.39920473e-01]
[-3.53756033e-01]
[-4.27445043e-01]
[ 8.05442795e-01]
[-4.46529405e-01]
[-1.89904406e-01]
[ 3.50387918e-01]
[-2.14339396e-01]
[ 3.16186957e-01]
[-4.03410379e-01]
[ 4.76154853e-01]
[-4.64421972e-01]
[ 1.83292581e-01]
[ 3.22499713e-01]
[-5.17954578e-01]
[ 2.57497437e-02]
[ 4.26516903e-01]
[-2.93560668e-01]
[ 5.34450834e-02]
[-1.07426566e-01]
[ 3.65050492e-01]
[-2.44835758e-01]
[-1.22053106e-01]
[ 6.80592474e-02]
[-1.12267718e-01]
[ 3.73459765e-01]
[-1.60091991e-01]
[-3.44004770e-01]
[ 5.82848684e-01]
[-3.32855543e-01]
[-4.97010589e-03]
[-2.67571979e-01]
[ 5.17533714e-01]
[-3.23625173e-01]
[ 4.15842209e-02]
[ 3.09994186e-01]
[-2.76155644e-01]
[ 1.59719627e-01]
[-5.54390588e-01]
[ 6.29207519e-01]
[-2.93647246e-01]
[-4.47903271e-03]
[ 3.30323722e-01]
[-2.98414529e-01]
[ 1.30068947e-01]
[-7.14049009e-02]
[ 1.58201781e-01]
[-1.38722315e-01]
[-8.60606599e-02]
[ 1.05736489e-01]
[-5.61986036e-02]
[ 5.48823240e-03]
[-3.24105544e-03]
[ 1.83805758e-01]
[-5.87915789e-01]
[ 6.69007531e-01]
[-2.50181699e-01]
[-1.84397493e-01]
[ 2.62083489e-01]
[-1.45250215e-01]
[ 1.02866927e-01]
[-7.57170519e-02]
[ 1.78233776e-01]
[-9.05827261e-02]
[-6.82608355e-02]
[-1.32277473e-01]
[ 3.30129118e-01]
[-1.07163280e-01]
[-9.70541969e-02]
[ 2.40832779e-01]
[-2.07909789e-01]
[-1.83596140e-01]
[ 3.64407398e-01]
[-1.58821627e-01]
[-9.74028243e-02]
[ 8.14949093e-03]
[-2.05600906e-01]
[ 3.97200003e-01]
[-8.12137115e-02]
[-1.88594692e-02]
[ 1.93518228e-02]
[ 4.08701301e-02]
[-1.64708739e-01]
[ 1.32983868e-01]
[-3.63054775e-01]
[ 4.45069156e-01]
[-2.90423043e-01]
[ 3.24980432e-01]
[-3.19020489e-01]
[ 3.14806943e-01]
[-3.57321337e-01]
[ 5.41230863e-01]
[-4.10219821e-01]
[ 1.52549049e-01]
[-2.07424603e-01]
[ 9.33934478e-03]
[ 3.44196720e-01]
[-4.13172854e-01]
[ 3.30351892e-01]
[-2.39256653e-01]
[-3.56589209e-03]
[ 2.32317065e-01]
[-1.13635358e-02]
[-5.02270524e-01]
[ 3.65358406e-01]
[ 1.86624380e-01]
[-1.60279199e-01]
[ 1.58237224e-01]
[-4.29898678e-01]
[ 1.29692092e-01]
[ 2.78657020e-01]
[ 1.97796122e-02]
[-2.98963634e-01]
[ 1.23725825e-01]
[-3.13556894e-01]
[ 3.67464513e-01]
[ 2.61752766e-01]
[-3.70946403e-01]
[-1.59506372e-01]
[ 2.97173904e-01]
[ 8.95554286e-02]
[-4.57052916e-01]
[ 2.83672682e-01]]
代码实现
import numpy as np
A = np.random.randn(200,500)
B = np.zeros((500,500))
C = []
for i in range(0,500):
C.append(i)
b = np.random.rand(1,500)
b = b.transpose()
for i in range(0,500):
for j in range(0,500):
if i==0:
B[i][j]=j+1
if j==0:
B[i][j]=i+1
else:
if i>=j:
B[i][j]=B[i-j][0]
else:
B[i][j]=B[0][j-i]
x=np.linalg.solve(B,b)
print("x= ",end="")
print(x)
Exercise 9.3: Norms
Compute the Frobenius norm ofA: ∥A∥F and the infinity norm of B: ∥B∥∞. Also find the largest and smallest singular values ofB.
样例结果
The the Frobenius norm of A:316.1542742333776
the infinity norm of B: 125250.0
The largest singular value of B: 87334.52045641872
The smallest singular value of B: 0.5000049348346122
代码实现
import numpy as np
A = np.random.randn(200,500)
B = np.zeros((500,500))
C = np.zeros((500,500))
for i in range(0,500):
for j in range(0,500):
if i==0:
B[i][j]=j+1
if j==0:
B[i][j]=i+1
else:
if i>=j:
B[i][j]=B[i-j][0]
else:
B[i][j]=B[0][j-i]
U,Simga,VT = np.linalg.svd(B)
print("The the Frobenius norm of A:", end="")
print(np.linalg.norm(A,ord="fro") )
print("the infinity norm of B: ", end="")
print(np.linalg.norm(B,ord=np.inf) )
print("The largest singular value of B: ", end="")
print(max(Simga))
print("The smallest singular value of B: ", end="")
print(min(Simga))
Exercise 9.4: Power iteration
Generate a matrix Z, n × n,with Gaussian entries, and use the power iteration to find the largesteigenvalue and corresponding eigenvector of Z. How many iterations are neededtill convergence?
Optional: use the time.clock()method to compare computation time when varying n.
样例结果
corresponding eigenvector:
[[0.91559639]
[0.89268121]
[0.87144762]
[0.87458469]
[0.93544372]
[0.92239593]
[0.9580073 ]
[0.85998745]
[0.86710414]
[0.89849702]
[0.9327396 ]
[0.92985359]
[0.92492912]
[0.94828568]
[0.87524395]
[0.9571552 ]
[0.83977028]
[0.9171816 ]
[0.97666198]
[0.90403646]
[0.88322289]
[0.9610705 ]
[0.89764944]
[0.91319393]
[1. ]
[0.84704578]
[0.85399273]
[0.9724098 ]
[0.86084083]
[0.9595304 ]
[0.79744201]
[0.92509031]
[0.90543023]
[0.81592596]
[0.87793256]
[0.79259957]
[0.95003318]
[0.89409636]
[0.89796365]
[0.84981036]
[0.93887177]
[0.92832027]
[0.89465133]
[0.9442769 ]
[0.8596261 ]
[0.90192049]
[0.9129606 ]
[0.90048482]
[0.9391955 ]
[0.91988383]
[0.90919921]
[0.93360468]
[0.95790932]
[0.90077368]
[0.90525969]
[0.94378787]
[0.87498242]
[0.88034921]
[0.83672044]
[0.95834085]
[0.87479429]
[0.96338713]
[0.89796626]
[0.94590632]
[0.89370282]
[0.87129726]
[0.91268794]
[0.91096478]
[0.88757922]
[0.87460715]
[0.88463397]
[0.85673907]
[0.92399461]
[0.91177149]
[0.88993293]
[0.85145739]
[0.97344888]
[0.93499517]
[0.91170027]
[0.94849799]
[0.92809657]
[0.85780627]
[0.91054134]
[0.90489654]
[0.9329966 ]
[0.87025115]
[0.94732186]
[0.85818768]
[0.92345759]
[0.90537044]
[0.93544906]
[0.96247193]
[0.94489962]
[0.83528039]
[0.92233327]
[0.9095816 ]
[0.90990527]
[0.93047281]
[0.86281859]
[0.86985438]
[0.97422147]
[0.89567786]
[0.95886992]
[0.89747697]
[0.90123251]
[0.93617358]
[0.96096396]
[0.87865638]
[0.87203685]
[0.91988447]
[0.8323721 ]
[0.95160632]
[0.92246841]
[0.90499615]
[0.87873996]
[0.93696255]
[0.93369107]
[0.87042756]
[0.9077112 ]
[0.88380723]
[0.9604506 ]
[0.97583191]
[0.91643467]
[0.89307383]
[0.87370851]
[0.9892885 ]
[0.84000359]
[0.85144256]
[0.89242572]
[0.83290574]
[0.85820479]
[0.87453674]
[0.87551866]
[0.8993992 ]
[0.88702429]
[0.94640939]
[0.9016169 ]
[0.93494489]
[0.91671573]
[0.96857068]
[0.8708158 ]
[0.93530149]
[0.89689452]
[0.95541044]
[0.93012103]
[0.91541332]
[0.88572612]
[0.88889341]
[0.84604597]
[0.91236831]
[0.90697303]
[0.85590704]
[0.92512585]
[0.95215093]
[0.94143391]
[0.981321 ]
[0.91820463]
[0.89896148]
[0.91086301]
[0.88439336]
[0.91817239]
[0.88880591]
[0.89581724]
[0.84920096]
[0.86460557]
[0.89611826]
[0.84486929]
[0.96190026]
[0.96311036]
[0.90073018]
[0.89755858]
[0.90938207]
[0.88700873]
[0.9257148 ]
[0.85543779]
[0.90367665]
[0.91201713]
[0.87187459]
[0.94117184]
[0.90071036]
[0.83150206]
[0.96937621]
[0.92196265]
[0.92568097]
[0.82051319]
[0.93548524]
[0.92170302]
[0.91919326]
[0.89899595]
[0.90106145]
[0.93684119]
[0.95047289]
[0.92568223]
[0.84363326]
[0.96187736]
[0.91418153]
[0.97973243]
[0.88874558]
[0.97534505]
[0.87660444]]
eigenvalue: 100.52496392461119
iterations: 6
Time: 0.0003459999999999852
代码实现
import numpy as np
import time
import scipy.linalg
import scipy
Z = np.mat(np.random.rand(200,200))
v = np.mat(np.ones(200)) #初始向量
n = 0 #迭代次数
v = v.T
x = v
t_ = 0
ei = scipy.linalg.norm(x,ord=np.inf) #特征值
time_ = time.clock()
while np.abs(t_-ei)>1e-4:
t_ = ei
x = Z * v
ei = scipy.linalg.norm(x,ord=np.inf)
v = x/ei
n = n+1
time_ = time.clock()-time_
print("corresponding eigenvector: ")
print(v)
print("eigenvalue: ",end="")
print(ei)
print("iterations: ",end="")
print(n)
print("Time: ",end="")
print(time_)
Exercise 9.5: Singular values
Generate an n × n matrix,denoted by C, where each entry is 1 with probability p and 0 otherwise. Use thelinear algebra library of Scipy to compute the singular values of C. What canyou say about the relationship between n, p and the largest singular value?
样例结果
p: 0.8
n: 70
the largest singular value of C: 56.197368953325764
p: 0.5
n: 20
the largest singular value of C: 10.779133254881227
p: 0.1
n: 30
the largest singular value of C: 3.9385153803168302
p: 0.4
n: 20
the largest singular value of C: 8.82997237316175
p: 0.5
n: 50
the largest singular value of C: 25.861788207081943
p: 0.5
n: 20
the largest singular value of C: 10.85155130553588
p: 0.9
n: 50
the largest singular value of C: 44.98491751547517
p: 0.1
n: 80
the largest singular value of C: 9.134182074856593
p: 0.2
n: 90
the largest singular value of C: 18.711843187908286
p: 0.8
n: 40
the largest singular value of C: 31.725598579547427
代码实现
import numpy as np
import scipy.linalg as la
import random
for i in range(0,10):
p=random.randint(1,9)/10
n=random.randint(1,9)*10
C=la.svdvals(np.random.binomial(1, p, [n,n]))
print("p: ", end="")
print(p)
print("n: ", end= "")
print(n)
print("the largest singular value of C: ", end="")
print(max(C))
Exercise 9.6: Nearest neighbor
Write a function that takes avalue z and an array A and finds the element in A that is closest to z. Thefunction should return the closest value, not index.
Hint: Use the built-infunctionality of Numpy rather than writing code to find this value manually. Inparticular, use brackets and argmin.
样例结果
random z: 0.885393383649773
cloest value: 0.8853989213942045
代码实现
import numpy as np
import scipy
A = np.random.randn(200,500)
A = A.reshape(1,100000)
z = np.random.random()
def f(a, z):
c = np.ones(100000)
c = z*c
return a[0,np.argmin(np.fabs(c-a))]
re =f(A,z)
print("random z: ", end = "")
print(z)
print("cloest value: ", end="")
print(re)