python第11周作业

1.Generate matrices A, with random Gaussian entries, B, a Toeplitz matrix, where A Rn×m and B Rm×m,

for n = 200, m = 500.

import numpy as np 
import time
import scipy
import scipy.linalg

np.random.seed(int(time.time()))
A = np.random.normal(0, 1, (200, 500))
B = scipy.linalg.toeplitz(list(range(1,501)))
Exercise 9.1: Matrix operations
Calculate A + A, AA>, A>A and AB. Write a function that computes A(B λI) for any λ.
np.random.seed(int(time.time()))
A = np.random.normal(0, 1, (200, 500))
B = scipy.linalg.toeplitz(list(range(1,501)))

AaA = A + A
AAT = np.matmul(A, A.T)
ATA = np.matmul(A.T, A)
AB = np.matmul(A, B)

def func(c):
	T = B - c * np.identity(500)
	return np.matmul(A, T)

结果:

[[-2.43481625  0.30609404 -1.04089433 ...,  2.06474909 -1.40572007
   1.59820044]
 [-2.145266   -1.37180988  0.47677339 ...,  3.13621931 -2.34917689
  -2.86347192]
 [ 1.39579581  2.3582495   0.30430538 ...,  0.36531283  1.11695448
   0.81979878]
 ..., 
 [-0.36408384  4.20189382  1.64300896 ..., -3.91156688 -2.00446271
  -0.37220382]
 [ 0.6513696  -0.69248735 -0.06958598 ...,  0.36882953 -0.71120829
   0.75370296]
 [-3.21804384  1.67379258 -3.94242806 ...,  0.23953906  2.15602927
   1.08936674]]
[[  4.88042142e+02   3.24708429e-01  -1.23663493e+01 ...,  -3.84843748e+01
    2.81886255e+01   4.09658717e+01]
 [  3.24708429e-01   4.71415031e+02  -2.02349093e+01 ...,  -6.15256393e+00
   -1.44221999e+00   3.95308830e+01]
 [ -1.23663493e+01  -2.02349093e+01   5.11639534e+02 ...,  -4.26799895e+01
   -1.27156660e+01  -7.32146132e+00]
 ..., 
 [ -3.84843748e+01  -6.15256393e+00  -4.26799895e+01 ...,   5.14871637e+02
   -1.37220534e+01  -4.47321696e+01]
 [  2.81886255e+01  -1.44221999e+00  -1.27156660e+01 ...,  -1.37220534e+01
    4.93422434e+02  -4.62980419e+01]
 [  4.09658717e+01   3.95308830e+01  -7.32146132e+00 ...,  -4.47321696e+01
   -4.62980419e+01   4.97840981e+02]]
[[ 185.10360245   11.87797242   -0.79199176 ...,    6.89548094  -25.2321327
   -16.28313649]
 [  11.87797242  189.94158862    0.51663773 ...,   13.813414      4.53860253
   -15.29495292]
 [  -0.79199176    0.51663773  173.34197838 ...,    5.91059159
     8.83135864    1.4515256 ]
 ..., 
 [   6.89548094   13.813414      5.91059159 ...,  221.45655496
     9.81504408   -1.26396403]
 [ -25.2321327     4.53860253    8.83135864 ...,    9.81504408  201.9162066
    -7.10646931]
 [ -16.28313649  -15.29495292    1.4515256  ...,   -1.26396403
    -7.10646931  198.86807285]]
[[  2751.75395236   2750.13073873   2748.81361915 ...,  -3154.9529813
   -3155.9570643   -3158.36686737]
 [  3823.04583129   3811.89239909   3799.36715702 ...,    663.95298093
     678.17379594    690.04543406]
 [  2658.88399701   2664.56583641   2672.60592531 ...,  -4794.86319598
   -4801.08599283  -4806.19183519]
 ..., 
 [  4124.91097027   4112.01367127   4103.3182661  ...,   2126.4145235
    2141.32440519   2154.22982417]
 [ 11423.95393771  11400.39055979  11376.13469451 ...,    660.00127418
     684.17352703    707.63457158]
 [ -6648.26444508  -6629.581224    -6609.22421034 ...,  -4276.13198888
   -4301.27864982  -4324.26928148]]
