python numpy与scipy.linalg的应用六题

Generate matrices A , with random Gaussian entries, B , a Toeplitz matrix, where A 2 R n × m and B 2 R m × 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 λ.


Exercise 9.2: Solving a linear system

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


Exercise 9.3: Norms
Compute the Frobenius norm of A : k A k F and the infinity norm of B : k B k 1 . Also find the largest and

smallest singular values of B.


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.


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?


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.

代码

import time
import numpy as np
from scipy import linalg
from scipy.linalg import svdvals
from scipy.linalg import toeplitz
A=np.random.randn(200,500)
z=list(range(0,500))
B=toeplitz(z)

'''
exercise 1

print(A+A)
print(np.dot(A,A.T))
print(np.dot(A.T,A))
print(np.dot(A,B))
def compute(A,B,y):
    I=np.eye(500)
    print(np.dot(A,(B-y*I)))
compute(A,B,2)
'''

'''
exercise 2

b = np.arange(500,0,-1)
x = b/B
print(x)
'''

'''
exercise 3

b = svdvals(B)
print(linalg.norm(A, 'fro'))
print(linalg.norm(B, np.inf))
print("最大奇异值:",max(b))
print("最小奇异值:",min(b))
'''

'''
exercise 4

def ex4():
    Z = np.mat(np.random.rand(200,200))  
    v = np.mat(np.ones(200)) #初始向量  
    n = 0   #迭代次数  
    v = v.T  
    x = v  
    t_ = 0   
    ei = linalg.norm(x,ord=np.inf)  #特征值  
    
    time_ = time.clock()  
    
    while np.abs(t_-ei)>1e-4:  
        t_ = ei  
        x = Z * v  
        ei = linalg.norm(x,ord=np.inf)  
        v = x/ei  
        n = n+1  
    
    time_ = time.clock()-time_ 
    return n, time_, v, ei


result = ex4()
print("iterations:" , result[0])
print("Time: " ,result[1])
print("corresponding eigenvector: " , result[2])
print("eigenvalue: " ,result[3])
'''

'''
exercise 5

def Singular(n,p):
    C=np.random.binomial(1,p,[n,n])
    b=svdvals(C)
    return max(b)
print(Singular(10,0.1))
print(Singular(100,0.1))
print(Singular(1000,0.1))
print("\n")
print(Singular(100,0.1))
print(Singular(100,0.2))
print(Singular(100,0.3))    #最大奇异值很接近np
'''

'''
exercise 6

def exercise6(x): 
    a = min(A[A>x])
    b = max(A[A<=x])
    x = b if abs(a-x) > abs(b-x) else a
    print(x)
exercise6(0.5)
'''

你可能感兴趣的:(python numpy与scipy.linalg的应用六题)