高级编程技术作业_16 Numpy练习题

9.1

输出结果:


    A =  [[ 70.40406743   2.02668602  94.54645001 ... 110.30864041 125.31322128
  119.79697459]
 [ 71.64816903  77.49512505 109.55139075 ...  78.44655339 158.64178009
  134.17303738]
 [141.89214559  42.20967935  70.17682633 ... 121.02294946 145.01765842
   53.30917758]
 ...
 [ 29.67143468  85.53905186 112.22148746 ... 110.65436129  -0.82559784
   64.5988095 ]
 [ 35.44325688 127.43012215 157.37878886 ...  77.20288963  50.69495693
   93.31628925]
 [ 44.30347401  62.54296209  99.36843126 ...  85.33538177 131.24167475
   22.93172829]]

B =  [[1000 1002 1003 ... 1498 1499 1500]
 [ 999 1000 1002 ... 1497 1498 1499]
 [ 998  999 1000 ... 1496 1497 1498]
 ...
 [ 503  504  505 ... 1000 1002 1003]
 [ 502  503  504 ...  999 1000 1002]
 [ 501  502  503 ...  998  999 1000]]

A + A =  [[140.80813487   4.05337203 189.09290001 ... 220.61728082 250.62644256
  239.59394918]
 [143.29633806 154.9902501  219.10278149 ... 156.89310677 317.28356018
  268.34607476]
 [283.78429119  84.4193587  140.35365266 ... 242.04589891 290.03531684
  106.61835516]
 ...
 [ 59.34286936 171.07810372 224.44297491 ... 221.30872258  -1.65119568
  129.19761901]
 [ 70.88651377 254.8602443  314.75757772 ... 154.40577925 101.38991386
  186.6325785 ]
 [ 88.60694802 125.08592418 198.73686252 ... 170.67076355 262.4833495
   45.86345658]]

AA^T =  [[6291826.55410204 4910960.06037724 4999961.72216207 ... 4866946.37001682
  4788549.27568732 5211085.7377057 ]
 [4910960.06037724 6020397.7378217  4787681.87597683 ... 4567348.78290756
  4529318.56493714 4978451.47927264]
 [4999961.72216207 4787681.87597683 5978799.56133382 ... 4652153.24062239
  4716925.48220016 5005117.92893685]
 ...
 [4866946.37001682 4567348.78290756 4652153.24062239 ... 5741347.00008402
  4572317.02150298 4946040.59588408]
 [4788549.27568732 4529318.56493714 4716925.48220016 ... 4572317.02150298
  5942580.79946773 4900690.12142427]
 [5211085.7377057  4978451.47927264 5005117.92893685 ... 4946040.59588408
  4900690.12142427 6436550.22171554]]

A^TA =  [[2238136.43365881 1826802.00805274 1802362.47677257 ... 1783015.28985001
  1738214.39854477 1925627.2033188 ]
 [1826802.00805274 2375845.40935941 1957716.06537959 ... 1888131.25621721
  1889109.17956934 2071089.36193791]
 [1802362.47677257 1957716.06537959 2428871.61033573 ... 1885620.86793199
  1795648.70608097 2045039.70884669]
 ...
 [1783015.28985001 1888131.25621721 1885620.86793199 ... 2368561.91801863
  1908990.72488245 2100275.24934264]
 [1738214.39854477 1889109.17956934 1795648.70608097 ... 1908990.72488245
  2357671.54228724 2023860.39099963]
 [1925627.2033188  2071089.36193791 2045039.70884669 ... 2100275.24934264
  2023860.39099963 2756460.56746607]]

AB =  [[38256892.58706635 38307600.21590782 38358239.46736789 ...
  63473875.10570574 63524622.6391202  63575385.17711553]
 [36704920.01173018 36753533.36181194 36802152.55884971 ...
  60878316.30290203 60926936.45136812 60975636.79506096]
 [37101842.27103059 37151262.05474441 37200582.15599199 ...
  61641912.92223003 61691311.83674774 61740734.74597438]
 ...
 [35693768.90678396 35741389.3549666  35789065.67076642 ...
  59393801.29969446 59441502.73080368 59489092.68195383]
 [36007430.86512521 36055378.81160522 36103418.74495051 ...
  59867636.25611033 59915625.96222308 59963589.16040315]
 [38308176.0480487  38359408.90540665 38410660.0022527  ...
  63799836.37347015 63851110.26273588 63902430.05829459]]

[[38256540.56672918 38307590.08247773 38357766.73511786 ...
  63473323.56250369 63523996.0730138  63574786.19224258]
 [36704561.77088503 36753145.8861867  36801604.80189598 ...
  60877924.07013509 60926143.24246768 60974965.92987405]
 [37101132.81030262 37151051.00634766 37200231.27186035 ...
  61641307.80748275 61690586.74845563 61740468.20008649]
 ...
 [35693620.54961057 35740961.65970729 35788504.56332914 ...
  59393248.02788801 59441506.85879289 59488769.68790631]
 [36007253.64884079 36054741.66099447 36102631.8510062  ...
  59867250.24166219 59915372.48743843 59963122.57895689]
 [38307954.53067864 38359096.19059621 38410163.1600964  ...
  63799409.69656128 63850454.05436213 63902315.39965314]]