[[  2754.18876861   2749.82464469   2749.85451348 ...,  -3157.01773039
   -3154.55134423  -3159.96506781]
 [  3825.19109729   3813.26420897   3798.89038363 ...,    660.81676162
     680.52297283    692.90890598]
 [  2657.4882012    2662.20758691   2672.30161993 ...,  -4795.22850881
   -4802.20294731  -4807.01163397]
 ..., 
 [  4125.27505411   4107.81177745   4101.67525713 ...,   2130.32609038
    2143.3288679    2154.60202799]
 [ 11423.30256811  11401.08304714  11376.2042805  ...,    659.63244465
     684.88473532    706.88086862]
 [ -6645.04640123  -6631.25501658  -6605.28178228 ...,  -4276.37152794
   -4303.43467908  -4325.35864823]]
[Finished in 0.8s]
Exercise 9.2: Solving a linear system
Generate a vector b with m entries and solve Bx = b.
b = np.random.random(500) * 500
x = np.linalg.solve(B, b)

结果:

[  2.91863964e-01   1.10752802e+02  -2.64336406e+02   2.37546766e+02
  -1.09032341e+02   9.16481966e+01  -1.79691165e+02   2.55673896e+02
  -1.73717600e+02  -3.70264353e+00   4.96868138e+01  -6.08914025e+01
  -4.53682636e+01   6.03112957e+01   2.22032512e+02  -3.27236563e+02
   7.98886999e+01   1.22327097e+02  -1.21550891e+02   9.71128273e+01
   2.27727003e+01  -4.17147694e+01  -1.44261875e+02   2.13355012e+02
   1.29749380e+01  -2.45278907e+02   1.68072690e+02   3.62124701e+01
  -1.16325155e+02   1.61347938e+02  -2.44412686e+02   1.67543448e+02
   8.06111200e+01  -1.66895771e+02  -4.27944144e+01   1.46689230e+02
  -1.64667279e+02   1.13228575e+02   4.16131875e+01   2.80574337e+01 ...]

Exercise 9.3: Norms

Compute the Frobenius norm of A: kAkF and the infinity norm of B: kBk. Also find the largest and
smallest singular values of B.

norma = np.linalg.norm(A, 'fro')
normb = np.linalg.norm(B, np.inf)
s = np.linalg.svd(B, compute_uv=0)
print(norma)
print(normb)
print(max(s))
print(min(s))

结果:

314.732306846
125250.0
87334.5204564
0.500004934835
Exercise 9.4: Power iteration
Generate a matrix Z, n × n, with Gaussian entries, and use the power iteration to find the largest
eigenvalue and corresponding eigenvector of Z. How many iterations are needed till convergence?
Optional: use the time.clock() method to compare computation time when varying n.

from time import clock
def power_iteration(Z, stop):  
    u = np.random.normal(size=[len(Z)]).transpose()  
    u = u/np.max(u)  
    v = u.copy()  
    eigen_change = np.inf  
    eigen_val = np.max(v)  
  
    iteration = 0  
    s_time = time.clock()  
    while abs(eigen_change) > stop:  
        v = np.matmul(Z, u)  
        max_v = np.max(v)  
        eigen_change = eigen_val - max_v  
        eigen_val = max_v  
        u = v/max_v  
        iteration += 1  
    print("The result is {}".format(np.allclose(np.matmul(Z, u), eigen_val*u)))  
    print("Total iteration: {}".format(iteration))  
    print("Total time: {}s".format(time.clock() - s_time))  
    return [eigen_val, u, iteration, time.clock() - s_time] 

Z = np.random.normal(0, 1, (200, 200))
power_iteration(Z, 0.0000001)


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
the linear algebra library of Scipy to compute the singular values of C. What can you say about the
relationship between n, p and the largest singular value?

p_ = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]  
for p in p_:  
    C = np.random.random(size=[n, n])  
    for i in range(n):  
        for j in range(n):  
            C[i][j] = 1 if C[i][j] > p else 0  
  
    s= np.linalg.svd(C, compute_uv=False)  
    m = np.max(s)  

关系: n*(1-p) ≈ max_singular

Exercise 9.6: Nearest neighbor
Write a function that takes a value z and an array A and finds the element in A that is closest to z. The
function should return the closest value, not index.
Hint: Use the built-in functionality of Numpy rather than writing code to find this value manually. In
particular, use brackets and argmin.

def func(A, z):
	index = np.argmin(np.abs(A - z))
	return A[index]



你可能感兴趣的:(python)