第十一章作业(Numpy)

9 Numpy
Generate matrices A, with random Gaussian entries, B, a Toeplitz matrix, where A 2 Rn×m and B 2 Rm×m,
for n = 200, m = 500.


Exercise 9.1: Matrix operations
Calculate A + A, AA>; A>A and AB. Write a function that computes A(B − λI) for any λ.


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


Exercise 9.3: Norms
Compute the Frobenius norm of A: kAkF and the infinity norm of B: kBk1. Also find the largest and
smallest singular values of B.


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.

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?


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 as np 
import scipy.linalg
import time
import random
import math
n = 200
m = 500
print("A: ")
A = np.random.normal(3,1,(n, m))
print(A)
print("B: ")
B = scipy.linalg.toeplitz(np.arange(1, m + 1))
print(B)

#Exercise 9.1
print("A + A: ")
print(A + A)

print("AA^T: ")
print(np.dot(A, A.T))

print("A^TA: ")
print(np.dot(A.T, A))

print("AB: ")
print(np.dot(A, B))

def func1(A, B , x = 2):
	I = np.eye(m, m)
	return np.dot(A, B - x * I)

print("fuct: ")
print(func1(A, B))

#Exercise 9.2
print("solve: ")
b = np.arange(10 , 10 + m)
print(np.linalg.solve(B, b))

#Exercise 9.3
print("A(F): ")
print(np.linalg.norm(A))

print("B(inf): ")
print(np.linalg.norm(B, np.inf))

a, b ,c = scipy.linalg.svd(B)
print("smallest singular values of B: ")
print(b.min())

# Exercise 9.4
Z = np.random.normal(3,1, (n, n))
a, b = np.linalg.eig(Z)
print("The largest eigenvalue: ")
print(a.max())
print("The eigenvector: ")
print(b)
print("CPU time: ")
print(time.clock())

#Exercise 9.5
C = np.zeros((n, n))
p = 0.7
for i in range(0, n):
	for j in range(0, n):
		temp = random.uniform(0, 1)
		if temp <= 0.7:
			C[i][j] = 1
a, b , c = scipy.linalg.svd(C)
print("The largest singular values:")
print(b.max())
print("largest singular value = np")


#Exercise 9.6
def func2(A, z):
	sum = np.inf
	for i in A.flat:
		if(abs(i - z) < sum):
			sum = abs(i - z)
	return z + sum
print("The nearest neighbor: ")
print(func2(A, 5))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值