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
from scipy.linalg import toeplitz
A = np.mat(np.random.randn(200, 500))
B = np.mat(toeplitz([np.random.randint(0,50) for i in range(500)]))
print("A = ")
print(A)
print("B = ")
print(B)
print("A + A = ")
print(A + A)
print("A * A^T = ")
print(A * (A.T))
print("A^T * A = ")
print(A.T * A)
print("A * B = ")
print(A * B)
t = input("Please enter t:")
t = (float)(t)
for i in range(500):
B[i, i] = B[i , i] - t
print("A * (B - tI) = ")
print(A * B)
运行结果:
Exercise 9.2:
题目描述:Solving a linear system Generate a vector b with m entries and solve Bx = b.
代码:
import numpy as np
from scipy.linalg import toeplitz
B = np.mat(toeplitz([np.random.randint(0,50) for i in range(500)]))
b = np.mat(np.random.randint(50,size=(500,1)))
x = np.linalg.solve(B, b)
print("B = ")
print(B)
print("b = ")
print(b)
print("Solution x = ")
print(x)
运行结果:
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.
代码:
import numpy as np
from scipy import linalg
from scipy.linalg import toeplitz
A = np.mat(np.random.randn(200, 500))
B = np.mat(toeplitz([np.random.randint(0,50) for i in range(500)]))
print("A = ")
print(A)
print("B = ")
print(B)
print("the Frobenius norm of A: ",end="")
sum = 0
for i in range(200):
for j in range(500):
sum += A[i, j] ** 2
print(np.sqrt(sum))
print("the infinity norm of A: ",end="")
max1 = 0
for i in range(200):
sum = 0
for j in range(300):
sum += np.fabs(A[i, j])
if sum > max1:
max1 = sum
print(max1)
b = linalg.svdvals(B)
print("Max singular values: ", end="")
print(max(b))
print("Min singular values: ", end="")
print(min(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.
代码:
import numpy as np
from scipy.linalg import toeplitz
import time
Z = np.mat(np.random.randn(200, 200))
x = np.mat(np.ones(200))
x = x.T #first eigenvector
max1 = 0
last = max(x) #first eigenvalue
n = 0 # iterations times
t = time.clock() # computation time
while np.fabs(max1 - last) > 0.00001:
last = max(x)
y = x / last
x = Z * y
n = n + 1
max1 = max(x)
t = time.clock() - t
print("largest eigenvalue: ", end="")
print(max1)
print("corresponding eigenvector: ", end="")
print(y)
print("iterations times: ", end="")
print(n)
print("computation time: ", end="")
print(t)
运行结果:
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?
代码:
import numpy as np
from scipy.linalg import toeplitz
from scipy.linalg import eigvals
import random
def deal(n, p):
#generate C
C = []
for i in range(n):
newLine = []
for j in range(n):
if np.random.random() <= p:
newLine.append(1)
else:
newLine.append(0)
C.append(newLine)
#compute the singular values
x = eigvals(np.mat(C))
return max(x)
for i in range(10):
n = random.randint(1,100)
p = random.random()
print("n = ",end="")
print(n)
print("p = ", end="")
print(p)
print("the largest singular value = ", end="")
print(deal(n, 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.
代码:
import numpy as np
from scipy.linalg import toeplitz
from scipy.linalg import eigvals
A = np.random.randn(50, 50)
z = np.random.random()
min = 10000
num = 0
for i in range(50):
for j in range(50):
if np.fabs(A[i, j] - z) < min:
min = np.fabs(i - j)
num = A[i, j]
print("A is ", end="")
print(A)
print("z is %f" % z)
print("nearest neighbor is %f" % num)
运行结果: