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

本文通过多个实例演示了如何使用Python进行矩阵运算,包括加法、乘法、求解线性方程组等,并探讨了矩阵范数、特征值及奇异值的计算方法。

Generate matrices AA, with random Gaussian entries, B, a Toeplitz matrix, where ARn×m and BRm×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 Bx=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:AFA:‖A‖F and the infinity norm of BB: B. 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 Z,n×n, 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 n×n matrix, denoted by CC, where each entry is 1 with probability pp and 0 otherwise. Use
the linear algebra library of Scipy to compute the singular values of CC. What can you say about the
relationship between n, 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 z and an array AA and finds the element in A 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))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值