第十一周作业——高级编程技术

· Set matrices A,with random Gaussian entries B,a Toeplitz matrix, n = 200, m = 500

import time

import numpy as np  

from scipy.linalg import toeplitz  

n = 200
m = 500
A = np.random.normal(0,1.0,100000)
A = A.reshape(n, m)

B = toeplitz(list(range(1, m + 1)))

1.Exercise 9.1,Matrix Operation

        Calculate A + A, AAT, ATA and AB, write a function that computes A(B- λI) for any λ.

result:

第十一周作业——高级编程技术

第十一周作业——高级编程技术

solution:

def Calculate(A, B, n, m):  

    #A + A  
    print("A + A")  
    C = A + A  
    print(C)           
    #A A^  
    print("A A^")  
    print(np.dot(A, A.T))       
    # A^ A  
    print("A^ A")  
    print(np.dot(A.T, A)) 
    # AB  
    print("AB")  
    print(np.dot(A, B))     
def functionLam(A,B,lam):
    print("A(B − λI)") 
    C = B - lam*np.eye(500)
    return [email protected] 

print(Calculate(A, B, n, m))
print(functionLam(A, B, 5.0))

2.Exercise 9.2,Solving a linear system

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

result:

第十一周作业——高级编程技术

solution:

def solveLinSys(A, B, n, m):  
    b = np.ones((m, 1))  
    x = np.linalg.solve(B, b)  
    print(x)  

print(solveLinSys(A, B, n, m))

3.Exercise 9.3,Norms

    Compute the Frobenius norm of A: ||A||f and the infinity norm of B: ||B||∞. Also find the Largest  and smallest singular values of B.

result:

第十一周作业——高级编程技术

solution:

def Norms(A, B, n, m):  
    Af = np.linalg.norm(A, 'fro')    
    Bf = np.linalg.norm(B, np.inf)  
    larSin = np.linalg.norm(B, 2)  
    smaSin = np.linalg.norm(B, -2)
    print("The Frobenius norm:", Af) 
    print("The infinity norm:", Bf)  
    print("The largest singular:", larSin)  
    print("The smallest singular:", smaSin)
print(Norms(A, B, n, m)) 

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?

result:

第十一周作业——高级编程技术

solution:

def powIte(A, B, n, m):   
    Z = np.random.standard_normal((n, n))   
    num = 0  
    corEig = np.ones(n)  
    larEigNorm = 0  
    larEig = np.zeros(n)  
      
    begin = time.clock()  
    while():  
        larEig = np.dot(Z, corEig)  
        larEigNormTemp = larEigNorm  
        larEigNorm = np.linalg.norm(larEig)  
        corEig = larEig / larEigNorm  
        num += 1  
        if(abs(larEigNormTemp - larEigNorm) < 0.0005):  
            break;
    end = time.clock()  
    print("The largest eigenvalue:", larEigNorm)  
    print("The corresponding eigenvector:", corEig)    
    print("The number of iterations:", num)  

    print("computation time when varying n:", end-begin)  

print(powIte(A, B, n, m))

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 algebre library of Scipy to compute the singular values of C. What can you say about the relationship between n, p and largest singular value?

result:

第十一周作业——高级编程技术

solution:

def sinVal(A, B, n, m):   
    p = 0.5  
    C = np.random. binomial(1, p, (n, n))  
    larSin = np.linalg.norm(C, 2)  
    smaSin = np.linalg.norm(C, -2)  
    print("The smallest singular:", smaSin)  
    print("The largest singular:", larSin)  
    print("n * p:", n*p)  
    print("The largest singular is closed with n * p \n They are equal!") 

print(sinVal(A, B, n, m))

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.

result:

第十一周作业——高级编程技术

solution:

def nearestNeighbor(A, B, n, m):  
    z = -5  
    clo = 0     
    ceil = 0
    floor = 0 
    B, C = A[A>z], A[A<=z]
    if(len(B)):  
        ceil = np.argmin(B)  
    else:  
        clo = C[np.argmax(C)]  
    if(len(C)):  
        floor = np.argmax(C)  
    else:  
        clo = B[ceil]        
    if(abs(B[ceil]-z) >= abs(C[floor]-z)):
        clo = C[floor] 
    else:  
        clo = B[ceil] 

    print("the closest value:", clo) 

print(nearestNeighbor(A, B, n, m))

备注: 有一些知识没有学到过,所以不太懂,不明白的地方差不多是网上找资料以及问同学比如数值那块要用到的知识点,才勉强做完。