首先,生成一个服从标准正太分布的200*500矩阵A,并生成一个500*500的teoplitz矩阵B,标准正太分布矩阵可通过输出均值和方差来确定。
import numpy as np
import time
seed = np.int64(time.time())
print(seed)
np.random.seed(seed)
A = np.random.normal(size=[200, 50])
print(np.mean(A))
print(np.var(A))
def toeplitz(X, Y):
matrix = np.zeros([len(X), len(Y)])
for i in range(len(X)):
for j in range(len(Y) - i):
matrix[j][j+i] = Y[i]
for j in range(i, len(X)):
matrix[j][j-i] = X[i]
return matrix
X = np.random.normal(size=[500])
Y = np.random.normal(size=[500])
B = toeplitz(X, Y)
print(B)
1.
Matrix operationsCalculate A + A, AAT; ATA and AB. Write a function that computes A(B - λI) for any λ.
A_add_A = A + A
A_mul_AT = np.dot(A, A.T)
AT_mul_A = np.dot(A.T, A)
A_mul_B = np.dot(A, B)
def A_B_I(A, B, i):
C= B - i * (np.eye(m))
return np.dot(A,C)
2.
Solving a linear systemGenerate a vector b with m entries and solve Bx = b
def solve(B):
b = np.ones((m, 1))
x = np.linalg.solve(B, b)
return x
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 .
def norm_and_singularity(A, B):
AF = np.linalg.norm(A, 'fro')
print(AF)
BF = np.linalg.norm(B, np.inf)
print(BF)
large= np.linalg.norm(B, 2)
small = np.linalg.norm(B, -2)
print("the largest singular:", large)
print("the smallest singular:", small)
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?
- def eigen(A, B):
- Z = np.random.standard_normal((200, 200))
- vector = np.ones(200)
- value = 0
- k = np.zeros(200)
- temp = 0
- while(True):
- k = np.dot(Z, vector)
- value_t = value
- value = np.linalg.norm(k)
- vector = k / value
- temp ++
- if(abs(value_t - value) < 0.005):
- break;
- print("the largest eigenvalue is:", value)
- print("the corresponding eigenvector is:", vector)
- print("The number of iterations is:", temp)
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?
def singular(A, B):
p = 0.5
C = np.random. binomial(1, p, (200, 200))
large = np.linalg.norm(C, 2)
small = np.linalg.norm(C, -2)
print("the largest singular is:", large)
print("the smallest singular:", small)
print("n * p:", n*p)
print("the largest singular is equal to n*p.")
6.
Nearest neighborWrite 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(A, B):
z = 2
B, C = A[A>z], A[A<=z]
ceil, floor = 0, 0
if(len(B)):
ceil = np.argmin(B)
else:
closest= C[np.argmax(C)]
if(len(C)):
floor = np.argmax(C)
else:
closest= B[ceil]
if(abs(B[ceil]-z) < abs(C[floor]-z)):
closest= B[ceil]
else:
closest= C[floor]
print("the closest value is:", closest)