Python Numpy 部分习题

Numpy 习题实战


代码文件

import numpy as np
from scipy.linalg import toeplitz

def matAddMat(A):
    return A+A

def matMultMatT(A):
    return A*A.T

def matTMuliMat(A):
    return A.T*A

def matMultmat(A, B):
    return A*B

def  computes(A, B, lada, m):
    return A*(B-lada*np.eye(m))

print("n = 4, m = 5")
n, m = 4, 5
print("The Random matrix A(n*m)")
A = np.mat(np.random.rand(n, m))
print(A)
print("The Teplitz matrix B(m*m)")
B = toeplitz(np.random.rand(1, m), np.random.rand(1, m))
print(B)

print("\nExercise 9.1.Matrix operations")
print("A+A:\n", matAddMat(A))
print("A*A^T:\n", matMultMatT(A))
print("A^TA:\n", matTMuliMat(A))
print("A*B:\n", matMultmat(A, B))
print("lada = 3, A(B-lada*I):\n", computes(A, B, 3, m))

def solut(B, b):
    return np.linalg.inv(B) * b

print("\nExercise 9.2.Solving a linear system")
b = np.array(np.random.rand(m, 1))
print("solut Bx = b: x = inv(B)*b:\n", solut(B, b))

print("\nExercise 9.3.Norms")
print("The Frobenius norm of A: ||A||F:", np.linalg.norm(A))
print("The infinity norm of B: ||B||∞ ", np.linalg.norm(B, np.inf))
print("The largest singular values of B:", np.linalg.norm(B, 2))
print("The smallest singular values of B:", np.linalg.norm(B, -2))

print("\nExercise 9.4.Power iteration")
Z = np.mat(np.random.rand(n, n))
print("Z:\n", Z)
w, v = np.linalg.eig(Z)
print("w:\n", w)
print("v:\n", v)

print("\nExercise 9.5.Singular values")
p = 0.6
C = np.mat(np.random.binomial(1, p, n*n))
C.resize(n, n)
print("p =", p , "\nC:\n", C)

print("The largest singular values of C:", np.linalg.norm(C, 2))
print("The smallest singular values of C:", np.linalg.norm(C, -2))

输出结果

n = 4, m = 5
The Random matrix A(n*m)
[[0.34880707 0.74755034 0.75285761 0.44950543 0.77465029]
 [0.94112227 0.75169149 0.88621216 0.3258467  0.09252789]
 [0.17883843 0.49219086 0.96409376 0.50459647 0.68688119]
 [0.57079547 0.93777532 0.66795402 0.69628784 0.48476601]]
The Teplitz matrix B(m*m)
[[0.96359366 0.33807915 0.11801527 0.91040076 0.69861167]
 [0.71489423 0.96359366 0.33807915 0.11801527 0.91040076]
 [0.85142028 0.71489423 0.96359366 0.33807915 0.11801527]
 [0.59776819 0.85142028 0.71489423 0.96359366 0.33807915]
 [0.52210721 0.59776819 0.85142028 0.71489423 0.96359366]]

Exercise 9.1.Matrix operations
A+A:
 [[0.69761414 1.49510067 1.50571522 0.89901085 1.54930058]
 [1.88224454 1.50338298 1.77242431 0.6516934  0.18505579]
 [0.35767687 0.98438171 1.92818752 1.00919294 1.37376239]
 [1.14159095 1.87555065 1.33590804 1.39257569 0.96953202]]
A*A^T:
 [[2.04943066 1.77553551 1.91505445 2.09151532]
 [1.77553551 2.35086069 1.62065289 2.10579251]
 [1.91505445 1.62065289 1.93013518 1.89193596]
 [2.09151532 2.10579251 1.89193596 2.37120745]]
A^TA:
 [[1.36516815 1.591485   1.6503182  0.95113145 0.75682656]
 [1.591485   2.245546   2.32986603 1.48228345 1.44132076]
 [1.6503182  2.32986603 2.72780593 1.57874946 1.65121999]
 [0.95113145 1.48228345 1.57874946 1.04766556 1.06249392]
 [0.75682656 1.44132076 1.65121999 1.06249392 1.31544834]]
A*B:
 [[2.18467649 2.22225206 2.00024637 1.64723564 1.9115163 ]
 [2.44186864 2.00878965 1.53087249 1.62525083 1.64572734]
 [2.00529831 2.064178   2.06205745 1.52411404 1.51927524]
 [2.45845322 2.45673617 1.9385529  1.87364075 2.03386238]]
lada = 3, A(B-lada*I):
 [[ 1.13825528 -0.02039895 -0.25832647  0.29871936 -0.41243457]
 [-0.38149817 -0.24628482 -1.12776398  0.64771073  1.36814366]
 [ 1.46878301  0.58760543 -0.83022383  0.01032464 -0.54136834]
 [ 0.7460668  -0.3565898  -0.06530916 -0.21522278  0.57956435]]

Exercise 9.2.Solving a linear system
solut Bx = b: x = inv(B)*b:
 [[ 0.84345228  0.02000342  0.86534343 -0.75249117 -0.47237639]
 [-0.00306382  0.00461153 -0.00221787  0.00607134 -0.00399418]
 [-0.20442002 -0.2541809   0.3299019  -0.20228036  0.4189214 ]
 [ 0.09925175 -0.17546299 -0.18908219  0.3128745   0.00720371]
 [ 0.06049773  0.23922568 -0.3665226  -0.50102417  0.73211936]]

Exercise 9.3.Norms
The Frobenius norm of A: ||A||F: 2.949853213736023
The infinity norm of B: ||B||∞  3.649783574568348
The largest singular values of B: 3.254445929659465
The smallest singular values of B: 0.37529659032906065

Exercise 9.4.Power iteration
Z:
 [[0.19580959 0.62569742 0.1304203  0.24313678]
 [0.50875127 0.28674366 0.04752893 0.50793588]
 [0.04684295 0.0980313  0.46953485 0.08283807]
 [0.54956075 0.9420632  0.36713555 0.22525138]]
w:
 [ 1.3779901  -0.46955142 -0.16516533  0.43406614]
v:
 [[ 0.44546951  0.42825742 -0.78074164  0.17340785]
 [ 0.53960893 -0.68432119  0.23825341  0.26661281]
 [ 0.14498712 -0.00198509 -0.05423655 -0.94804476]
 [ 0.69954117  0.59016621  0.5750967  -0.00764636]]

Exercise 9.5.Singular values
p = 0.6
C:
 [[0 1 0 0]
 [0 1 0 1]
 [1 0 1 1]
 [1 0 1 1]]
The largest singular values of C: 2.5430021603128266
The smallest singular values of C: 0.0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值