第十一周作业

原题矩阵规模太大了,这里为了方便截图检验,设置n = 2,m = 5

Exercise 9.1: 

代码:

import numpy as np
from scipy.linalg import toeplitz

def compute(A, B, lamda):
	return np.dot(A, B - lamda * np.eye(B.shape[1]))

A = np.random.randn(2,5)
x = np.random.randn(1,5)
y = np.random.randn(1,5)
B = toeplitz(x, y)
print("Matrix A:")
print(A)
print("Matrix B:")
print(B)
############
print("A+A:")
print(A + A)
print("AAT:")
print(np.dot(A, A.T))
print("ATA:")
print(np.dot(A.T, A))
print("AB:")
print(np.dot(A, B))
print("A(B-I):")
print(compute(A, B, 1))

结果:

Matrix A:
[[-1.39612299  1.96146785  0.09048303  1.27988762  1.105836  ]
 [-0.46515137 -1.04407125  0.5779924   0.1160498   2.09477522]]
Matrix B:
[[ 0.14963695  1.11204777  0.61889539 -0.57087541 -0.18088727]
 [-0.75591586  0.14963695  1.11204777  0.61889539 -0.57087541]
 [ 0.25316756 -0.75591586  0.14963695  1.11204777  0.61889539]
 [-1.90517251  0.25316756 -0.75591586  0.14963695  1.11204777]
 [ 0.56573786 -1.90517251  0.25316756 -0.75591586  0.14963695]]
A+A:
[[-2.79224597  3.9229357   0.18096606  2.55977524  2.21167201]
 [-0.93030275 -2.0881425   1.1559848   0.2320996   4.18955043]]
AAT:
[[8.66568828 1.11880339]
 [1.11880339 6.04207656]]
ATA:
[[ 2.16552519 -2.25279917 -0.39517939 -1.84086125 -2.51827063]
 [-2.25279917  4.9374409  -0.4259857   2.38929416 -0.01803282]
 [-0.39517939 -0.4259857   0.34226239  0.18288401  1.31082355]
 [-1.84086125  2.38929416  0.18288401  1.65157988  1.65844405]
 [-2.51827063 -0.01803282  1.31082355  1.65844405  5.61095648]]
AB:
[[-3.48150228 -3.11022727  0.64320593  1.46717664  0.77755674]
 [ 1.82995388 -5.07194391 -0.91984288 -1.30398071  1.48040008]]
A(B-I):
[[-2.08537929 -5.07169512  0.5527229   0.18728902 -0.32827926]
 [ 2.29510526 -4.02787266 -1.49783528 -1.42003051 -0.61437514]]


都是比较基本的运算调用。

Exercise 9.2: 

代码:

import numpy as np
from scipy.linalg import toeplitz

x = np.random.randn(1,5)
y = np.random.randn(1,5)
B = toeplitz(x, y)
print("Matrix B:")
print(B)
b = np.random.randn(1,5).T
print("vector b:")
print(b)
print("solve Bx = b,x is")
print(np.linalg.solve(B, b))

结果:

Matrix B:
[[ 0.15356159  1.95578204 -0.39315247 -0.58093584  1.33742878]
 [ 0.12695822  0.15356159  1.95578204 -0.39315247 -0.58093584]
 [-1.65568047  0.12695822  0.15356159  1.95578204 -0.39315247]
 [-0.25629207 -1.65568047  0.12695822  0.15356159  1.95578204]
 [-1.46854658 -0.25629207 -1.65568047  0.12695822  0.15356159]]
vector b:
[[ 0.22128892]
 [ 1.74450946]
 [ 0.09007106]
 [-0.22109333]
 [ 0.36026202]]
solve Bx = b,x is
[[-1.17357604]
 [ 0.12608276]
 [ 0.71273766]
 [-1.03671225]
 [-0.12496665]]

调用了solve方法解线性方程组。

Exercise 9.3: 

代码:

import numpy as np
from scipy.linalg import toeplitz

A = np.random.randn(2,5)
x = np.random.randn(1,5)
y = np.random.randn(1,5)
B = toeplitz(x, y)
print("Matrix A:")
print(A)
print("Matrix B:")
print(B)
############
print("the Frobenius norm of A:")
print(np.linalg.norm(A, ord = 'fro'))
print("the infinity norm of B:")
print(np.linalg.norm(B, ord = np.inf))
print("the largest singular value of B:")
print(np.linalg.cond(B, 2))
print("the smallest singular value of B:")
print(np.linalg.cond(B, -2))

结果:

Matrix A:
[[-0.34183582  0.02747836 -2.04670733  0.2444222  -0.07283119]
 [ 1.85484887  0.34124865  0.91227828 -0.96930431 -0.3298667 ]]
