第十一周的作业

  Generate matrices A, with random Gaussian entries, B, a Toeplitz matrix, where A ∈ Rn×m and B ∈ Rm×m, for n = 200, m = 500.
  先按题目要求生成矩阵A和B,下面解题中,若出现A、B则代表是这两个矩阵

import numpy
from scipy.linalg import toeplitz
A = numpy.random.normal(size=(200, 500))
B = toeplitz(range(1, 501))

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 λ.
   实现A(B − λI)函数时,先以B的维数,生成一个单位矩阵。

ans1 = numpy.add(A, A)
ans2 = numpy.dot(A, A.T)
ans3 = numpy.dot(A.T, A)
ans4 = numpy.dot(A, B)


def compute(A, B, r):
    return (A.dot(B - r * numpy.eye(len(B)))) # 先以B的维数,生成一个单位矩阵

这里写图片描述
这里写图片描述
这里写图片描述
Exercise 9.2: Solving a linear system
Generate a vector b with m entries and solve Bx = b.

b = numpy.asmatrix(numpy.random.random_integers(0, 500, (500, 1)))
ans5 = numpy.linalg.solve(B, b)

  所得答案向量规模太大,就不贴图了。

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.

ans6 = numpy.linalg.norm(A)
ans7 = numpy.linalg.norm(B, numpy.inf)
u, s, vh = numpy.linalg.svd(B)
ans8 = s.max()
ans9 = s.min()

这里写图片描述
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.

def powerIteration(Z, e):
    b_k = numpy.asmatrix(numpy.random.rand(Z.shape[1])).T
    b_k_bak = b_k
    iteration = 0
    start_time = time.clock()
    while iteration == 0 or abs(numpy.linalg.norm(b_k - b_k_bak)) >= e:
        b_k1 = numpy.dot(Z, b_k)
        b_k1_norm = numpy.linalg.norm(b_k1)
        b_k_bak = b_k
        b_k = b_k1 / b_k1_norm
        iteration += 1
    end_time = time.clock()
    print("iteration = ", iteration, end='')
    print(", time = ", end_time - start_time, "s.")
    return b_k

Z = numpy.asmatrix(numpy.random.random_integers(1, 100, size=(500, 500)))
print(powerIteration(Z, 10e-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?

n = 500
p_v = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
    for p in p_v:
        C = numpy.random.random(size=[n, n])
        for i in range(n):
            for j in range(n):
                if C[i][j] >= p:
                    C[i][j] = 1
                else:
                    C[i][j] = 0
        v, s, vh = numpy.linalg.svd(C)
        max_singular_value = s.max()
        print("n = %d, p = %f, largest singular value = %f" %
              (n, p, max_singular_value))

这里写图片描述
  由图可得,largest singular value = n * (1 - p)
  
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 nearestNeighbor(A, key):
    index = numpy.argmin(numpy.abs(A - key))
    return A[index // A.shape[1], index % A.shape[1]]

print(nearestNeighbor(A, 0.1))

这里写图片描述

猜你喜欢

转载自blog.csdn.net/m0_37600543/article/details/80399967