Python作业(2018-05-16,第十一周周三)

Introduction to Python Exercise Numpy

Generate matrices A, with random Gaussian entries, B, a Toeplitz matrix, where A ∈Rn×m and B ∈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 λ.

import numpy
n = 200
m = 500
a = numpy.random.normal(size=(n,m))
b = numpy.empty((m,m))
for i in range(m):
	j=0
	k=i
	while j<m and k<m:
		b[j][k] = i
		b[k][j] = i
		j+=1
		k+=1
print("A =")
print(a)
print("B =")
print(b)
print("A+A = ")
print(a+a)
print("A*A.T = ")
print(numpy.dot(a,a.T))
print("A.T*A = ")
print(numpy.dot(a.T,a))
print("A*B")
print(numpy.dot(a,b))
def func(a,b,x):
	i = numpy.identity(m)
	return a.dot(b-x*i)
x = int(input("Enter a constant value x = "))
print("A*(B-x*I) = ")
print(func(a,b,x))

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

import numpy
n = 200
m = 500
A = numpy.random.normal(size=(n,m))
B = numpy.empty((m,m))
for i in range(m):
	j=0
	k=i
	while j<m and k<m:
		B[j][k] = i
		B[k][j] = i
		j+=1
		k+=1
b = numpy.random.normal(size=(500,1))
print("B = ")
print(B)
print("b = ")
print(b)
print("The solution of Bx=b is")
print(numpy.linalg.solve(B,b))
Exercise 9.3: Norms

Compute the Frobenius norm of A: kAkF and the infinity norm of B: kBk∞. Also find the largest and smallest singular values of B.

import numpy
n = 200
m = 500
A = numpy.random.normal(size=(n,m))
B = numpy.empty((m,m))
for i in range(m):
	j=0
	k=i
	while j<m and k<m:
		B[j][k] = i
		B[k][j] = i
		j+=1
		k+=1
print("A = ")
print(A)
print("\nThe Frobenius norm of A is ")
print(numpy.linalg.norm(A,"fro"))
print("\nB = ")
print(B)
print("\nThe infinity norm of B is ")
print(numpy.linalg.norm(B,numpy.inf))
u,s,v = numpy.linalg.svd(B)
max_sgval = min_sgval = s[0]
for i in range(m):
	if max_sgval<s[i]:
		max_sgval=s[i]
	if min_sgval>s[i]:
		min_sgval=s[i]
print("\nThe largest singular value of B is")
print(max_sgval)
print("\nThe smallest singular value of B is")
print(min_sgval)
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 = 1
while(n<=10**10):
	Z = numpy.random.normal(size=(n,n))
	t0 = time.clock()
	w,v = numpy.linalg.eig(Z)
	t1 = time.clock()
	t = t1-t0
	print("\n-----n="+str(n)+"-----")
	print("time = "+str(t))
	max_eig_value = w[0]
	max_i=0
	for i in range(n):
		if max_eig_value<w[i]:
			max_eig_value = w[i]
			max_i = i
	print("max_eig_value = "+str(max_eig_value))
	print("The corresponding eigenvector is")
	print(v[:,i])
	n*=10

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
n=int(input("Enter the matrix's size(0 to quit) n = "))
while n!=0:
	p = float(input("Enter the probability p = "))
	A = numpy.random.binomial(1,p,size=(n,n))
	u,s,v = numpy.linalg.svd(A)
	max_sgval = s[0]
	for i in range(n):
		if s[i]>max_sgval:
			max_sgval = s[i]
	print("The largest singular value is "+str(max_sgval))
	n=int(input("Enter the matrix's size(0 to quit) n = "))
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
def get_closest_value(A,target):
	B = numpy.fabs(A-target)
	min_arg = numpy.argmin(B,axis=1)
	min_j = min_arg[0]
	min_i = 0
	min_value = B[min_i][min_j]
	for i in range(len(min_arg)):
		if B[min_i][min_j]>B[i][min_arg[i]]:
			min_i = i
			min_j = min_arg[i]
	return A[min_i][min_j]
A = numpy.random.normal(size=(10,10))
print("A = ")
print(A)
print ("target = 1")
print("The closest value = "+str(get_closest_value(A,1)))

猜你喜欢

转载自blog.csdn.net/baidu_41300735/article/details/80351129