高级编程技术第十四次作业 numpy

Generate matrices A , with random Gaussian entries, B, a Toeplitz matrix, where A R n × m and B R m × m , for n = 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 , A A T , A T A and A B . Write a function that computes 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 with m entries and solve B x = b

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 F and the infinity norm of B : B . Also find the largest and
smallest singular values of B .

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 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
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 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
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 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
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))

猜你喜欢

转载自blog.csdn.net/li_y21/article/details/80962414