本次作业题目如下:
首先,先生成矩阵A,B:
from numpy import *
from scipy.linalg import toeplitz
n = 200
m = 500
A = random.randn(n,m)
col = random.normal(size=m)
row = random.normal(size=m)
B = toeplitz(col,row)
print(A)
print(B)
生成情况:
9.1
def solve9_1(A,B):
Aadd = A + A
AmulT = dot(A,A.T)
TmulA = dot(A.T,A)
AmulB = dot(A,B)
mulx = 3 #暂时设I矩阵的乘子为3
w = calAB(A,B,mulx,m)
print("A+A:")
print(Aadd)
print("A A.T:")
print(AmulT)
print("A.T A :")
print(TmulA)
print("A B:")
print(AmulB)
print("A-XB:")
print(w)
def calAB(A,B,mulx,m):
print(eye(3,3))
return dot(A,(B-mulx*(eye(m,m))))
输出如下:
A+A:
[[-1.91764025 2.60468621 -0.68439504 … -2.22714446 -0.76065558
0.93503315]
[ 0.05893145 4.26973538 1.42860075 … -4.0245202 2.23765254
-0.12218119]
[-0.39186952 2.75417221 0.83623986 … 2.42530614 1.49309249
3.66628225]
…
[-0.24183093 -0.22857036 -3.00430053 … -0.31516838 1.94240653
-1.28723983]
[-5.46672822 0.55294623 4.6460154 … 0.06498061 -1.4338368
1.72773735]
[ 0.28323958 -2.94169055 1.47153861 … -2.18891809 -0.81813839
0.82304029]]
A A.T:
[[521.68427882 -22.94196426 29.11444508 … -31.99400011 -42.62411399
8.01879607]
[-22.94196426 493.8375307 -8.56563663 … 7.10970911 -3.33969036
-14.01084268]
[ 29.11444508 -8.56563663 449.71448008 … 21.55280285 10.23763682
10.75581677]
…
[-31.99400011 7.10970911 21.55280285 … 537.61337854 -10.03401497
-8.25375679]
[-42.62411399 -3.33969036 10.23763682 … -10.03401497 541.91824818
16.75039599]
[ 8.01879607 -14.01084268 10.75581677 … -8.25375679 16.75039599
500.52879525]]
A.T A :
[[185.6307274 -8.79743125 -7.39854533 … -6.93922975 20.78144017
-8.93099493]
[ -8.79743125 188.25479938 20.54299668 … -2.83527677 -5.41421182
-2.93946265]
[ -7.39854533 20.54299668 206.44454163 … 2.21558626 -5.22816547
5.67779687]
…
[ -6.93922975 -2.83527677 2.21558626 … 173.92042128 -12.48786783
31.81987501]
[ 20.78144017 -5.41421182 -5.22816547 … -12.48786783 195.97278235
-22.84675869]
[ -8.93099493 -2.93946265 5.67779687 … 31.81987501 -22.84675869
184.76263301]]
A B:
[[ 1.13722180e+01 -4.91259408e+01 2.29419203e+01 … -3.22813568e+00
-1.30868748e+01 -1.57074071e+01]
[ 3.18381322e+00 1.16403117e+01 -3.42792570e+01 … -6.50998824e+00
6.93571976e-01 1.42406710e+00]
[ 7.11846538e+00 -6.51230210e+00 2.38293279e+01 … -2.47848548e+01
-4.33351732e-01 2.09098294e+01]
…
[-4.42404144e+01 -4.37470678e+01 -2.81147244e+01 … 4.78908898e+01
7.59621785e+00 2.57316892e+01]
[ 1.57706328e+01 1.60386406e+01 2.09096466e+01 … 8.68017872e-01
4.25715726e+00 1.70664979e+01]
[-1.06553724e+01 7.78046251e+00 8.94181743e+00 … -1.70101988e+01
-8.11194147e+00 2.22697944e-02]]
A-XB:
[[ 14.24867842 -53.03297008 23.9685129 … 0.11258101 -11.9458914
-17.10995679]
[ 3.09541605 5.23570865 -36.42215814 … -0.47320793 -2.66290683
1.60733889]
[ 7.70626966 -10.64356041 22.57496807 … -28.42281403 -2.67299047
15.41040602]
…
[-43.87766797 -43.40421224 -23.6082736 … 48.36364236 4.68260806
27.66254894]
[ 23.97072516 15.20922128 13.94062353 … 0.77054696 6.40791246
14.4748919 ]
[-11.08023178 12.19299834 6.73450952 … -13.72682163 -6.8847339
-1.21229063]]
9-2
def solve9_2(B,m):
b = random.normal(size=m)
print('Bx = b ,x:')
print(linalg.solve(B, b))
输出如下:
Bx = b ,x:
[ 1.89028797e+00 -3.65759128e-01 -7.02149677e-01 -1.08115207e+00
-9.39715470e-01 1.03054080e+00 -9.67349027e-01 -1.84639624e+00
8.04538312e-01 -7.62992652e-01 -7.55975604e-01 1.11220695e+00
-8.16366303e-01 -8.68131368e-01 7.55617335e-01 -5.42731869e-01
1.03377791e-01 1.39300041e+00 -1.07950103e+00 1.61501709e+00
2.23927441e+00 4.80012595e-01 -5.40381154e-01 1.08105331e+00
-2.34137139e-01 -4.56868016e-01 3.25698227e-01 8.82100154e-02
-5.70319705e-01 -3.80628330e-01 -4.88328171e-01 -1.28720341e+00
-2.84405571e-01 -1.19091632e+00 -2.20597814e-01 -5.54843553e-01
-1.09132365e+00 5.37521607e-01 2.31848622e-01 -1.20144253e-01
6.01694236e-01 2.88929781e-01 -5.15464922e-02 8.83128599e-01
4.97036090e-01 9.09328260e-01 6.38406978e-01 -6.28204407e-01
-1.15933970e+00 -2.63021883e-01 -6.79304565e-01 -5.34804920e-01
-5.78866193e-01 1.40005177e-01 2.17893866e-01 2.26476338e-01
2.12143147e-01 -1.43171233e+00 -3.39223149e-01 9.36211482e-01
-4.07672720e-01 -1.11714844e-01 1.60214937e+00 1.49614229e-01
-6.56291393e-01 9.50112367e-01 -1.47102973e-01 -1.31465449e+00
-2.74951207e-01 2.60146408e-01 5.17719010e-01 1.65225824e+00
-5.76066813e-01 -5.28872525e-01 4.63849075e-01 -4.27923280e-01
-6.28014824e-01 2.92238611e-01 -3.48682395e-01 5.20566470e-01
9.35915180e-01 -1.15715182e+00 -6.85393527e-01 1.26558757e-01
-7.39375555e-01 -3.33920986e-01 -6.56233534e-02 2.60318548e-02
1.02115124e+00 2.12735770e-02 -3.83904515e-01 -5.01024436e-01
-2.27104266e-01 -6.21109499e-01 2.92299614e-01 -4.51866781e-02
-3.08966360e-01 6.97600855e-01 6.25774237e-01 2.38582197e-02
-7.27872284e-01 2.07466775e-01 -6.58716896e-01 6.20343246e-01
6.23580936e-01 -3.42169726e-02 -3.69431184e-01 1.18062978e+00
6.62892686e-01 -1.76338364e+00 2.52674776e-02 -6.64919725e-01
-3.36123757e-01 9.05994691e-01 -1.05810492e-01 -4.08063794e-01
9.42555967e-01 -1.20319340e+00 -1.18028441e+00 2.85307032e-01
-1.66006447e-01 -3.91143752e-02 8.94869832e-01 1.26751305e+00
4.78816080e-01 9.22444422e-02 1.91861503e-01 4.41033322e-01
-8.07765807e-01 -7.61411385e-01 -6.83008554e-02 8.02298813e-01
7.22903510e-01 4.66599293e-01 -2.30069255e-01 5.53458191e-01
-4.11000873e-01 -1.21971134e+00 -5.54790011e-01 -2.85977101e-01
-4.11238518e-01 5.97517770e-01 1.07766922e+00 3.04402459e-01
-5.80142483e-02 -9.53714833e-01 1.99514252e-01 1.61998224e-01
-4.97608100e-01 4.17078758e-01 -5.25051413e-01 6.93429112e-02
1.30323519e+00 3.19543958e-01 -3.20272203e-01 5.36063301e-02
-3.49679955e-01 8.84529072e-01 1.22525110e+00 -7.91033257e-01
-2.93484233e-02 1.02926205e+00 -1.22764679e+00 -8.38390404e-01
-2.39647193e-01 -7.09810436e-01 5.68820666e-01 3.17878868e-01
9.03455363e-01 4.63985853e-01 -1.24736535e+00 1.37542207e-01
1.49498378e+00 -8.46338411e-01 -2.39169567e-01 6.73497305e-01
3.15179065e-01 1.33319313e+00 7.58102111e-02 -9.01102556e-01
-7.08276688e-01 -8.78041154e-01 -1.29439187e+00 1.14731328e-01
1.16164901e-01 3.42612467e-01 2.04095128e+00 1.74133820e+00
-2.57129637e-01 -1.78730916e-01 -1.66628349e-01 -9.81295021e-01
-9.65603231e-02 -2.91074046e-01 4.59633826e-01 -4.68259203e-02
4.39057816e-01 5.53885903e-01 -2.58194238e-01 -1.14439962e+00
3.10727096e-01 4.59837879e-01 -1.77115569e-01 1.19585748e+00
2.35635316e-01 2.22656109e-01 1.99133440e-01 -1.07273874e-01
-1.32319349e+00 -6.63500495e-01 -1.34279301e-01 -1.59385680e-01
1.81692971e-01 1.20845300e+00 -7.64238786e-01 1.12267171e-01
5.62810568e-01 9.20994017e-01 7.08830617e-01 1.32957534e+00
9.84073090e-01 6.84565775e-01 6.57785619e-01 -5.47357966e-01
-4.38315725e-01 -6.00680051e-01 -8.66754599e-01 -9.93739296e-01
7.87786543e-01 -1.99186481e-01 -4.24887154e-02 1.66551415e-01
-8.20752301e-01 1.23349903e-01 -6.74891400e-01 3.99069214e-02
-9.37834669e-02 -1.62424263e-01 7.15878001e-01 5.92066391e-01
-2.19916435e-01 -6.24540599e-01 4.38956684e-01 -3.72081667e-02
-7.60450870e-01 8.46074884e-01 -4.24446779e-01 -5.23845430e-01
1.67758362e+00 -3.18843070e-01 -1.04042706e+00 7.15845301e-01
-7.99610010e-01 -5.06494107e-01 1.06240913e-01 -6.48575741e-01
-4.36989530e-01 2.77307455e-01 -5.71606047e-02 -5.62284802e-02
7.33229768e-02 -1.47253074e-01 -5.74353431e-01 5.21317513e-01
1.74738490e-01 -9.14069450e-01 1.11297434e-01 1.09531437e+00
9.34643625e-01 -3.04383629e-01 -5.00593299e-01 -5.26885534e-02
-8.50277896e-01 -3.34355394e-01 1.05073386e+00 -8.43504674e-01
2.73230247e-01 -3.40936528e-01 -4.25020296e-01 9.88239697e-01
-1.10683041e+00 -8.87940451e-01 9.98235556e-01 4.56556848e-01
-3.74384828e-01 1.19361934e-01 -1.02374337e-01 9.21704879e-01
-4.94608508e-01 -1.82539249e-01 -4.91599544e-01 -9.79560606e-02
-2.95176657e-01 -8.34421182e-02 3.69732180e-01 -1.04037294e+00
-1.29412595e+00 -1.21253769e-01 -6.21789051e-01 -1.90763769e-01
1.85020801e-01 -3.32672830e-01 4.44890705e-01 -3.29729594e-01
6.34722562e-01 -6.98591110e-01 -1.46144682e-01 2.64765085e-01
-3.69180487e-01 -3.18253354e-01 9.15635363e-02 -5.28565337e-01
6.91382836e-01 4.15834913e-01 -9.13074681e-01 -3.93656146e-01
5.43237273e-01 -3.03321709e-01 -1.11896804e+00 -5.48910343e-01
5.76047258e-02 3.63960739e-01 4.93811266e-01 -5.10102231e-01
1.76728604e-03 7.80985412e-01 -4.04445705e-01 -6.57571934e-01
-5.06696110e-01 2.98497129e-01 -1.46654707e+00 -2.64658390e-01
1.18212235e+00 -2.11429844e-01 -1.78761358e+00 2.49487978e-01
-5.01858035e-01 -8.54399163e-01 -9.57657262e-01 -1.22806548e+00
1.97755568e-01 1.69721932e-01 -4.48322404e-01 4.02370498e-01
-2.23825804e-01 6.26692191e-01 2.74310845e-01 -7.38893147e-01
1.05250578e-01 8.30653408e-01 8.37293771e-01 9.07313914e-02
1.40013056e+00 -5.42335979e-01 1.81366466e-01 2.72702179e-01
-5.27119296e-01 -1.55324532e+00 5.51166261e-01 4.93923268e-01
1.04657415e-01 1.77478981e-01 -7.39039219e-01 -7.71948634e-01
-2.29252588e-01 1.45474289e-01 -4.23629196e-01 -2.19454561e-01
-3.18166325e-01 2.83334444e-01 5.75461773e-01 9.80740987e-01
-1.45353266e-01 6.96554828e-02 4.97441823e-01 1.11204395e+00
3.35261465e-03 2.20093949e-01 6.19906834e-01 -5.34305272e-01
-6.81677620e-02 -3.94156714e-01 6.81156810e-02 2.74499144e-01
1.21614164e+00 9.63584836e-02 5.05747743e-01 2.12598170e-01
-4.81674921e-01 8.71021385e-01 6.73036216e-02 -6.82439521e-01
4.73916694e-01 -1.08127940e-02 -9.29802752e-02 1.19458397e-01
-2.29230326e-01 4.58774965e-01 7.50690258e-01 -1.13451953e+00
1.96306278e-01 1.75828892e+00 -5.11110501e-01 9.66147699e-01
2.60325558e+00 1.88517416e-01 -9.32279156e-01 5.99844134e-01
-2.46883959e-02 -2.98727282e-01 4.96323159e-01 -1.83957725e-01
-4.73623213e-01 7.12062104e-01 -7.16963096e-01 4.84699301e-01
4.28390503e-01 -1.58829868e+00 2.62028868e-01 4.70686627e-01
5.04071761e-03 7.18677177e-01 1.48088414e-01 -3.44764879e-02
3.27428607e-01 -1.45874099e+00 2.51511450e-01 8.73980205e-01
-2.92989950e-01 -5.02374970e-01 1.34711734e+00 2.32278462e-01
-1.52194519e-01 1.20708647e+00 3.03418095e-01 -7.03445762e-01
1.49041285e-01 -3.76093370e-01 4.64544135e-01 -1.38304506e-01
-2.85837066e-01 -3.74440054e-01 -2.06485814e-01 -2.38392301e-01
-9.03508515e-01 -2.80116334e-01 6.46603356e-01 -2.88434382e-02
5.81287307e-02 5.48020019e-01 7.70012376e-01 8.44367144e-01
-4.66183675e-01 8.53275101e-01 -3.10095355e-01 -8.47215328e-01
-9.58239689e-02 -1.00027889e+00 -3.81206733e-01 1.81670486e+00
6.94773654e-02 -3.38725465e-01 6.66417880e-02 -5.26526752e-01
8.75122983e-01 3.52353025e-01 -3.08280716e-01 -6.10109945e-02
5.81530263e-01 -4.48271540e-01 3.58688341e-01 -1.15363375e+00
-7.27380231e-01 -7.84962689e-01 -1.02086453e+00 -5.39332996e-01
1.64172207e-01 9.67835880e-02 1.62896515e-01 9.62266800e-01
-1.10425406e+00 -5.48080091e-01 8.43168607e-01 -6.16906702e-01
1.10402367e+00 8.11554634e-01 -1.43626214e+00 6.27691794e-02
7.63559109e-01 7.61118084e-02 -1.61689862e+00 1.66825144e-01
1.93584012e+00 5.76341035e-01 -9.43435123e-01 3.14684073e-01
1.26437811e-01 -6.63235068e-01 5.16163004e-01 -6.94718099e-01]
9-3
def solve9_3(A,B,m):
A_F = linalg.norm(A, 'fro')
B_F = linalg.norm(B, inf)
print("Frobenius norm of A:")
print(A_F)
print("the infinity norm of B:")
print(B_F)
print("max singular:")
print(scipy.linalg.svdvals(B)[0])
print("min singular:")
print(scipy.linalg.svdvals(B)[m-1])
输出如下:
Frobenius norm of A:
316.91646345800467
the infinity norm of B:
398.31366791190186
max singular:
47.92349491296538
min singular:
0.13834192729462932
9-4
def solve9_4(n):
Z = random.normal(size=(n, n))
i = 0
m1 =ones(n)
this_norm = 0
time_start = time.clock()
while(1):
m2 = dot(Z,m1)
last_norm = this_norm
this_norm = linalg.norm(m2)
m1 = m2/this_norm
i = i + 1
if abs(this_norm-last_norm) < 0.0001:
break
time_end = time.clock()
print("largest eigenvalue:")
print(this_norm)
print("corresponding eigenvector of Z.")
print(m2)
print("iterations times:")
print(i)
print("using time:")
print(time_end-time_start)
输出如下:
largest eigenvalue:
13.480834697822916
corresponding eigenvector of Z.
[ 0.22236303 0.42279333 1.93881886 0.69586908 0.47126882 -0.78508996
1.40815728 -2.1517613 1.0985738 -0.2167273 -1.54458016 -0.19638357
0.15688997 -0.82843143 1.3360905 -0.41906515 -0.07685245 0.33032948
-0.78260966 -0.03661007 -0.62313946 -0.05099703 -1.20723754 -0.01766513
0.64434856 0.79640251 -0.54587171 -0.94124868 0.57922737 0.00345511
-1.55106912 -0.27211316 0.962468 -0.23330768 0.44152155 -1.94543229
-0.31322954 2.90207717 -0.3077329 0.67358867 0.72080989 0.65521668
1.01327948 1.00720839 -0.89078699 0.02194493 -0.98653297 -0.20395716
-1.28295599 0.51483984 0.47295213 0.38731822 1.73106145 -0.21767262
-0.02799145 -0.19218766 -0.92262238 0.98276682 -0.01403889 -0.17595213
1.71104918 -1.37210981 -0.80015957 1.4215597 -1.67812024 -0.66067888
-0.67062915 -1.15734477 0.25346135 0.38107729 0.4737737 1.14107486
-0.17003712 1.14956426 -1.30179975 -0.50313889 0.74753339 -0.34944898
-0.14157243 -0.92173997 -0.92442893 1.78199553 0.81073464 1.83891855
0.43113903 -0.63455975 0.60997776 -0.58509905 -0.14585786 0.08028681
1.37604063 1.27991396 -1.67140945 -0.01770725 1.02834992 0.31706468
-1.11141462 2.46540574 0.04497793 0.96987765 0.28318274 0.45646249
-0.24353658 -0.20611197 -0.39851747 1.4079342 -0.55627691 -0.20497916
-1.18908875 -0.83126651 0.57965062 0.09549015 -0.52458348 -0.89144196
-0.47081123 -2.17551051 0.66606337 0.08510564 0.80416976 -1.60788507
-2.52487386 -0.8821207 -0.59240145 0.50854285 0.09461113 -1.04462777
-0.07574806 -0.27664601 -2.37101159 -0.05847874 0.61168508 0.1369635
1.27491607 -0.64337485 0.93668492 -0.78105782 0.92418752 -0.4610074
0.11330022 -0.75684723 -1.11562608 0.0210896 -0.72610324 0.63001531
1.58609104 0.40553665 -0.19244669 0.32738185 -0.65609278 0.69565156
-0.18017314 -1.2550441 0.42531429 0.5534141 0.06451233 0.09690268
0.84840746 -1.01092939 0.89680419 0.81923561 -0.16170814 0.11710908
-2.17612057 1.00997824 0.89844871 0.29258391 -1.88741636 -0.74154098
-1.27342824 -1.27805181 1.08837984 -0.21433917 -0.03896373 0.25284017
-0.60216128 0.14642559 -0.91691158 -0.08594362 0.46891706 -0.41127789
0.53473546 0.88899728 -0.09103233 1.5885868 -0.01309329 -0.86663526
-0.25547096 2.54847274 1.31369639 0.24991523 -0.39099077 -0.71190642
-0.34695598 1.99290962 -0.68591052 1.00980626 -0.60778651 -1.48872196
-0.13207539 0.78278478]
iterations times:
1736
using time:
0.08410471553477154
9-5
def sovle9_5(n,p):
C = random.binomial(1,p,(n,n))
print("singular value is :")
print(scipy.linalg.svdvals(C))
输出如下:
singular value is :
[1.00444300e+02 1.38000091e+01 1.35347118e+01 1.33624372e+01
1.32206037e+01 1.31523268e+01 1.30166068e+01 1.29561379e+01
1.27851973e+01 1.25967479e+01 1.24455225e+01 1.24396605e+01
1.23254703e+01 1.21251868e+01 1.19574959e+01 1.18957043e+01
1.17743657e+01 1.16497008e+01 1.16126916e+01 1.14972968e+01
1.14206302e+01 1.12728320e+01 1.11230836e+01 1.10548155e+01
1.09752822e+01 1.09375519e+01 1.08061541e+01 1.06977432e+01
1.06552093e+01 1.05709440e+01 1.05113393e+01 1.03754247e+01
1.03523772e+01 1.02104763e+01 1.01205945e+01 1.00816335e+01
9.97909454e+00 9.91808180e+00 9.86865315e+00 9.70296197e+00
9.65261665e+00 9.59623662e+00 9.48409261e+00 9.45824752e+00
9.39157299e+00 9.33353015e+00 9.23791852e+00 9.17417937e+00
9.07154973e+00 9.03508724e+00 9.00265563e+00 8.88891710e+00
8.83271354e+00 8.77233665e+00 8.67141533e+00 8.60270703e+00
8.55770167e+00 8.47330162e+00 8.46346098e+00 8.39028553e+00
8.30371359e+00 8.22395650e+00 8.16340776e+00 8.13557994e+00
8.05459102e+00 7.94321044e+00 7.88729438e+00 7.84548013e+00
7.78465102e+00 7.77227674e+00 7.56782992e+00 7.47456716e+00
7.41267234e+00 7.36754044e+00 7.33175207e+00 7.24259676e+00
7.22928161e+00 7.12520523e+00 7.11135260e+00 7.02951795e+00
6.99919666e+00 6.89829282e+00 6.78106614e+00 6.75746156e+00
6.70511017e+00 6.64884487e+00 6.54553619e+00 6.51030606e+00
6.46303365e+00 6.43326287e+00 6.41625105e+00 6.31171910e+00
6.20765636e+00 6.15532967e+00 6.11387682e+00 6.04616230e+00
5.92801456e+00 5.91723447e+00 5.77984894e+00 5.73257001e+00
5.69324535e+00 5.59368655e+00 5.52718567e+00 5.52545603e+00
5.48717653e+00 5.38133357e+00 5.32391297e+00 5.25959976e+00
5.21911863e+00 5.17701335e+00 5.14063516e+00 4.95968356e+00
4.91012591e+00 4.90716890e+00 4.87229286e+00 4.74444308e+00
4.67673321e+00 4.65449446e+00 4.57650499e+00 4.53719736e+00
4.49342676e+00 4.44422776e+00 4.38328260e+00 4.34205455e+00
4.26098120e+00 4.21859969e+00 4.18541395e+00 4.09194725e+00
3.98971497e+00 3.96587343e+00 3.93319344e+00 3.89529288e+00
3.83334354e+00 3.75921969e+00 3.73443515e+00 3.70058109e+00
3.64423501e+00 3.54464564e+00 3.44431487e+00 3.41622221e+00
3.31842163e+00 3.30428993e+00 3.21800774e+00 3.18771075e+00
3.10172260e+00 3.05526723e+00 3.00878734e+00 2.93829150e+00
2.91250194e+00 2.86977716e+00 2.85077813e+00 2.79506800e+00
2.70822848e+00 2.68249293e+00 2.58580060e+00 2.53792753e+00
2.44044828e+00 2.39504315e+00 2.33013160e+00 2.26704656e+00
2.24085460e+00 2.13705111e+00 2.09262800e+00 2.04467320e+00
1.95751184e+00 1.93599491e+00 1.87402440e+00 1.82306732e+00
1.74261789e+00 1.70394631e+00 1.64736229e+00 1.61406598e+00
1.51145846e+00 1.45450532e+00 1.42768514e+00 1.34995886e+00
1.31420903e+00 1.29847211e+00 1.22896830e+00 1.17385071e+00
1.12380230e+00 1.02859998e+00 9.63243951e-01 9.33835761e-01
8.71042073e-01 8.09963001e-01 6.78442802e-01 6.37597642e-01
5.64042926e-01 5.36875728e-01 4.85753295e-01 4.44820839e-01
4.27205656e-01 4.11754624e-01 3.69016344e-01 3.29138933e-01
2.03285919e-01 1.60010573e-01 7.12420777e-02 2.41618404e-02]
9-6:
先生成一个矩阵A减去z值的绝对值矩阵,再从中选出最小值的索引。
def solve9_6(A,z):
temp_A = abs(A-z)
print("the element in A that is closest to z is :")
print(A[argmin(temp_A)//500][argmin(temp_A)%500])
输出如下:
the element in A that is closest to z is :
0.4999918521790633
完整主函数如下:
n = 200
m = 500
A = random.randn(n,m)
col = random.normal(size=m)
row = random.normal(size=m)
B = toeplitz(col,row)
print(A)
print(B)
solve9_1(A,B)
solve9_2(B,m)
solve9_3(A,B,m)
solve9_4(n)
sovle9_5(n,p=0.5)
solve9_6(A,0.5)