代码展示:

from numpy import *
from scipy import linalg 


n = 200
m = 500
# Matrix A
A = random.normal(100, 50, 500 * 200).reshape(200, 500)

# Matrix B
a = arange(1000, 500, -1)
b = arange(1001, 1501)
B = linalg.toeplitz(a, b)

print("A = ", A)
print("B = ", B)


# A + A
print("A + A = ", A + A)
# AA^T
print("AA^T = ", A.dot(A.T))
# A^TA
print("A^TA = ", A.T.dot(A))
# AB
print("AB = ", A.dot(B))

# caculate A(B-λI)
def fun(r):
    return(dot(A, B-r*eye(500)))

print(fun(5))

9.2

输出结果:

result = 
[-9.05237869  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476  0.01610476
  0.01610476  0.01610476]

代码展示:

# Solve Bx = b
a = arange(1000, 500, -1)
b = arange(1001, 1501)
B = linalg.toeplitz(a, b)

x = linalg.solve(B, b)
print("result = \n",x)

9.3

输出结果:

Frobenius norm of A:  35318.008740531346
infinity norm of B:  625249.0
smallest singular value of B:  0.4999364459615248
largest singular value of B:  510492.87852403277

代码展示:


print("Frobenius norm of A: ", linalg.norm(A, ord='fro'))
print("infinity norm of B: ", linalg.norm(B, ord=inf))

svd = linalg.svdvals(B)
print("smallest singular value of B: ", min(svd))
print("largest singular value of B: ", max(svd))

9.4

输出结果:

largest eigenvalue:  1526.9722096821552
eigenvector:  [1408.99079328 1450.94952533 1411.47692745 1402.24926885 1414.24694316
 1406.4410868  1442.15756602 1404.77223135 1486.63020002 1374.52959856
 1460.65377191 1373.73815067 1391.81592264 1358.71290103 1498.40913297
 1376.60539882 1372.04026737 1390.33947849 1374.7744408  1381.28120218
 1430.59697048 1402.20752691 1297.65534152 1391.21064899 1430.32470807
 1414.31780251 1335.76440313 1420.51153749 1335.76188437 1359.08510289
 1355.25789317 1420.78063266 1368.1400817  1417.43044708 1386.51621399
 1364.58493744 1362.39323034 1425.95024128 1306.9811264  1417.75340241
 1327.27341398 1415.96638614 1302.04382763 1418.49628945 1385.05986977
 1443.66679247 1391.38001492 1448.45510952 1412.33462227 1384.82578567
 1348.18663862 1419.96625137 1456.21962838 1429.42284831 1364.20565699
 1314.92955853 1478.97845701 1353.4035212  1359.02838025 1328.93965773
 1373.48641328 1420.92672638 1413.2058963  1437.16207611 1360.94096814
 1442.17584858 1444.164669   1405.55938948 1512.00073387 1434.90645396
 1385.8147288  1360.15771322 1441.94212898 1438.3787635  1447.72090275
 1479.95326581 1445.08243542 1429.5761765  1471.69081229 1399.45906163
 1335.94811415 1404.55618072 1387.36394991 1368.58211173 1433.03890559
 1464.94454783 1489.21555571 1402.90326617 1430.51195744 1496.0909324
 1404.74238564 1408.88950258 1485.46102845 1423.30668121 1444.8841709
 1478.55276512 1299.2155357  1376.64971052 1338.09573641 1420.04682438
 1440.86129618 1371.3516449  1379.14519173 1424.06479953 1455.49072581
 1430.52373358 1399.87248982 1426.08888526 1333.84351633 1410.78382717
 1457.22547113 1389.19086009 1475.84183985 1467.19083104 1456.21257285
 1388.79584522 1406.64434223 1309.99794288 1397.8456633  1322.38315977
 1431.00858573 1377.30317487 1410.82585774 1426.32833711 1354.23487409
 1327.19432262 1454.82253794 1338.16572151 1497.0786195  1436.3671893
 1409.63667159 1414.99843012 1443.02689026 1508.18452646 1394.88155117
 1420.72753967 1368.91239124 1409.71893103 1370.5218434  1413.44324003
 1400.87690744 1507.30086478 1382.55978698 1475.12220581 1384.4090953
 1434.0327868  1392.31766288 1367.9476881  1478.21836777 1434.57033412
 1406.58549445 1445.4046114  1450.51889302 1436.51000248 1471.5765792
 1338.81412991 1412.26249067 1526.97220968 1474.95672038 1470.26490562
 1431.52551215 1289.51716482 1363.35444569 1438.64074378 1459.32456572
 1486.57191775 1371.30895081 1338.06222645 1438.46669692 1401.02533628
 1438.96822295 1395.19909032 1447.9464505  1439.82755031 1487.20164772
 1421.32667504 1452.82354413 1482.73835341 1439.78423896 1415.1194918
 1420.86275717 1448.37117153 1371.95521952 1409.81425291 1439.36673612
 1350.68709589 1373.28627945 1416.92475966 1471.33666331 1437.52778567
 1325.61565617 1340.75843665 1444.0692916  1374.84990021 1432.2014434
 1459.65504602 1401.4211842  1372.73031576 1415.30565122 1365.78381785]

