Generate matrices AA, with random Gaussian entries, B, a Toeplitz matrix, where and B∈Rm×mB∈Rm×m, for n=200,m=500n=200,m=500
import numpy
from scipy.linalg import toeplitz
n = 200
m = 500
A = numpy.random.randn(n, m)
r = numpy.random.randint(0, 5, m)
c = numpy.random.randint(0, 5, m)
B = toeplitz(r,c)
print(A)
print(B)
Exercise 9.1: Matrix operations Calculate
A+A,AAT,ATAA+A,AAT,ATA and ABAB. Write a function that computes A(B−λI)A(B−λI) for any λλ.
import numpy
n = 4
m = 3
def calc(A, B, c):
return numpy.dot(A, B - c * numpy.eye(m))
A = numpy.random.normal(size = (n, m))
B = numpy.random.normal(size = (m, m))
print(A, '\n')
print(B, '\n')
print(A + A, '\n')
print(numpy.dot(A, A.T), '\n')
print(numpy.dot(A.T, A), '\n')
print(calc(A, B, 1))
Exercise 9.2: Solving a linear system
Generate a vector b⃗ b→ with mm entries and solve
import numpy
n = 3
B = numpy.random.normal(size = (n, n))
b = numpy.random.normal(size = n)
x = numpy.linalg.solve(B, b)
print(B, '\n')
print(b, '\n')
print(x, '\n')
Exercise 9.3: Norms
Compute the Frobenius norm of A:∥A∥FA:‖A‖F and the infinity norm of BB: . Also find the largest and
smallest singular values of BB.
import numpy
import scipy
n = 2
m = 5
A = numpy.random.normal(size = (n, m))
B = numpy.random.normal(size = (n, m))
aa = 0
for i in range(0, n):
for j in range(0, m):
aa += A[i][j] ** 2
aa = aa ** 0.5
print(aa)
bb = 0
for i in range(0, n):
tmp = 0
for j in range(0, m):
tmp = tmp + numpy.fabs(B[i][j])
bb = max(bb, tmp)
print(bb)
C = scipy.linalg.svdvals(B)
print(C.min())
print(C.max())
Exercise 9.4: Power iteration
Generate a matrix , with Gaussian entries, and use the power iteration to find the largest
eigenvalue and corresponding eigenvector of ZZ. How many iterations are needed till convergence?
Optional: use the time.clock() method to compare computation time when varying n.
import numpy
import time
n = 5
def Max(x):
res = 0
for i in range(0, n):
res = max(res, abs(x[i]))
return res
Z = numpy.random.normal(size = (n, n))
tmp1 = numpy.ones(n, dtype = float)
ttt = time.clock()
r = 1
tmp2 = numpy.dot(Z, tmp1)
tmp2 = tmp2 / Max(tmp1)
while (Max(tmp1 - tmp2) > 1e-6 and r < 1e6):
r += 1
tmp1 = tmp2
tmp2 = numpy.dot(Z, tmp1.T)
tmp2 = tmp2 / Max(tmp1)
print(time.clock() - ttt)
随着 n 从 3 变化到 5 ,时间每次增加 0.8s 左右
Exercise 9.5: Singular values
Generate an matrix, denoted by CC, where each entry is with probability pp and otherwise. Use
the linear algebra library of Scipy to compute the singular values of CC. What can you say about the
relationship between , pp and the largest singular value?
import numpy
import scipy
n = 3
p = 0.5
A_ = numpy.random.rand(n, n)
A = numpy.where(A > p, 0, 1)
C = scipy.linalg.svdvals(A)
print(C.max())
Exercise 9.6: Nearest neighbor
Write a function that takes a value and an array AA and finds the element in that is closest to zz. 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
import scipy
def f(A, z):
A = A - z
B = numpy.abs(A)
x = B.argmin()
return A.flatten()[x] + z
a = numpy.random.randint(0, 10, 4)
print(a)
print(f(a, 2))