Python-Numpy(第十二周作业)

Generate matrices A, with random Gaussian entries, B, a Toeplitz matrix, where ARnm A ∈ R n ∗ m and BRmm B ∈ R m ∗ m , for n=200 n = 200 , m=500 m = 500 .

import numpy as np
import scipy.linalg as sy
import time

# Prepare
print("Prepare")
n = 200
m = 500
A = np.asmatrix(np.random.normal(0, 1, (n, m)))
B = sy.toeplitz(range(m))

print("A:")
print(A)
print("B:")
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 λ λ .

# Exe 9.1
print("Exe 9.1")
print('A+A=:')
print(A + A)
print("A*A'=:")
print(A * A.T)
print("A'*A=:")
print(A.T * A)


def cal(lamb_da):
    return A * (B - lamb_da * np.eye(m, m))


lam = input("input lambda:")
print(cal(int(lam)))


Exercise 9.2: Solving a linear system
Generate a vector b with m entries and solve Bx=b.

# Exe 9.2
print("Exe 9.2")
b = np.asmatrix(np.random.random_integers(0, m, (m, 1)))
print(np.linalg.solve(B, b))


Exercise 9.3: Norms
Compute the Frobenius norm of A:||A||F A : | | A | | F and the in nity norm of B:||B|| B : | | B | | ∞ . Also nd the largest and smallest singular values of B.

# Exe 9.3
print("Exe 9.3")
U, S, VT = np.linalg.svd(B)
print(np.linalg.norm(A))
print(np.linalg.norm(B, np.inf))
print(S.max(), S.min())


Exercise 9.4: Power iteration
Generate a matrix Z,nn Z , n ∗ n , with Gaussian entries, and use the power iteration to nd 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.

# Exe 9.4
print("Exe 9.4")
while n < 2400:
    Z = np.asmatrix(np.random.normal(10, 0.1, (n, n)))
    beg = time.clock()
    eigvals, eigvts = np.linalg.eig(Z)
    end = time.clock()
    eigvts = eigvts.T
    iter = zip(eigvals, eigvts)
    maximum = eigvals[0], eigvts[0]
    for item in iter:
        if item[0] > maximum[0]: maximum = item
    print("largest eigenvalue: ", maximum[0])
    print("corresponding eigenvector: ", maximum[1])
    print("n: " + str(n) + " time: " + str(end - beg))
    n += 200


Exercise 9.5: Singular values
Generate an nn 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?

# Exe 9.5
print("Exe 9.5")
for i in range(10):
    n = np.random.randint(200, 600)
    p = np.random.random()
    C = np.asmatrix(np.random.binomial(1, p, (n, n)))
    print("n: ", n, "p: ", p, "largest: ", n * p, sy.svd(C)[1].max())


结论:p ≈ largest singular value
Exercise 9.6: Nearest neighbor
Write a function that takes a value z and an array A and nds 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 nd this value manually. In particular, use brackets and argmin.

# Exe 9.6
print("Exe 9.6")
for i in range(10):
    n = np.random.randint(200, 600)
    A = np.random.normal(n, 12, (n, n))
    z = n
    A = np.fabs(A - np.asarray([np.ones(n) * z for j in range(n)]))
    index = np.argmin(A)
    print(z, A[index // n, index % n] + z)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值