python作业——numpy

这篇博客介绍了使用numpy进行矩阵操作的练习,包括生成高斯随机矩阵、Toeplitz矩阵,执行矩阵加法、乘法、求逆及特征值等线性代数运算。同时,讨论了求解线性系统、计算范数、找到最近邻元素的方法,并探讨了矩阵奇异值与概率p之间的关系。
摘要由CSDN通过智能技术生成

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

solution:

import numpy
import random
from scipy.linalg import toeplitz
import scipy
#####9.1
A= numpy.random.randint(0, 10, (200,500))
c = numpy.random.randint(0, 10, (500, 1))
r = numpy.random.randint(0, 10, (1, 500))
B = toeplitz(c, r)

print('A:', A)
print('B:', B)

#A+A
print(A + A)
#AA(T)
print(numpy.dot(A, A.T))
#A(T)A
print(numpy.dot(A.T, A))
#AB
print(numpy.dot(A,B))
#A(B − λI)
def fun1(A, B, tmp):
    return numpy.dot(A, B - tmp* numpy.ones(dtype=int, shape = B.shape))
print(fun1(A, B, 1))

Exercise 9.2

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

solution:

#####9.2
b = numpy.random.randint(0, 10, 500)
print(b)
BI = numpy.mat(B).I
print(BI)
print(numpy.dot(b, BI))

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.

solution:

#####9.4 power_iteration

n = input('input n: ')
n = int(n)
Z = numpy.random.randint(0, 10, (n, n))
def power_iteration(Z):
    time = 0
    b = numpy.random.randint(Z.shape[1])
    while True:
        b1 = numpy.dot(Z, b)
        norm = numpy.linalg.norm(b1)
        b = b1 / norm
        norm2 = numpy.linalg.norm(b)
        if abs(norm - norm2) / norm <= 0.00001:
            break
        time += 1
    print('iteration time:', time)
    print(b)

power_iteration(Z)

Exercise 9.3

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

solution:

#####9.3
print(numpy.linalg.norm(A))
print(numpy.linalg.norm(B, ord=numpy.inf))
print('largest:', numpy.linalg.norm(B, ord=2))
print('smallest:', numpy.linalg.norm(B, ord=-2))

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?

solution:

 
#####9.5 Singular values
n = int(input('input n: '))

#C = numpy.zeros((n,n))
C = []
p = float(input('input P: '))
for i in range(0, n):
    tmp = []
    for j in range(0, n):
        if random.uniform(0, 1) > p:
            tmp.append(0)
        else:
            tmp.append(1)
    C.append(tmp)

U, sigma, VT = scipy.linalg.svd(C)
print(sigma)

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.
#####9.6Nearest neighbor
AA = numpy.random.randint(0, 10, 10)
print(AA,len(AA))
print(AA)
z = int(input('input z: '))
BB = [abs(i - z) for i in AA]

print(BB)
print('num', AA[numpy.argmin(B)])



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值