Generate matrices A, with random Gaussian entries, B, a Toeplitz matrix, where A∈ R^n x m, and B ∈ R^m x m, for n = 200, m = 500
首先使用numpy.random.mormal生成A
函数语法:
numpy.random.normal(loc, scale, size)
loc : 概率分布的均值,对应着分布中心
scale : 概率分布的标准差,对应分布的宽度(scale越大越矮胖, 越小越高瘦)
size : 输出的空间大小
使用scipy.linalg.toeplitz生成B
import numpy from scipy.linalg import toeplitz import time A = numpy.random.normal(size = (200, 500)) print('A = ') print(A) B = toeplitz(range(1, 501)) print('\nB = ') print(B)
9-1
Exercise 9.1: Matrix operations
Calculate A + A, A AT ,AT A and A B. Write a function that computes A(B − λI) for any λ.
print('A + A = ') print(A + A) print('\nAA^T = ') print(A.dot(A.T)) print('\nA^TA = ') print(A.T.dot(A)) print('\nAB = ') print(A.dot(B)) def calcul(A, B, linda): input() tmp = B - linda * numpy.eye(500) return numpy.dot(A, tmp)
9-2
Exercise 9.2: Solving a linear system
Generate a vector b with m entries and solve Bx = b.
b = numpy.array(range(1, 501)) print('b = ') print(b) x = numpy.linalg.solve(B, b) print('\nx = ') print(x)
9-3
Exercise 9.3: Norms
Compute the Frobenius norm of A: kAk F and the infinity norm of B: kBk ∞ . Also find the largest and smallest singular values of B.
from numpy import linalg print('|A|f = ', linalg.norm(A, ord=2)) print('|B| = ', linalg.norm(B, ord=numpy.inf), '\n') sigma = linalg.eigvals(B) print('largest = ', numpy.max(sigma), '\nsmallest:', numpy.min(sigma))
9-4
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.
print('\n----------------------9.4----------------------') def power_iter(n, Z): esp = 1e-5 begin = time.clock() iter = 0 u = v = numpy.ones([n , 1]) #ones()返回一个全1数组 lam = iter_counter = 0 while(1): v = numpy.matmul(Z, u) pre = lam lam = v.max() u = v / lam if(abs(pre - lam) < esp): break iter_counter += 1 end = time.clock() time_used = end - begin return lam, iter_counter, time_used n = [100, 150, 200] for ni in n: Z = numpy.random.normal(size = (ni, ni)) lam, iter_counter, time_used = power_iter(ni, Z) print('when n = ', ni, ': ') print('Z = ', Z) print('lambda = ', lam) print('iter = ', iter_counter) print('time = ', time_used, '\n')
9-5
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?
print('\n----------------------9.5----------------------') def singular_value(n , p): C = numpy.random.binomial(1, p, (n, n)) u, sigma, v = linalg.svd(C) return sigma for n in [10, 20]: for p in [0.2, 0.3, 0.5]: sigma = singular_value(n, p) print('n = ', n, ', p = ', p) print('The largest singular value is: \n', sigma, '\n')
9-6
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 nearest_neighbor(A, z): B = numpy.abs(A - z) index = numpy.argmin(B) return A[index] A = numpy.array(range(1, 100)) print('The closest value is: ', nearest_neighbor(A, 2))