第一题
import numpy from scipy.linalg import toeplitz import time def func1(A,B,_lambda): C = B - _lambda * (numpy.eye(m)) print("A*(B-λI) : ",numpy.dot(A, C)) def Matrix_operations(A,B): print("A+A : ",A+A) print("A*AT : ",numpy.dot(A,A.T)) print("AT*A : ",numpy.dot(A.T,A)) print("A*B : ",numpy.dot(A,B)) func1(A,B,2.0) n, m = 200, 500
mu,sigma=0,1.0
A = numpy.random.normal(loc=0.0, scale=1.0, size=(n, m)) c = [a for a in range(1, m+1)] B = toeplitz(c, c) Matrix_operations(A,B)
第二题
import numpy from scipy.linalg import toeplitz import time def Solving_a_linear_system(B, m): b = numpy.ones((m, 1)) print(numpy.linalg.solve(B, b)) n, m = 200, 500
mu,sigma=0,1.0 A = numpy.random.normal(loc=0.0, scale=1.0, size=(n, m)) c = [a for a in range(1, m+1)] B = toeplitz(c, c) Solving_a_linear_system(B,m)
第三题
import numpy from scipy.linalg import toeplitz import time def Norms(A, B): print("Frobenius", numpy.linalg.norm(A, 'fro')) print("infinity", numpy.linalg.norm(B, numpy.inf)) print("largest", numpy.linalg.norm(B, 2)) print("smallest", numpy.linalg.norm(B, -2)) n, m = 200, 500
mu,sigma=0,1.0 A = numpy.random.normal(loc=0.0, scale=1.0, size=(n, m)) c = [a for a in range(1, m+1)] B = toeplitz(c, c) Norms(A,B)
第四题
import numpy from scipy.linalg import toeplitz import time def Power_iteration(n): Z = numpy.random.standard_normal((n, n)) begin = time.clock() num = 0 cev = numpy.ones(n) evalue = 0 vec = numpy.zeros(n) while 1: num += 1 vec= numpy.dot(Z, cev) tmp = evalue evalue = numpy.linalg.norm(vec) cev = vec / evalue if(abs(tmp - evalue) < 0.0005): break; end = time.clock() print("The number of iterations:", num) print("time:", end-begin) n, m = 200, 500
mu,sigma=0,1.0 A = numpy.random.normal(loc=0.0, scale=1.0, size=(n, m)) c = [a for a in range(1, m+1)] B = toeplitz(c, c) Power_iteration(n)
第五题
import numpy from scipy.linalg import toeplitz import time def Singular_values(n): C = numpy.random. binomial(1, 0.5, (n, n)) print("smallest", numpy.linalg.norm(C, -2)) print("largest", numpy.linalg.norm(C, 2)) print("largest singular is approximately equal to n * p") n, m = 200, 500
mu,sigma=0,1.0 A = numpy.random.normal(loc=0.0, scale=1.0, size=(n, m)) c = [a for a in range(1, m+1)] B = toeplitz(c, c) Singular_values(n)
第六题
import numpy from scipy.linalg import toeplitz import time def Nearest_neighbor(A, z): B = A[A>z] C = A[A<=z] l = numpy.argmin(B) r = numpy.argmax(C) if(abs(B[l]-z) < abs(C[r]-z)): print(B[l]) else: print(C[r]) n, m = 200, 500
mu,sigma=0,1.0 A = numpy.random.normal(loc=0.0, scale=1.0, size=(n, m)) c = [a for a in range(1, m+1)] B = toeplitz(c, c) Nearest_neighbor(A,1)