题目概述:
代码展示:
import numpy as np from scipy.linalg import toeplitz print('for the follow tests, let n = 3, m = 5, and\n') n = 3 m = 5 A = np.mat(np.random.rand(n, m)) B = np.mat(toeplitz(np.random.rand(1, m), np.random.rand(1, m))) print('A = ') print(A, '\n') print('B = ') print(B, '\n\n') #练习9.1 print('------Exercise 9.1: Matrix operations------\n') print('A + A = \n', A+A, '\n') print('A*A^T = \n', A*A.T, '\n') print('A^T*A = \n', A.T*A, '\n') print('A*B = \n', A*B) lamda = input("input 'λ' : ") lamda = int(lamda) print('A*(B-λI) = \n', A*(B-lamda*np.eye(m)), '\n\n') #练习9.2 print('------Exercise 9.2: Solving a linear system------\n') b = np.array(np.random.rand(m, 1)) print('b = \n', b, '\n') print('solution of Bx = b : \nx = \n', np.linalg.inv(B)*b, '\n\n') #练习9.3 print("------Exercise 9.3.Norms------\n") print('||A||F = \n', np.linalg.norm(A), '\n') print('||B||∞ = \n', np.linalg.norm(B, np.inf), '\n') print('The largest singular value of B : \n', np.linalg.norm(B, 2), '\n') print('The smallest singular value of B : \n', np.linalg.norm(B, -2), '\n\n') #练习9.4 print('------Exercise 9.4.Power iteration------\n') Z = np.mat(np.random.rand(n, n)) print('Z = \n', Z, '\n') val, vec = np.linalg.eig(Z) print('The eigenvalue of Z : \n', val, '\n') print('The eigenvector of Z : \n', vec, '\n\n') #练习9.5 print('------Exercise 9.5.Singular values------') p = input("input 'p' : ") p = float(p) C = np.mat(np.random.binomial(1, p, n*n)) C.resize(n, n) print('C = \n', C) print('The largest singular value of C :\n', np.linalg.norm(C, 2), '\n\n') #练习9.6 print('------Exercise 9.6: Nearest neighbor------\n') print('A = \n', A, '\n') z = np.random.random() print('z = \n', z, '\n') min = 100 num = 0 for i in range(n): for j in range(m): if np.fabs(A[i, j] - z) < min: min = np.fabs(A[i, j] - z) num = A[i, j] print('The closest element in A is : \n', round(num, 8))
运行结果:
for the follow tests, let n = 3, m = 5, and A = [[ 0.3186692 0.32585018 0.86686889 0.38635469 0.6165809 ] [ 0.51453018 0.6142813 0.40638036 0.55112143 0.56725979] [ 0.32387318 0.72992392 0.40869537 0.08761137 0.92279275]] B = [[ 0.7885492 0.54800173 0.67653105 0.55925278 0.0597043 ] [ 0.84306196 0.7885492 0.54800173 0.67653105 0.55925278] [ 0.74935218 0.84306196 0.7885492 0.54800173 0.67653105] [ 0.05049173 0.74935218 0.84306196 0.7885492 0.54800173] [ 0.53214501 0.05049173 0.74935218 0.84306196 0.7885492 ]] ------Exercise 9.1: Matrix operations------ A + A = [[ 0.63733841 0.65170036 1.73373777 0.77270937 1.23316179] [ 1.02906036 1.2285626 0.81276073 1.10224286 1.13451957] [ 0.64774636 1.45984784 0.81739073 0.17522273 1.84558551]] A*A^T = [[ 1.48863201 1.27909699 1.29816499] [ 1.27909699 1.43274632 1.35285464] [ 1.29816499 1.35285464 1.66393688]] A^T*A = [[ 0.4711852 0.65630746 0.61770485 0.43506292 0.78722545] [ 0.65630746 1.01630878 0.83041777 0.52838697 1.22293858] [ 0.61770485 0.83041777 1.08363857 0.59469015 1.14215916] [ 0.43506292 0.52838697 0.59469015 0.46068053 0.63169508] [ 0.78722545 1.22293858 1.14215916 0.63169508 1.55350214]] A*B = [[ 1.52320649 1.48305232 1.86548202 1.69818563 1.48564967] [ 1.55782312 1.55058426 1.89488064 1.83885146 1.39851487] [ 1.672501 1.20986468 1.70674481 1.74596628 1.47972238]] input 'λ' : 4 A*(B-λI) = [[ 0.24852968 0.1796516 -1.60199353 0.15276688 -0.98067392] [-0.5002976 -0.90654095 0.26935918 -0.36563426 -0.87052428] [ 0.37700829 -1.709831 0.07196334 1.39552081 -2.21144864]] ------Exercise 9.2: Solving a linear system------ b = [[ 0.2878287 ] [ 0.9143979 ] [ 0.87991404] [ 0.82151291] [ 0.60839439]] solution of Bx = b : x = [[-0.03337093] [ 0.62972514] [-0.44601544] [ 0.40468524] [ 0.74491808]] ------Exercise 9.3.Norms------ ||A||F = 2.14133491521 ||B||∞ = 3.60549611819 The largest singular value of B : 3.1538525343 The smallest singular value of B : 0.18523465047 ------Exercise 9.4.Power iteration------ Z = [[ 0.47981805 0.88084516 0.07030881] [ 0.87985725 0.0012207 0.88335738] [ 0.47377535 0.02662835 0.65682165]] The eigenvalue of Z : [ 1.4256607 -0.49845654 0.21065624] The eigenvector of Z : [[-0.63646159 -0.65731379 -0.68217298] [-0.6503228 0.70980817 0.15135371] [-0.41472508 0.25320139 0.71535451]] ------Exercise 9.5.Singular values------ input 'p' : 0.2 C = [[0 1 0] [0 1 0] [0 1 0]] The largest singular value of C : 1.73205080757 ------Exercise 9.6: Nearest neighbor------ A = [[ 0.3186692 0.32585018 0.86686889 0.38635469 0.6165809 ] [ 0.51453018 0.6142813 0.40638036 0.55112143 0.56725979] [ 0.32387318 0.72992392 0.40869537 0.08761137 0.92279275]] z = 0.08685816765499976 The closest element in A is : 0.08761137