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

Generate matrices A A , with random Gaussian entries, B, a Toeplitz matrix, where ARn×m and BRm×m B ∈ R m × m , for n=200,m=500 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,AAT,ATA A + A , A A T , A T A and AB A B . 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 m m 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:AF A : ‖ A ‖ F and the infinity norm of B B : B. Also find the largest and
smallest singular values of B 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 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 C , where each entry is 1 with probability p p and 0 otherwise. Use
the linear algebra library of Scipy to compute the singular values of C C . What can you say about the
relationship between n, p 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 A and finds the element in A that is closest to z z <script type="math/tex" id="MathJax-Element-1068">z</script>. 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))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值