iteration needed:  0.01818046060486722

代码展示:

import time
n = 200

Z = random.normal(100, 50, n * n).reshape(n, n)

x = ones(n)
y = zeros(n)
z = ones(n)
t = time.clock()

while abs(max(y-z)) > 0.001:
    z = y
    y = Z.dot(x)
    x = y / linalg.norm(y, ord=2)

print("largest eigenvalue: ", max(y))
print("eigenvector: ", y)
print("iteration needed: ", time.clock() - t)

9.5

输出结果:

      p=0.1  p=0.3  p=0.5  p=0.7  p=0.9
n=100 90.05  70.36  50.15  31.52  10.93

n=110 99.04  77.4  54.57  33.32  12.25

n=120 108.03  84.36  60.96  37.03  13.0

n=130 116.93  91.53  65.54  39.67  13.35

n=140 125.87  97.91  69.43  42.26  14.6

n=150 135.02  105.94  75.05  45.16  15.88

n=160 144.42  112.46  79.42  49.01  16.99

n=170 153.12  119.4  85.64  51.35  17.94

n=180 161.92  127.06  90.15  55.06  18.97

n=190 171.34  132.3  95.9  57.78  19.6

n=200 180.49  139.63  99.87  61.27  21.25

代码展示:

print("    ")
for p in range(1,11,2):
    print("p="+str(p/10.0), end="  ")

print("\n")
for n in range(100, 201, 10):
    print("n=" + str(n), end=" ")
    for p in range(1,11, 2):
        p = p / 10.0
        vec = [1 if p <= random.random() else 0 for i in range(0,n*n)]
        vec = array(vec)
        mat = vec.reshape(n,n)
        largest = round(max(linalg.svdvals(mat)),2)
        print(str(largest)+"  ", end="")

    print("\n")

可见随n增大而增大,随p增大而减小

9.6

输出结果:

A =  [[ 69.65743405  28.60068372 110.89712066  92.72582677  95.89859686
  128.39538731 198.13787984 172.99568792  -3.01522301   2.49526605]
 [ 98.71575943 110.3263429   81.938169    31.45225857 114.6506545
   73.0570406  173.30631843 242.31932991 154.77697217  90.2994558 ]
 [116.95634684 115.43468615 162.74759403  73.6318596  138.40063129
   54.26407965  46.77271818  73.51592642  54.31344732  63.46584513]
 [173.13744494  54.73599029  89.2847848  210.06934598  -1.96858511
   34.94944172 113.67334593  75.95537041 220.77959291  73.6030034 ]
 [ 74.01940898  72.52469092  83.9433482   61.83566294  78.43366547
  181.67178951 136.50645831 114.16955271  86.64643397 164.73753472]
 [ 33.44376272 167.38535504 155.01751624  14.28325307  96.89762787
  106.28287207 154.55877193  55.90322332  65.68756021 134.19873589]
 [ 53.98439143  54.04044119  47.39697463 105.89905506 129.41864305
  197.32025917 184.63253913 124.42793348 194.86206829  73.36744223]
 [ 82.4367627   91.13536188  68.68086659 140.61789471  78.28016705
   13.93466117 165.78438664  30.99826484 151.89839055 174.49636668]
 [122.20076039  77.33298645  86.59636449 -31.3336203  132.6090598
   36.53493356 102.33043455  73.86160768 193.73319936  60.40198394]
 [  8.85149787 103.59156346 125.65906932 109.30689323  80.62510491
   23.71598472  54.60391918  46.28608859  76.76828179  60.0425344 ]]

z =  98
nearest number in A to z is  98.715759428647

代码展示:

def nearest(z, A):
    if argwhere(A==z).size != 0:
        return z
    else:        
        temp1 = argwhere(A>z)
        temp2 = argwhere(A<z)
        if temp1.size != 0 and temp2.size != 0:
            x = min([A[i[0]][i[1]] for i in temp1])
            y = max([A[i[0]][i[1]] for i in temp2])
            if x - z > z - y:
                return y
            else:
                return x
        elif temp1.size != 0:
            return min([A[i[0]][i[1]] for i in temp1])
        else:
            return max([A[i[0]][i[1]] for i in temp2])


A = random.normal(100, 50, 10 * 10).reshape(10, 10)
z = z = random.randint(50,150)
print("A = ", A)
print("\nz = ", z)
print("nearest number in A to z is ", nearest(z, A))

猜你喜欢

转载自blog.csdn.net/akago9/article/details/80383280