python第11周作业——Numpy

Generate matrices A, with random Gaussian entries, B, a Toeplitz matrix, where A Rn×m and B Rm×m,for n = 200, m = 500.

Exercise 9.1: Matrix operations

题目:
Calculate A + A, AA’,A’A and AB. Write a function that computes A(B − λI) for any λ.

代码:

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

n = 200
m = 500
A = np.random.normal(0, 1, (n,m))
c = np.random.rand(m)
r = np.random.rand(m)
B = sl.toeplitz(c,r)
print("A:")
print(A)
print("B:")
print(B)

print("A+A:")
print(A+A)

print("AA':")
print(np.dot(A,A.T))

print("A'A:")
print(np.dot(A.T,A))

print("AB:")
print(np.dot(A,B))

# Write a function that computes A(B − λI) for any λ
def func1(A, B, Lambda):
    C = B - Lambda*(np.eye(m))
    D = np.dot(A,C)
    return D


print("A(B − λI):")
print(func1(A,B,3))     # lambda = 3

Exercise 9.2: Solving a linear system

题目:
Generate a vector b with m entries and solve Bx = b.

代码:

# skip
# 9.2
b = np.random.rand(m,1)
print("x for Bx=b:")
print(np.linalg.solve(B,b))

Exercise 9.3: Norms

题目:
Compute the Frobenius norm of A: ||A||F and the infinity norm of B: ||B||inf. Also find the largest and smallest singular values of B.

代码:

# skip
# 9.3 
print("||A||F:")
print(np.linalg.norm(A,'fro'))
print("||B||inf:")
print(np.linalg.norm(B, np.inf))
print("largest singular value of B:")
print(np.linalg.norm(B,2))
print("smallest singular value of B:")
print(np.linalg.norm(B,-2))

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.

代码:

# skip
# 9.4 
Z = np.random.normal(0, 1, (n,n))
print("Z:")
print(Z)
v = np.ones((n, 1))

def power_iter(v, Z):
    y0 = 1000
    u = v
    num = 0
    while True :
        v = np.dot(Z, u)
        y1 = v[np.argmax(np.abs(v))]
        u = v / y1
        num = num + 1
        if abs(y0-y1)<0.001:
            break
        else:
            y0 = y1
    x1 = u
    return x1, y1, num

begin = time.clock()
x1, y1, num = power_iter(v, Z)
end = time.clock()
print("the largest eigenvalue:")
print(y1)
print("corresponding eigenvector:")
print(x1)
print("iteration times:")
print(num)
print("time:")
print(end - begin)

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?

代码:

# skip
# 9.5
p = 0.5
C = np.random.binomial(1, p, (n, n))
print("C:")
print(C)
U,s,Vh = sl.svd(C)
print("singular values of C:")
print(s)
print("largest singular value of C:")
print(np.linalg.norm(C,2))

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.

代码:

# skip
# 9.6
def func6(A, z):
    return A[np.argmin(np.abs(A-z))]

A = np.random.rand(m)
print("A:")
print(A)
z = 1
print("Nearest neighbor:")
print(func6(A, z))

你可能感兴趣的:(python)