Matrix B:
[[ 8.59026858e-02  5.82853753e-01 -1.22317476e+00  3.94876866e-01
   6.18022893e-04]
 [ 1.32168827e+00  8.59026858e-02  5.82853753e-01 -1.22317476e+00
   3.94876866e-01]
 [ 5.44494671e-01  1.32168827e+00  8.59026858e-02  5.82853753e-01
  -1.22317476e+00]
 [ 9.89831759e-02  5.44494671e-01  1.32168827e+00  8.59026858e-02
   5.82853753e-01]
 [ 1.31068730e+00  9.89831759e-02  5.44494671e-01  1.32168827e+00
   8.59026858e-02]]
the Frobenius norm of A:
3.1319632480498907
the infinity norm of B:
3.7581141461500844
the largest singular value of B:
2.963944039156744
the smallest singular value of B:
0.3373882862796915

通过关于numpy.linalg.cond文档说明可以知道关于几个参数的用法。

Exercise 9.4: 

代码:

import numpy as np
import time

def power_iteration(A, emerson = 1e-5):
	x_k = np.random.randn(A.shape[1]).T
	eig_val = np.max(x_k)

	iterations = 0
	s_time = time.clock()
	while True:
		y_k = x_k/eig_val
		x_k1 = np.dot(A, y_k)
		eig_val = np.max(x_k1)
		if np.linalg.norm(x_k - x_k1) < emerson or iterations > 1e5:
			break
		iterations += 1
		x_k = x_k1
	e_time = time.clock()
	if iterations > 1e5:
		print("the result is not convergence.")
	else :
		print("the largest eigenvalue of Z:")
		print(eig_val)
		print("the corresponding eigen vector of Z:")
		print(y_k)
		print("the itrations are:")
		print(iterations)
	return e_time - s_time
		
		
		

Z = np.random.randn(2,2)
print("2X2 Matrix Z:")
print(Z)
tim1 = power_iteration(Z)
Z = np.random.randn(3,3)
print("3X3 Matrix Z:")
print(Z)
tim2 = power_iteration(Z)
print("the differece of tow sizes of matrix using power iteration:")
print(tim2 - tim1)
############



结果:
2X2 Matrix Z:
[[ 1.15460099 -0.635185  ]
 [ 0.18734087  0.3969566 ]]
the largest eigenvalue of Z:
0.9323504802842806
the corresponding eigen vector of Z:
[1.         0.34989887]
the itrations are:
26
3X3 Matrix Z:
[[ 0.56276902  0.28389285 -0.0534136 ]
 [ 0.7729379   0.15439234  1.26399685]
 [ 2.45134567  0.736767   -0.90305878]]
the largest eigenvalue of Z:
1.3021521711443254
the corresponding eigen vector of Z:
[0.3330787  1.         0.70436147]
the itrations are:
143
the differece of tow sizes of matrix using power iteration:
0.0011395646328154182

对于这样得到的矩阵幂迭代法并不总是会收敛,所以有时要试个几次。

对于算出来的最大特征值我用numpy提供的方法试验过了,相差一个判停标准的大小,所以基本可以证明正确性。

Exercise 9.5: 

代码:

import numpy as np


n, p = 5, 0.8
print("n:{}  p:{}".format(n, p))
C = np.array(np.random.binomial(1, p, n*n)).reshape(n, n)
print("The C:")
print(C)
_, vals, _ = np.linalg.svd(C)
print("The singular values of C:\n{}".format(vals))
print("The largest singular value:\n{}".format(np.max(vals)))
print("The n*p is:\n{}".format(n*p))
结果:
n:5  p:0.8
The C:
[[1 1 0 1 1]
 [0 0 1 1 1]
 [1 1 1 1 1]
 [0 0 1 1 0]
 [1 1 1 1 1]]
The singular values of C:
[4.00637949e+00 1.50345894e+00 6.59708279e-01 5.03308663e-01
 5.67223420e-19]
The largest singular value:
4.006379486076943
The n*p is:
4.0


通过svd分解计算奇异值,可以看到最大奇异值约等于n*p

Exercise 9.6: 

代码:

import numpy as np

def nearest_neighbor(A, z):
	index = np.argmin(np.abs(A-z))
	axi0 = index // A.shape[1]
	axi1 = index % A.shape[1]
	return A[axi0][axi1]

z = np.random.randn()
A = np.random.randn(2,5)
print("the matrix A:\n{}".format(A))
print("the value z:\n{}".format(z))
print("the nearest neighbor:\n{}".format(nearest_neighbor(A, z)))

结果:

the matrix A:
[[ 1.37603443 -0.81334734 -0.99669634  0.89441934  1.09195437]
 [-1.40090431  0.52012356  1.96160828 -0.41486128 -0.87353392]]
the value z:
-0.24677498776151868
the nearest neighbor:
-0.41486128459295607

只要将A中每一个元素减去z再求绝对值,最后再调用argmin找到最小那个元素的下标,通过A的size进行转换,即可得到这个元素。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值