【python】第十九次课作业

exercises

Numpy

exercises 9.1

题目:

源代码:

from numpy import *
import numpy as np

def fun(A, B, v, I):
	print("A + A = ")
	C = A + A
	print(C)
	
	print("AAT = ")
	D = A * A.T
	print(D)
	
	print("ATA = ")
	E = A.T * A
	print(E)
	
	print("AB = ")
	F = A * B
	print(F)
	
	print("A(B-vI) = ")
	J = I*v
	B = B - J
	A = A*B
	print(A)
	
I = mat(eye(500, 200, dtype = int))
A = mat(random.randint(10, size = (200, 500)))
B = mat(random.randint(10, size = (500, 200)))
v = int(input("Please enter v = "))
print(fun(A, B, v, I))

完成结果:


exercises 9.2

题目:

源代码:

from numpy import *
import numpy as np

def fun(B, b):
	print("x = ")
	x = B/b
	print(x)
	
B = mat(random.randint(1, 10, size = (500, 200)))
b = mat(random.randint(1, 10, size = (500, 1)))
fun(B, b)

完成结果:


exercise 9.3

题目:

源代码:

from numpy import *
import numpy as np

def fun(A, B):
	print("||A||1 = ")
	print(np.linalg.norm(A, ord=1))
	print("||A||2 = ")
	print(np.linalg.norm(A, ord=2))
	print("||B|| = ")
	print(np.linalg.norm(B, ord=np.inf))
	print("the largest singular value of B is ")
	print(B.max())
	print("the smallest singular value of B is ")
	print(B.min())
	
A = mat(random.randint(1, 10, size = (200, 500)))
B = mat(random.randint(1, 10, size = (500, 200)))
fun(A, B)

完成结果:


exercises 9.4

题目:

源代码:

from numpy import *
import numpy as np

def fun(Z, A, B):
	print("the largest eigenvalue is ")
	print(np.argsort(A)[0])
	print("the largest corresponding eigenvector is ")
	print(np.argsort(B)[0])
	
Z = np.random.random_integers(100, size = (200, 200))	
A,B = np.linalg.eig(Z)
fun(Z, A, B)

完成结果:


exercises 9.5

题目:

源代码:

from numpy import *
import numpy as np

def fun(C, p):
	for i in range(0, 200):
	    for j in range(0, 200):
		    if np.random.rand() < p:
			    C[i][j] = 1
	print("the largest singular vale is ")
	x = np.linalg.svd(C)
	print(x[0])
	
C = np.zeros([200,200])
p = float(input("p = "))
fun(C, p)

完成结果:


exercise 9.6

题目:

源代码:

def fun(z, A, n):
	le = abs(A[0]-z)
	zz = A[0]
	for i in range(0, n):
		if abs(A[i]-z) < le:
			le = abs(A[i]-z)
			zz = A[i]
	print(zz)
	
z = int(input("z = "))
n = int(input("n = "))
A = []
for i in range(0, n):
	data = int(input())
	A.append(data)
fun(z, A, n)

完成结果:


猜你喜欢

转载自blog.csdn.net/karroyzgj/article/details/80394464